| 1 | package net.minecraft.entity.item;
|
| 2 |
|
| 3 | import net.minecraft.entity.Entity;
|
| 4 | import net.minecraft.entity.EntityHanging;
|
| 5 | import net.minecraft.entity.player.EntityPlayer;
|
| 6 | import net.minecraft.init.Blocks;
|
| 7 | import net.minecraft.init.Items;
|
| 8 | import net.minecraft.item.ItemMap;
|
| 9 | import net.minecraft.item.ItemStack;
|
| 10 | import net.minecraft.nbt.NBTTagCompound;
|
| 11 | import net.minecraft.util.BlockPos;
|
| 12 | import net.minecraft.util.DamageSource;
|
| 13 | import net.minecraft.util.EnumFacing;
|
| 14 | import net.minecraft.world.World;
|
| 15 | import net.minecraft.world.storage.MapData;
|
| 16 |
|
| 17 | public class EntityItemFrame extends EntityHanging
|
| 18 | {
|
| 19 | /** Chance for this item frame's item to drop from the frame. */
|
| 20 | private float itemDropChance = 1.0F;
|
| 21 |
|
| 22 | public EntityItemFrame(World worldIn)
|
| 23 | {
|
| 24 | super(worldIn);
|
| 25 | }
|
| 26 |
|
| 27 | public EntityItemFrame(World worldIn, BlockPos p_i45852_2_, EnumFacing p_i45852_3_)
|
| 28 | {
|
| 29 | super(worldIn, p_i45852_2_);
|
| 30 | this.updateFacingWithBoundingBox(p_i45852_3_);
|
| 31 | }
|
| 32 |
|
| 33 | protected void entityInit()
|
| 34 | {
|
| 35 | this.getDataWatcher().addObjectByDataType(8, 5);
|
| 36 | this.getDataWatcher().addObject(9, Byte.valueOf((byte)0));
|
| 37 | }
|
| 38 |
|
| 39 | public float getCollisionBorderSize()
|
| 40 | {
|
| 41 | return 0.0F;
|
| 42 | }
|
| 43 |
|
| 44 | /**
|
| 45 | * Called when the entity is attacked.
|
| 46 | */
|
| 47 | public boolean attackEntityFrom(DamageSource source, float amount)
|
| 48 | {
|
| 49 | if (this.isEntityInvulnerable(source))
|
| 50 | {
|
| 51 | return false;
|
| 52 | }
|
| 53 | else if (!source.isExplosion() && this.getDisplayedItem() != null)
|
| 54 | {
|
| 55 | if (!this.worldObj.isRemote)
|
| 56 | {
|
| 57 | this.dropItemOrSelf(source.getEntity(), false);
|
| 58 | this.setDisplayedItem((ItemStack)null);
|
| 59 | }
|
| 60 |
|
| 61 | return true;
|
| 62 | }
|
| 63 | else
|
| 64 | {
|
| 65 | return super.attackEntityFrom(source, amount);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | public int getWidthPixels()
|
| 70 | {
|
| 71 | return 12;
|
| 72 | }
|
| 73 |
|
| 74 | public int getHeightPixels()
|
| 75 | {
|
| 76 | return 12;
|
| 77 | }
|
| 78 |
|
| 79 | /**
|
| 80 | * Checks if the entity is in range to render by using the past in distance and comparing it to its average edge
|
| 81 | * length * 64 * renderDistanceWeight Args: distance
|
| 82 | */
|
| 83 | public boolean isInRangeToRenderDist(double distance)
|
| 84 | {
|
| 85 | double d0 = 16.0D;
|
| 86 | d0 = d0 * 64.0D * this.renderDistanceWeight;
|
| 87 | return distance < d0 * d0;
|
| 88 | }
|
| 89 |
|
| 90 | /**
|
| 91 | * Called when this entity is broken. Entity parameter may be null.
|
| 92 | */
|
| 93 | public void onBroken(Entity brokenEntity)
|
| 94 | {
|
| 95 | this.dropItemOrSelf(brokenEntity, true);
|
| 96 | }
|
| 97 |
|
| 98 | public void dropItemOrSelf(Entity p_146065_1_, boolean p_146065_2_)
|
| 99 | {
|
| 100 | if (this.worldObj.getGameRules().getBoolean("doEntityDrops"))
|
| 101 | {
|
| 102 | ItemStack itemstack = this.getDisplayedItem();
|
| 103 |
|
| 104 | if (p_146065_1_ instanceof EntityPlayer)
|
| 105 | {
|
| 106 | EntityPlayer entityplayer = (EntityPlayer)p_146065_1_;
|
| 107 |
|
| 108 | if (entityplayer.capabilities.isCreativeMode)
|
| 109 | {
|
| 110 | this.removeFrameFromMap(itemstack);
|
| 111 | return;
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | if (p_146065_2_)
|
| 116 | {
|
| 117 | this.entityDropItem(new ItemStack(Items.item_frame), 0.0F);
|
| 118 | }
|
| 119 |
|
| 120 | if (itemstack != null && this.rand.nextFloat() < this.itemDropChance)
|
| 121 | {
|
| 122 | itemstack = itemstack.copy();
|
| 123 | this.removeFrameFromMap(itemstack);
|
| 124 | this.entityDropItem(itemstack, 0.0F);
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | /**
|
| 130 | * Removes the dot representing this frame's position from the map when the item frame is broken.
|
| 131 | */
|
| 132 | private void removeFrameFromMap(ItemStack p_110131_1_)
|
| 133 | {
|
| 134 | if (p_110131_1_ != null)
|
| 135 | {
|
| 136 | if (p_110131_1_.getItem() == Items.filled_map)
|
| 137 | {
|
| 138 | MapData mapdata = ((ItemMap)p_110131_1_.getItem()).getMapData(p_110131_1_, this.worldObj);
|
| 139 | mapdata.mapDecorations.remove("frame-" + this.getEntityId());
|
| 140 | }
|
| 141 |
|
| 142 | p_110131_1_.setItemFrame((EntityItemFrame)null);
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | public ItemStack getDisplayedItem()
|
| 147 | {
|
| 148 | return this.getDataWatcher().getWatchableObjectItemStack(8);
|
| 149 | }
|
| 150 |
|
| 151 | public void setDisplayedItem(ItemStack p_82334_1_)
|
| 152 | {
|
| 153 | this.setDisplayedItemWithUpdate(p_82334_1_, true);
|
| 154 | }
|
| 155 |
|
| 156 | private void setDisplayedItemWithUpdate(ItemStack p_174864_1_, boolean p_174864_2_)
|
| 157 | {
|
| 158 | if (p_174864_1_ != null)
|
| 159 | {
|
| 160 | p_174864_1_ = p_174864_1_.copy();
|
| 161 | p_174864_1_.stackSize = 1;
|
| 162 | p_174864_1_.setItemFrame(this);
|
| 163 | }
|
| 164 |
|
| 165 | this.getDataWatcher().updateObject(8, p_174864_1_);
|
| 166 | this.getDataWatcher().setObjectWatched(8);
|
| 167 |
|
| 168 | if (p_174864_2_ && this.hangingPosition != null)
|
| 169 | {
|
| 170 | this.worldObj.updateComparatorOutputLevel(this.hangingPosition, Blocks.air);
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | /**
|
| 175 | * Return the rotation of the item currently on this frame.
|
| 176 | */
|
| 177 | public int getRotation()
|
| 178 | {
|
| 179 | return this.getDataWatcher().getWatchableObjectByte(9);
|
| 180 | }
|
| 181 |
|
| 182 | public void setItemRotation(int p_82336_1_)
|
| 183 | {
|
| 184 | this.func_174865_a(p_82336_1_, true);
|
| 185 | }
|
| 186 |
|
| 187 | private void func_174865_a(int p_174865_1_, boolean p_174865_2_)
|
| 188 | {
|
| 189 | this.getDataWatcher().updateObject(9, Byte.valueOf((byte)(p_174865_1_ % 8)));
|
| 190 |
|
| 191 | if (p_174865_2_ && this.hangingPosition != null)
|
| 192 | {
|
| 193 | this.worldObj.updateComparatorOutputLevel(this.hangingPosition, Blocks.air);
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | /**
|
| 198 | * (abstract) Protected helper method to write subclass entity data to NBT.
|
| 199 | */
|
| 200 | public void writeEntityToNBT(NBTTagCompound tagCompound)
|
| 201 | {
|
| 202 | if (this.getDisplayedItem() != null)
|
| 203 | {
|
| 204 | tagCompound.setTag("Item", this.getDisplayedItem().writeToNBT(new NBTTagCompound()));
|
| 205 | tagCompound.setByte("ItemRotation", (byte)this.getRotation());
|
| 206 | tagCompound.setFloat("ItemDropChance", this.itemDropChance);
|
| 207 | }
|
| 208 |
|
| 209 | super.writeEntityToNBT(tagCompound);
|
| 210 | }
|
| 211 |
|
| 212 | /**
|
| 213 | * (abstract) Protected helper method to read subclass entity data from NBT.
|
| 214 | */
|
| 215 | public void readEntityFromNBT(NBTTagCompound tagCompund)
|
| 216 | {
|
| 217 | NBTTagCompound nbttagcompound = tagCompund.getCompoundTag("Item");
|
| 218 |
|
| 219 | if (nbttagcompound != null && !nbttagcompound.hasNoTags())
|
| 220 | {
|
| 221 | this.setDisplayedItemWithUpdate(ItemStack.loadItemStackFromNBT(nbttagcompound), false);
|
| 222 | this.func_174865_a(tagCompund.getByte("ItemRotation"), false);
|
| 223 |
|
| 224 | if (tagCompund.hasKey("ItemDropChance", 99))
|
| 225 | {
|
| 226 | this.itemDropChance = tagCompund.getFloat("ItemDropChance");
|
| 227 | }
|
| 228 |
|
| 229 | if (tagCompund.hasKey("Direction"))
|
| 230 | {
|
| 231 | this.func_174865_a(this.getRotation() * 2, false);
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | super.readEntityFromNBT(tagCompund);
|
| 236 | }
|
| 237 |
|
| 238 | /**
|
| 239 | * First layer of player interaction
|
| 240 | */
|
| 241 | public boolean interactFirst(EntityPlayer playerIn)
|
| 242 | {
|
| 243 | if (this.getDisplayedItem() == null)
|
| 244 | {
|
| 245 | ItemStack itemstack = playerIn.getHeldItem();
|
| 246 |
|
| 247 | if (itemstack != null && !this.worldObj.isRemote)
|
| 248 | {
|
| 249 | this.setDisplayedItem(itemstack);
|
| 250 |
|
| 251 | if (!playerIn.capabilities.isCreativeMode && --itemstack.stackSize <= 0)
|
| 252 | {
|
| 253 | playerIn.inventory.setInventorySlotContents(playerIn.inventory.currentItem, (ItemStack)null);
|
| 254 | }
|
| 255 | }
|
| 256 | }
|
| 257 | else if (!this.worldObj.isRemote)
|
| 258 | {
|
| 259 | this.setItemRotation(this.getRotation() + 1);
|
| 260 | }
|
| 261 |
|
| 262 | return true;
|
| 263 | }
|
| 264 |
|
| 265 | public int func_174866_q()
|
| 266 | {
|
| 267 | return this.getDisplayedItem() == null ? 0 : this.getRotation() % 8 + 1;
|
| 268 | }
|
| 269 | }
|
| 270 |
|