SPIGOT-5805: NPE when getting an Attribute from a Player

This commit is contained in:
md_5 2020-06-25 13:19:24 +10:00
parent b136f8430d
commit 38de0b81e2
No known key found for this signature in database
GPG key ID: E8E901AC7C617C11
2 changed files with 37 additions and 3 deletions

View file

@ -1,9 +1,9 @@
package org.bukkit.craftbukkit.attribute;
import com.google.common.base.Preconditions;
import net.minecraft.server.AttributeBase;
import net.minecraft.server.AttributeMapBase;
import net.minecraft.server.IRegistry;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.Attribute;
@ -21,12 +21,16 @@ public class CraftAttributeMap implements Attributable {
@Override
public AttributeInstance getAttribute(Attribute attribute) {
Preconditions.checkArgument(attribute != null, "attribute");
net.minecraft.server.AttributeModifiable nms = handle.a(IRegistry.ATTRIBUTE.get(CraftNamespacedKey.toMinecraft(attribute.getKey())));
net.minecraft.server.AttributeModifiable nms = handle.a(toMinecraft(attribute));
return (nms == null) ? null : new CraftAttributeInstance(nms, attribute);
}
public static AttributeBase toMinecraft(Attribute attribute) {
return IRegistry.ATTRIBUTE.get(CraftNamespacedKey.toMinecraft(attribute.getKey()));
}
public static Attribute fromMinecraft(String nms) {
return Registry.ATTRIBUTE.get(NamespacedKey.minecraft(nms));
return Registry.ATTRIBUTE.get(CraftNamespacedKey.fromString(nms));
}
}

View file

@ -0,0 +1,30 @@
package org.bukkit.craftbukkit.attribute;
import net.minecraft.server.AttributeBase;
import net.minecraft.server.IRegistry;
import net.minecraft.server.MinecraftKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.support.AbstractTestingBase;
import org.junit.Assert;
import org.junit.Test;
public class AttributeTest extends AbstractTestingBase {
@Test
public void testToBukkit() {
for (MinecraftKey nms : IRegistry.ATTRIBUTE.keySet()) {
Attribute bukkit = CraftAttributeMap.fromMinecraft(nms.toString());
Assert.assertNotNull(nms.toString(), bukkit);
}
}
@Test
public void testToNMS() {
for (Attribute attribute : Attribute.values()) {
AttributeBase nms = CraftAttributeMap.toMinecraft(attribute);
Assert.assertNotNull(attribute.name(), nms);
}
}
}