
Originally Posted by
MrBlueSL
Could you pm me the source for that, or point me in the right direction as to how to make that? That would be awesome to finally have, and I don't really know where I would start for that.
Sure. I'll pm you the source and explain it here for others who might want it as well.
There is 2 parts to this mod. Shift clicking items from your inventory to a chest and vise versa.
Note: All of this takes place in Main.cs
Items from inventory to chest
The tricky part is finding where in code the shift clicking items to the garbage can happens.
In VS I Ctrl + F and searched for "if (Main.keyState.IsKeyDown(Keys.LeftShift)"
I got 4-5 results so I laid down some breakpoints, ran the game, shift clicked an item in my inventory and bam, we know where to start.
This is where the my breakpoint fired.
Code:
if (Main.mouseX >= num8 && (float)Main.mouseX <= (float)num8 + (float)Main.inventoryBackTexture.Width * Main.inventoryScale && Main.mouseY >= num9 && (float)Main.mouseY <= (float)num9 + (float)Main.inventoryBackTexture.Height * Main.inventoryScale)
{
Main.player[Main.myPlayer].mouseInterface = true;
if (Main.mouseLeftRelease && Main.mouseLeft)
{
if (Main.keyState.IsKeyDown(Keys.LeftShift))
{
if (Main.player[Main.myPlayer].inventory[num10].type > 0)
{
if (Main.npcShop > 0)
{
if (Main.player[Main.myPlayer].inventory[num10].type < 71 || Main.player[Main.myPlayer].inventory[num10].type > 74)
{
if (Main.player[Main.myPlayer].SellItem(Main.player[Main.myPlayer].inventory[num10].value, Main.player[Main.myPlayer].inventory[num10].stack))
{
this.shop[Main.npcShop].AddShop(Main.player[Main.myPlayer].inventory[num10]);
Main.player[Main.myPlayer].inventory[num10].SetDefaults(0, false);
Main.PlaySound(18, -1, -1, 1);
}
else
{
if (Main.player[Main.myPlayer].inventory[num10].value == 0)
{
this.shop[Main.npcShop].AddShop(Main.player[Main.myPlayer].inventory[num10]);
Main.player[Main.myPlayer].inventory[num10].SetDefaults(0, false);
Main.PlaySound(7, -1, -1, 1);
}
}
}
}
/*
This is the area i wrote code
*/
else
{
//this is where the item gets sent to the trash
Main.PlaySound(7, -1, -1, 1);
Main.trashItem = (Item)Main.player[Main.myPlayer].inventory[num10].Clone();
Main.player[Main.myPlayer].inventory[num10].SetDefaults(0, false);
Recipe.FindRecipes();
}
}
}
else....
The code I put in there
Code:
else if (player[myPlayer].chest > -1) //check if player has a chest open
{
Player p = player[myPlayer];
Chest currentOpenChest = chest[p.chest];
Item inventoryItem = p.inventory[num10]; //clicked inventory item
for (int i = 0; i < currentOpenChest.item.Length; i++) //interate slots in chest
{
Item chestItem = currentOpenChest.item[i];
if (chestItem.type == inventoryItem.type) //if chest item is the same type as clicked inventory item
{
int remainingChestItemStackSpace = chestItem.maxStack - chestItem.stack;
chestItem.stack += inventoryItem.stack;
if (chestItem.stack > chestItem.maxStack)
chestItem.stack = chestItem.maxStack;
inventoryItem.stack -= remainingChestItemStackSpace;
if (Main.netMode == 1) //if playing online
{
NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)i, 0f, 0f, 0);
}
if (inventoryItem.stack <= 0) //if the stack is empty
{
inventoryItem.SetDefaults(0, false);
break;
}
}
}
if (inventoryItem.type > 0) //if the item did not get turned to consumed by the previous step, we look for an empty slot in the chest
{
for (int i = 0; i < currentOpenChest.item.Length; i++)
{
Item chestItem = currentOpenChest.item[i];
if (chestItem.type == 0)
{
currentOpenChest.item[i] = (Item)inventoryItem.Clone();
inventoryItem.SetDefaults(0, false);
if (Main.netMode == 1)
{
NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)i, 0f, 0f, 0);
}
break;
}
}
}
}
Items from chest to inventory
Very similar to putting items from inventory to chest.
For this, I searched for when the player had a chest opened.
That looks like this "if (Main.player[Main.myPlayer].chest > -1)"
I was also looking for a click while the chest was open. Threw down some breakpoints and got this.
Code:
if (Main.player[Main.myPlayer].chest > -1)
{
this.spriteBatch.DrawString(Main.fontMouseText, Main.chestText, new Vector2(504f, (float)this.invBottom), new Color((int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor, (int)Main.mouseTextColor), 0f, default(Vector2), 1f, SpriteEffects.None, 0f);
Main.inventoryScale = 0.755f;
if (Main.mouseX > 73 && Main.mouseX < (int)(73f + 280f * Main.inventoryScale) && Main.mouseY > this.invBottom && Main.mouseY < (int)((float)this.invBottom + 224f * Main.inventoryScale))
{
Main.player[Main.myPlayer].mouseInterface = true;
}
for (int num152 = 0; num152 < 10; num152++)
{
for (int num153 = 0; num153 < 4; num153++)
{
int num154 = (int)(73f + (float)(num152 * 56) * Main.inventoryScale);
int num155 = (int)((float)this.invBottom + (float)(num153 * 56) * Main.inventoryScale);
int num156 = num152 + num153 * 10;
Color white14 = new Color(100, 100, 100, 100);
if (Main.mouseX >= num154 && (float)Main.mouseX <= (float)num154 + (float)Main.inventoryBackTexture.Width * Main.inventoryScale && Main.mouseY >= num155 && (float)Main.mouseY <= (float)num155 + (float)Main.inventoryBackTexture.Height * Main.inventoryScale)
{
Main.player[Main.myPlayer].mouseInterface = true;
if (Main.mouseLeftRelease && Main.mouseLeft)
{
if ((Main.player[Main.myPlayer].selectedItem != num156 || Main.player[Main.myPlayer].itemAnimation <= 0) && Main.player[Main.myPlayer].itemTime == 0)
{
/*
This is where I wrote the code
*/
Item item10 = Main.mouseItem;
Main.mouseItem = Main.chest[Main.player[Main.myPlayer].chest].item[num156];
Main.chest[Main.player[Main.myPlayer].chest].item[num156] = item10;
if (Main.chest[Main.player[Main.myPlayer].chest].item[num156].type == 0 || Main.chest[Main.player[Main.myPlayer].chest].item[num156].stack < 1)
{
Main.chest[Main.player[Main.myPlayer].chest].item[num156] = new Item();
}
And here is the code i put there, it's very similar to the previous section
Code:
Player p = player[myPlayer];
Chest currentOpenChest =chest[p.chest];
Item chestItem = currentOpenChest.item[num156];
if (Main.keyState.IsKeyDown(Keys.LeftShift) && chestItem.type > 0)//check if shift key is down
{
for (int i = 0; i < p.inventory.Length; i++)
{
Item inventoryItem = p.inventory[i];
if (chestItem.type == inventoryItem.type)
{
int remainingInventoryStackSpace = inventoryItem.maxStack - inventoryItem.stack;
inventoryItem.stack += chestItem.stack;
if (inventoryItem.stack > inventoryItem.maxStack)
inventoryItem.stack = inventoryItem.maxStack;
chestItem.stack -= remainingInventoryStackSpace;
if (Main.netMode == 1)
{
NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)num156, 0f, 0f, 0);
}
if (chestItem.stack <= 0)
{
chestItem.SetDefaults(0, false);
break;
}
}
}
if (chestItem.type > 0)
{
for (int i = 0; i < p.inventory.Length - 9; i++)
{
//subtract 9 from the inventory length so we don't overflow into ammo and currency slots
//not sure what the 9th is though, mouse slot? anyway it's 9
int lastInventorySlot = p.inventory.Length - 9 - 1;
int index = lastInventorySlot - i;
//start from the bottom right of the inventory
Item inventoryItem = p.inventory[index];
if (inventoryItem.type == 0)
{
p.inventory[index] = (Item)chestItem.Clone();
chestItem.SetDefaults(0, false);
if (Main.netMode == 1)
{
NetMessage.SendData(32, -1, -1, "", Main.player[Main.myPlayer].chest, (float)num156, 0f, 0f, 0);
}
break;
}
}
}
}
And we're done!
I would like to release it for others to download, but I'm not sure how to go about it.
Anyway, enjoy
Another tip: set showSplash to false in Main.cs. It's nice when testing your mods.