Jump to content

[1.8] Swapping Items


Jedispencer21

Recommended Posts

What I am trying to do is to have it when you place a button, it swaps your current item with an item with higher damage, the problem I am having is that when I press the button, it takes the weapon from the slot, but it doesn't swap them, it just replaces the current held item with the swapped item, but doesn't take the current item and put it in the other slot.

 

Here is my code for swapping:

 

 

protected static void selectSlot(int slot)

{

if ((slot < 0) || (slot > 8)) return;

 

InventoryPlayer inventory = mc.thePlayer.inventory;

ItemStack stack = inventory.getStackInSlot(slot);

inventory.setInventorySlotContents(inventory.currentItem, stack);

ItemStack stack1 = inventory.getStackInSlot(inventory.currentItem);

inventory.setInventorySlotContents(slot, stack1);

}

 

 

Link to comment
Share on other sites

What I am trying to do is to have it when you place a button, it swaps your current item with an item with higher damage, the problem I am having is that when I press the button, it takes the weapon from the slot, but it doesn't swap them, it just replaces the current held item with the swapped item, but doesn't take the current item and put it in the other slot.

 

Here is my code for swapping:

 

 

protected static void selectSlot(int slot)
{
	if ((slot < 0) || (slot > ) return;

	InventoryPlayer inventory = mc.thePlayer.inventory;
	ItemStack stack = inventory.getStackInSlot(slot);
	inventory.setInventorySlotContents(inventory.currentItem, stack);
	ItemStack stack1 = inventory.getStackInSlot(inventory.currentItem);
	inventory.setInventorySlotContents(slot, stack1);
}

 

 

 

 

You are setting the inventory slot with your item, and then getting the item you just set. You want to do this:

ItemStack slotStack = inventory.getStackInSlot(slot);
ItemStack currentStack = inventory.getStackInSlot(inventory.currentItem);

if (slotStack != null){
    inventory.setInventorySlotContents(inventory.currentItem, slotStack);
} else {
    //return here? if you want to
}

if (currentStack != null){
    inventory.setInventorySlotContents(slot, currentStack);
} else {
    //same as above
}

 

Also, I dont know if you can do these operations on the client side, since you are using Minecraft.getMinecraft().thePlayer (client side player)

Link to comment
Share on other sites

That sort of worked, it swaps the items client side and appears to swap them, but it doesn't actually swap them server side also the other problem I have is that it doesn't select the item with the most damage, it just selects the right most item, this is the selection code:

 

 

public static void swapWeapons()

{

ItemStack currentStack = mc.thePlayer.getHeldItem();

Item currentItem = null;

 

if (currentStack != null) currentItem = currentStack.getItem();

 

int meleeWeaponSlot = getMostDamagingSlot();

 

if ((meleeWeaponSlot < 0))

{

displayNotification("No Weapons Available");

}

else if ((meleeWeaponSlot >= 0))

{

selectSlot(meleeWeaponSlot);

}

}

 

private static int getMostDamagingSlot()

{

ItemStack[] stack = mc.thePlayer.inventory.mainInventory;

double highestDamage = -1.0D;

int highestSlot = -1;

 

for (int i = 0; i < 9; ++i)

{

ItemStack hotbar = stack;

if (hotbar == null || !(hotbar.getItem() instanceof ItemSword) && !(hotbar.getItem() instanceof ItemTool)) continue;

double weaponDamage = getWeaponDamage(hotbar);

if (weaponDamage < highestDamage) continue;

highestDamage = weaponDamage;

highestSlot = i;

}

 

return highestSlot;

}

 

public static double getWeaponDamage(ItemStack stack)

{

Multimap attributeMods = stack.getItem().getAttributeModifiers(stack);

 

if (attributeMods.containsKey(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName()))

{

Collection attributes = attributeMods.get(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName());

 

if (attributes.size() > 0)

{

Object object = attributes.iterator().next();

 

if ((attributes instanceof AttributeModifier))

{

AttributeModifier weaponMod = (AttributeModifier) object;

return weaponMod.getAmount();

}

}

}

 

return -1.0D;

}

 

 

Link to comment
Share on other sites

Bumping after 40 minutes? Seriously?

 

Anyway, of course it is not swapping server side - you are using Minecraft.getMinecraft().thePlayer, which is CLIENT side only. Use an EntityPlayer from a parameter somewhere, probably in a packet since you are trying to swap with a button press - input interactions are also CLIENT side only.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.