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;
}
public final boolean isValidTag(TagObject tag) {
if(this.validTags == null)
return false;
for(TagObject valid : this.validTags) {
if(valid.equals(tag))
return true;
}
return false;
public final boolean hasTags() {
return this.validTags != null;
}
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) {

View file

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

View file

@ -41,6 +41,7 @@ import common.inventory.IInventory;
import common.inventory.InventoryPlayer;
import common.inventory.Slot;
import common.inventory.SlotCrafting;
import common.item.Item;
import common.item.ItemControl;
import common.item.ItemStack;
import common.log.Log;
@ -2876,61 +2877,64 @@ public class Player extends User implements ICrafting, Executor, IPlayer
NetHandler.checkThread(packet, this, this.server);
if(this.charEditor || !this.isAdmin())
return;
ItemStack stack = packet.getStack();
if(stack.getItem() != null && stack.size <= stack.getMaxStackSize() && stack.size > 0 && (!stack.hasTag() || stack.getItem().isValidTag(stack.getTag())))
{
int amount = stack.size;
Item item = packet.getItem();
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;
if(amount <= 0)
return;
if(packet.getSlot() == -1) {
this.entity.inventory.addItemStackToInventory(stack);
amount -= stack.size;
if(amount <= 0)
return;
if(packet.getSlot() == -1) {
this.entity.inventory.addItemStackToInventory(stack);
amount -= stack.size;
if(amount <= 0)
return;
}
else if(packet.getSlot() <= -2 - 9) {
this.entity.dropItem(stack, false, true);
}
else {
Slot slot = packet.getSlot() < 0 ? this.entity.inventoryContainer.getSlot(36 + -2 - packet.getSlot()) : this.entity.openContainer.getSlot(packet.getSlot());
if(slot == null || !slot.canCheatItem())
return;
stack.size = Math.min(slot.getSlotStackLimit(), stack.size);
amount = stack.size;
if(amount <= 0)
return;
if(slot.getHasStack()) {
ItemStack old = slot.getStack();
if(ItemStack.areItemsEqual(stack, old) && ItemStack.areItemStackTagsEqual(stack, old)) {
stack.size = Math.min(slot.getSlotStackLimit(), Math.min(stack.getMaxStackSize(), old.size + stack.size));
amount = stack.size - old.size;
if(amount <= 0 || !slot.isItemValid(stack))
return;
}
else {
if(!slot.isItemValid(stack))
return;
if(old.size == 1)
this.addFeed(TextColor.DRED + "* %s zerstört",
old.getColoredName(TextColor.DRED));
else
this.addFeed(TextColor.DRED + "* %d %s zerstört", old.size,
old.getColoredName(TextColor.DRED));
}
}
else if(!slot.isItemValid(stack))
return;
slot.putStack(stack);
}
this.entity.openContainer.detectAndSendChanges();
this.entity.worldObj.playSoundAtEntity(this.entity, SoundEvent.POP, 0.2F);
if(amount == 1)
this.addFeed(TextColor.DGREEN + "* %s geschummelt",
stack.getColoredName(TextColor.DGREEN));
else
this.addFeed(TextColor.DGREEN + "* %d %s geschummelt", amount,
stack.getColoredName(TextColor.DGREEN));
}
}
else if(packet.getSlot() <= -2 - 9) {
this.entity.dropItem(stack, false, true);
}
else {
Slot slot = packet.getSlot() < 0 ? this.entity.inventoryContainer.getSlot(36 + -2 - packet.getSlot()) : this.entity.openContainer.getSlot(packet.getSlot());
if(slot == null || !slot.canCheatItem())
return;
stack.size = Math.min(slot.getSlotStackLimit(), stack.size);
amount = stack.size;
if(amount <= 0)
return;
if(slot.getHasStack()) {
ItemStack old = slot.getStack();
if(ItemStack.areItemsEqual(stack, old) && ItemStack.areItemStackTagsEqual(stack, old)) {
stack.size = Math.min(slot.getSlotStackLimit(), Math.min(stack.getMaxStackSize(), old.size + stack.size));
amount = stack.size - old.size;
if(amount <= 0 || !slot.isItemValid(stack))
return;
}
else {
if(!slot.isItemValid(stack))
return;
if(old.size == 1)
this.addFeed(TextColor.DRED + "* %s zerstört",
old.getColoredName(TextColor.DRED));
else
this.addFeed(TextColor.DRED + "* %d %s zerstört", old.size,
old.getColoredName(TextColor.DRED));
}
}
else if(!slot.isItemValid(stack))
return;
slot.putStack(stack);
}
this.entity.openContainer.detectAndSendChanges();
this.entity.worldObj.playSoundAtEntity(this.entity, SoundEvent.POP, 0.2F);
if(amount == 1)
this.addFeed(TextColor.DGREEN + "* %s geschummelt",
stack.getColoredName(TextColor.DGREEN));
else
this.addFeed(TextColor.DGREEN + "* %d %s geschummelt", amount,
stack.getColoredName(TextColor.DGREEN));
}
public void processSign(CPacketSign packetIn)