tModLoader Hotbar Swapper

Poofless

Terrarian
Yea the source code am a nub with mods and me barely now programming but I found this mod-ish thing and maybe I can add it to the mod-ish thing to there like TmodLoader(anyway it's a long story :p) again you don't have to and it's not that important it's up to you
 

Solo-Ion

Dungeon Spirit
Yea the source code am a nub with mods and me barely now programming but I found this mod-ish thing and maybe I can add it to the mod-ish thing to there like TmodLoader(anyway it's a long story :p) again you don't have to and it's not that important it's up to you
I guess it's not such a big thing to share the code for item swapping here.
public class MPlayer : ModPlayer
{
//some of the following was copied from https://msdn.microsoft.com/en-us/library/bb203902.aspx
//the following was useful for inventory swapping http://stackoverflow.com/questions/2094239/swap-two-items-in-listt
//I got the sound ID's from here: http://dev.willhuxtable.com/ids/#sound
static KeyboardState oldState = Keyboard.GetState();
static Keys[] myKeys = { Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.D0 };
static bool oneSwapOn = true; //for toggling individual item swapping
static bool onePressOn = false;

int[] timer1 = new int[10];

public override void ProcessTriggers(TriggersSet triggersSet) //only called when the game has focus - once per game not once per player
{
//Main.NewText("Trigger: " + player.whoAmI);
KeyboardState newState = Keyboard.GetState(); //create a new storage for the keyboard state

if (oneSwapOn) //if individual items should be swapped
{
for (int i = 0; i < myKeys.Length; i++)
{
Keys thisKey = myKeys;
if (newState.IsKeyDown(thisKey)) //if the required key is pressed
{
// If not down last update, key has just been pressed.
if (!oldState.IsKeyDown(thisKey))
{
//Main.NewText("Key Pressed: " + i);

if (timer1 > 0 || onePressOn) //if the timer hasn't reached 0, or one-press swapping is on
{
itemSwap(i); //swap items at the required slot
//play sound here
if (Main.netMode != 2)
Main.PlaySound(12, (int)player.Center.X, (int)player.Center.Y, 1);
} else
timer1 = 20;//add one third of a second to the timer

//play sound here
if (Main.netMode != 2)
Main.PlaySound(12, (int)player.Center.X, (int)player.Center.Y, 1);
}
}
}
}

oldState = newState;
for (int i = 0; i < timer1.Length; i++) //subtract one from every timer
if (timer1 > 0)
timer1 -= 1;

//Main.NewText("key: " + HotBarSwapper.HotBarSwapper.hotBarSwap.GetAssignedKeys()[0]);
if (HotBarSwapper.HotBarSwapper.swapKey.JustPressed)
{
//Main.NewText("hotKey");
for (int j = 0; j < 10; j++) //for every item in the top row
{
//Main.NewText("j: " + j);
itemSwap(j); //swap with the item below
}
//play sound here
if (Main.netMode != 2)
Main.PlaySound(12, (int)player.Center.X, (int)player.Center.Y, 1);
}
if (HotBarSwapper.HotBarSwapper.toggleKey.JustPressed)
{
oneSwapOn = !oneSwapOn; //toggle oneSwapOn
Main.NewText("Single items swapping is now " + (oneSwapOn ? "on." : "off.")); //output text
}
if (HotBarSwapper.HotBarSwapper.onePress.JustPressed)
{
onePressOn = !onePressOn; //toggle oneSwapOn
Main.NewText("One press swapping is now " + (onePressOn ? "on." : "off.")); //output text
}
}

//swap an item with the one below
void itemSwap (int key)
{
Item tempItem = player.inventory[key]; //move item from the top row to a temp item
player.inventory[key] = player.inventory[key + 10]; //move item from second row to top row
player.inventory[key + 10] = tempItem; //move item from temp item to top row
}

}

I'm not sure how this would transfer to a stand-alone mod, though.
 

Slaughter Lord

Terrarian
Hey man, I love the mod you made, thanks for making it and sharing.
I have an issue with being able to swap single items by double pressing. It technically works but i have a mouse with 7 side buttons and mapped as NumPad 1 through 7. Then in the in-game controls i have "hotbar slot 1-7, set as numpad 1-7 (for mouse)" then "hotbar slot 8-0, set as 1, 2, and 3"
When double pressing any of my mouse buttons (numpad 1-7), you mod does not recognize. And even though i have hotbar slot 8 control set to #1 keyboard key, double pressing #1 swaps hotbar slot 1, instead of slot 8. So it seems the single item swapping functionality has it's controls hard-coded as 1-9 & 0.

It's not a big deal to not be able to use single item swapping for me, but if it doesn't take much time/energy to implement, i would like to use it. I'm sure im probably the only one using your mod and with this kind of hotkey layout anyway so no worries if not. Thanks​
 

Solo-Ion

Dungeon Spirit
Hey man, I love the mod you made, thanks for making it and sharing.
I have an issue with being able to swap single items by double pressing. It technically works but i have a mouse with 7 side buttons and mapped as NumPad 1 through 7. Then in the in-game controls i have "hotbar slot 1-7, set as numpad 1-7 (for mouse)" then "hotbar slot 8-0, set as 1, 2, and 3"
When double pressing any of my mouse buttons (numpad 1-7), you mod does not recognize. And even though i have hotbar slot 8 control set to #1 keyboard key, double pressing #1 swaps hotbar slot 1, instead of slot 8. So it seems the single item swapping functionality has it's controls hard-coded as 1-9 & 0.

It's not a big deal to not be able to use single item swapping for me, but if it doesn't take much time/energy to implement, i would like to use it. I'm sure im probably the only one using your mod and with this kind of hotkey layout anyway so no worries if not. Thanks​
That's a good find. You're right, I did hard code my mod to the number keys, regardless of how the player configured their keybindings. I've just run a quick test and I've found how to detect the games keybindings. It shouldn't take me too long to fix this bug.

Thanks for finding that. :)
 

Slaughter Lord

Terrarian
That's a good find. You're right, I did hard code my mod to the number keys, regardless of how the player configured their keybindings. I've just run a quick test and I've found how to detect the games keybindings. It shouldn't take me too long to fix this bug.

Thanks for finding that. :)

Awesome! Thanks for the reply and adding that capability in. Once you've updated your mod, will I need to re-download & install? I'm not too familiar with Terraria modding, so idk if using tModloader handles that aspect and what not. Thanks again​
 

Solo-Ion

Dungeon Spirit
Awesome! Thanks for the reply and adding that capability in. Once you've updated your mod, will I need to re-download & install? I'm not too familiar with Terraria modding, so idk if using tModloader handles that aspect and what not. Thanks again​
And fixed. Hotbar Swapper will now use Terraria's hotbar keybindings instead of being hard-coded to the number buttons. Thanks, I wouldn't have thought that that was a problem unless someone pointed it out to me.

Yes, you will need to re-download my mod. tModLoader doesn't auto-update mods, but it's easy enough to get it off the mod browser.
 

Maxijaxus

Terrarian
I found a bug. I can't record my Terraria screen right now and it's also a hassle to fix so I'm just gonna describe it.

To emulate said bug, put a Vortex Beater in the first slot of the hotbar. Then put a Razorblade Typhoon under it. Now start firing the Vortex Beater. Then immediately switch to the Razorblade Typhoon using this mod's controls. Then suddenly, you start firing these super fast razorblades. What I think is happening is that the bullets of the Vortex Beater is being replaced by razorblades.

For extra info, the Razorblade Typhoon's sprite when attacking is overlapping with the Vortex Beater's. Interestingly, it doesn't seem to work on other ranged weapons that fire bullets too. Not sure though because I only tried it with Megashark and Uzi.

Good luck with it.
 

Solo-Ion

Dungeon Spirit
I found a bug. I can't record my Terraria screen right now and it's also a hassle to fix so I'm just gonna describe it.

To emulate said bug, put a Vortex Beater in the first slot of the hotbar. Then put a Razorblade Typhoon under it. Now start firing the Vortex Beater. Then immediately switch to the Razorblade Typhoon using this mod's controls. Then suddenly, you start firing these super fast razorblades. What I think is happening is that the bullets of the Vortex Beater is being replaced by razorblades.

For extra info, the Razorblade Typhoon's sprite when attacking is overlapping with the Vortex Beater's. Interestingly, it doesn't seem to work on other ranged weapons that fire bullets too. Not sure though because I only tried it with Megashark and Uzi.

Good luck with it.
Wow, that's a very specific bug you've found there. I would not have found that out on my own... mainly because I haven't acquired either of those weapons yet. :sigh:

It could also be because the Vortex Beater fires two types of projectiles, or maybe because the Vortex Beater is a channeled weapon... either way, thank you. I'll look into this soon.
 

jopojelly

Retinazer
tModLoader
Wow, that's a very specific bug you've found there. I would not have found that out on my own... mainly because I haven't acquired either of those weapons yet. :sigh:

It could also be because the Vortex Beater fires two types of projectiles, or maybe because the Vortex Beater is a channeled weapon... either way, thank you. I'll look into this soon.
It's probably the same bug I had with one of my mods, you'll want to check

if (player.itemAnimation == 0 && player.itemTime == 0)

before allowing the swap.
 

Maxijaxus

Terrarian
No problem. I was fighting this Martian Saucer with a Razorblade and I accidentally doubled click my '1' key and switched to my Vortex Beater. So I immediately switched back, annihilating the saucer with a :red: ton of flying blades.
 

Solo-Ion

Dungeon Spirit
It's probably the same bug I had with one of my mods, you'll want to check

if (player.itemAnimation == 0 && player.itemTime == 0)

before allowing the swap.
Thanks, I'll probably do it that way.

No problem. I was fighting this Martian Saucer with a Razorblade and I accidentally doubled click my '1' key and switched to my Vortex Beater. So I immediately switched back, annihilating the saucer with a :red: ton of flying blades.
Coolness! Aliens beaten by bugs.

I'm going to have to try that out for myself.
 

Solo-Ion

Dungeon Spirit
After a small hiatus, I've updated this to work with new tModLoader. I haven't updated the version in the attachment. You have to get it from the workshop now.
 
Top Bottom