2021-06-11 12:02:28 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Thu, 6 May 2021 19:57:58 -0700
Subject: [PATCH] More Enchantment API
diff --git a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
2021-06-17 19:37:37 +00:00
index eb99e0c2462a2d1ab4508a5c3f1580b6e31d7465..227aeb002d8c23b6dba0af268f0a3297454cb4e3 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
+++ b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
2021-06-15 02:59:31 +00:00
@@ -71,7 +71,7 @@ public class CraftEnchantment extends Enchantment {
2021-06-11 12:02:28 +00:00
@Override
public boolean isCursed() {
2021-06-15 02:59:31 +00:00
- return this.target instanceof BindingCurseEnchantment || this.target instanceof VanishingCurseEnchantment;
+ return this.target.isCurse(); // Paper
2021-06-11 12:02:28 +00:00
}
@Override
2021-06-15 02:59:31 +00:00
@@ -192,6 +192,45 @@ public class CraftEnchantment extends Enchantment {
2021-06-11 12:02:28 +00:00
public net.kyori.adventure.text.Component displayName(int level) {
2021-06-17 19:37:37 +00:00
return io.papermc.paper.adventure.PaperAdventure.asAdventure(getHandle().getFullname(level));
2021-06-11 12:02:28 +00:00
}
+
+ @Override
+ public boolean isTradeable() {
+ return target.isTradeable();
+ }
+
+ @Override
+ public boolean isDiscoverable() {
+ return target.isDiscoverable();
+ }
+
+ @Override
+ public io.papermc.paper.enchantments.EnchantmentRarity getRarity() {
+ return fromNMSRarity(target.getRarity());
+ }
+
+ @Override
+ public float getDamageIncrease(int level, org.bukkit.entity.EntityCategory entityCategory) {
2021-06-15 02:59:31 +00:00
+ return target.getDamageBonus(level, org.bukkit.craftbukkit.entity.CraftLivingEntity.fromBukkitEntityCategory(entityCategory));
2021-06-11 12:02:28 +00:00
+ }
+
+ @Override
+ public java.util.Set<org.bukkit.inventory.EquipmentSlot> getActiveSlots() {
2021-06-15 02:59:31 +00:00
+ return java.util.stream.Stream.of(target.slots).map(org.bukkit.craftbukkit.CraftEquipmentSlot::getSlot).collect(java.util.stream.Collectors.toSet());
2021-06-11 12:02:28 +00:00
+ }
+
+ public static io.papermc.paper.enchantments.EnchantmentRarity fromNMSRarity(net.minecraft.world.item.enchantment.Enchantment.Rarity nmsRarity) {
+ if (nmsRarity == net.minecraft.world.item.enchantment.Enchantment.Rarity.COMMON) {
+ return io.papermc.paper.enchantments.EnchantmentRarity.COMMON;
+ } else if (nmsRarity == net.minecraft.world.item.enchantment.Enchantment.Rarity.UNCOMMON) {
+ return io.papermc.paper.enchantments.EnchantmentRarity.UNCOMMON;
+ } else if (nmsRarity == net.minecraft.world.item.enchantment.Enchantment.Rarity.RARE) {
+ return io.papermc.paper.enchantments.EnchantmentRarity.RARE;
+ } else if (nmsRarity == net.minecraft.world.item.enchantment.Enchantment.Rarity.VERY_RARE) {
+ return io.papermc.paper.enchantments.EnchantmentRarity.VERY_RARE;
+ }
+
+ throw new IllegalArgumentException(String.format("Unable to convert %s to a enum value of %s.", nmsRarity, io.papermc.paper.enchantments.EnchantmentRarity.class));
+ }
// Paper end
public net.minecraft.world.item.enchantment.Enchantment getHandle() {
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
2021-06-15 04:01:27 +00:00
index 0afcc71de94e90eabf3a2efb88b311bf5d16187e..526beeac806d65c53c117be7702ce6cc3c6ec1c1 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
2021-06-15 04:01:27 +00:00
@@ -840,5 +840,21 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
2021-06-11 12:02:28 +00:00
public void setHurtDirection(float hurtDirection) {
2021-06-15 02:59:31 +00:00
getHandle().hurtDir = hurtDirection;
2021-06-11 12:02:28 +00:00
}
+
+ public static MobType fromBukkitEntityCategory(EntityCategory entityCategory) {
+ switch (entityCategory) {
+ case NONE:
+ return MobType.UNDEFINED;
+ case UNDEAD:
+ return MobType.UNDEAD;
+ case ARTHROPOD:
+ return MobType.ARTHROPOD;
+ case ILLAGER:
+ return MobType.ILLAGER;
+ case WATER:
+ return MobType.WATER;
+ }
+ throw new IllegalArgumentException(entityCategory + " is an unrecognized entity category");
+ }
// Paper end
}
diff --git a/src/test/java/io/papermc/paper/enchantments/EnchantmentRarityTest.java b/src/test/java/io/papermc/paper/enchantments/EnchantmentRarityTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..62b56b5b43696b03fc72cac59f986d006edc3f76
--- /dev/null
+++ b/src/test/java/io/papermc/paper/enchantments/EnchantmentRarityTest.java
@@ -0,0 +1,18 @@
+package io.papermc.paper.enchantments;
+
+import net.minecraft.world.item.enchantment.Enchantment.Rarity;
+import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+
+public class EnchantmentRarityTest {
+
+ @Test
+ public void test() {
+ for (Rarity nmsRarity : Rarity.values()) {
+ // Will throw exception if a bukkit counterpart is not found
+ CraftEnchantment.fromNMSRarity(nmsRarity);
+ }
+ }
+}
diff --git a/src/test/java/io/papermc/paper/entity/EntityCategoryTest.java b/src/test/java/io/papermc/paper/entity/EntityCategoryTest.java
new file mode 100644
2021-06-15 02:59:31 +00:00
index 0000000000000000000000000000000000000000..b9824b1f9491304ceb91be18f4f3b3068526bb33
2021-06-11 12:02:28 +00:00
--- /dev/null
+++ b/src/test/java/io/papermc/paper/entity/EntityCategoryTest.java
@@ -0,0 +1,34 @@
+package io.papermc.paper.entity;
+
+import com.google.common.base.Joiner;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
2021-06-15 02:59:31 +00:00
+import net.minecraft.world.entity.MobType;
2021-06-11 12:02:28 +00:00
+import org.bukkit.craftbukkit.entity.CraftLivingEntity;
+import org.bukkit.entity.EntityCategory;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertTrue;
+
+public class EntityCategoryTest {
+
+ @Test
+ public void test() throws IllegalAccessException {
+
2021-06-15 02:59:31 +00:00
+ Map<MobType, String> enumMonsterTypeFieldMap = Maps.newHashMap();
+ for (Field field : MobType.class.getDeclaredFields()) {
+ if (field.getType() == MobType.class) {
+ enumMonsterTypeFieldMap.put( (MobType) field.get(null), field.getName());
2021-06-11 12:02:28 +00:00
+ }
+ }
+
+ for (EntityCategory entityCategory : EntityCategory.values()) {
+ enumMonsterTypeFieldMap.remove(CraftLivingEntity.fromBukkitEntityCategory(entityCategory));
+ }
2021-06-15 02:59:31 +00:00
+ assertTrue(MobType.class.getName() + " instance(s): " + Joiner.on(", ").join(enumMonsterTypeFieldMap.values()) + " do not have bukkit equivalents", enumMonsterTypeFieldMap.size() == 0);
2021-06-11 12:02:28 +00:00
+ }
+}