Skip to content

Commit 465962b

Browse files
Clear enchantments on screen close
1 parent 2fad493 commit 465962b

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

common/src/main/java/com/viaversion/viarewind/protocol/protocol1_8to1_9/packets/BlockItemPackets1_9.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ protected void registerPackets() {
6565

6666
protocol.registerClientbound(ClientboundPackets1_9.CLOSE_WINDOW, wrapper -> {
6767
final short windowId = wrapper.passthrough(Type.UNSIGNED_BYTE);
68-
wrapper.user().get(WindowTracker.class).remove(windowId);
68+
69+
final WindowTracker tracker = wrapper.user().get(WindowTracker.class);
70+
71+
final String windowType = tracker.get(windowId);
72+
if (windowType != null && windowType.equalsIgnoreCase("minecraft:enchanting_table")) {
73+
tracker.clearEnchantmentProperties();
74+
}
75+
tracker.remove(windowId);
6976
});
7077

7178
protocol.registerClientbound(ClientboundPackets1_9.OPEN_WINDOW, wrapper -> {
@@ -173,11 +180,11 @@ public void register() {
173180
final String windowType = tracker.get(windowId);
174181
if (windowType != null && windowType.equalsIgnoreCase("minecraft:enchanting_table")) {
175182
if (key >= 4 && key <= 6) { // Store enchantment ids
176-
tracker.storeEnchantmentTableProperty(windowId, key, value);
183+
tracker.putEnchantmentProperty(key, value);
177184
wrapper.cancel();
178185
} else if (key >= 7 && key <= 9) { // Mix levels with tracked ids
179186
key -= 3;
180-
final short property = tracker.getEnchantmentTableProperty(windowId, key);
187+
final short property = tracker.getEnchantmentValue(key);
181188
value = (short) (property | (value << 8));
182189
}
183190
}

common/src/main/java/com/viaversion/viarewind/protocol/protocol1_8to1_9/storage/WindowTracker.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
public class WindowTracker extends StoredObject {
3838
private final HashMap<Short, String> types = new HashMap<>();
3939
private final HashMap<Short, Item[]> brewingItems = new HashMap<>();
40-
private final Map<Short, WindowProperties> enchantmentProperties = new HashMap<>();
40+
private final Map<Short, Short> enchantmentProperties = new HashMap<>();
4141

4242
public WindowTracker(UserConnection user) {
4343
super(user);
@@ -65,19 +65,19 @@ public Item[] getBrewingItems(short windowId) {
6565
});
6666
}
6767

68-
public void storeEnchantmentTableProperty(short windowId, int key, int value) {
69-
enchantmentProperties.computeIfAbsent(windowId, aShort -> new WindowProperties()).put(key, value);
68+
public short getEnchantmentValue(final short key) {
69+
if (!enchantmentProperties.containsKey(key)) {
70+
return 0;
71+
}
72+
return enchantmentProperties.remove(key);
7073
}
7174

72-
public short getEnchantmentTableProperty(short windowId, int key) {
73-
final Integer value = enchantmentProperties.get(windowId).get(key);
74-
if (value == null) {
75-
return 0; // Assume default value, nothing we can do about
76-
}
77-
if (enchantmentProperties.get(windowId).properties.isEmpty()) { // Remove from list if nothing to track
78-
enchantmentProperties.remove(windowId);
79-
}
80-
return value.shortValue();
75+
public void putEnchantmentProperty(short key, short value) {
76+
enchantmentProperties.put(key, value);
77+
}
78+
79+
public void clearEnchantmentProperties() {
80+
enchantmentProperties.clear();
8181
}
8282

8383
public static void updateBrewingStand(UserConnection user, Item blazePowder, short windowId) throws Exception {
@@ -109,20 +109,4 @@ public static void updateBrewingStand(UserConnection user, Item blazePowder, sho
109109
setSlot.scheduleSend(Protocol1_8To1_9.class);
110110
}
111111
}
112-
113-
public static class WindowProperties {
114-
115-
private final Map<Integer, Integer> properties = new HashMap<>();
116-
117-
public Integer get(int key) {
118-
if (!properties.containsKey(key)) {
119-
return null;
120-
}
121-
return properties.remove(key);
122-
}
123-
124-
public void put(int key, int value) {
125-
properties.put(key, value);
126-
}
127-
}
128112
}

0 commit comments

Comments
 (0)