Guest User

TileEntity

a guest
Aug 14th, 2020
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class PurpleCampfireTileEntity extends TileEntity implements IClearable, ITickableTileEntity {
  2.        private final NonNullList<ItemStack> inventory = NonNullList.withSize(4, ItemStack.EMPTY);
  3.        private final int[] cookingTimes = new int[4];
  4.        private final int[] cookingTotalTimes = new int[4];
  5.  
  6.        public PurpleCampfireTileEntity() {
  7.           super(TileEntityList.CAMPFIRE.get());
  8.        }
  9.  
  10.        public void tick() {
  11.           boolean flag = this.getBlockState().get(PurpleCampfire.LIT);
  12.           boolean flag1 = this.world.isRemote;
  13.           if (flag1) {
  14.              if (flag) {
  15.                 this.addParticles();
  16.              }
  17.  
  18.           } else {
  19.              if (flag) {
  20.                 this.cookAndDrop();
  21.              } else {
  22.                 for(int i = 0; i < this.inventory.size(); ++i) {
  23.                    if (this.cookingTimes[i] > 0) {
  24.                       this.cookingTimes[i] = MathHelper.clamp(this.cookingTimes[i] - 2, 0, this.cookingTotalTimes[i]);
  25.                    }
  26.                 }
  27.              }
  28.  
  29.           }
  30.        }
  31.  
  32.        /**
  33.         * Individually tracks the cooking of each item, then spawns the finished product in-world and clears the
  34.         * corresponding held item.
  35.         */
  36.        private void cookAndDrop() {
  37.           for(int i = 0; i < this.inventory.size(); ++i) {
  38.              ItemStack itemstack = this.inventory.get(i);
  39.              if (!itemstack.isEmpty()) {
  40.                 int j = this.cookingTimes[i]++;
  41.                 if (this.cookingTimes[i] >= this.cookingTotalTimes[i]) {
  42.                    IInventory iinventory = new Inventory(itemstack);
  43.                    ItemStack itemstack1 = this.world.getRecipeManager().getRecipe(IRecipeType.CAMPFIRE_COOKING, iinventory, this.world).map((p_213979_1_) -> {
  44.                       return p_213979_1_.getCraftingResult(iinventory);
  45.                    }).orElse(itemstack);
  46.                    BlockPos blockpos = this.getPos();
  47.                    InventoryHelper.spawnItemStack(this.world, (double)blockpos.getX(), (double)blockpos.getY(), (double)blockpos.getZ(), itemstack1);
  48.                    this.inventory.set(i, ItemStack.EMPTY);
  49.                    this.inventoryChanged();
  50.                 }
  51.              }
  52.           }
  53.  
  54.        }
  55.  
  56.        private void addParticles() {
  57.           World world = this.getWorld();
  58.           if (world != null) {
  59.              BlockPos blockpos = this.getPos();
  60.              Random random = world.rand;
  61.              if (random.nextFloat() < 0.11F) {
  62.                 for(int i = 0; i < random.nextInt(2) + 2; ++i) {
  63.                    PurpleCampfire.spawnSmokeParticles(world, blockpos, this.getBlockState().get(PurpleCampfire.SIGNAL_FIRE), false);
  64.                 }
  65.              }
  66.  
  67.              int l = this.getBlockState().get(PurpleCampfire.FACING).getHorizontalIndex();
  68.  
  69.              for(int j = 0; j < this.inventory.size(); ++j) {
  70.                 if (!this.inventory.get(j).isEmpty() && random.nextFloat() < 0.2F) {
  71.                    Direction direction = Direction.byHorizontalIndex(Math.floorMod(j + l, 4));
  72.                    float f = 0.3125F;
  73.                    double d0 = (double)blockpos.getX() + 0.5D - (double)((float)direction.getXOffset() * 0.3125F) + (double)((float)direction.rotateY().getXOffset() * 0.3125F);
  74.                    double d1 = (double)blockpos.getY() + 0.5D;
  75.                    double d2 = (double)blockpos.getZ() + 0.5D - (double)((float)direction.getZOffset() * 0.3125F) + (double)((float)direction.rotateY().getZOffset() * 0.3125F);
  76.  
  77.                    for(int k = 0; k < 4; ++k) {
  78.                       world.addParticle(ParticleTypes.SMOKE, d0, d1, d2, 0.0D, 5.0E-4D, 0.0D);
  79.                    }
  80.                 }
  81.              }
  82.  
  83.           }
  84.        }
  85.  
  86.        /**
  87.         * Returns a NonNullList<ItemStack> of items currently held in the campfire.
  88.         */
  89.        public NonNullList<ItemStack> getInventory() {
  90.           return this.inventory;
  91.        }
  92.  
  93.        public void read(BlockState state, CompoundNBT nbt) {
  94.           super.read(state, nbt);
  95.           this.inventory.clear();
  96.           ItemStackHelper.loadAllItems(nbt, this.inventory);
  97.           if (nbt.contains("CookingTimes", 11)) {
  98.              int[] aint = nbt.getIntArray("CookingTimes");
  99.              System.arraycopy(aint, 0, this.cookingTimes, 0, Math.min(this.cookingTotalTimes.length, aint.length));
  100.           }
  101.  
  102.           if (nbt.contains("CookingTotalTimes", 11)) {
  103.              int[] aint1 = nbt.getIntArray("CookingTotalTimes");
  104.              System.arraycopy(aint1, 0, this.cookingTotalTimes, 0, Math.min(this.cookingTotalTimes.length, aint1.length));
  105.           }
  106.  
  107.        }
  108.  
  109.        public CompoundNBT write(CompoundNBT compound) {
  110.           this.writeItems(compound);
  111.           compound.putIntArray("CookingTimes", this.cookingTimes);
  112.           compound.putIntArray("CookingTotalTimes", this.cookingTotalTimes);
  113.           return compound;
  114.        }
  115.  
  116.        private CompoundNBT writeItems(CompoundNBT compound) {
  117.           super.write(compound);
  118.           ItemStackHelper.saveAllItems(compound, this.inventory, true);
  119.           return compound;
  120.        }
  121.  
  122.        /**
  123.         * Retrieves packet to send to the client whenever this Tile Entity is resynced via World.notifyBlockUpdate. For
  124.         * modded TE's, this packet comes back to you clientside in {@link #onDataPacket}
  125.         */
  126.        @Nullable
  127.        public SUpdateTileEntityPacket getUpdatePacket() {
  128.           return new SUpdateTileEntityPacket(this.pos, 13, this.getUpdateTag());
  129.        }
  130.  
  131.        /**
  132.         * Get an NBT compound to sync to the client with SPacketChunkData, used for initial loading of the chunk or when
  133.         * many blocks change at once. This compound comes back to you clientside in {@link handleUpdateTag}
  134.         */
  135.        public CompoundNBT getUpdateTag() {
  136.           return this.writeItems(new CompoundNBT());
  137.        }
  138.  
  139.        public Optional<CampfireCookingRecipe> findMatchingRecipe(ItemStack itemStackIn) {
  140.           return this.inventory.stream().noneMatch(ItemStack::isEmpty) ? Optional.empty() : this.world.getRecipeManager().getRecipe(IRecipeType.CAMPFIRE_COOKING, new Inventory(itemStackIn), this.world);
  141.        }
  142.  
  143.        public boolean addItem(ItemStack itemStackIn, int cookTime) {
  144.           for(int i = 0; i < this.inventory.size(); ++i) {
  145.              ItemStack itemstack = this.inventory.get(i);
  146.              if (itemstack.isEmpty()) {
  147.                 this.cookingTotalTimes[i] = cookTime;
  148.                 this.cookingTimes[i] = 0;
  149.                 this.inventory.set(i, itemStackIn.split(1));
  150.                 this.inventoryChanged();
  151.                 return true;
  152.              }
  153.           }
  154.  
  155.           return false;
  156.        }
  157.  
  158.        private void inventoryChanged() {
  159.           this.markDirty();
  160.           this.getWorld().notifyBlockUpdate(this.getPos(), this.getBlockState(), this.getBlockState(), 3);
  161.        }
  162.  
  163.        public void clear() {
  164.           this.inventory.clear();
  165.        }
  166.  
  167.        public void dropAllItems() {
  168.           if (this.world != null) {
  169.              if (!this.world.isRemote) {
  170.                 InventoryHelper.dropItems(this.world, this.getPos(), this.getInventory());
  171.              }
  172.  
  173.              this.inventoryChanged();
  174.           }
  175.  
  176.        }
  177. }
RAW Paste Data