Skip to content

Commit 0bf1356

Browse files
committed
Provide a more direct way to get the related computer
Effectively shift extracting the computer away from Plethora into CC:T. Ideally we wouldn't need this at all, but Plethora does some funky things with tick timings. See SquidDev-CC/plethora#125
1 parent 45a189e commit 0bf1356

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/main/java/dan200/computercraft/core/apis/ComputerAccess.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import dan200.computercraft.api.filesystem.IMount;
44
import dan200.computercraft.api.filesystem.IWritableMount;
55
import dan200.computercraft.api.peripheral.IComputerAccess;
6+
import dan200.computercraft.core.computer.Computer;
7+
import dan200.computercraft.core.computer.IComputerOwned;
68
import dan200.computercraft.core.filesystem.FileSystem;
79
import dan200.computercraft.core.filesystem.FileSystemException;
810

911
import javax.annotation.Nonnull;
12+
import javax.annotation.Nullable;
1013
import java.util.HashSet;
1114
import java.util.Set;
1215

13-
public abstract class ComputerAccess implements IComputerAccess
16+
public abstract class ComputerAccess implements IComputerAccess, IComputerOwned
1417
{
1518
private final IAPIEnvironment m_environment;
1619
private final Set<String> m_mounts = new HashSet<>();
@@ -133,6 +136,13 @@ public void queueEvent( @Nonnull final String event, final Object[] arguments )
133136
m_environment.queueEvent( event, arguments );
134137
}
135138

139+
@Nullable
140+
@Override
141+
public Computer getComputer()
142+
{
143+
return m_environment.getComputer();
144+
}
145+
136146
private String findFreeLocation( String desiredLoc )
137147
{
138148
try

src/main/java/dan200/computercraft/core/apis/IAPIEnvironment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
import dan200.computercraft.api.peripheral.IPeripheral;
1010
import dan200.computercraft.core.computer.Computer;
1111
import dan200.computercraft.core.computer.IComputerEnvironment;
12+
import dan200.computercraft.core.computer.IComputerOwned;
1213
import dan200.computercraft.core.filesystem.FileSystem;
1314
import dan200.computercraft.core.terminal.Terminal;
1415
import dan200.computercraft.core.tracking.TrackingField;
1516

16-
public interface IAPIEnvironment
17+
public interface IAPIEnvironment extends IComputerOwned
1718
{
1819
interface IPeripheralChangeListener
1920
{
2021
void onPeripheralChanged( int side, IPeripheral newPeripheral );
2122
}
22-
23+
24+
@Override
2325
Computer getComputer();
2426
int getComputerID();
2527
IComputerEnvironment getComputerEnvironment();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dan200.computercraft.core.computer;
2+
3+
import javax.annotation.Nullable;
4+
5+
public interface IComputerOwned
6+
{
7+
@Nullable
8+
Computer getComputer();
9+
}

src/main/java/dan200/computercraft/shared/peripheral/modem/WiredModemPeripheral.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import dan200.computercraft.api.network.wired.IWiredSender;
1111
import dan200.computercraft.api.peripheral.IComputerAccess;
1212
import dan200.computercraft.api.peripheral.IPeripheral;
13+
import dan200.computercraft.core.computer.Computer;
14+
import dan200.computercraft.core.computer.IComputerOwned;
1315
import net.minecraft.world.World;
1416

1517
import javax.annotation.Nonnull;
@@ -273,7 +275,7 @@ private Object[] callMethodRemote( String remoteName, ILuaContext context, Strin
273275
throw new LuaException( "No peripheral: " + remoteName );
274276
}
275277

276-
private static class RemotePeripheralWrapper implements IComputerAccess
278+
private static class RemotePeripheralWrapper implements IComputerAccess, IComputerOwned
277279
{
278280
private final WiredModemElement m_element;
279281
private final IPeripheral m_peripheral;
@@ -408,5 +410,12 @@ public IPeripheral getAvailablePeripheral( @Nonnull String name )
408410
return m_element.getRemotePeripherals().get( name );
409411
}
410412
}
413+
414+
@Nullable
415+
@Override
416+
public Computer getComputer()
417+
{
418+
return m_computer instanceof IComputerOwned ? ((IComputerOwned) m_computer).getComputer() : null;
419+
}
411420
}
412421
}

0 commit comments

Comments
 (0)