kalculos / AstralFlow

The plugin featurize your spigot servers.
https://af.ib67.io/docs
GNU Lesser General Public License v2.1
25 stars 4 forks source link

[FEAT] The PDC Filter #79

Open iceBear67 opened 2 years ago

iceBear67 commented 2 years ago

To reduce meaningless bandwidth overheads caused by #42 (with PDC), PDC Filter is set.

Patch

By redefine.

At FriendlyByteBuf (Paper, called PacketDataSerializer in spigot)

    public FriendlyByteBuf writeItem(ItemStack stack) {
        if (stack.isEmpty() || stack.getItem() == null) { // CraftBukkit - NPE fix itemstack.getItem()
            this.writeBoolean(false);
        } else {
            this.writeBoolean(true);
            Item item = stack.getItem();

            this.writeVarInt(Item.getId(item));
            this.writeByte(stack.getCount());
            CompoundTag nbttagcompound = null;

            if (item.canBeDepleted() || item.shouldOverrideMultiplayerNbt()) {
                // Spigot start - filter
                stack = stack.copy();
                // CraftItemStack.setItemMeta(stack, CraftItemStack.getItemMeta(stack)); // Paper - This is no longer needed due to NBT being supported
                // Spigot end
                nbttagcompound = stack.getTag();
                // Paper start
                if (nbttagcompound != null && nbttagcompound.contains("SkullOwner", 10)) {
                    CompoundTag owner = nbttagcompound.getCompound("SkullOwner");
                    if (owner.hasUUID("Id")) {
                        nbttagcompound.putUUID("SkullOwnerOrig", owner.getUUID("Id"));
                        net.minecraft.world.level.block.entity.SkullBlockEntity.sanitizeUUID(owner);
                    }
                }
                // Paper end
            }

            this.writeNbt(nbttagcompound);
        }

        return this;
    }

Inserting hooks before this.writeNbt(nbttagcompound) would work, however it is leading to full of asm codes.

Tasks

So here're our tasks:

  1. Find CompoundTag in obfuscated environments. (including 1.18+, treat them as obfuscated)
  2. Find FriendlyByteBuf / PacketDataSerializer in obfuscated environments.
  3. Determine injector solution. Predefine or redefine or server patching? I prefer redefine with mixin but there're no projects has implemented mixin injection for bukkit ( or poor API )

Other

This feature is an option and disabled by default.

iceBear67 commented 2 years ago

We will need to handle creative item actions due to minecraft feature. Players in creative mode sends itemstack data from their client, which overrides server-side item even though it has pdc or sth else.

iceBear67 commented 2 years ago

I ve heard something from paper-dev. They said the limitation isnt too small

I will try it later

iceBear67 commented 2 years ago
Internal Exception: net.minecraft.network.PacketEncoder$PacketTooLargeException: PacketTooLarge - PacketPlayOutSetSlot is 5000438. Max is 2097152

And that means we can store ~2M data in one pdc, that's fully enough for many situations.

iceBear67 commented 2 years ago

We will not work on this for this moment.