Skip to content

Commit

Permalink
Avoid reflection when using SportPaper
Browse files Browse the repository at this point in the history
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) <[email protected]>
  • Loading branch information
calcastor committed Jan 12, 2024
1 parent 682f968 commit b2f56f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacks1_8.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
25 changes: 25 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/v1_8/NMSHacksSportPaper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Material> materials) {
itemMeta.setCanDestroy(materials);
Expand Down

0 comments on commit b2f56f4

Please sign in to comment.