From 465aeae5bd2846ded85fcedc909829fa25dfaca7 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 23 Jan 2021 02:15:49 +0800 Subject: [PATCH] Add ClientLifecycleEvent.CLIENT_SETUP and allow deferring in ColorHandlers --- .../architectury/event/EventFactory.java | 30 ++++++++++++ .../events/client/ClientLifecycleEvent.java | 1 + .../architectury/registry/ColorHandlers.java | 21 +++++++- .../architectury/registry/RenderTypes.java | 1 - .../init/fabric/ArchitecturyClient.java | 10 ++++ .../registry/fabric/ColorHandlersImpl.java | 17 +++++-- fabric/src/main/resources/fabric.mod.json | 3 ++ .../event/forge/EventHandlerImplClient.java | 6 +++ .../event/forge/ModBasedEventHandlerImpl.java | 49 ------------------- .../registry/forge/ColorHandlersImpl.java | 32 ++++++++---- 10 files changed, 104 insertions(+), 66 deletions(-) create mode 100644 fabric/src/main/java/me/shedaniel/architectury/init/fabric/ArchitecturyClient.java delete mode 100644 forge/src/main/java/me/shedaniel/architectury/event/forge/ModBasedEventHandlerImpl.java diff --git a/common/src/main/java/me/shedaniel/architectury/event/EventFactory.java b/common/src/main/java/me/shedaniel/architectury/event/EventFactory.java index 443138ca..e054b55c 100644 --- a/common/src/main/java/me/shedaniel/architectury/event/EventFactory.java +++ b/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 Event createLoop(T... typeGetter) { + if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!"); + return createLoop((Class) typeGetter.getClass().getComponentType()); + } + @SuppressWarnings("UnstableApiUsage") public static Event createLoop(Class 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 Event createInteractionResult(T... typeGetter) { + if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!"); + return createInteractionResult((Class) typeGetter.getClass().getComponentType()); + } + @SuppressWarnings("UnstableApiUsage") public static Event createInteractionResult(Class 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 Event createInteractionResultHolder(T... typeGetter) { + if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!"); + return createInteractionResultHolder((Class) typeGetter.getClass().getComponentType()); + } + @SuppressWarnings("UnstableApiUsage") public static Event createInteractionResultHolder(Class 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 Event> createConsumerLoop(T... typeGetter) { + if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!"); + return createConsumerLoop((Class) typeGetter.getClass().getComponentType()); + } + @SuppressWarnings("UnstableApiUsage") public static Event> createConsumerLoop(Class clazz) { Event> event = of(listeners -> (Consumer) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{Consumer.class}, new AbstractInvocationHandler() { @@ -124,6 +148,12 @@ public final class EventFactory { return event; } + @SafeVarargs + public static Event> createActorLoop(T... typeGetter) { + if (typeGetter.length != 0) throw new IllegalStateException("array must be empty!"); + return createActorLoop((Class) typeGetter.getClass().getComponentType()); + } + @SuppressWarnings("UnstableApiUsage") public static Event> createActorLoop(Class clazz) { Event> event = of(listeners -> (Actor) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{Actor.class}, new AbstractInvocationHandler() { diff --git a/common/src/main/java/me/shedaniel/architectury/event/events/client/ClientLifecycleEvent.java b/common/src/main/java/me/shedaniel/architectury/event/events/client/ClientLifecycleEvent.java index 2b727c5f..c8b2de2b 100644 --- a/common/src/main/java/me/shedaniel/architectury/event/events/client/ClientLifecycleEvent.java +++ b/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 CLIENT_WORLD_LOAD = EventFactory.createLoop(ClientWorldState.class); + Event CLIENT_SETUP = EventFactory.createLoop(); @Deprecated @Environment(EnvType.CLIENT) diff --git a/common/src/main/java/me/shedaniel/architectury/registry/ColorHandlers.java b/common/src/main/java/me/shedaniel/architectury/registry/ColorHandlers.java index 6851087e..e9c30d53 100644 --- a/common/src/main/java/me/shedaniel/architectury/registry/ColorHandlers.java +++ b/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) () -> item) + .toArray(Supplier[]::new)); + } + + public static void registerBlockColors(BlockColor color, Block... blocks) { + registerBlockColors(color, Arrays.stream(blocks) + .map(block -> (Supplier) () -> block) + .toArray(Supplier[]::new)); + } + + @SafeVarargs + @ExpectPlatform + public static void registerItemColors(ItemColor color, Supplier... items) { throw new AssertionError(); } + @SafeVarargs @ExpectPlatform - public static void registerBlockColors(BlockColor color, Block... blocks) { + public static void registerBlockColors(BlockColor color, Supplier... blocks) { throw new AssertionError(); } } diff --git a/common/src/main/java/me/shedaniel/architectury/registry/RenderTypes.java b/common/src/main/java/me/shedaniel/architectury/registry/RenderTypes.java index 6ad0fff1..9fc7643d 100644 --- a/common/src/main/java/me/shedaniel/architectury/registry/RenderTypes.java +++ b/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(); } - } diff --git a/fabric/src/main/java/me/shedaniel/architectury/init/fabric/ArchitecturyClient.java b/fabric/src/main/java/me/shedaniel/architectury/init/fabric/ArchitecturyClient.java new file mode 100644 index 00000000..85aff307 --- /dev/null +++ b/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()); + } +} diff --git a/fabric/src/main/java/me/shedaniel/architectury/registry/fabric/ColorHandlersImpl.java b/fabric/src/main/java/me/shedaniel/architectury/registry/fabric/ColorHandlersImpl.java index 204ea10b..6e09315b 100644 --- a/fabric/src/main/java/me/shedaniel/architectury/registry/fabric/ColorHandlersImpl.java +++ b/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... 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... blocks) { + ColorProviderRegistry.BLOCK.register(blockColor, Arrays.stream(blocks) + .map(Supplier::get) + .toArray(Block[]::new)); } } diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json index ceeb75ad..e4476b10 100644 --- a/fabric/src/main/resources/fabric.mod.json +++ b/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" ] diff --git a/forge/src/main/java/me/shedaniel/architectury/event/forge/EventHandlerImplClient.java b/forge/src/main/java/me/shedaniel/architectury/event/forge/EventHandlerImplClient.java index c5dc285d..3e08053a 100644 --- a/forge/src/main/java/me/shedaniel/architectury/event/forge/EventHandlerImplClient.java +++ b/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()); + } } } diff --git a/forge/src/main/java/me/shedaniel/architectury/event/forge/ModBasedEventHandlerImpl.java b/forge/src/main/java/me/shedaniel/architectury/event/forge/ModBasedEventHandlerImpl.java deleted file mode 100644 index 1d44c81b..00000000 --- a/forge/src/main/java/me/shedaniel/architectury/event/forge/ModBasedEventHandlerImpl.java +++ /dev/null @@ -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 { - - } -} diff --git a/forge/src/main/java/me/shedaniel/architectury/registry/forge/ColorHandlersImpl.java b/forge/src/main/java/me/shedaniel/architectury/registry/forge/ColorHandlersImpl.java index 41c6a3d2..7bb927c1 100644 --- a/forge/src/main/java/me/shedaniel/architectury/registry/forge/ColorHandlersImpl.java +++ b/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> ITEM_COLORS = Lists.newArrayList(); - private static final List> BLOCK_COLORS = Lists.newArrayList(); + private static final List[]>> ITEM_COLORS = Lists.newArrayList(); + private static final List[]>> BLOCK_COLORS = Lists.newArrayList(); static { MinecraftForge.EVENT_BUS.addListener(event -> { - for (Pair pair : ITEM_COLORS) { - event.getItemColors().register(pair.getLeft(), pair.getRight()); + for (Pair[]> pair : ITEM_COLORS) { + event.getItemColors().register(pair.getLeft(), Arrays.stream(pair.getRight()) + .map(Supplier::get) + .toArray(ItemLike[]::new)); } }); MinecraftForge.EVENT_BUS.addListener(event -> { - for (Pair pair : BLOCK_COLORS) { - event.getBlockColors().register(pair.getLeft(), pair.getRight()); + for (Pair[]> 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... 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... 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)); } } }