Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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);

}

 

 

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)

  • Author

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;

}

 

 

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.

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.