diff --git a/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/eco/proxy/v1_15_R1/PacketPlayOutRecipeUpdateFix.java b/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/eco/proxy/v1_15_R1/PacketPlayOutRecipeUpdateFix.java index 68fa30ad..2114377b 100644 --- a/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/eco/proxy/v1_15_R1/PacketPlayOutRecipeUpdateFix.java +++ b/eco-core/core-nms/v1_15_R1/src/main/java/com/willfp/eco/proxy/v1_15_R1/PacketPlayOutRecipeUpdateFix.java @@ -1,9 +1,9 @@ package com.willfp.eco.proxy.v1_15_R1; import com.willfp.eco.proxy.proxies.PacketPlayOutRecipeUpdateFixProxy; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; import net.minecraft.server.v1_15_R1.IRecipe; import net.minecraft.server.v1_15_R1.PacketPlayOutRecipeUpdate; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; @@ -14,44 +14,33 @@ import java.util.List; @SuppressWarnings("unchecked") public final class PacketPlayOutRecipeUpdateFix implements PacketPlayOutRecipeUpdateFixProxy { @Override - public Object splitAndModifyPacket(@NotNull final Object object) { + public List splitPackets(@NotNull final Object object, + @NotNull final Player player) { if (!(object instanceof PacketPlayOutRecipeUpdate)) { throw new IllegalArgumentException("Parameter not packet!"); } PacketPlayOutRecipeUpdate oldPacket = (PacketPlayOutRecipeUpdate) object; List> recipes = new ArrayList<>(); - Field f = null; try { - f = oldPacket.getClass().getDeclaredField("a"); + Field f = oldPacket.getClass().getDeclaredField("a"); f.setAccessible(true); recipes.addAll((Collection>) f.get(oldPacket)); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } - if (f == null) { - return null; - } - - List> externRecipes = new ArrayList<>(); - for (IRecipe recipe : new ArrayList<>(recipes)) { - if (AbstractEcoPlugin.LOADED_ECO_PLUGINS.contains(recipe.getKey().getNamespace())) { - externRecipes.add(recipe); - recipes.remove(recipe); + List splitPackets = new ArrayList<>(); + List> splitRecipes = new ArrayList<>(); + for (int i = 0; i < recipes.size(); i++) { + splitRecipes.add(recipes.get(i)); + if (i % 100 == 0) { + PacketPlayOutRecipeUpdate newPacket = new PacketPlayOutRecipeUpdate(splitRecipes); + splitPackets.add(newPacket); + splitRecipes.clear(); } } - if (recipes.isEmpty()) { - return null; - } - - try { - f.set(object, recipes); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - return new PacketPlayOutRecipeUpdate(externRecipes); + return splitPackets; } } diff --git a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/PacketPlayOutRecipeUpdateFix.java b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/PacketPlayOutRecipeUpdateFix.java index ae1d8084..6f061a6b 100644 --- a/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/PacketPlayOutRecipeUpdateFix.java +++ b/eco-core/core-nms/v1_16_R1/src/main/java/com/willfp/eco/proxy/v1_16_R1/PacketPlayOutRecipeUpdateFix.java @@ -1,9 +1,9 @@ package com.willfp.eco.proxy.v1_16_R1; import com.willfp.eco.proxy.proxies.PacketPlayOutRecipeUpdateFixProxy; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; import net.minecraft.server.v1_16_R1.IRecipe; import net.minecraft.server.v1_16_R1.PacketPlayOutRecipeUpdate; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; @@ -14,44 +14,33 @@ import java.util.List; @SuppressWarnings("unchecked") public final class PacketPlayOutRecipeUpdateFix implements PacketPlayOutRecipeUpdateFixProxy { @Override - public Object splitAndModifyPacket(@NotNull final Object object) { + public List splitPackets(@NotNull final Object object, + @NotNull final Player player) { if (!(object instanceof PacketPlayOutRecipeUpdate)) { throw new IllegalArgumentException("Parameter not packet!"); } PacketPlayOutRecipeUpdate oldPacket = (PacketPlayOutRecipeUpdate) object; List> recipes = new ArrayList<>(); - Field f = null; try { - f = oldPacket.getClass().getDeclaredField("a"); + Field f = oldPacket.getClass().getDeclaredField("a"); f.setAccessible(true); recipes.addAll((Collection>) f.get(oldPacket)); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } - if (f == null) { - return null; - } - - List> externRecipes = new ArrayList<>(); - for (IRecipe recipe : new ArrayList<>(recipes)) { - if (AbstractEcoPlugin.LOADED_ECO_PLUGINS.contains(recipe.getKey().getNamespace())) { - externRecipes.add(recipe); - recipes.remove(recipe); + List splitPackets = new ArrayList<>(); + List> splitRecipes = new ArrayList<>(); + for (int i = 0; i < recipes.size(); i++) { + splitRecipes.add(recipes.get(i)); + if (i % 100 == 0) { + PacketPlayOutRecipeUpdate newPacket = new PacketPlayOutRecipeUpdate(splitRecipes); + splitPackets.add(newPacket); + splitRecipes.clear(); } } - if (recipes.isEmpty()) { - return null; - } - - try { - f.set(object, recipes); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - return new PacketPlayOutRecipeUpdate(externRecipes); + return splitPackets; } } diff --git a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/PacketPlayOutRecipeUpdateFix.java b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/PacketPlayOutRecipeUpdateFix.java index 2fb3139f..abb6ac00 100644 --- a/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/PacketPlayOutRecipeUpdateFix.java +++ b/eco-core/core-nms/v1_16_R2/src/main/java/com/willfp/eco/proxy/v1_16_R2/PacketPlayOutRecipeUpdateFix.java @@ -1,9 +1,9 @@ package com.willfp.eco.proxy.v1_16_R2; import com.willfp.eco.proxy.proxies.PacketPlayOutRecipeUpdateFixProxy; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; import net.minecraft.server.v1_16_R2.IRecipe; import net.minecraft.server.v1_16_R2.PacketPlayOutRecipeUpdate; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; @@ -14,44 +14,33 @@ import java.util.List; @SuppressWarnings("unchecked") public final class PacketPlayOutRecipeUpdateFix implements PacketPlayOutRecipeUpdateFixProxy { @Override - public Object splitAndModifyPacket(@NotNull final Object object) { + public List splitPackets(@NotNull final Object object, + @NotNull final Player player) { if (!(object instanceof PacketPlayOutRecipeUpdate)) { throw new IllegalArgumentException("Parameter not packet!"); } PacketPlayOutRecipeUpdate oldPacket = (PacketPlayOutRecipeUpdate) object; List> recipes = new ArrayList<>(); - Field f = null; try { - f = oldPacket.getClass().getDeclaredField("a"); + Field f = oldPacket.getClass().getDeclaredField("a"); f.setAccessible(true); recipes.addAll((Collection>) f.get(oldPacket)); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } - if (f == null) { - return null; - } - - List> externRecipes = new ArrayList<>(); - for (IRecipe recipe : new ArrayList<>(recipes)) { - if (AbstractEcoPlugin.LOADED_ECO_PLUGINS.contains(recipe.getKey().getNamespace())) { - externRecipes.add(recipe); - recipes.remove(recipe); + List splitPackets = new ArrayList<>(); + List> splitRecipes = new ArrayList<>(); + for (int i = 0; i < recipes.size(); i++) { + splitRecipes.add(recipes.get(i)); + if (i % 100 == 0) { + PacketPlayOutRecipeUpdate newPacket = new PacketPlayOutRecipeUpdate(splitRecipes); + splitPackets.add(newPacket); + splitRecipes.clear(); } } - if (recipes.isEmpty()) { - return null; - } - - try { - f.set(object, recipes); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - return new PacketPlayOutRecipeUpdate(externRecipes); + return splitPackets; } } diff --git a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/PacketPlayOutRecipeUpdateFix.java b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/PacketPlayOutRecipeUpdateFix.java index f77a65c9..3366a7e0 100644 --- a/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/PacketPlayOutRecipeUpdateFix.java +++ b/eco-core/core-nms/v1_16_R3/src/main/java/com/willfp/eco/proxy/v1_16_R3/PacketPlayOutRecipeUpdateFix.java @@ -1,9 +1,9 @@ package com.willfp.eco.proxy.v1_16_R3; import com.willfp.eco.proxy.proxies.PacketPlayOutRecipeUpdateFixProxy; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; import net.minecraft.server.v1_16_R3.IRecipe; import net.minecraft.server.v1_16_R3.PacketPlayOutRecipeUpdate; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; @@ -14,44 +14,33 @@ import java.util.List; @SuppressWarnings("unchecked") public final class PacketPlayOutRecipeUpdateFix implements PacketPlayOutRecipeUpdateFixProxy { @Override - public Object splitAndModifyPacket(@NotNull final Object object) { + public List splitPackets(@NotNull final Object object, + @NotNull final Player player) { if (!(object instanceof PacketPlayOutRecipeUpdate)) { throw new IllegalArgumentException("Parameter not packet!"); } PacketPlayOutRecipeUpdate oldPacket = (PacketPlayOutRecipeUpdate) object; List> recipes = new ArrayList<>(); - Field f = null; try { - f = oldPacket.getClass().getDeclaredField("a"); + Field f = oldPacket.getClass().getDeclaredField("a"); f.setAccessible(true); recipes.addAll((Collection>) f.get(oldPacket)); } catch (IllegalAccessException | NoSuchFieldException e) { e.printStackTrace(); } - if (f == null) { - return null; - } - - List> externRecipes = new ArrayList<>(); - for (IRecipe recipe : new ArrayList<>(recipes)) { - if (AbstractEcoPlugin.LOADED_ECO_PLUGINS.contains(recipe.getKey().getNamespace())) { - externRecipes.add(recipe); - recipes.remove(recipe); + List splitPackets = new ArrayList<>(); + List> splitRecipes = new ArrayList<>(); + for (int i = 0; i < recipes.size(); i++) { + splitRecipes.add(recipes.get(i)); + if (i % 100 == 0) { + PacketPlayOutRecipeUpdate newPacket = new PacketPlayOutRecipeUpdate(splitRecipes); + splitPackets.add(newPacket); + splitRecipes.clear(); } } - if (recipes.isEmpty()) { - return null; - } - - try { - f.set(object, recipes); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - - return new PacketPlayOutRecipeUpdate(externRecipes); + return splitPackets; } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/PacketPlayOutRecipeUpdateFix.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/PacketPlayOutRecipeUpdateFix.java index 4f5aa0fb..880d2e39 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/PacketPlayOutRecipeUpdateFix.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/PacketPlayOutRecipeUpdateFix.java @@ -11,6 +11,7 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import java.lang.reflect.InvocationTargetException; +import java.util.List; public class PacketPlayOutRecipeUpdateFix extends PacketAdapter { /** @@ -27,14 +28,16 @@ public class PacketPlayOutRecipeUpdateFix extends PacketAdapter { PacketContainer packet = event.getPacket(); Player player = event.getPlayer(); - Object otherPacket = InternalProxyUtils.getProxy(PacketPlayOutRecipeUpdateFixProxy.class).splitAndModifyPacket(packet.getHandle()); - if (otherPacket != null) { + List packets = InternalProxyUtils.getProxy(PacketPlayOutRecipeUpdateFixProxy.class).splitPackets(packet.getHandle(), player); + if (packets.size() > 1) { event.setCancelled(true); - PacketContainer container = PacketContainer.fromPacket(otherPacket); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(player, container); - } catch (InvocationTargetException e) { - e.printStackTrace(); + for (Object o : packets) { + PacketContainer container = PacketContainer.fromPacket(o); + try { + ProtocolLibrary.getProtocolManager().sendServerPacket(player, container); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } } } } diff --git a/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/PacketPlayOutRecipeUpdateFixProxy.java b/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/PacketPlayOutRecipeUpdateFixProxy.java index 8f844f00..1b8f847a 100644 --- a/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/PacketPlayOutRecipeUpdateFixProxy.java +++ b/eco-core/core-proxy/src/main/java/com/willfp/eco/proxy/proxies/PacketPlayOutRecipeUpdateFixProxy.java @@ -2,14 +2,19 @@ package com.willfp.eco.proxy.proxies; import com.willfp.eco.util.proxy.AbstractProxy; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.util.List; + public interface PacketPlayOutRecipeUpdateFixProxy extends AbstractProxy { /** * Split recipe update packet into smaller packets. * * @param object The packet. - * @return The extra packet for eco recipes. + * @param player The player. + * @return The packets, split up. */ - Object splitAndModifyPacket(@NotNull Object object); + List splitPackets(@NotNull Object object, + @NotNull Player player); }