Skip to content

Commit

Permalink
🔒️ Restricted blueprint expansion packet to negate abusive behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
RXJpaw committed Dec 15, 2023
1 parent 30caf54 commit 8cbfe26
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 91 deletions.
6 changes: 2 additions & 4 deletions src/main/java/pw/rxj/iron_quarry/item/BlueprintItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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<DirectionBytePacket> {
public static Pair<Integer, Integer> 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) {
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);
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/pw/rxj/iron_quarry/network/ZNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

public class ZNetwork {
private static final List<ComplexPacketHandler<?>> packetList = List.of(
PacketBlueprintPositionSet.INSTANCE,
PacketQuarryBlockBreak.INSTANCE
PacketQuarryBlockBreak.INSTANCE,
PacketBlueprintExpand.INSTANCE
);

@Environment(EnvType.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

This file was deleted.

4 changes: 3 additions & 1 deletion src/main/resources/assets/iron_quarry/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}

0 comments on commit 8cbfe26

Please sign in to comment.