From 77a929f6a1d62d8255f557aa62e2bd057b57f25c Mon Sep 17 00:00:00 2001 From: peaches <107517810+peaches94@users.noreply.github.com> Date: Mon, 25 Jul 2022 11:49:10 -0500 Subject: [PATCH] fix: use servers removeGameEventListener instead of reimpl (#1881) * fix: use servers removeGameEventListener instead of reimpl * fix: 1.18.2 impl --- .../v1_18_R2/PaperweightPlatformAdapter.java | 29 +++++---------- .../v1_19_R1/PaperweightPlatformAdapter.java | 36 +++++++------------ 2 files changed, 20 insertions(+), 45 deletions(-) diff --git a/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightPlatformAdapter.java b/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightPlatformAdapter.java index ad3205feb..8546de630 100644 --- a/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightPlatformAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_18_2/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_18_R2/PaperweightPlatformAdapter.java @@ -99,7 +99,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { private static final Field fieldLock; private static final long fieldLockOffset; - private static final Field fieldGameEventDispatcherSections; + private static final MethodHandle methodRemoveGameEventListener; private static final MethodHandle methodremoveTickingBlockEntity; private static final Field fieldRemove; @@ -148,9 +148,12 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { fieldLockOffset = -1; } - fieldGameEventDispatcherSections = LevelChunk.class.getDeclaredField(Refraction.pickName( - "gameEventDispatcherSections", "t")); - fieldGameEventDispatcherSections.setAccessible(true); + Method removeGameEventListener = LevelChunk.class.getDeclaredMethod( + Refraction.pickName("removeGameEventListener", "d"), + BlockEntity.class + ); + removeGameEventListener.setAccessible(true); + methodRemoveGameEventListener = MethodHandles.lookup().unreflect(removeGameEventListener); Method removeBlockEntityTicker = LevelChunk.class.getDeclaredMethod( Refraction.pickName( "removeBlockEntityTicker", @@ -573,23 +576,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { BlockEntity blockEntity = levelChunk.blockEntities.remove(beacon.getBlockPos()); if (blockEntity != null) { if (!levelChunk.level.isClientSide) { - Block block = beacon.getBlockState().getBlock(); - if (block instanceof EntityBlock) { - GameEventListener gameEventListener = ((EntityBlock) block).getListener(levelChunk.level, beacon); - if (gameEventListener != null) { - int i = SectionPos.blockToSectionCoord(beacon.getBlockPos().getY()); - GameEventDispatcher gameEventDispatcher = levelChunk.getEventDispatcher(i); - gameEventDispatcher.unregister(gameEventListener); - if (gameEventDispatcher.isEmpty()) { - try { - ((Int2ObjectMap) fieldGameEventDispatcherSections.get(levelChunk)) - .remove(i); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - } - } + methodRemoveGameEventListener.invoke(levelChunk, beacon); } fieldRemove.set(beacon, true); } diff --git a/worldedit-bukkit/adapters/adapter-1_19/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R1/PaperweightPlatformAdapter.java b/worldedit-bukkit/adapters/adapter-1_19/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R1/PaperweightPlatformAdapter.java index 9a7e73503..aa87aceea 100644 --- a/worldedit-bukkit/adapters/adapter-1_19/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R1/PaperweightPlatformAdapter.java +++ b/worldedit-bukkit/adapters/adapter-1_19/src/main/java/com/sk89q/worldedit/bukkit/adapter/impl/fawe/v1_19_R1/PaperweightPlatformAdapter.java @@ -62,6 +62,8 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -99,7 +101,7 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { private static final Field fieldLock; private static final long fieldLockOffset; - private static final Field fieldGameEventDispatcherSections; + private static final MethodHandle methodRemoveGameEventListener; private static final MethodHandle methodremoveTickingBlockEntity; private static final Field fieldRemove; @@ -148,9 +150,14 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { fieldLockOffset = -1; } - fieldGameEventDispatcherSections = LevelChunk.class.getDeclaredField(Refraction.pickName( - "gameEventDispatcherSections", "t")); - fieldGameEventDispatcherSections.setAccessible(true); + Method removeGameEventListener = LevelChunk.class.getDeclaredMethod( + Refraction.pickName("removeGameEventListener", "a"), + BlockEntity.class, + ServerLevel.class + ); + removeGameEventListener.setAccessible(true); + methodRemoveGameEventListener = MethodHandles.lookup().unreflect(removeGameEventListener); + Method removeBlockEntityTicker = LevelChunk.class.getDeclaredMethod( Refraction.pickName( "removeBlockEntityTicker", @@ -564,32 +571,13 @@ public final class PaperweightPlatformAdapter extends NMSAdapter { return BiomeTypes.get(biome.unwrapKey().orElseThrow().location().toString()); } - @SuppressWarnings("unchecked") static void removeBeacon(BlockEntity beacon, LevelChunk levelChunk) { try { - // Do the method ourselves to avoid trying to reflect generic method parameters - // similar to removeGameEventListener if (levelChunk.loaded || levelChunk.level.isClientSide()) { BlockEntity blockEntity = levelChunk.blockEntities.remove(beacon.getBlockPos()); if (blockEntity != null) { if (!levelChunk.level.isClientSide) { - Block block = beacon.getBlockState().getBlock(); - if (block instanceof EntityBlock) { - GameEventListener gameEventListener = ((EntityBlock) block).getListener(levelChunk.level, beacon); - if (gameEventListener != null) { - int i = SectionPos.blockToSectionCoord(beacon.getBlockPos().getY()); - GameEventDispatcher gameEventDispatcher = levelChunk.getEventDispatcher(i); - gameEventDispatcher.unregister(gameEventListener); - if (gameEventDispatcher.isEmpty()) { - try { - ((Int2ObjectMap) fieldGameEventDispatcherSections.get(levelChunk)) - .remove(i); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - } - } - } + methodRemoveGameEventListener.invoke(levelChunk, beacon, levelChunk.level); } fieldRemove.set(beacon, true); }