Skip to content

Commit 0215716

Browse files
committed
refactor inventory manager
- add owner to GUI - rename addItemsToPlayer / removeItemsFromPlayer to pullItems / pushItems, and use peripheral name instead of relative inventory around the manager - allow disable items transfer method via config
1 parent eaadb46 commit 0215716

12 files changed

Lines changed: 202 additions & 124 deletions

File tree

src/main/java/de/srendi/advancedperipherals/client/screens/InventoryManagerScreen.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package de.srendi.advancedperipherals.client.screens;
22

33
import de.srendi.advancedperipherals.AdvancedPeripherals;
4+
import de.srendi.advancedperipherals.client.ClientUUIDCache;
45
import de.srendi.advancedperipherals.client.screens.base.BaseScreen;
6+
import de.srendi.advancedperipherals.common.blocks.blockentities.InventoryManagerEntity;
57
import de.srendi.advancedperipherals.common.container.InventoryManagerContainer;
8+
import net.minecraft.client.gui.Font;
9+
import net.minecraft.client.gui.GuiGraphics;
610
import net.minecraft.network.chat.Component;
711
import net.minecraft.resources.ResourceLocation;
812
import net.minecraft.world.entity.player.Inventory;
13+
import org.jetbrains.annotations.NotNull;
14+
15+
import java.util.UUID;
916

1017
public class InventoryManagerScreen extends BaseScreen<InventoryManagerContainer> {
18+
private static final ResourceLocation BACKGROUND = AdvancedPeripherals.getRL("textures/gui/inventory_manager_gui.png");
1119

1220
public InventoryManagerScreen(InventoryManagerContainer screenContainer, Inventory inv, Component titleIn) {
1321
super(screenContainer, inv, titleIn);
@@ -24,7 +32,33 @@ public int getSizeY() {
2432
}
2533

2634
@Override
27-
public ResourceLocation getTexture() {
28-
return AdvancedPeripherals.getRL("textures/gui/inventory_manager_gui.png");
35+
public ResourceLocation getBackgroundTexture() {
36+
return BACKGROUND;
37+
}
38+
39+
@Override
40+
public void render(@NotNull GuiGraphics gui, int x, int y, float partialTicks) {
41+
super.render(gui, x, y, partialTicks);
42+
InventoryManagerEntity blockEntity = (InventoryManagerEntity) this.menu.getBlockEntity();
43+
Font font = gui.minecraft.font;
44+
UUID owner = blockEntity.getOwnerUUID();
45+
46+
String textToDraw = "No Owner";
47+
48+
if (owner != null) {
49+
String username = ClientUUIDCache.getUsername(owner);
50+
if (username == null) {
51+
username = owner.toString();
52+
}
53+
textToDraw = "Owner: " + username;
54+
}
55+
gui.drawString(
56+
font,
57+
textToDraw,
58+
(getGuiLeft() + InventoryManagerContainer.SLOT_X + 8) - font.width(textToDraw) / 2,
59+
(getGuiTop() + InventoryManagerContainer.SLOT_Y) + 24,
60+
0x404040,
61+
false
62+
);
2963
}
3064
}

src/main/java/de/srendi/advancedperipherals/client/screens/KeyboardScreen.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
import de.srendi.advancedperipherals.common.network.toserver.KeyboardMouseMovePacket;
1919
import de.srendi.advancedperipherals.common.network.toserver.KeyboardMouseScrollPacket;
2020
import net.minecraft.client.KeyMapping;
21-
import net.minecraft.client.Minecraft;
2221
import net.minecraft.client.gui.GuiGraphics;
2322
import net.minecraft.client.gui.screens.Screen;
2423
import net.minecraft.client.gui.screens.inventory.MenuAccess;
2524
import net.minecraft.network.chat.Component;
2625
import net.minecraft.world.entity.player.Inventory;
2726
import net.neoforged.neoforge.network.PacketDistributor;
28-
import org.jetbrains.annotations.NotNull;
2927
import org.lwjgl.glfw.GLFW;
3028

3129
/**
@@ -63,15 +61,14 @@ public KeyboardContainer getMenu() {
6361
}
6462

6563
@Override
66-
public void renderBackground(GuiGraphics graphics, int x, int y, float partialTicks) {
64+
public void renderBackground(GuiGraphics gui, int x, int y, float partialTicks) {
6765
}
6866

6967
@Override
70-
public void render(@NotNull GuiGraphics graphics, int x, int y, float partialTicks) {
71-
super.render(graphics, x, y, partialTicks);
68+
public void render(GuiGraphics gui, int x, int y, float partialTicks) {
69+
super.render(gui, x, y, partialTicks);
7270

73-
Minecraft minecraft = Minecraft.getInstance();
74-
int screenWidth = minecraft.getWindow().getGuiScaledWidth();
71+
int screenWidth = gui.minecraft.getWindow().getGuiScaledWidth();
7572

7673
// float scale = 2f;
7774
// // Make the text a bit smaller on small screens
@@ -81,7 +78,7 @@ public void render(@NotNull GuiGraphics graphics, int x, int y, float partialTic
8178
// poseStack.scale(scale, scale, 1);
8279

8380
Component text = Component.translatable("text.advancedperipherals.keyboard.close");
84-
graphics.drawCenteredString(minecraft.font, text, screenWidth / 2, 1, 0xFFFFFF);
81+
gui.drawCenteredString(gui.minecraft.font, text, screenWidth / 2, 1, 0xFFFFFF);
8582
}
8683

8784
@Override

src/main/java/de/srendi/advancedperipherals/client/screens/base/BaseScreen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public void render(@NotNull GuiGraphics guiGraphics, int x, int y, float partial
2525

2626
@Override
2727
protected void renderBg(@NotNull GuiGraphics guiGraphics, float partialTicks, int x, int y) {
28-
guiGraphics.blit(getTexture(), getGuiLeft(), getGuiTop(), 0, 0, imageWidth, imageHeight);
28+
guiGraphics.blit(getBackgroundTexture(), getGuiLeft(), getGuiTop(), 0, 0, imageWidth, imageHeight);
2929
}
3030

3131
public abstract int getSizeX();
3232

3333
public abstract int getSizeY();
3434

35-
public abstract ResourceLocation getTexture();
35+
public abstract ResourceLocation getBackgroundTexture();
3636
}

src/main/java/de/srendi/advancedperipherals/client/smartglasses/objects/twodim/TextRenderer.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.TextObject;
44
import net.minecraft.client.DeltaTracker;
5-
import net.minecraft.client.Minecraft;
65
import net.minecraft.client.gui.GuiGraphics;
76

87
import java.util.List;
@@ -11,10 +10,9 @@ public class TextRenderer implements ITwoDObjectRenderer<TextObject> {
1110

1211
@Override
1312
public void renderBatch(List<TextObject> objects, GuiGraphics gui, DeltaTracker partialTick) {
14-
Minecraft minecraft = Minecraft.getInstance();
1513
for (TextObject text : objects) {
1614
float x = text.x;
17-
float width = minecraft.font.width(text.content);
15+
float width = gui.minecraft.font.width(text.content);
1816
if (text.center) {
1917
x -= width / 2;
2018
}
@@ -27,7 +25,7 @@ public void renderBatch(List<TextObject> objects, GuiGraphics gui, DeltaTracker
2725

2826
int color = (text.color & 0xffffff) | ((int) (Math.min(Math.max(text.opacity, 0), 1) * 0xff) << 24);
2927

30-
gui.drawString(minecraft.font, text.content, 0, 0, color, text.shadow);
28+
gui.drawString(gui.minecraft.font, text.content, 0, 0, color, text.shadow);
3129

3230
gui.pose().popPose();
3331
}

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

Lines changed: 75 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import dan200.computercraft.api.lua.LuaException;
44
import dan200.computercraft.api.lua.LuaFunction;
55
import dan200.computercraft.api.lua.MethodResult;
6-
import dan200.computercraft.api.lua.ObjectLuaTable;
6+
import dan200.computercraft.api.peripheral.IComputerAccess;
7+
import dan200.computercraft.api.peripheral.IPeripheral;
8+
import dan200.computercraft.shared.peripheral.generic.GenericPeripheral;
79
import de.srendi.advancedperipherals.common.addons.computercraft.owner.InventoryManagerOwner;
810
import de.srendi.advancedperipherals.common.blocks.blockentities.InventoryManagerEntity;
911
import de.srendi.advancedperipherals.common.configuration.APConfig;
12+
import de.srendi.advancedperipherals.common.util.EmptyLuaTable;
1013
import de.srendi.advancedperipherals.common.util.LuaConverter;
1114
import de.srendi.advancedperipherals.common.util.Pair;
1215
import de.srendi.advancedperipherals.common.util.inventory.InventoryUtil;
@@ -16,13 +19,15 @@
1619
import net.minecraft.world.entity.player.Inventory;
1720
import net.minecraft.world.entity.player.Player;
1821
import net.minecraft.world.item.ItemStack;
22+
import net.minecraft.world.level.block.entity.BlockEntity;
1923
import net.neoforged.neoforge.capabilities.Capabilities;
2024
import net.neoforged.neoforge.items.IItemHandler;
2125
import net.neoforged.neoforge.items.wrapper.PlayerInvWrapper;
2226

2327
import java.util.HashMap;
2428
import java.util.List;
2529
import java.util.Map;
30+
import java.util.Optional;
2631

2732
public class InventoryManagerPeripheral extends BasePeripheral<InventoryManagerOwner> {
2833

@@ -37,6 +42,21 @@ public boolean isEnabled() {
3742
return APConfig.PERIPHERALS_CONFIG.enableInventoryManager.get();
3843
}
3944

45+
@Override
46+
protected Map<String, Object> getPeripheralConfiguration() {
47+
Map<String, Object> configs = super.getPeripheralConfiguration();
48+
configs.put("itemsTransferEnabled", APConfig.PERIPHERALS_CONFIG.enableItemsTransfer.get());
49+
return configs;
50+
}
51+
52+
private Player getOwnerPlayerOrError() throws LuaException {
53+
Player player = owner.getOwner();
54+
if (player == null) {
55+
throw new LuaException("The Inventory Manager doesn't have a memory card or it isn't bound to a player.");
56+
}
57+
return player;
58+
}
59+
4060
@LuaFunction
4161
public final MethodResult getOwner() throws LuaException {
4262
Player player = owner.getOwner();
@@ -46,53 +66,9 @@ public final MethodResult getOwner() throws LuaException {
4666
return MethodResult.of(player.getUUID().toString(), player.getGameProfile().getName());
4767
}
4868

49-
50-
// Add the specified item to the player
51-
// The item is specified the same as with the RS/ME bridge:
52-
// {name="minecraft:enchanted_book", count=1, nbt="ae70053c97f877de546b0248b9ddf525"}
53-
@LuaFunction(mainThread = true)
54-
public final MethodResult addItemToPlayer(String invDirection, Map<?, ?> item) throws LuaException {
55-
Pair<ItemFilter, String> filter = ItemFilter.parse(new ObjectLuaTable(item));
56-
if (filter.rightPresent()) {
57-
return MethodResult.of(null, filter.right());
58-
}
59-
return addItemCommon(invDirection, filter.left());
60-
}
61-
62-
private MethodResult addItemCommon(String invDirection, ItemFilter filter) throws LuaException {
63-
Direction direction = validateSide(invDirection);
64-
65-
IItemHandler inventoryTo = new PlayerInvWrapper(getOwnerPlayerOrError().getInventory());
66-
IItemHandler inventoryFrom = getLevel().getCapability(Capabilities.ItemHandler.BLOCK, owner.getPos().relative(direction), direction.getOpposite());
67-
if (inventoryFrom == null) {
68-
return MethodResult.of(null, "INVENTORY_FROM_INVALID");
69-
}
70-
71-
// if (invSlot >= inventoryTo.getSlots() || invSlot < 0)
72-
// throw new LuaException("Inventory out of bounds " + invSlot + " (max: " + (inventoryTo.getSlots() - 1) + ")");
73-
74-
return MethodResult.of(InventoryUtil.moveItem(inventoryFrom, inventoryTo, filter));
75-
}
76-
7769
@LuaFunction(mainThread = true)
78-
public final MethodResult removeItemFromPlayer(String invDirection, Map<?, ?> item) throws LuaException {
79-
Pair<ItemFilter, String> filter = ItemFilter.parse(new ObjectLuaTable(item));
80-
if (filter.rightPresent()) {
81-
return MethodResult.of(null, filter.right());
82-
}
83-
return removeItemCommon(invDirection, filter.left());
84-
}
85-
86-
private MethodResult removeItemCommon(String invDirection, ItemFilter filter) throws LuaException {
87-
Direction direction = validateSide(invDirection);
88-
89-
IItemHandler inventoryFrom = new PlayerInvWrapper(getOwnerPlayerOrError().getInventory());
90-
IItemHandler inventoryTo = getLevel().getCapability(Capabilities.ItemHandler.BLOCK, owner.getPos().relative(direction), direction.getOpposite());
91-
if (inventoryTo == null) {
92-
return MethodResult.of(null, "INVENTORY_TO_INVALID");
93-
}
94-
95-
return MethodResult.of(InventoryUtil.moveItem(inventoryFrom, inventoryTo, filter));
70+
public final int size() throws LuaException {
71+
return getOwnerPlayerOrError().getInventory().getContainerSize();
9672
}
9773

9874
@LuaFunction(mainThread = true)
@@ -111,23 +87,47 @@ public final Map<Integer, Object> list() throws LuaException {
11187
}
11288

11389
@LuaFunction(mainThread = true)
114-
public final MethodResult listChest(String target) throws LuaException {
115-
Direction direction = validateSide(target);
90+
public final MethodResult pushItems(IComputerAccess computer, String toName, Optional<Map<?, ?>> filterTable) throws LuaException {
91+
checkAllowItemTransfers();
11692

117-
IItemHandler inventory = getLevel().getCapability(Capabilities.ItemHandler.BLOCK, owner.getPos().relative(direction), direction.getOpposite());
118-
if (inventory == null) {
119-
return MethodResult.of(null, "INVENTORY_TO_INVALID");
93+
IPeripheral toPeripheral = computer.getAvailablePeripheral(toName);
94+
if (toPeripheral == null) {
95+
throw new LuaException("Target '" + toName + "' does not exist");
96+
}
97+
IItemHandler inventoryTo = extractItemHandler(toPeripheral);
98+
if (inventoryTo == null) {
99+
throw new LuaException("Target '" + toName + "' is not an inventory");
120100
}
121101

122-
int size = inventory.getSlots();
123-
Map<Integer, Object> items = new HashMap<>(size * 4 / 3 + 1);
124-
for (int slot = 0; slot < inventory.getSlots(); slot++) {
125-
ItemStack stack = inventory.getStackInSlot(slot);
126-
if (!stack.isEmpty()) {
127-
items.put(slot + 1, LuaConverter.itemStackToLua(stack));
128-
}
102+
Pair<ItemFilter, String> filter = ItemFilter.parse(EmptyLuaTable.orEmpty(filterTable.orElse(null)));
103+
if (filter.rightPresent()) {
104+
return MethodResult.of(null, filter.right());
105+
}
106+
107+
IItemHandler inventoryFrom = new PlayerInvWrapper(getOwnerPlayerOrError().getInventory());
108+
return MethodResult.of(InventoryUtil.moveItem(inventoryFrom, inventoryTo, filter.left()));
109+
}
110+
111+
@LuaFunction(mainThread = true)
112+
public final MethodResult pullItems(IComputerAccess computer, String fromName, Optional<Map<?, ?>> filterTable) throws LuaException {
113+
checkAllowItemTransfers();
114+
115+
IPeripheral toPeripheral = computer.getAvailablePeripheral(fromName);
116+
if (toPeripheral == null) {
117+
throw new LuaException("Target '" + fromName + "' does not exist");
129118
}
130-
return MethodResult.of(items);
119+
IItemHandler inventoryFrom = extractItemHandler(toPeripheral);
120+
if (inventoryFrom == null) {
121+
throw new LuaException("Target '" + fromName + "' is not an inventory");
122+
}
123+
124+
Pair<ItemFilter, String> filter = ItemFilter.parse(EmptyLuaTable.orEmpty(filterTable.orElse(null)));
125+
if (filter.rightPresent()) {
126+
return MethodResult.of(null, filter.right());
127+
}
128+
129+
IItemHandler inventoryTo = new PlayerInvWrapper(getOwnerPlayerOrError().getInventory());
130+
return MethodResult.of(InventoryUtil.moveItem(inventoryFrom, inventoryTo, filter.left()));
131131
}
132132

133133
@LuaFunction(mainThread = true)
@@ -174,11 +174,21 @@ public final Map<String, Object> getItemInOffHand() throws LuaException {
174174
return LuaConverter.itemStackToLua(getOwnerPlayerOrError().getOffhandItem());
175175
}
176176

177-
private Player getOwnerPlayerOrError() throws LuaException {
178-
Player player = owner.getOwner();
179-
if (player == null) {
180-
throw new LuaException("The Inventory Manager doesn't have a memory card or it isn't bound to a player.");
177+
private void checkAllowItemTransfers() throws LuaException {
178+
if (!APConfig.PERIPHERALS_CONFIG.enableItemsTransfer.get()) {
179+
throw new LuaException("This function is disabled in the config [Inventory_Manager.enableItemsTransfer]. Activate it or ask admins if they can activate it.");
181180
}
182-
return player;
181+
}
182+
183+
private static IItemHandler extractItemHandler(IPeripheral peripheral) {
184+
Object target = peripheral.getTarget();
185+
if (target instanceof IItemHandler handler) {
186+
return handler;
187+
}
188+
if (target instanceof BlockEntity be) {
189+
Direction side = peripheral instanceof GenericPeripheral sided ? sided.side() : null;
190+
return be.getLevel().getCapability(Capabilities.ItemHandler.BLOCK, be.getBlockPos(), side);
191+
}
192+
return null;
183193
}
184194
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,23 @@ public PlayerDetectorPeripheral(IPocketAccess pocket) {
5050
super(PERIPHERAL_TYPE, PocketPeripheralOwner.of(pocket));
5151
}
5252

53-
private boolean isAllowedMultiDimensional() {
54-
int maxRange = MAX_RANGE;
55-
return APConfig.PERIPHERALS_CONFIG.playerDetMultiDimensional.get() && maxRange == -1;
56-
}
57-
5853
@Override
5954
public boolean isEnabled() {
6055
return APConfig.PERIPHERALS_CONFIG.enablePlayerDetector.get();
6156
}
6257

58+
@Override
59+
protected Map<String, Object> getPeripheralConfiguration() {
60+
Map<String, Object> configs = super.getPeripheralConfiguration();
61+
configs.put("playerSpyEnabled", APConfig.PERIPHERALS_CONFIG.playerSpy.get());
62+
return configs;
63+
}
64+
65+
private boolean isAllowedMultiDimensional() {
66+
int maxRange = MAX_RANGE;
67+
return APConfig.PERIPHERALS_CONFIG.playerDetMultiDimensional.get() && maxRange == -1;
68+
}
69+
6370
@LuaFunction(mainThread = true)
6471
public final String[] getOnlinePlayers() {
6572
return getPlayers()
@@ -139,7 +146,7 @@ public final boolean isPlayerInRange(int range, String username) {
139146
@LuaFunction(mainThread = true)
140147
public final Map<String, Object> getPlayer(IArguments arguments) throws LuaException {
141148
if (!APConfig.PERIPHERALS_CONFIG.playerSpy.get()) {
142-
throw new LuaException("This function is disabled in the config. Activate it or ask admins if they can activate it.");
149+
throw new LuaException("This function is disabled in the config [Player_Detector.playerSpy]. Activate it or ask admins if they can activate it.");
143150
}
144151
ServerPlayer player = getPlayer(arguments.getString(0));
145152
if (player == null) {

0 commit comments

Comments
 (0)