Skip to content

Commit 9ab72c6

Browse files
committed
1 parent 191d3b9 commit 9ab72c6

6 files changed

Lines changed: 117 additions & 45 deletions

File tree

src/main/java/de/srendi/advancedperipherals/common/blocks/base/PeripheralBlockEntity.java

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import dan200.computercraft.api.peripheral.IComputerAccess;
44
import dan200.computercraft.api.peripheral.IPeripheral;
5-
import de.srendi.advancedperipherals.AdvancedPeripherals;
65
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
6+
import de.srendi.advancedperipherals.lib.peripherals.DisabledPeripheral;
77
import de.srendi.advancedperipherals.lib.peripherals.IPeripheralTileEntity;
88
import net.minecraft.core.BlockPos;
99
import net.minecraft.core.Direction;
@@ -29,65 +29,85 @@
2929
import org.jetbrains.annotations.Nullable;
3030

3131
import java.util.Collections;
32+
import java.util.Optional;
3233

3334
public abstract class PeripheralBlockEntity<T extends BasePeripheral<?>> extends BaseContainerBlockEntity implements WorldlyContainer, MenuProvider, IPeripheralTileEntity, ICapabilityProvider {
3435
private static final String PERIPHERAL_SETTINGS_KEY = "peripheralSettings";
35-
protected CompoundTag peripheralSettings;
36+
protected CompoundTag peripheralSettings = new CompoundTag();
3637
protected NonNullList<ItemStack> items;
3738
@Nullable
38-
protected T peripheral = null;
39-
private IItemHandler itemHandler;
40-
private IFluidHandler fluidHandler;
39+
private IItemHandler itemHandler = null;
40+
private IFluidHandler fluidHandler = null;
41+
private IPeripheral peripheral = null;
4142

42-
public PeripheralBlockEntity(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, BlockState state) {
43+
protected PeripheralBlockEntity(BlockEntityType<?> tileEntityTypeIn, BlockPos pos, BlockState state) {
4344
super(tileEntityTypeIn, pos, state);
4445
if (this instanceof IInventoryBlock<?> inventoryBlock) {
4546
items = NonNullList.withSize(inventoryBlock.getInvSize(), ItemStack.EMPTY);
4647
} else {
4748
items = NonNullList.withSize(0, ItemStack.EMPTY);
4849
}
49-
peripheralSettings = new CompoundTag();
5050
}
5151

5252
@Nullable
5353
@Override
5454
public IPeripheral createPeripheralCap(@Nullable Direction side) {
55-
if (peripheral == null)
56-
// Perform later peripheral creation, because creating peripheral
57-
// on init of tile entity cause some infinity loop, if peripheral
58-
// are depend on tile entity data
59-
this.peripheral = this.createPeripheral();
60-
if (peripheral.isEnabled()) {
61-
return peripheral;
62-
} else {
63-
AdvancedPeripherals.debug(peripheral.getType() + " is disabled, you can enable it in the Configuration.");
55+
// Perform later peripheral creation, because creating peripheral
56+
// on init of tile entity cause some infinity loop, if peripheral
57+
// are depend on tile entity data
58+
if (this.peripheral == null) {
59+
// Recreate peripheral to allow CC: Tweaked correctly handle
60+
// peripheral update logic, so new peripheral and old one will be
61+
// different
62+
this.peripheral = this.createPeripheralDisable();
6463
}
65-
return null;
64+
return this.peripheral;
6665
}
6766

6867
@Nullable
6968
@Override
7069
public IFluidHandler createFluidHandlerCap(@Nullable Direction side) {
71-
if (fluidHandler == null)
72-
fluidHandler = new FluidTank(0);
73-
return fluidHandler;
70+
if (this.fluidHandler == null) {
71+
this.fluidHandler = new FluidTank(0);
72+
}
73+
return this.fluidHandler;
7474
}
7575

7676
@Nullable
7777
@Override
7878
public IItemHandler createItemHandlerCap(@Nullable Direction side) {
79-
if (itemHandler == null)
80-
itemHandler = new SidedInvWrapper(this, null);
81-
return itemHandler;
79+
if (this.itemHandler == null) {
80+
this.itemHandler = new SidedInvWrapper(this, null);
81+
}
82+
return this.itemHandler;
8283
}
8384

8485
@NotNull
8586
protected abstract T createPeripheral();
8687

88+
protected IPeripheral createPeripheralDisable() {
89+
T peripheral = this.createPeripheral();
90+
if (peripheral.isEnabled()) {
91+
return peripheral;
92+
}
93+
return new DisabledPeripheral(peripheral);
94+
}
95+
8796
public Iterable<IComputerAccess> getConnectedComputers() {
88-
if (peripheral == null) // just avoid some NPE in strange cases
89-
return Collections.emptyList();
90-
return peripheral.getConnectedComputers();
97+
return this.getPeripheralOptional().map(BasePeripheral::getConnectedComputers).orElse(Collections.emptyList());
98+
}
99+
100+
@Nullable
101+
public T getPeripheral() {
102+
IPeripheral peripheral = this.createPeripheralCap(null);
103+
if (peripheral == null || peripheral instanceof DisabledPeripheral) {
104+
return null;
105+
}
106+
return (T) peripheral;
107+
}
108+
109+
public Optional<T> getPeripheralOptional() {
110+
return Optional.ofNullable(this.getPeripheral());
91111
}
92112

93113
/*@Override
@@ -210,7 +230,6 @@ public CompoundTag getPeripheralSettings() {
210230

211231
@Override
212232
public void markSettingsChanged() {
213-
setChanged();
233+
this.setChanged();
214234
}
215235
}
216-

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/ChatBoxEntity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ protected ChatBoxPeripheral createPeripheral() {
2323

2424
@Override
2525
public <T extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<T> type) {
26-
if (peripheral != null) {
27-
peripheral.update();
28-
}
26+
this.getPeripheralOptional().ifPresent(ChatBoxPeripheral::update);
2927
}
3028
}

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/MEBridgeEntity.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ public <T extends BlockEntity> void handleTick(Level level, BlockState state, Bl
6767
mainNode.setVisualRepresentation(new ItemStack(Blocks.ME_BRIDGE.get()));
6868
mainNode.setInWorldNode(true);
6969
mainNode.create(level, getBlockPos());
70-
//peripheral can be null if `createPeripheralCap` was not called before
71-
if (peripheral == null)
72-
peripheral = createPeripheral();
73-
peripheral.setNode(mainNode);
70+
this.getPeripheralOptional().ifPresent(peripheral -> peripheral.setNode(mainNode));
7471
initialized = true;
7572
}
7673

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
11
package de.srendi.advancedperipherals.lib.peripherals;
22

3-
import dan200.computercraft.api.pocket.IPocketAccess;
4-
import de.srendi.advancedperipherals.common.addons.computercraft.owner.PocketPeripheralOwner;
3+
import dan200.computercraft.api.lua.IArguments;
4+
import dan200.computercraft.api.lua.ILuaContext;
5+
import dan200.computercraft.api.lua.LuaException;
6+
import dan200.computercraft.api.lua.LuaFunction;
7+
import dan200.computercraft.api.lua.MethodResult;
8+
import dan200.computercraft.api.peripheral.IComputerAccess;
9+
import dan200.computercraft.api.peripheral.IDynamicPeripheral;
10+
import dan200.computercraft.api.peripheral.IPeripheral;
511

6-
public class DisabledPeripheral extends BasePeripheral<PocketPeripheralOwner> {
7-
public static final DisabledPeripheral INSTANCE = new DisabledPeripheral("disabledPeripheral", null);
12+
import java.lang.reflect.Method;
13+
import java.util.stream.Stream;
814

9-
private DisabledPeripheral(String type, IPocketAccess access) {
10-
super(type, new PocketPeripheralOwner(access));
15+
public class DisabledPeripheral implements IDynamicPeripheral {
16+
private static final MethodResult TRUE_RESULT = MethodResult.of(true);
17+
18+
private final IPeripheral basePeripheral;
19+
private final String[] methods;
20+
21+
public DisabledPeripheral(IPeripheral basePeripheral) {
22+
this.basePeripheral = basePeripheral;
23+
Stream.Builder<String> builder = Stream.builder();
24+
builder.add("peripheralDisabled");
25+
for (Method method : basePeripheral.getClass().getMethods()) {
26+
LuaFunction annotation = method.getAnnotation(LuaFunction.class);
27+
if (annotation == null) {
28+
continue;
29+
}
30+
String[] names = annotation.value();
31+
if (names.length == 0) {
32+
builder.add(method.getName());
33+
} else {
34+
for (String name : names) {
35+
builder.add(name);
36+
}
37+
}
38+
}
39+
Stream<String> methodStream = builder.build();
40+
if (basePeripheral instanceof IDynamicPeripheral dynPeripheral) {
41+
methodStream = Stream.concat(methodStream, Stream.of(dynPeripheral.getMethodNames()));
42+
}
43+
this.methods = methodStream.toArray(String[]::new);
44+
}
45+
46+
@Override
47+
public String getType() {
48+
return this.basePeripheral.getType();
49+
}
50+
51+
@Override
52+
public Object getTarget() {
53+
return this.basePeripheral.getTarget();
54+
}
55+
56+
@Override
57+
public boolean equals(IPeripheral other) {
58+
return other instanceof DisabledPeripheral disabled && this.basePeripheral.equals(disabled.basePeripheral);
59+
}
60+
61+
@Override
62+
public String[] getMethodNames() {
63+
return this.methods;
1164
}
1265

1366
@Override
14-
public boolean isEnabled() {
15-
return true;
67+
public MethodResult callMethod(IComputerAccess computer, ILuaContext context, int method, IArguments arguments) throws LuaException {
68+
if (method == 0) {
69+
return TRUE_RESULT;
70+
}
71+
throw new LuaException("This peripheral is disabled, please contact server administrator if you want to use it");
1672
}
1773
}

src/main/java/de/srendi/advancedperipherals/lib/pocket/BasePocketUpgrade.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ protected BasePocketUpgrade(ResourceLocation id, ItemStack stack) {
2525
@Override
2626
public IPeripheral createPeripheral(@NotNull IPocketAccess access) {
2727
peripheral = getPeripheral(access);
28-
if (!peripheral.isEnabled()) return DisabledPeripheral.INSTANCE;
28+
if (!peripheral.isEnabled()) {
29+
return new DisabledPeripheral(peripheral);
30+
}
2931
return peripheral;
3032
}
3133
}

src/main/java/de/srendi/advancedperipherals/lib/turtle/PeripheralTurtleUpgrade.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected PeripheralTurtleUpgrade(ResourceLocation id, ItemStack item) {
3333
public IPeripheral createPeripheral(@NotNull ITurtleAccess turtle, @NotNull TurtleSide side) {
3434
T peripheral = buildPeripheral(turtle, side);
3535
if (!peripheral.isEnabled()) {
36-
return DisabledPeripheral.INSTANCE;
36+
return new DisabledPeripheral(peripheral);
3737
}
3838
return peripheral;
3939
}

0 commit comments

Comments
 (0)