返回提交历史
Modified
src/main/java/com/xfestudio/mydimension/MyDimension.java
+5
-1
Added
src/main/java/com/xfestudio/mydimension/compat/create/CreateTrainCompat.java
+230
-0
Modified
src/main/java/com/xfestudio/mydimension/world/portal/MindPortalManager.java
+79
-0
Modified
src/main/resources/META-INF/mods.toml
+7
-0
XFEstudio/MyDimension
为 MyDimension 增加 Create 火车传送门兼容支持
新增 CreateTrainCompat 类,兼容 Create 0.5.1/6.x 传送门轨道 API,避免静态依赖,未安装 Create 时可正常加载。在 commonSetup 阶段注册兼容逻辑,MindPortalManager 新增 findTrainExit 方法并在激活传送门时通知 Create。mods.toml 增加可选依赖声明,异常有日志提示,保证兼容性和健壮性。
f4017bf
代码差异
4 个文件
+321
-1
@@ -1,6 +1,7 @@
1
1
package com.xfestudio.mydimension;
2
2
3
3
import com.mojang.logging.LogUtils;
4
import com.xfestudio.mydimension.compat.create.CreateTrainCompat;
4
5
import com.xfestudio.mydimension.network.ModNetwork;
5
6
import com.xfestudio.mydimension.registry.ModChunkGenerators;
6
7
import com.xfestudio.mydimension.registry.ModBlockEntities;
@@ -33,6 +34,9 @@ public class MyDimension {
33
34
}
34
35
35
36
private void commonSetup(FMLCommonSetupEvent event) {
36
event.enqueueWork(ModNetwork::register);
37
event.enqueueWork(() -> {
38
ModNetwork.register();
39
CreateTrainCompat.register();
40
});
37
41
}
38
42
}
@@ -0,0 +1,230 @@
1
package com.xfestudio.mydimension.compat.create;
2
3
import com.xfestudio.mydimension.MyDimension;
4
import com.xfestudio.mydimension.registry.ModBlocks;
5
import com.xfestudio.mydimension.world.portal.MindPortalManager;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.resources.ResourceLocation;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.tags.BlockTags;
11
import net.minecraft.tags.TagKey;
12
import net.minecraft.world.level.block.Block;
13
import net.minecraft.world.level.block.state.BlockState;
14
import net.minecraftforge.fml.ModList;
15
16
import java.lang.reflect.Constructor;
17
import java.lang.reflect.InvocationHandler;
18
import java.lang.reflect.Method;
19
import java.lang.reflect.Proxy;
20
import java.util.concurrent.atomic.AtomicBoolean;
21
22
/**
23
* Optional Create integration kept free of static references to Create classes.
24
* This lets the base mod load normally when Create is not installed and also bridges
25
* the portal-track API used by both the Create 0.5.1 and 6.x lines for Minecraft 1.20.1.
26
*/
27
public final class CreateTrainCompat {
28
private static final String CREATE_MOD_ID = "create";
29
private static final TagKey<Block> CREATE_TRACKS = BlockTags.create(
30
new ResourceLocation(CREATE_MOD_ID, "tracks"));
31
private static final AtomicBoolean PROVIDER_FAILURE_LOGGED = new AtomicBoolean();
32
33
private static boolean registered;
34
35
private CreateTrainCompat() {
36
}
37
38
public static void register() {
39
if (registered || !ModList.get().isLoaded(CREATE_MOD_ID)) {
40
return;
41
}
42
43
try {
44
if (registerModernApi()) {
45
registered = true;
46
MyDimension.LOGGER.info("Enabled Create 6.x train portal compatibility");
47
return;
48
}
49
if (registerLegacyApi()) {
50
registered = true;
51
MyDimension.LOGGER.info("Enabled Create 0.5.1 train portal compatibility");
52
return;
53
}
54
MyDimension.LOGGER.warn("Create is installed, but its portal-track API is not supported");
55
} catch (ReflectiveOperationException | LinkageError | RuntimeException exception) {
56
MyDimension.LOGGER.error("Failed to enable Create train portal compatibility", exception);
57
}
58
}
59
60
/**
61
* Create normally probes for portals on the first tick after a track is placed. Scheduling
62
* neighboring tracks here also supports activating a portal after the tracks are already laid.
63
*/
64
public static void onPortalActivated(ServerLevel level, MindPortalManager.PortalEndpoint endpoint) {
65
if (!registered) {
66
return;
67
}
68
69
Direction[] normalDirections = endpoint.axis() == Direction.Axis.X
70
? new Direction[] {Direction.NORTH, Direction.SOUTH}
71
: new Direction[] {Direction.WEST, Direction.EAST};
72
for (int horizontal = 0; horizontal < MindPortalManager.INNER_WIDTH; horizontal++) {
73
for (int vertical = 0; vertical < MindPortalManager.INNER_HEIGHT; vertical++) {
74
BlockPos portalPos = endpoint.axis() == Direction.Axis.X
75
? endpoint.core().offset(horizontal, vertical, 0)
76
: endpoint.core().offset(0, vertical, horizontal);
77
for (Direction direction : normalDirections) {
78
BlockPos trackPos = portalPos.relative(direction);
79
BlockState trackState = level.getBlockState(trackPos);
80
if (trackState.is(CREATE_TRACKS)
81
&& !level.getBlockTicks().hasScheduledTick(trackPos, trackState.getBlock())) {
82
level.scheduleTick(trackPos, trackState.getBlock(), 1);
83
}
84
}
85
}
86
}
87
}
88
89
private static boolean registerModernApi() throws ReflectiveOperationException {
90
Class<?> providerClass = tryLoad("com.simibubi.create.api.contraption.train.PortalTrackProvider");
91
if (providerClass == null) {
92
return false;
93
}
94
95
Class<?> blockFaceClass = Class.forName("net.createmod.catnip.math.BlockFace");
96
Method findExit = providerClass.getMethod("findExit", ServerLevel.class, blockFaceClass);
97
Method getConnectedPos = blockFaceClass.getMethod("getConnectedPos");
98
Method getFace = blockFaceClass.getMethod("getFace");
99
Constructor<?> blockFaceConstructor = blockFaceClass.getConstructor(BlockPos.class, Direction.class);
100
Constructor<?> exitConstructor = findExit.getReturnType()
101
.getConstructor(ServerLevel.class, blockFaceClass);
102
103
InvocationHandler handler = (proxy, method, arguments) -> {
104
Object objectMethodResult = handleObjectMethod(proxy, method, arguments);
105
if (objectMethodResult != Unhandled.INSTANCE) {
106
return objectMethodResult;
107
}
108
if (!method.getName().equals("findExit") || arguments == null || arguments.length != 2) {
109
throw new UnsupportedOperationException("Unsupported Create portal provider method: " + method);
110
}
111
return resolveModernExit(arguments, getConnectedPos, getFace, blockFaceConstructor, exitConstructor);
112
};
113
Object provider = Proxy.newProxyInstance(
114
providerClass.getClassLoader(), new Class<?>[] {providerClass}, handler);
115
116
Object registry = providerClass.getField("REGISTRY").get(null);
117
Class<?> registryClass = Class.forName("com.simibubi.create.api.registry.SimpleRegistry");
118
registryClass.getMethod("register", Object.class, Object.class)
119
.invoke(registry, ModBlocks.MIND_PORTAL.get(), provider);
120
return true;
121
}
122
123
private static boolean registerLegacyApi() throws ReflectiveOperationException {
124
Class<?> allPortalTracksClass = tryLoad("com.simibubi.create.content.trains.track.AllPortalTracks");
125
Class<?> providerClass = tryLoad(
126
"com.simibubi.create.content.trains.track.AllPortalTracks$PortalTrackProvider");
127
if (allPortalTracksClass == null || providerClass == null) {
128
return false;
129
}
130
131
Class<?> pairClass = Class.forName("com.simibubi.create.foundation.utility.Pair");
132
Class<?> blockFaceClass = Class.forName("com.simibubi.create.foundation.utility.BlockFace");
133
Method getFirst = pairClass.getMethod("getFirst");
134
Method getSecond = pairClass.getMethod("getSecond");
135
Method pairFactory = pairClass.getMethod("of", Object.class, Object.class);
136
Method getConnectedPos = blockFaceClass.getMethod("getConnectedPos");
137
Method getFace = blockFaceClass.getMethod("getFace");
138
Constructor<?> blockFaceConstructor = blockFaceClass.getConstructor(BlockPos.class, Direction.class);
139
140
InvocationHandler handler = (proxy, method, arguments) -> {
141
Object objectMethodResult = handleObjectMethod(proxy, method, arguments);
142
if (objectMethodResult != Unhandled.INSTANCE) {
143
return objectMethodResult;
144
}
145
if (!method.getName().equals("apply") || arguments == null || arguments.length != 1) {
146
throw new UnsupportedOperationException("Unsupported Create portal provider method: " + method);
147
}
148
return resolveLegacyExit(arguments[0], getFirst, getSecond, getConnectedPos, getFace,
149
blockFaceConstructor, pairFactory);
150
};
151
Object provider = Proxy.newProxyInstance(
152
providerClass.getClassLoader(), new Class<?>[] {providerClass}, handler);
153
154
allPortalTracksClass.getMethod("registerIntegration", Block.class, providerClass)
155
.invoke(null, ModBlocks.MIND_PORTAL.get(), provider);
156
return true;
157
}
158
159
private static Object resolveModernExit(Object[] arguments, Method getConnectedPos, Method getFace,
160
Constructor<?> blockFaceConstructor, Constructor<?> exitConstructor) {
161
try {
162
ServerLevel level = (ServerLevel) arguments[0];
163
MindPortalManager.TrainPortalExit exit = resolve(level, arguments[1], getConnectedPos, getFace);
164
if (exit == null) {
165
return null;
166
}
167
Object outboundFace = blockFaceConstructor.newInstance(exit.trackPos(), exit.trackFace());
168
return exitConstructor.newInstance(exit.level(), outboundFace);
169
} catch (ReflectiveOperationException | ClassCastException exception) {
170
logProviderFailure(exception);
171
return null;
172
}
173
}
174
175
private static Object resolveLegacyExit(Object inboundPair, Method getFirst, Method getSecond,
176
Method getConnectedPos, Method getFace,
177
Constructor<?> blockFaceConstructor, Method pairFactory) {
178
try {
179
ServerLevel level = (ServerLevel) getFirst.invoke(inboundPair);
180
Object inboundFace = getSecond.invoke(inboundPair);
181
MindPortalManager.TrainPortalExit exit = resolve(level, inboundFace, getConnectedPos, getFace);
182
if (exit == null) {
183
return null;
184
}
185
Object outboundFace = blockFaceConstructor.newInstance(exit.trackPos(), exit.trackFace());
186
return pairFactory.invoke(null, exit.level(), outboundFace);
187
} catch (ReflectiveOperationException | ClassCastException exception) {
188
logProviderFailure(exception);
189
return null;
190
}
191
}
192
193
private static MindPortalManager.TrainPortalExit resolve(ServerLevel level, Object inboundFace,
194
Method getConnectedPos, Method getFace)
195
throws ReflectiveOperationException {
196
BlockPos portalPos = (BlockPos) getConnectedPos.invoke(inboundFace);
197
Direction inboundDirection = (Direction) getFace.invoke(inboundFace);
198
return MindPortalManager.findTrainExit(level, portalPos, inboundDirection);
199
}
200
201
private static Object handleObjectMethod(Object proxy, Method method, Object[] arguments) {
202
if (method.getDeclaringClass() != Object.class) {
203
return Unhandled.INSTANCE;
204
}
205
return switch (method.getName()) {
206
case "toString" -> "MyDimension Create train portal provider";
207
case "hashCode" -> System.identityHashCode(proxy);
208
case "equals" -> arguments != null && arguments.length == 1 && proxy == arguments[0];
209
default -> Unhandled.INSTANCE;
210
};
211
}
212
213
private static Class<?> tryLoad(String className) throws LinkageError {
214
try {
215
return Class.forName(className, true, CreateTrainCompat.class.getClassLoader());
216
} catch (ClassNotFoundException ignored) {
217
return null;
218
}
219
}
220
221
private static void logProviderFailure(Exception exception) {
222
if (PROVIDER_FAILURE_LOGGED.compareAndSet(false, true)) {
223
MyDimension.LOGGER.error("Create failed to resolve a MyDimension train portal exit", exception);
224
}
225
}
226
227
private enum Unhandled {
228
INSTANCE
229
}
230
}
@@ -1,6 +1,7 @@
1
1
package com.xfestudio.mydimension.world.portal;
2
2
3
3
import com.xfestudio.mydimension.MyDimension;
4
import com.xfestudio.mydimension.compat.create.CreateTrainCompat;
4
5
import com.xfestudio.mydimension.item.RiftItem;
5
6
import com.xfestudio.mydimension.registry.ModBlocks;
6
7
import com.xfestudio.mydimension.world.MindTeamAccess;
@@ -31,6 +32,7 @@ import net.minecraft.world.level.block.state.BlockState;
31
32
import net.minecraft.world.level.levelgen.Heightmap;
32
33
import net.minecraft.world.level.saveddata.SavedData;
33
34
import net.minecraft.world.phys.Vec3;
35
import org.jetbrains.annotations.Nullable;
34
36
35
37
import java.util.HashMap;
36
38
import java.util.Map;
@@ -197,6 +199,79 @@ public final class MindPortalManager {
197
199
SoundSource.PLAYERS, 0.8F, 1.0F);
198
200
}
199
201
202
/**
203
* Resolves the outbound track face for integrations which can move an entire vehicle through a portal.
204
* The returned face points from the outbound track back towards its portal block.
205
*/
206
@Nullable
207
public static TrainPortalExit findTrainExit(ServerLevel level, BlockPos portalPos, Direction inboundDirection) {
208
if (inboundDirection.getAxis() == Direction.Axis.Y) {
209
return null;
210
}
211
212
BlockState portalState = level.getBlockState(portalPos);
213
if (!portalState.is(ModBlocks.MIND_PORTAL.get())) {
214
return null;
215
}
216
217
Direction.Axis sourceAxis = portalState.getValue(MindPortalBlock.AXIS);
218
if (inboundDirection.getAxis() == sourceAxis) {
219
return null;
220
}
221
222
MindPortalBlockEntity sourceCore = findCoreEntity(level, portalPos, sourceAxis);
223
if (sourceCore == null || sourceCore.linkId() == null) {
224
return null;
225
}
226
227
PortalLink link = data(level.getServer()).links.get(sourceCore.linkId());
228
if (link == null) {
229
return null;
230
}
231
232
PortalEndpoint current = sourceCore.sourceSide() ? link.source() : link.destination();
233
if (!current.dimension().equals(level.dimension())
234
|| !current.core().equals(sourceCore.getBlockPos())
235
|| current.axis() != sourceAxis) {
236
return null;
237
}
238
239
int horizontal = sourceAxis == Direction.Axis.X
240
? portalPos.getX() - current.core().getX()
241
: portalPos.getZ() - current.core().getZ();
242
int vertical = portalPos.getY() - current.core().getY();
243
if (horizontal < 0 || horizontal >= INNER_WIDTH || vertical < 0 || vertical >= INNER_HEIGHT) {
244
return null;
245
}
246
247
PortalEndpoint destination = sourceCore.sourceSide() ? link.destination() : link.source();
248
ServerLevel destinationLevel = level.getServer().getLevel(destination.dimension());
249
if (destinationLevel == null) {
250
return null;
251
}
252
253
BlockPos destinationPortalPos = offset(destination.core(), destination.axis(), horizontal, vertical);
254
BlockState destinationState = destinationLevel.getBlockState(destinationPortalPos);
255
if (!destinationState.is(ModBlocks.MIND_PORTAL.get())
256
|| destinationState.getValue(MindPortalBlock.AXIS) != destination.axis()) {
257
return null;
258
}
259
260
MindPortalBlockEntity destinationCore = findCoreEntity(
261
destinationLevel, destinationPortalPos, destination.axis());
262
if (destinationCore == null || !sourceCore.linkId().equals(destinationCore.linkId())) {
263
return null;
264
}
265
266
Direction outboundDirection = inboundDirection;
267
if (outboundDirection.getAxis() == destination.axis()) {
268
outboundDirection = outboundDirection.getClockWise();
269
}
270
271
BlockPos outboundTrackPos = destinationPortalPos.relative(outboundDirection);
272
return new TrainPortalExit(destinationLevel, outboundTrackPos, outboundDirection.getOpposite());
273
}
274
200
275
private static PortalFrame findFrame(ServerLevel level, BlockPos clickedPos) {
201
276
for (Direction.Axis axis : new Direction.Axis[] {Direction.Axis.X, Direction.Axis.Z}) {
202
277
for (int horizontal = -1; horizontal <= INNER_WIDTH; horizontal++) {
@@ -330,6 +405,7 @@ public final class MindPortalManager {
330
405
}
331
406
}
332
407
}
408
CreateTrainCompat.onPortalActivated(level, endpoint);
333
409
}
334
410
335
411
private static void destroyLink(MinecraftServer server, UUID linkId) {
@@ -425,6 +501,9 @@ public final class MindPortalManager {
425
501
public record PortalEndpoint(ResourceKey<Level> dimension, BlockPos core, Direction.Axis axis) {
426
502
}
427
503
504
public record TrainPortalExit(ServerLevel level, BlockPos trackPos, Direction trackFace) {
505
}
506
428
507
private record PortalLink(UUID id, UUID owner, PortalEndpoint source, PortalEndpoint destination,
429
508
ResourceKey<Level> baseDimension, ResourceKey<Level> targetDimension,
430
509
UUID targetOwner) {
@@ -23,3 +23,10 @@ mandatory = true
23
23
versionRange = "${minecraft_version_range}"
24
24
ordering = "NONE"
25
25
side = "BOTH"
26
27
[[dependencies.${mod_id}]]
28
modId = "create"
29
mandatory = false
30
versionRange = "[0.5.1,)"
31
ordering = "AFTER"
32
side = "BOTH"