Merge pull request #2 from GiantLuigi4/master

rework keyboards
This commit is contained in:
Mysticpasta1 2023-03-03 23:49:53 -06:00 committed by GitHub
commit 57beebfc02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 235 additions and 389 deletions

View File

@ -40,6 +40,7 @@ dependencies {
modImplementation "com.github.Mysticpasta1:mcef-forge:a9bf168a92" modImplementation "com.github.Mysticpasta1:mcef-forge:a9bf168a92"
modImplementation "curse.maven:cloth_config_forge-348521:3972423" modImplementation "curse.maven:cloth_config_forge-348521:3972423"
modImplementation "curse.maven:SU-370704:4410614" modImplementation "curse.maven:SU-370704:4410614"
modImplementation "curse.maven:spark-361579:4381167"
// Uncomment the following line to enable the deprecated Fabric API modules. // 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. // 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.

View File

@ -13,9 +13,11 @@ import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Explosion; import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level; 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.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RenderShape; 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.DirectionProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty; import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.Property; 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.level.material.PushReaction;
import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
@ -41,173 +42,86 @@ import net.montoyo.wd.init.BlockInit;
import net.montoyo.wd.item.ItemLinker; import net.montoyo.wd.item.ItemLinker;
import net.montoyo.wd.net.Messages; import net.montoyo.wd.net.Messages;
import net.montoyo.wd.net.client.CMessageCloseGui; import net.montoyo.wd.net.client.CMessageCloseGui;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public class BlockKeyboardLeft extends BlockPeripheral { public class BlockKeyboardLeft extends BlockPeripheral {
public static final EnumProperty<DefaultPeripheral> type = EnumProperty.create("type", DefaultPeripheral.class); public static final EnumProperty<DefaultPeripheral> 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 FACING = DirectionProperty.create("facing", Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST);
private static final Property<?>[] properties = new Property<?>[] { type, facing }; // public static final DirectionProperty HALF = DirectionProperty.create("facing", Direction.EAST, Direction.WEST);
private static final Property<?>[] properties = new Property<?>[] {TYPE, FACING};
public BlockKeyboardLeft() { 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 @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(properties); 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<ItemStack> 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<? extends BlockEntity> 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 @Override
public RenderShape getRenderShape(BlockState state) { public RenderShape getRenderShape(BlockState state) {
return RenderShape.MODEL; return RenderShape.MODEL;
} }
// @Override
// public int damageDropped(IBlockState state) {
// return state.getValue(type).toMetadata(0);
// }
@Override @Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) {
if(player.isShiftKeyDown()) double rpos = (entity.getY() - ((double) pos.getY())) * 16.0;
return InteractionResult.FAIL; if (!world.isClientSide && rpos >= 1.0 && rpos <= 2.0 && Math.random() < 0.25) {
TileEntityKeyboard tek = BlockKeyboardLeft.getTileEntity(state, world, pos);
if(player.getItemInHand(hand).getItem() instanceof ItemLinker) if (tek != null)
return InteractionResult.FAIL; tek.simulateCat(entity);
}
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 @Override
// public boolean isFullCube(IBlockState state) { TODO: FIx. public @NotNull InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
// return state.getValue(type) != DefaultPeripheral.KEYBOARD; if(player.getItemInHand(hand).getItem() instanceof ItemLinker)
// } return InteractionResult.PASS;
//
// @Override TileEntityKeyboard tek = BlockKeyboardLeft.getTileEntity(state, level, pos);
// public boolean isFullBlock(IBlockState state) { if(tek != null)
// return state.getValue(type) != DefaultPeripheral.KEYBOARD; return tek.onRightClick(player, hand);
// }
// return InteractionResult.PASS;
// @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 @Override
public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return state.getValue(type) == DefaultPeripheral.KEYBOARD ? BlockKeyboardRight.KEYBOARD_AABB : Shapes.block(); 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);
}
} }
@Override @Override
@ -215,65 +129,47 @@ public class BlockKeyboardLeft extends BlockPeripheral {
return PushReaction.IGNORE; return PushReaction.IGNORE;
} }
private void removeRightPiece(Level world, BlockPos pos) { private static void removeRightPiece(BlockState state, Level world, BlockPos pos) {
for(Direction nf: Direction.Plane.HORIZONTAL) { BlockPos relative = pos.relative(BlockKeyboardLeft.mapDirection(state.getValue(FACING)));
BlockPos np = pos.offset(nf.getNormal());
if(world.getBlockState(np).getBlock() instanceof BlockKeyboardRight) { BlockState ns = world.getBlockState(relative);
world.setBlock(np, Blocks.AIR.defaultBlockState(), 3); if(ns.getBlock() instanceof BlockKeyboardRight) {
break; world.setBlock(relative, Blocks.AIR.defaultBlockState(), 3);
}
} }
} }
@Override public static void remove(BlockState state, Level world, BlockPos pos, boolean setState, boolean drop) {
public void neighborChanged(BlockState state, Level world, BlockPos pos, Block neighborType, BlockPos neighbor, boolean isMoving) { removeRightPiece(state, world, pos);
BlockEntity te = world.getBlockEntity(pos); if (setState) {
if(te != null && te instanceof TileEntityPeripheralBase) if (drop) {
((TileEntityPeripheralBase) te).onNeighborChange(neighborType, neighbor); // TODO: force drop item
}
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);
world.setBlock(pos, Blocks.AIR.defaultBlockState(), 3); 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 // @Override
public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) { // public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) {
if(!world.isClientSide) { // if(!world.isClientSide) {
if(state.getBlock() == this && state.getValue(type) == DefaultPeripheral.KEYBOARD) // remove(state, world, pos, false, false);
removeRightPiece(world, pos); // }
// }
Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> 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);
@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);
}
} }
super.onRemove(arg, arg2, arg3, arg4, bl);
} }
public static PacketDistributor.TargetPoint point(Level world, BlockPos bp) { public static PacketDistributor.TargetPoint point(Level world, BlockPos bp) {
return new PacketDistributor.TargetPoint(bp.getX(), bp.getY(), bp.getZ(), 64.0, world.dimension()); 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();
}
} }

View File

@ -4,11 +4,9 @@
package net.montoyo.wd.block; package net.montoyo.wd.block;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.core.Vec3i; import net.minecraft.core.Vec3i;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity; 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.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level; 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.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
@ -25,34 +22,28 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition; 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.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty; 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.FluidState;
import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult; 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.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.network.PacketDistributor;
import net.montoyo.wd.core.DefaultPeripheral; import net.montoyo.wd.core.DefaultPeripheral;
import net.montoyo.wd.core.IPeripheral; import net.montoyo.wd.core.IPeripheral;
import net.montoyo.wd.entity.TileEntityKeyboard; import net.montoyo.wd.entity.TileEntityKeyboard;
import net.montoyo.wd.entity.TileEntityScreen;
import net.montoyo.wd.init.BlockInit; import net.montoyo.wd.init.BlockInit;
import net.montoyo.wd.init.TileInit;
import net.montoyo.wd.item.ItemLinker; 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.BlockSide;
import net.montoyo.wd.utilities.Vector3i; import net.montoyo.wd.utilities.Vector3i;
import org.jetbrains.annotations.NotNull; 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 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 static final VoxelShape KEYBOARD_AABB = Shapes.box(0.0, 0.0, 0.0, 1.0, 1.0 / 16.0, 1.0);
public BlockKeyboardRight() { public BlockKeyboardRight() {
@ -60,9 +51,37 @@ public class BlockKeyboardRight extends Block implements IPeripheral {
.strength(1.5f, 10.f)); .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 @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(facing); builder.add(FACING);
} }
@Override @Override
@ -75,134 +94,37 @@ public class BlockKeyboardRight extends Block implements IPeripheral {
return KEYBOARD_AABB; 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 @Override
public boolean connect(Level world, BlockPos pos, BlockState state, Vector3i scrPos, BlockSide scrSide) { 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); 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 @Override
public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) { public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) {
double rpos = (entity.getY() - ((double) pos.getY())) * 16.0; double rpos = (entity.getY() - ((double) pos.getY())) * 16.0;
if (!world.isClientSide && rpos >= 1.0 && rpos <= 2.0 && Math.random() < 0.25) { 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) if (tek != null)
tek.simulateCat(entity); 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 @Override
public @NotNull InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { 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) if(player.getItemInHand(hand).getItem() instanceof ItemLinker)
return InteractionResult.PASS; return InteractionResult.PASS;
TileEntityKeyboard tek = getTileEntity(level, pos); TileEntityKeyboard tek = BlockKeyboardLeft.getTileEntity(state, level, pos);
if(tek != null) if(tek != null)
return tek.onRightClick(player, hand); return tek.onRightClick(player, hand);
return InteractionResult.PASS; return InteractionResult.PASS;
} }
@Override
public VoxelShape getOcclusionShape(BlockState arg, BlockGetter arg2, BlockPos arg3) {
return Shapes.empty();
}
} }

View File

@ -181,30 +181,7 @@ public class BlockPeripheral extends WDBlockContainer {
if(world.isClientSide) if(world.isClientSide)
return; return;
if(state.getValue(type) == DefaultPeripheral.KEYBOARD) { if(placer instanceof Player) {
//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) {
BlockEntity te = world.getBlockEntity(pos); BlockEntity te = world.getBlockEntity(pos);
if(te instanceof TileEntityServer) if(te instanceof TileEntityServer)

View File

@ -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();
}
}

View File

@ -144,10 +144,10 @@ public class ScreenRenderer implements BlockEntityRenderer<TileEntityScreen> {
//Bounding box debugging //Bounding box debugging
/*poseStack.pushPose(); // poseStack.pushPose();
poseStack.translate(-te.getBlockPos().getX(), -te.getBlockPos().getY(), -te.getBlockPos().getZ()); // poseStack.translate(-te.getBlockPos().getX(), -te.getBlockPos().getY(), -te.getBlockPos().getZ());
renderAABB(te.getRenderBoundingBox()); // renderAABB(te.getRenderBoundingBox());
poseStack.popPose();*/ // poseStack.popPose();
//Re-enable lighting //Re-enable lighting
RenderSystem.enableCull(); RenderSystem.enableCull();

View File

@ -21,12 +21,11 @@ public class BlockInit {
public static final RegistryObject<Block> blockScreen = BLOCKS.register("screen", () -> new BlockScreen(BlockBehaviour.Properties.of(Material.STONE))); public static final RegistryObject<Block> blockScreen = BLOCKS.register("screen", () -> new BlockScreen(BlockBehaviour.Properties.of(Material.STONE)));
public static final RegistryObject<Block> blockKeyBoard = BlockInit.BLOCKS.register("kb_left", BlockKeyboardLeft::new); public static final RegistryObject<Block> blockKeyBoard = BlockInit.BLOCKS.register("kb_left", BlockKeyboardLeft::new);
public static final RegistryObject<Block> blockKbRight = BLOCKS.register("kb_right", BlockKeyboardRight::new);
public static final RegistryObject<Block> blockRedControl = BlockInit.BLOCKS.register("redctrl", BlockRedCTRL::new); public static final RegistryObject<Block> blockRedControl = BlockInit.BLOCKS.register("redctrl", BlockRedCTRL::new);
public static final RegistryObject<Block> blockRControl = BlockInit.BLOCKS.register("rctrl", BlockRCTRL::new); public static final RegistryObject<Block> blockRControl = BlockInit.BLOCKS.register("rctrl", BlockRCTRL::new);
public static final RegistryObject<Block> blockServer = BlockInit.BLOCKS.register("server", BlockServer::new); public static final RegistryObject<Block> blockServer = BlockInit.BLOCKS.register("server", BlockServer::new);
public static final RegistryObject<Block> blockKbRight = BLOCKS.register("kb_right", BlockKeyboardRight::new);
} }

View File

@ -8,6 +8,7 @@ import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject; import net.minecraftforge.registries.RegistryObject;
import net.montoyo.wd.WebDisplays; import net.montoyo.wd.WebDisplays;
import net.montoyo.wd.block.WDBlockContainer; import net.montoyo.wd.block.WDBlockContainer;
import net.montoyo.wd.block.item.KeyboardItem;
import net.montoyo.wd.core.CraftComponent; import net.montoyo.wd.core.CraftComponent;
import net.montoyo.wd.core.DefaultUpgrade; import net.montoyo.wd.core.DefaultUpgrade;
import net.montoyo.wd.item.*; import net.montoyo.wd.item.*;
@ -45,7 +46,7 @@ public class ItemInit{
public static final RegistryObject<Item> screen = ITEMS.register("screen", () -> new BlockItem(BlockInit.blockScreen.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject<Item> screen = ITEMS.register("screen", () -> new BlockItem(BlockInit.blockScreen.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB)));
public static final RegistryObject<Item> keyboard = ITEMS.register("keyboard", () -> new BlockItem(BlockInit.blockKbRight.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject<Item> keyboard = ITEMS.register("keyboard", () -> new KeyboardItem(BlockInit.blockKeyBoard.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB)));
public static final RegistryObject<Item> redctrl = ITEMS.register("redctrl", () -> new BlockItem(BlockInit.blockRedControl.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject<Item> redctrl = ITEMS.register("redctrl", () -> new BlockItem(BlockInit.blockRedControl.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB)));
public static final RegistryObject<Item> rctrl = ITEMS.register("rctrl", () -> new BlockItem(BlockInit.blockRControl.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject<Item> rctrl = ITEMS.register("rctrl", () -> new BlockItem(BlockInit.blockRControl.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB)));
public static final RegistryObject<Item> server = ITEMS.register("server", () -> new BlockItem(BlockInit.blockServer.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB))); public static final RegistryObject<Item> server = ITEMS.register("server", () -> new BlockItem(BlockInit.blockServer.get(), new Item.Properties().tab(WebDisplays.CREATIVE_TAB)));

View File

@ -160,43 +160,47 @@ public class Server implements Runnable {
} }
} }
if(key.isReadable()) { try {
ServerClient cli = clientMap.get(key.channel()); if (key.isReadable()) {
ServerClient cli = clientMap.get(key.channel());
if(cli == null) if (cli == null)
Log.warning("Received read info from unknown client"); Log.warning("Received read info from unknown client");
else { else {
try { try {
readBuffer.clear(); readBuffer.clear();
int read = cli.getChannel().read(readBuffer); int read = cli.getChannel().read(readBuffer);
if(read < 0) if (read < 0)
cli.setShouldRemove(); //End of stream cli.setShouldRemove(); //End of stream
else if(read > 0) { else if (read > 0) {
readBuffer.position(0); readBuffer.position(0);
readBuffer.limit(read); readBuffer.limit(read);
cli.readyRead(readBuffer); 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()) { if (key.isWritable()) {
ServerClient cli = clientMap.get(key.channel()); ServerClient cli = clientMap.get(key.channel());
if(cli == null) if (cli == null)
Log.warning("Received write info from unknown client"); Log.warning("Received write info from unknown client");
else { else {
try { try {
cli.readyWrite(); cli.readyWrite();
} catch(Throwable t) { } catch (Throwable t) {
Log.warningEx("Could not write data to client", t); Log.warningEx("Could not write data to client", t);
cli.setShouldRemove(); cli.setShouldRemove();
}
} }
} }
} catch (Throwable err) {
continue;
} }
} }

View File

@ -9,6 +9,7 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style; import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -42,6 +43,8 @@ public abstract class Util {
bb.writeInt(ray.length); bb.writeInt(ray.length);
for(Object o : ray) for(Object o : ray)
serialize(bb, o); serialize(bb, o);
} else if (cls == ResourceLocation.class) {
bb.writeUtf(f.toString());
} else if(!cls.isPrimitive()) { } else if(!cls.isPrimitive()) {
Field[] fields = cls.getFields(); Field[] fields = cls.getFields();
@ -80,6 +83,8 @@ public abstract class Util {
ray[i] = unserialize(bb, cls.getComponentType()); ray[i] = unserialize(bb, cls.getComponentType());
return Arrays.copyOf(ray, ray.length, cls); return Arrays.copyOf(ray, ray.length, cls);
} else if(cls == ResourceLocation.class) {
return new ResourceLocation(bb.readUtf());
} else if(!cls.isPrimitive()) { } else if(!cls.isPrimitive()) {
Object ret; Object ret;
Field[] fields = cls.getFields(); Field[] fields = cls.getFields();