[Java] 纯文本查看 复制代码
public class ExampleState extends PersistentState {
private static final Logger LOGGER = LogManager.getLogger();
private ExampleObject object;
public ExampleState() {
super("example");
}
public void setObject(ExampleObject object) {
this.object = object;
}
public ExampleObject getObject() {
return object;
}
public void fromTag(CompoundTag tag) {
object.fromTag(tag.getCompound("ExampleData"));
}
public CompoundTag toTag(CompoundTag tag) {
if(this.handler == null) {
LOGGER.warn("无法保存未创建的存储对象。");
} else {
tag.put("ExampleData", object.toTag());
}
return tag;
}
}
[Java] 纯文本查看 复制代码
//存储对象类
private Runnable[] updateListeners = new Runnable[0];
public void addUpdateListener(Runnable listener) {
this.updateListeners = Arrays.copyOf(this.updateListeners, this.updateListeners.length + );
this.updateListeners[this.updateListeners.length - 1] = listener;
}
protected void runUpdateListeners() {
Runnable[] var1 = this.updateListeners;
for(Runnable runnable : var1) {
runnable.run();
}
}
public class ExampleSynchronizer implements Runnable {
private final PersistentState compound;
public ExampleSynchronizer(PersistentState compound) {
this.compound = compound;
}
public void run() {
this.compound.markDirty();
}
}
[Java] 纯文本查看 复制代码
@Mixin(MinecraftServer.class)
public abstract class MinecraftServerMixin {
private ExampleObject object;
@Inject(method = "(Ljava/io/File;Ljava/net/Proxy;Lcom/mojang/datafixers/DataFixer;Lnet/minecraft/server/command/CommandManager;Lcom/mojang/authlib/yggdrasil/YggdrasilAuthenticationService;Lcom/mojang/authlib/minecraft/MinecraftSessionService;Lcom/mojang/authlib/GameProfileRepository;Lnet/minecraft/util/UserCache;Lnet/minecraft/server/WorldGenerationProgressListenerFactory;Ljava/lang/String;)V", at = @At(value = "RETURN"))
private void initExampleObject(File gameDir, Proxy proxy, DataFixer dataFixer, CommandManager commandManager, YggdrasilAuthenticationService authService, MinecraftSessionService sessionService, GameProfileRepository gameProfileRepository, UserCache userCache, WorldGenerationProgressListenerFactory worldGenerationProgressListenerFactory, String levelName, CallbackInfo ci) {
this.object = new ExampleObject();
}
private void initExampleObject(PersistentStateManager persistentStateManager) {
ExampleState exampleState = persistentStateManager.getOrCreate(ExampleState::new, "example");
exampleState.setObject(object);
object.addUpdateListener(new ExampleSynchronizer(exampleState));
}
@Inject(method = "createWorlds(Lnet/minecraft/world/WorldSaveHandler;Lnet/minecraft/world/level/LevelProperties;Lnet/minecraft/world/level/LevelInfo;Lnet/minecraft/server/WorldGenerationProgressListener;)V", at = @At(value = "TAIL"), locals = LocalCapture.CAPTURE\_FAILHARD)
private void createTemperatureState(WorldSaveHandler worldSaveHandler, LevelProperties properties, LevelInfo levelInfo, WorldGenerationProgressListener worldGenerationProgressListener, CallbackInfo ci, ServerWorld serverWorld, ersistentStateManager persistentStateManager) {
this.initExampleObject(persistentStateManager);
}
}