Skip to content
This repository was archived by the owner on Oct 20, 2024. It is now read-only.

Commit 10525ef

Browse files
committed
Fix LoadingCarts in 1.20.2 and patch-in backwards-compat.
1 parent befc288 commit 10525ef

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Fix velocityMultiplier not being applied. (By [GeeTransit](https://github.com/GeeTransit))
55
* Carts will now stop when the link is broken. (By [GeeTransit](https://github.com/GeeTransit))
66
* Ensure train carts don't reverse on sharp curves. (By [GeeTransit](https://github.com/GeeTransit))
7+
* Updated to 1.20.2.
78
* Updated icon and contributors.
89
* Loader version is now enforced.
910
* Some other minor improvements.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.vini2003.linkart.utility;
2+
3+
import java.lang.invoke.*;
4+
5+
public class Lambdas {
6+
7+
//TODO maybe use ASM?
8+
public static <T> T handle(MethodHandles.Lookup lookup, Class<T> type, MethodHandle h) {
9+
CallSite site = supply(() -> LambdaMetafactory.metafactory(lookup, "invoke",
10+
MethodType.methodType(type), h.type(), h, h.type()));
11+
return supply(() -> (T) site.getTarget().invoke());
12+
}
13+
14+
public static <R, E extends Throwable> R supply(ThrowingSupplier<R, E> supplier) {
15+
try {
16+
return supplier.get();
17+
} catch (Throwable e) {
18+
return sneak(e);
19+
}
20+
}
21+
22+
public static <E extends Throwable, R> R sneak(Throwable exception) throws E {
23+
throw (E) exception;
24+
}
25+
26+
@FunctionalInterface
27+
public interface ThrowingSupplier<T, E extends Throwable> {
28+
T get() throws E;
29+
}
30+
}

src/main/java/com/github/vini2003/linkart/utility/LoadingCarts.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.vini2003.linkart.utility;
22

3+
import net.fabricmc.loader.api.FabricLoader;
34
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
45
import net.minecraft.nbt.NbtCompound;
56
import net.minecraft.nbt.NbtElement;
@@ -10,16 +11,39 @@
1011
import net.minecraft.util.math.BlockPos;
1112
import net.minecraft.util.math.ChunkPos;
1213
import net.minecraft.world.PersistentState;
14+
import net.minecraft.world.PersistentStateManager;
1315

16+
import java.lang.invoke.*;
1417
import java.util.HashSet;
1518
import java.util.Set;
19+
import java.util.function.Function;
20+
import java.util.function.Supplier;
1621

1722
public class LoadingCarts extends PersistentState {
1823

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+
1945
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());
2347
}
2448

2549
private final Set<BlockPos> chunksToReload = new HashSet<>();

0 commit comments

Comments
 (0)