mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2026-09-17 06:41:22 +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:
@@ -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