-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔒️ Restricted blueprint expansion packet to negate abusive behaviour.
- Loading branch information
Showing
7 changed files
with
103 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintExpand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
49 changes: 0 additions & 49 deletions
49
src/main/java/pw/rxj/iron_quarry/network/PacketBlueprintPositionSet.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/pw/rxj/iron_quarry/network/packet/DirectionBytePacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
src/main/java/pw/rxj/iron_quarry/network/packet/DoubleBlockPosPacket.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters