Skip to content

Commit 9ba1214

Browse files
committed
Fixed an issue where the keyboard does not bound to the glasses when it was bound to different glasses before
1 parent b94b8be commit 9ba1214

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,11 @@ public boolean isEnabled() {
5454
@NotNull
5555
@Override
5656
public InteractionResult useOn(UseOnContext context) {
57-
if (context.getPlayer() == null)
58-
return InteractionResult.PASS;
57+
if (context.getPlayer() == null) return InteractionResult.PASS;
5958

60-
if (SideHelper.isClientPlayer(context.getPlayer()))
61-
return InteractionResult.PASS;
59+
if (SideHelper.isClientPlayer(context.getPlayer())) return InteractionResult.PASS;
6260

63-
if (!context.getPlayer().isShiftKeyDown())
64-
return InteractionResult.PASS;
61+
if (!context.getPlayer().isShiftKeyDown()) return InteractionResult.PASS;
6562

6663
BlockEntity entity = context.getLevel().getBlockEntity(context.getClickedPos());
6764
if (entity instanceof TileComputerBase) {
@@ -74,16 +71,21 @@ public InteractionResult useOn(UseOnContext context) {
7471

7572
@Override
7673
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;
74+
if (level.isClientSide()) return;
75+
76+
if (access == null) return;
7977

8078
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-
}
79+
int instanceId = access.getComputer().getInstanceID();
80+
int oldInstanceId = -1;
81+
82+
if (data.contains(GLASSES_BIND_TAG)) oldInstanceId = data.getInt(GLASSES_BIND_TAG);
83+
84+
if (!data.contains(BOUND_TYPE_TAG) || ((oldInstanceId != -1 && oldInstanceId != instanceId)) || !data.getBoolean(BOUND_TYPE_TAG)) {
85+
data.putBoolean(BOUND_TYPE_TAG, true);
86+
data.putInt(GLASSES_BIND_TAG, access.getComputer().getInstanceID());
87+
data.remove(BIND_TAG);
88+
8789
}
8890

8991
if (KeybindUtil.isKeyPressed(KeyBindings.GLASSES_HOTKEY_KEYBINDING)) {

0 commit comments

Comments
 (0)