Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Honey Tank Block
- private static HoneyTankTileEntity getTileEntity(@javax.annotation.Nullable IBlockReader world, @javax.annotation.Nullable BlockPos pos) {
- TileEntity entity = world.getTileEntity(pos);
- if (entity instanceof HoneyTankTileEntity) {
- return (HoneyTankTileEntity) entity;
- }
- return null;
- }
- public static int getBlockColor(BlockState state, @javax.annotation.Nullable IBlockReader world, @javax.annotation.Nullable BlockPos pos, int tintIndex) {
- if (tintIndex == 1) {
- HoneyTankTileEntity tank = getTileEntity(world, pos);
- if (tank == null) return -1;
- if (tank.fluidTank.getFluid().getFluid() instanceof HoneyFlowingFluid) {
- HoneyFlowingFluid fluid = (HoneyFlowingFluid) tank.fluidTank.getFluid().getFluid();
- return fluid.getHoneyData().isRainbow() ? RainbowColor.getRGB() : fluid.getHoneyData().getHoneyColorInt();
- } else {
- return 0xFFF69909;
- }
- }
- return -1;
- }
- @Override
- public void animateTick(@Nonnull BlockState stateIn, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Random rand) {
- HoneyTankTileEntity tank = getTileEntity(world, pos);
- LOGGER.info("doing animate Tick");
- if (tank == null) {
- return;
- }
- if (tank.fluidTank.getFluid().getFluid() instanceof HoneyFlowingFluid) {
- HoneyFlowingFluid fluid = (HoneyFlowingFluid) tank.fluidTank.getFluid().getFluid();
- if (fluid.getHoneyData().isRainbow()) {
- world.notifyBlockUpdate(pos, stateIn, stateIn, 2);
- }
- }
- super.animateTick(stateIn, world, pos, rand);
- }
- @Nonnull
- @Override
- public ActionResultType onUse(@Nonnull BlockState state, World world, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand hand, @Nonnull BlockRayTraceResult blockRayTraceResult) {
- ItemStack heldItem = player.getHeldItem(hand);
- boolean usingHoney = heldItem.getItem() instanceof HoneyBottleItem;
- boolean usingBottle = heldItem.getItem() instanceof GlassBottleItem;
- boolean usingBucket = heldItem.getItem() instanceof BucketItem;
- TileEntity tileEntity = world.getTileEntity(pos);
- if (!world.isRemote) {
- if (tileEntity instanceof HoneyTankTileEntity) {
- HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
- if (usingBucket) {
- tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
- .ifPresent(iFluidHandler -> FluidUtil.interactWithFluidHandler(player, hand, world, pos, null));
- } else if (usingBottle) {
- world.playSound(player, pos, SoundEvents.ITEM_BOTTLE_EMPTY, SoundCategory.PLAYERS, 1.0f, 1.0f);
- tank.fillBottle(player, hand);
- } else if (usingHoney) {
- world.playSound(player, pos, SoundEvents.ITEM_BOTTLE_FILL, SoundCategory.PLAYERS, 1.0f, 1.0f);
- tank.emptyBottle(player, hand);
- }
- updateBlockState(world, pos);
- world.notifyBlockUpdate(pos, state, state, 2);
- }
- }
- if (usingBottle || usingBucket || usingHoney) {
- return ActionResultType.SUCCESS;
- }
- return ActionResultType.FAIL;
- }
- private void updateBlockState(World world, BlockPos pos) {
- BlockState state = world.getBlockState(pos);
- if (state.getBlock() instanceof HoneyTank) {
- world.setBlockState(pos, state.with(LEVEL, getLevel(world, pos)));
- }
- }
- // Honeytank Tile entity
- @Nonnull
- @Override
- public CompoundNBT write(CompoundNBT tag) {
- return super.write(getNBT(tag));
- }
- public CompoundNBT getNBT(CompoundNBT tag) {
- tag.putInt("tier", tier.tier);
- if (fluidTank.isEmpty()) return tag;
- tag.put("fluid", fluidTank.writeToNBT(new CompoundNBT()));
- return tag;
- }
- public void updateNBT(CompoundNBT tag) {
- fluidTank.readFromNBT(tag.getCompound("fluid"));
- tier = TankTier.getTier(tag.getInt("tier"));
- if (fluidTank.getTankCapacity(0) != tier.maxFillAmount) fluidTank.setCapacity(tier.maxFillAmount);
- if (fluidTank.getFluidAmount() > fluidTank.getTankCapacity(0))
- fluidTank.getFluid().setAmount(fluidTank.getTankCapacity(0));
- }
- @Nullable
- @Override
- public SUpdateTileEntityPacket getUpdatePacket() {
- return new SUpdateTileEntityPacket(pos, 0, getNBT(new CompoundNBT()));
- }
- @Override
- public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
- CompoundNBT nbt = pkt.getNbtCompound();
- updateNBT(nbt);
- }
- @Override
- public void fromTag(@Nonnull BlockState state, CompoundNBT tag) {
- updateNBT(tag);
- super.fromTag(state, tag);
- }
- @Override
- public void tick() {
- }
- @Override
- public void handleUpdateTag(BlockState state, CompoundNBT tag) {
- LOGGER.info("\n------------------------BIG ANNOYING CODE BLOCK-------------------\n" +
- "Updating Honey Tank Block\n" +
- "--------------------------------------------------------------------");
- world.notifyBlockUpdate(pos, state, state, 2);
- state.getBlock().animateTick(state, world, pos, world.rand);
- }
Add Comment
Please, Sign In to add comment