improve protocol

This commit is contained in:
Sen 2025-07-05 14:46:56 +02:00
parent 0c7459d371
commit eed9ea565f
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
3 changed files with 114 additions and 99 deletions

View file

@ -245,14 +245,16 @@ public class Item {
return false; return false;
} }
public final boolean isValidTag(TagObject tag) { public final boolean hasTags() {
if(this.validTags == null) return this.validTags != null;
return false;
for(TagObject valid : this.validTags) {
if(valid.equals(tag))
return true;
} }
return false;
public final TagObject getItemTag(int index) {
return this.validTags == null || index < 0 || index >= this.validTags.size() ? null : this.validTags.get(index);
}
public final int getTagIndex(TagObject tag) {
return tag == null || this.validTags == null ? -1 : this.validTags.indexOf(tag);
} }
public float getRadiation(ItemStack stack) { public float getRadiation(ItemStack stack) {

View file

@ -2,51 +2,60 @@ package common.packet;
import java.io.IOException; import java.io.IOException;
import common.init.ItemRegistry;
import common.item.Item;
import common.item.ItemStack; import common.item.ItemStack;
import common.network.IPlayer; import common.network.IPlayer;
import common.network.Packet; import common.network.Packet;
import common.network.PacketBuffer; import common.network.PacketBuffer;
public class CPacketCheat implements Packet<IPlayer> public class CPacketCheat implements Packet<IPlayer> {
{ private Item item;
private ItemStack stack; private int tagIndex;
private int slot; private int slot;
private boolean stacked;
public CPacketCheat() public CPacketCheat() {
{
} }
public CPacketCheat(ItemStack stackIn, int slot, boolean full) public CPacketCheat(ItemStack stack, int slot, boolean full) {
{ this.item = stack.getItem();
this.stack = stackIn.copy(); this.tagIndex = this.item.getTagIndex(stack.getTag());
this.slot = slot; this.slot = slot;
this.stack.size = full ? this.stack.getMaxStackSize() : 1; this.stacked = full;
} }
public void processPacket(IPlayer handler) public void processPacket(IPlayer handler) {
{
handler.processCheat(this); handler.processCheat(this);
} }
public void readPacketData(PacketBuffer buf) throws IOException public void readPacketData(PacketBuffer buf) throws IOException {
{ this.item = ItemRegistry.byId(buf.readShort());
this.stack = buf.readItemStack(); this.tagIndex = buf.readVarInt();
this.slot = buf.readByte(); this.slot = buf.readShort();
this.stacked = buf.readBoolean();
} }
public void writePacketData(PacketBuffer buf) throws IOException public void writePacketData(PacketBuffer buf) throws IOException {
{ buf.writeShort(ItemRegistry.getId(this.item));
buf.writeItemStack(this.stack); buf.writeVarInt(this.tagIndex);
buf.writeByte(this.slot); buf.writeShort(this.slot);
buf.writeBoolean(this.stacked);
} }
public ItemStack getStack() public Item getItem() {
{ return this.item;
return this.stack;
} }
public int getSlot() public int getTagIndex() {
{ return this.tagIndex;
}
public boolean isStacked() {
return this.stacked;
}
public int getSlot() {
return this.slot; return this.slot;
} }
} }

View file

@ -41,6 +41,7 @@ import common.inventory.IInventory;
import common.inventory.InventoryPlayer; import common.inventory.InventoryPlayer;
import common.inventory.Slot; import common.inventory.Slot;
import common.inventory.SlotCrafting; import common.inventory.SlotCrafting;
import common.item.Item;
import common.item.ItemControl; import common.item.ItemControl;
import common.item.ItemStack; import common.item.ItemStack;
import common.log.Log; import common.log.Log;
@ -2876,9 +2877,13 @@ public class Player extends User implements ICrafting, Executor, IPlayer
NetHandler.checkThread(packet, this, this.server); NetHandler.checkThread(packet, this, this.server);
if(this.charEditor || !this.isAdmin()) if(this.charEditor || !this.isAdmin())
return; return;
ItemStack stack = packet.getStack(); Item item = packet.getItem();
if(stack.getItem() != null && stack.size <= stack.getMaxStackSize() && stack.size > 0 && (!stack.hasTag() || stack.getItem().isValidTag(stack.getTag()))) if(item == null)
{ return;
ItemStack stack = new ItemStack(item, packet.isStacked() ? item.getItemStackLimit() : 1);
stack.setTag(item.getItemTag(packet.getTagIndex()));
if(item.hasTags() != stack.hasTag())
return;
int amount = stack.size; int amount = stack.size;
if(amount <= 0) if(amount <= 0)
return; return;
@ -2931,7 +2936,6 @@ public class Player extends User implements ICrafting, Executor, IPlayer
this.addFeed(TextColor.DGREEN + "* %d %s geschummelt", amount, this.addFeed(TextColor.DGREEN + "* %d %s geschummelt", amount,
stack.getColoredName(TextColor.DGREEN)); stack.getColoredName(TextColor.DGREEN));
} }
}
public void processSign(CPacketSign packetIn) public void processSign(CPacketSign packetIn)
{ {