Jelajahi Sumber

Command registry events and ReloadListenerRegistry

shedaniel 4 tahun lalu
induk
melakukan
abb57db971

+ 12 - 0
common/src/main/java/me/shedaniel/architectury/event/events/CommandRegistrationEvent.java

@@ -0,0 +1,12 @@
+package me.shedaniel.architectury.event.events;
+
+import com.mojang.brigadier.CommandDispatcher;
+import me.shedaniel.architectury.event.Event;
+import me.shedaniel.architectury.event.EventFactory;
+import net.minecraft.commands.CommandSourceStack;
+
+public interface CommandRegistrationEvent {
+    Event<CommandRegistrationEvent> EVENT = EventFactory.createLoop(CommandRegistrationEvent.class);
+    
+    void register(CommandDispatcher<CommandSourceStack> var1);
+}

+ 4 - 0
common/src/main/java/me/shedaniel/architectury/event/events/GuiEvent.java

@@ -19,10 +19,14 @@ package me.shedaniel.architectury.event.events;
 import com.mojang.blaze3d.vertex.PoseStack;
 import me.shedaniel.architectury.event.Event;
 import me.shedaniel.architectury.event.EventFactory;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
 
+@Environment(EnvType.CLIENT)
 public interface GuiEvent {
     Event<RenderHud> RENDER_HUD = EventFactory.createLoop(RenderHud.class);
     
+    @Environment(EnvType.CLIENT)
     interface RenderHud {
         void renderHud(PoseStack matrices, float tickDelta);
     }

+ 25 - 0
common/src/main/java/me/shedaniel/architectury/registry/ReloadListenerRegistry.java

@@ -0,0 +1,25 @@
+package me.shedaniel.architectury.registry;
+
+import me.shedaniel.architectury.ArchitecturyPopulator;
+import me.shedaniel.architectury.Populatable;
+import net.minecraft.server.packs.PackType;
+import net.minecraft.server.packs.resources.PreparableReloadListener;
+
+public final class ReloadListenerRegistry {
+    private ReloadListenerRegistry() {}
+    
+    @Populatable
+    private static final Impl IMPL = null;
+    
+    public static void registerReloadListener(PackType type, PreparableReloadListener listener) {
+        IMPL.registerReloadListener(type, listener);
+    }
+    
+    public interface Impl {
+        void registerReloadListener(PackType type, PreparableReloadListener listener);
+    }
+    
+    static {
+        ArchitecturyPopulator.populate(ReloadListenerRegistry.class);
+    }
+}

+ 4 - 4
fabric/src/main/java/me/shedaniel/architectury/event/fabric/EventFactoryImpl.java

@@ -17,14 +17,12 @@
 package me.shedaniel.architectury.event.fabric;
 
 import me.shedaniel.architectury.event.EventFactory;
-import me.shedaniel.architectury.event.events.GuiEvent;
-import me.shedaniel.architectury.event.events.LifecycleEvent;
-import me.shedaniel.architectury.event.events.TickEvent;
-import me.shedaniel.architectury.event.events.TooltipEvent;
+import me.shedaniel.architectury.event.events.*;
 import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
 import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
 import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
 import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
+import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
 import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
 import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
 
@@ -54,6 +52,8 @@ public class EventFactoryImpl implements EventFactory.Impl {
         ServerTickEvents.END_SERVER_TICK.register(TickEvent.SERVER_POST.invoker()::tick);
         ServerTickEvents.START_WORLD_TICK.register(TickEvent.SERVER_WORLD_PRE.invoker()::tick);
         ServerTickEvents.END_WORLD_TICK.register(TickEvent.SERVER_WORLD_POST.invoker()::tick);
+        
+        CommandRegistrationCallback.EVENT.register((commandDispatcher, b) -> CommandRegistrationEvent.EVENT.invoker().register(commandDispatcher));
     }
     
     @Override

+ 43 - 0
fabric/src/main/java/me/shedaniel/architectury/registry/fabric/ReloadListenerRegistryImpl.java

@@ -0,0 +1,43 @@
+package me.shedaniel.architectury.registry.fabric;
+
+import com.google.common.primitives.Longs;
+import me.shedaniel.architectury.registry.ReloadListenerRegistry;
+import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
+import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.server.packs.PackType;
+import net.minecraft.server.packs.resources.PreparableReloadListener;
+import net.minecraft.server.packs.resources.ResourceManager;
+import net.minecraft.util.profiling.ProfilerFiller;
+import org.apache.commons.lang3.StringUtils;
+
+import java.security.SecureRandom;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executor;
+
+public class ReloadListenerRegistryImpl implements ReloadListenerRegistry.Impl {
+    private static final SecureRandom RANDOM = new SecureRandom();
+    
+    @Override
+    public void registerReloadListener(PackType type, PreparableReloadListener listener) {
+        byte[] bytes = new byte[8];
+        RANDOM.nextBytes(bytes);
+        ResourceLocation id = new ResourceLocation("architectury:reload_" + StringUtils.leftPad(Math.abs(Longs.fromByteArray(bytes)) + "", 19));
+        ResourceManagerHelper.get(type).registerReloadListener(new IdentifiableResourceReloadListener() {
+            @Override
+            public ResourceLocation getFabricId() {
+                return id;
+            }
+    
+            @Override
+            public String getName() {
+                return listener.getName();
+            }
+    
+            @Override
+            public CompletableFuture<Void> reload(PreparationBarrier preparationBarrier, ResourceManager resourceManager, ProfilerFiller profilerFiller, ProfilerFiller profilerFiller2, Executor executor, Executor executor2) {
+                return listener.reload(preparationBarrier, resourceManager, profilerFiller, profilerFiller2, executor, executor2);
+            }
+        });
+    }
+}

+ 7 - 4
forge/src/main/java/me/shedaniel/architectury/event/forge/EventFactoryImpl.java

@@ -17,16 +17,14 @@
 package me.shedaniel.architectury.event.forge;
 
 import me.shedaniel.architectury.event.EventFactory;
-import me.shedaniel.architectury.event.events.GuiEvent;
-import me.shedaniel.architectury.event.events.LifecycleEvent;
-import me.shedaniel.architectury.event.events.TickEvent;
-import me.shedaniel.architectury.event.events.TooltipEvent;
+import me.shedaniel.architectury.event.events.*;
 import net.minecraft.client.Minecraft;
 import net.minecraft.world.server.ServerWorld;
 import net.minecraftforge.api.distmarker.Dist;
 import net.minecraftforge.api.distmarker.OnlyIn;
 import net.minecraftforge.client.event.RenderGameOverlayEvent;
 import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.RegisterCommandsEvent;
 import net.minecraftforge.event.TickEvent.ClientTickEvent;
 import net.minecraftforge.event.TickEvent.Phase;
 import net.minecraftforge.event.TickEvent.ServerTickEvent;
@@ -115,6 +113,11 @@ public class EventFactoryImpl implements EventFactory.Impl {
         public static void event(FMLServerStoppedEvent event) {
             LifecycleEvent.SERVER_STOPPED.invoker().stateChanged(event.getServer());
         }
+        
+        @SubscribeEvent
+        public static void event(RegisterCommandsEvent event) {
+            CommandRegistrationEvent.EVENT.invoker().register(event.getDispatcher());
+        }
     }
     
     @OnlyIn(Dist.DEDICATED_SERVER)

+ 40 - 0
forge/src/main/java/me/shedaniel/architectury/registry/forge/ReloadListenerRegistryImpl.java

@@ -0,0 +1,40 @@
+package me.shedaniel.architectury.registry.forge;
+
+import com.google.common.collect.Lists;
+import me.shedaniel.architectury.registry.ReloadListenerRegistry;
+import net.minecraft.client.Minecraft;
+import net.minecraft.resources.IFutureReloadListener;
+import net.minecraft.resources.IReloadableResourceManager;
+import net.minecraft.resources.ResourcePackType;
+import net.minecraftforge.api.distmarker.Dist;
+import net.minecraftforge.api.distmarker.OnlyIn;
+import net.minecraftforge.common.MinecraftForge;
+import net.minecraftforge.event.AddReloadListenerEvent;
+
+import java.util.List;
+
+public class ReloadListenerRegistryImpl implements ReloadListenerRegistry.Impl {
+    private List<IFutureReloadListener> serverDataReloadListeners = Lists.newArrayList();
+    
+    public ReloadListenerRegistryImpl() {
+        MinecraftForge.EVENT_BUS.<AddReloadListenerEvent>addListener(event -> {
+            for (IFutureReloadListener listener : serverDataReloadListeners) {
+                event.addListener(listener);
+            }
+        });
+    }
+    
+    @Override
+    public void registerReloadListener(ResourcePackType type, IFutureReloadListener listener) {
+        if (type == ResourcePackType.SERVER_DATA) {
+            serverDataReloadListeners.add(listener);
+        } else if (type == ResourcePackType.CLIENT_RESOURCES) {
+            reloadClientReloadListener(listener);
+        }
+    }
+    
+    @OnlyIn(Dist.CLIENT)
+    private void reloadClientReloadListener(IFutureReloadListener listener) {
+        ((IReloadableResourceManager) Minecraft.getInstance().getResourceManager()).registerReloadListener(listener);
+    }
+}