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
}
}