Skip to content

Commit

Permalink
Refactor some event handlers, to better separate the client-side ones
Browse files Browse the repository at this point in the history
  • Loading branch information
maruohon committed Jun 25, 2017
1 parent 3533dc3 commit 1c34f17
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 80 deletions.
42 changes: 42 additions & 0 deletions src/main/java/baubles/client/ClientEventHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package baubles.client;

import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;
import net.minecraftforge.fml.relauncher.Side;
import baubles.api.BaubleType;
import baubles.api.IBauble;
import baubles.common.items.ItemRing;
import baubles.common.network.PacketHandler;
import baubles.common.network.PacketOpenBaublesInventory;

public class ClientEventHandler
{
@SubscribeEvent
public void registerItemModels(ModelRegistryEvent event) {
ModelLoader.setCustomModelResourceLocation(ItemRing.RING, 0, new ModelResourceLocation("baubles:ring", "inventory"));
}

@SubscribeEvent
public void playerTick(PlayerTickEvent event) {
if (event.side == Side.CLIENT && event.phase == Phase.START ) {
if (ClientProxy.KEY_BAUBLES.isPressed() && FMLClientHandler.instance().getClient().inGameHasFocus) {
PacketHandler.INSTANCE.sendToServer(new PacketOpenBaublesInventory(event.player));
}
}
}

@SubscribeEvent
public void tooltipEvent(ItemTooltipEvent event) {
if (!event.getItemStack().isEmpty() && event.getItemStack().getItem() instanceof IBauble) {
BaubleType bt = ((IBauble)event.getItemStack().getItem()).getBaubleType(event.getItemStack());
event.getToolTip().add(TextFormatting.GOLD + net.minecraft.client.resources.I18n.format("name." + bt));
}
}
}
28 changes: 11 additions & 17 deletions src/main/java/baubles/client/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,33 @@
package baubles.client;

import java.util.Map;
import org.lwjgl.input.Keyboard;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.entity.RenderPlayer;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import baubles.client.gui.GuiEvents;
import baubles.client.gui.GuiPlayerExpanded;
import baubles.common.Baubles;
import baubles.common.CommonProxy;
import baubles.common.event.KeyHandler;
import baubles.common.items.ItemRing;

@Mod.EventBusSubscriber(Side.CLIENT)
public class ClientProxy extends CommonProxy {

public static final KeyBinding KEY_BAUBLES = new KeyBinding("keybind.baublesinventory", Keyboard.KEY_B, "key.categories.inventory");

@Override
public void registerKeyBindings() {
keyHandler = new KeyHandler();
public void registerEventHandlers() {
super.registerEventHandlers();

ClientRegistry.registerKeyBinding(KEY_BAUBLES);

MinecraftForge.EVENT_BUS.register(new ClientEventHandler());
MinecraftForge.EVENT_BUS.register(new GuiEvents());
MinecraftForge.EVENT_BUS.register(keyHandler);
}

@Override
Expand All @@ -47,11 +46,6 @@ public World getClientWorld() {
return FMLClientHandler.instance().getClient().world;
}

@SubscribeEvent
public static void registerItemModels(ModelRegistryEvent event) {
ModelLoader.setCustomModelResourceLocation(ItemRing.RING, 0, new ModelResourceLocation("baubles:ring", "inventory"));
}

@Override
public void init() {
Map<String, RenderPlayer> skinMap = Minecraft.getMinecraft().getRenderManager().getSkinMap();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/baubles/client/gui/GuiPlayerExpanded.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ResourceLocation;
import baubles.common.Baubles;
import baubles.client.ClientProxy;
import baubles.common.container.ContainerPlayerExpanded;

public class GuiPlayerExpanded extends InventoryEffectRenderer {
Expand Down Expand Up @@ -156,7 +156,7 @@ protected void actionPerformed(GuiButton button)

@Override
protected void keyTyped(char par1, int par2) throws IOException {
if (par2 == Baubles.proxy.keyHandler.key.getKeyCode())
if (par2 == ClientProxy.KEY_BAUBLES.getKeyCode())
{
this.mc.player.closeScreen();
} else
Expand Down
14 changes: 1 addition & 13 deletions src/main/java/baubles/common/Baubles.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import baubles.api.cap.BaublesCapabilities.CapabilityBaubles;
import baubles.api.cap.BaublesContainer;
import baubles.api.cap.IBaublesItemHandler;
import baubles.common.event.CommandBaubles;
import baubles.common.event.EventHandlerEntity;
import baubles.common.network.PacketHandler;

@Mod(
Expand All @@ -39,7 +36,6 @@ public class Baubles {
@Instance(value=Baubles.MODID)
public static Baubles instance;

public EventHandlerEntity entityEventHandler;
public File modDir;

public static final Logger log = LogManager.getLogger(MODID.toUpperCase());
Expand All @@ -60,11 +56,8 @@ public void preInit(FMLPreInitializationEvent event) {
CapabilityManager.INSTANCE.register(IBaublesItemHandler.class,
new CapabilityBaubles<IBaublesItemHandler>(), BaublesContainer.class);

proxy.registerEventHandlers();
PacketHandler.init();

entityEventHandler = new EventHandlerEntity();

MinecraftForge.EVENT_BUS.register(entityEventHandler);

/////////////////////
Config.save();
Expand All @@ -73,14 +66,9 @@ public void preInit(FMLPreInitializationEvent event) {
@EventHandler
public void init(FMLInitializationEvent evt) {
NetworkRegistry.INSTANCE.registerGuiHandler(instance, proxy);
proxy.registerKeyBindings();
proxy.init();
}

@EventHandler
public void postInit(FMLPostInitializationEvent evt) {
}

@EventHandler
public void serverLoad(FMLServerStartingEvent event)
{
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/baubles/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.network.IGuiHandler;
import baubles.common.container.ContainerPlayerExpanded;
import baubles.common.event.KeyHandler;
import baubles.common.event.EventHandlerEntity;

public class CommonProxy implements IGuiHandler {

public KeyHandler keyHandler;

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
return null;
Expand All @@ -28,7 +27,9 @@ public World getClientWorld() {
return null;
}

public void registerKeyBindings() { }
public void registerEventHandlers() {
MinecraftForge.EVENT_BUS.register(new EventHandlerEntity());
}

public void init() { }
}
12 changes: 0 additions & 12 deletions src/main/java/baubles/common/event/EventHandlerEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.event.entity.player.PlayerDropsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent;
import baubles.api.BaubleType;
import baubles.api.BaublesApi;
import baubles.api.IBauble;
import baubles.api.cap.BaublesContainer;
Expand Down Expand Up @@ -151,12 +147,4 @@ public void dropItemsAt(EntityPlayer player, List<EntityItem> drops, Entity e) {
}
}
}

@SubscribeEvent
public void tooltipEvent(ItemTooltipEvent event) {
if (event.getItemStack()!=null && !event.getItemStack().isEmpty() && event.getItemStack().getItem() instanceof IBauble) {
BaubleType bt = ((IBauble)event.getItemStack().getItem()).getBaubleType(event.getItemStack());
event.getToolTip().add(TextFormatting.GOLD + I18n.format("name." + bt));
}
}
}
32 changes: 0 additions & 32 deletions src/main/java/baubles/common/event/KeyHandler.java

This file was deleted.

0 comments on commit 1c34f17

Please sign in to comment.