Fix attribute builder in 1.17

Addresses #1224
This commit is contained in:
Dan Mulloy 2021-06-20 12:08:47 -04:00
parent c54a99945d
commit 9a0703d05d
No known key found for this signature in database
GPG Key ID: BFACD592A5F0DFD6
2 changed files with 15 additions and 12 deletions

View File

@ -3,14 +3,12 @@ package com.comphenix.protocol.wrappers;
import java.lang.reflect.Constructor;
import java.util.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.reflect.accessors.Accessors;
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
import com.comphenix.protocol.reflect.fuzzy.FuzzyFieldContract;
import com.comphenix.protocol.reflect.fuzzy.FuzzyMethodContract;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
@ -35,9 +33,6 @@ public class WrappedAttribute extends AbstractWrapper {
// The one constructor
private static Constructor<?> ATTRIBUTE_CONSTRUCTOR;
private static Object REGISTRY = null;
private static MethodAccessor REGISTRY_GET = null;
private static final Map<String, String> REMAP;
static {
@ -77,7 +72,7 @@ public class WrappedAttribute extends AbstractWrapper {
// Initialize modifier
if (ATTRIBUTE_MODIFIER == null) {
ATTRIBUTE_MODIFIER = new StructureModifier<Object>(MinecraftReflection.getAttributeSnapshotClass());
ATTRIBUTE_MODIFIER = new StructureModifier<>(MinecraftReflection.getAttributeSnapshotClass());
}
this.modifier = ATTRIBUTE_MODIFIER.withTarget(handle);
}
@ -170,8 +165,14 @@ public class WrappedAttribute extends AbstractWrapper {
/**
* Retrieve the parent update attributes packet.
* @return The parent packet.
* @deprecated Removed in 1.17
*/
@Nullable
public PacketContainer getParentPacket() {
if (MinecraftVersion.CAVES_CLIFFS_1.atOrAbove()) {
return null;
}
return new PacketContainer(
PacketType.Play.Server.UPDATE_ATTRIBUTES,
modifier.withType(MinecraftReflection.getPacketClass()).read(0)
@ -226,7 +227,7 @@ public class WrappedAttribute extends AbstractWrapper {
}
};
attributeModifiers = new CachedSet<WrappedAttributeModifier>(converted);
attributeModifiers = new CachedSet<>(converted);
}
return Collections.unmodifiableSet(attributeModifiers);
}
@ -249,9 +250,7 @@ public class WrappedAttribute extends AbstractWrapper {
if (getBaseValue() == other.getBaseValue() &&
Objects.equal(getAttributeKey(), other.getAttributeKey())) {
return getModifiers().stream()
.filter((elem) -> !other.getModifiers().contains(elem))
.count() == 0;
return other.getModifiers().containsAll(getModifiers());
}
}
return false;
@ -414,7 +413,6 @@ public class WrappedAttribute extends AbstractWrapper {
* @throws RuntimeException If anything went wrong with the reflection.
*/
public WrappedAttribute build() {
Preconditions.checkNotNull(packet, "packet cannot be NULL.");
Preconditions.checkNotNull(attributeKey, "attributeKey cannot be NULL.");
// Remember to set the base value

View File

@ -74,6 +74,11 @@ public class WrappedAttributeTest {
assertTrue(attribute.hasModifier(doubleModifier.getUUID()));
assertTrue(attribute.hasModifier(constantModifier.getUUID()));
}
@Test
public void testFromTemplate() {
assertEquals(attribute, WrappedAttribute.newBuilder(attribute).build());
}
/**
* Retrieve the equivalent NMS attribute.