Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,50 @@

public class KeyboardContainer extends BaseContainer implements ComputerMenu {

@Nullable
private final ServerInputState<KeyboardContainer> 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() {
return this.keyboardItem;
}

@Override
public boolean stillValid(@NotNull Player playerIn) {
public boolean stillValid(@NotNull Player player) {
return true;
}

Expand All @@ -73,6 +83,7 @@ public ServerComputer getComputer() {
return computer;
}

@Nullable
@Override
public ServerInputHandler getInput() {
return input;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
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;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
Expand All @@ -30,7 +32,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;

Expand All @@ -39,8 +40,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));
Expand Down Expand Up @@ -75,48 +75,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
Expand All @@ -128,7 +107,8 @@ public InteractionResultHolder<ItemStack> 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));
}
Expand All @@ -140,20 +120,14 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
public void appendHoverText(ItemStack stack, @Nullable Level levelIn, List<Component> 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?
Expand All @@ -173,7 +147,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);
}
Expand All @@ -184,7 +157,7 @@ public MenuProvider createContainer(Player playerEntity, ItemStack itemStack) {
@NotNull
@Override
public Component getDisplayName() {
return Component.literal("");
return Component.empty();
}

@Override
Expand All @@ -194,8 +167,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ public BaseItem() {

@Override
public InteractionResultHolder<ItemStack> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Loading