1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 06:33:23 +00:00

Correctly handle capability creation & invalidation

Also make cable part detection more consistent.
This commit is contained in:
SquidDev 2019-04-09 17:29:23 +01:00
parent aa0e1883d1
commit 362dbd97ac
13 changed files with 152 additions and 88 deletions

View File

@ -43,6 +43,7 @@ private ComputerCraftAPIImpl()
{
}
@Nonnull
@Override
public String getInstalledVersion()
{
@ -110,6 +111,7 @@ public void registerPocketUpgrade( @Nonnull IPocketUpgrade upgrade )
PocketUpgrades.register( upgrade );
}
@Nonnull
@Override
public IPacketNetwork getWirelessNetwork()
{
@ -122,19 +124,18 @@ public void registerAPIFactory( @Nonnull ILuaAPIFactory factory )
ApiFactories.register( factory );
}
@Nonnull
@Override
public IWiredNode createWiredNodeForElement( @Nonnull IWiredElement element )
{
return new WiredNode( element );
}
@Nonnull
@Override
public IWiredElement getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
public LazyOptional<IWiredElement> getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
{
TileEntity tile = world.getTileEntity( pos );
if( tile == null ) return null;
LazyOptional<IWiredElement> element = tile.getCapability( CapabilityWiredElement.CAPABILITY, side );
return CapabilityWiredElement.unwrap( element );
return tile == null ? LazyOptional.empty() : tile.getCapability( CapabilityWiredElement.CAPABILITY, side );
}
}

View File

@ -24,6 +24,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -240,8 +241,8 @@ public static IWiredNode createWiredNodeForElement( @Nonnull IWiredElement eleme
* @return The element's node
* @see IWiredElement#getNode()
*/
@Nullable
public static IWiredElement getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
@Nonnull
public static LazyOptional<IWiredElement> getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
{
return getInstance().getWiredElementAt( world, pos, side );
}
@ -266,12 +267,15 @@ private static IComputerCraftAPI getInstance()
public interface IComputerCraftAPI
{
@Nonnull
String getInstalledVersion();
int createUniqueNumberedSaveDir( @Nonnull World world, @Nonnull String parentSubPath );
@Nullable
IWritableMount createSaveDirMount( @Nonnull World world, @Nonnull String subPath, long capacity );
@Nullable
IMount createResourceMount( @Nonnull String domain, @Nonnull String subPath );
void registerPeripheralProvider( @Nonnull IPeripheralProvider provider );
@ -286,12 +290,15 @@ public interface IComputerCraftAPI
void registerPocketUpgrade( @Nonnull IPocketUpgrade upgrade );
@Nonnull
IPacketNetwork getWirelessNetwork();
void registerAPIFactory( @Nonnull ILuaAPIFactory factory );
@Nonnull
IWiredNode createWiredNodeForElement( @Nonnull IWiredElement element );
IWiredElement getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side );
@Nonnull
LazyOptional<IWiredElement> getWiredElementAt( @Nonnull IBlockReader world, @Nonnull BlockPos pos, @Nonnull EnumFacing side );
}
}

View File

@ -74,7 +74,8 @@ public static void drawHighlight( DrawBlockHighlightEvent event )
double z = player.lastTickPosZ + (player.posZ - player.lastTickPosZ) * partialTicks;
VoxelShape shape = WorldUtil.isVecInside( CableShapes.getModemShape( state ), event.getTarget().hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) )
? CableShapes.getModemShape( state ) : CableShapes.getCableShape( state );
? CableShapes.getModemShape( state )
: CableShapes.getCableShape( state );
WorldRenderer.drawShape( shape, pos.getX() - x, pos.getY() - y, pos.getZ() - z, 0.0F, 0.0F, 0.0F, 0.4F );

View File

@ -11,6 +11,7 @@
import dan200.computercraft.shared.peripheral.modem.wired.CableModemVariant;
import dan200.computercraft.shared.peripheral.modem.wired.CableShapes;
import dan200.computercraft.shared.peripheral.modem.wired.TileCable;
import dan200.computercraft.shared.util.WorldUtil;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
@ -25,7 +26,6 @@
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.World;
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.MinecraftForgeClient;
@ -60,8 +60,7 @@ public void render( @Nonnull TileCable te, double x, double y, double z, float p
Block block = state.getBlock();
if( block != ComputerCraft.Blocks.cable ) return;
VoxelShape shape = CableShapes.getModemShape( state );
state = te.hasModem() && shape.getBoundingBox().grow( 0.02, 0.02, 0.02 ).contains( hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) )
state = WorldUtil.isVecInside( CableShapes.getModemShape( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) )
? block.getDefaultState().with( BlockCable.MODEM, state.get( BlockCable.MODEM ) )
: state.with( BlockCable.MODEM, CableModemVariant.None );

View File

@ -7,7 +7,6 @@
package dan200.computercraft.core.computer;
import com.google.common.base.Objects;
import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.peripheral.IWorkMonitor;
import dan200.computercraft.core.apis.IAPIEnvironment;

View File

@ -62,7 +62,7 @@ private static class MountInfo
@Nonnull
private ItemStack m_diskStack = ItemStack.EMPTY;
private final LazyOptional<IItemHandlerModifiable> m_itemCap = LazyOptional.of( () -> new InvWrapper( this ) );
private LazyOptional<IItemHandlerModifiable> itemHandlerCap;
private IMount m_diskMount = null;
private boolean m_recordQueued = false;
@ -82,6 +82,17 @@ public void destroy()
if( m_recordPlaying ) stopRecord();
}
@Override
protected void invalidateCaps()
{
super.invalidateCaps();
if( itemHandlerCap != null )
{
itemHandlerCap.invalidate();
itemHandlerCap = null;
}
}
@Override
public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ )
{
@ -540,7 +551,11 @@ private void stopRecord()
@Override
public <T> LazyOptional<T> getCapability( @Nonnull Capability<T> cap, @Nullable final EnumFacing side )
{
if( cap == ITEM_HANDLER_CAPABILITY ) return m_itemCap.cast();
if( cap == ITEM_HANDLER_CAPABILITY )
{
if( itemHandlerCap == null ) itemHandlerCap = LazyOptional.of( () -> new InvWrapper( this ) );
return itemHandlerCap.cast();
}
return super.getCapability( cap, side );
}

View File

@ -86,7 +86,7 @@ public static boolean doesConnectVisually( IBlockState state, IBlockReader world
{
if( !state.get( CABLE ) ) return false;
if( state.get( MODEM ).getFacing() == direction ) return true;
return ComputerCraftAPI.getWiredElementAt( world, pos.offset( direction ), direction.getOpposite() ) != null;
return ComputerCraftAPI.getWiredElementAt( world, pos.offset( direction ), direction.getOpposite() ).isPresent();
}
@Nonnull
@ -113,8 +113,7 @@ public boolean removedByPlayer( IBlockState state, World world, BlockPos pos, En
ItemStack item;
IBlockState newState;
VoxelShape bb = CableShapes.getModemShape( state );
if( WorldUtil.isVecInside( bb, hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
if( WorldUtil.isVecInside( CableShapes.getModemShape( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
{
newState = state.with( MODEM, CableModemVariant.None );
item = new ItemStack( ComputerCraft.Items.wiredModem );
@ -161,9 +160,7 @@ public ItemStack getPickBlock( IBlockState state, RayTraceResult hit, IBlockRead
if( modem == null ) return new ItemStack( ComputerCraft.Items.cable );
// We've a modem and cable, so try to work out which one we're interacting with
TileEntity tile = world.getTileEntity( pos );
return tile instanceof TileCable && hit != null &&
CableShapes.getModemShape( state ).getBoundingBox().contains( hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) )
return hit != null && WorldUtil.isVecInside( CableShapes.getModemShape( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) )
? new ItemStack( ComputerCraft.Items.wiredModem )
: new ItemStack( ComputerCraft.Items.cable );
@ -202,21 +199,6 @@ public IBlockState updatePostPlacement( @Nonnull IBlockState state, EnumFacing s
return getFluidState( state ).getBlockState();
}
if( side == state.get( MODEM ).getFacing() && !state.isValidPosition( world, pos ) )
{
if( !state.get( CABLE ) ) return getFluidState( state ).getBlockState();
/* TODO:
TileEntity entity = world.getTileEntity( pos );
if( entity instanceof TileCable )
{
entity.modemChanged();
entity.connectionsChanged();
}
*/
state = state.with( MODEM, CableModemVariant.None );
}
return state.with( CONNECTIONS.get( side ), doesConnectVisually( state, world, pos, side ) );
}

View File

@ -35,6 +35,7 @@
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.common.util.NonNullConsumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -90,7 +91,7 @@ protected void detachPeripheral( String name )
private boolean m_connectionsFormed = false;
private final WiredModemElement m_cable = new CableElement();
private LazyOptional<IWiredElement> m_cableCapability = LazyOptional.of( () -> m_cable );
private LazyOptional<IWiredElement> elementCap;
private final IWiredNode m_node = m_cable.getNode();
private final WiredModemPeripheral m_modem = new WiredModemPeripheral(
new ModemState( () -> TickScheduler.schedule( this ) ),
@ -113,6 +114,8 @@ public Vec3d getPosition()
}
};
private final NonNullConsumer<LazyOptional<IWiredElement>> connectedNodeChanged = x -> connectionsChanged();
public TileCable()
{
super( FACTORY );
@ -152,6 +155,17 @@ public void remove()
onRemove();
}
@Override
protected void invalidateCaps()
{
super.invalidateCaps();
if( elementCap != null )
{
elementCap.invalidate();
elementCap = null;
}
}
@Override
public void onLoad()
{
@ -322,18 +336,20 @@ void connectionsChanged()
BlockPos offset = current.offset( facing );
if( !world.isBlockLoaded( offset ) ) continue;
IWiredElement element = ComputerCraftAPI.getWiredElementAt( world, offset, facing.getOpposite() );
if( element == null ) continue;
LazyOptional<IWiredElement> element = ComputerCraftAPI.getWiredElementAt( world, offset, facing.getOpposite() );
if( !element.isPresent() ) continue;
element.addListener( connectedNodeChanged );
IWiredNode node = element.orElseThrow( NullPointerException::new ).getNode();
if( BlockCable.canConnectIn( state, facing ) )
{
// If we can connect to it then do so
m_node.connectTo( element.getNode() );
m_node.connectTo( node );
}
else if( m_node.getNetwork() == element.getNode().getNetwork() )
else if( m_node.getNetwork() == node.getNetwork() )
{
// Otherwise if we're on the same network then attempt to void it.
m_node.disconnectFrom( element.getNode() );
m_node.disconnectFrom( node );
}
}
}
@ -341,9 +357,11 @@ else if( m_node.getNetwork() == element.getNode().getNetwork() )
void modemChanged()
{
// Tell anyone who cares that the connection state has changed
// TODO: Be more restrictive about this.
m_cableCapability.invalidate();
m_cableCapability = LazyOptional.of( () -> m_cable );
if( elementCap != null )
{
elementCap.invalidate();
elementCap = null;
}
if( getWorld().isRemote ) return;
@ -405,8 +423,9 @@ public <T> LazyOptional<T> getCapability( @Nonnull Capability<T> capability, @Nu
{
if( capability == CapabilityWiredElement.CAPABILITY )
{
return !m_destroyed && BlockCable.canConnectIn( getBlockState(), facing )
? m_cableCapability.cast() : LazyOptional.empty();
if( m_destroyed || !BlockCable.canConnectIn( getBlockState(), facing ) ) return LazyOptional.empty();
if( elementCap == null ) elementCap = LazyOptional.of( () -> m_cable );
return elementCap.cast();
}
return super.getCapability( capability, facing );
@ -418,7 +437,7 @@ public IPeripheral getPeripheral( @Nonnull EnumFacing side )
return !m_destroyed && hasModem() && side == getDirection() ? m_modem : null;
}
public boolean hasCable()
boolean hasCable()
{
return getBlockState().get( BlockCable.CABLE );
}
@ -428,7 +447,7 @@ public boolean hasModem()
return getBlockState().get( BlockCable.MODEM ) != CableModemVariant.None;
}
boolean canAttachPeripheral()
private boolean canAttachPeripheral()
{
return hasCable() && hasModem();
}

View File

@ -8,7 +8,7 @@
import com.google.common.base.Objects;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.ComputerCraftAPIImpl;
import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.api.network.wired.IWiredElement;
import dan200.computercraft.api.network.wired.IWiredNode;
import dan200.computercraft.api.peripheral.IPeripheral;
@ -33,6 +33,7 @@
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.common.util.NonNullConsumer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -105,10 +106,10 @@ public Vec3d getPosition()
private final ModemState m_modemState = new ModemState( () -> TickScheduler.schedule( this ) );
private final WiredModemElement m_element = new FullElement( this );
private final LazyOptional<WiredModemElement> m_elementCap = LazyOptional.of( () -> m_element );
private LazyOptional<IWiredElement> elementCap;
private final IWiredNode m_node = m_element.getNode();
private int m_state = 0;
private final NonNullConsumer<LazyOptional<IWiredElement>> connectedNodeChanged = x -> connectionsChanged();
public TileWiredModemFull()
{
@ -143,6 +144,17 @@ public void onChunkUnloaded()
doRemove();
}
@Override
protected void invalidateCaps()
{
super.invalidateCaps();
if( elementCap != null )
{
elementCap.invalidate();
elementCap = null;
}
}
@Override
public void remove()
{
@ -275,11 +287,11 @@ private void connectionsChanged()
BlockPos offset = current.offset( facing );
if( !world.isBlockLoaded( offset ) ) continue;
IWiredElement element = ComputerCraftAPIImpl.INSTANCE.getWiredElementAt( world, offset, facing.getOpposite() );
if( element == null ) continue;
LazyOptional<IWiredElement> element = ComputerCraftAPI.getWiredElementAt( world, offset, facing.getOpposite() );
if( !element.isPresent() ) continue;
// If we can connect to it then do so
m_node.connectTo( element.getNode() );
element.addListener( connectedNodeChanged );
m_node.connectTo( element.orElseThrow( NullPointerException::new ).getNode() );
}
}
@ -350,7 +362,11 @@ private void updateConnectedPeripherals()
@Override
public <T> LazyOptional<T> getCapability( @Nonnull Capability<T> capability, @Nullable EnumFacing facing )
{
if( capability == CapabilityWiredElement.CAPABILITY ) return m_elementCap.cast();
if( capability == CapabilityWiredElement.CAPABILITY )
{
if( elementCap == null ) elementCap = LazyOptional.of( () -> m_element );
return elementCap.cast();
}
return super.getCapability( capability, facing );
}

View File

@ -8,6 +8,7 @@
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.api.peripheral.IPeripheralTile;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.peripheral.modem.ModemPeripheral;
import dan200.computercraft.shared.peripheral.modem.ModemState;
@ -22,8 +23,9 @@
import net.minecraft.world.World;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class TileWirelessModem extends TileGeneric
public class TileWirelessModem extends TileGeneric implements IPeripheralTile
{
public static final NamedBlockEntityType<TileWirelessModem> FACTORY_NORMAL = NamedBlockEntityType.create(
new ResourceLocation( ComputerCraft.MOD_ID, "wireless_modem_normal" ),
@ -146,4 +148,13 @@ private void updateBlockState()
getWorld().setBlockState( getPos(), state.with( BlockWirelessModem.ON, on ) );
}
}
@Nullable
@Override
public IPeripheral getPeripheral( @Nonnull EnumFacing side )
{
updateDirection();
return side == modemDirection ? modem : null;
}
}

View File

@ -61,8 +61,7 @@ public final class TilePrinter extends TileGeneric implements DefaultSidedInvent
ITextComponent customName;
private final NonNullList<ItemStack> m_inventory = NonNullList.withSize( 13, ItemStack.EMPTY );
private IItemHandlerModifiable m_itemHandlerAll = new InvWrapper( this );
private LazyOptional<IItemHandlerModifiable>[] m_itemHandlerSides;
private LazyOptional<IItemHandlerModifiable>[] itemHandlerCaps;
private final Terminal m_page = new Terminal( ItemPrintout.LINE_MAX_LENGTH, ItemPrintout.LINES_PER_PAGE );
private String m_pageTitle = "";
@ -79,6 +78,22 @@ public void destroy()
ejectContents();
}
@Override
protected void invalidateCaps()
{
super.invalidateCaps();
if( itemHandlerCaps != null )
{
for( int i = 0; i < itemHandlerCaps.length; i++ )
{
if( itemHandlerCaps[i] == null ) continue;
itemHandlerCaps[i].invalidate();
itemHandlerCaps[i] = null;
}
}
}
@Override
public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ )
{
@ -516,7 +531,6 @@ private void updateBlockState( boolean top, boolean bottom )
getWorld().setBlockState( getPos(), state.with( BlockPrinter.TOP, top ).with( BlockPrinter.BOTTOM, bottom ) );
}
@SuppressWarnings( { "unchecked", "rawtypes" } )
@Nonnull
@Override
@ -524,28 +538,16 @@ public <T> LazyOptional<T> getCapability( @Nonnull Capability<T> capability, @Nu
{
if( capability == ITEM_HANDLER_CAPABILITY )
{
LazyOptional<IItemHandlerModifiable>[] handlers = m_itemHandlerSides;
if( handlers == null ) handlers = m_itemHandlerSides = new LazyOptional[6];
LazyOptional<IItemHandlerModifiable>[] handlers = itemHandlerCaps;
if( handlers == null ) handlers = itemHandlerCaps = new LazyOptional[7];
LazyOptional<IItemHandlerModifiable> handler;
if( facing == null )
int index = facing == null ? 0 : 1 + facing.getIndex();
LazyOptional<IItemHandlerModifiable> handler = handlers[index];
if( handler == null )
{
int i = 6;
handler = handlers[i];
if( handler == null )
{
handler = handlers[i] = LazyOptional.of( () -> m_itemHandlerAll );
}
}
else
{
int i = facing.ordinal();
handler = handlers[i];
if( handler == null )
{
handler = handlers[i] = LazyOptional.of( () -> new SidedInvWrapper( this, facing ) );
}
handler = handlers[index] = facing == null
? LazyOptional.of( () -> new InvWrapper( this ) )
: LazyOptional.of( () -> new SidedInvWrapper( this, facing ) );
}
return handler.cast();

View File

@ -12,8 +12,8 @@
import dan200.computercraft.api.turtle.ITurtleAccess;
import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.shared.common.TileGeneric;
import dan200.computercraft.shared.computer.blocks.ComputerPeripheral;
import dan200.computercraft.shared.computer.blocks.ComputerProxy;
import dan200.computercraft.shared.computer.blocks.TileComputerBase;
@ -79,7 +79,7 @@ enum MoveState
private NonNullList<ItemStack> m_inventory;
private NonNullList<ItemStack> m_previousInventory;
private final IItemHandlerModifiable m_itemHandler = new InvWrapper( this );
private final LazyOptional<IItemHandlerModifiable> m_itemHandlerCap = LazyOptional.of( () -> m_itemHandler );
private LazyOptional<IItemHandlerModifiable> itemHandlerCap;
private boolean m_inventoryChanged;
private TurtleBrain m_brain;
private MoveState m_moveState;
@ -159,12 +159,20 @@ protected void unload()
}
}
@Override
protected void invalidateCaps()
{
super.invalidateCaps();
if( itemHandlerCap != null )
{
itemHandlerCap.invalidate();
itemHandlerCap = null;
}
}
@Override
public boolean onActivate( EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ )
{
// Request description from server
// requestTileEntityUpdate();
// Apply dye
ItemStack currentItem = player.getHeldItem( hand );
if( !currentItem.isEmpty() )
@ -613,7 +621,11 @@ public IItemHandlerModifiable getItemHandler()
@Override
public <T> LazyOptional<T> getCapability( @Nonnull Capability<T> cap, @Nullable EnumFacing side )
{
if( cap == ITEM_HANDLER_CAPABILITY ) return m_itemHandlerCap.cast();
if( cap == ITEM_HANDLER_CAPABILITY )
{
if( itemHandlerCap == null ) itemHandlerCap = LazyOptional.of( () -> new InvWrapper( this ) );
return itemHandlerCap.cast();
}
return super.getCapability( cap, side );
}
}

View File

@ -40,7 +40,7 @@ public static boolean isLiquidBlock( World world, BlockPos pos )
public static boolean isVecInside( VoxelShape shape, Vec3d vec )
{
if( shape.isEmpty() ) return false;
// return shape.contains( pos.x, pos.y, pos.z );
// AxisAlignedBB.contains, but without strict inequalities.
AxisAlignedBB bb = shape.getBoundingBox();
return vec.x >= bb.minX && vec.x <= bb.maxX && vec.y >= bb.minY && vec.y <= bb.maxY && vec.z >= bb.minZ && vec.z <= bb.maxZ;
}