From cf93b6914bc6715f0b6350a9cf70a9f83ded8945 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Jun 2025 21:15:57 -0600 Subject: [PATCH 1/3] fix keyboard does not work in multiplayer --- .../common/container/KeyboardContainer.java | 43 +++++---- .../common/data/EnUsLanguageProvider.java | 1 - .../common/items/KeyboardItem.java | 93 +++++++++---------- .../common/items/base/BaseItem.java | 13 ++- .../common/items/base/IInventoryItem.java | 5 +- .../toclient/KeyboardMouseCapturePacket.java | 3 - .../network/toserver/GlassesHotkeyPacket.java | 24 ++++- .../smartglasses/SmartGlassesComputer.java | 13 ++- .../smartglasses/modules/IModuleItem.java | 2 +- .../modules/hotkey/HotkeyModuleItem.java | 18 ++-- .../modules/keyboard/KeyboardModule.java | 44 +++++++++ .../nightvision/NightVisionModuleItem.java | 11 ++- .../modules/overlay/OverlayGlassesItem.java | 3 +- 13 files changed, 176 insertions(+), 97 deletions(-) diff --git a/src/main/java/de/srendi/advancedperipherals/common/container/KeyboardContainer.java b/src/main/java/de/srendi/advancedperipherals/common/container/KeyboardContainer.java index 7a098c5b3..06adb95e5 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/container/KeyboardContainer.java +++ b/src/main/java/de/srendi/advancedperipherals/common/container/KeyboardContainer.java @@ -22,32 +22,42 @@ public class KeyboardContainer extends BaseContainer implements ComputerMenu { + @Nullable private final ServerInputState input; private final ItemStack keyboardItem; @Nullable - private ServerComputer computer = null; + private ServerComputer computer; public KeyboardContainer(int id, Inventory inventory, BlockPos pos, Level level, ItemStack keyboardItem) { + this(id, inventory, pos, level, keyboardItem, null); + } + + public KeyboardContainer(int id, Inventory inventory, BlockPos pos, Level level, ItemStack keyboardItem, ServerComputer computer) { super(APContainerTypes.KEYBOARD_CONTAINER.get(), id, inventory, pos, level); - this.input = new ServerInputState<>(this); this.keyboardItem = keyboardItem; + if (level.isClientSide) { + this.input = null; + this.computer = null; + return; + } + this.input = new ServerInputState<>(this); + this.computer = computer; + if (computer != null) { + return; + } CompoundTag data = keyboardItem.getOrCreateTag(); - - if (!data.getBoolean(KeyboardItem.BOUND_TYPE_TAG)) { - // Cannot use instance ID here since they will change after reload the block - int computerId = keyboardItem.getOrCreateTag().getInt(KeyboardItem.BIND_TAG); - - for (ServerComputer computer : ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers()) { - if (computer.getID() == computerId) { - this.computer = computer; - break; - } + if (!data.contains(KeyboardItem.BIND_TAG)) { + return; + } + // Cannot use instance ID here since they will change after reload the block + int computerId = data.getInt(KeyboardItem.BIND_TAG); + for (ServerComputer computr : ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers()) { + if (computr.getID() == computerId) { + this.computer = computr; + break; } - } else if (data.contains(KeyboardItem.GLASSES_BIND_TAG)) { - computer = ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().get(data.getInt(KeyboardItem.GLASSES_BIND_TAG)); } - } public ItemStack getKeyboardItem() { @@ -55,7 +65,7 @@ public ItemStack getKeyboardItem() { } @Override - public boolean stillValid(@NotNull Player playerIn) { + public boolean stillValid(@NotNull Player player) { return true; } @@ -73,6 +83,7 @@ public ServerComputer getComputer() { return computer; } + @Nullable @Override public ServerInputHandler getInput() { return input; diff --git a/src/main/java/de/srendi/advancedperipherals/common/data/EnUsLanguageProvider.java b/src/main/java/de/srendi/advancedperipherals/common/data/EnUsLanguageProvider.java index ab443c42c..4b69cd179 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/data/EnUsLanguageProvider.java +++ b/src/main/java/de/srendi/advancedperipherals/common/data/EnUsLanguageProvider.java @@ -149,7 +149,6 @@ private void addTooltips() { addTooltip(APItems.COMPUTER_TOOL.get(), "&7This tool was made to tune our blocks. But for now, it's just a blue useless wrench."); addTooltip(APItems.MEMORY_CARD.get(), "&7Can save the rights of a player to use it in an inventory manager."); addTooltip("binding.bound_to", "&7Bound to &b%s&7."); - addTooltip("binding.bound_to_glasses", "&7Bound to Glasses with id &b%s&7."); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java b/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java index 2a875f1f7..f1ec4340a 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java @@ -1,20 +1,23 @@ package de.srendi.advancedperipherals.common.items; import dan200.computercraft.shared.computer.blocks.TileComputerBase; +import dan200.computercraft.shared.computer.core.ServerComputer; import de.srendi.advancedperipherals.client.KeyBindings; import de.srendi.advancedperipherals.common.container.KeyboardContainer; import de.srendi.advancedperipherals.common.items.base.BaseItem; import de.srendi.advancedperipherals.common.items.base.IInventoryItem; import de.srendi.advancedperipherals.common.network.APNetworking; -import de.srendi.advancedperipherals.common.network.toclient.KeyboardMouseCapturePacket; +import de.srendi.advancedperipherals.common.network.toserver.GlassesHotkeyPacket; import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess; import de.srendi.advancedperipherals.common.smartglasses.modules.IModule; import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleItem; import de.srendi.advancedperipherals.common.smartglasses.modules.keyboard.KeyboardModule; import de.srendi.advancedperipherals.common.util.EnumColor; import de.srendi.advancedperipherals.common.util.SideHelper; +import net.minecraft.client.player.LocalPlayer; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; @@ -30,7 +33,6 @@ import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; -import net.minecraftforge.network.NetworkHooks; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -39,8 +41,7 @@ public class KeyboardItem extends BaseItem implements IInventoryItem, IModuleItem { public static final String BIND_TAG = "bind"; - public static final String GLASSES_BIND_TAG = "glasses_id"; - public static final String BOUND_TYPE_TAG = "bind_type"; + public static final String OPENING_TAG = "KeyboardOpening"; public KeyboardItem() { super(new Properties().stacksTo(1)); @@ -75,48 +76,27 @@ public InteractionResult useOn(UseOnContext context) { @Override public void inventoryTick(ItemStack itemStack, Level level, Entity entity, int inventorySlot, boolean isCurrentItem, @Nullable SmartGlassesAccess access, @Nullable IModule module) { - if (level.isClientSide()) { + if (!level.isClientSide()) { + itemStack.removeTagKey(BIND_TAG); return; } - - if (access == null || !(module instanceof KeyboardModule keyboadModule)) { + if (!(entity instanceof LocalPlayer player)) { return; } - + boolean pressed = KeyBindings.GLASSES_HOTKEY_KEYBINDING.isDown(); CompoundTag data = itemStack.getOrCreateTag(); - int instanceId = access.getComputer().getInstanceID(); - int oldInstanceId = -1; - - if (data.contains(GLASSES_BIND_TAG)) { - oldInstanceId = data.getInt(GLASSES_BIND_TAG); - } - - if (!data.contains(BOUND_TYPE_TAG) || ((oldInstanceId != -1 && oldInstanceId != instanceId)) || !data.getBoolean(BOUND_TYPE_TAG)) { - data.putBoolean(BOUND_TYPE_TAG, true); - data.putInt(GLASSES_BIND_TAG, access.getComputer().getInstanceID()); - data.remove(BIND_TAG); - } - - if (!(entity instanceof ServerPlayer serverPlayer)) { + if (data.getBoolean(OPENING_TAG) == pressed) { return; } - // TODO: this for sure won't work on dedicated server - if (!KeyBindings.GLASSES_HOTKEY_KEYBINDING.isDown()) { + data.putBoolean(OPENING_TAG, pressed); + if (!pressed) { return; } - - access.getComputer().queueEvent("keyboard_open"); - if (serverPlayer.containerMenu instanceof KeyboardContainer openedKeyboard && openedKeyboard.getKeyboardItem() == itemStack) { + if (player.containerMenu instanceof KeyboardContainer openedKeyboard && openedKeyboard.getKeyboardItem().equals(itemStack)) { return; } - - NetworkHooks.openScreen(serverPlayer, this.createContainer(serverPlayer, itemStack), buf -> { - buf.writeBlockPos(serverPlayer.blockPosition()); - buf.writeItem(itemStack); - }); - if (keyboadModule.isCapturingMouse()) { - APNetworking.sendTo(new KeyboardMouseCapturePacket(true), serverPlayer); - } + APNetworking.sendToServer(new GlassesHotkeyPacket("", -1)); + return; } @Override @@ -128,7 +108,8 @@ public InteractionResultHolder use(Level worldIn, Player playerIn, In if (playerIn.isShiftKeyDown()) { return new InteractionResultHolder<>(InteractionResult.PASS, playerIn.getItemInHand(handIn)); } - if (!playerIn.getItemInHand(handIn).getOrCreateTag().contains(BIND_TAG)) { + CompoundTag data = playerIn.getItemInHand(handIn).getTag(); + if (data == null || !data.contains(BIND_TAG)) { playerIn.displayClientMessage(EnumColor.buildTextComponent(Component.translatable("text.advancedperipherals.keyboard_notbound")), false); return new InteractionResultHolder<>(InteractionResult.PASS, playerIn.getItemInHand(handIn)); } @@ -140,20 +121,14 @@ public InteractionResultHolder use(Level worldIn, Player playerIn, In public void appendHoverText(ItemStack stack, @Nullable Level levelIn, List tooltip, TooltipFlag flagIn) { super.appendHoverText(stack, levelIn, tooltip, flagIn); CompoundTag data = stack.getOrCreateTag(); - if (data.contains(BOUND_TYPE_TAG) && !data.getBoolean(BOUND_TYPE_TAG)) { - if (data.contains(BIND_TAG)) { - tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.bound_to", data.getInt(BIND_TAG)))); - } - } else { - if (data.contains(GLASSES_BIND_TAG)) { - tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.bound_to_glasses", data.getInt(GLASSES_BIND_TAG)))); - } + if (data.contains(BIND_TAG)) { + tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.bound_to", data.getInt(BIND_TAG)))); } } private void bind(Player player, ItemStack itemStack, Level world, BlockPos pos) { CompoundTag data = itemStack.getOrCreateTag(); - data.putBoolean(BOUND_TYPE_TAG, false); + data.remove(BIND_TAG); if (!(world.getBlockEntity(pos) instanceof TileComputerBase computer)) { // TODO: should it show bind failed message? @@ -173,7 +148,6 @@ private void bind(Player player, ItemStack itemStack, Level world, BlockPos pos) private void clear(Player player, ItemStack itemStack) { CompoundTag data = itemStack.getOrCreateTag(); data.remove(BIND_TAG); - data.putBoolean(BOUND_TYPE_TAG, false); player.displayClientMessage(EnumColor.buildTextComponent(Component.translatable("text.advancedperipherals.cleared_keyboard")), true); } @@ -184,7 +158,7 @@ public MenuProvider createContainer(Player playerEntity, ItemStack itemStack) { @NotNull @Override public Component getDisplayName() { - return Component.literal(""); + return Component.empty(); } @Override @@ -194,8 +168,29 @@ public AbstractContainerMenu createMenu(int pContainerId, @NotNull Inventory pla }; } + public MenuProvider createContainerWithComputer(Player playerEntity, ItemStack itemStack, ServerComputer computer) { + return new MenuProvider() { + @NotNull + @Override + public Component getDisplayName() { + return Component.empty(); + } + + @Override + public AbstractContainerMenu createMenu(int pContainerId, @NotNull Inventory playerInv, @NotNull Player player) { + return new KeyboardContainer(pContainerId, playerInv, player.blockPosition(), player.getLevel(), itemStack, computer); + } + }; + } + + @Override + public void writeContainerData(Player player, ItemStack stack, FriendlyByteBuf buf) { + buf.writeBlockPos(player.blockPosition()); + buf.writeItem(stack); + } + @Override - public IModule createModule(SmartGlassesAccess access) { - return new KeyboardModule(); + public IModule createModule(SmartGlassesAccess access, ItemStack stack) { + return new KeyboardModule(this, stack); } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/items/base/BaseItem.java b/src/main/java/de/srendi/advancedperipherals/common/items/base/BaseItem.java index e2a7018c0..7f3743614 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/items/base/BaseItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/items/base/BaseItem.java @@ -34,15 +34,14 @@ public BaseItem() { @Override public InteractionResultHolder use(Level worldIn, Player playerIn, InteractionHand handIn) { - if (worldIn.isClientSide) - return new InteractionResultHolder<>(InteractionResult.PASS, playerIn.getItemInHand(handIn)); + ItemStack stack = playerIn.getItemInHand(handIn); + if (worldIn.isClientSide) { + return new InteractionResultHolder<>(InteractionResult.PASS, stack); + } if (this instanceof IInventoryItem inventoryItem) { ServerPlayer serverPlayerEntity = (ServerPlayer) playerIn; - ItemStack stack = playerIn.getItemInHand(handIn); - NetworkHooks.openScreen(serverPlayerEntity, inventoryItem.createContainer(playerIn, stack), buf -> { - buf.writeBlockPos(playerIn.blockPosition()); - buf.writeItem(stack); - }); + NetworkHooks.openScreen(serverPlayerEntity, inventoryItem.createContainer(playerIn, stack), buf -> inventoryItem.writeContainerData(playerIn, stack, buf)); + return new InteractionResultHolder<>(InteractionResult.SUCCESS, stack); } return super.use(worldIn, playerIn, handIn); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/items/base/IInventoryItem.java b/src/main/java/de/srendi/advancedperipherals/common/items/base/IInventoryItem.java index 7d4ff67f6..1897596e9 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/items/base/IInventoryItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/items/base/IInventoryItem.java @@ -1,11 +1,12 @@ package de.srendi.advancedperipherals.common.items.base; +import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; public interface IInventoryItem { + MenuProvider createContainer(Player player, ItemStack itemStack); - MenuProvider createContainer(Player playerEntity, ItemStack itemStack); - + void writeContainerData(Player player, ItemStack itemStack, FriendlyByteBuf buf); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/network/toclient/KeyboardMouseCapturePacket.java b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/KeyboardMouseCapturePacket.java index f6e0d53ee..026f24151 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/network/toclient/KeyboardMouseCapturePacket.java +++ b/src/main/java/de/srendi/advancedperipherals/common/network/toclient/KeyboardMouseCapturePacket.java @@ -4,8 +4,6 @@ import de.srendi.advancedperipherals.common.network.base.IPacket; import net.minecraft.client.Minecraft; import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.api.distmarker.Dist; -import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.network.NetworkEvent; public class KeyboardMouseCapturePacket implements IPacket { @@ -16,7 +14,6 @@ public KeyboardMouseCapturePacket(boolean enable) { this.enable = enable; } - @OnlyIn(Dist.CLIENT) @Override public void handle(NetworkEvent.Context context) { if (!(Minecraft.getInstance().screen instanceof KeyboardScreen screen)) { diff --git a/src/main/java/de/srendi/advancedperipherals/common/network/toserver/GlassesHotkeyPacket.java b/src/main/java/de/srendi/advancedperipherals/common/network/toserver/GlassesHotkeyPacket.java index 0f31cd4ea..6892e9eee 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/network/toserver/GlassesHotkeyPacket.java +++ b/src/main/java/de/srendi/advancedperipherals/common/network/toserver/GlassesHotkeyPacket.java @@ -2,7 +2,9 @@ import de.srendi.advancedperipherals.common.items.SmartGlassesItem; import de.srendi.advancedperipherals.common.network.base.IPacket; +import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess; import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesComputer; +import de.srendi.advancedperipherals.common.smartglasses.modules.keyboard.KeyboardModule; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; @@ -29,15 +31,33 @@ public void handle(NetworkEvent.Context context) { return; } + ItemStack smartGlasses = null; + SmartGlassesComputer computer = null; for (ItemStack stack : serverPlayer.getAllSlots()) { if (stack.getItem() instanceof SmartGlassesItem) { - SmartGlassesComputer computer = SmartGlassesItem.getServerComputer(server, stack); + computer = SmartGlassesItem.getServerComputer(server, stack); if (computer != null) { - computer.queueEvent("glasses_key_pressed", new Object[]{keyBind, keyPressDuration}); + smartGlasses = stack; break; } } } + if (computer == null) { + return; + } + if (keyPressDuration >= 0) { + computer.queueEvent("glasses_key_pressed", new Object[]{keyBind, keyPressDuration}); + return; + } + SmartGlassesAccess glasses = computer.getSmartGlassesAccess(); + computer.getModules().values() + .stream() + .filter(KeyboardModule.class::isInstance) + .map(KeyboardModule.class::cast) + .findFirst() + .ifPresent((keyboardModule) -> { + keyboardModule.openKeyboard(glasses); + }); } @Override diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/SmartGlassesComputer.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/SmartGlassesComputer.java index 2880f55f4..cf817da89 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/SmartGlassesComputer.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/SmartGlassesComputer.java @@ -187,12 +187,15 @@ private void updatePeripheralsAndModules(SmartGlassesItemHandler itemHandler) { } this.upgrades = upgradesBuilder.build(); for (int slot = SmartGlassesItemHandler.PERIPHERAL_SLOTS; slot < SmartGlassesItemHandler.SLOTS; slot++) { - ItemStack peripheralItem = itemHandler.getStackInSlot(slot); + ItemStack moduleItem = itemHandler.getStackInSlot(slot); IModule oldModule = modules.get(slot); - if (!peripheralItem.isEmpty() && peripheralItem.getItem() instanceof IModuleItem module) { - IModule newModule = module.createModule(smartGlassesAccess); - if (oldModule != null && oldModule.getName().equals(newModule.getName())) { - continue; + if (!moduleItem.isEmpty() && moduleItem.getItem() instanceof IModuleItem module) { + IModule newModule = module.createModule(smartGlassesAccess, moduleItem); + if (oldModule != null) { + if (oldModule.getName().equals(newModule.getName())) { + continue; + } + oldModule.onUnequipped(smartGlassesAccess); } modules.put(slot, newModule); } else if (oldModule != null) { diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/IModuleItem.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/IModuleItem.java index bed3560a2..b0d4975f6 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/IModuleItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/IModuleItem.java @@ -8,7 +8,7 @@ public interface IModuleItem { - IModule createModule(SmartGlassesAccess access); + IModule createModule(SmartGlassesAccess access, ItemStack stack); /** * This method is called every tick the item is in the inventory of the smart glasses diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/hotkey/HotkeyModuleItem.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/hotkey/HotkeyModuleItem.java index ad0169382..6763be02e 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/hotkey/HotkeyModuleItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/hotkey/HotkeyModuleItem.java @@ -8,8 +8,8 @@ import de.srendi.advancedperipherals.common.smartglasses.modules.IModule; import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleItem; import de.srendi.advancedperipherals.common.util.KeybindUtil; +import net.minecraft.client.player.LocalPlayer; import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; @@ -24,23 +24,25 @@ public boolean isEnabled() { } @Override - public IModule createModule(SmartGlassesAccess access) { + public IModule createModule(SmartGlassesAccess access, ItemStack stack) { return new HotkeyModule(); } @Override public void inventoryTick(@NotNull ItemStack stack, @NotNull Level level, @NotNull Entity entity, int slot, boolean isSelected) { - if (!level.isClientSide() || !(entity instanceof Player player)) + if (!level.isClientSide() || !(entity instanceof LocalPlayer)) { return; + } if (KeybindUtil.isKeyPressed(KeyBindings.GLASSES_HOTKEY_KEYBINDING)) { // Add another 50ms to the duration, one tick setKeyPressDuration(stack, getKeyPressDuration(stack) + 50); - } else if (getKeyPressDuration(stack) > 0) { - // If the key is not pressed, but the duration is greater than 0, we can assume that the key was pressed - // We can now post the event - - int duration = getKeyPressDuration(stack); + return; + } + int duration = getKeyPressDuration(stack); + // If the key is not pressed, but the duration is greater than 0, we can assume that the key was pressed + // We can now post the event + if (duration > 0) { setKeyPressDuration(stack, 0); String keyBind = KeyBindings.GLASSES_HOTKEY_KEYBINDING.getKey().getName(); diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/keyboard/KeyboardModule.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/keyboard/KeyboardModule.java index b7a02ea6b..a839c046e 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/keyboard/KeyboardModule.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/keyboard/KeyboardModule.java @@ -1,20 +1,32 @@ package de.srendi.advancedperipherals.common.smartglasses.modules.keyboard; import de.srendi.advancedperipherals.AdvancedPeripherals; +import de.srendi.advancedperipherals.common.container.KeyboardContainer; +import de.srendi.advancedperipherals.common.items.KeyboardItem; import de.srendi.advancedperipherals.common.network.APNetworking; import de.srendi.advancedperipherals.common.network.toclient.KeyboardMouseCapturePacket; import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess; +import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesComputer; import de.srendi.advancedperipherals.common.smartglasses.modules.IModule; import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.network.NetworkHooks; import org.jetbrains.annotations.Nullable; public class KeyboardModule implements IModule { + private final KeyboardItem keyboardItem; + private final ItemStack keyboardItemStack; private boolean lastCaptureMouse = false; private volatile boolean captureMouse = false; + public KeyboardModule(KeyboardItem keyboardItem, ItemStack stack) { + this.keyboardItem = keyboardItem; + this.keyboardItemStack = stack; + } + @Override public ResourceLocation getName() { return AdvancedPeripherals.getRL("keyboard"); @@ -48,4 +60,36 @@ public void tick(SmartGlassesAccess glasses) { APNetworking.sendTo(new KeyboardMouseCapturePacket(captureMouse), player); } } + + @Override + public void onUnequipped(SmartGlassesAccess glasses) { + if (!(glasses.getEntity() instanceof ServerPlayer player)) { + return; + } + if (!(player.containerMenu instanceof KeyboardContainer keyboard)) { + return; + } + if (keyboard.getKeyboardItem() == this.keyboardItemStack) { + player.closeContainer(); + } + } + + public void openKeyboard(SmartGlassesAccess glasses) { + if (!(glasses.getEntity() instanceof ServerPlayer player)) { + return; + } + SmartGlassesComputer computer = glasses.getComputer(); + ItemStack stack = computer.getStack(); + + stack.getOrCreateTag().putBoolean(KeyboardItem.OPENING_TAG, true); + computer.queueEvent("keyboard_open"); + + KeyboardItem keyboardItem = this.keyboardItem; + NetworkHooks.openScreen(player, keyboardItem.createContainerWithComputer(player, stack, computer), buf -> keyboardItem.writeContainerData(player, stack, buf)); + boolean captureMouse = this.captureMouse; + this.lastCaptureMouse = captureMouse; + if (captureMouse) { + APNetworking.sendTo(new KeyboardMouseCapturePacket(true), player); + } + } } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/nightvision/NightVisionModuleItem.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/nightvision/NightVisionModuleItem.java index 47f5bdc23..825bab885 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/nightvision/NightVisionModuleItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/nightvision/NightVisionModuleItem.java @@ -20,7 +20,7 @@ public boolean isEnabled() { } @Override - public IModule createModule(SmartGlassesAccess access) { + public IModule createModule(SmartGlassesAccess access, ItemStack stack) { return new NightVisionModule(); } @@ -34,7 +34,14 @@ public void inventoryTick(ItemStack itemStack, Level level, Entity entity, int i return; } if (nightVisionModule.isNightVisionEnabled()) { - player.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION, 20 * 13 - 1 /* minus 1 tick then the client timing won't flash */)); + player.addEffect(new MobEffectInstance( + MobEffects.NIGHT_VISION, + 20 * 13 - 1, /* minus 1 tick then the client timing won't flash */ + 0, + false, + false, + true + )); } else { player.removeEffect(MobEffects.NIGHT_VISION); } diff --git a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesItem.java b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesItem.java index 20a3bd78b..46126c53f 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/overlay/OverlayGlassesItem.java @@ -4,6 +4,7 @@ import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess; import de.srendi.advancedperipherals.common.smartglasses.modules.IModule; import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleItem; +import net.minecraft.world.item.ItemStack; public class OverlayGlassesItem extends BaseItem implements IModuleItem { @@ -13,7 +14,7 @@ public boolean isEnabled() { } @Override - public IModule createModule(SmartGlassesAccess smartGlassesAccess) { + public IModule createModule(SmartGlassesAccess smartGlassesAccess, ItemStack stack) { return new OverlayModule(smartGlassesAccess); } } From 0e9029442d07d77003e28c836630b9f1d8b29bad Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Jun 2025 21:16:17 -0600 Subject: [PATCH 2/3] fix keyboard_close fires with delay --- .../client/screens/KeyboardScreen.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/srendi/advancedperipherals/client/screens/KeyboardScreen.java b/src/main/java/de/srendi/advancedperipherals/client/screens/KeyboardScreen.java index 58204bbe2..669a3e068 100644 --- a/src/main/java/de/srendi/advancedperipherals/client/screens/KeyboardScreen.java +++ b/src/main/java/de/srendi/advancedperipherals/client/screens/KeyboardScreen.java @@ -95,6 +95,9 @@ public final void removed() { return; } super.removed(); + if (this.minecraft.player != null) { + this.keyboardContainer.removed(this.minecraft.player); + } this.minecraft.keyboardHandler.setSendRepeatsToGui(false); } @@ -167,7 +170,11 @@ public boolean mouseScrolled(double x, double y, double direction) { @Override public final boolean keyPressed(int key, int scancode, int modifiers) { if (key == GLFW.GLFW_KEY_ESCAPE) { - super.onClose(); + if (this.minecraft.player != null) { + this.minecraft.player.closeContainer(); + } else { + super.onClose(); + } return true; } // Forward the tab key to the terminal, rather than moving between controls. From b471dd375037905bdf1ef4c1badb5b1982b0cde1 Mon Sep 17 00:00:00 2001 From: zyxkad Date: Wed, 18 Jun 2025 21:35:18 -0600 Subject: [PATCH 3/3] remove unused import --- .../de/srendi/advancedperipherals/common/items/KeyboardItem.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java b/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java index f1ec4340a..cb5045a06 100644 --- a/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java +++ b/src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java @@ -19,7 +19,6 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; -import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder;