mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-12 10:20:28 +00:00
Move drop consumer code into a separate class
This has always been separate from turtles, so there is no reason to keep it there.
This commit is contained in:
parent
41a320e9a4
commit
f8b328a048
@ -891,21 +891,6 @@ public class ComputerCraft
|
||||
TurtleUpgrades.register( upgrade );
|
||||
}
|
||||
|
||||
public static void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
turtleProxy.setDropConsumer( entity, consumer );
|
||||
}
|
||||
|
||||
public static void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
turtleProxy.setDropConsumer( world, pos, consumer );
|
||||
}
|
||||
|
||||
public static List<ItemStack> clearDropConsumer()
|
||||
{
|
||||
return turtleProxy.clearDropConsumer();
|
||||
}
|
||||
|
||||
//region Compatibility
|
||||
@Deprecated
|
||||
public static IMedia getMedia( ItemStack stack )
|
||||
|
@ -11,12 +11,11 @@ import net.minecraftforge.fml.common.gameevent.TickEvent;
|
||||
|
||||
public class FrameInfo
|
||||
{
|
||||
private static FrameInfo instance;
|
||||
private static final FrameInfo instance = new FrameInfo();
|
||||
|
||||
public static FrameInfo instance()
|
||||
{
|
||||
if( instance != null ) return instance;
|
||||
return instance = new FrameInfo();
|
||||
return instance;
|
||||
}
|
||||
|
||||
private int tick;
|
||||
|
@ -10,15 +10,4 @@ import dan200.computercraft.shared.proxy.CCTurtleProxyCommon;
|
||||
|
||||
public class CCTurtleProxyServer extends CCTurtleProxyCommon
|
||||
{
|
||||
public CCTurtleProxyServer()
|
||||
{
|
||||
}
|
||||
|
||||
// ICCTurtleProxy implementation
|
||||
|
||||
@Override
|
||||
public void init()
|
||||
{
|
||||
super.init();
|
||||
}
|
||||
}
|
||||
|
@ -21,10 +21,10 @@ 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.entity.item.EntityItem;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@ -35,17 +35,12 @@ import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingDropsEvent;
|
||||
import net.minecraftforge.event.world.BlockEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.EventPriority;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
@ -76,59 +71,6 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
registerTileEntities();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
dropConsumer = consumer;
|
||||
remainingDrops = new ArrayList<>();
|
||||
dropEntity = new WeakReference<>( entity );
|
||||
dropWorld = new WeakReference<>( entity.world );
|
||||
dropPos = null;
|
||||
dropBounds = new AxisAlignedBB( entity.getPosition() ).grow( 2, 2, 2 );
|
||||
|
||||
entity.captureDrops = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
dropConsumer = consumer;
|
||||
remainingDrops = new ArrayList<>();
|
||||
dropEntity = null;
|
||||
dropWorld = new WeakReference<>( world );
|
||||
dropPos = pos;
|
||||
dropBounds = new AxisAlignedBB( pos ).grow( 2, 2, 2 );
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemStack> clearDropConsumer()
|
||||
{
|
||||
if( dropEntity != null )
|
||||
{
|
||||
Entity entity = dropEntity.get();
|
||||
if( entity != null )
|
||||
{
|
||||
entity.captureDrops = false;
|
||||
if( entity.capturedDrops != null )
|
||||
{
|
||||
for( EntityItem entityItem : entity.capturedDrops ) handleDrops( entityItem.getItem() );
|
||||
entity.capturedDrops.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ItemStack> remainingStacks = remainingDrops;
|
||||
|
||||
dropConsumer = null;
|
||||
remainingDrops = null;
|
||||
dropEntity = null;
|
||||
dropWorld = null;
|
||||
dropPos = null;
|
||||
dropBounds = null;
|
||||
|
||||
return remainingStacks;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void registerBlocks( RegistryEvent.Register<Block> event )
|
||||
{
|
||||
@ -308,57 +250,12 @@ public abstract class CCTurtleProxyCommon implements ICCTurtleProxy
|
||||
|
||||
private void registerForgeHandlers()
|
||||
{
|
||||
ForgeHandlers handlers = new ForgeHandlers();
|
||||
MinecraftForge.EVENT_BUS.register( handlers );
|
||||
}
|
||||
|
||||
private void handleDrops( ItemStack stack )
|
||||
{
|
||||
ItemStack remaining = dropConsumer.apply( stack );
|
||||
if( !remaining.isEmpty() ) remainingDrops.add( remaining );
|
||||
MinecraftForge.EVENT_BUS.register( new ForgeHandlers() );
|
||||
MinecraftForge.EVENT_BUS.register( DropConsumer.instance() );
|
||||
}
|
||||
|
||||
private class ForgeHandlers
|
||||
{
|
||||
@SubscribeEvent( priority = EventPriority.LOWEST )
|
||||
public void onEntityLivingDrops( LivingDropsEvent event )
|
||||
{
|
||||
// Capture any mob drops for the current entity
|
||||
if( dropEntity != null && event.getEntity() == dropEntity.get() )
|
||||
{
|
||||
List<EntityItem> drops = event.getDrops();
|
||||
for( EntityItem entityItem : drops ) handleDrops( entityItem.getItem() );
|
||||
drops.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent( priority = EventPriority.LOWEST )
|
||||
public void onHarvestDrops( BlockEvent.HarvestDropsEvent event )
|
||||
{
|
||||
// Capture block drops for the current entity
|
||||
if( dropWorld != null && dropWorld.get() == event.getWorld()
|
||||
&& dropPos != null && dropPos.equals( event.getPos() ) )
|
||||
{
|
||||
for( ItemStack item : event.getDrops() )
|
||||
{
|
||||
if( event.getWorld().rand.nextFloat() < event.getDropChance() ) handleDrops( item );
|
||||
}
|
||||
event.getDrops().clear();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent( priority = EventPriority.LOWEST )
|
||||
public void onEntitySpawn( EntityJoinWorldEvent event )
|
||||
{
|
||||
// Capture any nearby item spawns
|
||||
if( dropWorld != null && dropWorld.get() == event.getWorld() && event.getEntity() instanceof EntityItem
|
||||
&& dropBounds.contains( event.getEntity().getPositionVector() ) )
|
||||
{
|
||||
handleDrops( ((EntityItem) event.getEntity()).getItem() );
|
||||
event.setCanceled( true );
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onTurtleAction( TurtleActionEvent event )
|
||||
{
|
||||
|
@ -6,23 +6,9 @@
|
||||
|
||||
package dan200.computercraft.shared.proxy;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public interface ICCTurtleProxy
|
||||
{
|
||||
void preInit();
|
||||
|
||||
void init();
|
||||
|
||||
void setDropConsumer( Entity entity, Function<ItemStack, ItemStack> consumer );
|
||||
|
||||
void setDropConsumer( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer );
|
||||
|
||||
List<ItemStack> clearDropConsumer();
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ import dan200.computercraft.api.turtle.TurtleAnimation;
|
||||
import dan200.computercraft.api.turtle.TurtleCommandResult;
|
||||
import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
|
||||
import dan200.computercraft.shared.TurtlePermissions;
|
||||
import dan200.computercraft.shared.TurtleUpgrades;
|
||||
import dan200.computercraft.shared.util.DirectionUtil;
|
||||
import dan200.computercraft.shared.util.DropConsumer;
|
||||
import dan200.computercraft.shared.util.InventoryUtil;
|
||||
import dan200.computercraft.shared.util.WorldUtil;
|
||||
import net.minecraft.block.Block;
|
||||
@ -226,7 +226,7 @@ public class TurtlePlaceCommand implements ITurtleCommand
|
||||
// Start claiming entity drops
|
||||
Entity hitEntity = hit.getKey();
|
||||
Vec3d hitPos = hit.getValue();
|
||||
ComputerCraft.setDropConsumer(
|
||||
DropConsumer.instance().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 = ComputerCraft.clearDropConsumer();
|
||||
List<ItemStack> remainingDrops = DropConsumer.instance().clear();
|
||||
for( ItemStack remaining : remainingDrops )
|
||||
{
|
||||
WorldUtil.dropItemStack( remaining, world, position, turtle.getDirection().getOpposite() );
|
||||
|
@ -13,6 +13,7 @@ import dan200.computercraft.api.turtle.event.TurtleBlockEvent;
|
||||
import dan200.computercraft.shared.TurtlePermissions;
|
||||
import dan200.computercraft.shared.turtle.core.TurtlePlaceCommand;
|
||||
import dan200.computercraft.shared.turtle.core.TurtlePlayer;
|
||||
import dan200.computercraft.shared.util.DropConsumer;
|
||||
import dan200.computercraft.shared.util.InventoryUtil;
|
||||
import dan200.computercraft.shared.util.WorldUtil;
|
||||
import net.minecraft.block.Block;
|
||||
@ -134,7 +135,7 @@ public class TurtleTool extends AbstractTurtleUpgrade
|
||||
}
|
||||
|
||||
// Start claiming entity drops
|
||||
ComputerCraft.setDropConsumer( hitEntity, turtleDropConsumer( turtle ) );
|
||||
DropConsumer.instance().set( hitEntity, turtleDropConsumer( turtle ) );
|
||||
|
||||
// Attack the entity
|
||||
boolean attacked = false;
|
||||
@ -224,7 +225,7 @@ public class TurtleTool extends AbstractTurtleUpgrade
|
||||
}
|
||||
|
||||
// Consume the items the block drops
|
||||
ComputerCraft.setDropConsumer( world, blockPosition, turtleDropConsumer( turtle ) );
|
||||
DropConsumer.instance().set( world, blockPosition, turtleDropConsumer( turtle ) );
|
||||
|
||||
TileEntity tile = world.getTileEntity( blockPosition );
|
||||
|
||||
@ -256,7 +257,7 @@ public class TurtleTool extends AbstractTurtleUpgrade
|
||||
|
||||
private void stopConsuming( ITurtleAccess turtle )
|
||||
{
|
||||
List<ItemStack> extra = ComputerCraft.clearDropConsumer();
|
||||
List<ItemStack> extra = DropConsumer.instance().clear();
|
||||
for( ItemStack remainder : extra )
|
||||
{
|
||||
WorldUtil.dropItemStack( remainder, turtle.getWorld(), turtle.getPosition(), turtle.getDirection().getOpposite() );
|
||||
|
136
src/main/java/dan200/computercraft/shared/util/DropConsumer.java
Normal file
136
src/main/java/dan200/computercraft/shared/util/DropConsumer.java
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* This file is part of ComputerCraft - http://www.computercraft.info
|
||||
* Copyright Daniel Ratcliffe, 2011-2018. Do not distribute without permission.
|
||||
* Send enquiries to dratcliffe@gmail.com
|
||||
*/
|
||||
|
||||
package dan200.computercraft.shared.util;
|
||||
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.AxisAlignedBB;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
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.eventhandler.EventPriority;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class DropConsumer
|
||||
{
|
||||
private static final DropConsumer instance = new DropConsumer();
|
||||
|
||||
public static DropConsumer instance()
|
||||
{
|
||||
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;
|
||||
|
||||
public void set( Entity entity, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
dropConsumer = consumer;
|
||||
remainingDrops = new ArrayList<>();
|
||||
dropEntity = new WeakReference<>( entity );
|
||||
dropWorld = new WeakReference<>( entity.world );
|
||||
dropPos = null;
|
||||
dropBounds = new AxisAlignedBB( entity.getPosition() ).grow( 2, 2, 2 );
|
||||
|
||||
entity.captureDrops = true;
|
||||
}
|
||||
|
||||
public void set( World world, BlockPos pos, Function<ItemStack, ItemStack> consumer )
|
||||
{
|
||||
dropConsumer = consumer;
|
||||
remainingDrops = new ArrayList<>();
|
||||
dropEntity = null;
|
||||
dropWorld = new WeakReference<>( world );
|
||||
dropPos = pos;
|
||||
dropBounds = new AxisAlignedBB( pos ).grow( 2, 2, 2 );
|
||||
}
|
||||
|
||||
public List<ItemStack> clear()
|
||||
{
|
||||
if( dropEntity != null )
|
||||
{
|
||||
Entity entity = dropEntity.get();
|
||||
if( entity != null )
|
||||
{
|
||||
entity.captureDrops = false;
|
||||
if( entity.capturedDrops != null )
|
||||
{
|
||||
for( EntityItem entityItem : entity.capturedDrops ) handleDrops( entityItem.getItem() );
|
||||
entity.capturedDrops.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ItemStack> remainingStacks = remainingDrops;
|
||||
|
||||
dropConsumer = null;
|
||||
remainingDrops = null;
|
||||
dropEntity = null;
|
||||
dropWorld = null;
|
||||
dropPos = null;
|
||||
dropBounds = null;
|
||||
|
||||
return remainingStacks;
|
||||
}
|
||||
|
||||
private void handleDrops( ItemStack stack )
|
||||
{
|
||||
ItemStack remaining = dropConsumer.apply( stack );
|
||||
if( !remaining.isEmpty() ) remainingDrops.add( remaining );
|
||||
}
|
||||
|
||||
@SubscribeEvent( priority = EventPriority.LOWEST )
|
||||
public void onEntityLivingDrops( LivingDropsEvent event )
|
||||
{
|
||||
// Capture any mob drops for the current entity
|
||||
if( dropEntity != null && event.getEntity() == dropEntity.get() )
|
||||
{
|
||||
List<EntityItem> drops = event.getDrops();
|
||||
for( EntityItem entityItem : drops ) handleDrops( entityItem.getItem() );
|
||||
drops.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent( priority = EventPriority.LOWEST )
|
||||
public void onHarvestDrops( BlockEvent.HarvestDropsEvent event )
|
||||
{
|
||||
// Capture block drops for the current entity
|
||||
if( dropWorld != null && dropWorld.get() == event.getWorld()
|
||||
&& dropPos != null && dropPos.equals( event.getPos() ) )
|
||||
{
|
||||
for( ItemStack item : event.getDrops() )
|
||||
{
|
||||
if( event.getWorld().rand.nextFloat() < event.getDropChance() ) handleDrops( item );
|
||||
}
|
||||
event.getDrops().clear();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent( priority = EventPriority.LOWEST )
|
||||
public void onEntitySpawn( EntityJoinWorldEvent event )
|
||||
{
|
||||
// Capture any nearby item spawns
|
||||
if( dropWorld != null && dropWorld.get() == event.getWorld() && event.getEntity() instanceof EntityItem
|
||||
&& dropBounds.contains( event.getEntity().getPositionVector() ) )
|
||||
{
|
||||
handleDrops( ((EntityItem) event.getEntity()).getItem() );
|
||||
event.setCanceled( true );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user