11package de .srendi .advancedperipherals .common .items ;
22
33import dan200 .computercraft .shared .computer .blocks .TileComputerBase ;
4+ import dan200 .computercraft .shared .computer .core .ServerComputer ;
45import de .srendi .advancedperipherals .client .KeyBindings ;
56import de .srendi .advancedperipherals .common .container .KeyboardContainer ;
67import de .srendi .advancedperipherals .common .items .base .BaseItem ;
78import de .srendi .advancedperipherals .common .items .base .IInventoryItem ;
89import de .srendi .advancedperipherals .common .network .APNetworking ;
9- import de .srendi .advancedperipherals .common .network .toclient . KeyboardMouseCapturePacket ;
10+ import de .srendi .advancedperipherals .common .network .toserver . GlassesHotkeyPacket ;
1011import de .srendi .advancedperipherals .common .smartglasses .SmartGlassesAccess ;
1112import de .srendi .advancedperipherals .common .smartglasses .modules .IModule ;
1213import de .srendi .advancedperipherals .common .smartglasses .modules .IModuleItem ;
1314import de .srendi .advancedperipherals .common .smartglasses .modules .keyboard .KeyboardModule ;
1415import de .srendi .advancedperipherals .common .util .EnumColor ;
1516import de .srendi .advancedperipherals .common .util .SideHelper ;
17+ import net .minecraft .client .player .LocalPlayer ;
1618import net .minecraft .core .BlockPos ;
1719import net .minecraft .nbt .CompoundTag ;
20+ import net .minecraft .network .FriendlyByteBuf ;
1821import net .minecraft .network .chat .Component ;
1922import net .minecraft .server .level .ServerPlayer ;
2023import net .minecraft .world .InteractionHand ;
3033import net .minecraft .world .item .context .UseOnContext ;
3134import net .minecraft .world .level .Level ;
3235import net .minecraft .world .level .block .entity .BlockEntity ;
33- import net .minecraftforge .network .NetworkHooks ;
3436import org .jetbrains .annotations .NotNull ;
3537import org .jetbrains .annotations .Nullable ;
3638
3941public class KeyboardItem extends BaseItem implements IInventoryItem , IModuleItem {
4042
4143 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" ;
44+ public static final String OPENING_TAG = "KeyboardOpening" ;
4445
4546 public KeyboardItem () {
4647 super (new Properties ().stacksTo (1 ));
@@ -75,48 +76,27 @@ public InteractionResult useOn(UseOnContext context) {
7576
7677 @ Override
7778 public void inventoryTick (ItemStack itemStack , Level level , Entity entity , int inventorySlot , boolean isCurrentItem , @ Nullable SmartGlassesAccess access , @ Nullable IModule module ) {
78- if (level .isClientSide ()) {
79+ if (!level .isClientSide ()) {
80+ itemStack .removeTagKey (BIND_TAG );
7981 return ;
8082 }
81-
82- if (access == null || !(module instanceof KeyboardModule keyboadModule )) {
83+ if (!(entity instanceof LocalPlayer player )) {
8384 return ;
8485 }
85-
86+ boolean pressed = KeyBindings . GLASSES_HOTKEY_KEYBINDING . isDown ();
8687 CompoundTag data = itemStack .getOrCreateTag ();
87- int instanceId = access .getComputer ().getInstanceID ();
88- int oldInstanceId = -1 ;
89-
90- if (data .contains (GLASSES_BIND_TAG )) {
91- oldInstanceId = data .getInt (GLASSES_BIND_TAG );
92- }
93-
94- if (!data .contains (BOUND_TYPE_TAG ) || ((oldInstanceId != -1 && oldInstanceId != instanceId )) || !data .getBoolean (BOUND_TYPE_TAG )) {
95- data .putBoolean (BOUND_TYPE_TAG , true );
96- data .putInt (GLASSES_BIND_TAG , access .getComputer ().getInstanceID ());
97- data .remove (BIND_TAG );
98- }
99-
100- if (!(entity instanceof ServerPlayer serverPlayer )) {
88+ if (data .getBoolean (OPENING_TAG ) == pressed ) {
10189 return ;
10290 }
103- // TODO: this for sure won't work on dedicated server
104- if (!KeyBindings . GLASSES_HOTKEY_KEYBINDING . isDown () ) {
91+ data . putBoolean ( OPENING_TAG , pressed );
92+ if (!pressed ) {
10593 return ;
10694 }
107-
108- access .getComputer ().queueEvent ("keyboard_open" );
109- if (serverPlayer .containerMenu instanceof KeyboardContainer openedKeyboard && openedKeyboard .getKeyboardItem () == itemStack ) {
95+ if (player .containerMenu instanceof KeyboardContainer openedKeyboard && openedKeyboard .getKeyboardItem ().equals (itemStack )) {
11096 return ;
11197 }
112-
113- NetworkHooks .openScreen (serverPlayer , this .createContainer (serverPlayer , itemStack ), buf -> {
114- buf .writeBlockPos (serverPlayer .blockPosition ());
115- buf .writeItem (itemStack );
116- });
117- if (keyboadModule .isCapturingMouse ()) {
118- APNetworking .sendTo (new KeyboardMouseCapturePacket (true ), serverPlayer );
119- }
98+ APNetworking .sendToServer (new GlassesHotkeyPacket ("" , -1 ));
99+ return ;
120100 }
121101
122102 @ Override
@@ -128,7 +108,8 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
128108 if (playerIn .isShiftKeyDown ()) {
129109 return new InteractionResultHolder <>(InteractionResult .PASS , playerIn .getItemInHand (handIn ));
130110 }
131- if (!playerIn .getItemInHand (handIn ).getOrCreateTag ().contains (BIND_TAG )) {
111+ CompoundTag data = playerIn .getItemInHand (handIn ).getTag ();
112+ if (data == null || !data .contains (BIND_TAG )) {
132113 playerIn .displayClientMessage (EnumColor .buildTextComponent (Component .translatable ("text.advancedperipherals.keyboard_notbound" )), false );
133114 return new InteractionResultHolder <>(InteractionResult .PASS , playerIn .getItemInHand (handIn ));
134115 }
@@ -140,20 +121,14 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
140121 public void appendHoverText (ItemStack stack , @ Nullable Level levelIn , List <Component > tooltip , TooltipFlag flagIn ) {
141122 super .appendHoverText (stack , levelIn , tooltip , flagIn );
142123 CompoundTag data = stack .getOrCreateTag ();
143- if (data .contains (BOUND_TYPE_TAG ) && !data .getBoolean (BOUND_TYPE_TAG )) {
144- if (data .contains (BIND_TAG )) {
145- tooltip .add (EnumColor .buildTextComponent (Component .translatable ("item.advancedperipherals.tooltip.binding.bound_to" , data .getInt (BIND_TAG ))));
146- }
147- } else {
148- if (data .contains (GLASSES_BIND_TAG )) {
149- tooltip .add (EnumColor .buildTextComponent (Component .translatable ("item.advancedperipherals.tooltip.binding.bound_to_glasses" , data .getInt (GLASSES_BIND_TAG ))));
150- }
124+ if (data .contains (BIND_TAG )) {
125+ tooltip .add (EnumColor .buildTextComponent (Component .translatable ("item.advancedperipherals.tooltip.binding.bound_to" , data .getInt (BIND_TAG ))));
151126 }
152127 }
153128
154129 private void bind (Player player , ItemStack itemStack , Level world , BlockPos pos ) {
155130 CompoundTag data = itemStack .getOrCreateTag ();
156- data .putBoolean ( BOUND_TYPE_TAG , false );
131+ data .remove ( BIND_TAG );
157132
158133 if (!(world .getBlockEntity (pos ) instanceof TileComputerBase computer )) {
159134 // TODO: should it show bind failed message?
@@ -173,7 +148,6 @@ private void bind(Player player, ItemStack itemStack, Level world, BlockPos pos)
173148 private void clear (Player player , ItemStack itemStack ) {
174149 CompoundTag data = itemStack .getOrCreateTag ();
175150 data .remove (BIND_TAG );
176- data .putBoolean (BOUND_TYPE_TAG , false );
177151
178152 player .displayClientMessage (EnumColor .buildTextComponent (Component .translatable ("text.advancedperipherals.cleared_keyboard" )), true );
179153 }
@@ -184,7 +158,7 @@ public MenuProvider createContainer(Player playerEntity, ItemStack itemStack) {
184158 @ NotNull
185159 @ Override
186160 public Component getDisplayName () {
187- return Component .literal ( "" );
161+ return Component .empty ( );
188162 }
189163
190164 @ Override
@@ -194,8 +168,29 @@ public AbstractContainerMenu createMenu(int pContainerId, @NotNull Inventory pla
194168 };
195169 }
196170
171+ public MenuProvider createContainerWithComputer (Player playerEntity , ItemStack itemStack , ServerComputer computer ) {
172+ return new MenuProvider () {
173+ @ NotNull
174+ @ Override
175+ public Component getDisplayName () {
176+ return Component .empty ();
177+ }
178+
179+ @ Override
180+ public AbstractContainerMenu createMenu (int pContainerId , @ NotNull Inventory playerInv , @ NotNull Player player ) {
181+ return new KeyboardContainer (pContainerId , playerInv , player .blockPosition (), player .getLevel (), itemStack , computer );
182+ }
183+ };
184+ }
185+
186+ @ Override
187+ public void writeContainerData (Player player , ItemStack stack , FriendlyByteBuf buf ) {
188+ buf .writeBlockPos (player .blockPosition ());
189+ buf .writeItem (stack );
190+ }
191+
197192 @ Override
198- public IModule createModule (SmartGlassesAccess access ) {
199- return new KeyboardModule ();
193+ public IModule createModule (SmartGlassesAccess access , ItemStack stack ) {
194+ return new KeyboardModule (this , stack );
200195 }
201196}
0 commit comments