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 ;
19- import net .minecraft .server .level .ServerPlayer ;
2022import net .minecraft .world .InteractionHand ;
2123import net .minecraft .world .InteractionResult ;
2224import net .minecraft .world .InteractionResultHolder ;
3032import net .minecraft .world .item .context .UseOnContext ;
3133import net .minecraft .world .level .Level ;
3234import net .minecraft .world .level .block .entity .BlockEntity ;
33- import net .minecraftforge .network .NetworkHooks ;
3435import org .jetbrains .annotations .NotNull ;
3536import org .jetbrains .annotations .Nullable ;
3637
3940public class KeyboardItem extends BaseItem implements IInventoryItem , IModuleItem {
4041
4142 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" ;
43+ public static final String OPENING_TAG = "KeyboardOpening" ;
4444
4545 public KeyboardItem () {
4646 super (new Properties ().stacksTo (1 ));
@@ -75,48 +75,27 @@ public InteractionResult useOn(UseOnContext context) {
7575
7676 @ Override
7777 public void inventoryTick (ItemStack itemStack , Level level , Entity entity , int inventorySlot , boolean isCurrentItem , @ Nullable SmartGlassesAccess access , @ Nullable IModule module ) {
78- if (level .isClientSide ()) {
78+ if (!level .isClientSide ()) {
79+ itemStack .removeTagKey (BIND_TAG );
7980 return ;
8081 }
81-
82- if (access == null || !(module instanceof KeyboardModule keyboadModule )) {
82+ if (!(entity instanceof LocalPlayer player )) {
8383 return ;
8484 }
85-
85+ boolean pressed = KeyBindings . GLASSES_HOTKEY_KEYBINDING . isDown ();
8686 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 )) {
87+ if (data .getBoolean (OPENING_TAG ) == pressed ) {
10188 return ;
10289 }
103- // TODO: this for sure won't work on dedicated server
104- if (!KeyBindings . GLASSES_HOTKEY_KEYBINDING . isDown () ) {
90+ data . putBoolean ( OPENING_TAG , pressed );
91+ if (!pressed ) {
10592 return ;
10693 }
107-
108- access .getComputer ().queueEvent ("keyboard_open" );
109- if (serverPlayer .containerMenu instanceof KeyboardContainer openedKeyboard && openedKeyboard .getKeyboardItem () == itemStack ) {
94+ if (player .containerMenu instanceof KeyboardContainer openedKeyboard && openedKeyboard .getKeyboardItem ().equals (itemStack )) {
11095 return ;
11196 }
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- }
97+ APNetworking .sendToServer (new GlassesHotkeyPacket ("" , -1 ));
98+ return ;
12099 }
121100
122101 @ Override
@@ -128,7 +107,8 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
128107 if (playerIn .isShiftKeyDown ()) {
129108 return new InteractionResultHolder <>(InteractionResult .PASS , playerIn .getItemInHand (handIn ));
130109 }
131- if (!playerIn .getItemInHand (handIn ).getOrCreateTag ().contains (BIND_TAG )) {
110+ CompoundTag data = playerIn .getItemInHand (handIn ).getTag ();
111+ if (data == null || !data .contains (BIND_TAG )) {
132112 playerIn .displayClientMessage (EnumColor .buildTextComponent (Component .translatable ("text.advancedperipherals.keyboard_notbound" )), false );
133113 return new InteractionResultHolder <>(InteractionResult .PASS , playerIn .getItemInHand (handIn ));
134114 }
@@ -140,20 +120,14 @@ public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, In
140120 public void appendHoverText (ItemStack stack , @ Nullable Level levelIn , List <Component > tooltip , TooltipFlag flagIn ) {
141121 super .appendHoverText (stack , levelIn , tooltip , flagIn );
142122 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- }
123+ if (data .contains (BIND_TAG )) {
124+ tooltip .add (EnumColor .buildTextComponent (Component .translatable ("item.advancedperipherals.tooltip.binding.bound_to" , data .getInt (BIND_TAG ))));
151125 }
152126 }
153127
154128 private void bind (Player player , ItemStack itemStack , Level world , BlockPos pos ) {
155129 CompoundTag data = itemStack .getOrCreateTag ();
156- data .putBoolean ( BOUND_TYPE_TAG , false );
130+ data .remove ( BIND_TAG );
157131
158132 if (!(world .getBlockEntity (pos ) instanceof TileComputerBase computer )) {
159133 // TODO: should it show bind failed message?
@@ -173,7 +147,6 @@ private void bind(Player player, ItemStack itemStack, Level world, BlockPos pos)
173147 private void clear (Player player , ItemStack itemStack ) {
174148 CompoundTag data = itemStack .getOrCreateTag ();
175149 data .remove (BIND_TAG );
176- data .putBoolean (BOUND_TYPE_TAG , false );
177150
178151 player .displayClientMessage (EnumColor .buildTextComponent (Component .translatable ("text.advancedperipherals.cleared_keyboard" )), true );
179152 }
@@ -184,7 +157,7 @@ public MenuProvider createContainer(Player playerEntity, ItemStack itemStack) {
184157 @ NotNull
185158 @ Override
186159 public Component getDisplayName () {
187- return Component .literal ( "" );
160+ return Component .empty ( );
188161 }
189162
190163 @ Override
@@ -194,8 +167,29 @@ public AbstractContainerMenu createMenu(int pContainerId, @NotNull Inventory pla
194167 };
195168 }
196169
170+ public MenuProvider createContainerWithComputer (Player playerEntity , ItemStack itemStack , ServerComputer computer ) {
171+ return new MenuProvider () {
172+ @ NotNull
173+ @ Override
174+ public Component getDisplayName () {
175+ return Component .empty ();
176+ }
177+
178+ @ Override
179+ public AbstractContainerMenu createMenu (int pContainerId , @ NotNull Inventory playerInv , @ NotNull Player player ) {
180+ return new KeyboardContainer (pContainerId , playerInv , player .blockPosition (), player .getLevel (), itemStack , computer );
181+ }
182+ };
183+ }
184+
185+ @ Override
186+ public void writeContainerData (Player player , ItemStack stack , FriendlyByteBuf buf ) {
187+ buf .writeBlockPos (player .blockPosition ());
188+ buf .writeItem (stack );
189+ }
190+
197191 @ Override
198- public IModule createModule (SmartGlassesAccess access ) {
199- return new KeyboardModule ();
192+ public IModule createModule (SmartGlassesAccess access , ItemStack stack ) {
193+ return new KeyboardModule (this , stack );
200194 }
201195}
0 commit comments