Advertisement
Cyndi4U

Spinning Wheel Block Java

May 31st, 2021
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.72 KB | None | 0 0
  1.  
  2. package net.mcreator.vftweaks.block;
  3.  
  4. import net.minecraftforge.registries.ObjectHolder;
  5. import net.minecraftforge.items.wrapper.SidedInvWrapper;
  6. import net.minecraftforge.items.IItemHandler;
  7. import net.minecraftforge.items.CapabilityItemHandler;
  8. import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
  9. import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
  10. import net.minecraftforge.eventbus.api.SubscribeEvent;
  11. import net.minecraftforge.event.RegistryEvent;
  12. import net.minecraftforge.energy.EnergyStorage;
  13. import net.minecraftforge.energy.CapabilityEnergy;
  14. import net.minecraftforge.common.util.LazyOptional;
  15. import net.minecraftforge.common.capabilities.Capability;
  16. import net.minecraftforge.common.ToolType;
  17. import net.minecraftforge.api.distmarker.OnlyIn;
  18. import net.minecraftforge.api.distmarker.Dist;
  19.  
  20. import net.minecraft.world.server.ServerWorld;
  21. import net.minecraft.world.World;
  22. import net.minecraft.world.IWorld;
  23. import net.minecraft.world.IBlockReader;
  24. import net.minecraft.util.text.StringTextComponent;
  25. import net.minecraft.util.text.ITextComponent;
  26. import net.minecraft.util.math.shapes.VoxelShapes;
  27. import net.minecraft.util.math.shapes.VoxelShape;
  28. import net.minecraft.util.math.shapes.ISelectionContext;
  29. import net.minecraft.util.math.BlockRayTraceResult;
  30. import net.minecraft.util.math.BlockPos;
  31. import net.minecraft.util.Rotation;
  32. import net.minecraft.util.NonNullList;
  33. import net.minecraft.util.Mirror;
  34. import net.minecraft.util.Hand;
  35. import net.minecraft.util.Direction;
  36. import net.minecraft.util.ActionResultType;
  37. import net.minecraft.tileentity.TileEntityType;
  38. import net.minecraft.tileentity.TileEntity;
  39. import net.minecraft.tileentity.LockableLootTileEntity;
  40. import net.minecraft.state.properties.BlockStateProperties;
  41. import net.minecraft.state.StateContainer;
  42. import net.minecraft.state.DirectionProperty;
  43. import net.minecraft.state.BooleanProperty;
  44. import net.minecraft.state.IntegerProperty;
  45. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  46. import net.minecraft.network.NetworkManager;
  47. import net.minecraft.nbt.CompoundNBT;
  48. import net.minecraft.loot.LootContext;
  49. import net.minecraft.item.ItemStack;
  50. import net.minecraft.item.ItemGroup;
  51. import net.minecraft.item.Item;
  52. import net.minecraft.item.BlockItemUseContext;
  53. import net.minecraft.item.BlockItem;
  54. import net.minecraft.inventory.container.INamedContainerProvider;
  55. import net.minecraft.inventory.container.Container;
  56. import net.minecraft.inventory.container.ChestContainer;
  57. import net.minecraft.inventory.ItemStackHelper;
  58. import net.minecraft.inventory.InventoryHelper;
  59. import net.minecraft.inventory.ISidedInventory;
  60. import net.minecraft.fluid.Fluids;
  61. import net.minecraft.fluid.FluidState;
  62. import net.minecraft.entity.player.PlayerInventory;
  63. import net.minecraft.entity.player.PlayerEntity;
  64. import net.minecraft.client.renderer.RenderTypeLookup;
  65. import net.minecraft.client.renderer.RenderType;
  66. import net.minecraft.block.material.PushReaction;
  67. import net.minecraft.block.material.Material;
  68. import net.minecraft.block.SoundType;
  69. import net.minecraft.block.IWaterLoggable;
  70. import net.minecraft.block.HorizontalBlock;
  71. import net.minecraft.block.BlockState;
  72. import net.minecraft.block.Block;
  73.  
  74. import net.mcreator.vftweaks.procedures.SpinningWheelUpdateTickProcedure;
  75. import net.mcreator.vftweaks.procedures.SpinningWheelRightClickedProcedure;
  76. import net.mcreator.vftweaks.VftweaksModElements;
  77.  
  78. import javax.annotation.Nullable;
  79.  
  80. import java.util.stream.IntStream;
  81. import java.util.Random;
  82. import java.util.Map;
  83. import java.util.List;
  84. import java.util.HashMap;
  85. import java.util.Collections;
  86.  
  87. @VftweaksModElements.ModElement.Tag
  88. public class SpinningWheelBlock extends VftweaksModElements.ModElement {
  89.     @ObjectHolder("vftweaks:spinning_wheel")
  90.     public static final Block block = null;
  91.     @ObjectHolder("vftweaks:spinning_wheel")
  92.     public static final TileEntityType<CustomTileEntity> tileEntityType = null;
  93.     public SpinningWheelBlock(VftweaksModElements instance) {
  94.         super(instance, 138);
  95.         FMLJavaModLoadingContext.get().getModEventBus().register(new TileEntityRegisterHandler());
  96.     }
  97.  
  98.     @Override
  99.     public void initElements() {
  100.         elements.blocks.add(() -> new CustomBlock());
  101.         elements.items.add(() -> new BlockItem(block, new Item.Properties().group(ItemGroup.DECORATIONS)).setRegistryName(block.getRegistryName()));
  102.     }
  103.     private static class TileEntityRegisterHandler {
  104.         @SubscribeEvent
  105.         public void registerTileEntity(RegistryEvent.Register<TileEntityType<?>> event) {
  106.             event.getRegistry().register(TileEntityType.Builder.create(CustomTileEntity::new, block).build(null).setRegistryName("spinning_wheel"));
  107.         }
  108.     }
  109.     @Override
  110.     @OnlyIn(Dist.CLIENT)
  111.     public void clientLoad(FMLClientSetupEvent event) {
  112.         RenderTypeLookup.setRenderLayer(block, RenderType.getCutout());
  113.     }
  114.     public static class CustomBlock extends Block implements IWaterLoggable {
  115.         public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
  116.         public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
  117.         public static final IntegerProperty STATE = BlockStateProperties.AGE_0_2;
  118.         public CustomBlock() {
  119.             super(Block.Properties.create(Material.WOOD).sound(SoundType.WOOD).hardnessAndResistance(2.5f, 2.5f).setLightLevel(s -> 0).harvestLevel(1)
  120.                     .harvestTool(ToolType.AXE).setRequiresTool().notSolid().setOpaque((bs, br, bp) -> false));
  121.             this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH).with(WATERLOGGED, false).with(STATE, Integer.valueOf(0)));
  122.             setRegistryName("spinning_wheel");
  123.         }
  124.  
  125.         @Override
  126.         public boolean propagatesSkylightDown(BlockState state, IBlockReader reader, BlockPos pos) {
  127.             return true;
  128.         }
  129.  
  130.         @Override
  131.         public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, ISelectionContext context) {
  132.             switch ((Direction) state.get(FACING)) {
  133.                 case SOUTH :
  134.                 default :
  135.                     return VoxelShapes.or(makeCuboidShape(16, 0, 12, 1, 15, 4));
  136.                 case NORTH :
  137.                     return VoxelShapes.or(makeCuboidShape(0, 0, 4, 15, 15, 12));
  138.                 case EAST :
  139.                     return VoxelShapes.or(makeCuboidShape(12, 0, 0, 4, 15, 15));
  140.                 case WEST :
  141.                     return VoxelShapes.or(makeCuboidShape(4, 0, 16, 12, 15, 1));
  142.             }
  143.         }
  144.  
  145.         @Override
  146.         protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  147.             builder.add(FACING, WATERLOGGED, STATE);
  148.         }
  149.  
  150.         public BlockState rotate(BlockState state, Rotation rot) {
  151.             return state.with(FACING, rot.rotate(state.get(FACING)));
  152.         }
  153.  
  154.         public BlockState mirror(BlockState state, Mirror mirrorIn) {
  155.             return state.rotate(mirrorIn.toRotation(state.get(FACING)));
  156.         }
  157.  
  158.         @Override
  159.         public BlockState getStateForPlacement(BlockItemUseContext context) {
  160.             boolean flag = context.getWorld().getFluidState(context.getPos()).getFluid() == Fluids.WATER;;
  161.             return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite()).with(WATERLOGGED, flag);
  162.         }
  163.  
  164.         @Override
  165.         public FluidState getFluidState(BlockState state) {
  166.             return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state);
  167.         }
  168.  
  169.         @Override
  170.         public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld world, BlockPos currentPos,
  171.                 BlockPos facingPos) {
  172.             if (state.get(WATERLOGGED)) {
  173.                 world.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(world));
  174.             }
  175.             return super.updatePostPlacement(state, facing, facingState, world, currentPos, facingPos);
  176.         }
  177.  
  178.         @Override
  179.         public int getFlammability(BlockState state, IBlockReader world, BlockPos pos, Direction face) {
  180.             return 20;
  181.         }
  182.  
  183.         @Override
  184.         public PushReaction getPushReaction(BlockState state) {
  185.             return PushReaction.DESTROY;
  186.         }
  187.  
  188.         @Override
  189.         public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
  190.             List<ItemStack> dropsOriginal = super.getDrops(state, builder);
  191.             if (!dropsOriginal.isEmpty())
  192.                 return dropsOriginal;
  193.             return Collections.singletonList(new ItemStack(this, 1));
  194.         }
  195.  
  196.         @Override
  197.         public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean moving) {
  198.             super.onBlockAdded(state, world, pos, oldState, moving);
  199.             int x = pos.getX();
  200.             int y = pos.getY();
  201.             int z = pos.getZ();
  202.             world.getPendingBlockTicks().scheduleTick(new BlockPos(x, y, z), this, 10);
  203.         }
  204.  
  205.         @Override
  206.         public void tick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
  207.             int x = pos.getX();
  208.             int y = pos.getY();
  209.             int z = pos.getZ();
  210.             if (((new Object() {
  211.                 public double getValue(BlockPos pos, String tag) {
  212.                     TileEntity tileEntity = world.getTileEntity(pos);
  213.                     if (tileEntity != null)
  214.                         return tileEntity.getTileData().getDouble(tag);
  215.                     return -1;
  216.                 }
  217.             }.getValue(new BlockPos((int) x, (int) y, (int) z), "status")) == 0)) {
  218.                 world.setBlockState(pos, state.with(STATE, Integer.valueOf(0)), 3);
  219.             } else if (((new Object() {
  220.                 public double getValue(BlockPos pos, String tag) {
  221.                     TileEntity tileEntity = world.getTileEntity(pos);
  222.                     if (tileEntity != null)
  223.                         return tileEntity.getTileData().getDouble(tag);
  224.                     return -1;
  225.                 }
  226.             }.getValue(new BlockPos((int) x, (int) y, (int) z), "status")) == 1)) {
  227.                 world.setBlockState(pos, state.with(STATE, Integer.valueOf(1)), 3);
  228.             } else if (((new Object() {
  229.                 public double getValue(BlockPos pos, String tag) {
  230.                     TileEntity tileEntity = world.getTileEntity(pos);
  231.                     if (tileEntity != null)
  232.                         return tileEntity.getTileData().getDouble(tag);
  233.                     return -1;
  234.                 }
  235.             }.getValue(new BlockPos((int) x, (int) y, (int) z), "status")) == 2)) {
  236.                 world.setBlockState(pos, state.with(STATE, Integer.valueOf(2)), 3);
  237.             }
  238.             super.tick(state, world, pos, random);
  239.             {
  240.                 Map<String, Object> $_dependencies = new HashMap<>();
  241.                 SpinningWheelUpdateTickProcedure.executeProcedure($_dependencies);
  242.             }
  243.             world.getPendingBlockTicks().scheduleTick(new BlockPos(x, y, z), this, 10);
  244.         }
  245.  
  246.         @Override
  247.         public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity entity, Hand hand,
  248.                 BlockRayTraceResult hit) {
  249.             super.onBlockActivated(state, world, pos, entity, hand, hit);
  250.             int x = pos.getX();
  251.             int y = pos.getY();
  252.             int z = pos.getZ();
  253.             Direction direction = hit.getFace();
  254.             {
  255.                 Map<String, Object> $_dependencies = new HashMap<>();
  256.                 $_dependencies.put("entity", entity);
  257.                 $_dependencies.put("x", x);
  258.                 $_dependencies.put("y", y);
  259.                 $_dependencies.put("z", z);
  260.                 $_dependencies.put("world", world);
  261.                 SpinningWheelRightClickedProcedure.executeProcedure($_dependencies);
  262.             }
  263.             return ActionResultType.SUCCESS;
  264.         }
  265.  
  266.         @Override
  267.         public INamedContainerProvider getContainer(BlockState state, World worldIn, BlockPos pos) {
  268.             TileEntity tileEntity = worldIn.getTileEntity(pos);
  269.             return tileEntity instanceof INamedContainerProvider ? (INamedContainerProvider) tileEntity : null;
  270.         }
  271.  
  272.         @Override
  273.         public boolean hasTileEntity(BlockState state) {
  274.             return true;
  275.         }
  276.  
  277.         @Override
  278.         public TileEntity createTileEntity(BlockState state, IBlockReader world) {
  279.             return new CustomTileEntity();
  280.         }
  281.  
  282.         @Override
  283.         public boolean eventReceived(BlockState state, World world, BlockPos pos, int eventID, int eventParam) {
  284.             super.eventReceived(state, world, pos, eventID, eventParam);
  285.             TileEntity tileentity = world.getTileEntity(pos);
  286.             return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);
  287.         }
  288.  
  289.         @Override
  290.         public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) {
  291.             if (state.getBlock() != newState.getBlock()) {
  292.                 TileEntity tileentity = world.getTileEntity(pos);
  293.                 if (tileentity instanceof CustomTileEntity) {
  294.                     InventoryHelper.dropInventoryItems(world, pos, (CustomTileEntity) tileentity);
  295.                     world.updateComparatorOutputLevel(pos, this);
  296.                 }
  297.                 super.onReplaced(state, world, pos, newState, isMoving);
  298.             }
  299.         }
  300.  
  301.         @Override
  302.         public boolean hasComparatorInputOverride(BlockState state) {
  303.             return true;
  304.         }
  305.  
  306.         @Override
  307.         public int getComparatorInputOverride(BlockState blockState, World world, BlockPos pos) {
  308.             TileEntity tileentity = world.getTileEntity(pos);
  309.             if (tileentity instanceof CustomTileEntity)
  310.                 return Container.calcRedstoneFromInventory((CustomTileEntity) tileentity);
  311.             else
  312.                 return 0;
  313.         }
  314.     }
  315.  
  316.     public static class CustomTileEntity extends LockableLootTileEntity implements ISidedInventory {
  317.         private NonNullList<ItemStack> stacks = NonNullList.<ItemStack>withSize(1, ItemStack.EMPTY);
  318.         protected CustomTileEntity() {
  319.             super(tileEntityType);
  320.         }
  321.  
  322.         @Override
  323.         public void read(BlockState blockState, CompoundNBT compound) {
  324.             super.read(blockState, compound);
  325.             if (!this.checkLootAndRead(compound)) {
  326.                 this.stacks = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
  327.             }
  328.             ItemStackHelper.loadAllItems(compound, this.stacks);
  329.             if (compound.get("energyStorage") != null)
  330.                 CapabilityEnergy.ENERGY.readNBT(energyStorage, null, compound.get("energyStorage"));
  331.         }
  332.  
  333.         @Override
  334.         public CompoundNBT write(CompoundNBT compound) {
  335.             super.write(compound);
  336.             if (!this.checkLootAndWrite(compound)) {
  337.                 ItemStackHelper.saveAllItems(compound, this.stacks);
  338.             }
  339.             compound.put("energyStorage", CapabilityEnergy.ENERGY.writeNBT(energyStorage, null));
  340.             return compound;
  341.         }
  342.  
  343.         @Override
  344.         public SUpdateTileEntityPacket getUpdatePacket() {
  345.             return new SUpdateTileEntityPacket(this.pos, 0, this.getUpdateTag());
  346.         }
  347.  
  348.         @Override
  349.         public CompoundNBT getUpdateTag() {
  350.             return this.write(new CompoundNBT());
  351.         }
  352.  
  353.         @Override
  354.         public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
  355.             this.read(this.getBlockState(), pkt.getNbtCompound());
  356.         }
  357.  
  358.         @Override
  359.         public int getSizeInventory() {
  360.             return stacks.size();
  361.         }
  362.  
  363.         @Override
  364.         public boolean isEmpty() {
  365.             for (ItemStack itemstack : this.stacks)
  366.                 if (!itemstack.isEmpty())
  367.                     return false;
  368.             return true;
  369.         }
  370.  
  371.         @Override
  372.         public ITextComponent getDefaultName() {
  373.             return new StringTextComponent("spinning_wheel");
  374.         }
  375.  
  376.         @Override
  377.         public int getInventoryStackLimit() {
  378.             return 64;
  379.         }
  380.  
  381.         @Override
  382.         public Container createMenu(int id, PlayerInventory player) {
  383.             return ChestContainer.createGeneric9X3(id, player, this);
  384.         }
  385.  
  386.         @Override
  387.         public ITextComponent getDisplayName() {
  388.             return new StringTextComponent("Spinning Wheel");
  389.         }
  390.  
  391.         @Override
  392.         protected NonNullList<ItemStack> getItems() {
  393.             return this.stacks;
  394.         }
  395.  
  396.         @Override
  397.         protected void setItems(NonNullList<ItemStack> stacks) {
  398.             this.stacks = stacks;
  399.         }
  400.  
  401.         @Override
  402.         public boolean isItemValidForSlot(int index, ItemStack stack) {
  403.             return true;
  404.         }
  405.  
  406.         @Override
  407.         public int[] getSlotsForFace(Direction side) {
  408.             return IntStream.range(0, this.getSizeInventory()).toArray();
  409.         }
  410.  
  411.         @Override
  412.         public boolean canInsertItem(int index, ItemStack stack, @Nullable Direction direction) {
  413.             return this.isItemValidForSlot(index, stack);
  414.         }
  415.  
  416.         @Override
  417.         public boolean canExtractItem(int index, ItemStack stack, Direction direction) {
  418.             return true;
  419.         }
  420.         private final LazyOptional<? extends IItemHandler>[] handlers = SidedInvWrapper.create(this, Direction.values());
  421.         private final EnergyStorage energyStorage = new EnergyStorage(64, 64, 64, 0) {
  422.             @Override
  423.             public int receiveEnergy(int maxReceive, boolean simulate) {
  424.                 int retval = super.receiveEnergy(maxReceive, simulate);
  425.                 if (!simulate) {
  426.                     markDirty();
  427.                     world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2);
  428.                 }
  429.                 return retval;
  430.             }
  431.  
  432.             @Override
  433.             public int extractEnergy(int maxExtract, boolean simulate) {
  434.                 int retval = super.extractEnergy(maxExtract, simulate);
  435.                 if (!simulate) {
  436.                     markDirty();
  437.                     world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2);
  438.                 }
  439.                 return retval;
  440.             }
  441.         };
  442.         @Override
  443.         public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
  444.             if (!this.removed && facing != null && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
  445.                 return handlers[facing.ordinal()].cast();
  446.             if (!this.removed && capability == CapabilityEnergy.ENERGY)
  447.                 return LazyOptional.of(() -> energyStorage).cast();
  448.             return super.getCapability(capability, facing);
  449.         }
  450.  
  451.         @Override
  452.         public void remove() {
  453.             super.remove();
  454.             for (LazyOptional<? extends IItemHandler> handler : handlers)
  455.                 handler.invalidate();
  456.         }
  457.     }
  458. }
  459.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement