|
1 | 1 | package com.github.vini2003.linkart.utility; |
2 | 2 |
|
| 3 | +import net.fabricmc.loader.api.FabricLoader; |
3 | 4 | import net.minecraft.entity.vehicle.AbstractMinecartEntity; |
4 | 5 | import net.minecraft.nbt.NbtCompound; |
5 | 6 | import net.minecraft.nbt.NbtElement; |
|
10 | 11 | import net.minecraft.util.math.BlockPos; |
11 | 12 | import net.minecraft.util.math.ChunkPos; |
12 | 13 | import net.minecraft.world.PersistentState; |
| 14 | +import net.minecraft.world.PersistentStateManager; |
13 | 15 |
|
| 16 | +import java.lang.invoke.*; |
14 | 17 | import java.util.HashSet; |
15 | 18 | import java.util.Set; |
| 19 | +import java.util.function.Function; |
| 20 | +import java.util.function.Supplier; |
16 | 21 |
|
17 | 22 | public class LoadingCarts extends PersistentState { |
18 | 23 |
|
| 24 | + private static Function<PersistentStateManager, LoadingCarts> GETTER; |
| 25 | + static { |
| 26 | + Supplier<LoadingCarts> supplier = LoadingCarts::new; |
| 27 | + Function<NbtCompound, LoadingCarts> function = nbt -> new LoadingCarts().readNbt(nbt); |
| 28 | + try { |
| 29 | + Type<LoadingCarts> type = new Type<>(supplier, function, null);//thanks, FAPI |
| 30 | + GETTER = manager -> manager.getOrCreate(type, "linkart_loading_carts"); |
| 31 | + } catch (Throwable e) { |
| 32 | + String mth = FabricLoader.getInstance().getMappingResolver().mapMethodName("intermediary", "net.minecraft.class_26", "method_17924", "(Ljava/util/function/Function;Ljava/util/function/Supplier;Ljava/lang/String;)Lnet/minecraft/class_18;"); |
| 33 | + MethodHandle h = Lambdas.supply(() -> MethodHandles.lookup().findVirtual(PersistentStateManager.class, mth, MethodType.methodType(PersistentState.class, Function.class, Supplier.class, String.class))); |
| 34 | + |
| 35 | + //Not pretty, but is faster than reflection and handles. |
| 36 | + interface Invoker { |
| 37 | + PersistentState invoke(PersistentStateManager manager, Function<?,?> f, Supplier<?> s, String name); |
| 38 | + } |
| 39 | + |
| 40 | + Invoker invoker = Lambdas.handle(MethodHandles.lookup(), Invoker.class, h); |
| 41 | + GETTER = manager -> (LoadingCarts) invoker.invoke(manager, function, supplier, "linkart_loading_carts");; |
| 42 | + } |
| 43 | + } |
| 44 | + |
19 | 45 | public static LoadingCarts getOrCreate(ServerWorld world) { |
20 | | - return world.getPersistentStateManager().getOrCreate( |
21 | | - (nbt) -> new LoadingCarts().readNbt(nbt), |
22 | | - LoadingCarts::new, "linkart_loading_carts"); |
| 46 | + return GETTER.apply(world.getPersistentStateManager()); |
23 | 47 | } |
24 | 48 |
|
25 | 49 | private final Set<BlockPos> chunksToReload = new HashSet<>(); |
|
0 commit comments