Skip to content

Commit 389233f

Browse files
committed
Don't wrap attribute in holder < 1.20.5
Addresses #2998
1 parent 4051cac commit 389233f

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

src/main/java/com/comphenix/protocol/wrappers/WrappedAttribute.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package com.comphenix.protocol.wrappers;
22

3+
import java.lang.reflect.Constructor;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
import java.util.UUID;
9+
import javax.annotation.Nonnull;
10+
import javax.annotation.Nullable;
11+
312
import com.comphenix.protocol.PacketType;
413
import com.comphenix.protocol.events.PacketContainer;
514
import com.comphenix.protocol.reflect.FuzzyReflection;
@@ -9,37 +18,33 @@
918
import com.comphenix.protocol.utility.MinecraftVersion;
1019
import com.comphenix.protocol.wrappers.collection.CachedSet;
1120
import com.comphenix.protocol.wrappers.collection.ConvertedSet;
21+
1222
import com.google.common.base.Objects;
1323
import com.google.common.base.Preconditions;
1424
import com.google.common.collect.Sets;
1525

16-
import javax.annotation.Nonnull;
17-
import javax.annotation.Nullable;
18-
import java.lang.reflect.Constructor;
19-
import java.util.*;
20-
2126
/**
2227
* Represents a single attribute sent in packet 44.
2328
*
2429
* @author Kristian
2530
*/
26-
public class WrappedAttribute extends AbstractWrapper {
27-
public static boolean KEY_WRAPPED = MinecraftVersion.NETHER_UPDATE.atOrAbove();
28-
public static boolean IS_STATIC = MinecraftVersion.CAVES_CLIFFS_1.atOrAbove();
29-
public static boolean IS_IN_HOLDER = MinecraftVersion.v1_20_5.atOrAbove();
31+
public final class WrappedAttribute extends AbstractWrapper {
32+
static final boolean KEY_WRAPPED = MinecraftVersion.NETHER_UPDATE.atOrAbove();
33+
static final boolean IS_STATIC = MinecraftVersion.CAVES_CLIFFS_1.atOrAbove();
34+
static final boolean IS_IN_HOLDER = MinecraftVersion.v1_20_5.atOrAbove();
3035

3136
// Shared structure modifier
32-
private static StructureModifier<Object> ATTRIBUTE_MODIFIER;
37+
static StructureModifier<Object> ATTRIBUTE_MODIFIER;
3338

3439
// The one constructor
35-
private static Constructor<?> ATTRIBUTE_CONSTRUCTOR;
40+
static Constructor<?> ATTRIBUTE_CONSTRUCTOR;
3641

3742
/**
3843
* Remaps attributes if needed.
3944
*
4045
* @return the remapped attribute or the attribute itself if no remapping was necessary.
4146
*/
42-
private static String remap(String string) {
47+
static String remap(String string) {
4348
switch (string) {
4449
case "generic.maxHealth": return "generic.max_health";
4550
case "generic.followRange": return "generic.follow_range";
@@ -55,11 +60,7 @@ private static String remap(String string) {
5560
}
5661
}
5762

58-
/**
59-
* Reference to the underlying attribute snapshot.
60-
*/
61-
protected Object handle;
62-
protected StructureModifier<Object> modifier;
63+
StructureModifier<Object> modifier;
6364

6465
// Cached computed value
6566
private double computedValue = Double.NaN;
@@ -72,7 +73,7 @@ private static String remap(String string) {
7273
*
7374
* @param handle - the NMS instance.
7475
*/
75-
private WrappedAttribute(@Nonnull Object handle) {
76+
WrappedAttribute(@Nonnull Object handle) {
7677
super(MinecraftReflection.getAttributeSnapshotClass());
7778
setHandle(handle);
7879

@@ -83,7 +84,6 @@ private WrappedAttribute(@Nonnull Object handle) {
8384
this.modifier = ATTRIBUTE_MODIFIER.withTarget(handle);
8485
}
8586

86-
8787
/**
8888
* Construct a new wrapped attribute around a specific NMS instance.
8989
*
@@ -205,7 +205,7 @@ public PacketContainer getParentPacket() {
205205
* @return TRUE if it does, FALSE otherwise.
206206
*/
207207
public boolean hasModifier(UUID id) {
208-
return getModifiers().contains(WrappedAttributeModifier.newBuilder(id).build());
208+
return getModifierByUUID(id) != null;
209209
}
210210

211211
/**
@@ -214,14 +214,14 @@ public boolean hasModifier(UUID id) {
214214
* @param id - the id to look for.
215215
* @return The single attribute modifier with the given ID.
216216
*/
217+
@Nullable
217218
public WrappedAttributeModifier getModifierByUUID(UUID id) {
218-
if (hasModifier(id)) {
219-
for (WrappedAttributeModifier modifier : getModifiers()) {
220-
if (Objects.equal(modifier.getUUID(), id)) {
221-
return modifier;
222-
}
219+
for (WrappedAttributeModifier modifier : getModifiers()) {
220+
if (Objects.equal(modifier.getUUID(), id)) {
221+
return modifier;
223222
}
224223
}
224+
225225
return null;
226226
}
227227

@@ -283,7 +283,7 @@ public boolean equals(Object obj) {
283283
public int hashCode() {
284284
if (attributeModifiers == null)
285285
getModifiers();
286-
return Objects.hashCode(getAttributeKey(), getBaseValue(), attributeModifiers);
286+
return Objects.hashCode(getAttributeKey(), getBaseValue(), attributeModifiers.hashCode());
287287
}
288288

289289
/**
@@ -365,7 +365,7 @@ public static class Builder {
365365
private PacketContainer packet;
366366
private Collection<WrappedAttributeModifier> modifiers = Collections.emptyList();
367367

368-
private Builder(WrappedAttribute template) {
368+
Builder(WrappedAttribute template) {
369369
if (template != null) {
370370
baseValue = template.getBaseValue();
371371
attributeKey = template.getAttributeKey();
@@ -431,7 +431,7 @@ public Builder packet(PacketContainer packet) {
431431
* @return Unwrapped modifiers.
432432
*/
433433
private Set<Object> getUnwrappedModifiers() {
434-
final Set<Object> output = new HashSet<>();
434+
Set<Object> output = new HashSet<>();
435435

436436
for (WrappedAttributeModifier modifier : modifiers) {
437437
output.add(modifier.getHandle());
@@ -476,7 +476,9 @@ public WrappedAttribute build() {
476476
throw new IllegalArgumentException("Invalid attribute name: " + this.attributeKey);
477477
}
478478

479-
attributeKey = registry.getHolder(attributeKey);
479+
if (IS_IN_HOLDER) {
480+
attributeKey = registry.getHolder(attributeKey);
481+
}
480482
} else {
481483
attributeKey = this.attributeKey;
482484
}

src/test/java/com/comphenix/protocol/wrappers/WrappedAttributeTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.comphenix.protocol.PacketType;
88
import com.comphenix.protocol.events.PacketContainer;
99
import com.comphenix.protocol.wrappers.WrappedAttributeModifier.Operation;
10+
1011
import com.google.common.collect.Lists;
1112
import net.minecraft.core.Holder;
1213
import net.minecraft.core.IRegistry;
@@ -17,12 +18,9 @@
1718
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
1819
import org.junit.jupiter.api.BeforeAll;
1920
import org.junit.jupiter.api.BeforeEach;
20-
import org.junit.jupiter.api.Disabled;
2121
import org.junit.jupiter.api.Test;
2222

23-
import static org.junit.jupiter.api.Assertions.assertEquals;
24-
import static org.junit.jupiter.api.Assertions.assertNotSame;
25-
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
import static org.junit.jupiter.api.Assertions.*;
2624

2725
public class WrappedAttributeTest {
2826

@@ -71,7 +69,6 @@ public void testEquality() {
7169
}
7270

7371
@Test
74-
@Disabled // TODO -- modifiers are missing (or the hasModifier check is wrong)
7572
public void testAttribute() {
7673
assertEquals(this.attribute, WrappedAttribute.fromHandle(this.getAttributeCopy(this.attribute)));
7774

0 commit comments

Comments
 (0)