mirror of
https://github.com/FabricMC/fabric.git
synced 2024-11-24 08:38:17 -05:00
Backport Datapack reload events
This commit is contained in:
parent
564b358c7d
commit
dbdd067feb
3 changed files with 21 additions and 21 deletions
|
@ -16,7 +16,7 @@
|
|||
|
||||
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.PlayerManager;
|
||||
|
||||
|
@ -113,7 +113,7 @@ public final class ServerLifecycleEvents {
|
|||
}
|
||||
|
||||
public interface StartDataPackReload {
|
||||
void startDataPackReload(MinecraftServer server, ServerResourceManager serverResourceManager);
|
||||
void startDataPackReload(MinecraftServer server, ReloadableResourceManager serverResourceManager);
|
||||
}
|
||||
|
||||
public interface EndDataPackReload {
|
||||
|
@ -126,6 +126,6 @@ public final class ServerLifecycleEvents {
|
|||
* @param serverResourceManager the server resource manager
|
||||
* @param success if the reload was successful
|
||||
*/
|
||||
void endDataPackReload(MinecraftServer server, ServerResourceManager serverResourceManager, boolean success);
|
||||
void endDataPackReload(MinecraftServer server, ReloadableResourceManager serverResourceManager, boolean success);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@
|
|||
|
||||
package net.fabricmc.fabric.mixin.event.lifecycle;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
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.Redirect;
|
||||
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 net.minecraft.block.entity.BlockEntity;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.resource.ServerResourceManager;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
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.ServerLifecycleEvents;
|
||||
|
@ -46,7 +47,7 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
|
|||
@Mixin(MinecraftServer.class)
|
||||
public abstract class MinecraftServerMixin {
|
||||
@Shadow
|
||||
private ServerResourceManager serverResourceManager;
|
||||
private ReloadableResourceManager dataManager;
|
||||
|
||||
@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;setupServer()Z"), method = "run")
|
||||
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)
|
||||
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) {
|
||||
ServerBlockEntityEvents.BLOCK_ENTITY_UNLOAD.invoker().onUnload(blockEntity, serverWorld);
|
||||
}
|
||||
|
@ -100,17 +99,18 @@ public abstract class MinecraftServerMixin {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Inject(method = "reloadResources", at = @At("HEAD"))
|
||||
private void startResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) {
|
||||
ServerLifecycleEvents.START_DATA_PACK_RELOAD.invoker().startDataPackReload((MinecraftServer) (Object) this, this.serverResourceManager);
|
||||
@Inject(method = "reloadDataPacks", at = @At("HEAD"))
|
||||
private void startResourceReload(LevelProperties levelProperties, CallbackInfo cir) {
|
||||
ServerLifecycleEvents.START_DATA_PACK_RELOAD.invoker().startDataPackReload((MinecraftServer) (Object) this, this.dataManager);
|
||||
}
|
||||
|
||||
@Inject(method = "reloadResources", at = @At("TAIL"))
|
||||
private void endResourceReload(Collection<String> collection, CallbackInfoReturnable<CompletableFuture<Void>> cir) {
|
||||
cir.getReturnValue().handleAsync((value, throwable) -> {
|
||||
// Hook into fail
|
||||
ServerLifecycleEvents.END_DATA_PACK_RELOAD.invoker().endDataPackReload((MinecraftServer) (Object) this, this.serverResourceManager, throwable == null);
|
||||
return value;
|
||||
}, (MinecraftServer) (Object) this);
|
||||
@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 CompletableFuture<Unit> endResourceReload(ReloadableResourceManager reloadableResourceManager, Executor prepareExecutor, Executor applyExecutor, List<ResourcePack> packs, CompletableFuture<Unit> initialStage) throws Exception {
|
||||
return reloadableResourceManager.beginReload(prepareExecutor, applyExecutor, packs, initialStage)
|
||||
.handleAsync((value, throwable) -> {
|
||||
// Hook into fail
|
||||
ServerLifecycleEvents.END_DATA_PACK_RELOAD.invoker().endDataPackReload((MinecraftServer) (Object) this, this.dataManager, throwable == null);
|
||||
return value;
|
||||
}, (MinecraftServer) (Object) this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ public class ServerLifecycleTests implements ModInitializer {
|
|||
});
|
||||
|
||||
ServerWorldEvents.LOAD.register((server, world) -> {
|
||||
LOGGER.info("Loaded world " + world.getRegistryKey().getValue().toString());
|
||||
LOGGER.info("Loaded world " + world.dimension.getType().toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue