| 1 | package net.minecraft.tileentity;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.block.Block;
|
| 7 | import net.minecraft.block.BlockStainedGlass;
|
| 8 | import net.minecraft.block.BlockStainedGlassPane;
|
| 9 | import net.minecraft.block.state.IBlockState;
|
| 10 | import net.minecraft.entity.passive.EntitySheep;
|
| 11 | import net.minecraft.entity.player.EntityPlayer;
|
| 12 | import net.minecraft.entity.player.InventoryPlayer;
|
| 13 | import net.minecraft.init.Blocks;
|
| 14 | import net.minecraft.init.Items;
|
| 15 | import net.minecraft.inventory.Container;
|
| 16 | import net.minecraft.inventory.ContainerBeacon;
|
| 17 | import net.minecraft.inventory.IInventory;
|
| 18 | import net.minecraft.item.EnumDyeColor;
|
| 19 | import net.minecraft.item.ItemStack;
|
| 20 | import net.minecraft.nbt.NBTTagCompound;
|
| 21 | import net.minecraft.network.Packet;
|
| 22 | import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
| 23 | import net.minecraft.potion.Potion;
|
| 24 | import net.minecraft.potion.PotionEffect;
|
| 25 | import net.minecraft.stats.AchievementList;
|
| 26 | import net.minecraft.util.AxisAlignedBB;
|
| 27 | import net.minecraft.util.BlockPos;
|
| 28 | import net.minecraft.util.ITickable;
|
| 29 |
|
| 30 | public class TileEntityBeacon extends TileEntityLockable implements ITickable, IInventory
|
| 31 | {
|
| 32 | /** List of effects that Beacon can apply */
|
| 33 | public static final Potion[][] effectsList = new Potion[][] {{Potion.moveSpeed, Potion.digSpeed}, {Potion.resistance, Potion.jump}, {Potion.damageBoost}, {Potion.regeneration}};
|
| 34 | private final List<TileEntityBeacon.BeamSegment> beamSegments = Lists.<TileEntityBeacon.BeamSegment>newArrayList();
|
| 35 | private long beamRenderCounter;
|
| 36 | private float field_146014_j;
|
| 37 | private boolean isComplete;
|
| 38 |
|
| 39 | /** Level of this beacon's pyramid. */
|
| 40 | private int levels = -1;
|
| 41 |
|
| 42 | /** Primary potion effect given by this beacon. */
|
| 43 | private int primaryEffect;
|
| 44 |
|
| 45 | /** Secondary potion effect given by this beacon. */
|
| 46 | private int secondaryEffect;
|
| 47 |
|
| 48 | /** Item given to this beacon as payment. */
|
| 49 | private ItemStack payment;
|
| 50 | private String customName;
|
| 51 |
|
| 52 | /**
|
| 53 | * Like the old updateEntity(), except more generic.
|
| 54 | */
|
| 55 | public void update()
|
| 56 | {
|
| 57 | if (this.worldObj.getTotalWorldTime() % 80L == 0L)
|
| 58 | {
|
| 59 | this.updateBeacon();
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | public void updateBeacon()
|
| 64 | {
|
| 65 | this.updateSegmentColors();
|
| 66 | this.addEffectsToPlayers();
|
| 67 | }
|
| 68 |
|
| 69 | private void addEffectsToPlayers()
|
| 70 | {
|
| 71 | if (this.isComplete && this.levels > 0 && !this.worldObj.isRemote && this.primaryEffect > 0)
|
| 72 | {
|
| 73 | double d0 = (double)(this.levels * 10 + 10);
|
| 74 | int i = 0;
|
| 75 |
|
| 76 | if (this.levels >= 4 && this.primaryEffect == this.secondaryEffect)
|
| 77 | {
|
| 78 | i = 1;
|
| 79 | }
|
| 80 |
|
| 81 | int j = this.pos.getX();
|
| 82 | int k = this.pos.getY();
|
| 83 | int l = this.pos.getZ();
|
| 84 | AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double)j, (double)k, (double)l, (double)(j + 1), (double)(k + 1), (double)(l + 1))).expand(d0, d0, d0).addCoord(0.0D, (double)this.worldObj.getHeight(), 0.0D);
|
| 85 | List<EntityPlayer> list = this.worldObj.<EntityPlayer>getEntitiesWithinAABB(EntityPlayer.class, axisalignedbb);
|
| 86 |
|
| 87 | for (EntityPlayer entityplayer : list)
|
| 88 | {
|
| 89 | entityplayer.addPotionEffect(new PotionEffect(this.primaryEffect, 180, i, true, true));
|
| 90 | }
|
| 91 |
|
| 92 | if (this.levels >= 4 && this.primaryEffect != this.secondaryEffect && this.secondaryEffect > 0)
|
| 93 | {
|
| 94 | for (EntityPlayer entityplayer1 : list)
|
| 95 | {
|
| 96 | entityplayer1.addPotionEffect(new PotionEffect(this.secondaryEffect, 180, 0, true, true));
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private void updateSegmentColors()
|
| 103 | {
|
| 104 | int i = this.levels;
|
| 105 | int j = this.pos.getX();
|
| 106 | int k = this.pos.getY();
|
| 107 | int l = this.pos.getZ();
|
| 108 | this.levels = 0;
|
| 109 | this.beamSegments.clear();
|
| 110 | this.isComplete = true;
|
| 111 | TileEntityBeacon.BeamSegment tileentitybeacon$beamsegment = new TileEntityBeacon.BeamSegment(EntitySheep.getDyeRgb(EnumDyeColor.WHITE));
|
| 112 | this.beamSegments.add(tileentitybeacon$beamsegment);
|
| 113 | boolean flag = true;
|
| 114 | BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
|
| 115 |
|
| 116 | for (int i1 = k + 1; i1 < 256; ++i1)
|
| 117 | {
|
| 118 | IBlockState iblockstate = this.worldObj.getBlockState(blockpos$mutableblockpos.set(j, i1, l));
|
| 119 | float[] afloat;
|
| 120 |
|
| 121 | if (iblockstate.getBlock() == Blocks.stained_glass)
|
| 122 | {
|
| 123 | afloat = EntitySheep.getDyeRgb((EnumDyeColor)iblockstate.getValue(BlockStainedGlass.COLOR));
|
| 124 | }
|
| 125 | else
|
| 126 | {
|
| 127 | if (iblockstate.getBlock() != Blocks.stained_glass_pane)
|
| 128 | {
|
| 129 | if (iblockstate.getBlock().getLightOpacity() >= 15 && iblockstate.getBlock() != Blocks.bedrock)
|
| 130 | {
|
| 131 | this.isComplete = false;
|
| 132 | this.beamSegments.clear();
|
| 133 | break;
|
| 134 | }
|
| 135 |
|
| 136 | tileentitybeacon$beamsegment.incrementHeight();
|
| 137 | continue;
|
| 138 | }
|
| 139 |
|
| 140 | afloat = EntitySheep.getDyeRgb((EnumDyeColor)iblockstate.getValue(BlockStainedGlassPane.COLOR));
|
| 141 | }
|
| 142 |
|
| 143 | if (!flag)
|
| 144 | {
|
| 145 | afloat = new float[] {(tileentitybeacon$beamsegment.getColors()[0] + afloat[0]) / 2.0F, (tileentitybeacon$beamsegment.getColors()[1] + afloat[1]) / 2.0F, (tileentitybeacon$beamsegment.getColors()[2] + afloat[2]) / 2.0F};
|
| 146 | }
|
| 147 |
|
| 148 | if (Arrays.equals(afloat, tileentitybeacon$beamsegment.getColors()))
|
| 149 | {
|
| 150 | tileentitybeacon$beamsegment.incrementHeight();
|
| 151 | }
|
| 152 | else
|
| 153 | {
|
| 154 | tileentitybeacon$beamsegment = new TileEntityBeacon.BeamSegment(afloat);
|
| 155 | this.beamSegments.add(tileentitybeacon$beamsegment);
|
| 156 | }
|
| 157 |
|
| 158 | flag = false;
|
| 159 | }
|
| 160 |
|
| 161 | if (this.isComplete)
|
| 162 | {
|
| 163 | for (int l1 = 1; l1 <= 4; this.levels = l1++)
|
| 164 | {
|
| 165 | int i2 = k - l1;
|
| 166 |
|
| 167 | if (i2 < 0)
|
| 168 | {
|
| 169 | break;
|
| 170 | }
|
| 171 |
|
| 172 | boolean flag1 = true;
|
| 173 |
|
| 174 | for (int j1 = j - l1; j1 <= j + l1 && flag1; ++j1)
|
| 175 | {
|
| 176 | for (int k1 = l - l1; k1 <= l + l1; ++k1)
|
| 177 | {
|
| 178 | Block block = this.worldObj.getBlockState(new BlockPos(j1, i2, k1)).getBlock();
|
| 179 |
|
| 180 | if (block != Blocks.emerald_block && block != Blocks.gold_block && block != Blocks.diamond_block && block != Blocks.iron_block)
|
| 181 | {
|
| 182 | flag1 = false;
|
| 183 | break;
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | if (!flag1)
|
| 189 | {
|
| 190 | break;
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | if (this.levels == 0)
|
| 195 | {
|
| 196 | this.isComplete = false;
|
| 197 | }
|
| 198 | }
|
| 199 |
|
| 200 | if (!this.worldObj.isRemote && this.levels == 4 && i < this.levels)
|
| 201 | {
|
| 202 | for (EntityPlayer entityplayer : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, (new AxisAlignedBB((double)j, (double)k, (double)l, (double)j, (double)(k - 4), (double)l)).expand(10.0D, 5.0D, 10.0D)))
|
| 203 | {
|
| 204 | entityplayer.triggerAchievement(AchievementList.fullBeacon);
|
| 205 | }
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | public List<TileEntityBeacon.BeamSegment> getBeamSegments()
|
| 210 | {
|
| 211 | return this.beamSegments;
|
| 212 | }
|
| 213 |
|
| 214 | public float shouldBeamRender()
|
| 215 | {
|
| 216 | if (!this.isComplete)
|
| 217 | {
|
| 218 | return 0.0F;
|
| 219 | }
|
| 220 | else
|
| 221 | {
|
| 222 | int i = (int)(this.worldObj.getTotalWorldTime() - this.beamRenderCounter);
|
| 223 | this.beamRenderCounter = this.worldObj.getTotalWorldTime();
|
| 224 |
|
| 225 | if (i > 1)
|
| 226 | {
|
| 227 | this.field_146014_j -= (float)i / 40.0F;
|
| 228 |
|
| 229 | if (this.field_146014_j < 0.0F)
|
| 230 | {
|
| 231 | this.field_146014_j = 0.0F;
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | this.field_146014_j += 0.025F;
|
| 236 |
|
| 237 | if (this.field_146014_j > 1.0F)
|
| 238 | {
|
| 239 | this.field_146014_j = 1.0F;
|
| 240 | }
|
| 241 |
|
| 242 | return this.field_146014_j;
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | /**
|
| 247 | * Allows for a specialized description packet to be created. This is often used to sync tile entity data from the
|
| 248 | * server to the client easily. For example this is used by signs to synchronise the text to be displayed.
|
| 249 | */
|
| 250 | public Packet getDescriptionPacket()
|
| 251 | {
|
| 252 | NBTTagCompound nbttagcompound = new NBTTagCompound();
|
| 253 | this.writeToNBT(nbttagcompound);
|
| 254 | return new S35PacketUpdateTileEntity(this.pos, 3, nbttagcompound);
|
| 255 | }
|
| 256 |
|
| 257 | public double getMaxRenderDistanceSquared()
|
| 258 | {
|
| 259 | return 65536.0D;
|
| 260 | }
|
| 261 |
|
| 262 | private int func_183001_h(int p_183001_1_)
|
| 263 | {
|
| 264 | if (p_183001_1_ >= 0 && p_183001_1_ < Potion.potionTypes.length && Potion.potionTypes[p_183001_1_] != null)
|
| 265 | {
|
| 266 | Potion potion = Potion.potionTypes[p_183001_1_];
|
| 267 | return potion != Potion.moveSpeed && potion != Potion.digSpeed && potion != Potion.resistance && potion != Potion.jump && potion != Potion.damageBoost && potion != Potion.regeneration ? 0 : p_183001_1_;
|
| 268 | }
|
| 269 | else
|
| 270 | {
|
| 271 | return 0;
|
| 272 | }
|
| 273 | }
|
| 274 |
|
| 275 | public void readFromNBT(NBTTagCompound compound)
|
| 276 | {
|
| 277 | super.readFromNBT(compound);
|
| 278 | this.primaryEffect = this.func_183001_h(compound.getInteger("Primary"));
|
| 279 | this.secondaryEffect = this.func_183001_h(compound.getInteger("Secondary"));
|
| 280 | this.levels = compound.getInteger("Levels");
|
| 281 | }
|
| 282 |
|
| 283 | public void writeToNBT(NBTTagCompound compound)
|
| 284 | {
|
| 285 | super.writeToNBT(compound);
|
| 286 | compound.setInteger("Primary", this.primaryEffect);
|
| 287 | compound.setInteger("Secondary", this.secondaryEffect);
|
| 288 | compound.setInteger("Levels", this.levels);
|
| 289 | }
|
| 290 |
|
| 291 | /**
|
| 292 | * Returns the number of slots in the inventory.
|
| 293 | */
|
| 294 | public int getSizeInventory()
|
| 295 | {
|
| 296 | return 1;
|
| 297 | }
|
| 298 |
|
| 299 | /**
|
| 300 | * Returns the stack in the given slot.
|
| 301 | */
|
| 302 | public ItemStack getStackInSlot(int index)
|
| 303 | {
|
| 304 | return index == 0 ? this.payment : null;
|
| 305 | }
|
| 306 |
|
| 307 | /**
|
| 308 | * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
|
| 309 | */
|
| 310 | public ItemStack decrStackSize(int index, int count)
|
| 311 | {
|
| 312 | if (index == 0 && this.payment != null)
|
| 313 | {
|
| 314 | if (count >= this.payment.stackSize)
|
| 315 | {
|
| 316 | ItemStack itemstack = this.payment;
|
| 317 | this.payment = null;
|
| 318 | return itemstack;
|
| 319 | }
|
| 320 | else
|
| 321 | {
|
| 322 | this.payment.stackSize -= count;
|
| 323 | return new ItemStack(this.payment.getItem(), count, this.payment.getMetadata());
|
| 324 | }
|
| 325 | }
|
| 326 | else
|
| 327 | {
|
| 328 | return null;
|
| 329 | }
|
| 330 | }
|
| 331 |
|
| 332 | /**
|
| 333 | * Removes a stack from the given slot and returns it.
|
| 334 | */
|
| 335 | public ItemStack removeStackFromSlot(int index)
|
| 336 | {
|
| 337 | if (index == 0 && this.payment != null)
|
| 338 | {
|
| 339 | ItemStack itemstack = this.payment;
|
| 340 | this.payment = null;
|
| 341 | return itemstack;
|
| 342 | }
|
| 343 | else
|
| 344 | {
|
| 345 | return null;
|
| 346 | }
|
| 347 | }
|
| 348 |
|
| 349 | /**
|
| 350 | * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
|
| 351 | */
|
| 352 | public void setInventorySlotContents(int index, ItemStack stack)
|
| 353 | {
|
| 354 | if (index == 0)
|
| 355 | {
|
| 356 | this.payment = stack;
|
| 357 | }
|
| 358 | }
|
| 359 |
|
| 360 | /**
|
| 361 | * Get the name of this object. For players this returns their username
|
| 362 | */
|
| 363 | public String getName()
|
| 364 | {
|
| 365 | return this.hasCustomName() ? this.customName : "container.beacon";
|
| 366 | }
|
| 367 |
|
| 368 | /**
|
| 369 | * Returns true if this thing is named
|
| 370 | */
|
| 371 | public boolean hasCustomName()
|
| 372 | {
|
| 373 | return this.customName != null && this.customName.length() > 0;
|
| 374 | }
|
| 375 |
|
| 376 | public void setName(String name)
|
| 377 | {
|
| 378 | this.customName = name;
|
| 379 | }
|
| 380 |
|
| 381 | /**
|
| 382 | * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
|
| 383 | */
|
| 384 | public int getInventoryStackLimit()
|
| 385 | {
|
| 386 | return 1;
|
| 387 | }
|
| 388 |
|
| 389 | /**
|
| 390 | * Do not make give this method the name canInteractWith because it clashes with Container
|
| 391 | */
|
| 392 | public boolean isUseableByPlayer(EntityPlayer player)
|
| 393 | {
|
| 394 | return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
|
| 395 | }
|
| 396 |
|
| 397 | public void openInventory(EntityPlayer player)
|
| 398 | {
|
| 399 | }
|
| 400 |
|
| 401 | public void closeInventory(EntityPlayer player)
|
| 402 | {
|
| 403 | }
|
| 404 |
|
| 405 | /**
|
| 406 | * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
|
| 407 | */
|
| 408 | public boolean isItemValidForSlot(int index, ItemStack stack)
|
| 409 | {
|
| 410 | return stack.getItem() == Items.emerald || stack.getItem() == Items.diamond || stack.getItem() == Items.gold_ingot || stack.getItem() == Items.iron_ingot;
|
| 411 | }
|
| 412 |
|
| 413 | public String getGuiID()
|
| 414 | {
|
| 415 | return "minecraft:beacon";
|
| 416 | }
|
| 417 |
|
| 418 | public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
|
| 419 | {
|
| 420 | return new ContainerBeacon(playerInventory, this);
|
| 421 | }
|
| 422 |
|
| 423 | public int getField(int id)
|
| 424 | {
|
| 425 | switch (id)
|
| 426 | {
|
| 427 | case 0:
|
| 428 | return this.levels;
|
| 429 |
|
| 430 | case 1:
|
| 431 | return this.primaryEffect;
|
| 432 |
|
| 433 | case 2:
|
| 434 | return this.secondaryEffect;
|
| 435 |
|
| 436 | default:
|
| 437 | return 0;
|
| 438 | }
|
| 439 | }
|
| 440 |
|
| 441 | public void setField(int id, int value)
|
| 442 | {
|
| 443 | switch (id)
|
| 444 | {
|
| 445 | case 0:
|
| 446 | this.levels = value;
|
| 447 | break;
|
| 448 |
|
| 449 | case 1:
|
| 450 | this.primaryEffect = this.func_183001_h(value);
|
| 451 | break;
|
| 452 |
|
| 453 | case 2:
|
| 454 | this.secondaryEffect = this.func_183001_h(value);
|
| 455 | }
|
| 456 | }
|
| 457 |
|
| 458 | public int getFieldCount()
|
| 459 | {
|
| 460 | return 3;
|
| 461 | }
|
| 462 |
|
| 463 | public void clear()
|
| 464 | {
|
| 465 | this.payment = null;
|
| 466 | }
|
| 467 |
|
| 468 | public boolean receiveClientEvent(int id, int type)
|
| 469 | {
|
| 470 | if (id == 1)
|
| 471 | {
|
| 472 | this.updateBeacon();
|
| 473 | return true;
|
| 474 | }
|
| 475 | else
|
| 476 | {
|
| 477 | return super.receiveClientEvent(id, type);
|
| 478 | }
|
| 479 | }
|
| 480 |
|
| 481 | public static class BeamSegment
|
| 482 | {
|
| 483 | private final float[] colors;
|
| 484 | private int height;
|
| 485 |
|
| 486 | public BeamSegment(float[] p_i45669_1_)
|
| 487 | {
|
| 488 | this.colors = p_i45669_1_;
|
| 489 | this.height = 1;
|
| 490 | }
|
| 491 |
|
| 492 | protected void incrementHeight()
|
| 493 | {
|
| 494 | ++this.height;
|
| 495 | }
|
| 496 |
|
| 497 | public float[] getColors()
|
| 498 | {
|
| 499 | return this.colors;
|
| 500 | }
|
| 501 |
|
| 502 | public int getHeight()
|
| 503 | {
|
| 504 | return this.height;
|
| 505 | }
|
| 506 | }
|
| 507 | }
|
| 508 |
|