Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 206 lines 4.7 kB view raw
1package org.bukkit.craftbukkit.block; 2 3import org.bukkit.Chunk; 4import org.bukkit.Location; 5import org.bukkit.Material; 6import org.bukkit.World; 7import org.bukkit.block.Block; 8import org.bukkit.block.BlockState; 9import org.bukkit.craftbukkit.CraftChunk; 10import org.bukkit.craftbukkit.CraftWorld; 11import org.bukkit.material.MaterialData; 12 13public class CraftBlockState implements BlockState { 14 private final CraftWorld world; 15 private final CraftChunk chunk; 16 private final int x; 17 private final int y; 18 private final int z; 19 protected int type; 20 protected MaterialData data; 21 protected byte light; 22 23 public CraftBlockState(final Block block) { 24 this.world = (CraftWorld) block.getWorld(); 25 this.x = block.getX(); 26 this.y = block.getY(); 27 this.z = block.getZ(); 28 this.type = block.getTypeId(); 29 this.light = block.getLightLevel(); 30 this.chunk = (CraftChunk) block.getChunk(); 31 32 createData(block.getData()); 33 } 34 35 public static CraftBlockState getBlockState(net.minecraft.server.World world, int x, int y, int z) { 36 return new CraftBlockState(world.getWorld().getBlockAt(x, y, z)); 37 } 38 39 /** 40 * Gets the world which contains this Block 41 * 42 * @return World containing this block 43 */ 44 public World getWorld() { 45 return world; 46 } 47 48 /** 49 * Gets the x-coordinate of this block 50 * 51 * @return x-coordinate 52 */ 53 public int getX() { 54 return x; 55 } 56 57 /** 58 * Gets the y-coordinate of this block 59 * 60 * @return y-coordinate 61 */ 62 public int getY() { 63 return y; 64 } 65 66 /** 67 * Gets the z-coordinate of this block 68 * 69 * @return z-coordinate 70 */ 71 public int getZ() { 72 return z; 73 } 74 75 /** 76 * Gets the chunk which contains this block 77 * 78 * @return Containing Chunk 79 */ 80 public Chunk getChunk() { 81 return chunk; 82 } 83 84 /** 85 * Sets the metadata for this block 86 * 87 * @param data New block specific metadata 88 */ 89 public void setData(final MaterialData data) { 90 Material mat = getType(); 91 92 if ((mat == null) || (mat.getData() == null)) { 93 this.data = data; 94 } else { 95 if ((data.getClass() == mat.getData()) || (data.getClass() == MaterialData.class)) { 96 this.data = data; 97 } else { 98 throw new IllegalArgumentException("Provided data is not of type " + mat.getData().getName() + ", found " + data.getClass().getName()); 99 } 100 } 101 } 102 103 /** 104 * Gets the metadata for this block 105 * 106 * @return block specific metadata 107 */ 108 public MaterialData getData() { 109 return data; 110 } 111 112 /** 113 * Sets the type of this block 114 * 115 * @param type Material to change this block to 116 */ 117 public void setType(final Material type) { 118 setTypeId(type.getId()); 119 } 120 121 /** 122 * Sets the type-id of this block 123 * 124 * @param type Type-Id to change this block to 125 */ 126 public boolean setTypeId(final int type) { 127 this.type = type; 128 129 createData((byte) 0); 130 return true; 131 } 132 133 /** 134 * Gets the type of this block 135 * 136 * @return block type 137 */ 138 public Material getType() { 139 return Material.getMaterial(getTypeId()); 140 } 141 142 /** 143 * Gets the type-id of this block 144 * 145 * @return block type-id 146 */ 147 public int getTypeId() { 148 return type; 149 } 150 151 /** 152 * Gets the light level between 0-15 153 * 154 * @return light level 155 */ 156 public byte getLightLevel() { 157 return light; 158 } 159 160 public Block getBlock() { 161 return world.getBlockAt(x, y, z); 162 } 163 164 public boolean update() { 165 return update(false); 166 } 167 168 public boolean update(boolean force) { 169 Block block = getBlock(); 170 171 synchronized (block) { 172 if (block.getType() != this.getType()) { 173 if (force) { 174 block.setTypeId(this.getTypeId()); 175 } else { 176 return false; 177 } 178 } 179 180 block.setData(getRawData()); 181 } 182 183 return true; 184 } 185 186 private void createData(final byte data) { 187 Material mat = Material.getMaterial(type); 188 if (mat == null || mat.getData() == null) { 189 this.data = new MaterialData(type, data); 190 } else { 191 this.data = mat.getNewData(data); 192 } 193 } 194 195 public byte getRawData() { 196 return data.getData(); 197 } 198 199 public Location getLocation() { 200 return new Location(world, x, y, z); 201 } 202 203 public void setData(byte data) { 204 createData(data); 205 } 206}