diff --git a/src/main/java/pw/rxj/iron_quarry/item/BlueprintItem.java b/src/main/java/pw/rxj/iron_quarry/item/BlueprintItem.java index d720923..f4b8704 100644 --- a/src/main/java/pw/rxj/iron_quarry/item/BlueprintItem.java +++ b/src/main/java/pw/rxj/iron_quarry/item/BlueprintItem.java @@ -31,7 +31,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import pw.rxj.iron_quarry.interfaces.*; -import pw.rxj.iron_quarry.network.PacketBlueprintPositionSet; +import pw.rxj.iron_quarry.network.PacketBlueprintExpand; import pw.rxj.iron_quarry.network.ZNetwork; import pw.rxj.iron_quarry.recipe.HandledSmithingRecipe; import pw.rxj.iron_quarry.screen.QuarryBlockScreen; @@ -121,9 +121,7 @@ public boolean handleMainHandScrolling(ClientPlayerEntity player, ItemStack stac }; if(this.expandInDirection(stack, player, direction, amount)) { - BlockPos firstPos = this.getFirstPos(stack).orElse(null); - BlockPos secondPos = this.getSecondPos(stack).orElse(null); - ZNetwork.sendToServer(PacketBlueprintPositionSet.bake(firstPos, secondPos)); + ZNetwork.sendToServer(PacketBlueprintExpand.bake(direction, (byte) amount)); return true; } diff --git a/src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintExpand.java b/src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintExpand.java new file mode 100644 index 0000000..bf6197a --- /dev/null +++ b/src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintExpand.java @@ -0,0 +1,64 @@ +package pw.rxj.iron_quarry.network; + +import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.entity.player.PlayerInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.network.ServerPlayNetworkHandler; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.Direction; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import oshi.util.tuples.Pair; +import pw.rxj.iron_quarry.Main; +import pw.rxj.iron_quarry.item.BlueprintItem; +import pw.rxj.iron_quarry.network.packet.DirectionBytePacket; +import pw.rxj.iron_quarry.util.ReadableString; +import pw.rxj.iron_quarry.util.ZUtil; + +public class PacketBlueprintExpand extends ComplexPacketHandler { + public static Pair ALLOWED_DISTANCE = new Pair<>(-16, 16); + + protected static PacketBlueprintExpand INSTANCE = new PacketBlueprintExpand(); + + private PacketBlueprintExpand() { } + + @Override + public Identifier getChannelId() { + return Identifier.of(Main.MOD_ID, "blueprint_position_set"); + } + @Override + public @Nullable DirectionBytePacket read(PacketByteBuf buf) { + return DirectionBytePacket.read(buf); + } + + protected boolean checkPacket(ServerPlayerEntity player, DirectionBytePacket packet) { + if(packet.data < ALLOWED_DISTANCE.getA() || packet.data > ALLOWED_DISTANCE.getB()) { + player.networkHandler.disconnect(ReadableString.translatable("kick_reason.iron_quarry.packet_blueprint_position_set.disallowed_distance", ALLOWED_DISTANCE.getA(), ALLOWED_DISTANCE.getB())); + return false; + } + + return true; + } + + @Override + protected void receiveFromClient(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, @NotNull DirectionBytePacket packet, PacketSender response) { + if(!this.checkPacket(player, packet)) return; + + PlayerInventory playerInventory = player.getInventory(); + ItemStack holdingStack = playerInventory.getMainHandStack(); + + if(ZUtil.getBlockOrItem(holdingStack) instanceof BlueprintItem blueprintItem) { + if(blueprintItem.isSealed(holdingStack)) return; + + blueprintItem.setWorld(holdingStack, player.getWorld()); + blueprintItem.expandInDirection(holdingStack, player, packet.direction, packet.data); + } + } + + public static PacketByteBuf bake(Direction direction, byte distance) { + return DirectionBytePacket.write(INSTANCE.getChannelId(), direction, distance); + } +} diff --git a/src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintPositionSet.java b/src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintPositionSet.java deleted file mode 100644 index ebcf799..0000000 --- a/src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintPositionSet.java +++ /dev/null @@ -1,49 +0,0 @@ -package pw.rxj.iron_quarry.network; - -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.minecraft.entity.player.PlayerInventory; -import net.minecraft.item.ItemStack; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import pw.rxj.iron_quarry.Main; -import pw.rxj.iron_quarry.item.BlueprintItem; -import pw.rxj.iron_quarry.network.packet.DoubleBlockPosPacket; -import pw.rxj.iron_quarry.util.ZUtil; - -public class PacketBlueprintPositionSet extends ComplexPacketHandler { - protected static PacketBlueprintPositionSet INSTANCE = new PacketBlueprintPositionSet(); - - private PacketBlueprintPositionSet() { } - - @Override - public Identifier getChannelId() { - return Identifier.of(Main.MOD_ID, "blueprint_position_set"); - } - @Override - public @Nullable DoubleBlockPosPacket read(PacketByteBuf buf) { - return DoubleBlockPosPacket.read(buf); - } - - @Override - protected void receiveFromClient(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, @NotNull DoubleBlockPosPacket packet, PacketSender response) { - PlayerInventory playerInventory = player.getInventory(); - ItemStack holdingStack = playerInventory.getMainHandStack(); - if(ZUtil.getBlockOrItem(holdingStack) instanceof BlueprintItem blueprintItem) { - blueprintItem.setWorld(holdingStack, player.getWorld()); - blueprintItem.setFirstPos(holdingStack, packet.blockPos1); - blueprintItem.setSecondPos(holdingStack, packet.blockPos2); - } - } - - public static @Nullable PacketByteBuf bake(@Nullable BlockPos firstPos, @Nullable BlockPos secondPos) { - if(firstPos == null || secondPos == null) return null; - - return DoubleBlockPosPacket.write(INSTANCE.getChannelId(), firstPos, secondPos); - } -} diff --git a/src/main/java/pw/rxj/iron_quarry/network/ZNetwork.java b/src/main/java/pw/rxj/iron_quarry/network/ZNetwork.java index 088d329..6bf6dba 100644 --- a/src/main/java/pw/rxj/iron_quarry/network/ZNetwork.java +++ b/src/main/java/pw/rxj/iron_quarry/network/ZNetwork.java @@ -20,8 +20,8 @@ public class ZNetwork { private static final List> packetList = List.of( - PacketBlueprintPositionSet.INSTANCE, - PacketQuarryBlockBreak.INSTANCE + PacketQuarryBlockBreak.INSTANCE, + PacketBlueprintExpand.INSTANCE ); @Environment(EnvType.CLIENT) diff --git a/src/main/java/pw/rxj/iron_quarry/network/packet/DirectionBytePacket.java b/src/main/java/pw/rxj/iron_quarry/network/packet/DirectionBytePacket.java new file mode 100644 index 0000000..042bc48 --- /dev/null +++ b/src/main/java/pw/rxj/iron_quarry/network/packet/DirectionBytePacket.java @@ -0,0 +1,35 @@ +package pw.rxj.iron_quarry.network.packet; + +import io.netty.buffer.Unpooled; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.Direction; + +public class DirectionBytePacket { + public final Direction direction; + public final byte data; + + private DirectionBytePacket(Direction direction, byte data) { + this.direction = direction; + this.data = data; + } + + public static DirectionBytePacket read(PacketByteBuf buf) { + try { + Direction direction = Direction.byId(buf.readByte()); + byte data = buf.readByte(); + + return new DirectionBytePacket(direction, data); + } catch(Exception ignored) { + return null; + } + } + public static PacketByteBuf write(Identifier channel, Direction direction, byte data) { + PacketByteBuf packet = new PacketByteBuf(Unpooled.buffer()); + packet.writeIdentifier(channel); + packet.writeByte(direction.getId()); + packet.writeByte(data); + + return packet; + } + } \ No newline at end of file diff --git a/src/main/java/pw/rxj/iron_quarry/network/packet/DoubleBlockPosPacket.java b/src/main/java/pw/rxj/iron_quarry/network/packet/DoubleBlockPosPacket.java deleted file mode 100644 index 8dba639..0000000 --- a/src/main/java/pw/rxj/iron_quarry/network/packet/DoubleBlockPosPacket.java +++ /dev/null @@ -1,35 +0,0 @@ -package pw.rxj.iron_quarry.network.packet; - -import io.netty.buffer.Unpooled; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.util.Identifier; -import net.minecraft.util.math.BlockPos; - -public class DoubleBlockPosPacket { - public final BlockPos blockPos1; - public final BlockPos blockPos2; - - private DoubleBlockPosPacket(BlockPos blockPos1, BlockPos blockPos2) { - this.blockPos1 = blockPos1; - this.blockPos2 = blockPos2; - } - - public static DoubleBlockPosPacket read(PacketByteBuf buf) { - try { - BlockPos blockPos1 = buf.readBlockPos(); - BlockPos blockPos2 = buf.readBlockPos(); - - return new DoubleBlockPosPacket(blockPos1, blockPos2); - } catch(Exception ignored) { - return null; - } - } - public static PacketByteBuf write(Identifier channel, BlockPos blockPos1, BlockPos blockPos2) { - PacketByteBuf packet = new PacketByteBuf(Unpooled.buffer()); - packet.writeIdentifier(channel); - packet.writeBlockPos(blockPos1); - packet.writeBlockPos(blockPos2); - - return packet; - } - } \ No newline at end of file diff --git a/src/main/resources/assets/iron_quarry/lang/en_us.json b/src/main/resources/assets/iron_quarry/lang/en_us.json index f8f1d40..f4b43ca 100644 --- a/src/main/resources/assets/iron_quarry/lang/en_us.json +++ b/src/main/resources/assets/iron_quarry/lang/en_us.json @@ -64,5 +64,7 @@ "screen.iron_quarry.quarry_block.tooltip.blueprint_info": "§{RGB_TAB_TITLE Insert §lSealed Blueprint§r to start the quarry.}", "screen.iron_quarry.quarry_block.tooltip.battery_info": "§{RGB_TAB_TITLE Insert §la battery§r to charge the quarry externally.}", "screen.iron_quarry.quarry_block.title.configuration": "§{RGB_TAB_TITLE Configuration}", - "screen.iron_quarry.quarry_block.title.augmentation": "§{RGB_TAB_TITLE Augmentation}" + "screen.iron_quarry.quarry_block.title.augmentation": "§{RGB_TAB_TITLE Augmentation}", + + "kick_reason.iron_quarry.packet_blueprint_position_set.disallowed_distance": "Tried to expand blueprint position by an illegal amount. (Must be between %s and %s)" } \ No newline at end of file