From 6ea108ec2e26a5d960c1bb432fea460e7d6175da Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Wed, 31 Oct 2012 00:59:06 -0400 Subject: [PATCH] Implement the API for ItemFrames. Adds BUKKIT-2668 By: h31ix --- .../craftbukkit/entity/CraftItemFrame.java | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java index b03bb679da..94c4a12e99 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftItemFrame.java @@ -1,7 +1,13 @@ package org.bukkit.craftbukkit.entity; import net.minecraft.server.EntityItemFrame; +import net.minecraft.server.ItemStack; + +import org.apache.commons.lang.Validate; +import org.bukkit.Material; +import org.bukkit.Rotation; import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.entity.EntityType; import org.bukkit.entity.ItemFrame; @@ -10,6 +16,61 @@ public class CraftItemFrame extends CraftHanging implements ItemFrame { super(server, entity); } + public void setItem(org.bukkit.inventory.ItemStack item) { + if (item == null || item.getTypeId() == 0) { + getHandle().getDataWatcher().a(2, 5); + getHandle().getDataWatcher().h(2); + } else { + getHandle().a(CraftItemStack.createNMSItemStack(item)); + } + } + + public org.bukkit.inventory.ItemStack getItem() { + ItemStack i = getHandle().i(); + return i == null ? new org.bukkit.inventory.ItemStack(Material.AIR) : new CraftItemStack(i); + } + + public Rotation getRotation() { + return toBukkitRotation(getHandle().j()); + } + + Rotation toBukkitRotation(int value) { + // Translate NMS rotation integer to Bukkit API + switch (value) { + case 0: + return Rotation.NONE; + case 1: + return Rotation.CLOCKWISE; + case 2: + return Rotation.FLIPPED; + case 3: + return Rotation.COUNTER_CLOCKWISE; + default: + throw new AssertionError("Unknown rotation " + getHandle().j() + " for " + getHandle()); + } + } + + public void setRotation(Rotation rotation) { + Validate.notNull(rotation, "Rotation cannot be null"); + getHandle().g(toInteger(rotation)); + } + + static int toInteger(Rotation rotation) { + // Translate Bukkit API rotation to NMS integer + switch (rotation) { + case NONE: + return 0; + case CLOCKWISE: + return 1; + case FLIPPED: + return 2; + case COUNTER_CLOCKWISE: + return 3; + default: + throw new IllegalArgumentException(rotation + " is not applicable to an ItemFrame"); + } + } + @Override public EntityItemFrame getHandle() { return (EntityItemFrame) entity; @@ -17,7 +78,7 @@ public class CraftItemFrame extends CraftHanging implements ItemFrame { @Override public String toString() { - return "CraftItemFrame"; + return "CraftItemFrame{item=" + getItem() + ", rotation=" + getRotation() + "}"; } public EntityType getType() {