From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Encode42 Date: Wed, 24 Mar 2021 17:59:54 -0400 Subject: [PATCH] Config to allow 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 99695e38b6a10c3cffda6e453f9f0619c7406cc0..2283f69607cb769545c85bcae940ac956779e80b 100644 --- a/src/main/java/net/minecraft/server/commands/EnchantCommand.java +++ b/src/main/java/net/minecraft/server/commands/EnchantCommand.java @@ -70,7 +70,7 @@ public class EnchantCommand { private static int enchant(CommandSourceStack source, Collection targets, Holder enchantment, int level) throws CommandSyntaxException { Enchantment enchantment2 = enchantment.value(); - if (level > enchantment2.getMaxLevel()) { + if (!org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchantCommand && level > enchantment2.getMaxLevel()) { // Purpur - Config to allow unsafe enchants throw ERROR_LEVEL_TOO_HIGH.create(level, enchantment2.getMaxLevel()); } else { int i = 0; @@ -81,7 +81,7 @@ public class EnchantCommand { ItemStack itemStack = livingEntity.getMainHandItem(); if (!itemStack.isEmpty()) { if (enchantment2.canEnchant(itemStack) - && EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantmentsForCrafting(itemStack).keySet(), enchantment)) { + && EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantmentsForCrafting(itemStack).keySet(), enchantment) || (org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchantCommand && !itemStack.hasEnchantment(enchantment))) { // Purpur - Config to allow unsafe enchants 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 2d300905c05c7c23a4da30b3651b331381a1dc0d..30746b3ae45840206bf23e811a9819f3b31c9814 100644 --- a/src/main/java/net/minecraft/world/inventory/AnvilMenu.java +++ b/src/main/java/net/minecraft/world/inventory/AnvilMenu.java @@ -231,7 +231,10 @@ public class AnvilMenu extends ItemCombinerMenu { i2 = l1 == i2 ? i2 + 1 : Math.max(i2, l1); Enchantment enchantment = (Enchantment) holder.value(); - boolean flag3 = enchantment.canEnchant(itemstack); + // Purpur start - Config to allow unsafe enchants + boolean flag3 = this.canDoUnsafeEnchants || (org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchants && org.purpurmc.purpur.PurpurConfig.allowInapplicableEnchants) || enchantment.canEnchant(itemstack); // whether the enchantment can be applied on specific item type + boolean flag4 = true; // whether two incompatible enchantments can be applied on a single item + // Purpur end - Config to allow unsafe enchants if (this.player.getAbilities().instabuild || itemstack.is(Items.ENCHANTED_BOOK)) { flag3 = true; @@ -243,16 +246,22 @@ public class AnvilMenu extends ItemCombinerMenu { Holder holder1 = (Holder) iterator1.next(); if (!holder1.equals(holder) && !Enchantment.areCompatible(holder, holder1)) { - flag3 = this.canDoUnsafeEnchants; // Purpur - Anvil API + flag4 = this.canDoUnsafeEnchants || (org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchants && org.purpurmc.purpur.PurpurConfig.allowIncompatibleEnchants); // Purpur - Anvil API // Purpur - flag3 -> flag4 - Config to allow unsafe enchants + // Purpur start - Config to allow unsafe enchants + if (!flag4 && org.purpurmc.purpur.PurpurConfig.replaceIncompatibleEnchants) { + iterator1.remove(); // replace current enchant with the incompatible one trying to be applied + flag4 = true; + } + // Purpur end - Config to allow unsafe enchants ++i; } } - if (!flag3) { + if (!flag3 || !flag4) { // Purpur - Config to allow unsafe enchants flag2 = true; } else { flag1 = true; - if (i2 > enchantment.getMaxLevel()) { + if ((!org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchants || !org.purpurmc.purpur.PurpurConfig.allowHigherEnchantsLevels) && i2 > enchantment.getMaxLevel()) { // Purpur - Config to allow unsafe enchants i2 = enchantment.getMaxLevel(); } @@ -378,7 +387,7 @@ public class AnvilMenu extends ItemCombinerMenu { this.broadcastChanges(); // Purpur start - Anvil API - if (this.canDoUnsafeEnchants && itemstack1 != ItemStack.EMPTY) { + if ((this.canDoUnsafeEnchants || org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchants) && itemstack1 != ItemStack.EMPTY) { // Purpur - Config to allow unsafe enchants ((ServerPlayer) this.player).connection.send(new ClientboundContainerSetSlotPacket(this.containerId, this.incrementStateId(), 2, itemstack1)); ((ServerPlayer) this.player).connection.send(new ClientboundContainerSetDataPacket(this.containerId, 0, this.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 50d82217aade8bb6db557df9655e5746608af3ef..fab7bebb8c7fd21a3bcfbd2f04702eb56f9eb310 100644 --- a/src/main/java/net/minecraft/world/item/ItemStack.java +++ b/src/main/java/net/minecraft/world/item/ItemStack.java @@ -1251,6 +1251,12 @@ public final class ItemStack implements DataComponentHolder { return !((ItemEnchantments) this.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY)).isEmpty(); } + // Purpur start - Config to allow unsafe enchants + public boolean hasEnchantment(Holder enchantment) { + return this.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY).getLevel(enchantment) > 0; + } + // Purpur end - Config to allow unsafe enchants + public ItemEnchantments getEnchantments() { return (ItemEnchantments) this.getOrDefault(DataComponents.ENCHANTMENTS, ItemEnchantments.EMPTY); } diff --git a/src/main/java/org/purpurmc/purpur/PurpurConfig.java b/src/main/java/org/purpurmc/purpur/PurpurConfig.java index 3c1b41f0ae58669d6fae640e8763c43e74bd0239..e19346e82ace64d5439d2607709682a9381edfe7 100644 --- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java +++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java @@ -285,6 +285,29 @@ public class PurpurConfig { cryingObsidianValidForPortalFrame = getBoolean("settings.blocks.crying_obsidian.valid-for-portal-frame", cryingObsidianValidForPortalFrame); } + public static boolean allowUnsafeEnchants = false; + public static boolean allowInapplicableEnchants = true; + public static boolean allowIncompatibleEnchants = true; + public static boolean allowHigherEnchantsLevels = true; + public static boolean allowUnsafeEnchantCommand = false; + public static boolean replaceIncompatibleEnchants = false; + private static void enchantmentSettings() { + if (version < 30) { + boolean oldValue = getBoolean("settings.enchantment.allow-unsafe-enchants", false); + set("settings.enchantment.anvil.allow-unsafe-enchants", oldValue); + set("settings.enchantment.anvil.allow-inapplicable-enchants", true); + set("settings.enchantment.anvil.allow-incompatible-enchants", true); + set("settings.enchantment.anvil.allow-higher-enchants-levels", true); + set("settings.enchantment.allow-unsafe-enchants", null); + } + allowUnsafeEnchants = getBoolean("settings.enchantment.anvil.allow-unsafe-enchants", allowUnsafeEnchants); + allowInapplicableEnchants = getBoolean("settings.enchantment.anvil.allow-inapplicable-enchants", allowInapplicableEnchants); + allowIncompatibleEnchants = getBoolean("settings.enchantment.anvil.allow-incompatible-enchants", allowIncompatibleEnchants); + allowHigherEnchantsLevels = getBoolean("settings.enchantment.anvil.allow-higher-enchants-levels", allowHigherEnchantsLevels); + allowUnsafeEnchantCommand = getBoolean("settings.enchantment.allow-unsafe-enchant-command", allowUnsafeEnchants); // allowUnsafeEnchants as default for backwards compatability + replaceIncompatibleEnchants = getBoolean("settings.enchantment.anvil.replace-incompatible-enchants", replaceIncompatibleEnchants); + } + public static boolean endermanShortHeight = false; private static void entitySettings() { endermanShortHeight = getBoolean("settings.entity.enderman.short-height", endermanShortHeight);