From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Encode42 <me@encode42.dev>
Date: Wed, 24 Mar 2021 17:59:54 -0400
Subject: [PATCH] Config to allow for unsafe enchants


diff --git a/src/main/java/net/minecraft/server/commands/EnchantCommand.java b/src/main/java/net/minecraft/server/commands/EnchantCommand.java
index 514cc0e8805045549eacde6c280859aa2dc4a91d..a3ac6bebcef7b1e1f9c3ebe525656a15e13f9be8 100644
--- a/src/main/java/net/minecraft/server/commands/EnchantCommand.java
+++ b/src/main/java/net/minecraft/server/commands/EnchantCommand.java
@@ -44,7 +44,7 @@ public class EnchantCommand {
     }
 
     private static int enchant(CommandSourceStack source, Collection<? extends Entity> targets, Enchantment enchantment, int level) throws CommandSyntaxException {
-        if (level > enchantment.getMaxLevel()) {
+        if (!net.pl3x.purpur.PurpurConfig.allowUnsafeEnchants && level > enchantment.getMaxLevel()) { // Purpur
             throw ERROR_LEVEL_TOO_HIGH.create(level, enchantment.getMaxLevel());
         } else {
             int i = 0;
@@ -54,7 +54,7 @@ public class EnchantCommand {
                     LivingEntity livingEntity = (LivingEntity)entity;
                     ItemStack itemStack = livingEntity.getMainHandItem();
                     if (!itemStack.isEmpty()) {
-                        if (enchantment.canEnchant(itemStack) && EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantments(itemStack).keySet(), enchantment)) {
+                        if ((enchantment.canEnchant(itemStack) && EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantments(itemStack).keySet(), enchantment)) || (net.pl3x.purpur.PurpurConfig.allowUnsafeEnchants && !itemStack.hasEnchantment(enchantment))) { // Purpur
                             itemStack.enchant(enchantment, level);
                             ++i;
                         } else if (targets.size() == 1) {
diff --git a/src/main/java/net/minecraft/world/inventory/AnvilMenu.java b/src/main/java/net/minecraft/world/inventory/AnvilMenu.java
index 410ac71efff92dfa1f1e11895d0f5bf3fca1be17..f380659b261253e327f018ce9b54b15195ad65d7 100644
--- a/src/main/java/net/minecraft/world/inventory/AnvilMenu.java
+++ b/src/main/java/net/minecraft/world/inventory/AnvilMenu.java
@@ -209,7 +209,7 @@ public class AnvilMenu extends ItemCombinerMenu {
                             int i2 = (Integer) map1.get(enchantment);
 
                             i2 = l1 == i2 ? i2 + 1 : Math.max(i2, l1);
-                            boolean flag3 = canDoUnsafeEnchants || enchantment.canEnchant(itemstack); // Purpur
+                            boolean flag3 = canDoUnsafeEnchants || net.pl3x.purpur.PurpurConfig.allowUnsafeEnchants || enchantment.canEnchant(itemstack); // Purpur
 
                             if (this.player.getAbilities().instabuild || itemstack.is(Items.ENCHANTED_BOOK)) {
                                 flag3 = true;
@@ -221,7 +221,7 @@ public class AnvilMenu extends ItemCombinerMenu {
                                 Enchantment enchantment1 = (Enchantment) iterator1.next();
 
                                 if (enchantment1 != enchantment && !enchantment.isCompatibleWith(enchantment1)) {
-                                    flag3 = canDoUnsafeEnchants; // Purpur
+                                    flag3 = canDoUnsafeEnchants || net.pl3x.purpur.PurpurConfig.allowUnsafeEnchants; // Purpur
                                     ++i;
                                 }
                             }
@@ -332,7 +332,7 @@ public class AnvilMenu extends ItemCombinerMenu {
             org.bukkit.craftbukkit.event.CraftEventFactory.callPrepareAnvilEvent(this.getBukkitView(), itemstack1); // CraftBukkit
             this.broadcastChanges();
             // Purpur start
-            if (canDoUnsafeEnchants && itemstack1 != ItemStack.EMPTY) {
+            if ((canDoUnsafeEnchants || net.pl3x.purpur.PurpurConfig.allowUnsafeEnchants) && itemstack1 != ItemStack.EMPTY) {
                 ((ServerPlayer) player).connection.send(new ClientboundContainerSetSlotPacket(containerId, incrementStateId(), 2, itemstack1));
                 ((ServerPlayer) player).connection.send(new ClientboundContainerSetDataPacket(containerId, 0, cost.get()));
             }
diff --git a/src/main/java/net/minecraft/world/item/ItemStack.java b/src/main/java/net/minecraft/world/item/ItemStack.java
index d1b4fe830170ddee2b6f39eba29e7068e6ae37a3..115f8eb919e90bc4a79db1d0f7875a31cc962f07 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -1145,6 +1145,12 @@ public final class ItemStack {
         return this.tag != null && this.tag.contains("Enchantments", 9) ? !this.tag.getList("Enchantments", 10).isEmpty() : false;
     }
 
+    // Purpur start
+    public boolean hasEnchantment(Enchantment enchantment) {
+        return isEnchanted() && EnchantmentHelper.deserializeEnchantments(getEnchantmentTags()).containsKey(enchantment);
+    }
+    // Purpur end
+
     public void addTagElement(String key, net.minecraft.nbt.Tag tag) {
         this.getOrCreateTag().put(key, tag);
     }
diff --git a/src/main/java/net/pl3x/purpur/PurpurConfig.java b/src/main/java/net/pl3x/purpur/PurpurConfig.java
index 80fc65c5f3a89241fb7aadaccbf730a5f05dec64..01330b33775a340237df6175c208162cec0379d6 100644
--- a/src/main/java/net/pl3x/purpur/PurpurConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurConfig.java
@@ -379,6 +379,7 @@ public class PurpurConfig {
 
     public static boolean allowInfinityMending = false;
     public static boolean allowCrossbowInfinity = false;
+    public static boolean allowUnsafeEnchants = false;
     private static void enchantmentSettings() {
         if (version < 5) {
             boolean oldValue = getBoolean("settings.enchantment.allow-infinite-and-mending-together", false);
@@ -387,6 +388,7 @@ public class PurpurConfig {
         }
         allowInfinityMending = getBoolean("settings.enchantment.allow-infinity-and-mending-together", allowInfinityMending);
         allowCrossbowInfinity = getBoolean("settings.enchantment.allow-infinity-on-crossbow", allowCrossbowInfinity);
+        allowUnsafeEnchants = getBoolean("settings.enchantment.allow-unsafe-enchants", allowUnsafeEnchants);
     }
 
     public static boolean endermanShortHeight = false;
