Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.mcreator.vftweaks.block;
- import net.minecraftforge.registries.ObjectHolder;
- import net.minecraftforge.items.wrapper.SidedInvWrapper;
- import net.minecraftforge.items.IItemHandler;
- import net.minecraftforge.items.CapabilityItemHandler;
- import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
- import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
- import net.minecraftforge.eventbus.api.SubscribeEvent;
- import net.minecraftforge.event.RegistryEvent;
- import net.minecraftforge.energy.EnergyStorage;
- import net.minecraftforge.energy.CapabilityEnergy;
- import net.minecraftforge.common.util.LazyOptional;
- import net.minecraftforge.common.capabilities.Capability;
- import net.minecraftforge.common.ToolType;
- import net.minecraftforge.api.distmarker.OnlyIn;
- import net.minecraftforge.api.distmarker.Dist;
- import net.minecraft.world.server.ServerWorld;
- import net.minecraft.world.World;
- import net.minecraft.world.IWorld;
- import net.minecraft.world.IBlockReader;
- import net.minecraft.util.text.StringTextComponent;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.math.shapes.VoxelShapes;
- import net.minecraft.util.math.shapes.VoxelShape;
- import net.minecraft.util.math.shapes.ISelectionContext;
- import net.minecraft.util.math.BlockRayTraceResult;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.Rotation;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.Mirror;
- import net.minecraft.util.Hand;
- import net.minecraft.util.Direction;
- import net.minecraft.util.ActionResultType;
- import net.minecraft.tileentity.TileEntityType;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.tileentity.LockableLootTileEntity;
- import net.minecraft.state.properties.BlockStateProperties;
- import net.minecraft.state.StateContainer;
- import net.minecraft.state.DirectionProperty;
- import net.minecraft.state.BooleanProperty;
- import net.minecraft.state.IntegerProperty;
- import net.minecraft.network.play.server.SUpdateTileEntityPacket;
- import net.minecraft.network.NetworkManager;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.loot.LootContext;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.ItemGroup;
- import net.minecraft.item.Item;
- import net.minecraft.item.BlockItemUseContext;
- import net.minecraft.item.BlockItem;
- import net.minecraft.inventory.container.INamedContainerProvider;
- import net.minecraft.inventory.container.Container;
- import net.minecraft.inventory.container.ChestContainer;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.inventory.InventoryHelper;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.fluid.Fluids;
- import net.minecraft.fluid.FluidState;
- import net.minecraft.entity.player.PlayerInventory;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.client.renderer.RenderTypeLookup;
- import net.minecraft.client.renderer.RenderType;
- import net.minecraft.block.material.PushReaction;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.SoundType;
- import net.minecraft.block.IWaterLoggable;
- import net.minecraft.block.HorizontalBlock;
- import net.minecraft.block.BlockState;
- import net.minecraft.block.Block;
- import net.mcreator.vftweaks.procedures.SpinningWheelUpdateTickProcedure;
- import net.mcreator.vftweaks.procedures.SpinningWheelRightClickedProcedure;
- import net.mcreator.vftweaks.VftweaksModElements;
- import javax.annotation.Nullable;
- import java.util.stream.IntStream;
- import java.util.Random;
- import java.util.Map;
- import java.util.List;
- import java.util.HashMap;
- import java.util.Collections;
- @VftweaksModElements.ModElement.Tag
- public class SpinningWheelBlock extends VftweaksModElements.ModElement {
- @ObjectHolder("vftweaks:spinning_wheel")
- public static final Block block = null;
- @ObjectHolder("vftweaks:spinning_wheel")
- public static final TileEntityType<CustomTileEntity> tileEntityType = null;
- public SpinningWheelBlock(VftweaksModElements instance) {
- super(instance, 138);
- FMLJavaModLoadingContext.get().getModEventBus().register(new TileEntityRegisterHandler());
- }
- @Override
- public void initElements() {
- elements.blocks.add(() -> new CustomBlock());
- elements.items.add(() -> new BlockItem(block, new Item.Properties().group(ItemGroup.DECORATIONS)).setRegistryName(block.getRegistryName()));
- }
- private static class TileEntityRegisterHandler {
- @SubscribeEvent
- public void registerTileEntity(RegistryEvent.Register<TileEntityType<?>> event) {
- event.getRegistry().register(TileEntityType.Builder.create(CustomTileEntity::new, block).build(null).setRegistryName("spinning_wheel"));
- }
- }
- @Override
- @OnlyIn(Dist.CLIENT)
- public void clientLoad(FMLClientSetupEvent event) {
- RenderTypeLookup.setRenderLayer(block, RenderType.getCutout());
- }
- public static class CustomBlock extends Block implements IWaterLoggable {
- public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
- public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
- public static final IntegerProperty STATE = BlockStateProperties.AGE_0_2;
- public CustomBlock() {
- super(Block.Properties.create(Material.WOOD).sound(SoundType.WOOD).hardnessAndResistance(2.5f, 2.5f).setLightLevel(s -> 0).harvestLevel(1)
- .harvestTool(ToolType.AXE).setRequiresTool().notSolid().setOpaque((bs, br, bp) -> false));
- this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH).with(WATERLOGGED, false).with(STATE, Integer.valueOf(0)));
- setRegistryName("spinning_wheel");
- }
- @Override
- public boolean propagatesSkylightDown(BlockState state, IBlockReader reader, BlockPos pos) {
- return true;
- }
- @Override
- public VoxelShape getShape(BlockState state, IBlockReader world, BlockPos pos, ISelectionContext context) {
- switch ((Direction) state.get(FACING)) {
- case SOUTH :
- default :
- return VoxelShapes.or(makeCuboidShape(16, 0, 12, 1, 15, 4));
- case NORTH :
- return VoxelShapes.or(makeCuboidShape(0, 0, 4, 15, 15, 12));
- case EAST :
- return VoxelShapes.or(makeCuboidShape(12, 0, 0, 4, 15, 15));
- case WEST :
- return VoxelShapes.or(makeCuboidShape(4, 0, 16, 12, 15, 1));
- }
- }
- @Override
- protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
- builder.add(FACING, WATERLOGGED, STATE);
- }
- public BlockState rotate(BlockState state, Rotation rot) {
- return state.with(FACING, rot.rotate(state.get(FACING)));
- }
- public BlockState mirror(BlockState state, Mirror mirrorIn) {
- return state.rotate(mirrorIn.toRotation(state.get(FACING)));
- }
- @Override
- public BlockState getStateForPlacement(BlockItemUseContext context) {
- boolean flag = context.getWorld().getFluidState(context.getPos()).getFluid() == Fluids.WATER;;
- return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite()).with(WATERLOGGED, flag);
- }
- @Override
- public FluidState getFluidState(BlockState state) {
- return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state);
- }
- @Override
- public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld world, BlockPos currentPos,
- BlockPos facingPos) {
- if (state.get(WATERLOGGED)) {
- world.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(world));
- }
- return super.updatePostPlacement(state, facing, facingState, world, currentPos, facingPos);
- }
- @Override
- public int getFlammability(BlockState state, IBlockReader world, BlockPos pos, Direction face) {
- return 20;
- }
- @Override
- public PushReaction getPushReaction(BlockState state) {
- return PushReaction.DESTROY;
- }
- @Override
- public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
- List<ItemStack> dropsOriginal = super.getDrops(state, builder);
- if (!dropsOriginal.isEmpty())
- return dropsOriginal;
- return Collections.singletonList(new ItemStack(this, 1));
- }
- @Override
- public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean moving) {
- super.onBlockAdded(state, world, pos, oldState, moving);
- int x = pos.getX();
- int y = pos.getY();
- int z = pos.getZ();
- world.getPendingBlockTicks().scheduleTick(new BlockPos(x, y, z), this, 10);
- }
- @Override
- public void tick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
- int x = pos.getX();
- int y = pos.getY();
- int z = pos.getZ();
- if (((new Object() {
- public double getValue(BlockPos pos, String tag) {
- TileEntity tileEntity = world.getTileEntity(pos);
- if (tileEntity != null)
- return tileEntity.getTileData().getDouble(tag);
- return -1;
- }
- }.getValue(new BlockPos((int) x, (int) y, (int) z), "status")) == 0)) {
- world.setBlockState(pos, state.with(STATE, Integer.valueOf(0)), 3);
- } else if (((new Object() {
- public double getValue(BlockPos pos, String tag) {
- TileEntity tileEntity = world.getTileEntity(pos);
- if (tileEntity != null)
- return tileEntity.getTileData().getDouble(tag);
- return -1;
- }
- }.getValue(new BlockPos((int) x, (int) y, (int) z), "status")) == 1)) {
- world.setBlockState(pos, state.with(STATE, Integer.valueOf(1)), 3);
- } else if (((new Object() {
- public double getValue(BlockPos pos, String tag) {
- TileEntity tileEntity = world.getTileEntity(pos);
- if (tileEntity != null)
- return tileEntity.getTileData().getDouble(tag);
- return -1;
- }
- }.getValue(new BlockPos((int) x, (int) y, (int) z), "status")) == 2)) {
- world.setBlockState(pos, state.with(STATE, Integer.valueOf(2)), 3);
- }
- super.tick(state, world, pos, random);
- {
- Map<String, Object> $_dependencies = new HashMap<>();
- SpinningWheelUpdateTickProcedure.executeProcedure($_dependencies);
- }
- world.getPendingBlockTicks().scheduleTick(new BlockPos(x, y, z), this, 10);
- }
- @Override
- public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity entity, Hand hand,
- BlockRayTraceResult hit) {
- super.onBlockActivated(state, world, pos, entity, hand, hit);
- int x = pos.getX();
- int y = pos.getY();
- int z = pos.getZ();
- Direction direction = hit.getFace();
- {
- Map<String, Object> $_dependencies = new HashMap<>();
- $_dependencies.put("entity", entity);
- $_dependencies.put("x", x);
- $_dependencies.put("y", y);
- $_dependencies.put("z", z);
- $_dependencies.put("world", world);
- SpinningWheelRightClickedProcedure.executeProcedure($_dependencies);
- }
- return ActionResultType.SUCCESS;
- }
- @Override
- public INamedContainerProvider getContainer(BlockState state, World worldIn, BlockPos pos) {
- TileEntity tileEntity = worldIn.getTileEntity(pos);
- return tileEntity instanceof INamedContainerProvider ? (INamedContainerProvider) tileEntity : null;
- }
- @Override
- public boolean hasTileEntity(BlockState state) {
- return true;
- }
- @Override
- public TileEntity createTileEntity(BlockState state, IBlockReader world) {
- return new CustomTileEntity();
- }
- @Override
- public boolean eventReceived(BlockState state, World world, BlockPos pos, int eventID, int eventParam) {
- super.eventReceived(state, world, pos, eventID, eventParam);
- TileEntity tileentity = world.getTileEntity(pos);
- return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);
- }
- @Override
- public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) {
- if (state.getBlock() != newState.getBlock()) {
- TileEntity tileentity = world.getTileEntity(pos);
- if (tileentity instanceof CustomTileEntity) {
- InventoryHelper.dropInventoryItems(world, pos, (CustomTileEntity) tileentity);
- world.updateComparatorOutputLevel(pos, this);
- }
- super.onReplaced(state, world, pos, newState, isMoving);
- }
- }
- @Override
- public boolean hasComparatorInputOverride(BlockState state) {
- return true;
- }
- @Override
- public int getComparatorInputOverride(BlockState blockState, World world, BlockPos pos) {
- TileEntity tileentity = world.getTileEntity(pos);
- if (tileentity instanceof CustomTileEntity)
- return Container.calcRedstoneFromInventory((CustomTileEntity) tileentity);
- else
- return 0;
- }
- }
- public static class CustomTileEntity extends LockableLootTileEntity implements ISidedInventory {
- private NonNullList<ItemStack> stacks = NonNullList.<ItemStack>withSize(1, ItemStack.EMPTY);
- protected CustomTileEntity() {
- super(tileEntityType);
- }
- @Override
- public void read(BlockState blockState, CompoundNBT compound) {
- super.read(blockState, compound);
- if (!this.checkLootAndRead(compound)) {
- this.stacks = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
- }
- ItemStackHelper.loadAllItems(compound, this.stacks);
- if (compound.get("energyStorage") != null)
- CapabilityEnergy.ENERGY.readNBT(energyStorage, null, compound.get("energyStorage"));
- }
- @Override
- public CompoundNBT write(CompoundNBT compound) {
- super.write(compound);
- if (!this.checkLootAndWrite(compound)) {
- ItemStackHelper.saveAllItems(compound, this.stacks);
- }
- compound.put("energyStorage", CapabilityEnergy.ENERGY.writeNBT(energyStorage, null));
- return compound;
- }
- @Override
- public SUpdateTileEntityPacket getUpdatePacket() {
- return new SUpdateTileEntityPacket(this.pos, 0, this.getUpdateTag());
- }
- @Override
- public CompoundNBT getUpdateTag() {
- return this.write(new CompoundNBT());
- }
- @Override
- public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
- this.read(this.getBlockState(), pkt.getNbtCompound());
- }
- @Override
- public int getSizeInventory() {
- return stacks.size();
- }
- @Override
- public boolean isEmpty() {
- for (ItemStack itemstack : this.stacks)
- if (!itemstack.isEmpty())
- return false;
- return true;
- }
- @Override
- public ITextComponent getDefaultName() {
- return new StringTextComponent("spinning_wheel");
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public Container createMenu(int id, PlayerInventory player) {
- return ChestContainer.createGeneric9X3(id, player, this);
- }
- @Override
- public ITextComponent getDisplayName() {
- return new StringTextComponent("Spinning Wheel");
- }
- @Override
- protected NonNullList<ItemStack> getItems() {
- return this.stacks;
- }
- @Override
- protected void setItems(NonNullList<ItemStack> stacks) {
- this.stacks = stacks;
- }
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return true;
- }
- @Override
- public int[] getSlotsForFace(Direction side) {
- return IntStream.range(0, this.getSizeInventory()).toArray();
- }
- @Override
- public boolean canInsertItem(int index, ItemStack stack, @Nullable Direction direction) {
- return this.isItemValidForSlot(index, stack);
- }
- @Override
- public boolean canExtractItem(int index, ItemStack stack, Direction direction) {
- return true;
- }
- private final LazyOptional<? extends IItemHandler>[] handlers = SidedInvWrapper.create(this, Direction.values());
- private final EnergyStorage energyStorage = new EnergyStorage(64, 64, 64, 0) {
- @Override
- public int receiveEnergy(int maxReceive, boolean simulate) {
- int retval = super.receiveEnergy(maxReceive, simulate);
- if (!simulate) {
- markDirty();
- world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2);
- }
- return retval;
- }
- @Override
- public int extractEnergy(int maxExtract, boolean simulate) {
- int retval = super.extractEnergy(maxExtract, simulate);
- if (!simulate) {
- markDirty();
- world.notifyBlockUpdate(pos, world.getBlockState(pos), world.getBlockState(pos), 2);
- }
- return retval;
- }
- };
- @Override
- public <T> LazyOptional<T> getCapability(Capability<T> capability, @Nullable Direction facing) {
- if (!this.removed && facing != null && capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)
- return handlers[facing.ordinal()].cast();
- if (!this.removed && capability == CapabilityEnergy.ENERGY)
- return LazyOptional.of(() -> energyStorage).cast();
- return super.getCapability(capability, facing);
- }
- @Override
- public void remove() {
- super.remove();
- for (LazyOptional<? extends IItemHandler> handler : handlers)
- handler.invalidate();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement