1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-02 01:52:55 +00:00
SquidDev 5409d441b5 Expose peripherals as a capability
This registers IPeripheral as a capability. As a result, all (Minecraft
facing) functionality operates using LazyOptional<_>s instead.

Peripheral providers should now return a LazyOptional<IPeripheral> too.
Hopefully this will allow custom peripherals to mark themselves as
invalid (say, because a dependency has changed).

While peripheral providers are somewhat redundant, they still have their
usages. If a peripheral is applied to a large number of blocks (for
instance, all inventories) then using capabilities does incur some
memory overhead.

We also make the following changes based on the above:
 - Remove the default implementation for IWiredElement, migrating the
   definition to a common "Capabilities" class.

 - Remove IPeripheralTile - we'll exclusively use capabilities now.
   Absurdly this is the most complex change, as all TEs needed to be
   migrated too.

   I'm not 100% sure of the correctness of this changes so far - I've
   tested it pretty well, but blocks with more complex peripheral logic
   (wired/wireless modems and turtles) are still a little messy.

 - Remove the "command block" peripheral provider, attaching a
   capability instead.
2020-05-15 17:09:12 +01:00

80 lines
1.6 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2020. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.shared.computer.blocks;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nonnull;
public class ComputerPeripheral implements IPeripheral
{
private final String type;
private final ComputerProxy computer;
public ComputerPeripheral( String type, ComputerProxy computer )
{
this.type = type;
this.computer = computer;
}
@Nonnull
@Override
public String getType()
{
return type;
}
@LuaFunction
public final void turnOn()
{
computer.turnOn();
}
@LuaFunction
public final void shutdown()
{
computer.shutdown();
}
@LuaFunction
public final void reboot()
{
computer.reboot();
}
@LuaFunction
public final int getID()
{
return computer.assignID();
}
@LuaFunction
public final boolean isOn()
{
return computer.isOn();
}
@LuaFunction
public final String getLabel()
{
return computer.getLabel();
}
@Override
public boolean equals( IPeripheral other )
{
return other instanceof ComputerPeripheral && computer == ((ComputerPeripheral) other).computer;
}
@Nonnull
@Override
public Object getTarget()
{
return computer.getTile();
}
}