Skip to content

Commit ea54b20

Browse files
committed
fix isVaild when peripheral disconnected
1 parent 8c5c16a commit ea54b20

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/InventoryManagerPeripheral.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
import java.util.List;
2727
import java.util.Map;
2828
import java.util.Optional;
29+
import java.util.Set;
30+
import java.util.concurrent.ConcurrentHashMap;
2931

3032
public class InventoryManagerPeripheral extends BasePeripheral<InventoryManagerOwner> {
31-
33+
public static final String PLAYER_INV_MAGIC_NAME = "@";
3234
public static final String PERIPHERAL_TYPE = "inventory_manager";
3335

36+
private final Set<IComputerAccess> computerAccesses = ConcurrentHashMap.newKeySet();
37+
3438
public InventoryManagerPeripheral(InventoryManagerEntity tileEntity) {
3539
super(PERIPHERAL_TYPE, new InventoryManagerOwner(tileEntity));
3640
}
@@ -40,13 +44,33 @@ public boolean isEnabled() {
4044
return APConfig.PERIPHERALS_CONFIG.enableInventoryManager.get();
4145
}
4246

47+
@Override
48+
public void attach(@NotNull IComputerAccess computer) {
49+
super.attach(computer);
50+
this.computerAccesses.add(computer);
51+
}
52+
53+
@Override
54+
public void detach(@NotNull IComputerAccess computer) {
55+
this.computerAccesses.remove(computer);
56+
super.detach(computer);
57+
}
58+
59+
public boolean isAccessValid(IComputerAccess access) {
60+
return this.computerAccesses.contains(access);
61+
}
62+
4363
@Override
4464
protected Map<String, Object> getPeripheralConfiguration() {
4565
Map<String, Object> configs = super.getPeripheralConfiguration();
4666
configs.put("itemsTransferEnabled", APConfig.PERIPHERALS_CONFIG.enableItemsTransfer.get());
4767
return configs;
4868
}
4969

70+
public Player getOwnerPlayer() {
71+
return owner.getOwner();
72+
}
73+
5074
private ServerPlayer getOwnerPlayerOrError() throws LuaException {
5175
Player player = owner.getOwner();
5276
if (player == null) {
@@ -108,7 +132,7 @@ public final MethodResult pullItems(IComputerAccess computer, String fromName, O
108132

109133
@LuaFunction(mainThread = true)
110134
public final PlayerStorageItemWrapper wrapStorageItem(IComputerAccess computer, int slot) throws LuaException {
111-
return PlayerStorageItemWrapper.create(computer, this.getOwnerPlayerOrError(), slot - 1);
135+
return PlayerStorageItemWrapper.create(computer, this, this.getOwnerPlayerOrError(), slot - 1);
112136
}
113137

114138
@LuaFunction(mainThread = true)
@@ -158,6 +182,9 @@ private void assertAllowItemTransfers() throws LuaException {
158182

159183
@NotNull
160184
private IItemHandler getInventoryHandler(IComputerAccess computer, String name) throws LuaException {
185+
if (name.equals(PLAYER_INV_MAGIC_NAME)) {
186+
return this.getPlayerInventory();
187+
}
161188
IPeripheral toPeripheral = computer.getAvailablePeripheral(name);
162189
if (toPeripheral == null) {
163190
throw new LuaException("Target '" + name + "' does not exist");

src/main/java/de/srendi/advancedperipherals/common/util/inventory/PlayerStorageItemWrapper.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import dan200.computercraft.api.lua.MethodResult;
66
import dan200.computercraft.api.peripheral.IComputerAccess;
77
import dan200.computercraft.api.peripheral.IPeripheral;
8+
import de.srendi.advancedperipherals.common.addons.computercraft.peripheral.InventoryManagerPeripheral;
89
import de.srendi.advancedperipherals.common.util.EmptyLuaTable;
910
import de.srendi.advancedperipherals.common.util.Pair;
1011
import net.minecraft.server.level.ServerPlayer;
@@ -21,24 +22,24 @@
2122

2223
// TODO: fluid variant?
2324
public class PlayerStorageItemWrapper {
24-
public static final String PLAYER_INV_MAGIC_NAME = "@";
25-
2625
private final IComputerAccess computer;
26+
private final WeakReference<InventoryManagerPeripheral> peripheral;
2727
private final WeakReference<ServerPlayer> player;
2828
private final int slot;
2929
private final ItemStack stack;
3030
private final IItemHandler handler;
3131

32-
protected PlayerStorageItemWrapper(IComputerAccess computer, ServerPlayer player, int slot, ItemStack stack, IItemHandler handler) {
32+
protected PlayerStorageItemWrapper(IComputerAccess computer, InventoryManagerPeripheral peripheral, ServerPlayer player, int slot, ItemStack stack, IItemHandler handler) {
3333
this.computer = computer;
34+
this.peripheral = new WeakReference<>(peripheral);
3435
this.player = new WeakReference<>(player);
3536
this.slot = slot;
3637
this.stack = stack;
3738
this.handler = handler;
3839
}
3940

4041
@Nullable
41-
public static PlayerStorageItemWrapper create(IComputerAccess computer, @NotNull ServerPlayer player, int slot) {
42+
public static PlayerStorageItemWrapper create(IComputerAccess computer, InventoryManagerPeripheral peripheral, @NotNull ServerPlayer player, int slot) {
4243
ItemStack stack = player.getInventory().getItem(slot);
4344
if (stack.isEmpty()) {
4445
return null;
@@ -47,15 +48,22 @@ public static PlayerStorageItemWrapper create(IComputerAccess computer, @NotNull
4748
if (handler == null) {
4849
return null;
4950
}
50-
return new PlayerStorageItemWrapper(computer, player, slot, stack, handler);
51+
return new PlayerStorageItemWrapper(computer, peripheral, player, slot, stack, handler);
5152
}
5253

5354
public boolean isValid() {
5455
ServerPlayer player = this.player.get();
5556
if (player == null || player.isRemoved()) {
5657
return false;
5758
}
58-
return player.getInventory().getItem(this.slot) == this.stack;
59+
if (player.getInventory().getItem(this.slot) != this.stack) {
60+
return false;
61+
}
62+
InventoryManagerPeripheral peripheral = this.peripheral.get();
63+
if (peripheral == null || !peripheral.isAccessValid(this.computer)) {
64+
return false;
65+
}
66+
return peripheral.getOwnerPlayer() == player;
5967
}
6068

6169
protected final void assertValid() throws LuaException {
@@ -96,22 +104,22 @@ public final MethodResult pushItems(String toName, Optional<Map<?, ?>> filterTab
96104
}
97105

98106
@LuaFunction(mainThread = true)
99-
public final MethodResult pullItems(String toName, Optional<Map<?, ?>> filterTable) throws LuaException {
107+
public final MethodResult pullItems(String fromName, Optional<Map<?, ?>> filterTable) throws LuaException {
100108
this.assertValid();
101109

102110
Pair<ItemFilter, String> filter = ItemFilter.parse(EmptyLuaTable.orEmpty(filterTable.orElse(null)));
103111
if (filter.rightPresent()) {
104112
return MethodResult.of(null, filter.right());
105113
}
106114

107-
IItemHandler inventoryFrom = this.getInventoryHandler(toName);
115+
IItemHandler inventoryFrom = this.getInventoryHandler(fromName);
108116

109117
return MethodResult.of(ItemUtil.moveItem(inventoryFrom, this.handler, filter.left()));
110118
}
111119

112120
@NotNull
113121
private IItemHandler getInventoryHandler(String name) throws LuaException {
114-
if (name.equals(PLAYER_INV_MAGIC_NAME)) {
122+
if (name.equals(InventoryManagerPeripheral.PLAYER_INV_MAGIC_NAME)) {
115123
ServerPlayer player = this.player.get();
116124
if (player == null || player.isRemoved()) {
117125
throw new LuaException("Storage item outdate");

0 commit comments

Comments
 (0)