mirror of
https://github.com/architectury/architectury-api.git
synced 2026-04-02 05:27:42 -05:00
More events and hooks
This commit is contained in:
@@ -19,22 +19,27 @@ package me.shedaniel.architectury.event.forge;
|
||||
import me.shedaniel.architectury.event.EventFactory;
|
||||
import me.shedaniel.architectury.event.events.*;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.IGuiEventListener;
|
||||
import net.minecraft.client.world.ClientWorld;
|
||||
import net.minecraft.entity.player.ServerPlayerEntity;
|
||||
import net.minecraft.util.ActionResult;
|
||||
import net.minecraft.util.ActionResultType;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.world.server.ServerWorld;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
|
||||
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
||||
import net.minecraftforge.client.event.*;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.CommandEvent;
|
||||
import net.minecraftforge.event.RegisterCommandsEvent;
|
||||
import net.minecraftforge.event.TickEvent.ClientTickEvent;
|
||||
import net.minecraftforge.event.TickEvent.Phase;
|
||||
import net.minecraftforge.event.TickEvent.ServerTickEvent;
|
||||
import net.minecraftforge.event.TickEvent.WorldTickEvent;
|
||||
import net.minecraftforge.event.ServerChatEvent;
|
||||
import net.minecraftforge.event.TickEvent.*;
|
||||
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
||||
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedInEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerLoggedOutEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent.PlayerRespawnEvent;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.LogicalSide;
|
||||
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
|
||||
@@ -43,6 +48,8 @@ import net.minecraftforge.fml.event.server.FMLServerStoppedEvent;
|
||||
import net.minecraftforge.fml.event.server.FMLServerStoppingEvent;
|
||||
import net.minecraftforge.fml.server.ServerLifecycleHooks;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class EventFactoryImpl implements EventFactory.Impl {
|
||||
@Override
|
||||
public void registerClient() {
|
||||
@@ -76,7 +83,8 @@ public class EventFactoryImpl implements EventFactory.Impl {
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(RenderGameOverlayEvent.Post event) {
|
||||
GuiEvent.RENDER_HUD.invoker().renderHud(event.getMatrixStack(), event.getPartialTicks());
|
||||
if (event.getType() == RenderGameOverlayEvent.ElementType.ALL)
|
||||
GuiEvent.RENDER_HUD.invoker().renderHud(event.getMatrixStack(), event.getPartialTicks());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@@ -93,6 +101,50 @@ public class EventFactoryImpl implements EventFactory.Impl {
|
||||
public static void event(ClientPlayerNetworkEvent.RespawnEvent event) {
|
||||
PlayerEvent.CLIENT_PLAYER_RESPAWN.invoker().respawn(event.getOldPlayer(), event.getNewPlayer());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(GuiScreenEvent.InitGuiEvent.Pre event) {
|
||||
if (GuiEvent.INIT_PRE.invoker().init(event.getGui(), event.getWidgetList(), (List<IGuiEventListener>) event.getGui().children()) == ActionResultType.FAIL) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(GuiScreenEvent.InitGuiEvent.Post event) {
|
||||
GuiEvent.INIT_POST.invoker().init(event.getGui(), event.getWidgetList(), (List<IGuiEventListener>) event.getGui().children());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(RenderGameOverlayEvent.Text event) {
|
||||
GuiEvent.DEBUG_TEXT_LEFT.invoker().gatherText(event.getLeft());
|
||||
GuiEvent.DEBUG_TEXT_RIGHT.invoker().gatherText(event.getRight());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(ClientChatEvent event) {
|
||||
ActionResult<String> process = ChatEvent.CLIENT.invoker().process(event.getMessage());
|
||||
if (process.getObject() != null)
|
||||
event.setMessage(process.getObject());
|
||||
if (process.getResult() == ActionResultType.FAIL)
|
||||
event.setCanceled(true);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(ClientChatReceivedEvent event) {
|
||||
ActionResult<ITextComponent> process = ChatEvent.CLIENT_RECEIVED.invoker().process(event.getType(), event.getMessage(), event.getSenderUUID());
|
||||
if (process.getObject() != null)
|
||||
event.setMessage(process.getObject());
|
||||
if (process.getResult() == ActionResultType.FAIL)
|
||||
event.setCanceled(true);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(WorldEvent.Save event) {
|
||||
if (event.getWorld() instanceof ClientWorld) {
|
||||
ClientWorld world = (ClientWorld) event.getWorld();
|
||||
LifecycleEvent.CLIENT_WORLD_LOAD.invoker().act(world);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Common {
|
||||
@@ -136,7 +188,7 @@ public class EventFactoryImpl implements EventFactory.Impl {
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(RegisterCommandsEvent event) {
|
||||
CommandRegistrationEvent.EVENT.invoker().register(event.getDispatcher());
|
||||
CommandRegistrationEvent.EVENT.invoker().register(event.getDispatcher(), event.getEnvironment());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@@ -153,6 +205,57 @@ public class EventFactoryImpl implements EventFactory.Impl {
|
||||
public static void event(PlayerRespawnEvent event) {
|
||||
PlayerEvent.PLAYER_RESPAWN.invoker().respawn((ServerPlayerEntity) event.getPlayer(), event.isEndConquered());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(CommandEvent event) {
|
||||
CommandPerformEvent performEvent = new CommandPerformEvent(event.getParseResults(), event.getException());
|
||||
if (CommandPerformEvent.EVENT.invoker().act(performEvent) == ActionResultType.FAIL) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
event.setParseResults(performEvent.getResults());
|
||||
event.setException(performEvent.getThrowable());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(PlayerTickEvent event) {
|
||||
if (event.phase == Phase.START) {
|
||||
TickEvent.PLAYER_PRE.invoker().tick(event.player);
|
||||
} else if (event.phase == Phase.END) {
|
||||
TickEvent.PLAYER_POST.invoker().tick(event.player);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(ServerChatEvent event) {
|
||||
ActionResult<ITextComponent> process = ChatEvent.SERVER.invoker().process(event.getMessage(), event.getComponent());
|
||||
if (process.getObject() != null)
|
||||
event.setComponent(process.getObject());
|
||||
if (process.getResult() == ActionResultType.FAIL)
|
||||
event.setCanceled(true);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(WorldEvent.Load event) {
|
||||
if (event.getWorld() instanceof ServerWorld) {
|
||||
ServerWorld world = (ServerWorld) event.getWorld();
|
||||
LifecycleEvent.SERVER_WORLD_LOAD.invoker().act(world);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(WorldEvent.Save event) {
|
||||
if (event.getWorld() instanceof ServerWorld) {
|
||||
ServerWorld world = (ServerWorld) event.getWorld();
|
||||
LifecycleEvent.SERVER_WORLD_SAVE.invoker().act(world);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void event(LivingDeathEvent event) {
|
||||
if (EntityEvent.LIVING_DEATH.invoker().die(event.getEntityLiving(), event.getSource()) == ActionResultType.FAIL) {
|
||||
event.setCanceled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.DEDICATED_SERVER)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2020 shedaniel
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package me.shedaniel.architectury.me.shedaniel.architectury.hooks.forge;
|
||||
|
||||
import me.shedaniel.architectury.hooks.ScreenHooks;
|
||||
import net.minecraft.client.gui.IGuiEventListener;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.Widget;
|
||||
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class ScreenHooksImpl implements ScreenHooks.Impl {
|
||||
@Override
|
||||
public <T extends Widget> T addButton(Screen screen, T t) {
|
||||
try {
|
||||
return (T) ObfuscationReflectionHelper.findMethod(Screen.class, "func_230480_a_", Widget.class).invoke(screen, t);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends IGuiEventListener> T addChild(Screen screen, T t) {
|
||||
try {
|
||||
return (T) ObfuscationReflectionHelper.findMethod(Screen.class, "func_230481_d_", IGuiEventListener.class).invoke(screen, t);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2020 shedaniel
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package me.shedaniel.architectury.registry.forge;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import me.shedaniel.architectury.registry.ColorHandlers;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.color.IBlockColor;
|
||||
import net.minecraft.client.renderer.color.IItemColor;
|
||||
import net.minecraft.util.IItemProvider;
|
||||
import net.minecraftforge.client.event.ColorHandlerEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ColorHandlersImpl implements ColorHandlers.Impl {
|
||||
private static final List<Pair<IItemColor, IItemProvider[]>> ITEM_COLORS = Lists.newArrayList();
|
||||
private static final List<Pair<IBlockColor, Block[]>> BLOCK_COLORS = Lists.newArrayList();
|
||||
|
||||
public ColorHandlersImpl() {
|
||||
MinecraftForge.EVENT_BUS.<ColorHandlerEvent.Item>addListener(event -> {
|
||||
for (Pair<IItemColor, IItemProvider[]> pair : ITEM_COLORS) {
|
||||
event.getItemColors().register(pair.getLeft(), pair.getRight());
|
||||
}
|
||||
});
|
||||
MinecraftForge.EVENT_BUS.<ColorHandlerEvent.Block>addListener(event -> {
|
||||
for (Pair<IBlockColor, Block[]> pair : BLOCK_COLORS) {
|
||||
event.getBlockColors().register(pair.getLeft(), pair.getRight());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerItemColors(IItemColor itemColor, IItemProvider... items) {
|
||||
if (Minecraft.getInstance().getItemColors() == null) {
|
||||
ITEM_COLORS.add(Pair.of(itemColor, items));
|
||||
} else {
|
||||
Minecraft.getInstance().getItemColors().register(itemColor, items);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerBlockColors(IBlockColor blockColor, Block... blocks) {
|
||||
if (Minecraft.getInstance().getBlockColors() == null) {
|
||||
BLOCK_COLORS.add(Pair.of(blockColor, blocks));
|
||||
} else {
|
||||
Minecraft.getInstance().getBlockColors().register(blockColor, blocks);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,20 @@ public class RegistriesImpl implements Registries.Impl {
|
||||
return new RegistryProviderImpl(modId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ResourceLocation getId(T t, RegistryKey<net.minecraft.util.registry.Registry<T>> registryKey) {
|
||||
if (t instanceof IForgeRegistryEntry)
|
||||
return ((IForgeRegistryEntry<?>) t).getRegistryName();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ResourceLocation getId(T t, net.minecraft.util.registry.Registry<T> registry) {
|
||||
if (t instanceof IForgeRegistryEntry)
|
||||
return ((IForgeRegistryEntry<?>) t).getRegistryName();
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class RegistryProviderImpl implements Registries.RegistryProvider {
|
||||
private final String modId;
|
||||
private final IEventBus eventBus;
|
||||
|
||||
Reference in New Issue
Block a user