Add ability to change player item in hand. Adds BUKKIT-3318

This commit is contained in:
Patrick Seidel 2012-12-27 16:30:08 -05:00 committed by GJ
parent b532042973
commit 7c40a073d8
2 changed files with 13 additions and 0 deletions

View File

@ -760,6 +760,10 @@ public class PlayerConnection extends Connection {
if (packet16blockitemswitch.itemInHandIndex >= 0 && packet16blockitemswitch.itemInHandIndex < PlayerInventory.getHotbarSize()) { if (packet16blockitemswitch.itemInHandIndex >= 0 && packet16blockitemswitch.itemInHandIndex < PlayerInventory.getHotbarSize()) {
PlayerItemHeldEvent event = new PlayerItemHeldEvent(this.getPlayer(), this.player.inventory.itemInHandIndex, packet16blockitemswitch.itemInHandIndex); PlayerItemHeldEvent event = new PlayerItemHeldEvent(this.getPlayer(), this.player.inventory.itemInHandIndex, packet16blockitemswitch.itemInHandIndex);
this.server.getPluginManager().callEvent(event); this.server.getPluginManager().callEvent(event);
if (event.isCancelled()) {
this.sendPacket(new Packet16BlockItemSwitch(this.player.inventory.itemInHandIndex));
return;
}
// CraftBukkit end // CraftBukkit end
this.player.inventory.itemInHandIndex = packet16blockitemswitch.itemInHandIndex; this.player.inventory.itemInHandIndex = packet16blockitemswitch.itemInHandIndex;

View File

@ -1,7 +1,10 @@
package org.bukkit.craftbukkit.inventory; package org.bukkit.craftbukkit.inventory;
import net.minecraft.server.Packet16BlockItemSwitch;
import net.minecraft.server.PlayerInventory; import net.minecraft.server.PlayerInventory;
import org.apache.commons.lang.Validate;
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -33,6 +36,12 @@ public class CraftInventoryPlayer extends CraftInventory implements org.bukkit.i
return getInventory().itemInHandIndex; return getInventory().itemInHandIndex;
} }
public void setHeldItemSlot(int slot) {
Validate.isTrue(slot >= 0 && slot < PlayerInventory.getHotbarSize(), "Slot is not between 0 and 8 inclusive");
this.getInventory().itemInHandIndex = slot;
((CraftPlayer) this.getHolder()).getHandle().playerConnection.sendPacket(new Packet16BlockItemSwitch(slot));
}
public ItemStack getHelmet() { public ItemStack getHelmet() {
return getItem(getSize() + 3); return getItem(getSize() + 3);
} }