From b2f56f49bbc56546e5f86e18d7af3d724d46e8bf Mon Sep 17 00:00:00 2001 From: "BT (calcastor/mame)" <43831917+calcastor@users.noreply.github.com> Date: Mon, 1 Jan 2024 08:59:01 -0800 Subject: [PATCH] Avoid reflection when using SportPaper The reflection used in NMSHacks1_8 breaks in modern Java versions, but we can modify SportPaper to avoid that here. Signed-off-by: BT (calcastor/mame) <43831917+calcastor@users.noreply.github.com> --- .../tc/oc/pgm/util/nms/v1_8/NMSHacks1_8.java | 8 +++--- .../pgm/util/nms/v1_8/NMSHacksSportPaper.java | 25 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacks1_8.java b/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacks1_8.java index be896fc8b0..f5f401b629 100644 --- a/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacks1_8.java +++ b/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacks1_8.java @@ -837,12 +837,12 @@ public boolean canMineBlock(MaterialData blockMaterial, ItemStack tool) { || (nmsTool != null && nmsTool.canDestroySpecialBlock(nmsBlock))); } - Field worldServerField = ReflectionUtils.getField(CraftWorld.class, "world"); - Field dimensionField = ReflectionUtils.getField(WorldServer.class, "dimension"); - Field modifiersField = ReflectionUtils.getField(Field.class, "modifiers"); - @Override public void resetDimension(World world) { + Field worldServerField = ReflectionUtils.getField(CraftWorld.class, "world"); + Field dimensionField = ReflectionUtils.getField(WorldServer.class, "dimension"); + Field modifiersField = ReflectionUtils.getField(Field.class, "modifiers"); + try { modifiersField.setInt(dimensionField, dimensionField.getModifiers() & ~Modifier.FINAL); diff --git a/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacksSportPaper.java b/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacksSportPaper.java index 1221040eee..97f55a9117 100644 --- a/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacksSportPaper.java +++ b/util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacksSportPaper.java @@ -1,6 +1,8 @@ package tc.oc.pgm.util.nms.v1_8; import com.google.common.collect.SetMultimap; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Map; import java.util.Set; @@ -12,11 +14,14 @@ import net.minecraft.server.v1_8_R3.PacketPlayOutScoreboardTeam; import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntity; import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntityLiving; +import net.minecraft.server.v1_8_R3.WorldServer; import org.bukkit.Bukkit; import org.bukkit.ChunkSnapshot; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.BlockState; +import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; import org.bukkit.craftbukkit.v1_8_R3.scoreboard.CraftTeam; import org.bukkit.entity.EntityType; @@ -30,6 +35,7 @@ import org.bukkit.util.Vector; import tc.oc.pgm.util.attribute.AttributeModifier; import tc.oc.pgm.util.nms.EnumPlayerInfoAction; +import tc.oc.pgm.util.reflect.ReflectionUtils; import tc.oc.pgm.util.skin.Skin; public class NMSHacksSportPaper extends NMSHacks1_8 { @@ -185,6 +191,25 @@ public Skin getPlayerSkinForViewer(Player player, Player viewer) { : getPlayerSkin(player); } + @Override + public void resetDimension(World world) { + try { + ((CraftWorld) world).getHandle().dimension = 11; + } catch (IllegalAccessError e) { + // Fallback for older versions of SportPaper + Field worldServerField = ReflectionUtils.getField(CraftWorld.class, "world"); + Field dimensionField = ReflectionUtils.getField(WorldServer.class, "dimension"); + Field modifiersField = ReflectionUtils.getField(Field.class, "modifiers"); + + try { + modifiersField.setInt(dimensionField, dimensionField.getModifiers() & ~Modifier.FINAL); + dimensionField.set(worldServerField.get(world), 11); + } catch (IllegalAccessException ex) { + // No-op, newer version of Java have disabled modifying final fields + } + } + } + @Override public void setCanDestroy(ItemMeta itemMeta, Collection materials) { itemMeta.setCanDestroy(materials);