瀏覽代碼

Add ClientLifecycleEvent.CLIENT_SETUP and allow deferring in ColorHandlers

shedaniel 4 年之前
父節點
當前提交
465aeae5bd

+ 30 - 0
common/src/main/java/me/shedaniel/architectury/event/EventFactory.java

@@ -58,6 +58,12 @@ public final class EventFactory {
         return new EventImpl<>(function);
     }
     
+    @SafeVarargs
+    public static <T> Event<T> createLoop(T... typeGetter) {
+        if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!");
+        return createLoop((Class<T>) typeGetter.getClass().getComponentType());
+    }
+    
     @SuppressWarnings("UnstableApiUsage")
     public static <T> Event<T> createLoop(Class<T> clazz) {
         return of(listeners -> (T) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{clazz}, new AbstractInvocationHandler() {
@@ -71,6 +77,12 @@ public final class EventFactory {
         }));
     }
     
+    @SafeVarargs
+    public static <T> Event<T> createInteractionResult(T... typeGetter) {
+        if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!");
+        return createInteractionResult((Class<T>) typeGetter.getClass().getComponentType());
+    }
+    
     @SuppressWarnings("UnstableApiUsage")
     public static <T> Event<T> createInteractionResult(Class<T> clazz) {
         return of(listeners -> (T) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{clazz}, new AbstractInvocationHandler() {
@@ -87,6 +99,12 @@ public final class EventFactory {
         }));
     }
     
+    @SafeVarargs
+    public static <T> Event<T> createInteractionResultHolder(T... typeGetter) {
+        if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!");
+        return createInteractionResultHolder((Class<T>) typeGetter.getClass().getComponentType());
+    }
+    
     @SuppressWarnings("UnstableApiUsage")
     public static <T> Event<T> createInteractionResultHolder(Class<T> clazz) {
         return of(listeners -> (T) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{clazz}, new AbstractInvocationHandler() {
@@ -103,6 +121,12 @@ public final class EventFactory {
         }));
     }
     
+    @SafeVarargs
+    public static <T> Event<Consumer<T>> createConsumerLoop(T... typeGetter) {
+        if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!");
+        return createConsumerLoop((Class<T>) typeGetter.getClass().getComponentType());
+    }
+    
     @SuppressWarnings("UnstableApiUsage")
     public static <T> Event<Consumer<T>> createConsumerLoop(Class<T> clazz) {
         Event<Consumer<T>> event = of(listeners -> (Consumer<T>) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{Consumer.class}, new AbstractInvocationHandler() {
@@ -124,6 +148,12 @@ public final class EventFactory {
         return event;
     }
     
+    @SafeVarargs
+    public static <T> Event<Actor<T>> createActorLoop(T... typeGetter) {
+        if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!");
+        return createActorLoop((Class<T>) typeGetter.getClass().getComponentType());
+    }
+    
     @SuppressWarnings("UnstableApiUsage")
     public static <T> Event<Actor<T>> createActorLoop(Class<T> clazz) {
         Event<Actor<T>> event = of(listeners -> (Actor<T>) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{Actor.class}, new AbstractInvocationHandler() {

+ 1 - 0
common/src/main/java/me/shedaniel/architectury/event/events/client/ClientLifecycleEvent.java

@@ -41,6 +41,7 @@ public interface ClientLifecycleEvent {
      * Invoked after a world is loaded only on client, equivalent to forge's {@code WorldEvent.Load}.
      */
     Event<ClientWorldState> CLIENT_WORLD_LOAD = EventFactory.createLoop(ClientWorldState.class);
+    Event<ClientState> CLIENT_SETUP = EventFactory.createLoop();
     
     @Deprecated
     @Environment(EnvType.CLIENT)

+ 19 - 2
common/src/main/java/me/shedaniel/architectury/registry/ColorHandlers.java

@@ -27,17 +27,34 @@ import net.minecraft.client.color.item.ItemColor;
 import net.minecraft.world.level.ItemLike;
 import net.minecraft.world.level.block.Block;
 
+import java.util.Arrays;
+import java.util.function.Supplier;
+
 @Environment(EnvType.CLIENT)
 public final class ColorHandlers {
     private ColorHandlers() {}
     
-    @ExpectPlatform
     public static void registerItemColors(ItemColor color, ItemLike... items) {
+        registerItemColors(color, Arrays.stream(items)
+                .map(item -> (Supplier<ItemLike>) () -> item)
+                .toArray(Supplier[]::new));
+    }
+    
+    public static void registerBlockColors(BlockColor color, Block... blocks) {
+        registerBlockColors(color, Arrays.stream(blocks)
+                .map(block -> (Supplier<Block>) () -> block)
+                .toArray(Supplier[]::new));
+    }
+    
+    @SafeVarargs
+    @ExpectPlatform
+    public static void registerItemColors(ItemColor color, Supplier<ItemLike>... items) {
         throw new AssertionError();
     }
     
+    @SafeVarargs
     @ExpectPlatform
-    public static void registerBlockColors(BlockColor color, Block... blocks) {
+    public static void registerBlockColors(BlockColor color, Supplier<Block>... blocks) {
         throw new AssertionError();
     }
 }

+ 0 - 1
common/src/main/java/me/shedaniel/architectury/registry/RenderTypes.java

@@ -39,6 +39,5 @@ public final class RenderTypes {
     public static void register(RenderType type, Fluid... fluids) {
         throw new AssertionError();
     }
-    
 }
 

+ 10 - 0
fabric/src/main/java/me/shedaniel/architectury/init/fabric/ArchitecturyClient.java

@@ -0,0 +1,10 @@
+package me.shedaniel.architectury.init.fabric;
+
+import me.shedaniel.architectury.event.events.client.ClientLifecycleEvent;
+import net.minecraft.client.Minecraft;
+
+public class ArchitecturyClient {
+    public static void init() {
+        ClientLifecycleEvent.CLIENT_SETUP.invoker().stateChanged(Minecraft.getInstance());
+    }
+}

+ 13 - 4
fabric/src/main/java/me/shedaniel/architectury/registry/fabric/ColorHandlersImpl.java

@@ -25,12 +25,21 @@ import net.minecraft.client.color.item.ItemColor;
 import net.minecraft.world.level.ItemLike;
 import net.minecraft.world.level.block.Block;
 
+import java.util.Arrays;
+import java.util.function.Supplier;
+
 public class ColorHandlersImpl {
-    public static void registerItemColors(ItemColor color, ItemLike... items) {
-        ColorProviderRegistry.ITEM.register(color, items);
+    @SafeVarargs
+    public static void registerItemColors(ItemColor itemColor, Supplier<ItemLike>... items) {
+        ColorProviderRegistry.ITEM.register(itemColor, Arrays.stream(items)
+                .map(Supplier::get)
+                .toArray(ItemLike[]::new));
     }
     
-    public static void registerBlockColors(BlockColor color, Block... blocks) {
-        ColorProviderRegistry.BLOCK.register(color, blocks);
+    @SafeVarargs
+    public static void registerBlockColors(BlockColor blockColor, Supplier<Block>... blocks) {
+        ColorProviderRegistry.BLOCK.register(blockColor, Arrays.stream(blocks)
+                .map(Supplier::get)
+                .toArray(Block[]::new));
     }
 }

+ 3 - 0
fabric/src/main/resources/fabric.mod.json

@@ -16,6 +16,9 @@
     "main": [
       "me.shedaniel.architectury.utils.fabric.GameInstanceImpl::init"
     ],
+    "client": [
+      "me.shedaniel.architectury.init.fabric.ArchitecturyClient::init"
+    ],
     "modmenu": [
       "me.shedaniel.architectury.compat.fabric.ModMenuCompatibility"
     ]

+ 6 - 0
forge/src/main/java/me/shedaniel/architectury/event/forge/EventHandlerImplClient.java

@@ -38,6 +38,7 @@ import net.minecraftforge.event.entity.player.ItemTooltipEvent;
 import net.minecraftforge.event.entity.player.PlayerInteractEvent;
 import net.minecraftforge.event.world.WorldEvent;
 import net.minecraftforge.eventbus.api.SubscribeEvent;
+import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
 
 import java.util.List;
 
@@ -275,5 +276,10 @@ public class EventHandlerImplClient {
         public static void event(net.minecraftforge.client.event.TextureStitchEvent.Post event) {
             TextureStitchEvent.POST.invoker().stitch(event.getMap());
         }
+    
+        @SubscribeEvent
+        public static void event(FMLClientSetupEvent event) {
+            ClientLifecycleEvent.CLIENT_SETUP.invoker().stateChanged(event.getMinecraftSupplier().get());
+        }
     }
 }

+ 0 - 49
forge/src/main/java/me/shedaniel/architectury/event/forge/ModBasedEventHandlerImpl.java

@@ -1,49 +0,0 @@
-/*
- * This file is part of architectury.
- * Copyright (C) 2020, 2021 shedaniel
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package me.shedaniel.architectury.event.forge;
-
-import me.shedaniel.architectury.event.events.TextureStitchEvent;
-import net.minecraftforge.api.distmarker.Dist;
-import net.minecraftforge.api.distmarker.OnlyIn;
-import net.minecraftforge.eventbus.api.SubscribeEvent;
-
-public class ModBasedEventHandlerImpl {
-    @OnlyIn(Dist.CLIENT)
-    public static class Client {
-        @SubscribeEvent
-        public static void event(net.minecraftforge.client.event.TextureStitchEvent.Pre event) {
-            TextureStitchEvent.PRE.invoker().stitch(event.getMap(), event::addSprite);
-        }
-        
-        @SubscribeEvent
-        public static void event(net.minecraftforge.client.event.TextureStitchEvent.Post event) {
-            TextureStitchEvent.POST.invoker().stitch(event.getMap());
-        }
-    }
-    
-    public static class Common {
-        
-    }
-    
-    @OnlyIn(Dist.DEDICATED_SERVER)
-    public static class Server {
-        
-    }
-}

+ 22 - 10
forge/src/main/java/me/shedaniel/architectury/registry/forge/ColorHandlersImpl.java

@@ -29,38 +29,50 @@ import net.minecraftforge.client.event.ColorHandlerEvent;
 import net.minecraftforge.common.MinecraftForge;
 import org.apache.commons.lang3.tuple.Pair;
 
+import java.util.Arrays;
 import java.util.List;
+import java.util.function.Supplier;
 
 public class ColorHandlersImpl {
-    private static final List<Pair<ItemColor, ItemLike[]>> ITEM_COLORS = Lists.newArrayList();
-    private static final List<Pair<BlockColor, Block[]>> BLOCK_COLORS = Lists.newArrayList();
+    private static final List<Pair<ItemColor, Supplier<ItemLike>[]>> ITEM_COLORS = Lists.newArrayList();
+    private static final List<Pair<BlockColor, Supplier<Block>[]>> BLOCK_COLORS = Lists.newArrayList();
     
     static {
         MinecraftForge.EVENT_BUS.<ColorHandlerEvent.Item>addListener(event -> {
-            for (Pair<ItemColor, ItemLike[]> pair : ITEM_COLORS) {
-                event.getItemColors().register(pair.getLeft(), pair.getRight());
+            for (Pair<ItemColor, Supplier<ItemLike>[]> pair : ITEM_COLORS) {
+                event.getItemColors().register(pair.getLeft(), Arrays.stream(pair.getRight())
+                        .map(Supplier::get)
+                        .toArray(ItemLike[]::new));
             }
         });
         MinecraftForge.EVENT_BUS.<ColorHandlerEvent.Block>addListener(event -> {
-            for (Pair<BlockColor, Block[]> pair : BLOCK_COLORS) {
-                event.getBlockColors().register(pair.getLeft(), pair.getRight());
+            for (Pair<BlockColor, Supplier<Block>[]> pair : BLOCK_COLORS) {
+                event.getBlockColors().register(pair.getLeft(), Arrays.stream(pair.getRight())
+                        .map(Supplier::get)
+                        .toArray(Block[]::new));
             }
         });
     }
     
-    public static void registerItemColors(ItemColor itemColor, ItemLike... items) {
+    @SafeVarargs
+    public static void registerItemColors(ItemColor itemColor, Supplier<ItemLike>... items) {
         if (Minecraft.getInstance().getItemColors() == null) {
             ITEM_COLORS.add(Pair.of(itemColor, items));
         } else {
-            Minecraft.getInstance().getItemColors().register(itemColor, items);
+            Minecraft.getInstance().getItemColors().register(itemColor, Arrays.stream(items)
+                    .map(Supplier::get)
+                    .toArray(ItemLike[]::new));
         }
     }
     
-    public static void registerBlockColors(BlockColor blockColor, Block... blocks) {
+    @SafeVarargs
+    public static void registerBlockColors(BlockColor blockColor, Supplier<Block>... blocks) {
         if (Minecraft.getInstance().getBlockColors() == null) {
             BLOCK_COLORS.add(Pair.of(blockColor, blocks));
         } else {
-            Minecraft.getInstance().getBlockColors().register(blockColor, blocks);
+            Minecraft.getInstance().getBlockColors().register(blockColor, Arrays.stream(blocks)
+                    .map(Supplier::get)
+                    .toArray(Block[]::new));
         }
     }
 }