Skip to content

Commit 5243c18

Browse files
committed
entity effect magic
Fixes ViaVersion#3927
1 parent 77d702b commit 5243c18

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/BlockItemPacketRewriter1_20_5.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,32 @@ public void registerPackets() {
225225
wrapper.passthrough(Types.DOUBLE); // X
226226
wrapper.passthrough(Types.DOUBLE); // Y
227227
wrapper.passthrough(Types.DOUBLE); // Z
228-
wrapper.passthrough(Types.FLOAT); // Offset X
229-
wrapper.passthrough(Types.FLOAT); // Offset Y
230-
wrapper.passthrough(Types.FLOAT); // Offset Z
228+
final float offX = wrapper.passthrough(Types.FLOAT);
229+
final float offY = wrapper.passthrough(Types.FLOAT);
230+
final float offZ = wrapper.passthrough(Types.FLOAT);
231231
final float data = wrapper.passthrough(Types.FLOAT);
232-
wrapper.passthrough(Types.INT); // Particle Count
232+
final int count = wrapper.passthrough(Types.INT);
233233

234234
// Read data and add it to Particle
235235
final ParticleMappings mappings = protocol.getMappingData().getParticleMappings();
236236
final int mappedId = mappings.getNewId(particleId);
237237
final Particle particle = new Particle(mappedId);
238238
if (mappedId == mappings.mappedId("entity_effect")) {
239-
particle.add(Types.INT, data != 0 ? ThreadLocalRandom.current().nextInt() : 0); // rgb
239+
final int color;
240+
if (data == 0) {
241+
// Black
242+
color = 0;
243+
} else if (count != 0) {
244+
// Randomized color
245+
color = ThreadLocalRandom.current().nextInt();
246+
} else {
247+
// From offset
248+
final int red = Math.round(offX * 255);
249+
final int green = Math.round(offY * 255);
250+
final int blue = Math.round(offZ * 255);
251+
color = (red << 16) | (green << 8) | blue;
252+
}
253+
particle.add(Types.INT, EntityPacketRewriter1_20_5.withAlpha(color));
240254
} else if (particleId == mappings.id("dust_color_transition")) {
241255
for (int i = 0; i < 7; i++) {
242256
particle.add(Types.FLOAT, wrapper.read(Types.FLOAT));

common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/EntityPacketRewriter1_20_5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ private void writeAttribute(final PacketWrapper wrapper, final String attributeI
387387
}
388388
}
389389

390-
private int withAlpha(final int rgb) {
390+
static int withAlpha(final int rgb) {
391391
return 255 << 24 | rgb & 0xffffff;
392392
}
393393

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727

2828
public final class EfficiencyAttributeStorage implements StorableObject {
2929

30-
public static final EnchantAttributeModifier EFFICIENCY = new EnchantAttributeModifier("minecraft:enchantment.efficiency/mainhand", 19, 0, level -> (level * level) + 1);
31-
public static final EnchantAttributeModifier SOUL_SPEED = new EnchantAttributeModifier("minecraft:enchantment.soul_speed", 21, 0.1, level -> 0.04D + ((level - 1) * 0.01D));
32-
public static final EnchantAttributeModifier SWIFT_SNEAK = new EnchantAttributeModifier("minecraft:enchantment.swift_sneak", 25, 0.3, level -> level * 0.15D);
33-
public static final EnchantAttributeModifier DEPTH_STRIDER = new EnchantAttributeModifier("minecraft:enchantment.depth_strider", 30, 0, level -> level / 3D);
30+
private static final EnchantAttributeModifier EFFICIENCY = new EnchantAttributeModifier("minecraft:enchantment.efficiency/mainhand", 19, 0, level -> (level * level) + 1);
31+
private static final EnchantAttributeModifier SOUL_SPEED = new EnchantAttributeModifier("minecraft:enchantment.soul_speed", 21, 0.1, level -> 0.04D + ((level - 1) * 0.01D));
32+
private static final EnchantAttributeModifier SWIFT_SNEAK = new EnchantAttributeModifier("minecraft:enchantment.swift_sneak", 25, 0.3, level -> level * 0.15D);
33+
private static final EnchantAttributeModifier DEPTH_STRIDER = new EnchantAttributeModifier("minecraft:enchantment.depth_strider", 30, 0, level -> level / 3D);
3434
private static final ActiveEnchants DEFAULT = new ActiveEnchants(-1,
3535
new ActiveEnchant(EFFICIENCY, 0),
3636
new ActiveEnchant(SOUL_SPEED, 0),
@@ -115,18 +115,7 @@ public record ActiveEnchants(int entityId, ActiveEnchant efficiency, ActiveEncha
115115
public record ActiveEnchant(EnchantAttributeModifier modifier, int level) {
116116
}
117117

118-
public static final class EnchantAttributeModifier { // Private constructor, equals by reference
119-
private final String key;
120-
private final int attributeId;
121-
private final double baseValue;
122-
private final LevelToModifier modifierFunction;
123-
124-
private EnchantAttributeModifier(final String key, final int attributeId, final double baseValue, final LevelToModifier modifierFunction) {
125-
this.key = key;
126-
this.attributeId = attributeId;
127-
this.baseValue = baseValue;
128-
this.modifierFunction = modifierFunction;
129-
}
118+
public record EnchantAttributeModifier(String key, int attributeId, double baseValue, LevelToModifier modifierFunction) {
130119
}
131120

132121
@FunctionalInterface

0 commit comments

Comments
 (0)