Don't wrap attribute in holder < 1.20.5

Addresses #2998
This commit is contained in:
Dan Mulloy 2024-06-11 21:08:02 -05:00
parent 4051caca0b
commit 389233f599
No known key found for this signature in database
GPG Key ID: 3C5AD5D866D1539A
2 changed files with 33 additions and 34 deletions

View File

@ -1,5 +1,14 @@
package com.comphenix.protocol.wrappers; package com.comphenix.protocol.wrappers;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.FuzzyReflection;
@ -9,37 +18,33 @@ import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.collection.CachedSet; import com.comphenix.protocol.wrappers.collection.CachedSet;
import com.comphenix.protocol.wrappers.collection.ConvertedSet; import com.comphenix.protocol.wrappers.collection.ConvertedSet;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.reflect.Constructor;
import java.util.*;
/** /**
* Represents a single attribute sent in packet 44. * Represents a single attribute sent in packet 44.
* *
* @author Kristian * @author Kristian
*/ */
public class WrappedAttribute extends AbstractWrapper { public final class WrappedAttribute extends AbstractWrapper {
public static boolean KEY_WRAPPED = MinecraftVersion.NETHER_UPDATE.atOrAbove(); static final boolean KEY_WRAPPED = MinecraftVersion.NETHER_UPDATE.atOrAbove();
public static boolean IS_STATIC = MinecraftVersion.CAVES_CLIFFS_1.atOrAbove(); static final boolean IS_STATIC = MinecraftVersion.CAVES_CLIFFS_1.atOrAbove();
public static boolean IS_IN_HOLDER = MinecraftVersion.v1_20_5.atOrAbove(); static final boolean IS_IN_HOLDER = MinecraftVersion.v1_20_5.atOrAbove();
// Shared structure modifier // Shared structure modifier
private static StructureModifier<Object> ATTRIBUTE_MODIFIER; static StructureModifier<Object> ATTRIBUTE_MODIFIER;
// The one constructor // The one constructor
private static Constructor<?> ATTRIBUTE_CONSTRUCTOR; static Constructor<?> ATTRIBUTE_CONSTRUCTOR;
/** /**
* Remaps attributes if needed. * Remaps attributes if needed.
* *
* @return the remapped attribute or the attribute itself if no remapping was necessary. * @return the remapped attribute or the attribute itself if no remapping was necessary.
*/ */
private static String remap(String string) { static String remap(String string) {
switch (string) { switch (string) {
case "generic.maxHealth": return "generic.max_health"; case "generic.maxHealth": return "generic.max_health";
case "generic.followRange": return "generic.follow_range"; case "generic.followRange": return "generic.follow_range";
@ -55,11 +60,7 @@ public class WrappedAttribute extends AbstractWrapper {
} }
} }
/** StructureModifier<Object> modifier;
* Reference to the underlying attribute snapshot.
*/
protected Object handle;
protected StructureModifier<Object> modifier;
// Cached computed value // Cached computed value
private double computedValue = Double.NaN; private double computedValue = Double.NaN;
@ -72,7 +73,7 @@ public class WrappedAttribute extends AbstractWrapper {
* *
* @param handle - the NMS instance. * @param handle - the NMS instance.
*/ */
private WrappedAttribute(@Nonnull Object handle) { WrappedAttribute(@Nonnull Object handle) {
super(MinecraftReflection.getAttributeSnapshotClass()); super(MinecraftReflection.getAttributeSnapshotClass());
setHandle(handle); setHandle(handle);
@ -83,7 +84,6 @@ public class WrappedAttribute extends AbstractWrapper {
this.modifier = ATTRIBUTE_MODIFIER.withTarget(handle); this.modifier = ATTRIBUTE_MODIFIER.withTarget(handle);
} }
/** /**
* Construct a new wrapped attribute around a specific NMS instance. * Construct a new wrapped attribute around a specific NMS instance.
* *
@ -205,7 +205,7 @@ public class WrappedAttribute extends AbstractWrapper {
* @return TRUE if it does, FALSE otherwise. * @return TRUE if it does, FALSE otherwise.
*/ */
public boolean hasModifier(UUID id) { public boolean hasModifier(UUID id) {
return getModifiers().contains(WrappedAttributeModifier.newBuilder(id).build()); return getModifierByUUID(id) != null;
} }
/** /**
@ -214,14 +214,14 @@ public class WrappedAttribute extends AbstractWrapper {
* @param id - the id to look for. * @param id - the id to look for.
* @return The single attribute modifier with the given ID. * @return The single attribute modifier with the given ID.
*/ */
@Nullable
public WrappedAttributeModifier getModifierByUUID(UUID id) { public WrappedAttributeModifier getModifierByUUID(UUID id) {
if (hasModifier(id)) {
for (WrappedAttributeModifier modifier : getModifiers()) { for (WrappedAttributeModifier modifier : getModifiers()) {
if (Objects.equal(modifier.getUUID(), id)) { if (Objects.equal(modifier.getUUID(), id)) {
return modifier; return modifier;
} }
} }
}
return null; return null;
} }
@ -283,7 +283,7 @@ public class WrappedAttribute extends AbstractWrapper {
public int hashCode() { public int hashCode() {
if (attributeModifiers == null) if (attributeModifiers == null)
getModifiers(); getModifiers();
return Objects.hashCode(getAttributeKey(), getBaseValue(), attributeModifiers); return Objects.hashCode(getAttributeKey(), getBaseValue(), attributeModifiers.hashCode());
} }
/** /**
@ -365,7 +365,7 @@ public class WrappedAttribute extends AbstractWrapper {
private PacketContainer packet; private PacketContainer packet;
private Collection<WrappedAttributeModifier> modifiers = Collections.emptyList(); private Collection<WrappedAttributeModifier> modifiers = Collections.emptyList();
private Builder(WrappedAttribute template) { Builder(WrappedAttribute template) {
if (template != null) { if (template != null) {
baseValue = template.getBaseValue(); baseValue = template.getBaseValue();
attributeKey = template.getAttributeKey(); attributeKey = template.getAttributeKey();
@ -431,7 +431,7 @@ public class WrappedAttribute extends AbstractWrapper {
* @return Unwrapped modifiers. * @return Unwrapped modifiers.
*/ */
private Set<Object> getUnwrappedModifiers() { private Set<Object> getUnwrappedModifiers() {
final Set<Object> output = new HashSet<>(); Set<Object> output = new HashSet<>();
for (WrappedAttributeModifier modifier : modifiers) { for (WrappedAttributeModifier modifier : modifiers) {
output.add(modifier.getHandle()); output.add(modifier.getHandle());
@ -476,7 +476,9 @@ public class WrappedAttribute extends AbstractWrapper {
throw new IllegalArgumentException("Invalid attribute name: " + this.attributeKey); throw new IllegalArgumentException("Invalid attribute name: " + this.attributeKey);
} }
if (IS_IN_HOLDER) {
attributeKey = registry.getHolder(attributeKey); attributeKey = registry.getHolder(attributeKey);
}
} else { } else {
attributeKey = this.attributeKey; attributeKey = this.attributeKey;
} }

View File

@ -7,6 +7,7 @@ import com.comphenix.protocol.BukkitInitialization;
import com.comphenix.protocol.PacketType; import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedAttributeModifier.Operation; import com.comphenix.protocol.wrappers.WrappedAttributeModifier.Operation;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import net.minecraft.core.Holder; import net.minecraft.core.Holder;
import net.minecraft.core.IRegistry; import net.minecraft.core.IRegistry;
@ -17,12 +18,9 @@ import net.minecraft.world.entity.ai.attributes.AttributeBase;
import net.minecraft.world.entity.ai.attributes.AttributeModifier; import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class WrappedAttributeTest { public class WrappedAttributeTest {
@ -71,7 +69,6 @@ public class WrappedAttributeTest {
} }
@Test @Test
@Disabled // TODO -- modifiers are missing (or the hasModifier check is wrong)
public void testAttribute() { public void testAttribute() {
assertEquals(this.attribute, WrappedAttribute.fromHandle(this.getAttributeCopy(this.attribute))); assertEquals(this.attribute, WrappedAttribute.fromHandle(this.getAttributeCopy(this.attribute)));