Skip to content

Commit

Permalink
Rewrite enchantment window properties in 1.8->1.9
Browse files Browse the repository at this point in the history
Closes #485
  • Loading branch information
FlorianMichael committed Apr 28, 2024
1 parent fb8ea8c commit 2fad493
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ public void register() {
});
}
});

protocol.registerClientbound(ClientboundPackets1_9.WINDOW_PROPERTY, wrapper -> {
final short windowId = wrapper.passthrough(Type.UNSIGNED_BYTE);
short key = wrapper.read(Type.SHORT);
short value = wrapper.read(Type.SHORT);

final WindowTracker tracker = wrapper.user().get(WindowTracker.class);
final String windowType = tracker.get(windowId);
if (windowType != null && windowType.equalsIgnoreCase("minecraft:enchanting_table")) {
if (key >= 4 && key <= 6) { // Store enchantment ids
tracker.storeEnchantmentTableProperty(windowId, key, value);
wrapper.cancel();
} else if (key >= 7 && key <= 9) { // Mix levels with tracked ids
key -= 3;
final short property = tracker.getEnchantmentTableProperty(windowId, key);
value = (short) (property | (value << 8));
}
}
wrapper.write(Type.SHORT, key);
wrapper.write(Type.SHORT, value);
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
import com.viaversion.viaversion.protocols.protocol1_8.ClientboundPackets1_8;

import java.util.HashMap;
import java.util.Map;

public class WindowTracker extends StoredObject {
private final HashMap<Short, String> types = new HashMap<>();
private final HashMap<Short, Item[]> brewingItems = new HashMap<>();
private final Map<Short, WindowProperties> enchantmentProperties = new HashMap<>();

public WindowTracker(UserConnection user) {
super(user);
Expand Down Expand Up @@ -63,6 +65,21 @@ public Item[] getBrewingItems(short windowId) {
});
}

public void storeEnchantmentTableProperty(short windowId, int key, int value) {
enchantmentProperties.computeIfAbsent(windowId, aShort -> new WindowProperties()).put(key, value);
}

public short getEnchantmentTableProperty(short windowId, int key) {
final Integer value = enchantmentProperties.get(windowId).get(key);
if (value == null) {
return 0; // Assume default value, nothing we can do about
}
if (enchantmentProperties.get(windowId).properties.isEmpty()) { // Remove from list if nothing to track
enchantmentProperties.remove(windowId);
}
return value.shortValue();
}

public static void updateBrewingStand(UserConnection user, Item blazePowder, short windowId) throws Exception {
if (blazePowder != null && blazePowder.identifier() != 377) {
return;
Expand Down Expand Up @@ -92,4 +109,20 @@ public static void updateBrewingStand(UserConnection user, Item blazePowder, sho
setSlot.scheduleSend(Protocol1_8To1_9.class);
}
}

public static class WindowProperties {

private final Map<Integer, Integer> properties = new HashMap<>();

public Integer get(int key) {
if (!properties.containsKey(key)) {
return null;
}
return properties.remove(key);
}

public void put(int key, int value) {
properties.put(key, value);
}
}
}

0 comments on commit 2fad493

Please sign in to comment.