aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0667-Get-entity-default-attributes.patch
blob: f4b8eb404deeba98581b74b5d96d9b006d4bf278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Fri, 20 Aug 2021 13:03:21 -0700
Subject: [PATCH] Get entity default attributes

== AT ==
public net.minecraft.world.entity.ai.attributes.AttributeSupplier getAttributeInstance(Lnet/minecraft/world/entity/ai/attributes/Attribute;)Lnet/minecraft/world/entity/ai/attributes/AttributeInstance;

diff --git a/src/main/java/io/papermc/paper/attribute/UnmodifiableAttributeInstance.java b/src/main/java/io/papermc/paper/attribute/UnmodifiableAttributeInstance.java
new file mode 100644
index 0000000000000000000000000000000000000000..12135ffeacd648f6bc4d7d327059ea1a7e8c79c4
--- /dev/null
+++ b/src/main/java/io/papermc/paper/attribute/UnmodifiableAttributeInstance.java
@@ -0,0 +1,30 @@
+package io.papermc.paper.attribute;
+
+import net.minecraft.world.entity.ai.attributes.AttributeInstance;
+import org.bukkit.attribute.Attribute;
+import org.bukkit.attribute.AttributeModifier;
+import org.bukkit.craftbukkit.attribute.CraftAttributeInstance;
+
+import java.util.Collection;
+
+public class UnmodifiableAttributeInstance extends CraftAttributeInstance {
+
+    public UnmodifiableAttributeInstance(AttributeInstance handle, Attribute attribute) {
+        super(handle, attribute);
+    }
+
+    @Override
+    public void setBaseValue(double d) {
+        throw new UnsupportedOperationException("Cannot modify default attributes");
+    }
+
+    @Override
+    public void addModifier(AttributeModifier modifier) {
+        throw new UnsupportedOperationException("Cannot modify default attributes");
+    }
+
+    @Override
+    public void removeModifier(AttributeModifier modifier) {
+        throw new UnsupportedOperationException("Cannot modify default attributes");
+    }
+}
diff --git a/src/main/java/io/papermc/paper/attribute/UnmodifiableAttributeMap.java b/src/main/java/io/papermc/paper/attribute/UnmodifiableAttributeMap.java
new file mode 100644
index 0000000000000000000000000000000000000000..57ea677907daa4689b8712b2686830197256a119
--- /dev/null
+++ b/src/main/java/io/papermc/paper/attribute/UnmodifiableAttributeMap.java
@@ -0,0 +1,33 @@
+package io.papermc.paper.attribute;
+
+import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
+import org.bukkit.attribute.Attributable;
+import org.bukkit.attribute.Attribute;
+import org.bukkit.attribute.AttributeInstance;
+import org.bukkit.craftbukkit.attribute.CraftAttribute;
+import org.bukkit.craftbukkit.attribute.CraftAttributeMap;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+public class UnmodifiableAttributeMap implements Attributable {
+
+    private final AttributeSupplier handle;
+
+    public UnmodifiableAttributeMap(@NotNull AttributeSupplier handle) {
+        this.handle = handle;
+    }
+
+    @Override
+    public @Nullable AttributeInstance getAttribute(@NotNull Attribute attribute) {
+        net.minecraft.world.entity.ai.attributes.Attribute nmsAttribute = CraftAttribute.bukkitToMinecraft(attribute);
+        if (!this.handle.hasAttribute(nmsAttribute)) {
+            return null;
+        }
+        return new UnmodifiableAttributeInstance(this.handle.getAttributeInstance(nmsAttribute), attribute);
+    }
+
+    @Override
+    public void registerAttribute(@NotNull Attribute attribute) {
+        throw new UnsupportedOperationException("Cannot register new attributes here");
+    }
+}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntityType.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntityType.java
index 33be9fa46c3bdb4958b32d30c730ceff075aaaea..85602b01b3bef4cd223a86b9b7cc85dbb4b3a105 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntityType.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntityType.java
@@ -164,6 +164,21 @@ public class CraftEntityType<E extends Entity> extends EntityType<E> {
     }
     // Paper end
 
+    // Paper start - default entity attributes
+    @Override
+    public boolean hasDefaultAttributes() {
+        return net.minecraft.world.entity.ai.attributes.DefaultAttributes.hasSupplier(this.entityType);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public org.bukkit.attribute.Attributable getDefaultAttributes() {
+        Preconditions.checkArgument(this.hasDefaultAttributes(), this.key + " doesn't have default attributes");
+        net.minecraft.world.entity.ai.attributes.AttributeSupplier supplier = net.minecraft.world.entity.ai.attributes.DefaultAttributes.getSupplier((net.minecraft.world.entity.EntityType<? extends net.minecraft.world.entity.LivingEntity>) this.entityType);
+        return new io.papermc.paper.attribute.UnmodifiableAttributeMap(supplier);
+    }
+    // Paper end
+
     public static class CraftEntityTypeRegistry extends CraftRegistry<EntityType<?>, net.minecraft.world.entity.EntityType<?>> {
         private static final Map<NamespacedKey, Class<? extends Entity>> CLASS_MAP = new HashMap<>();
         private static final Map<NamespacedKey, Boolean> SPAWNABLE = new HashMap<>();
diff --git a/src/test/java/io/papermc/paper/attribute/EntityTypeAttributesTest.java b/src/test/java/io/papermc/paper/attribute/EntityTypeAttributesTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..7b999deba66aa6d22cd7520f6c13550a44ca327d
--- /dev/null
+++ b/src/test/java/io/papermc/paper/attribute/EntityTypeAttributesTest.java
@@ -0,0 +1,39 @@
+package io.papermc.paper.attribute;
+
+import org.bukkit.attribute.Attributable;
+import org.bukkit.attribute.Attribute;
+import org.bukkit.attribute.AttributeInstance;
+import org.bukkit.attribute.AttributeModifier;
+import org.bukkit.entity.EntityType;
+import org.bukkit.support.AbstractTestingBase;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+public class EntityTypeAttributesTest extends AbstractTestingBase {
+
+    @Test
+    public void testIllegalEntity() {
+        assertFalse(EntityType.EGG.hasDefaultAttributes());
+        assertThrows(IllegalArgumentException.class, () -> EntityType.EGG.getDefaultAttributes());
+    }
+
+    @Test
+    public void testLegalEntity() {
+        assertTrue(EntityType.ZOMBIE.hasDefaultAttributes());
+        EntityType.ZOMBIE.getDefaultAttributes();
+    }
+
+    @Test
+    public void testUnmodifiabilityOfAttributable() {
+        Attributable attributable = EntityType.ZOMBIE.getDefaultAttributes();
+        assertThrows(UnsupportedOperationException.class, () -> attributable.registerAttribute(Attribute.GENERIC_ATTACK_DAMAGE));
+        AttributeInstance instance = attributable.getAttribute(Attribute.GENERIC_FOLLOW_RANGE);
+        assertNotNull(instance);
+        assertThrows(UnsupportedOperationException.class, () -> instance.addModifier(new AttributeModifier("test", 3, AttributeModifier.Operation.ADD_NUMBER)));
+        assertThrows(UnsupportedOperationException.class, () -> instance.setBaseValue(3.2));
+    }
+}