Skip to content

Commit 2e1e8c1

Browse files
committed
Use Paper's spawnIn directly.
1 parent 80acf4e commit 2e1e8c1

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

InvSee++_Platforms/Impl_1_21_11_R7/src/main/java/com/janboerman/invsee/spigot/impl_1_21_11_R7/HybridServerSupport.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.minecraft.core.RegistryAccess;
1717
import net.minecraft.nbt.CompoundTag;
1818
import net.minecraft.server.MinecraftServer;
19+
import net.minecraft.server.level.ServerLevel;
1920
import net.minecraft.server.level.ServerPlayer;
2021
import net.minecraft.server.players.NameAndId;
2122
import net.minecraft.util.ProblemReporter;
@@ -147,17 +148,18 @@ public static void spawnIn(FakeEntityPlayer fakeEntityPlayer, Level world) {
147148
try {
148149
fakeEntityPlayer.spawnIn(world, true/*ignore respawn anchor charge*/); //note: not only sets the ServerLevel, also sets x/y/z coordinates and gamemode.
149150
} catch (NoSuchMethodError e1) {
151+
RuntimeException ex = new RuntimeException("No method known to set the player's location.");
152+
ex.addSuppressed(e1);
150153
try {
151-
Method[] methods = FuzzyReflection.getMethodOfType(ServerPlayer.class, void.class, Level.class);
152-
methods[0].invoke(fakeEntityPlayer, world); //on Paper the boolean parameter for respawn anchor charge does not exist.
153-
} catch (ArrayIndexOutOfBoundsException | ReflectiveOperationException e2) {
154-
RuntimeException ex = new RuntimeException("No method known to set the player's location.");
155-
ex.addSuppressed(e1);
156-
ex.addSuppressed(e2);
154+
// Because we are likely on Paper, and it uses mojang mappings at runtime, we can just reflectively call the method
155+
// without worrying about obfuscation mappings :)
156+
Method method = ServerPlayer.class.getDeclaredMethod("spawnIn", ServerLevel.class);
157+
method.invoke(fakeEntityPlayer, world);
158+
} catch (ReflectiveOperationException e3) {
159+
ex.addSuppressed(e3);
157160
throw ex;
158161
}
159162
}
160163
}
161164

162-
163165
}

InvSee++_Platforms/Impl_1_21_9_R6/src/main/java/com/janboerman/invsee/spigot/impl_1_21_9_R6/HybridServerSupport.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
import java.lang.reflect.Field;
88
import java.lang.reflect.InvocationTargetException;
99
import java.lang.reflect.Method;
10+
import java.util.Arrays;
1011
import java.util.Optional;
1112
import java.util.UUID;
13+
import java.util.stream.Collectors;
1214

1315
import com.janboerman.invsee.utils.FuzzyReflection;
1416

1517
import net.minecraft.core.NonNullList;
1618
import net.minecraft.core.RegistryAccess;
1719
import net.minecraft.nbt.CompoundTag;
1820
import net.minecraft.server.MinecraftServer;
21+
import net.minecraft.server.level.ServerLevel;
1922
import net.minecraft.server.level.ServerPlayer;
2023
import net.minecraft.server.players.NameAndId;
2124
import net.minecraft.util.ProblemReporter;
@@ -147,13 +150,15 @@ public static void spawnIn(FakeEntityPlayer fakeEntityPlayer, Level world) {
147150
try {
148151
fakeEntityPlayer.spawnIn(world, true/*ignore respawn anchor charge*/); //note: not only sets the ServerLevel, also sets x/y/z coordinates and gamemode.
149152
} catch (NoSuchMethodError e1) {
153+
RuntimeException ex = new RuntimeException("No method known to set the player's location.");
154+
ex.addSuppressed(e1);
150155
try {
151-
Method[] methods = FuzzyReflection.getMethodOfType(ServerPlayer.class, void.class, Level.class);
152-
methods[0].invoke(fakeEntityPlayer, world); //on Paper the boolean parameter for respawn anchor charge does not exist.
153-
} catch (ArrayIndexOutOfBoundsException | ReflectiveOperationException e2) {
154-
RuntimeException ex = new RuntimeException("No method known to set the player's location.");
155-
ex.addSuppressed(e1);
156-
ex.addSuppressed(e2);
156+
// Because we are likely on Paper, and it uses mojang mappings at runtime, we can just reflectively call the method
157+
// without worrying about obfuscation mappings :)
158+
Method method = ServerPlayer.class.getDeclaredMethod("spawnIn", ServerLevel.class);
159+
method.invoke(fakeEntityPlayer, world);
160+
} catch (ReflectiveOperationException e3) {
161+
ex.addSuppressed(e3);
157162
throw ex;
158163
}
159164
}

0 commit comments

Comments
 (0)