mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-27 20:07:39 +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:
		| @@ -6,43 +6,40 @@ | ||||
|  | ||||
| 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.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(); | ||||
|  | ||||
|     public static FrameInfo instance() | ||||
|     { | ||||
|         return instance; | ||||
|     } | ||||
|  | ||||
|     private int tick; | ||||
|     private long renderFrame; | ||||
|     private static int tick; | ||||
|     private static long renderFrame; | ||||
|  | ||||
|     private FrameInfo() | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     public boolean getGlobalCursorBlink() | ||||
|     public static boolean getGlobalCursorBlink() | ||||
|     { | ||||
|         return (tick / 8) % 2 == 0; | ||||
|     } | ||||
|  | ||||
|     public long getRenderFrame() | ||||
|     public static long getRenderFrame() | ||||
|     { | ||||
|         return renderFrame; | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent | ||||
|     public void onTick( TickEvent.ClientTickEvent event ) | ||||
|     public static void onTick( TickEvent.ClientTickEvent event ) | ||||
|     { | ||||
|         if( event.phase == TickEvent.Phase.START ) tick++; | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent | ||||
|     public void onRenderTick( TickEvent.RenderTickEvent event ) | ||||
|     public static void onRenderTick( TickEvent.RenderTickEvent event ) | ||||
|     { | ||||
|         if( event.phase == TickEvent.Phase.START ) renderFrame++; | ||||
|     } | ||||
|   | ||||
| @@ -388,7 +388,7 @@ public class WidgetTerminal extends Widget | ||||
|                 // 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. | ||||
|                 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 th = terminal.getHeight(); | ||||
|                 int tx = terminal.getCursorX(); | ||||
|   | ||||
| @@ -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.ModelBakery; | ||||
| 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.vertex.DefaultVertexFormats; | ||||
| import net.minecraft.client.resources.IResourceManager; | ||||
| @@ -83,10 +82,16 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon | ||||
|         super.init(); | ||||
|  | ||||
|         // Setup turtle colours | ||||
|         Minecraft.getMinecraft().getItemColors().registerItemColorHandler( | ||||
|             new TurtleItemColour(), | ||||
|             ComputerCraft.Blocks.turtle, ComputerCraft.Blocks.turtleExpanded, ComputerCraft.Blocks.turtleAdvanced | ||||
|         ); | ||||
|         Minecraft.getMinecraft().getItemColors().registerItemColorHandler( ( stack, tintIndex ) -> { | ||||
|             if( tintIndex == 0 ) | ||||
|             { | ||||
|                 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 | ||||
|         ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() ); | ||||
| @@ -110,8 +115,7 @@ public class CCTurtleProxyClient extends CCTurtleProxyCommon | ||||
|  | ||||
|     private void registerForgeHandlers() | ||||
|     { | ||||
|         ForgeHandlers handlers = new ForgeHandlers(); | ||||
|         MinecraftForge.EVENT_BUS.register( handlers ); | ||||
|         MinecraftForge.EVENT_BUS.register( new 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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -8,8 +8,8 @@ package dan200.computercraft.client.proxy; | ||||
|  | ||||
| import dan200.computercraft.ComputerCraft; | ||||
| import dan200.computercraft.client.ClientTableFormatter; | ||||
| import dan200.computercraft.client.FrameInfo; | ||||
| import dan200.computercraft.client.render.*; | ||||
| import dan200.computercraft.client.render.TileEntityCableRenderer; | ||||
| import dan200.computercraft.client.render.TileEntityMonitorRenderer; | ||||
| import dan200.computercraft.shared.command.CommandCopy; | ||||
| import dan200.computercraft.shared.command.text.TableBuilder; | ||||
| 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.event.ModelRegistryEvent; | ||||
| import net.minecraftforge.client.model.ModelLoader; | ||||
| import net.minecraftforge.common.MinecraftForge; | ||||
| import net.minecraftforge.event.world.WorldEvent; | ||||
| import net.minecraftforge.fml.client.registry.ClientRegistry; | ||||
| import net.minecraftforge.fml.common.Mod; | ||||
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||||
| import net.minecraftforge.fml.relauncher.Side; | ||||
| import net.minecraftforge.fml.relauncher.SideOnly; | ||||
| @@ -50,9 +50,6 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon | ||||
|     { | ||||
|         super.preInit(); | ||||
|  | ||||
|         // Setup client forge handlers | ||||
|         registerForgeHandlers(); | ||||
|  | ||||
|         // Register any client-specific commands | ||||
|         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.diskExpanded ), ComputerCraft.Items.diskExpanded ); | ||||
|  | ||||
|         mc.getItemColors().registerItemColorHandler( ( stack, layer ) -> | ||||
|         { | ||||
|         mc.getItemColors().registerItemColorHandler( ( stack, layer ) -> { | ||||
|             switch( layer ) | ||||
|             { | ||||
|                 case 0: | ||||
| @@ -160,15 +156,6 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon | ||||
|         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 | ||||
|     public void playRecordClient( BlockPos pos, SoundEvent record, String info ) | ||||
|     { | ||||
| @@ -183,10 +170,11 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon | ||||
|         ClientTableFormatter.INSTANCE.display( table ); | ||||
|     } | ||||
|  | ||||
|     public class ForgeHandlers | ||||
|     @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT ) | ||||
|     public static class ForgeHandlers | ||||
|     { | ||||
|         @SubscribeEvent | ||||
|         public void onWorldUnload( WorldEvent.Unload event ) | ||||
|         public static void onWorldUnload( WorldEvent.Unload event ) | ||||
|         { | ||||
|             if( event.getWorld().isRemote ) | ||||
|             { | ||||
|   | ||||
| @@ -24,9 +24,9 @@ import net.minecraft.client.renderer.texture.TextureMap; | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraftforge.client.ForgeHooksClient; | ||||
| import net.minecraftforge.client.event.RenderSpecificHandEvent; | ||||
| import net.minecraftforge.fml.common.Mod; | ||||
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||||
| import net.minecraftforge.fml.relauncher.Side; | ||||
| import net.minecraftforge.fml.relauncher.SideOnly; | ||||
| import org.lwjgl.opengl.GL11; | ||||
|  | ||||
| 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 | ||||
|  */ | ||||
| @SideOnly( Side.CLIENT ) | ||||
| public class ItemPocketRenderer extends ItemMapLikeRenderer | ||||
| @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT ) | ||||
| public final class ItemPocketRenderer extends ItemMapLikeRenderer | ||||
| { | ||||
|     private static final ItemPocketRenderer INSTANCE = new ItemPocketRenderer(); | ||||
|  | ||||
|     private ItemPocketRenderer() | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent | ||||
|     public void renderItem( RenderSpecificHandEvent event ) | ||||
|     public static void renderItem( RenderSpecificHandEvent event ) | ||||
|     { | ||||
|         ItemStack stack = event.getItemStack(); | ||||
|         if( !(stack.getItem() instanceof ItemPocketComputer) ) return; | ||||
|  | ||||
|         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 | ||||
| @@ -145,7 +151,7 @@ public class ItemPocketRenderer extends ItemMapLikeRenderer | ||||
|  | ||||
|                     // And render the cursor; | ||||
|                     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 ) | ||||
|                     { | ||||
|                         TextBuffer cursorColour = new TextBuffer( "0123456789abcdef".charAt( terminal.getTextColour() ), 1 ); | ||||
|   | ||||
| @@ -12,7 +12,9 @@ import net.minecraft.client.renderer.GlStateManager; | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraftforge.client.event.RenderItemInFrameEvent; | ||||
| import net.minecraftforge.client.event.RenderSpecificHandEvent; | ||||
| import net.minecraftforge.fml.common.Mod; | ||||
| 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_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.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 | ||||
|     public void onRenderInHand( RenderSpecificHandEvent event ) | ||||
|     public static void onRenderInHand( RenderSpecificHandEvent event ) | ||||
|     { | ||||
|         ItemStack stack = event.getItemStack(); | ||||
|         if( stack.getItem() != ComputerCraft.Items.printout ) return; | ||||
|  | ||||
|         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 | ||||
| @@ -51,7 +63,7 @@ public class ItemPrintoutRenderer extends ItemMapLikeRenderer | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent | ||||
|     public void onRenderInFrame( RenderItemInFrameEvent event ) | ||||
|     public static void onRenderInFrame( RenderItemInFrameEvent event ) | ||||
|     { | ||||
|         ItemStack stack = event.getItem(); | ||||
|         if( stack.getItem() != ComputerCraft.Items.printout ) return; | ||||
| @@ -69,6 +81,7 @@ public class ItemPrintoutRenderer extends ItemMapLikeRenderer | ||||
|         drawPrintout( stack ); | ||||
|  | ||||
|         GlStateManager.enableLighting(); | ||||
|         GlStateManager.disableBlend(); | ||||
|     } | ||||
|  | ||||
|     private static void drawPrintout( ItemStack stack ) | ||||
|   | ||||
| @@ -23,17 +23,24 @@ import net.minecraft.util.math.BlockPos; | ||||
| import net.minecraft.util.math.RayTraceResult; | ||||
| import net.minecraft.world.World; | ||||
| import net.minecraftforge.client.event.DrawBlockHighlightEvent; | ||||
| import net.minecraftforge.fml.common.Mod; | ||||
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||||
| import net.minecraftforge.fml.relauncher.Side; | ||||
| 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 double MIN = CableBounds.MIN - EXPAND; | ||||
|     private static final double MAX = CableBounds.MAX + EXPAND; | ||||
|  | ||||
|     private RenderOverlayCable() | ||||
|     { | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent | ||||
|     public void drawHighlight( DrawBlockHighlightEvent event ) | ||||
|     public static void drawHighlight( DrawBlockHighlightEvent event ) | ||||
|     { | ||||
|         if( event.getTarget().typeOfHit != RayTraceResult.Type.BLOCK ) return; | ||||
|  | ||||
|   | ||||
| @@ -49,7 +49,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon | ||||
|         // 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 | ||||
|         // pass multiple times. | ||||
|         long renderFrame = FrameInfo.instance().getRenderFrame(); | ||||
|         long renderFrame = FrameInfo.getRenderFrame(); | ||||
|         if( originTerminal.lastRenderFrame == renderFrame && !monitorPos.equals( originTerminal.lastRenderPos ) ) | ||||
|         { | ||||
|             return; | ||||
| @@ -230,7 +230,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon | ||||
|                                 GlStateManager.glEndList(); | ||||
|                             } | ||||
|                         } | ||||
|                         if( FrameInfo.instance().getGlobalCursorBlink() ) | ||||
|                         if( FrameInfo.getGlobalCursorBlink() ) | ||||
|                         { | ||||
|                             GlStateManager.callList( originTerminal.renderDisplayLists[2] ); | ||||
|                             GlStateManager.resetColor(); | ||||
|   | ||||
| @@ -21,38 +21,23 @@ import dan200.computercraft.shared.turtle.items.ItemTurtleLegacy; | ||||
| import dan200.computercraft.shared.turtle.items.ItemTurtleNormal; | ||||
| import dan200.computercraft.shared.turtle.items.TurtleItemFactory; | ||||
| import dan200.computercraft.shared.turtle.upgrades.*; | ||||
| import dan200.computercraft.shared.util.DropConsumer; | ||||
| import dan200.computercraft.shared.util.ImpostorRecipe; | ||||
| import net.minecraft.block.Block; | ||||
| import net.minecraft.entity.Entity; | ||||
| import net.minecraft.init.Items; | ||||
| import net.minecraft.item.Item; | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraft.item.crafting.IRecipe; | ||||
| 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.event.RegistryEvent; | ||||
| import net.minecraftforge.fml.common.Mod; | ||||
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||||
| import net.minecraftforge.fml.common.registry.EntityRegistry; | ||||
| import net.minecraftforge.fml.common.registry.GameRegistry; | ||||
| 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 | ||||
| { | ||||
|     private Function<ItemStack, ItemStack> dropConsumer; | ||||
|     private List<ItemStack> remainingDrops; | ||||
|     private WeakReference<World> dropWorld; | ||||
|     private BlockPos dropPos; | ||||
|     private AxisAlignedBB dropBounds; | ||||
|     private WeakReference<Entity> dropEntity; | ||||
|  | ||||
|     @Override | ||||
|     public void preInit() | ||||
|     { | ||||
| @@ -67,7 +52,6 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy | ||||
|     @Override | ||||
|     public void init() | ||||
|     { | ||||
|         registerForgeHandlers(); | ||||
|         registerTileEntities(); | ||||
|     } | ||||
|  | ||||
| @@ -248,16 +232,11 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy | ||||
|         GameRegistry.registerTileEntity( TileTurtleAdvanced.class, new ResourceLocation( ComputerCraft.MOD_ID, "turtleadv" ) ); | ||||
|     } | ||||
|  | ||||
|     private void registerForgeHandlers() | ||||
|     { | ||||
|         MinecraftForge.EVENT_BUS.register( new ForgeHandlers() ); | ||||
|         MinecraftForge.EVENT_BUS.register( DropConsumer.instance() ); | ||||
|     } | ||||
|  | ||||
|     private class ForgeHandlers | ||||
|     @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID ) | ||||
|     public static class ForgeHandlers | ||||
|     { | ||||
|         @SubscribeEvent | ||||
|         public void onTurtleAction( TurtleActionEvent event ) | ||||
|         public static void onTurtleAction( TurtleActionEvent event ) | ||||
|         { | ||||
|             if( ComputerCraft.turtleDisabledActions.contains( event.getAction() ) ) | ||||
|             { | ||||
|   | ||||
| @@ -85,10 +85,10 @@ import net.minecraft.world.World; | ||||
| import net.minecraftforge.common.MinecraftForge; | ||||
| import net.minecraftforge.event.RegistryEvent; | ||||
| import net.minecraftforge.event.entity.player.PlayerContainerEvent; | ||||
| import net.minecraftforge.event.world.WorldEvent; | ||||
| import net.minecraftforge.fml.client.event.ConfigChangedEvent; | ||||
| import net.minecraftforge.fml.common.FMLCommonHandler; | ||||
| import net.minecraftforge.fml.common.Loader; | ||||
| import net.minecraftforge.fml.common.Mod; | ||||
| import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; | ||||
| import net.minecraftforge.fml.common.gameevent.TickEvent; | ||||
| import net.minecraftforge.fml.common.network.FMLNetworkEvent; | ||||
| @@ -371,9 +371,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy | ||||
|  | ||||
|     private void registerForgeHandlers() | ||||
|     { | ||||
|         ForgeHandlers handlers = new ForgeHandlers(); | ||||
|         MinecraftForge.EVENT_BUS.register( handlers ); | ||||
|         NetworkRegistry.INSTANCE.registerGuiHandler( ComputerCraft.instance, handlers ); | ||||
|         NetworkRegistry.INSTANCE.registerGuiHandler( ComputerCraft.instance, new GuiHandler() ); | ||||
|     } | ||||
|  | ||||
|     private void registerNetwork() | ||||
| @@ -421,14 +419,12 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy | ||||
|             showTableClient( packet.getTable() ) ); | ||||
|     } | ||||
|  | ||||
|     public class ForgeHandlers implements IGuiHandler | ||||
|     public class GuiHandler implements IGuiHandler | ||||
|     { | ||||
|         private ForgeHandlers() | ||||
|         private GuiHandler() | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         // IGuiHandler implementation | ||||
|  | ||||
|         @Override | ||||
|         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; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|         // Event handlers | ||||
|     @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID ) | ||||
|     public final static class ForgeHandlers | ||||
|     { | ||||
|         private ForgeHandlers() | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         @SubscribeEvent | ||||
|         public void onConnectionOpened( FMLNetworkEvent.ClientConnectedToServerEvent event ) | ||||
|         public static void onConnectionOpened( FMLNetworkEvent.ClientConnectedToServerEvent event ) | ||||
|         { | ||||
|             ComputerCraft.clientComputerRegistry.reset(); | ||||
|         } | ||||
|  | ||||
|         @SubscribeEvent | ||||
|         public void onConnectionClosed( FMLNetworkEvent.ClientDisconnectionFromServerEvent event ) | ||||
|         public static void onConnectionClosed( FMLNetworkEvent.ClientDisconnectionFromServerEvent event ) | ||||
|         { | ||||
|             ComputerCraft.clientComputerRegistry.reset(); | ||||
|         } | ||||
|  | ||||
|         @SubscribeEvent | ||||
|         public void onClientTick( TickEvent.ClientTickEvent event ) | ||||
|         public static void onClientTick( TickEvent.ClientTickEvent event ) | ||||
|         { | ||||
|             if( event.phase == TickEvent.Phase.START ) | ||||
|             { | ||||
| @@ -586,7 +588,7 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy | ||||
|         } | ||||
|  | ||||
|         @SubscribeEvent | ||||
|         public void onServerTick( TickEvent.ServerTickEvent event ) | ||||
|         public static void onServerTick( TickEvent.ServerTickEvent event ) | ||||
|         { | ||||
|             if( event.phase == TickEvent.Phase.START ) | ||||
|             { | ||||
| @@ -596,23 +598,13 @@ public abstract class ComputerCraftProxyCommon implements IComputerCraftProxy | ||||
|         } | ||||
|  | ||||
|         @SubscribeEvent | ||||
|         public void onWorldLoad( WorldEvent.Load event ) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         @SubscribeEvent | ||||
|         public void onWorldUnload( WorldEvent.Unload event ) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         @SubscribeEvent | ||||
|         public void onConfigChanged( ConfigChangedEvent.OnConfigChangedEvent event ) | ||||
|         public static void onConfigChanged( ConfigChangedEvent.OnConfigChangedEvent event ) | ||||
|         { | ||||
|             if( event.getModID().equals( ComputerCraft.MOD_ID ) ) Config.sync(); | ||||
|         } | ||||
|  | ||||
|         @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 | ||||
|             Container container = event.getContainer(); | ||||
|   | ||||
| @@ -226,7 +226,7 @@ public class TurtlePlaceCommand implements ITurtleCommand | ||||
|         // Start claiming entity drops | ||||
|         Entity hitEntity = hit.getKey(); | ||||
|         Vec3d hitPos = hit.getValue(); | ||||
|         DropConsumer.instance().set( | ||||
|         DropConsumer.set( | ||||
|             hitEntity, | ||||
|             drop -> InventoryUtil.storeItems( drop, turtle.getItemHandler(), turtle.getSelectedSlot() ) | ||||
|         ); | ||||
| @@ -266,7 +266,7 @@ public class TurtlePlaceCommand implements ITurtleCommand | ||||
|         } | ||||
|  | ||||
|         // Stop claiming drops | ||||
|         List<ItemStack> remainingDrops = DropConsumer.instance().clear(); | ||||
|         List<ItemStack> remainingDrops = DropConsumer.clear(); | ||||
|         for( ItemStack remaining : remainingDrops ) | ||||
|         { | ||||
|             WorldUtil.dropItemStack( remaining, world, position, turtle.getDirection().getOpposite() ); | ||||
|   | ||||
| @@ -135,7 +135,7 @@ public class TurtleTool extends AbstractTurtleUpgrade | ||||
|             } | ||||
|  | ||||
|             // Start claiming entity drops | ||||
|             DropConsumer.instance().set( hitEntity, turtleDropConsumer( turtle ) ); | ||||
|             DropConsumer.set( hitEntity, turtleDropConsumer( turtle ) ); | ||||
|  | ||||
|             // Attack the entity | ||||
|             boolean attacked = false; | ||||
| @@ -225,7 +225,7 @@ public class TurtleTool extends AbstractTurtleUpgrade | ||||
|         } | ||||
|  | ||||
|         // Consume the items the block drops | ||||
|         DropConsumer.instance().set( world, blockPosition, turtleDropConsumer( turtle ) ); | ||||
|         DropConsumer.set( world, blockPosition, turtleDropConsumer( turtle ) ); | ||||
|  | ||||
|         TileEntity tile = world.getTileEntity( blockPosition ); | ||||
|  | ||||
| @@ -257,7 +257,7 @@ public class TurtleTool extends AbstractTurtleUpgrade | ||||
|  | ||||
|     private void stopConsuming( ITurtleAccess turtle ) | ||||
|     { | ||||
|         List<ItemStack> extra = DropConsumer.instance().clear(); | ||||
|         List<ItemStack> extra = DropConsumer.clear(); | ||||
|         for( ItemStack remainder : extra ) | ||||
|         { | ||||
|             WorldUtil.dropItemStack( remainder, turtle.getWorld(), turtle.getPosition(), turtle.getDirection().getOpposite() ); | ||||
|   | ||||
| @@ -6,6 +6,7 @@ | ||||
|  | ||||
| package dan200.computercraft.shared.util; | ||||
|  | ||||
| import dan200.computercraft.ComputerCraft; | ||||
| import net.minecraft.entity.Entity; | ||||
| import net.minecraft.entity.item.EntityItem; | ||||
| 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.living.LivingDropsEvent; | ||||
| 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.SubscribeEvent; | ||||
|  | ||||
| @@ -23,23 +25,21 @@ import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 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(); | ||||
|  | ||||
|     public static DropConsumer instance() | ||||
|     private DropConsumer() | ||||
|     { | ||||
|         return instance; | ||||
|     } | ||||
|  | ||||
|     private Function<ItemStack, ItemStack> dropConsumer; | ||||
|     private List<ItemStack> remainingDrops; | ||||
|     private WeakReference<World> dropWorld; | ||||
|     private BlockPos dropPos; | ||||
|     private AxisAlignedBB dropBounds; | ||||
|     private WeakReference<Entity> dropEntity; | ||||
|     private static Function<ItemStack, ItemStack> dropConsumer; | ||||
|     private static List<ItemStack> remainingDrops; | ||||
|     private static WeakReference<World> dropWorld; | ||||
|     private static BlockPos dropPos; | ||||
|     private static AxisAlignedBB dropBounds; | ||||
|     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; | ||||
|         remainingDrops = new ArrayList<>(); | ||||
| @@ -51,7 +51,7 @@ public class DropConsumer | ||||
|         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; | ||||
|         remainingDrops = new ArrayList<>(); | ||||
| @@ -61,7 +61,7 @@ public class DropConsumer | ||||
|         dropBounds = new AxisAlignedBB( pos ).grow( 2, 2, 2 ); | ||||
|     } | ||||
|  | ||||
|     public List<ItemStack> clear() | ||||
|     public static List<ItemStack> clear() | ||||
|     { | ||||
|         if( dropEntity != null ) | ||||
|         { | ||||
| @@ -89,14 +89,14 @@ public class DropConsumer | ||||
|         return remainingStacks; | ||||
|     } | ||||
|  | ||||
|     private void handleDrops( ItemStack stack ) | ||||
|     private static void handleDrops( ItemStack stack ) | ||||
|     { | ||||
|         ItemStack remaining = dropConsumer.apply( stack ); | ||||
|         if( !remaining.isEmpty() ) remainingDrops.add( remaining ); | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent( priority = EventPriority.LOWEST ) | ||||
|     public void onEntityLivingDrops( LivingDropsEvent event ) | ||||
|     public static void onEntityLivingDrops( LivingDropsEvent event ) | ||||
|     { | ||||
|         // Capture any mob drops for the current entity | ||||
|         if( dropEntity != null && event.getEntity() == dropEntity.get() ) | ||||
| @@ -108,7 +108,7 @@ public class DropConsumer | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent( priority = EventPriority.LOWEST ) | ||||
|     public void onHarvestDrops( BlockEvent.HarvestDropsEvent event ) | ||||
|     public static void onHarvestDrops( BlockEvent.HarvestDropsEvent event ) | ||||
|     { | ||||
|         // Capture block drops for the current entity | ||||
|         if( dropWorld != null && dropWorld.get() == event.getWorld() | ||||
| @@ -123,7 +123,7 @@ public class DropConsumer | ||||
|     } | ||||
|  | ||||
|     @SubscribeEvent( priority = EventPriority.LOWEST ) | ||||
|     public void onEntitySpawn( EntityJoinWorldEvent event ) | ||||
|     public static void onEntitySpawn( EntityJoinWorldEvent event ) | ||||
|     { | ||||
|         // Capture any nearby item spawns | ||||
|         if( dropWorld != null && dropWorld.get() == event.getWorld() && event.getEntity() instanceof EntityItem | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 SquidDev
					SquidDev