More hooks for registering keybinds, render types, creative tabs, block entity renderers

This commit is contained in:
shedaniel
2020-11-02 00:06:10 +08:00
parent abb57db971
commit 76cf64adf0
24 changed files with 476 additions and 21 deletions

View File

@@ -25,6 +25,7 @@ import net.fabricmc.api.Environment;
import net.jodah.typetools.TypeResolver;
import net.minecraft.world.InteractionResult;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
@@ -47,7 +48,7 @@ public final class EventFactory {
public static <T> Event<T> createLoop(Class<T> clazz) {
return create(listeners -> (T) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{clazz}, new AbstractInvocationHandler() {
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable {
for (T listener : listeners) {
method.invoke(listener, args);
}
@@ -60,7 +61,7 @@ public final class EventFactory {
public static <T> Event<T> createInteractionResult(Class<T> clazz) {
return create(listeners -> (T) Proxy.newProxyInstance(EventFactory.class.getClassLoader(), new Class[]{clazz}, new AbstractInvocationHandler() {
@Override
protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
protected Object handleInvocation(@NotNull Object proxy, @NotNull Method method, Object @NotNull [] args) throws Throwable {
for (T listener : listeners) {
InteractionResult result = (InteractionResult) method.invoke(listener, args);
if (result != InteractionResult.PASS) {

View File

@@ -21,17 +21,21 @@ import me.shedaniel.architectury.event.EventFactory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import java.util.logging.Level;
public interface LifecycleEvent {
@Environment(EnvType.CLIENT)
Event<ClientState> CLIENT_STARTED = EventFactory.createLoop(ClientState.class);
@Environment(EnvType.CLIENT)
Event<ClientState> CLIENT_STOPPING = EventFactory.createLoop(ClientState.class);
@Environment(EnvType.CLIENT) Event<ClientState> CLIENT_STARTED = EventFactory.createLoop(ClientState.class);
@Environment(EnvType.CLIENT) Event<ClientState> CLIENT_STOPPING = EventFactory.createLoop(ClientState.class);
Event<ServerState> SERVER_STARTING = EventFactory.createLoop(ServerState.class);
Event<ServerState> SERVER_STARTED = EventFactory.createLoop(ServerState.class);
Event<ServerState> SERVER_STOPPING = EventFactory.createLoop(ServerState.class);
Event<ServerState> SERVER_STOPPED = EventFactory.createLoop(ServerState.class);
Event<WorldLoad> WORLD_LOAD = EventFactory.createLoop(WorldLoad.class);
Event<WorldSave> WORLD_SAVE = EventFactory.createLoop(WorldSave.class);
interface InstanceState<T> {
void stateChanged(T instance);
@@ -41,4 +45,12 @@ public interface LifecycleEvent {
interface ClientState extends InstanceState<Minecraft> {}
interface ServerState extends InstanceState<MinecraftServer> {}
interface WorldLoad {
void load(ResourceKey<Level> key, ServerLevel world);
}
interface WorldSave {
void save(ServerLevel world);
}
}

View File

@@ -0,0 +1,44 @@
package me.shedaniel.architectury.event.events;
import me.shedaniel.architectury.event.Event;
import me.shedaniel.architectury.event.EventFactory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.server.level.ServerPlayer;
public interface PlayerEvent {
Event<PlayerJoin> PLAYER_JOIN = EventFactory.createLoop(PlayerJoin.class);
Event<PlayerQuit> PLAYER_QUIT = EventFactory.createLoop(PlayerQuit.class);
Event<PlayerRespawn> PLAYER_RESPAWN = EventFactory.createLoop(PlayerRespawn.class);
@Environment(EnvType.CLIENT) Event<ClientPlayerJoin> CLIENT_PLAYER_JOIN = EventFactory.createLoop(ClientPlayerJoin.class);
@Environment(EnvType.CLIENT) Event<ClientPlayerQuit> CLIENT_PLAYER_QUIT = EventFactory.createLoop(ClientPlayerQuit.class);
@Environment(EnvType.CLIENT) Event<ClientPlayerRespawn> CLIENT_PLAYER_RESPAWN = EventFactory.createLoop(ClientPlayerRespawn.class);
interface PlayerJoin {
void join(ServerPlayer player);
}
interface PlayerQuit {
void quit(ServerPlayer player);
}
interface PlayerRespawn {
void respawn(ServerPlayer newPlayer, boolean conqueredEnd);
}
@Environment(EnvType.CLIENT)
interface ClientPlayerJoin {
void join(LocalPlayer player);
}
@Environment(EnvType.CLIENT)
interface ClientPlayerQuit {
void quit(LocalPlayer player);
}
@Environment(EnvType.CLIENT)
interface ClientPlayerRespawn {
void respawn(LocalPlayer oldPlayer, LocalPlayer newPlayer);
}
}

View File

@@ -27,16 +27,12 @@ import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
public interface TickEvent<T> {
@Environment(EnvType.CLIENT)
Event<Client> CLIENT_PRE = EventFactory.createLoop(Client.class);
@Environment(EnvType.CLIENT)
Event<Client> CLIENT_POST = EventFactory.createLoop(Client.class);
@Environment(EnvType.CLIENT) Event<Client> CLIENT_PRE = EventFactory.createLoop(Client.class);
@Environment(EnvType.CLIENT) Event<Client> CLIENT_POST = EventFactory.createLoop(Client.class);
Event<Server> SERVER_PRE = EventFactory.createLoop(Server.class);
Event<Server> SERVER_POST = EventFactory.createLoop(Server.class);
@Environment(EnvType.CLIENT)
Event<ClientWorld> CLIENT_WORLD_PRE = EventFactory.createLoop(ClientWorld.class);
@Environment(EnvType.CLIENT)
Event<ClientWorld> CLIENT_WORLD_POST = EventFactory.createLoop(ClientWorld.class);
@Environment(EnvType.CLIENT) Event<ClientWorld> CLIENT_WORLD_PRE = EventFactory.createLoop(ClientWorld.class);
@Environment(EnvType.CLIENT) Event<ClientWorld> CLIENT_WORLD_POST = EventFactory.createLoop(ClientWorld.class);
Event<ServerWorld> SERVER_WORLD_PRE = EventFactory.createLoop(ServerWorld.class);
Event<ServerWorld> SERVER_WORLD_POST = EventFactory.createLoop(ServerWorld.class);

View File

@@ -0,0 +1,32 @@
package me.shedaniel.architectury.registry;
import me.shedaniel.architectury.ArchitecturyPopulator;
import me.shedaniel.architectury.Populatable;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import java.util.function.Function;
@Environment(EnvType.CLIENT)
public final class BlockEntityRenderers {
private BlockEntityRenderers() {}
@Populatable
private static final Impl IMPL = null;
public static <T extends BlockEntity> void registerRenderer(BlockEntityType<T> type, Function<BlockEntityRenderDispatcher, BlockEntityRenderer<T>> provider) {
IMPL.registerRenderer(type, provider);
}
public interface Impl {
<T extends BlockEntity> void registerRenderer(BlockEntityType<T> type, Function<BlockEntityRenderDispatcher, BlockEntityRenderer<T>> provider);
}
static {
ArchitecturyPopulator.populate(BlockEntityRenderers.class);
}
}

View File

@@ -0,0 +1,29 @@
package me.shedaniel.architectury.registry;
import me.shedaniel.architectury.ArchitecturyPopulator;
import me.shedaniel.architectury.Populatable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import java.util.function.Supplier;
public final class CreativeTabs {
public CreativeTabs() {}
@Populatable
private static final Impl IMPL = null;
// I am sorry, fabric wants a resource location instead of the translation key for whatever reason
public static CreativeModeTab create(ResourceLocation name, Supplier<ItemStack> icon) {
return IMPL.create(name, icon);
}
public interface Impl {
CreativeModeTab create(ResourceLocation name, Supplier<ItemStack> icon);
}
static {
ArchitecturyPopulator.populate(CreativeTabs.class);
}
}

View File

@@ -0,0 +1,27 @@
package me.shedaniel.architectury.registry;
import me.shedaniel.architectury.ArchitecturyPopulator;
import me.shedaniel.architectury.Populatable;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.KeyMapping;
@Environment(EnvType.CLIENT)
public final class KeyBindings {
private KeyBindings() {}
@Populatable
private static final Impl IMPL = null;
public static void registerKeyBinding(KeyMapping binding) {
IMPL.registerKeyBinding(binding);
}
public interface Impl {
void registerKeyBinding(KeyMapping binding);
}
static {
ArchitecturyPopulator.populate(KeyBindings.class);
}
}

View File

@@ -5,8 +5,8 @@ import me.shedaniel.architectury.Populatable;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.resources.PreparableReloadListener;
public final class ReloadListenerRegistry {
private ReloadListenerRegistry() {}
public final class ReloadListeners {
private ReloadListeners() {}
@Populatable
private static final Impl IMPL = null;
@@ -20,6 +20,6 @@ public final class ReloadListenerRegistry {
}
static {
ArchitecturyPopulator.populate(ReloadListenerRegistry.class);
ArchitecturyPopulator.populate(ReloadListeners.class);
}
}

View File

@@ -0,0 +1,36 @@
package me.shedaniel.architectury.registry;
import me.shedaniel.architectury.ArchitecturyPopulator;
import me.shedaniel.architectury.Populatable;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.material.Fluid;
@Environment(EnvType.CLIENT)
public final class RenderTypes {
private RenderTypes() {}
@Populatable
private static final Impl IMPL = null;
public static void register(RenderType type, Block... blocks) {
IMPL.register(type, blocks);
}
public static void register(RenderType type, Fluid... fluids) {
IMPL.register(type, fluids);
}
public interface Impl {
void register(RenderType type, Block... blocks);
void register(RenderType type, Fluid... fluids);
}
static {
ArchitecturyPopulator.populate(RenderTypes.class);
}
}