Backport Datapack reload events

This commit is contained in:
modmuss50 2020-07-17 15:50:57 +01:00
parent 564b358c7d
commit dbdd067feb
3 changed files with 21 additions and 21 deletions

View file

@ -16,7 +16,7 @@
package net.fabricmc.fabric.api.event.lifecycle.v1; package net.fabricmc.fabric.api.event.lifecycle.v1;
import net.minecraft.resource.ServerResourceManager; import net.minecraft.resource.ReloadableResourceManager;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager; import net.minecraft.server.PlayerManager;
@ -113,7 +113,7 @@ public final class ServerLifecycleEvents {
} }
public interface StartDataPackReload { public interface StartDataPackReload {
void startDataPackReload(MinecraftServer server, ServerResourceManager serverResourceManager); void startDataPackReload(MinecraftServer server, ReloadableResourceManager serverResourceManager);
} }
public interface EndDataPackReload { public interface EndDataPackReload {
@ -126,6 +126,6 @@ public final class ServerLifecycleEvents {
* @param serverResourceManager the server resource manager * @param serverResourceManager the server resource manager
* @param success if the reload was successful * @param success if the reload was successful
*/ */
void endDataPackReload(MinecraftServer server, ServerResourceManager serverResourceManager, boolean success); void endDataPackReload(MinecraftServer server, ReloadableResourceManager serverResourceManager, boolean success);
} }
} }

View file

@ -16,11 +16,11 @@
package net.fabricmc.fabric.mixin.event.lifecycle; package net.fabricmc.fabric.mixin.event.lifecycle;
import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
@ -29,14 +29,15 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture; import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.resource.ServerResourceManager;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.resource.ReloadableResourceManager;
import net.minecraft.resource.ResourcePack;
import net.minecraft.util.Unit;
import net.minecraft.world.level.LevelProperties;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
@ -46,7 +47,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
@Mixin(MinecraftServer.class) @Mixin(MinecraftServer.class)
public abstract class MinecraftServerMixin { public abstract class MinecraftServerMixin {
@Shadow @Shadow
private ServerResourceManager serverResourceManager; private ReloadableResourceManager dataManager;
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;setupServer()Z"), method = "run") @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;setupServer()Z"), method = "run")
private void beforeSetupServer(CallbackInfo info) { private void beforeSetupServer(CallbackInfo info) {
@ -83,8 +84,6 @@ public abstract class MinecraftServerMixin {
*/ */
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerWorld;close()V"), method = "shutdown", locals = LocalCapture.CAPTURE_FAILEXCEPTION) @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerWorld;close()V"), method = "shutdown", locals = LocalCapture.CAPTURE_FAILEXCEPTION)
private void closeWorld(CallbackInfo ci, Iterator<ServerWorld> worlds, ServerWorld serverWorld) { private void closeWorld(CallbackInfo ci, Iterator<ServerWorld> worlds, ServerWorld serverWorld) {
final List<Entity> entities = serverWorld.getEntities(null, entity -> true); // Get every single entity in the world
for (BlockEntity blockEntity : serverWorld.blockEntities) { for (BlockEntity blockEntity : serverWorld.blockEntities) {
ServerBlockEntityEvents.BLOCK_ENTITY_UNLOAD.invoker().onUnload(blockEntity, serverWorld); ServerBlockEntityEvents.BLOCK_ENTITY_UNLOAD.invoker().onUnload(blockEntity, serverWorld);
} }
@ -100,17 +99,18 @@ public abstract class MinecraftServerMixin {
return result; return result;
} }
@Inject(method = "reloadResources", at = @At("HEAD")) @Inject(method = "reloadDataPacks", at = @At("HEAD"))
private void startResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) { private void startResourceReload(LevelProperties levelProperties, CallbackInfo cir) {
ServerLifecycleEvents.START_DATA_PACK_RELOAD.invoker().startDataPackReload((MinecraftServer) (Object) this, this.serverResourceManager); ServerLifecycleEvents.START_DATA_PACK_RELOAD.invoker().startDataPackReload((MinecraftServer) (Object) this, this.dataManager);
} }
@Inject(method = "reloadResources", at = @At("TAIL")) @Redirect(method = "reloadDataPacks", at = @At(value = "INVOKE", target = "Lnet/minecraft/resource/ReloadableResourceManager;beginReload(Ljava/util/concurrent/Executor;Ljava/util/concurrent/Executor;Ljava/util/List;Ljava/util/concurrent/CompletableFuture;)Ljava/util/concurrent/CompletableFuture;"))
private void endResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) { private CompletableFuture<Unit> endResourceReload(ReloadableResourceManager reloadableResourceManager, Executor prepareExecutor, Executor applyExecutor, List<ResourcePack> packs, CompletableFuture<Unit> initialStage) throws Exception {
cir.getReturnValue().handleAsync((value, throwable) -> { return reloadableResourceManager.beginReload(prepareExecutor, applyExecutor, packs, initialStage)
// Hook into fail .handleAsync((value, throwable) -> {
ServerLifecycleEvents.END_DATA_PACK_RELOAD.invoker().endDataPackReload((MinecraftServer) (Object) this, this.serverResourceManager, throwable == null); // Hook into fail
return value; ServerLifecycleEvents.END_DATA_PACK_RELOAD.invoker().endDataPackReload((MinecraftServer) (Object) this, this.dataManager, throwable == null);
}, (MinecraftServer) (Object) this); return value;
}, (MinecraftServer) (Object) this);
} }
} }

View file

@ -44,7 +44,7 @@ public class ServerLifecycleTests implements ModInitializer {
}); });
ServerWorldEvents.LOAD.register((server, world) -> { ServerWorldEvents.LOAD.register((server, world) -> {
LOGGER.info("Loaded world " + world.getRegistryKey().getValue().toString()); LOGGER.info("Loaded world " + world.dimension.getType().toString());
}); });
} }
} }