Files
Purpur/patches/server/0154-Config-to-allow-for-unsafe-enchants.patch
2023-06-14 23:11:18 -07:00

126 lines
8.6 KiB
Diff

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 664cbce2e06fcb95d3d3d6c5302fc9119f938925..bc9778c705d23acd84fa1cdeff6b403b4cda3686 100644
--- a/src/main/java/net/minecraft/server/commands/EnchantCommand.java
+++ b/src/main/java/net/minecraft/server/commands/EnchantCommand.java
@@ -48,7 +48,7 @@ public class EnchantCommand {
private static int enchant(CommandSourceStack source, Collection<? extends Entity> targets, Holder<Enchantment> enchantment, int level) throws CommandSyntaxException {
Enchantment enchantment2 = enchantment.value();
- if (level > enchantment2.getMaxLevel()) {
+ if (!org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchantCommand && level > enchantment2.getMaxLevel()) { // Purpur
throw ERROR_LEVEL_TOO_HIGH.create(level, enchantment2.getMaxLevel());
} else {
int i = 0;
@@ -58,7 +58,7 @@ public class EnchantCommand {
LivingEntity livingEntity = (LivingEntity)entity;
ItemStack itemStack = livingEntity.getMainHandItem();
if (!itemStack.isEmpty()) {
- if (enchantment2.canEnchant(itemStack) && EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantments(itemStack).keySet(), enchantment2)) {
+ if ((enchantment2.canEnchant(itemStack) && EnchantmentHelper.isEnchantmentCompatible(EnchantmentHelper.getEnchantments(itemStack).keySet(), enchantment2)) || (org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchantCommand && !itemStack.hasEnchantment(enchantment2))) { // Purpur
itemStack.enchant(enchantment2, 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 8a7c30e316db4960b0b62ca0e366c19febead214..29ba769c5f803ce2dc5285e65284febf9ee28eb2 100644
--- a/src/main/java/net/minecraft/world/inventory/AnvilMenu.java
+++ b/src/main/java/net/minecraft/world/inventory/AnvilMenu.java
@@ -228,7 +228,8 @@ 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 || (org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchants && org.purpurmc.purpur.PurpurConfig.allowInapplicableEnchants) || enchantment.canEnchant(itemstack); // Purpur
+ boolean flag4 = true; // Purpur
if (this.player.getAbilities().instabuild || itemstack.is(Items.ENCHANTED_BOOK)) {
flag3 = true;
@@ -240,16 +241,16 @@ public class AnvilMenu extends ItemCombinerMenu {
Enchantment enchantment1 = (Enchantment) iterator1.next();
if (enchantment1 != enchantment && !enchantment.isCompatibleWith(enchantment1)) {
- flag3 = canDoUnsafeEnchants; // Purpur
+ flag4 = canDoUnsafeEnchants || (org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchants && org.purpurmc.purpur.PurpurConfig.allowIncompatibleEnchants); // Purpur flag3 -> flag4
++i;
}
}
- if (!flag3) {
+ if (!flag3 || !flag4) { // Purpur
flag2 = true;
} else {
flag1 = true;
- if (i2 > enchantment.getMaxLevel()) {
+ if ((!org.purpurmc.purpur.PurpurConfig.allowUnsafeEnchants || !org.purpurmc.purpur.PurpurConfig.allowHigherEnchantsLevels) && i2 > enchantment.getMaxLevel()) { // Purpur
i2 = enchantment.getMaxLevel();
}
@@ -389,7 +390,7 @@ public class AnvilMenu extends ItemCombinerMenu {
sendAllDataToRemote(); // CraftBukkit - SPIGOT-6686: Always send completed inventory to stay in sync with client
this.broadcastChanges();
// Purpur start
- if (canDoUnsafeEnchants && itemstack1 != ItemStack.EMPTY) {
+ if ((canDoUnsafeEnchants || org.purpurmc.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 190a446ba6c41a8da7e11b953c8fb2cb8119c412..43ed450fbd7472495b1965dcbdc7ad107ba91dcc 100644
--- a/src/main/java/net/minecraft/world/item/ItemStack.java
+++ b/src/main/java/net/minecraft/world/item/ItemStack.java
@@ -1202,6 +1202,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, Tag element) {
this.getOrCreateTag().put(key, element);
}
diff --git a/src/main/java/org/purpurmc/purpur/PurpurConfig.java b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
index a191d631e4c5401e1fafc0d9a4a4d5eed4f300af..80f47a113b48670695a5ffe67dd6e0f800970e1e 100644
--- a/src/main/java/org/purpurmc/purpur/PurpurConfig.java
+++ b/src/main/java/org/purpurmc/purpur/PurpurConfig.java
@@ -285,14 +285,32 @@ public class PurpurConfig {
public static boolean allowInfinityMending = false;
public static boolean allowCrossbowInfinity = false;
+ 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;
private static void enchantmentSettings() {
if (version < 5) {
boolean oldValue = getBoolean("settings.enchantment.allow-infinite-and-mending-together", false);
set("settings.enchantment.allow-infinity-and-mending-together", oldValue);
set("settings.enchantment.allow-infinite-and-mending-together", null);
}
+ 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);
+ }
allowInfinityMending = getBoolean("settings.enchantment.allow-infinity-and-mending-together", allowInfinityMending);
allowCrossbowInfinity = getBoolean("settings.enchantment.allow-infinity-on-crossbow", allowCrossbowInfinity);
+ 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
}
public static boolean endermanShortHeight = false;