1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-26 17:06:54 +00:00

Utilise @Mod.EventBusSubscriber a little more

This offers several advantages

 - Less registration code: the subscribers are reigstered automatically,
   and we don't need to worry about sided-proxies.
 - We no longer have so many .instance() calls.
This commit is contained in:
SquidDev 2019-01-12 16:27:40 +00:00
parent 77666d7399
commit 5d97b9c8f3
13 changed files with 114 additions and 143 deletions

View File

@ -6,43 +6,40 @@
package dan200.computercraft.client; package dan200.computercraft.client;
import dan200.computercraft.ComputerCraft;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent; import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
public class FrameInfo @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
public final class FrameInfo
{ {
private static final FrameInfo instance = new FrameInfo(); private static int tick;
private static long renderFrame;
public static FrameInfo instance()
{
return instance;
}
private int tick;
private long renderFrame;
private FrameInfo() private FrameInfo()
{ {
} }
public boolean getGlobalCursorBlink() public static boolean getGlobalCursorBlink()
{ {
return (tick / 8) % 2 == 0; return (tick / 8) % 2 == 0;
} }
public long getRenderFrame() public static long getRenderFrame()
{ {
return renderFrame; return renderFrame;
} }
@SubscribeEvent @SubscribeEvent
public void onTick( TickEvent.ClientTickEvent event ) public static void onTick( TickEvent.ClientTickEvent event )
{ {
if( event.phase == TickEvent.Phase.START ) tick++; if( event.phase == TickEvent.Phase.START ) tick++;
} }
@SubscribeEvent @SubscribeEvent
public void onRenderTick( TickEvent.RenderTickEvent event ) public static void onRenderTick( TickEvent.RenderTickEvent event )
{ {
if( event.phase == TickEvent.Phase.START ) renderFrame++; if( event.phase == TickEvent.Phase.START ) renderFrame++;
} }

View File

@ -388,7 +388,7 @@ public class WidgetTerminal extends Widget
// Get the data from the terminal first // Get the data from the terminal first
// Unfortunately we have to keep the lock for the whole of drawing, so the text doesn't change under us. // Unfortunately we have to keep the lock for the whole of drawing, so the text doesn't change under us.
FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance(); FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
boolean tblink = m_focus && terminal.getCursorBlink() && FrameInfo.instance().getGlobalCursorBlink(); boolean tblink = m_focus && terminal.getCursorBlink() && FrameInfo.getGlobalCursorBlink();
int tw = terminal.getWidth(); int tw = terminal.getWidth();
int th = terminal.getHeight(); int th = terminal.getHeight();
int tx = terminal.getCursorX(); int tx = terminal.getCursorX();

View File

@ -18,7 +18,6 @@ import net.minecraft.client.renderer.ItemMeshDefinition;
import net.minecraft.client.renderer.block.model.IBakedModel; import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ModelBakery; import net.minecraft.client.renderer.block.model.ModelBakery;
import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.client.renderer.texture.TextureMap; import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.resources.IResourceManager; import net.minecraft.client.resources.IResourceManager;
@ -83,10 +82,16 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon
super.init(); super.init();
// Setup turtle colours // Setup turtle colours
Minecraft.getMinecraft().getItemColors().registerItemColorHandler( Minecraft.getMinecraft().getItemColors().registerItemColorHandler( ( stack, tintIndex ) -> {
new TurtleItemColour(), if( tintIndex == 0 )
ComputerCraft.Blocks.turtle, ComputerCraft.Blocks.turtleExpanded, ComputerCraft.Blocks.turtleAdvanced {
); ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
int colour = turtle.getColour( stack );
if( colour != -1 ) return colour;
}
return 0xFFFFFF;
}, ComputerCraft.Blocks.turtle, ComputerCraft.Blocks.turtleExpanded, ComputerCraft.Blocks.turtleAdvanced );
// Setup renderers // Setup renderers
ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() ); ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() );
@ -110,8 +115,7 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon
private void registerForgeHandlers() private void registerForgeHandlers()
{ {
ForgeHandlers handlers = new ForgeHandlers(); MinecraftForge.EVENT_BUS.register( new ForgeHandlers() );
MinecraftForge.EVENT_BUS.register( handlers );
} }
public static class ForgeHandlers public static class ForgeHandlers
@ -196,19 +200,4 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon
} }
} }
private static class TurtleItemColour implements IItemColor
{
@Override
public int colorMultiplier( @Nonnull ItemStack stack, int tintIndex )
{
if( tintIndex == 0 )
{
ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
int colour = turtle.getColour( stack );
if( colour != -1 ) return colour;
}
return 0xFFFFFF;
}
}
} }

View File

@ -8,8 +8,8 @@ package dan200.computercraft.client.proxy;
import dan200.computercraft.ComputerCraft; import dan200.computercraft.ComputerCraft;
import dan200.computercraft.client.ClientTableFormatter; import dan200.computercraft.client.ClientTableFormatter;
import dan200.computercraft.client.FrameInfo; import dan200.computercraft.client.render.TileEntityCableRenderer;
import dan200.computercraft.client.render.*; import dan200.computercraft.client.render.TileEntityMonitorRenderer;
import dan200.computercraft.shared.command.CommandCopy; import dan200.computercraft.shared.command.CommandCopy;
import dan200.computercraft.shared.command.text.TableBuilder; import dan200.computercraft.shared.command.text.TableBuilder;
import dan200.computercraft.shared.media.items.ItemDiskLegacy; import dan200.computercraft.shared.media.items.ItemDiskLegacy;
@ -33,9 +33,9 @@ import net.minecraft.world.World;
import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.client.event.ModelRegistryEvent; import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader; import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.client.registry.ClientRegistry; import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.fml.relauncher.SideOnly;
@ -50,9 +50,6 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
{ {
super.preInit(); super.preInit();
// Setup client forge handlers
registerForgeHandlers();
// Register any client-specific commands // Register any client-specific commands
ClientCommandHandler.instance.registerCommand( CommandCopy.INSTANCE ); ClientCommandHandler.instance.registerCommand( CommandCopy.INSTANCE );
} }
@ -95,8 +92,7 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
mc.getItemColors().registerItemColorHandler( new DiskColorHandler( ComputerCraft.Items.disk ), ComputerCraft.Items.disk ); mc.getItemColors().registerItemColorHandler( new DiskColorHandler( ComputerCraft.Items.disk ), ComputerCraft.Items.disk );
mc.getItemColors().registerItemColorHandler( new DiskColorHandler( ComputerCraft.Items.diskExpanded ), ComputerCraft.Items.diskExpanded ); mc.getItemColors().registerItemColorHandler( new DiskColorHandler( ComputerCraft.Items.diskExpanded ), ComputerCraft.Items.diskExpanded );
mc.getItemColors().registerItemColorHandler( ( stack, layer ) -> mc.getItemColors().registerItemColorHandler( ( stack, layer ) -> {
{
switch( layer ) switch( layer )
{ {
case 0: case 0:
@ -160,15 +156,6 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
return world.getSaveHandler().getWorldDirectory(); return world.getSaveHandler().getWorldDirectory();
} }
private void registerForgeHandlers()
{
MinecraftForge.EVENT_BUS.register( new ForgeHandlers() );
MinecraftForge.EVENT_BUS.register( new RenderOverlayCable() );
MinecraftForge.EVENT_BUS.register( new ItemPocketRenderer() );
MinecraftForge.EVENT_BUS.register( new ItemPrintoutRenderer() );
MinecraftForge.EVENT_BUS.register( FrameInfo.instance() );
}
@Override @Override
public void playRecordClient( BlockPos pos, SoundEvent record, String info ) public void playRecordClient( BlockPos pos, SoundEvent record, String info )
{ {
@ -183,10 +170,11 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
ClientTableFormatter.INSTANCE.display( table ); ClientTableFormatter.INSTANCE.display( table );
} }
public class ForgeHandlers @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
public static class ForgeHandlers
{ {
@SubscribeEvent @SubscribeEvent
public void onWorldUnload( WorldEvent.Unload event ) public static void onWorldUnload( WorldEvent.Unload event )
{ {
if( event.getWorld().isRemote ) if( event.getWorld().isRemote )
{ {

View File

@ -24,9 +24,9 @@ import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.client.ForgeHooksClient; import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.event.RenderSpecificHandEvent; import net.minecraftforge.client.event.RenderSpecificHandEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT; import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
@ -35,17 +35,23 @@ import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH;
/** /**
* Emulates map rendering for pocket computers * Emulates map rendering for pocket computers
*/ */
@SideOnly( Side.CLIENT ) @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
public class ItemPocketRenderer extends ItemMapLikeRenderer public final class ItemPocketRenderer extends ItemMapLikeRenderer
{ {
private static final ItemPocketRenderer INSTANCE = new ItemPocketRenderer();
private ItemPocketRenderer()
{
}
@SubscribeEvent @SubscribeEvent
public void renderItem( RenderSpecificHandEvent event ) public static void renderItem( RenderSpecificHandEvent event )
{ {
ItemStack stack = event.getItemStack(); ItemStack stack = event.getItemStack();
if( !(stack.getItem() instanceof ItemPocketComputer) ) return; if( !(stack.getItem() instanceof ItemPocketComputer) ) return;
event.setCanceled( true ); event.setCanceled( true );
renderItemFirstPerson( event.getHand(), event.getInterpolatedPitch(), event.getEquipProgress(), event.getSwingProgress(), event.getItemStack() ); INSTANCE.renderItemFirstPerson( event.getHand(), event.getInterpolatedPitch(), event.getEquipProgress(), event.getSwingProgress(), event.getItemStack() );
} }
@Override @Override
@ -145,7 +151,7 @@ public class ItemPocketRenderer extends ItemMapLikeRenderer
// And render the cursor; // And render the cursor;
int tx = terminal.getCursorX(), ty = terminal.getCursorY(); int tx = terminal.getCursorX(), ty = terminal.getCursorY();
if( terminal.getCursorBlink() && FrameInfo.instance().getGlobalCursorBlink() && if( terminal.getCursorBlink() && FrameInfo.getGlobalCursorBlink() &&
tx >= 0 && ty >= 0 && tx < tw && ty < th ) tx >= 0 && ty >= 0 && tx < tw && ty < th )
{ {
TextBuffer cursorColour = new TextBuffer( "0123456789abcdef".charAt( terminal.getTextColour() ), 1 ); TextBuffer cursorColour = new TextBuffer( "0123456789abcdef".charAt( terminal.getTextColour() ), 1 );

View File

@ -12,7 +12,9 @@ import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraftforge.client.event.RenderItemInFrameEvent; import net.minecraftforge.client.event.RenderItemInFrameEvent;
import net.minecraftforge.client.event.RenderSpecificHandEvent; import net.minecraftforge.client.event.RenderSpecificHandEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT; import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH; import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_WIDTH;
@ -20,17 +22,27 @@ import static dan200.computercraft.client.render.PrintoutRenderer.*;
import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE; import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE;
import static dan200.computercraft.shared.media.items.ItemPrintout.LINE_MAX_LENGTH; import static dan200.computercraft.shared.media.items.ItemPrintout.LINE_MAX_LENGTH;
public class ItemPrintoutRenderer extends ItemMapLikeRenderer /**
* Emulates map and item-frame rendering for prinouts
*/
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
{ {
private static final ItemPrintoutRenderer INSTANCE = new ItemPrintoutRenderer();
private ItemPrintoutRenderer()
{
}
@SubscribeEvent @SubscribeEvent
public void onRenderInHand( RenderSpecificHandEvent event ) public static void onRenderInHand( RenderSpecificHandEvent event )
{ {
ItemStack stack = event.getItemStack(); ItemStack stack = event.getItemStack();
if( stack.getItem() != ComputerCraft.Items.printout ) return; if( stack.getItem() != ComputerCraft.Items.printout ) return;
event.setCanceled( true ); event.setCanceled( true );
renderItemFirstPerson( event.getHand(), event.getInterpolatedPitch(), event.getEquipProgress(), event.getSwingProgress(), event.getItemStack() ); INSTANCE.renderItemFirstPerson( event.getHand(), event.getInterpolatedPitch(), event.getEquipProgress(), event.getSwingProgress(), event.getItemStack() );
} }
@Override @Override
@ -51,7 +63,7 @@ public class ItemPrintoutRenderer extends ItemMapLikeRenderer
} }
@SubscribeEvent @SubscribeEvent
public void onRenderInFrame( RenderItemInFrameEvent event ) public static void onRenderInFrame( RenderItemInFrameEvent event )
{ {
ItemStack stack = event.getItem(); ItemStack stack = event.getItem();
if( stack.getItem() != ComputerCraft.Items.printout ) return; if( stack.getItem() != ComputerCraft.Items.printout ) return;
@ -69,6 +81,7 @@ public class ItemPrintoutRenderer extends ItemMapLikeRenderer
drawPrintout( stack ); drawPrintout( stack );
GlStateManager.enableLighting(); GlStateManager.enableLighting();
GlStateManager.disableBlend();
} }
private static void drawPrintout( ItemStack stack ) private static void drawPrintout( ItemStack stack )

View File

@ -23,17 +23,24 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.client.event.DrawBlockHighlightEvent; import net.minecraftforge.client.event.DrawBlockHighlightEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.Side;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
public class RenderOverlayCable @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
public final class RenderOverlayCable
{ {
private static final float EXPAND = 0.002f; private static final float EXPAND = 0.002f;
private static final double MIN = CableBounds.MIN - EXPAND; private static final double MIN = CableBounds.MIN - EXPAND;
private static final double MAX = CableBounds.MAX + EXPAND; private static final double MAX = CableBounds.MAX + EXPAND;
private RenderOverlayCable()
{
}
@SubscribeEvent @SubscribeEvent
public void drawHighlight( DrawBlockHighlightEvent event ) public static void drawHighlight( DrawBlockHighlightEvent event )
{ {
if( event.getTarget().typeOfHit != RayTraceResult.Type.BLOCK ) return; if( event.getTarget().typeOfHit != RayTraceResult.Type.BLOCK ) return;

View File

@ -49,7 +49,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
// Ensure each monitor terminal is rendered only once. We allow rendering a specific tile // Ensure each monitor terminal is rendered only once. We allow rendering a specific tile
// multiple times in a single frame to ensure compatibility with shaders which may run a // multiple times in a single frame to ensure compatibility with shaders which may run a
// pass multiple times. // pass multiple times.
long renderFrame = FrameInfo.instance().getRenderFrame(); long renderFrame = FrameInfo.getRenderFrame();
if( originTerminal.lastRenderFrame == renderFrame && !monitorPos.equals( originTerminal.lastRenderPos ) ) if( originTerminal.lastRenderFrame == renderFrame && !monitorPos.equals( originTerminal.lastRenderPos ) )
{ {
return; return;
@ -230,7 +230,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
GlStateManager.glEndList(); GlStateManager.glEndList();
} }
} }
if( FrameInfo.instance().getGlobalCursorBlink() ) if( FrameInfo.getGlobalCursorBlink() )
{ {
GlStateManager.callList( originTerminal.renderDisplayLists[2] ); GlStateManager.callList( originTerminal.renderDisplayLists[2] );
GlStateManager.resetColor(); GlStateManager.resetColor();

View File

@ -21,38 +21,23 @@ import dan200.computercraft.shared.turtle.items.ItemTurtleLegacy;
import dan200.computercraft.shared.turtle.items.ItemTurtleNormal; import dan200.computercraft.shared.turtle.items.ItemTurtleNormal;
import dan200.computercraft.shared.turtle.items.TurtleItemFactory; import dan200.computercraft.shared.turtle.items.TurtleItemFactory;
import dan200.computercraft.shared.turtle.upgrades.*; import dan200.computercraft.shared.turtle.upgrades.*;
import dan200.computercraft.shared.util.DropConsumer;
import dan200.computercraft.shared.util.ImpostorRecipe; import dan200.computercraft.shared.util.ImpostorRecipe;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.init.Items; import net.minecraft.init.Items;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.EntityRegistry; import net.minecraftforge.fml.common.registry.EntityRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.IForgeRegistry;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.function.Function;
public abstract class CCTurtleProxyCommon implements ICCTurtleProxy public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
{ {
private Function<ItemStack, ItemStack> dropConsumer;
private List<ItemStack> remainingDrops;
private WeakReference<World> dropWorld;
private BlockPos dropPos;
private AxisAlignedBB dropBounds;
private WeakReference<Entity> dropEntity;
@Override @Override
public void preInit() public void preInit()
{ {
@ -67,7 +52,6 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
@Override @Override
public void init() public void init()
{ {
registerForgeHandlers();
registerTileEntities(); registerTileEntities();
} }
@ -248,16 +232,11 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
GameRegistry.registerTileEntity( TileTurtleAdvanced.class, new ResourceLocation( ComputerCraft.MOD_ID, "turtleadv" ) ); GameRegistry.registerTileEntity( TileTurtleAdvanced.class, new ResourceLocation( ComputerCraft.MOD_ID, "turtleadv" ) );
} }
private void registerForgeHandlers() @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID )
{ public static class ForgeHandlers
MinecraftForge.EVENT_BUS.register( new ForgeHandlers() );
MinecraftForge.EVENT_BUS.register( DropConsumer.instance() );
}
private class ForgeHandlers
{ {
@SubscribeEvent @SubscribeEvent
public void onTurtleAction( TurtleActionEvent event ) public static void onTurtleAction( TurtleActionEvent event )
{ {
if( ComputerCraft.turtleDisabledActions.contains( event.getAction() ) ) if( ComputerCraft.turtleDisabledActions.contains( event.getAction() ) )
{ {

View File

@ -85,10 +85,10 @@ import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.entity.player.PlayerContainerEvent; import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.client.event.ConfigChangedEvent; import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Loader; import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent; import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent; import net.minecraftforge.fml.common.network.FMLNetworkEvent;
@ -371,9 +371,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
private void registerForgeHandlers() private void registerForgeHandlers()
{ {
ForgeHandlers handlers = new ForgeHandlers(); NetworkRegistry.INSTANCE.registerGuiHandler( ComputerCraft.instance, new GuiHandler() );
MinecraftForge.EVENT_BUS.register( handlers );
NetworkRegistry.INSTANCE.registerGuiHandler( ComputerCraft.instance, handlers );
} }
private void registerNetwork() private void registerNetwork()
@ -421,14 +419,12 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
showTableClient( packet.getTable() ) ); showTableClient( packet.getTable() ) );
} }
public class ForgeHandlers implements IGuiHandler public class GuiHandler implements IGuiHandler
{ {
private ForgeHandlers() private GuiHandler()
{ {
} }
// IGuiHandler implementation
@Override @Override
public Object getServerGuiElement( int id, EntityPlayer player, World world, int x, int y, int z ) public Object getServerGuiElement( int id, EntityPlayer player, World world, int x, int y, int z )
{ {
@ -561,23 +557,29 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
return null; return null;
} }
} }
}
// Event handlers @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID )
public final static class ForgeHandlers
{
private ForgeHandlers()
{
}
@SubscribeEvent @SubscribeEvent
public void onConnectionOpened( FMLNetworkEvent.ClientConnectedToServerEvent event ) public static void onConnectionOpened( FMLNetworkEvent.ClientConnectedToServerEvent event )
{ {
ComputerCraft.clientComputerRegistry.reset(); ComputerCraft.clientComputerRegistry.reset();
} }
@SubscribeEvent @SubscribeEvent
public void onConnectionClosed( FMLNetworkEvent.ClientDisconnectionFromServerEvent event ) public static void onConnectionClosed( FMLNetworkEvent.ClientDisconnectionFromServerEvent event )
{ {
ComputerCraft.clientComputerRegistry.reset(); ComputerCraft.clientComputerRegistry.reset();
} }
@SubscribeEvent @SubscribeEvent
public void onClientTick( TickEvent.ClientTickEvent event ) public static void onClientTick( TickEvent.ClientTickEvent event )
{ {
if( event.phase == TickEvent.Phase.START ) if( event.phase == TickEvent.Phase.START )
{ {
@ -586,7 +588,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
} }
@SubscribeEvent @SubscribeEvent
public void onServerTick( TickEvent.ServerTickEvent event ) public static void onServerTick( TickEvent.ServerTickEvent event )
{ {
if( event.phase == TickEvent.Phase.START ) if( event.phase == TickEvent.Phase.START )
{ {
@ -596,23 +598,13 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy
} }
@SubscribeEvent @SubscribeEvent
public void onWorldLoad( WorldEvent.Load event ) public static void onConfigChanged( ConfigChangedEvent.OnConfigChangedEvent event )
{
}
@SubscribeEvent
public void onWorldUnload( WorldEvent.Unload event )
{
}
@SubscribeEvent
public void onConfigChanged( ConfigChangedEvent.OnConfigChangedEvent event )
{ {
if( event.getModID().equals( ComputerCraft.MOD_ID ) ) Config.sync(); if( event.getModID().equals( ComputerCraft.MOD_ID ) ) Config.sync();
} }
@SubscribeEvent @SubscribeEvent
public void onContainerOpen( PlayerContainerEvent.Open event ) public static void onContainerOpen( PlayerContainerEvent.Open event )
{ {
// If we're opening a computer container then broadcast the terminal state // If we're opening a computer container then broadcast the terminal state
Container container = event.getContainer(); Container container = event.getContainer();

View File

@ -226,7 +226,7 @@ public class TurtlePlaceCommand implements ITurtleCommand
// Start claiming entity drops // Start claiming entity drops
Entity hitEntity = hit.getKey(); Entity hitEntity = hit.getKey();
Vec3d hitPos = hit.getValue(); Vec3d hitPos = hit.getValue();
DropConsumer.instance().set( DropConsumer.set(
hitEntity, hitEntity,
drop -> InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() ) drop -> InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() )
); );
@ -266,7 +266,7 @@ public class TurtlePlaceCommand implements ITurtleCommand
} }
// Stop claiming drops // Stop claiming drops
List<ItemStack> remainingDrops = DropConsumer.instance().clear(); List<ItemStack> remainingDrops = DropConsumer.clear();
for( ItemStack remaining : remainingDrops ) for( ItemStack remaining : remainingDrops )
{ {
WorldUtil.dropItemStack( remaining, world, position, turtle.getDirection().getOpposite() ); WorldUtil.dropItemStack( remaining, world, position, turtle.getDirection().getOpposite() );

View File

@ -135,7 +135,7 @@ public class TurtleTool extends AbstractTurtleUpgrade
} }
// Start claiming entity drops // Start claiming entity drops
DropConsumer.instance().set( hitEntity, turtleDropConsumer( turtle ) ); DropConsumer.set( hitEntity, turtleDropConsumer( turtle ) );
// Attack the entity // Attack the entity
boolean attacked = false; boolean attacked = false;
@ -225,7 +225,7 @@ public class TurtleTool extends AbstractTurtleUpgrade
} }
// Consume the items the block drops // Consume the items the block drops
DropConsumer.instance().set( world, blockPosition, turtleDropConsumer( turtle ) ); DropConsumer.set( world, blockPosition, turtleDropConsumer( turtle ) );
TileEntity tile = world.getTileEntity( blockPosition ); TileEntity tile = world.getTileEntity( blockPosition );
@ -257,7 +257,7 @@ public class TurtleTool extends AbstractTurtleUpgrade
private void stopConsuming( ITurtleAccess turtle ) private void stopConsuming( ITurtleAccess turtle )
{ {
List<ItemStack> extra = DropConsumer.instance().clear(); List<ItemStack> extra = DropConsumer.clear();
for( ItemStack remainder : extra ) for( ItemStack remainder : extra )
{ {
WorldUtil.dropItemStack( remainder, turtle.getWorld(), turtle.getPosition(), turtle.getDirection().getOpposite() ); WorldUtil.dropItemStack( remainder, turtle.getWorld(), turtle.getPosition(), turtle.getDirection().getOpposite() );

View File

@ -6,6 +6,7 @@
package dan200.computercraft.shared.util; package dan200.computercraft.shared.util;
import dan200.computercraft.ComputerCraft;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -15,6 +16,7 @@ import net.minecraft.world.World;
import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent; import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.world.BlockEvent; import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
@ -23,23 +25,21 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
public class DropConsumer @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID )
public final class DropConsumer
{ {
private static final DropConsumer instance = new DropConsumer(); private DropConsumer()
public static DropConsumer instance()
{ {
return instance;
} }
private Function<ItemStack, ItemStack> dropConsumer; private static Function<ItemStack, ItemStack> dropConsumer;
private List<ItemStack> remainingDrops; private static List<ItemStack> remainingDrops;
private WeakReference<World> dropWorld; private static WeakReference<World> dropWorld;
private BlockPos dropPos; private static BlockPos dropPos;
private AxisAlignedBB dropBounds; private static AxisAlignedBB dropBounds;
private WeakReference<Entity> dropEntity; private static WeakReference<Entity> dropEntity;
public void set( Entity entity, Function<ItemStack, ItemStack> consumer ) public static void set( Entity entity, Function<ItemStack, ItemStack> consumer )
{ {
dropConsumer = consumer; dropConsumer = consumer;
remainingDrops = new ArrayList<>(); remainingDrops = new ArrayList<>();
@ -51,7 +51,7 @@ public class DropConsumer
entity.captureDrops = true; entity.captureDrops = true;
} }
public void set( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer ) public static void set( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
{ {
dropConsumer = consumer; dropConsumer = consumer;
remainingDrops = new ArrayList<>(); remainingDrops = new ArrayList<>();
@ -61,7 +61,7 @@ public class DropConsumer
dropBounds = new AxisAlignedBB( pos ).grow( 2, 2, 2 ); dropBounds = new AxisAlignedBB( pos ).grow( 2, 2, 2 );
} }
public List<ItemStack> clear() public static List<ItemStack> clear()
{ {
if( dropEntity != null ) if( dropEntity != null )
{ {
@ -89,14 +89,14 @@ public class DropConsumer
return remainingStacks; return remainingStacks;
} }
private void handleDrops( ItemStack stack ) private static void handleDrops( ItemStack stack )
{ {
ItemStack remaining = dropConsumer.apply( stack ); ItemStack remaining = dropConsumer.apply( stack );
if( !remaining.isEmpty() ) remainingDrops.add( remaining ); if( !remaining.isEmpty() ) remainingDrops.add( remaining );
} }
@SubscribeEvent( priority = EventPriority.LOWEST ) @SubscribeEvent( priority = EventPriority.LOWEST )
public void onEntityLivingDrops( LivingDropsEvent event ) public static void onEntityLivingDrops( LivingDropsEvent event )
{ {
// Capture any mob drops for the current entity // Capture any mob drops for the current entity
if( dropEntity != null && event.getEntity() == dropEntity.get() ) if( dropEntity != null && event.getEntity() == dropEntity.get() )
@ -108,7 +108,7 @@ public class DropConsumer
} }
@SubscribeEvent( priority = EventPriority.LOWEST ) @SubscribeEvent( priority = EventPriority.LOWEST )
public void onHarvestDrops( BlockEvent.HarvestDropsEvent event ) public static void onHarvestDrops( BlockEvent.HarvestDropsEvent event )
{ {
// Capture block drops for the current entity // Capture block drops for the current entity
if( dropWorld != null && dropWorld.get() == event.getWorld() if( dropWorld != null && dropWorld.get() == event.getWorld()
@ -123,7 +123,7 @@ public class DropConsumer
} }
@SubscribeEvent( priority = EventPriority.LOWEST ) @SubscribeEvent( priority = EventPriority.LOWEST )
public void onEntitySpawn( EntityJoinWorldEvent event ) public static void onEntitySpawn( EntityJoinWorldEvent event )
{ {
// Capture any nearby item spawns // Capture any nearby item spawns
if( dropWorld != null && dropWorld.get() == event.getWorld() && event.getEntity() instanceof EntityItem if( dropWorld != null && dropWorld.get() == event.getWorld() && event.getEntity() instanceof EntityItem