Vaerys_Dawn

Honey Tank Tile Entity

Dec 11th, 2020
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.57 KB | None | 0 0
  1.     public static final RegistryObject<TileEntityType<?>> HONEY_TANK_TILE_ENTITY = TILE_ENTITY_TYPES.register("honey_tank", () -> TileEntityType.Builder
  2.             .create(() -> new HoneyTankTileEntity(4000), ModBlocks.PURPUR_HONEY_TANK.get(), ModBlocks.NETHER_HONEY_TANK.get(), ModBlocks.WOODEN_HONEY_TANK.get())
  3.             .build(null));
  4.  
  5. public static final RegistryObject<Block> PURPUR_HONEY_TANK = BLOCKS.register("purpur_honey_tank", () -> new HoneyTank(HoneyTank.PROPERTIES, 64000, HoneyTank.TankType.PURPUR));
  6.     public static final RegistryObject<Block> NETHER_HONEY_TANK = BLOCKS.register("nether_honey_tank", () -> new HoneyTank(HoneyTank.PROPERTIES, 16000, HoneyTank.TankType.NETHER));
  7.     public static final RegistryObject<Block> WOODEN_HONEY_TANK = BLOCKS.register("wooden_honey_tank", () -> new HoneyTank(HoneyTank.PROPERTIES, 4000, HoneyTank.TankType.WOODEN));
  8.  
  9.  
  10. package com.resourcefulbees.resourcefulbees.block;
  11.  
  12. import com.resourcefulbees.resourcefulbees.tileentity.HoneyTankTileEntity;
  13. import net.minecraft.block.AbstractBlock;
  14. import net.minecraft.block.Block;
  15. import net.minecraft.block.BlockState;
  16. import net.minecraft.block.SoundType;
  17. import net.minecraft.block.material.Material;
  18. import net.minecraft.entity.player.PlayerEntity;
  19. import net.minecraft.fluid.FluidState;
  20. import net.minecraft.fluid.Fluids;
  21. import net.minecraft.item.*;
  22. import net.minecraft.state.BooleanProperty;
  23. import net.minecraft.state.IntegerProperty;
  24. import net.minecraft.state.StateContainer;
  25. import net.minecraft.state.properties.BlockStateProperties;
  26. import net.minecraft.tileentity.TileEntity;
  27. import net.minecraft.util.ActionResultType;
  28. import net.minecraft.util.Direction;
  29. import net.minecraft.util.Hand;
  30. import net.minecraft.util.math.BlockPos;
  31. import net.minecraft.util.math.BlockRayTraceResult;
  32. import net.minecraft.util.math.shapes.ISelectionContext;
  33. import net.minecraft.util.math.shapes.VoxelShape;
  34. import net.minecraft.world.IBlockReader;
  35. import net.minecraft.world.IWorld;
  36. import net.minecraft.world.World;
  37. import net.minecraftforge.fluids.FluidUtil;
  38. import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
  39. import org.jetbrains.annotations.Nullable;
  40.  
  41. import javax.annotation.Nonnull;
  42.  
  43. public class HoneyTank extends Block {
  44.  
  45.     protected static final VoxelShape VOXEL_SHAPE = Block.makeCuboidShape(1.0D, 0.0D, 1.0D, 15.0D, 16.0D, 15.0D);
  46.  
  47.     public static final IntegerProperty LEVEL = IntegerProperty.create("level", 0, 14);
  48.     public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
  49.  
  50.     public static final AbstractBlock.Properties PROPERTIES = Block.Properties.create(Material.GLASS)
  51.             .sound(SoundType.GLASS)
  52.             .nonOpaque();
  53.  
  54.     public final int maxHoneyAmount;
  55.  
  56.     public HoneyTank(Properties properties, int maxHoneyAmount) {
  57.         super(properties);
  58.         this.maxHoneyAmount = maxHoneyAmount;
  59.         BlockState defaultState = this.stateContainer.getBaseState()
  60.                 .with(LEVEL, 0)
  61.                 .with(WATERLOGGED, false);
  62.         this.setDefaultState(defaultState);
  63.     }
  64.  
  65.     @Nullable
  66.     @Override
  67.     public TileEntity createTileEntity(BlockState state, IBlockReader world) {
  68.         return new HoneyTankTileEntity(maxHoneyAmount);
  69.     }
  70.  
  71.     @Override
  72.     public boolean hasTileEntity(BlockState state) {
  73.         return true;
  74.     }
  75.  
  76.     @Nonnull
  77.     @Override
  78.     public ActionResultType onUse(@Nonnull BlockState state, World world, @Nonnull BlockPos pos, @Nonnull PlayerEntity player, @Nonnull Hand hand, @Nonnull BlockRayTraceResult blockRayTraceResult) {
  79.         if (!world.isRemote) {
  80.             ItemStack heldItem = player.getHeldItem(hand);
  81.             boolean usingHoney = heldItem.getItem() instanceof HoneyBottleItem;
  82.             boolean usingBottle = heldItem.getItem() instanceof GlassBottleItem;
  83.             boolean usingBucket = heldItem.getItem() instanceof BucketItem;
  84.             TileEntity tileEntity = world.getTileEntity(pos);
  85.  
  86.             if (tileEntity instanceof HoneyTankTileEntity) {
  87.                 HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
  88.                 if (usingBucket) {
  89.                     tileEntity.getCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
  90.                             .ifPresent(iFluidHandler -> FluidUtil.interactWithFluidHandler(player, hand, world, pos, null));
  91.                 } else if (usingBottle) {
  92.                     tank.fillBottle(player, hand, world, pos, null);
  93.                 } else if (usingHoney) {
  94.                     tank.emptyBottle(player, hand, world, pos, null);
  95.                 }
  96.                 updateBlockState(world, pos);
  97.             }
  98.         }
  99.         return ActionResultType.SUCCESS;
  100.     }
  101.  
  102.     private void updateBlockState(World world, BlockPos pos) {
  103.         BlockState state = world.getBlockState(pos);
  104.         if (state.getBlock() instanceof HoneyTank) {
  105.             world.setBlockState(pos, state.with(LEVEL, getLevel(world, pos)));
  106.         }
  107.     }
  108.  
  109.  
  110.     @Override
  111.     public FluidState getFluidState(BlockState state) {
  112.         return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : Fluids.EMPTY.getDefaultState();
  113.     }
  114.  
  115.     @Override
  116.     public BlockState getStateForPlacement(BlockItemUseContext context) {
  117.         FluidState fluidState = context.getWorld().getFluidState(context.getPos());
  118.         return this.getDefaultState();
  119.     }
  120.  
  121.     @Override
  122.     protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  123.         builder.add(WATERLOGGED, LEVEL);
  124.     }
  125.  
  126.     @Override
  127.     public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
  128.         return VOXEL_SHAPE;
  129.     }
  130.  
  131.     @Override
  132.     public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld world, BlockPos currentPos, BlockPos facingPos) {
  133.         if (stateIn.get(WATERLOGGED)) {
  134.             world.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(world));
  135.         }
  136.         if (!world.isRemote()) {
  137.             stateIn.with(LEVEL, getLevel(world, currentPos));
  138.         }
  139.         return stateIn;
  140.     }
  141.  
  142.     private int getLevel(IWorld world, BlockPos currentPos) {
  143.         TileEntity tileEntity = world.getTileEntity(currentPos);
  144.         if (tileEntity instanceof HoneyTankTileEntity) {
  145.             HoneyTankTileEntity tank = (HoneyTankTileEntity) tileEntity;
  146.             float fillPercentage = ((float) tank.fluidTank.getFluidAmount()) / ((float) tank.fluidTank.getTankCapacity(0));
  147.             return (int) (fillPercentage * 14);
  148.         }
  149.         return 0;
  150.     }
  151. }
  152.  
  153. package com.resourcefulbees.resourcefulbees.tileentity;
  154.  
  155. import com.resourcefulbees.resourcefulbees.lib.ModConstants;
  156. import com.resourcefulbees.resourcefulbees.registry.ModFluids;
  157. import com.resourcefulbees.resourcefulbees.registry.ModTileEntityTypes;
  158. import com.resourcefulbees.resourcefulbees.utils.BeeInfoUtils;
  159. import net.minecraft.entity.player.PlayerEntity;
  160. import net.minecraft.item.ItemStack;
  161. import net.minecraft.item.Items;
  162. import net.minecraft.tileentity.ITickableTileEntity;
  163. import net.minecraft.tileentity.TileEntity;
  164. import net.minecraft.util.Direction;
  165. import net.minecraft.util.Hand;
  166. import net.minecraft.util.math.BlockPos;
  167. import net.minecraft.world.World;
  168. import net.minecraftforge.common.capabilities.Capability;
  169. import net.minecraftforge.common.util.LazyOptional;
  170. import net.minecraftforge.fluids.FluidStack;
  171. import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
  172. import net.minecraftforge.fluids.capability.IFluidHandler;
  173. import net.minecraftforge.fluids.capability.templates.FluidTank;
  174. import org.apache.logging.log4j.LogManager;
  175. import org.apache.logging.log4j.Logger;
  176.  
  177. import javax.annotation.Nonnull;
  178. import javax.annotation.Nullable;
  179. import java.util.function.Predicate;
  180.  
  181. public class HoneyTankTileEntity extends TileEntity implements ITickableTileEntity {
  182.  
  183.     public static final Logger LOGGER = LogManager.getLogger();
  184.  
  185.     public final FluidTank fluidTank;
  186.     private final LazyOptional<IFluidHandler> fluidOptional;
  187.     public static final FluidStack HONEY_BOTTLE_FLUID_STACK = new FluidStack(ModFluids.HONEY_STILL.get(), ModConstants.HONEY_PER_BOTTLE);
  188.  
  189.     public HoneyTankTileEntity(int maxHoneyAmount) {
  190.         super(ModTileEntityTypes.HONEY_TANK_TILE_ENTITY.get());
  191.         fluidTank = new FluidTank(maxHoneyAmount, honeyFluidPredicate());
  192.         fluidOptional = LazyOptional.of(() -> fluidTank);
  193.     }
  194.  
  195.     private static Predicate<FluidStack> honeyFluidPredicate() {
  196.         return fluidStack -> fluidStack.getFluid().isIn(BeeInfoUtils.getFluidTag("forge:honey"));
  197.     }
  198.  
  199.     @Nonnull
  200.     @Override
  201.     public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
  202.         if (cap.equals(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)) return fluidOptional.cast();
  203.         return super.getCapability(cap, side);
  204.     }
  205.  
  206.     @Override
  207.     public void tick() {
  208.  
  209.     }
  210.  
  211.     public void fillBottle(PlayerEntity player, Hand hand, World world, BlockPos pos, @Nullable Direction side) {
  212.         if (!fluidTank.getFluid().isFluidEqual(HONEY_BOTTLE_FLUID_STACK) || fluidTank.isEmpty()) {
  213.             return;
  214.         }
  215.         if (fluidTank.getFluidAmount() >= ModConstants.HONEY_PER_BOTTLE) {
  216.             fluidTank.drain(HONEY_BOTTLE_FLUID_STACK, IFluidHandler.FluidAction.EXECUTE);
  217.             ItemStack stack = player.getHeldItem(hand);
  218.             if (stack.getCount() > 1) {
  219.                 stack.setCount(stack.getCount() - 1);
  220.                 player.addItemStackToInventory(new ItemStack(Items.HONEY_BOTTLE, 1));
  221.             } else {
  222.                 player.setHeldItem(hand, new ItemStack(Items.HONEY_BOTTLE, 1));
  223.             }
  224.         }
  225.     }
  226.  
  227.     public void emptyBottle(PlayerEntity player, Hand hand, World world, BlockPos pos, @Nullable Direction side) {
  228.         if (!fluidTank.getFluid().isFluidEqual(HONEY_BOTTLE_FLUID_STACK) && !fluidTank.isEmpty()) {
  229.             return;
  230.         }
  231.         if (fluidTank.getFluidAmount() + ModConstants.HONEY_PER_BOTTLE <= fluidTank.getTankCapacity(0)) {
  232.             fluidTank.fill(HONEY_BOTTLE_FLUID_STACK, IFluidHandler.FluidAction.EXECUTE);
  233.             ItemStack stack = player.getHeldItem(hand);
  234.             if (stack.getCount() > 1) {
  235.                 stack.setCount(stack.getCount() - 1);
  236.                 player.addItemStackToInventory(new ItemStack(Items.GLASS_BOTTLE, 1));
  237.             } else {
  238.                 player.setHeldItem(hand, new ItemStack(Items.GLASS_BOTTLE, 1));
  239.             }
  240.         }
  241.     }
  242. }
  243.  
  244.  
Advertisement
Add Comment
Please, Sign In to add comment