diff --git a/build.gradle b/build.gradle index 901179e..7145fe2 100644 --- a/build.gradle +++ b/build.gradle @@ -40,6 +40,7 @@ dependencies { modImplementation "com.github.Mysticpasta1:mcef-forge:a9bf168a92" modImplementation "curse.maven:cloth_config_forge-348521:3972423" modImplementation "curse.maven:SU-370704:4410614" + modImplementation "curse.maven:spark-361579:4381167" // Uncomment the following line to enable the deprecated Fabric API modules. // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. diff --git a/src/main/java/net/montoyo/wd/block/BlockKeyboardLeft.java b/src/main/java/net/montoyo/wd/block/BlockKeyboardLeft.java index 682ce23..0331ad9 100644 --- a/src/main/java/net/montoyo/wd/block/BlockKeyboardLeft.java +++ b/src/main/java/net/montoyo/wd/block/BlockKeyboardLeft.java @@ -13,9 +13,11 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Explosion; import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelReader; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.RenderShape; @@ -25,7 +27,6 @@ import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.DirectionProperty; import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.block.state.properties.Property; -import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.PushReaction; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; @@ -41,239 +42,134 @@ import net.montoyo.wd.init.BlockInit; import net.montoyo.wd.item.ItemLinker; import net.montoyo.wd.net.Messages; import net.montoyo.wd.net.client.CMessageCloseGui; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class BlockKeyboardLeft extends BlockPeripheral { - public static final EnumProperty type = EnumProperty.create("type", DefaultPeripheral.class); - public static final DirectionProperty facing = DirectionProperty.create("facing", Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST); - private static final Property[] properties = new Property[] { type, facing }; + public static final EnumProperty TYPE = EnumProperty.create("type", DefaultPeripheral.class); + public static final DirectionProperty FACING = DirectionProperty.create("facing", Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST); +// public static final DirectionProperty HALF = DirectionProperty.create("facing", Direction.EAST, Direction.WEST); + + private static final Property[] properties = new Property[] {TYPE, FACING}; public BlockKeyboardLeft() { } - + + // TODO: make non static (for extensibility purposes) + public static TileEntityKeyboard getTileEntity(BlockState state, Level world, BlockPos pos) { + if (state.getBlock() instanceof BlockKeyboardLeft) { + BlockEntity te = world.getBlockEntity(pos); // TODO: check? + if (te instanceof TileEntityKeyboard) + return (TileEntityKeyboard) te; + } + + BlockPos relative = pos.relative(BlockKeyboardLeft.mapDirection(state.getValue(FACING).getOpposite())); + BlockState ns = world.getBlockState(relative); + + if(ns.getBlock() instanceof BlockPeripheral && ns.getValue(BlockPeripheral.type) == DefaultPeripheral.KEYBOARD) { + BlockEntity te = world.getBlockEntity(relative); // TODO: check? + if (te instanceof TileEntityKeyboard) + return (TileEntityKeyboard) te; + } + + return null; + } + + public static Direction mapDirection(Direction facing) { + return switch (facing) { + case NORTH -> Direction.EAST; + case EAST -> Direction.SOUTH; + case SOUTH -> Direction.WEST; + case WEST -> Direction.NORTH; + default -> facing; + }; + } + @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { builder.add(properties); } - - // @Nullable TODO: Fix -// @Override -// public BlockState getStateForPlacement(BlockPlaceContext context) { -// Direction rot = Direction.fromYRot(placer.getYHeadRot()); -// return defaultBlockState().setValue(type, DefaultPeripheral.fromMetadata(meta)).setValue(facing, rot); -// -// -// return getStateForPlacement(context); -// } -// -// public BlockState getStateForPlacement(@Nonnull Level world, @Nonnull BlockPos pos, @Nonnull Direction nocare, float hitX, -// float hitY, float hitZ, int meta, @Nonnull LivingEntity placer, InteractionHand hand) { -// } - -// @Override -// public void getSubBlocks(CreativeTabs tab, NonNullList list) { -// for(DefaultPeripheral dp : DefaultPeripheral.values()) -// list.add(new ItemStack(getItem(), 1, dp.toMetadata(0))); -// } - -// @Override -// @Nonnull -// public IBlockState getStateFromMeta(int meta) { -// DefaultPeripheral dp = DefaultPeripheral.fromMetadata(meta); -// IBlockState state = getDefaultState().withProperty(type, dp); -// -// if(dp.hasFacing()) -// state = state.withProperty(facing, (meta >> 2) & 3); -// -// return state; -// } -// -// @Override -// public int getMetaFromState(IBlockState state) { -// return state.getValue(type).toMetadata(state.getValue(facing)); -// } - - - /*@Nullable - @Override - public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { - BlockEntityType.BlockEntitySupplier cls = state.getValue(type).getTEClass(); - if(cls == null) - return null; - - try { - return cls.create(pos, state); - } catch(Throwable t) { - Log.errorEx("Couldn't instantiate peripheral TileEntity:", t); - } - - return null; - } */ - + @Override public RenderShape getRenderShape(BlockState state) { return RenderShape.MODEL; } - -// @Override -// public int damageDropped(IBlockState state) { -// return state.getValue(type).toMetadata(0); -// } - - + @Override - public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { - if(player.isShiftKeyDown()) - return InteractionResult.FAIL; - - if(player.getItemInHand(hand).getItem() instanceof ItemLinker) - return InteractionResult.FAIL; - - BlockEntity te = world.getBlockEntity(pos); - - if(te instanceof TileEntityKeyboard) - return ((TileEntityKeyboard) te).onRightClick(player, hand); - else if(te instanceof TileEntityServer) { - ((TileEntityServer) te).onPlayerRightClick(player); - return InteractionResult.PASS; - } else - return InteractionResult.FAIL; - } - -// @Override -// public boolean isFullCube(IBlockState state) { TODO: FIx. -// return state.getValue(type) != DefaultPeripheral.KEYBOARD; -// } -// -// @Override -// public boolean isFullBlock(IBlockState state) { -// return state.getValue(type) != DefaultPeripheral.KEYBOARD; -// } -// -// @Override -// public boolean isNormalCube(IBlockState state, IBlockAccess world, BlockPos pos) { -// return state.getValue(type) != DefaultPeripheral.KEYBOARD; -// } -// -// @Override -// public boolean isOpaqueCube(IBlockState state) { -// return state.getValue(type) != DefaultPeripheral.KEYBOARD; -// } -// -// @Override -// public boolean doesSideBlockRendering(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing face) { -// return state.getValue(type) != DefaultPeripheral.KEYBOARD; -// } - - @Override - public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { - return state.getValue(type) == DefaultPeripheral.KEYBOARD ? BlockKeyboardRight.KEYBOARD_AABB : Shapes.block(); - } - - @Override - public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { - if(world.isClientSide) - return; - - if(state.getValue(type) == DefaultPeripheral.KEYBOARD) { - //Keyboard special treatment - Direction f = state.getValue(facing); - Vec3i dir = f.getClockWise().getNormal(); - BlockPos left = pos.offset(dir); - BlockPos right = pos.subtract(dir); - - if(!world.isEmptyBlock(pos.below()) && BlockKeyboardRight.checkNeighborhood(world, pos, null)) { - if(world.isEmptyBlock(right) && !world.isEmptyBlock(right.below()) && BlockKeyboardRight.checkNeighborhood(world, right, pos)) { - world.setBlock(right, BlockInit.blockKbRight.get().defaultBlockState().setValue(BlockKeyboardRight.facing, f), 3); - world.setBlock(pos.offset(f.getNormal()), BlockInit.blockKeyBoard.get().defaultBlockState().setValue(BlockKeyboardRight.facing, f), 3); - return; - } else if(world.isEmptyBlock(left) && !world.isEmptyBlock(left.below()) && BlockKeyboardRight.checkNeighborhood(world, left, pos)) { - world.setBlock(left, state, 3); - world.setBlock(pos.offset(f.getNormal()), BlockInit.blockKeyBoard.get().defaultBlockState().setValue(BlockKeyboardRight.facing, f), 3); - return; - } - } - - //Not good; remove this shit... - world.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState()); - if(!(placer instanceof Player) || !((Player) placer).isCreative()) { -// dropBlockAsItem(world, pos, state, 0); TODO: Loottable? - } - } else if(placer instanceof Player) { - BlockEntity te = world.getBlockEntity(pos); - - if(te instanceof TileEntityServer) - ((TileEntityServer) te).setOwner((Player) placer); - else if(te instanceof TileEntityInterfaceBase) - ((TileEntityInterfaceBase) te).setOwner((Player) placer); + public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) { + double rpos = (entity.getY() - ((double) pos.getY())) * 16.0; + if (!world.isClientSide && rpos >= 1.0 && rpos <= 2.0 && Math.random() < 0.25) { + TileEntityKeyboard tek = BlockKeyboardLeft.getTileEntity(state, world, pos); + + if (tek != null) + tek.simulateCat(entity); } } - + + @Override + public @NotNull InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { + if(player.getItemInHand(hand).getItem() instanceof ItemLinker) + return InteractionResult.PASS; + + TileEntityKeyboard tek = BlockKeyboardLeft.getTileEntity(state, level, pos); + if(tek != null) + return tek.onRightClick(player, hand); + + return InteractionResult.PASS; + } + + @Override + public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { + return state.getValue(TYPE) == DefaultPeripheral.KEYBOARD ? BlockKeyboardRight.KEYBOARD_AABB : Shapes.block(); + } + @Override public PushReaction getPistonPushReaction(BlockState state) { return PushReaction.IGNORE; } - - private void removeRightPiece(Level world, BlockPos pos) { - for(Direction nf: Direction.Plane.HORIZONTAL) { - BlockPos np = pos.offset(nf.getNormal()); - - if(world.getBlockState(np).getBlock() instanceof BlockKeyboardRight) { - world.setBlock(np, Blocks.AIR.defaultBlockState(), 3); - break; - } + + private static void removeRightPiece(BlockState state, Level world, BlockPos pos) { + BlockPos relative = pos.relative(BlockKeyboardLeft.mapDirection(state.getValue(FACING))); + + BlockState ns = world.getBlockState(relative); + if(ns.getBlock() instanceof BlockKeyboardRight) { + world.setBlock(relative, Blocks.AIR.defaultBlockState(), 3); } } - - @Override - public void neighborChanged(BlockState state, Level world, BlockPos pos, Block neighborType, BlockPos neighbor, boolean isMoving) { - BlockEntity te = world.getBlockEntity(pos); - if(te != null && te instanceof TileEntityPeripheralBase) - ((TileEntityPeripheralBase) te).onNeighborChange(neighborType, neighbor); - - if(world.isClientSide || state.getValue(type) != DefaultPeripheral.KEYBOARD) - return; - - if(neighbor.getX() == pos.getX() && neighbor.getY() == pos.getY() - 1 && neighbor.getZ() == pos.getZ() && world.isEmptyBlock(neighbor)) { - removeRightPiece(world, pos); + + public static void remove(BlockState state, Level world, BlockPos pos, boolean setState, boolean drop) { + removeRightPiece(state, world, pos); + if (setState) { + if (drop) { + // TODO: force drop item + } world.setBlock(pos, Blocks.AIR.defaultBlockState(), 3); -// dropBlockAsItem(world, pos, state, 0); //TODO Loottable - Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(world, pos)), new CMessageCloseGui(pos)); } + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(world, pos)), new CMessageCloseGui(pos)); } - + +// @Override +// public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) { +// if(!world.isClientSide) { +// remove(state, world, pos, false, false); +// } +// } + @Override - public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) { - if(!world.isClientSide) { - if(state.getBlock() == this && state.getValue(type) == DefaultPeripheral.KEYBOARD) - removeRightPiece(world, pos); - - Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(world, pos)), new CMessageCloseGui(pos)); + public void onRemove(BlockState arg, Level arg2, BlockPos arg3, BlockState arg4, boolean bl) { + if(!arg2.isClientSide) { + remove(arg, arg2, arg3, false, false); } + super.onRemove(arg, arg2, arg3, arg4, bl); } - - @Override - public void onBlockExploded(BlockState state, Level level, BlockPos pos, Explosion explosion) { - playerDestroy(level, null, pos, level.getBlockState(pos), null, null); - } - - @Override - public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) { - if(!world.isClientSide && world.getBlockState(pos).getValue(type) == DefaultPeripheral.KEYBOARD) { - double rpos = (entity.getY() - ((double) pos.getY())) * 16.0; - - if(rpos >= 1.0 && rpos <= 2.0 && Math.random() < 0.25) { - BlockEntity te = world.getBlockEntity(pos); - - if(te != null && te instanceof TileEntityKeyboard) - ((TileEntityKeyboard) te).simulateCat(entity); - } - } - } - + public static PacketDistributor.TargetPoint point(Level world, BlockPos bp) { return new PacketDistributor.TargetPoint(bp.getX(), bp.getY(), bp.getZ(), 64.0, world.dimension()); } - + + @Override + public VoxelShape getOcclusionShape(BlockState arg, BlockGetter arg2, BlockPos arg3) { + return Shapes.empty(); + } } diff --git a/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java b/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java index 714ccc9..b95dcbe 100644 --- a/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java +++ b/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java @@ -4,11 +4,9 @@ package net.montoyo.wd.block; -import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.Vec3i; -import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; @@ -17,7 +15,6 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.BaseEntityBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; @@ -25,44 +22,66 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.DirectionProperty; -import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Material; -import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.BlockHitResult; -import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; +import net.minecraftforge.network.PacketDistributor; import net.montoyo.wd.core.DefaultPeripheral; import net.montoyo.wd.core.IPeripheral; import net.montoyo.wd.entity.TileEntityKeyboard; -import net.montoyo.wd.entity.TileEntityScreen; import net.montoyo.wd.init.BlockInit; -import net.montoyo.wd.init.TileInit; import net.montoyo.wd.item.ItemLinker; +import net.montoyo.wd.net.Messages; +import net.montoyo.wd.net.client.CMessageCloseGui; import net.montoyo.wd.utilities.BlockSide; import net.montoyo.wd.utilities.Vector3i; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.lwjgl.system.CallbackI; - -import java.util.List; -import java.util.Objects; +// TODO: merge into KeyboardLeft public class BlockKeyboardRight extends Block implements IPeripheral { - public static final DirectionProperty facing = BlockStateProperties.HORIZONTAL_FACING; + public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING; public static final VoxelShape KEYBOARD_AABB = Shapes.box(0.0, 0.0, 0.0, 1.0, 1.0 / 16.0, 1.0); public BlockKeyboardRight() { super(Properties.of(Material.STONE) .strength(1.5f, 10.f)); } - + + private static void removeLeftPiece(BlockState state, Level world, BlockPos pos) { + BlockPos relative = pos.relative(BlockKeyboardLeft.mapDirection(state.getValue(FACING).getOpposite())); + + BlockState ns = world.getBlockState(relative); + if(ns.getBlock() instanceof BlockKeyboardLeft) { + world.setBlock(relative, Blocks.AIR.defaultBlockState(), 3); + } + } + + public static void remove(BlockState state, Level world, BlockPos pos, boolean setState, boolean drop) { + removeLeftPiece(state, world, pos); + if (setState) { + if (drop) { + // TODO: force drop item + } + world.setBlock(pos, Blocks.AIR.defaultBlockState(), 3); + } + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> BlockKeyboardLeft.point(world, pos)), new CMessageCloseGui(pos)); + } + + @Override + public void onRemove(BlockState arg, Level arg2, BlockPos arg3, BlockState arg4, boolean bl) { + if(!arg2.isClientSide) { + remove(arg, arg2, arg3, false, false); + } + super.onRemove(arg, arg2, arg3, arg4, bl); + } + @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { - builder.add(facing); + builder.add(FACING); } @Override @@ -75,134 +94,37 @@ public class BlockKeyboardRight extends Block implements IPeripheral { return KEYBOARD_AABB; } - private TileEntityKeyboard getTileEntity(Level world, BlockPos pos) { - for (Direction nf : Direction.Plane.HORIZONTAL) { - BlockPos np = pos.offset(nf.getNormal()); - BlockState ns = world.getBlockState(np); - - if(ns.getBlock() instanceof BlockPeripheral && ns.getValue(BlockPeripheral.type) == DefaultPeripheral.KEYBOARD) { - BlockEntity te = world.getBlockEntity(pos); - if (te instanceof TileEntityKeyboard) - return (TileEntityKeyboard) te; - - break; - } - } - - return null; - } - @Override public boolean connect(Level world, BlockPos pos, BlockState state, Vector3i scrPos, BlockSide scrSide) { - TileEntityKeyboard keyboard = getTileEntity(world, pos); + TileEntityKeyboard keyboard = BlockKeyboardLeft.getTileEntity(state, world, pos); return keyboard != null && keyboard.connect(world, pos, state, scrPos, scrSide); } - - public static boolean checkNeighborhood(Level world, BlockPos bp, BlockPos ignore) { - for (Direction neighbor : Direction.Plane.HORIZONTAL) { - BlockPos np = bp.offset(neighbor.getNormal()); - - if (ignore == null || !np.equals(ignore)) { - BlockState state = world.getBlockState(np); - - if (state.getBlock() instanceof BlockPeripheral) { - if (state.getValue(BlockPeripheral.type) == DefaultPeripheral.KEYBOARD) - return false; - } else if (state.getBlock() instanceof BlockKeyboardRight) - return false; - } - } - - return true; - } - - public void removeLeftPiece(Level world, BlockPos pos, boolean dropItem) { - for (Direction nf : Direction.Plane.HORIZONTAL) { - BlockPos np = pos.offset(nf.getNormal()); - BlockState ns = world.getBlockState(np); - - if (ns.getBlock() instanceof BlockPeripheral && ns.getValue(BlockPeripheral.type) == DefaultPeripheral.KEYBOARD) { - /* if(dropItem) - if(world instanceof ServerLevel serverWorld) { - // ns.getBlock().getDrops(ns, serverWorld, np,0); - } */ - world.setBlock(np, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL_IMMEDIATE); - break; - } - } - } - - @Override - public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block, BlockPos neighbor, boolean isMoving) { - if (world.isClientSide()) - return; - - if (neighbor.getX() == pos.getX() && neighbor.getY() == pos.getY() - 1 && neighbor.getZ() == pos.getZ()) { - removeLeftPiece(world, pos, true); - world.setBlock(pos, Blocks.AIR.defaultBlockState(), Block.UPDATE_ALL_IMMEDIATE); - } - } - - @Override - public boolean onDestroyedByPlayer(BlockState state, Level world, BlockPos pos, Player ply, boolean willHarvest, FluidState fluid) { - if (!world.isClientSide) - removeLeftPiece(world, pos, !ply.isCreative()); - - return super.onDestroyedByPlayer(state, world, pos, ply, willHarvest, fluid); - } - - + @Override public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) { double rpos = (entity.getY() - ((double) pos.getY())) * 16.0; if (!world.isClientSide && rpos >= 1.0 && rpos <= 2.0 && Math.random() < 0.25) { - TileEntityKeyboard tek = getTileEntity(world, pos); + TileEntityKeyboard tek = BlockKeyboardLeft.getTileEntity(state, world, pos); if (tek != null) tek.simulateCat(entity); } } - @Override - public boolean canBeReplaced(BlockState state, BlockPlaceContext context) { - if (!context.getItemInHand().isEmpty()) //Keyboard - return false; - - //Special checks for the keyboard - BlockPos pos = context.getClickedPos().offset(context.getHorizontalDirection().getNormal()); - if (context.getLevel().getBlockState(pos.below()) == Blocks.AIR.defaultBlockState() || !BlockKeyboardRight.checkNeighborhood(context.getLevel(), pos, null)) - return true; - - int f = (int) Math.floor(((double) (context.getPlayer().getYRot() * 4.0f / 360.0f)) + 2.5) & 3; - Vec3i dir = Direction.from2DDataValue(f).getNormal(); - BlockPos left = pos.offset(dir); - BlockPos right = pos.subtract(dir); - - if (context.getLevel().getBlockState(right) == Blocks.AIR.defaultBlockState() && !(context.getLevel().getBlockState(right.below()) == Blocks.AIR.defaultBlockState()) && BlockKeyboardRight.checkNeighborhood(context.getLevel(), right, null)) - return false; - else - return !(context.getLevel().getBlockState(left) == Blocks.AIR.defaultBlockState() && !(context.getLevel().getBlockState(left.below()) == Blocks.AIR.defaultBlockState()) && BlockKeyboardRight.checkNeighborhood(context.getLevel(), left, null)); - } - @Override public @NotNull InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { - if (player.isShiftKeyDown()) { - ItemStack itemStack = player.getItemInHand(hand); - if (canBeReplaced(state, new BlockPlaceContext(player, hand, itemStack, hit))) { - int f = (int) Math.floor(((double) (player.getYRot() * 4.0f / 360.0f)) + 2.5) & 3; - Vec3i dir = Direction.from2DDataValue(f).getNormal(); - level.setBlock(pos.offset(dir), BlockInit.blockKeyBoard.get().defaultBlockState(), UPDATE_ALL_IMMEDIATE); - return InteractionResult.SUCCESS; - } - } - if(player.getItemInHand(hand).getItem() instanceof ItemLinker) return InteractionResult.PASS; - TileEntityKeyboard tek = getTileEntity(level, pos); + TileEntityKeyboard tek = BlockKeyboardLeft.getTileEntity(state, level, pos); if(tek != null) return tek.onRightClick(player, hand); return InteractionResult.PASS; } + + @Override + public VoxelShape getOcclusionShape(BlockState arg, BlockGetter arg2, BlockPos arg3) { + return Shapes.empty(); + } } diff --git a/src/main/java/net/montoyo/wd/block/BlockPeripheral.java b/src/main/java/net/montoyo/wd/block/BlockPeripheral.java index ead69d7..9dee7d4 100644 --- a/src/main/java/net/montoyo/wd/block/BlockPeripheral.java +++ b/src/main/java/net/montoyo/wd/block/BlockPeripheral.java @@ -181,30 +181,7 @@ public class BlockPeripheral extends WDBlockContainer { if(world.isClientSide) return; - if(state.getValue(type) == DefaultPeripheral.KEYBOARD) { - //Keyboard special treatment - Direction f = state.getValue(facing); - Vec3i dir = f.getClockWise().getNormal(); - BlockPos left = pos.offset(dir); - BlockPos right = pos.subtract(dir); - - if(!world.isEmptyBlock(pos.below()) && BlockKeyboardRight.checkNeighborhood(world, pos, null)) { - if(world.isEmptyBlock(right) && !world.isEmptyBlock(right.below()) && BlockKeyboardRight.checkNeighborhood(world, right, pos)) { - world.setBlock(right, BlockInit.blockKbRight.get().defaultBlockState().setValue(BlockKeyboardRight.facing, f), 3); - return; - } else if(world.isEmptyBlock(left) && !world.isEmptyBlock(left.below()) && BlockKeyboardRight.checkNeighborhood(world, left, pos)) { - world.setBlock(left, state, 3); - world.setBlock(pos, BlockInit.blockKbRight.get().defaultBlockState().setValue(BlockKeyboardRight.facing, f), 3); - return; - } - } - - //Not good; remove this shit... - world.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState()); - if(!(placer instanceof Player) || !((Player) placer).isCreative()) { -// dropBlockAsItem(world, pos, state, 0); TODO: Loottable? - } - } else if(placer instanceof Player) { + if(placer instanceof Player) { BlockEntity te = world.getBlockEntity(pos); if(te instanceof TileEntityServer) diff --git a/src/main/java/net/montoyo/wd/block/item/KeyboardItem.java b/src/main/java/net/montoyo/wd/block/item/KeyboardItem.java new file mode 100644 index 0000000..572aa8b --- /dev/null +++ b/src/main/java/net/montoyo/wd/block/item/KeyboardItem.java @@ -0,0 +1,41 @@ +package net.montoyo.wd.block.item; + +import net.minecraft.core.Direction; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.context.BlockPlaceContext; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; +import net.montoyo.wd.block.BlockKeyboardLeft; +import net.montoyo.wd.init.BlockInit; + +public class KeyboardItem extends BlockItem { + public KeyboardItem(Block arg, Properties arg2) { + super(arg, arg2); + } + + @Override + protected boolean placeBlock(BlockPlaceContext arg, BlockState arg2) { + Direction facing = arg.getHorizontalDirection(); + arg2 = arg2.setValue(BlockKeyboardLeft.FACING, facing); + + Direction d = BlockKeyboardLeft.mapDirection(facing); + + if (isValid(arg, arg2, d)) { + Block kbRight = BlockInit.blockKbRight.get(); + BlockState rightState = kbRight.defaultBlockState(); + + rightState = rightState.setValue(BlockKeyboardLeft.FACING, facing); + if (!arg.getLevel().setBlock( + arg.getClickedPos().relative(d), + rightState, + 11 + )) return false; + return arg.getLevel().setBlock(arg.getClickedPos(), arg2, 11);// 161 + } + return false; + } + + private boolean isValid(BlockPlaceContext context, BlockState state, Direction d) { + return context.getLevel().getBlockState(context.getClickedPos().relative(d)).isAir(); + } +} diff --git a/src/main/java/net/montoyo/wd/client/renderers/ScreenRenderer.java b/src/main/java/net/montoyo/wd/client/renderers/ScreenRenderer.java index 4c07fb3..ae0e803 100644 --- a/src/main/java/net/montoyo/wd/client/renderers/ScreenRenderer.java +++ b/src/main/java/net/montoyo/wd/client/renderers/ScreenRenderer.java @@ -144,10 +144,10 @@ public class ScreenRenderer implements BlockEntityRenderer { //Bounding box debugging - /*poseStack.pushPose(); - poseStack.translate(-te.getBlockPos().getX(), -te.getBlockPos().getY(), -te.getBlockPos().getZ()); - renderAABB(te.getRenderBoundingBox()); - poseStack.popPose();*/ +// poseStack.pushPose(); +// poseStack.translate(-te.getBlockPos().getX(), -te.getBlockPos().getY(), -te.getBlockPos().getZ()); +// renderAABB(te.getRenderBoundingBox()); +// poseStack.popPose(); //Re-enable lighting RenderSystem.enableCull(); diff --git a/src/main/java/net/montoyo/wd/init/BlockInit.java b/src/main/java/net/montoyo/wd/init/BlockInit.java index 037c100..61befff 100644 --- a/src/main/java/net/montoyo/wd/init/BlockInit.java +++ b/src/main/java/net/montoyo/wd/init/BlockInit.java @@ -21,12 +21,11 @@ public class BlockInit { public static final RegistryObject blockScreen = BLOCKS.register("screen", () -> new BlockScreen(BlockBehaviour.Properties.of(Material.STONE))); public static final RegistryObject blockKeyBoard = BlockInit.BLOCKS.register("kb_left", BlockKeyboardLeft::new); + public static final RegistryObject blockKbRight = BLOCKS.register("kb_right", BlockKeyboardRight::new); public static final RegistryObject blockRedControl = BlockInit.BLOCKS.register("redctrl", BlockRedCTRL::new); public static final RegistryObject blockRControl = BlockInit.BLOCKS.register("rctrl", BlockRCTRL::new); public static final RegistryObject blockServer = BlockInit.BLOCKS.register("server", BlockServer::new); - - public static final RegistryObject blockKbRight = BLOCKS.register("kb_right", BlockKeyboardRight::new); } diff --git a/src/main/java/net/montoyo/wd/init/ItemInit.java b/src/main/java/net/montoyo/wd/init/ItemInit.java index 58b8856..c16d24b 100644 --- a/src/main/java/net/montoyo/wd/init/ItemInit.java +++ b/src/main/java/net/montoyo/wd/init/ItemInit.java @@ -8,6 +8,7 @@ import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; import net.montoyo.wd.WebDisplays; import net.montoyo.wd.block.WDBlockContainer; +import net.montoyo.wd.block.item.KeyboardItem; import net.montoyo.wd.core.CraftComponent; import net.montoyo.wd.core.DefaultUpgrade; import net.montoyo.wd.item.*; @@ -45,7 +46,7 @@ public class ItemInit{ public static final RegistryObject screen = ITEMS.register("screen", () -> new BlockItem(BlockInit.blockScreen.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); - public static final RegistryObject keyboard = ITEMS.register("keyboard", () -> new BlockItem(BlockInit.blockKbRight.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); + public static final RegistryObject keyboard = ITEMS.register("keyboard", () -> new KeyboardItem(BlockInit.blockKeyBoard.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject redctrl = ITEMS.register("redctrl", () -> new BlockItem(BlockInit.blockRedControl.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject rctrl = ITEMS.register("rctrl", () -> new BlockItem(BlockInit.blockRControl.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject server = ITEMS.register("server", () -> new BlockItem(BlockInit.blockServer.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); diff --git a/src/main/java/net/montoyo/wd/miniserv/server/Server.java b/src/main/java/net/montoyo/wd/miniserv/server/Server.java index af98522..73fe883 100644 --- a/src/main/java/net/montoyo/wd/miniserv/server/Server.java +++ b/src/main/java/net/montoyo/wd/miniserv/server/Server.java @@ -159,44 +159,48 @@ public class Server implements Runnable { clientList.add(toAdd); } } - - if(key.isReadable()) { - ServerClient cli = clientMap.get(key.channel()); - - if(cli == null) - Log.warning("Received read info from unknown client"); - else { - try { - readBuffer.clear(); - int read = cli.getChannel().read(readBuffer); - - if(read < 0) - cli.setShouldRemove(); //End of stream - else if(read > 0) { - readBuffer.position(0); - readBuffer.limit(read); - cli.readyRead(readBuffer); + + try { + if (key.isReadable()) { + ServerClient cli = clientMap.get(key.channel()); + + if (cli == null) + Log.warning("Received read info from unknown client"); + else { + try { + readBuffer.clear(); + int read = cli.getChannel().read(readBuffer); + + if (read < 0) + cli.setShouldRemove(); //End of stream + else if (read > 0) { + readBuffer.position(0); + readBuffer.limit(read); + cli.readyRead(readBuffer); + } + } catch (Throwable t) { + Log.warningEx("Could not read data from client", t); + cli.setShouldRemove(); } - } catch(Throwable t) { - Log.warningEx("Could not read data from client", t); - cli.setShouldRemove(); } } - } - - if(key.isWritable()) { - ServerClient cli = clientMap.get(key.channel()); - - if(cli == null) - Log.warning("Received write info from unknown client"); - else { - try { - cli.readyWrite(); - } catch(Throwable t) { - Log.warningEx("Could not write data to client", t); - cli.setShouldRemove(); + + if (key.isWritable()) { + ServerClient cli = clientMap.get(key.channel()); + + if (cli == null) + Log.warning("Received write info from unknown client"); + else { + try { + cli.readyWrite(); + } catch (Throwable t) { + Log.warningEx("Could not write data to client", t); + cli.setShouldRemove(); + } } } + } catch (Throwable err) { + continue; } } diff --git a/src/main/java/net/montoyo/wd/utilities/Util.java b/src/main/java/net/montoyo/wd/utilities/Util.java index 163d945..3712691 100644 --- a/src/main/java/net/montoyo/wd/utilities/Util.java +++ b/src/main/java/net/montoyo/wd/utilities/Util.java @@ -9,6 +9,7 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Style; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Player; import java.lang.reflect.Field; @@ -42,6 +43,8 @@ public abstract class Util { bb.writeInt(ray.length); for(Object o : ray) serialize(bb, o); + } else if (cls == ResourceLocation.class) { + bb.writeUtf(f.toString()); } else if(!cls.isPrimitive()) { Field[] fields = cls.getFields(); @@ -80,6 +83,8 @@ public abstract class Util { ray[i] = unserialize(bb, cls.getComponentType()); return Arrays.copyOf(ray, ray.length, cls); + } else if(cls == ResourceLocation.class) { + return new ResourceLocation(bb.readUtf()); } else if(!cls.isPrimitive()) { Object ret; Field[] fields = cls.getFields();