Skip to content

Commit 5af40a8

Browse files
committed
The keyboard is now a working module, it currently uses the hotkey module keybind. I will see what I will do with the hotkey module or if I will create a new Keybind
I've created two new nbt tags to bind the module to the glasses to sync that to the container, I am not sure if there is maybe a better way, but it works
1 parent ab6a8cd commit 5af40a8

File tree

7 files changed

+100
-22
lines changed

7 files changed

+100
-22
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// 1.19.2 2024-09-24T12:35:48.4625121 Languages: en_us
2-
324712d2a78b7f5276fc1bd1c69925939ea0b1b1 assets/advancedperipherals/lang/en_us.json
1+
// 1.19.2 2024-09-25T15:15:12.3008086 Languages: en_us
2+
c5dde7701295d5ad506663dba947865df73d753c assets/advancedperipherals/lang/en_us.json

src/generated/resources/assets/advancedperipherals/lang/en_us.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"item.advancedperipherals.smart_glasses": "Smart Glasses",
5050
"item.advancedperipherals.smart_glasses_interface": "Smart Glasses Interface",
5151
"item.advancedperipherals.smart_glasses_netherite": "Netherite reinforced Smart Glasses",
52-
"item.advancedperipherals.tooltip.binding.boundto": "&7Bound to &b%s&7.",
52+
"item.advancedperipherals.tooltip.binding.bound_to": "&7Bound to &b%s&7.",
53+
"item.advancedperipherals.tooltip.binding.bound_to_glasses": "&7Bound to Smart Glasses with Id &b%s&7.",
5354
"item.advancedperipherals.tooltip.block_reader": "&7Reads nbt data of blocks to interact with blocks which do not have computer support.",
5455
"item.advancedperipherals.tooltip.chat_box": "&7Interacts with the ingame chat, can read and write messages.",
5556
"item.advancedperipherals.tooltip.chunk_controller": "&7A crafting ingredient for the Chunky Turtle.",
@@ -89,6 +90,7 @@
8990
"text.advancedperipherals.bind_memorycard": "&7Added you to the memory card",
9091
"text.advancedperipherals.cleared_keyboard": "&7Bind removed",
9192
"text.advancedperipherals.cleared_memorycard": "&7Cleared the memory card",
93+
"text.advancedperipherals.could_not_bind_keyboard": "&cCould not bind the keyboard to the computer located at &c%s",
9294
"text.advancedperipherals.keyboard_notbound": "&7Keyboard not bound",
9395
"text.advancedperipherals.saddle_turtle_dismount_hint": "Controlling %1$s. Press %2$s and %3$s to dismount.",
9496
"text.advancedperipherals.smart_glasses.modules": "Modules",

src/main/java/de/srendi/advancedperipherals/common/container/KeyboardContainer.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,49 @@
1111
import de.srendi.advancedperipherals.common.setup.APContainerTypes;
1212
import de.srendi.advancedperipherals.common.util.NBTUtil;
1313
import net.minecraft.core.BlockPos;
14+
import net.minecraft.nbt.CompoundTag;
1415
import net.minecraft.world.entity.player.Inventory;
1516
import net.minecraft.world.entity.player.Player;
1617
import net.minecraft.world.item.ItemStack;
1718
import net.minecraft.world.level.Level;
1819
import net.minecraftforge.server.ServerLifecycleHooks;
1920
import org.jetbrains.annotations.NotNull;
20-
21-
import javax.annotation.Nullable;
21+
import org.jetbrains.annotations.Nullable;
2222

2323
public class KeyboardContainer extends BaseContainer implements ComputerMenu {
2424

2525
private final ServerInputState<KeyboardContainer> input;
26-
private final BlockPos computerPos;
2726
@Nullable
2827
private ServerComputer computer = null;
2928

3029
public KeyboardContainer(int id, Inventory inventory, BlockPos pos, Level level, ItemStack keyboardItem) {
3130
super(APContainerTypes.KEYBOARD_CONTAINER.get(), id, inventory, pos, level);
3231
this.input = new ServerInputState<>( this );
33-
this.computerPos = NBTUtil.blockPosFromNBT(keyboardItem.getOrCreateTag().getCompound(KeyboardItem.BIND_TAG));
32+
33+
CompoundTag data = keyboardItem.getOrCreateTag();
34+
35+
if (!data.getBoolean(KeyboardItem.BOUND_TYPE_TAG)) {
36+
BlockPos computerPos = NBTUtil.blockPosFromNBT(keyboardItem.getOrCreateTag().getCompound(KeyboardItem.BIND_TAG));
37+
38+
for (ServerComputer computer : ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers()) {
39+
if (computer.getPosition() != null && computer.getPosition().equals(computerPos)) {
40+
this.computer = computer;
41+
}
42+
}
43+
} else if (data.contains(KeyboardItem.GLASSES_BIND_TAG)) {
44+
computer = ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().get(data.getInt(KeyboardItem.GLASSES_BIND_TAG));
45+
}
46+
3447
}
3548

3649
@Override
3750
public boolean stillValid(@NotNull Player playerIn) {
3851
return true;
3952
}
4053

54+
@Nullable
4155
@Override
4256
public ServerComputer getComputer() {
43-
if (computer != null)
44-
return computer;
45-
46-
for (ServerComputer computer : ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers()) {
47-
if (computer.getPosition().equals(computerPos)) {
48-
this.computer = computer;
49-
}
50-
}
51-
5257
return computer;
5358
}
5459

src/main/java/de/srendi/advancedperipherals/common/data/EnUsLanguageProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ private void addTooltips() {
127127
addTooltip(APBlocks.PERIPHERAL_CASING.get(), "&7An empty hull without the love it deserves. Used as crafting ingredient.");
128128
addTooltip(APItems.MEMORY_CARD.get(), "&7Can save the rights of a player to use it in an inventory manager.");
129129
addTooltip(APItems.KEYBOARD.get(), "&7Can be bound to a computer, used to type something on a computer without the need to open the computer.");
130-
addTooltip("binding.boundto", "&7Bound to &b%s&7.");
130+
addTooltip("binding.bound_to", "&7Bound to &b%s&7.");
131+
addTooltip("binding.bound_to_glasses", "&7Bound to Smart Glasses with Id &b%s&7.");
131132
addTooltip(APBlocks.INVENTORY_MANAGER.get(), "&7This block is able to send or receive specific items from a player inventory.");
132133
addTooltip(APBlocks.REDSTONE_INTEGRATOR.get(), "&7This block is able to interact with redstone. Works exactly like the redstone api of an computer.");
133134
addTooltip(APBlocks.BLOCK_READER.get(), "&7Reads nbt data of blocks to interact with blocks which do not have computer support.");
@@ -148,6 +149,7 @@ private void addTexts() {
148149
addText("bind_keyboard", "&7Bound to &b%s&7");
149150
addText("cleared_keyboard", "&7Bind removed");
150151
addText("keyboard_notbound", "&7Keyboard not bound");
152+
addText("could_not_bind_keyboard", "&cCould not bind the keyboard to the computer located at &c%s");
151153
addText("automata_core_feed_by_player", "You're trying to feed an entity to a soul, but your own body refuses to do this. Maybe something more mechanical can do this?");
152154
addText("smart_glasses.peripherals", "Peripherals");
153155
addText("smart_glasses.modules", "Modules");

src/main/java/de/srendi/advancedperipherals/common/items/KeyboardItem.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package de.srendi.advancedperipherals.common.items;
22

33
import dan200.computercraft.shared.computer.blocks.TileComputerBase;
4+
import de.srendi.advancedperipherals.client.KeyBindings;
45
import de.srendi.advancedperipherals.common.container.KeyboardContainer;
56
import de.srendi.advancedperipherals.common.items.base.BaseItem;
67
import de.srendi.advancedperipherals.common.items.base.IInventoryItem;
8+
import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess;
9+
import de.srendi.advancedperipherals.common.smartglasses.modules.IModule;
10+
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleItem;
11+
import de.srendi.advancedperipherals.common.smartglasses.modules.keyboard.KeyboardModule;
712
import de.srendi.advancedperipherals.common.util.EnumColor;
13+
import de.srendi.advancedperipherals.common.util.KeybindUtil;
814
import de.srendi.advancedperipherals.common.util.NBTUtil;
915
import de.srendi.advancedperipherals.common.util.SideHelper;
1016
import net.minecraft.core.BlockPos;
1117
import net.minecraft.nbt.CompoundTag;
1218
import net.minecraft.network.chat.Component;
19+
import net.minecraft.server.level.ServerPlayer;
1320
import net.minecraft.world.InteractionHand;
1421
import net.minecraft.world.InteractionResult;
1522
import net.minecraft.world.InteractionResultHolder;
1623
import net.minecraft.world.MenuProvider;
24+
import net.minecraft.world.entity.Entity;
1725
import net.minecraft.world.entity.player.Inventory;
1826
import net.minecraft.world.entity.player.Player;
1927
import net.minecraft.world.inventory.AbstractContainerMenu;
@@ -22,14 +30,17 @@
2230
import net.minecraft.world.item.context.UseOnContext;
2331
import net.minecraft.world.level.Level;
2432
import net.minecraft.world.level.block.entity.BlockEntity;
33+
import net.minecraftforge.network.NetworkHooks;
2534
import org.jetbrains.annotations.NotNull;
2635
import org.jetbrains.annotations.Nullable;
2736

2837
import java.util.List;
2938

30-
public class KeyboardItem extends BaseItem implements IInventoryItem {
39+
public class KeyboardItem extends BaseItem implements IInventoryItem, IModuleItem {
3140

3241
public static final String BIND_TAG = "bind";
42+
public static final String GLASSES_BIND_TAG = "glasses_id";
43+
public static final String BOUND_TYPE_TAG = "bind_type";
3344

3445
public KeyboardItem() {
3546
super(new Properties().stacksTo(1));
@@ -61,6 +72,30 @@ public InteractionResult useOn(UseOnContext context) {
6172
return super.useOn(context);
6273
}
6374

75+
@Override
76+
public void inventoryTick(ItemStack itemStack, Level level, Entity entity, int inventorySlot, boolean isCurrentItem, @Nullable SmartGlassesAccess access, @Nullable IModule module) {
77+
if (level.isClientSide())
78+
return;
79+
80+
CompoundTag data = itemStack.getOrCreateTag();
81+
if (data.contains(BOUND_TYPE_TAG) && !data.getBoolean(BOUND_TYPE_TAG)) {
82+
if (access != null) {
83+
data.putBoolean(BOUND_TYPE_TAG, true);
84+
data.putInt(GLASSES_BIND_TAG, access.getComputer().getInstanceID());
85+
data.remove(BIND_TAG);
86+
}
87+
}
88+
89+
if (KeybindUtil.isKeyPressed(KeyBindings.GLASSES_HOTKEY_KEYBINDING)) {
90+
if (entity instanceof ServerPlayer serverPlayer) {
91+
NetworkHooks.openScreen(serverPlayer, ((IInventoryItem) this).createContainer(serverPlayer, itemStack), buf -> {
92+
buf.writeBlockPos(serverPlayer.blockPosition());
93+
buf.writeItem(itemStack);
94+
});
95+
}
96+
}
97+
}
98+
6499
@Override
65100
public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand handIn) {
66101
if (playerIn.level.isClientSide())
@@ -82,20 +117,29 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
82117
public void appendHoverText(ItemStack stack, @Nullable Level levelIn, List<Component> tooltip, TooltipFlag flagIn) {
83118
super.appendHoverText(stack, levelIn, tooltip, flagIn);
84119
CompoundTag data = stack.getOrCreateTag();
85-
if (data.contains(BIND_TAG)) {
86-
tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.boundto", NBTUtil.blockPosFromNBT(data.getCompound(BIND_TAG)).toShortString())));
120+
if (data.contains(BOUND_TYPE_TAG) && !data.getBoolean(BOUND_TYPE_TAG)) {
121+
if (data.contains(BIND_TAG)) {
122+
tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.bound_to", NBTUtil.blockPosFromNBT(data.getCompound(BIND_TAG)).toShortString())));
123+
}
124+
} else {
125+
if (data.contains(GLASSES_BIND_TAG)) {
126+
tooltip.add(EnumColor.buildTextComponent(Component.translatable("item.advancedperipherals.tooltip.binding.bound_to_glasses", data.getInt(GLASSES_BIND_TAG))));
127+
128+
}
87129
}
88130
}
89131

90132
private void bind(Player player, ItemStack itemStack, BlockPos pos) {
91133
CompoundTag tag = NBTUtil.toNBT(pos);
92134

135+
itemStack.getOrCreateTag().putBoolean(BOUND_TYPE_TAG, false);
93136
itemStack.getOrCreateTag().put(BIND_TAG, tag);
94137
player.displayClientMessage(EnumColor.buildTextComponent(Component.translatable("text.advancedperipherals.bind_keyboard", pos.toShortString())), true);
95138
}
96139

97140
private void clear(Player player, ItemStack itemStack) {
98141
itemStack.getOrCreateTag().remove(BIND_TAG);
142+
itemStack.getOrCreateTag().putBoolean(BOUND_TYPE_TAG, false);
99143

100144
player.displayClientMessage(EnumColor.buildTextComponent(Component.translatable("text.advancedperipherals.cleared_keyboard")), true);
101145
}
@@ -115,4 +159,9 @@ public AbstractContainerMenu createMenu(int pContainerId, @NotNull Inventory pla
115159
}
116160
};
117161
}
162+
163+
@Override
164+
public IModule createModule(SmartGlassesAccess access) {
165+
return new KeyboardModule();
166+
}
118167
}

src/main/java/de/srendi/advancedperipherals/common/smartglasses/modules/IModuleFunctions.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@
88
*/
99
public interface IModuleFunctions extends IPeripheralPlugin {
1010

11-
IModuleFunctions EMPTY = new IModuleFunctions() {};
12-
1311
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.srendi.advancedperipherals.common.smartglasses.modules.keyboard;
2+
3+
import de.srendi.advancedperipherals.AdvancedPeripherals;
4+
import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess;
5+
import de.srendi.advancedperipherals.common.smartglasses.modules.IModule;
6+
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions;
7+
import net.minecraft.resources.ResourceLocation;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
public class KeyboardModule implements IModule {
11+
12+
@Override
13+
public ResourceLocation getName() {
14+
return AdvancedPeripherals.getRL("keyboard");
15+
}
16+
17+
@Nullable
18+
@Override
19+
public IModuleFunctions getFunctions(SmartGlassesAccess smartGlassesAccess) {
20+
return null;
21+
}
22+
}

0 commit comments

Comments
 (0)