3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-19 09:20:08 +01:00
Dieser Commit ist enthalten in:
SirYwell 2024-04-11 18:15:00 +02:00
Ursprung 29b06692e0
Commit 09541dbe07
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden

Datei anzeigen

@ -1,5 +1,6 @@
package com.fastasyncworldedit.bukkit.adapter; package com.fastasyncworldedit.bukkit.adapter;
import com.destroystokyo.paper.util.SneakyThrow;
import com.fastasyncworldedit.bukkit.util.BukkitReflectionUtils; import com.fastasyncworldedit.bukkit.util.BukkitReflectionUtils;
import com.sk89q.worldedit.bukkit.BukkitAdapter; import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.adapter.Refraction; import com.sk89q.worldedit.bukkit.adapter.Refraction;
@ -24,11 +25,22 @@ import static java.lang.invoke.MethodType.methodType;
public class BukkitFoliaAdapter { public class BukkitFoliaAdapter {
// @formatter:off private static final MethodHandle GET_ENTITIES = createEntitiesGetter();
@SuppressWarnings("unchecked")
public static List<Entity> getEntities(World world, Region region) { public static List<Entity> getEntities(World world, Region region) {
try {
return (List<Entity>) GET_ENTITIES.invoke(world, region);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
// @formatter:off
private static MethodHandle createEntitiesGetter() {
try { try {
MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandles.Lookup lookup = MethodHandles.lookup();
Class<?> craftWorldClass = world.getClass(); // assume it's CraftWorld Class<?> craftWorldClass = BukkitReflectionUtils.getCbClass("CraftWorld");
Class<?> serverLevel = Class.forName(Refraction.pickName("net.minecraft.server.level.ServerLevel", "net.minecraft.server.level.WorldServer")); Class<?> serverLevel = Class.forName(Refraction.pickName("net.minecraft.server.level.ServerLevel", "net.minecraft.server.level.WorldServer"));
Class<?> craftEntity = BukkitReflectionUtils.getCbClass("entity.CraftEntity"); Class<?> craftEntity = BukkitReflectionUtils.getCbClass("entity.CraftEntity");
Class<?> nmsEntityClass = Class.forName("net.minecraft.world.entity.Entity"); Class<?> nmsEntityClass = Class.forName("net.minecraft.world.entity.Entity");
@ -55,12 +67,14 @@ public class BukkitFoliaAdapter {
MethodHandle addConvertedReturn = collectArguments(dropArguments(arrayListIdentity, 1, nmsEntityClass), 0, dropReturn(addConverted)); MethodHandle addConvertedReturn = collectArguments(dropArguments(arrayListIdentity, 1, nmsEntityClass), 0, dropReturn(addConverted));
MethodHandle addConvertedReturnCollapsed = permuteArguments(addConvertedReturn, methodType(ArrayList.class, ArrayList.class, nmsEntityClass), 0, 1, 0, 1); MethodHandle addConvertedReturnCollapsed = permuteArguments(addConvertedReturn, methodType(ArrayList.class, ArrayList.class, nmsEntityClass), 0, 1, 0, 1);
MethodHandle newArrayListHandle = lookup.findConstructor(ArrayList.class, methodType(void.class)); MethodHandle newArrayListHandle = lookup.findConstructor(ArrayList.class, methodType(void.class));
MethodHandle ifInRegion = guardWithTest(isInRegion, dropArguments(addConvertedReturnCollapsed, 2, Region.class), dropArguments(arrayListIdentity, 1, nmsEntityClass, Region.class)); MethodHandle ifTrue = dropArguments(addConvertedReturnCollapsed, 2, Region.class);
MethodHandle ifFalse = dropArguments(arrayListIdentity, 1, nmsEntityClass, Region.class);
MethodHandle ifInRegion = guardWithTest(isInRegion, ifTrue, ifFalse);
MethodHandle iterate = iteratedLoop(null, newArrayListHandle, dropArguments(ifInRegion, 2, Iterable.class)); MethodHandle iterate = iteratedLoop(null, newArrayListHandle, dropArguments(ifInRegion, 2, Iterable.class));
MethodHandle preIter = filterArguments(iterate, 0, getEntities); return filterArguments(iterate, 0, getEntities);
return (List<Entity>) preIter.invoke(world, region);
} catch (Throwable t) { } catch (Throwable t) {
throw new RuntimeException(t); SneakyThrow.sneaky(t);
return null;
} }
} }
// @formatter:on // @formatter:on