Paper/Remapped-Spigot-Server-Patches/0734-More-Enchantment-API.patch
2021-06-11 13:56:17 +02:00

212 lines
9.0 KiB
Diff

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/net/minecraft/world/item/enchantment/Enchantment.java b/src/main/java/net/minecraft/world/item/enchantment/Enchantment.java
index c23ec1b31950471905c65e46273ae105de853d9b..b994a7aca0ce01b5c0d44b9b126295ffcd2f795d 100644
--- a/src/main/java/net/minecraft/world/item/enchantment/Enchantment.java
+++ b/src/main/java/net/minecraft/world/item/enchantment/Enchantment.java
@@ -17,7 +17,7 @@ import net.minecraft.world.item.ItemStack;
public abstract class Enchantment {
- private final EquipmentSlot[] slots;
+ private final EquipmentSlot[] slots; public final EquipmentSlot[] getSlots() { return this.slots; } // Paper - OBFHELPER
private final Enchantment.Rarity rarity;
public final EnchantmentCategory category;
@Nullable
@@ -46,6 +46,7 @@ public abstract class Enchantment {
return map;
}
+ public Enchantment.Rarity getRarity() { return getRarity(); } // Paper - OBFHELPER
public Enchantment.Rarity getRarity() {
return this.rarity;
}
@@ -70,6 +71,7 @@ public abstract class Enchantment {
return 0;
}
+ public float getDamageIncrease(int level, MobType enumMonsterType) { return getDamageBonus(level, enumMonsterType); } // Paper - OBFHELPER
public float getDamageBonus(int level, MobType group) {
return 0.0F;
}
@@ -123,14 +125,17 @@ public abstract class Enchantment {
return false;
}
+ public boolean isCursed() { return isCurse(); } // Paper - OBFHELPER
public boolean isCurse() {
return false;
}
+ public boolean isTradeable() { return isTradeable(); } // Paper - OBFHELPER
public boolean isTradeable() {
return true;
}
+ public boolean isDiscoverable() { return isDiscoverable(); } // Paper - OBFHELPER
public boolean isDiscoverable() {
return true;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
index b1ffe6c7a5915f00a476e88f3a38349b740b4910..20858bb8463ff86c96b5fcdeca455c20c696870f 100644
--- a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
+++ b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
@@ -1,8 +1,6 @@
package org.bukkit.craftbukkit.enchantments;
import net.minecraft.core.Registry;
-import net.minecraft.world.item.enchantment.BindingCurseEnchantment;
-import net.minecraft.world.item.enchantment.VanishingCurseEnchantment;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
@@ -71,7 +69,7 @@ public class CraftEnchantment extends Enchantment {
@Override
public boolean isCursed() {
- return target instanceof BindingCurseEnchantment || target instanceof VanishingCurseEnchantment;
+ return target.isCursed(); // Paper
}
@Override
@@ -192,6 +190,45 @@ public class CraftEnchantment extends Enchantment {
public net.kyori.adventure.text.Component displayName(int level) {
return io.papermc.paper.adventure.PaperAdventure.asAdventure(getHandle().getTranslationComponentForLevel(level));
}
+
+ @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) {
+ return target.getDamageIncrease(level, org.bukkit.craftbukkit.entity.CraftLivingEntity.fromBukkitEntityCategory(entityCategory));
+ }
+
+ @Override
+ public java.util.Set<org.bukkit.inventory.EquipmentSlot> getActiveSlots() {
+ return java.util.stream.Stream.of(target.getSlots()).map(org.bukkit.craftbukkit.CraftEquipmentSlot::getSlot).collect(java.util.stream.Collectors.toSet());
+ }
+
+ 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
index eefb6bd580ea176c3a242695ab4af46e7c61b492..25ba7a26c951fc5e4638bdb0db36e94d3e08fb2e 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftLivingEntity.java
@@ -835,5 +835,21 @@ public class CraftLivingEntity extends CraftEntity implements LivingEntity {
public void setHurtDirection(float hurtDirection) {
getHandle().setHurtDirection(hurtDirection);
}
+
+ 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
index 0000000000000000000000000000000000000000..adb0e5b4268fa115b814143cf29d9a3688e4bc17
--- /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;
+import net.minecraft.world.entity.EnumMonsterType;
+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 {
+
+ Map<EnumMonsterType, String> enumMonsterTypeFieldMap = Maps.newHashMap();
+ for (Field field : EnumMonsterType.class.getDeclaredFields()) {
+ if (field.getType() == EnumMonsterType.class) {
+ enumMonsterTypeFieldMap.put( (EnumMonsterType) field.get(null), field.getName());
+ }
+ }
+
+ for (EntityCategory entityCategory : EntityCategory.values()) {
+ enumMonsterTypeFieldMap.remove(CraftLivingEntity.fromBukkitEntityCategory(entityCategory));
+ }
+ assertTrue(EnumMonsterType.class.getName() + " instance(s): " + Joiner.on(", ").join(enumMonsterTypeFieldMap.values()) + " do not have bukkit equivalents", enumMonsterTypeFieldMap.size() == 0);
+ }
+}