| 1 | package net.minecraft.tileentity;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import java.util.Map;
|
| 5 | import java.util.concurrent.Callable;
|
| 6 | import net.minecraft.block.Block;
|
| 7 | import net.minecraft.block.BlockJukebox;
|
| 8 | import net.minecraft.block.state.IBlockState;
|
| 9 | import net.minecraft.crash.CrashReportCategory;
|
| 10 | import net.minecraft.init.Blocks;
|
| 11 | import net.minecraft.nbt.NBTTagCompound;
|
| 12 | import net.minecraft.network.Packet;
|
| 13 | import net.minecraft.util.BlockPos;
|
| 14 | import net.minecraft.world.World;
|
| 15 | import org.apache.logging.log4j.LogManager;
|
| 16 | import org.apache.logging.log4j.Logger;
|
| 17 |
|
| 18 | public abstract class TileEntity
|
| 19 | {
|
| 20 | private static final Logger logger = LogManager.getLogger();
|
| 21 | private static Map < String, Class <? extends TileEntity >> nameToClassMap = Maps. < String, Class <? extends TileEntity >> newHashMap();
|
| 22 | private static Map < Class <? extends TileEntity > , String > classToNameMap = Maps. < Class <? extends TileEntity > , String > newHashMap();
|
| 23 |
|
| 24 | /** the instance of the world the tile entity is in. */
|
| 25 | protected World worldObj;
|
| 26 | protected BlockPos pos = BlockPos.ORIGIN;
|
| 27 | protected boolean tileEntityInvalid;
|
| 28 | private int blockMetadata = -1;
|
| 29 |
|
| 30 | /** the Block type that this TileEntity is contained within */
|
| 31 | protected Block blockType;
|
| 32 |
|
| 33 | /**
|
| 34 | * Adds a new two-way mapping between the class and its string name in both hashmaps.
|
| 35 | */
|
| 36 | private static void addMapping(Class <? extends TileEntity > cl, String id)
|
| 37 | {
|
| 38 | if (nameToClassMap.containsKey(id))
|
| 39 | {
|
| 40 | throw new IllegalArgumentException("Duplicate id: " + id);
|
| 41 | }
|
| 42 | else
|
| 43 | {
|
| 44 | nameToClassMap.put(id, cl);
|
| 45 | classToNameMap.put(cl, id);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | /**
|
| 50 | * Returns the worldObj for this tileEntity.
|
| 51 | */
|
| 52 | public World getWorld()
|
| 53 | {
|
| 54 | return this.worldObj;
|
| 55 | }
|
| 56 |
|
| 57 | /**
|
| 58 | * Sets the worldObj for this tileEntity.
|
| 59 | */
|
| 60 | public void setWorldObj(World worldIn)
|
| 61 | {
|
| 62 | this.worldObj = worldIn;
|
| 63 | }
|
| 64 |
|
| 65 | /**
|
| 66 | * Returns true if the worldObj isn't null.
|
| 67 | */
|
| 68 | public boolean hasWorldObj()
|
| 69 | {
|
| 70 | return this.worldObj != null;
|
| 71 | }
|
| 72 |
|
| 73 | public void readFromNBT(NBTTagCompound compound)
|
| 74 | {
|
| 75 | this.pos = new BlockPos(compound.getInteger("x"), compound.getInteger("y"), compound.getInteger("z"));
|
| 76 | }
|
| 77 |
|
| 78 | public void writeToNBT(NBTTagCompound compound)
|
| 79 | {
|
| 80 | String s = (String)classToNameMap.get(this.getClass());
|
| 81 |
|
| 82 | if (s == null)
|
| 83 | {
|
| 84 | throw new RuntimeException(this.getClass() + " is missing a mapping! This is a bug!");
|
| 85 | }
|
| 86 | else
|
| 87 | {
|
| 88 | compound.setString("id", s);
|
| 89 | compound.setInteger("x", this.pos.getX());
|
| 90 | compound.setInteger("y", this.pos.getY());
|
| 91 | compound.setInteger("z", this.pos.getZ());
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | /**
|
| 96 | * Creates a new entity and loads its data from the specified NBT.
|
| 97 | */
|
| 98 | public static TileEntity createAndLoadEntity(NBTTagCompound nbt)
|
| 99 | {
|
| 100 | TileEntity tileentity = null;
|
| 101 |
|
| 102 | try
|
| 103 | {
|
| 104 | Class <? extends TileEntity > oclass = (Class)nameToClassMap.get(nbt.getString("id"));
|
| 105 |
|
| 106 | if (oclass != null)
|
| 107 | {
|
| 108 | tileentity = (TileEntity)oclass.newInstance();
|
| 109 | }
|
| 110 | }
|
| 111 | catch (Exception exception)
|
| 112 | {
|
| 113 | exception.printStackTrace();
|
| 114 | }
|
| 115 |
|
| 116 | if (tileentity != null)
|
| 117 | {
|
| 118 | tileentity.readFromNBT(nbt);
|
| 119 | }
|
| 120 | else
|
| 121 | {
|
| 122 | logger.warn("Skipping BlockEntity with id " + nbt.getString("id"));
|
| 123 | }
|
| 124 |
|
| 125 | return tileentity;
|
| 126 | }
|
| 127 |
|
| 128 | public int getBlockMetadata()
|
| 129 | {
|
| 130 | if (this.blockMetadata == -1)
|
| 131 | {
|
| 132 | IBlockState iblockstate = this.worldObj.getBlockState(this.pos);
|
| 133 | this.blockMetadata = iblockstate.getBlock().getMetaFromState(iblockstate);
|
| 134 | }
|
| 135 |
|
| 136 | return this.blockMetadata;
|
| 137 | }
|
| 138 |
|
| 139 | /**
|
| 140 | * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
|
| 141 | * hasn't changed and skip it.
|
| 142 | */
|
| 143 | public void markDirty()
|
| 144 | {
|
| 145 | if (this.worldObj != null)
|
| 146 | {
|
| 147 | IBlockState iblockstate = this.worldObj.getBlockState(this.pos);
|
| 148 | this.blockMetadata = iblockstate.getBlock().getMetaFromState(iblockstate);
|
| 149 | this.worldObj.markChunkDirty(this.pos, this);
|
| 150 |
|
| 151 | if (this.getBlockType() != Blocks.air)
|
| 152 | {
|
| 153 | this.worldObj.updateComparatorOutputLevel(this.pos, this.getBlockType());
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | /**
|
| 159 | * Returns the square of the distance between this entity and the passed in coordinates.
|
| 160 | */
|
| 161 | public double getDistanceSq(double x, double y, double z)
|
| 162 | {
|
| 163 | double d0 = (double)this.pos.getX() + 0.5D - x;
|
| 164 | double d1 = (double)this.pos.getY() + 0.5D - y;
|
| 165 | double d2 = (double)this.pos.getZ() + 0.5D - z;
|
| 166 | return d0 * d0 + d1 * d1 + d2 * d2;
|
| 167 | }
|
| 168 |
|
| 169 | public double getMaxRenderDistanceSquared()
|
| 170 | {
|
| 171 | return 4096.0D;
|
| 172 | }
|
| 173 |
|
| 174 | public BlockPos getPos()
|
| 175 | {
|
| 176 | return this.pos;
|
| 177 | }
|
| 178 |
|
| 179 | /**
|
| 180 | * Gets the block type at the location of this entity (client-only).
|
| 181 | */
|
| 182 | public Block getBlockType()
|
| 183 | {
|
| 184 | if (this.blockType == null)
|
| 185 | {
|
| 186 | this.blockType = this.worldObj.getBlockState(this.pos).getBlock();
|
| 187 | }
|
| 188 |
|
| 189 | return this.blockType;
|
| 190 | }
|
| 191 |
|
| 192 | /**
|
| 193 | * Allows for a specialized description packet to be created. This is often used to sync tile entity data from the
|
| 194 | * server to the client easily. For example this is used by signs to synchronise the text to be displayed.
|
| 195 | */
|
| 196 | public Packet getDescriptionPacket()
|
| 197 | {
|
| 198 | return null;
|
| 199 | }
|
| 200 |
|
| 201 | public boolean isInvalid()
|
| 202 | {
|
| 203 | return this.tileEntityInvalid;
|
| 204 | }
|
| 205 |
|
| 206 | /**
|
| 207 | * invalidates a tile entity
|
| 208 | */
|
| 209 | public void invalidate()
|
| 210 | {
|
| 211 | this.tileEntityInvalid = true;
|
| 212 | }
|
| 213 |
|
| 214 | /**
|
| 215 | * validates a tile entity
|
| 216 | */
|
| 217 | public void validate()
|
| 218 | {
|
| 219 | this.tileEntityInvalid = false;
|
| 220 | }
|
| 221 |
|
| 222 | public boolean receiveClientEvent(int id, int type)
|
| 223 | {
|
| 224 | return false;
|
| 225 | }
|
| 226 |
|
| 227 | public void updateContainingBlockInfo()
|
| 228 | {
|
| 229 | this.blockType = null;
|
| 230 | this.blockMetadata = -1;
|
| 231 | }
|
| 232 |
|
| 233 | public void addInfoToCrashReport(CrashReportCategory reportCategory)
|
| 234 | {
|
| 235 | reportCategory.addCrashSectionCallable("Name", new Callable<String>()
|
| 236 | {
|
| 237 | public String call() throws Exception
|
| 238 | {
|
| 239 | return (String)TileEntity.classToNameMap.get(TileEntity.this.getClass()) + " // " + TileEntity.this.getClass().getCanonicalName();
|
| 240 | }
|
| 241 | });
|
| 242 |
|
| 243 | if (this.worldObj != null)
|
| 244 | {
|
| 245 | CrashReportCategory.addBlockInfo(reportCategory, this.pos, this.getBlockType(), this.getBlockMetadata());
|
| 246 | reportCategory.addCrashSectionCallable("Actual block type", new Callable<String>()
|
| 247 | {
|
| 248 | public String call() throws Exception
|
| 249 | {
|
| 250 | int i = Block.getIdFromBlock(TileEntity.this.worldObj.getBlockState(TileEntity.this.pos).getBlock());
|
| 251 |
|
| 252 | try
|
| 253 | {
|
| 254 | return String.format("ID #%d (%s // %s)", new Object[] {Integer.valueOf(i), Block.getBlockById(i).getUnlocalizedName(), Block.getBlockById(i).getClass().getCanonicalName()});
|
| 255 | }
|
| 256 | catch (Throwable var3)
|
| 257 | {
|
| 258 | return "ID #" + i;
|
| 259 | }
|
| 260 | }
|
| 261 | });
|
| 262 | reportCategory.addCrashSectionCallable("Actual block data value", new Callable<String>()
|
| 263 | {
|
| 264 | public String call() throws Exception
|
| 265 | {
|
| 266 | IBlockState iblockstate = TileEntity.this.worldObj.getBlockState(TileEntity.this.pos);
|
| 267 | int i = iblockstate.getBlock().getMetaFromState(iblockstate);
|
| 268 |
|
| 269 | if (i < 0)
|
| 270 | {
|
| 271 | return "Unknown? (Got " + i + ")";
|
| 272 | }
|
| 273 | else
|
| 274 | {
|
| 275 | String s = String.format("%4s", new Object[] {Integer.toBinaryString(i)}).replace(" ", "0");
|
| 276 | return String.format("%1$d / 0x%1$X / 0b%2$s", new Object[] {Integer.valueOf(i), s});
|
| 277 | }
|
| 278 | }
|
| 279 | });
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | public void setPos(BlockPos posIn)
|
| 284 | {
|
| 285 | this.pos = posIn;
|
| 286 | }
|
| 287 |
|
| 288 | public boolean func_183000_F()
|
| 289 | {
|
| 290 | return false;
|
| 291 | }
|
| 292 |
|
| 293 | static
|
| 294 | {
|
| 295 | addMapping(TileEntityFurnace.class, "Furnace");
|
| 296 | addMapping(TileEntityChest.class, "Chest");
|
| 297 | addMapping(TileEntityEnderChest.class, "EnderChest");
|
| 298 | addMapping(BlockJukebox.TileEntityJukebox.class, "RecordPlayer");
|
| 299 | addMapping(TileEntityDispenser.class, "Trap");
|
| 300 | addMapping(TileEntityDropper.class, "Dropper");
|
| 301 | addMapping(TileEntitySign.class, "Sign");
|
| 302 | addMapping(TileEntityMobSpawner.class, "MobSpawner");
|
| 303 | addMapping(TileEntityNote.class, "Music");
|
| 304 | addMapping(TileEntityPiston.class, "Piston");
|
| 305 | addMapping(TileEntityBrewingStand.class, "Cauldron");
|
| 306 | addMapping(TileEntityEnchantmentTable.class, "EnchantTable");
|
| 307 | addMapping(TileEntityEndPortal.class, "Airportal");
|
| 308 | addMapping(TileEntityCommandBlock.class, "Control");
|
| 309 | addMapping(TileEntityBeacon.class, "Beacon");
|
| 310 | addMapping(TileEntitySkull.class, "Skull");
|
| 311 | addMapping(TileEntityDaylightDetector.class, "DLDetector");
|
| 312 | addMapping(TileEntityHopper.class, "Hopper");
|
| 313 | addMapping(TileEntityComparator.class, "Comparator");
|
| 314 | addMapping(TileEntityFlowerPot.class, "FlowerPot");
|
| 315 | addMapping(TileEntityBanner.class, "Banner");
|
| 316 | }
|
| 317 | }
|
| 318 |
|