Skip to content

Commit 00fedc7

Browse files
committed
Also handle aqua affinity
1 parent 05aa05c commit 00fedc7

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

bukkit/src/main/java/com/viaversion/viaversion/bukkit/listeners/v1_20_5to1_21/PlayerChangeItemListener.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
public final class PlayerChangeItemListener extends ViaBukkitListener {
4242

4343
private final Enchantment efficiency = Enchantment.getByKey(NamespacedKey.minecraft("efficiency"));
44+
private final Enchantment aquaAffinity = Enchantment.getByKey(NamespacedKey.minecraft("aqua_affinity"));
4445
private final Enchantment depthStrider = Enchantment.getByKey(NamespacedKey.minecraft("depth_strider"));
4546
private final Enchantment soulSpeed = Enchantment.getByKey(NamespacedKey.minecraft("soul_speed"));
4647
private final Enchantment swiftSneak = Enchantment.getByKey(NamespacedKey.minecraft("swift_sneak"));
@@ -61,6 +62,8 @@ public void onPlayerInventorySlotChangedEvent(final PlayerInventorySlotChangeEve
6162
sendAttributeUpdate(player, item, Slot.BOOTS);
6263
} else if (slot == 37) {
6364
sendAttributeUpdate(player, item, Slot.LEGGINGS);
65+
} else if (slot == 39) {
66+
sendAttributeUpdate(player, item, Slot.HELMET);
6467
}
6568
}
6669

@@ -84,11 +87,13 @@ private void sendAttributeUpdate(final Player player, @Nullable final ItemStack
8487

8588
final EfficiencyAttributeStorage.ActiveEnchants activeEnchants = storage.activeEnchants();
8689
int efficiencyLevel = activeEnchants.efficiency().level();
90+
int aquaAffinityLevel = activeEnchants.aquaAffinity().level();
8791
int soulSpeedLevel = activeEnchants.soulSpeed().level();
8892
int swiftSneakLevel = activeEnchants.swiftSneak().level();
8993
int depthStriderLevel = activeEnchants.depthStrider().level();
9094
switch (slot) {
9195
case HAND -> efficiencyLevel = item != null ? item.getEnchantmentLevel(efficiency) : 0;
96+
case HELMET -> aquaAffinityLevel = item != null ? item.getEnchantmentLevel(aquaAffinity) : 0;
9297
case LEGGINGS -> swiftSneakLevel = item != null && swiftSneak != null ? item.getEnchantmentLevel(swiftSneak) : 0;
9398
case BOOTS -> {
9499
depthStriderLevel = item != null && depthStrider != null ? item.getEnchantmentLevel(depthStrider) : 0;
@@ -97,10 +102,10 @@ private void sendAttributeUpdate(final Player player, @Nullable final ItemStack
97102
//soulSpeedLevel = item != null && soulSpeed != null ? item.getEnchantmentLevel(soulSpeed) : 0;
98103
}
99104
}
100-
storage.setEnchants(player.getEntityId(), connection, efficiencyLevel, soulSpeedLevel, swiftSneakLevel, depthStriderLevel);
105+
storage.setEnchants(player.getEntityId(), connection, efficiencyLevel, soulSpeedLevel, swiftSneakLevel, aquaAffinityLevel, depthStriderLevel);
101106
}
102107

103108
private enum Slot {
104-
HAND, BOOTS, LEGGINGS
109+
HAND, BOOTS, LEGGINGS, HELMET
105110
}
106111
}

common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/storage/EfficiencyAttributeStorage.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,27 @@ public final class EfficiencyAttributeStorage implements StorableObject {
3030
private static final EnchantAttributeModifier EFFICIENCY = new EnchantAttributeModifier("minecraft:enchantment.efficiency/mainhand", 19, 0, level -> (level * level) + 1);
3131
private static final EnchantAttributeModifier SOUL_SPEED = new EnchantAttributeModifier("minecraft:enchantment.soul_speed", 21, 0.1, level -> 0.04D + ((level - 1) * 0.01D));
3232
private static final EnchantAttributeModifier SWIFT_SNEAK = new EnchantAttributeModifier("minecraft:enchantment.swift_sneak", 25, 0.3, level -> level * 0.15D);
33+
private static final EnchantAttributeModifier AQUA_AFFINITY = new EnchantAttributeModifier("minecraft:enchantment.aqua_affinity", 28, 0.2, level -> level * 4, (byte) 2);
3334
private static final EnchantAttributeModifier DEPTH_STRIDER = new EnchantAttributeModifier("minecraft:enchantment.depth_strider", 30, 0, level -> level / 3D);
3435
private static final ActiveEnchants DEFAULT = new ActiveEnchants(-1,
3536
new ActiveEnchant(EFFICIENCY, 0),
3637
new ActiveEnchant(SOUL_SPEED, 0),
3738
new ActiveEnchant(SWIFT_SNEAK, 0),
39+
new ActiveEnchant(AQUA_AFFINITY, 0),
3840
new ActiveEnchant(DEPTH_STRIDER, 0)
3941
);
4042
private final Object lock = new Object();
41-
private volatile ActiveEnchants activeEnchants = DEFAULT;
4243
private volatile boolean attributesSent = true;
4344
private volatile boolean loginSent;
45+
private ActiveEnchants activeEnchants = DEFAULT;
4446

45-
public void setEnchants(final int entityId, final UserConnection connection, final int efficiency, final int soulSpeed, final int swiftSneak, final int depthStrider) {
47+
public void setEnchants(final int entityId, final UserConnection connection, final int efficiency, final int soulSpeed,
48+
final int swiftSneak, final int aquaAffinity, final int depthStrider) {
4649
// Always called from the main thread
4750
if (efficiency == activeEnchants.efficiency.level
4851
&& soulSpeed == activeEnchants.soulSpeed.level
4952
&& swiftSneak == activeEnchants.swiftSneak.level
53+
&& aquaAffinity == activeEnchants.aquaAffinity.level
5054
&& depthStrider == activeEnchants.depthStrider.level) {
5155
return;
5256
}
@@ -56,6 +60,7 @@ public void setEnchants(final int entityId, final UserConnection connection, fin
5660
new ActiveEnchant(EFFICIENCY, efficiency),
5761
new ActiveEnchant(SOUL_SPEED, soulSpeed),
5862
new ActiveEnchant(SWIFT_SNEAK, swiftSneak),
63+
new ActiveEnchant(AQUA_AFFINITY, aquaAffinity),
5964
new ActiveEnchant(DEPTH_STRIDER, depthStrider)
6065
);
6166
this.attributesSent = false;
@@ -99,7 +104,7 @@ private void sendAttributesPacket(final UserConnection connection) {
99104
attributesPacket.write(Types.VAR_INT, 1); // Modifiers
100105
attributesPacket.write(Types.STRING, modifier.key);
101106
attributesPacket.write(Types.DOUBLE, enchant.modifier.modifierFunction.get(enchant.level));
102-
attributesPacket.write(Types.BYTE, (byte) 0); // 'Add' operation
107+
attributesPacket.write(Types.BYTE, modifier.operation);
103108
} else {
104109
attributesPacket.write(Types.VAR_INT, 0); // Modifiers
105110
}
@@ -108,14 +113,18 @@ private void sendAttributesPacket(final UserConnection connection) {
108113
attributesPacket.scheduleSend(Protocol1_20_5To1_21.class);
109114
}
110115

111-
public record ActiveEnchants(int entityId, ActiveEnchant efficiency, ActiveEnchant soulSpeed,
112-
ActiveEnchant swiftSneak, ActiveEnchant depthStrider) {
116+
public record ActiveEnchants(int entityId, ActiveEnchant efficiency, ActiveEnchant aquaAffinity,
117+
ActiveEnchant soulSpeed, ActiveEnchant swiftSneak, ActiveEnchant depthStrider) {
113118
}
114119

115120
public record ActiveEnchant(EnchantAttributeModifier modifier, int level) {
116121
}
117122

118-
public record EnchantAttributeModifier(String key, int attributeId, double baseValue, LevelToModifier modifierFunction) {
123+
public record EnchantAttributeModifier(String key, int attributeId, double baseValue, LevelToModifier modifierFunction, byte operation) {
124+
125+
private EnchantAttributeModifier(String key, int attributeId, double baseValue, LevelToModifier modifierFunction) {
126+
this(key, attributeId, baseValue, modifierFunction, (byte) 0);
127+
}
119128
}
120129

121130
@FunctionalInterface

0 commit comments

Comments
 (0)