Vaerys_Dawn

Tank fails to update colour on chunk load

Dec 23rd, 2020 (edited)
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.50 KB | None | 0 0
  1. Honey Tank Block
  2.  
  3.     private static HoneyTankTileEntity getTileEntity(@javax.annotation.Nullable IBlockReader world, @javax.annotation.Nullable BlockPos pos) {
  4.         TileEntity entity = world.getTileEntity(pos);
  5.         if (entity instanceof HoneyTankTileEntity) {
  6.             return (HoneyTankTileEntity) entity;
  7.         }
  8.         return null;
  9.     }
  10.  
  11.     public static int getBlockColor(BlockState state, @javax.annotation.Nullable IBlockReader world, @javax.annotation.Nullable BlockPos pos, int tintIndex) {
  12.         if (tintIndex == 1) {
  13.             HoneyTankTileEntity tank = getTileEntity(world, pos);
  14.             if (tank == null) return -1;
  15.             if (tank.fluidTank.getFluid().getFluid() instanceof HoneyFlowingFluid) {
  16.                 HoneyFlowingFluid fluid = (HoneyFlowingFluid) tank.fluidTank.getFluid().getFluid();
  17.                 return fluid.getHoneyData().isRainbow() ? RainbowColor.getRGB() : fluid.getHoneyData().getHoneyColorInt();
  18.             } else {
  19.                 return 0xFFF69909;
  20.             }
  21.         }
  22.         return -1;
  23.     }
  24.  
  25.     @Override
  26.     public void animateTick(@Nonnull BlockState stateIn, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Random rand) {
  27.         HoneyTankTileEntity tank = getTileEntity(world, pos);
  28.         LOGGER.info("doing animate Tick");
  29.         if (tank == null) {
  30.             return;
  31.         }
  32.         if (tank.fluidTank.getFluid().getFluid() instanceof HoneyFlowingFluid) {
  33.             HoneyFlowingFluid fluid = (HoneyFlowingFluid) tank.fluidTank.getFluid().getFluid();
  34.             if (fluid.getHoneyData().isRainbow()) {
  35.                 world.notifyBlockUpdate(pos, stateIn, stateIn, 2);
  36.             }
  37.         }
  38.         super.animateTick(stateIn, world, pos, rand);
  39.     }
  40.  
  41.  @Nonnull
  42.     @Override
  43.     public ActionResultType onUse(@Nonnull BlockState state, World world, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand hand, @Nonnull BlockRayTraceResult blockRayTraceResult) {
  44.  
  45.         ItemStack heldItem = player.getHeldItem(hand);
  46.         boolean usingHoney = heldItem.getItem() instanceof HoneyBottleItem;
  47.         boolean usingBottle = heldItem.getItem() instanceof GlassBottleItem;
  48.         boolean usingBucket = heldItem.getItem() instanceof BucketItem;
  49.         TileEntity tileEntity = world.getTileEntity(pos);
  50.  
  51.         if (!world.isRemote) {
  52.             if (tileEntity instanceof HoneyTankTileEntity) {
  53.                 HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
  54.                 if (usingBucket) {
  55.                     tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
  56.                             .ifPresent(iFluidHandler -> FluidUtil.interactWithFluidHandler(player, hand, world, pos, null));
  57.                 } else if (usingBottle) {
  58.                     world.playSound(player, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.PLAYERS, 1.0f, 1.0f);
  59.                     tank.fillBottle(player, hand);
  60.                 } else if (usingHoney) {
  61.                     world.playSound(player, pos, SoundEvents.ITEM_BOTTLE_FILL, SoundCategory.PLAYERS, 1.0f, 1.0f);
  62.                     tank.emptyBottle(player, hand);
  63.                 }
  64.                 updateBlockState(world, pos);
  65.                 world.notifyBlockUpdate(pos, state, state, 2);
  66.             }
  67.         }
  68.         if (usingBottle || usingBucket || usingHoney) {
  69.             return ActionResultType.SUCCESS;
  70.         }
  71.         return ActionResultType.FAIL;
  72.     }
  73.  
  74.     private void updateBlockState(World world, BlockPos pos) {
  75.         BlockState state = world.getBlockState(pos);
  76.         if (state.getBlock() instanceof HoneyTank) {
  77.             world.setBlockState(pos, state.with(LEVEL, getLevel(world, pos)));
  78.         }
  79.     }
  80.  
  81.  
  82. // Honeytank Tile entity
  83.     @Nonnull
  84.     @Override
  85.     public CompoundNBT write(CompoundNBT tag) {
  86.         return super.write(getNBT(tag));
  87.     }
  88.  
  89.     public CompoundNBT getNBT(CompoundNBT tag) {
  90.         tag.putInt("tier", tier.tier);
  91.         if (fluidTank.isEmpty()) return tag;
  92.         tag.put("fluid", fluidTank.writeToNBT(new CompoundNBT()));
  93.         return tag;
  94.     }
  95.  
  96.     public void updateNBT(CompoundNBT tag) {
  97.         fluidTank.readFromNBT(tag.getCompound("fluid"));
  98.         tier = TankTier.getTier(tag.getInt("tier"));
  99.         if (fluidTank.getTankCapacity(0) != tier.maxFillAmount) fluidTank.setCapacity(tier.maxFillAmount);
  100.         if (fluidTank.getFluidAmount() > fluidTank.getTankCapacity(0))
  101.             fluidTank.getFluid().setAmount(fluidTank.getTankCapacity(0));
  102.     }
  103.  
  104.     @Nullable
  105.     @Override
  106.     public SUpdateTileEntityPacket getUpdatePacket() {
  107.         return new SUpdateTileEntityPacket(pos, 0, getNBT(new CompoundNBT()));
  108.     }
  109.  
  110.     @Override
  111.     public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
  112.         CompoundNBT nbt = pkt.getNbtCompound();
  113.         updateNBT(nbt);
  114.     }
  115.  
  116.     @Override
  117.     public void fromTag(@Nonnull BlockState state, CompoundNBT tag) {
  118.         updateNBT(tag);
  119.         super.fromTag(state, tag);
  120.     }
  121.  
  122.     @Override
  123.     public void tick() {
  124.  
  125.     }
  126.  
  127.     @Override
  128.     public void handleUpdateTag(BlockState state, CompoundNBT tag) {
  129.         LOGGER.info("\n------------------------BIG ANNOYING CODE BLOCK-------------------\n" +
  130.                 "Updating Honey Tank Block\n" +
  131.                 "--------------------------------------------------------------------");
  132.         world.notifyBlockUpdate(pos, state, state, 2);
  133.         state.getBlock().animateTick(state, world, pos, world.rand);
  134.     }
Add Comment
Please, Sign In to add comment