diff options
author | meiskam <meiskam@gmail.com> | 2012-12-21 18:44:56 -0500 |
---|---|---|
committer | feildmaster <admin@feildmaster.com> | 2012-12-21 22:47:11 -0600 |
commit | fdb4b2d911b66c2dff05ba39b4ab58a34f6ff911 (patch) | |
tree | d7f6883a103f7c698ec743866ec06958e8e43d08 /src/main/java/org | |
parent | 39e2af7bd1592be5a968f21cb461c66c33af6958 (diff) | |
download | craftbukkit-fdb4b2d911b66c2dff05ba39b4ab58a34f6ff911.tar craftbukkit-fdb4b2d911b66c2dff05ba39b4ab58a34f6ff911.tar.gz craftbukkit-fdb4b2d911b66c2dff05ba39b4ab58a34f6ff911.tar.lz craftbukkit-fdb4b2d911b66c2dff05ba39b4ab58a34f6ff911.tar.xz craftbukkit-fdb4b2d911b66c2dff05ba39b4ab58a34f6ff911.zip |
Implement Skull BlockState. Adds BUKKIT-3259
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java | 2 | ||||
-rw-r--r-- | src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java | 195 |
2 files changed, 197 insertions, 0 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java index 63053ebc..91677bbe 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java @@ -255,6 +255,8 @@ public class CraftBlock implements Block { return new CraftJukebox(this); case BREWING_STAND: return new CraftBrewingStand(this); + case SKULL: + return new CraftSkull(this); default: return new CraftBlockState(this); } diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java b/src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java new file mode 100644 index 00000000..2f2564f2 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java @@ -0,0 +1,195 @@ +package org.bukkit.craftbukkit.block; + +import com.google.common.base.Strings; + +import net.minecraft.server.TileEntitySkull; + +import org.bukkit.SkullType; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.Skull; +import org.bukkit.craftbukkit.CraftWorld; + +public class CraftSkull extends CraftBlockState implements Skull { + private final TileEntitySkull skull; + private String player; + private SkullType skullType; + private byte rotation; + private final int MAX_OWNER_LENGTH = 16; + + public CraftSkull(final Block block) { + super(block); + + CraftWorld world = (CraftWorld) block.getWorld(); + skull = (TileEntitySkull) world.getTileEntityAt(getX(), getY(), getZ()); + player = skull.getExtraType(); + skullType = getSkullType(skull.getSkullType()); + rotation = (byte) skull.getRotation(); + } + + static SkullType getSkullType(int id) { + switch (id) { + case 0: + return SkullType.SKELETON; + case 1: + return SkullType.WITHER; + case 2: + return SkullType.ZOMBIE; + case 3: + return SkullType.PLAYER; + case 4: + return SkullType.CREEPER; + default: + throw new AssertionError(id); + } + } + + static int getSkullType(SkullType type) { + switch(type) { + case SKELETON: + return 0; + case WITHER: + return 1; + case ZOMBIE: + return 2; + case PLAYER: + return 3; + case CREEPER: + return 4; + default: + throw new AssertionError(type); + } + } + + static byte getBlockFace(BlockFace rotation) { + switch (rotation) { + case NORTH: + return 0; + case NORTH_NORTH_EAST: + return 1; + case NORTH_EAST: + return 2; + case EAST_NORTH_EAST: + return 3; + case EAST: + return 4; + case EAST_SOUTH_EAST: + return 5; + case SOUTH_EAST: + return 6; + case SOUTH_SOUTH_EAST: + return 7; + case SOUTH: + return 8; + case SOUTH_SOUTH_WEST: + return 9; + case SOUTH_WEST: + return 10; + case WEST_SOUTH_WEST: + return 11; + case WEST: + return 12; + case WEST_NORTH_WEST: + return 13; + case NORTH_WEST: + return 14; + case NORTH_NORTH_WEST: + return 15; + default: + throw new IllegalArgumentException("Invalid BlockFace rotation: " + rotation); + } + } + + static BlockFace getBlockFace(byte rotation) { + switch (rotation) { + case 0: + return BlockFace.NORTH; + case 1: + return BlockFace.NORTH_NORTH_EAST; + case 2: + return BlockFace.NORTH_EAST; + case 3: + return BlockFace.EAST_NORTH_EAST; + case 4: + return BlockFace.EAST; + case 5: + return BlockFace.EAST_SOUTH_EAST; + case 6: + return BlockFace.SOUTH_EAST; + case 7: + return BlockFace.SOUTH_SOUTH_EAST; + case 8: + return BlockFace.SOUTH; + case 9: + return BlockFace.SOUTH_SOUTH_WEST; + case 10: + return BlockFace.SOUTH_WEST; + case 11: + return BlockFace.WEST_SOUTH_WEST; + case 12: + return BlockFace.WEST; + case 13: + return BlockFace.WEST_NORTH_WEST; + case 14: + return BlockFace.NORTH_WEST; + case 15: + return BlockFace.NORTH_NORTH_WEST; + default: + throw new AssertionError(rotation); + } + } + + public boolean hasOwner() { + return !Strings.isNullOrEmpty(player); + } + + public String getOwner() { + return player; + } + + public boolean setOwner(String name) { + if (name != null && name.length() > MAX_OWNER_LENGTH) { + return false; + } + player = name; + + if (skullType != SkullType.PLAYER) { + skullType = SkullType.PLAYER; + } + + return true; + } + + public BlockFace getRotation() { + return getBlockFace(rotation); + } + + public void setRotation(BlockFace rotation) { + this.rotation = getBlockFace(rotation); + } + + public SkullType getSkullType() { + return skullType; + } + + public void setSkullType(SkullType skullType) { + this.skullType = skullType; + + if (skullType != SkullType.PLAYER) { + player = null; + } + } + + @Override + public boolean update(boolean force) { + boolean result = super.update(force); + + if (result) { + skull.setSkullType(getSkullType(skullType), player); + skull.setRotation(rotation); + skull.update(); + } + + return result; + } +} |