diff options
Diffstat (limited to 'patch-remap/mache-vineflower-stripped/net/minecraft/world/inventory/EnchantmentMenu.java.patch')
-rw-r--r-- | patch-remap/mache-vineflower-stripped/net/minecraft/world/inventory/EnchantmentMenu.java.patch | 261 |
1 files changed, 261 insertions, 0 deletions
diff --git a/patch-remap/mache-vineflower-stripped/net/minecraft/world/inventory/EnchantmentMenu.java.patch b/patch-remap/mache-vineflower-stripped/net/minecraft/world/inventory/EnchantmentMenu.java.patch new file mode 100644 index 0000000000..1a830e8631 --- /dev/null +++ b/patch-remap/mache-vineflower-stripped/net/minecraft/world/inventory/EnchantmentMenu.java.patch @@ -0,0 +1,261 @@ +--- a/net/minecraft/world/inventory/EnchantmentMenu.java ++++ b/net/minecraft/world/inventory/EnchantmentMenu.java +@@ -25,6 +23,22 @@ + import net.minecraft.world.level.Level; + import net.minecraft.world.level.block.Blocks; + import net.minecraft.world.level.block.EnchantmentTableBlock; ++// CraftBukkit start ++import java.util.Map; ++import net.minecraft.world.item.enchantment.Enchantment; ++import net.minecraft.world.item.enchantment.EnchantmentHelper; ++import net.minecraft.world.item.enchantment.EnchantmentInstance; ++import org.bukkit.Location; ++import org.bukkit.NamespacedKey; ++import org.bukkit.craftbukkit.inventory.CraftInventoryEnchanting; ++import org.bukkit.craftbukkit.inventory.CraftInventoryView; ++import org.bukkit.craftbukkit.inventory.CraftItemStack; ++import org.bukkit.craftbukkit.util.CraftNamespacedKey; ++import org.bukkit.enchantments.EnchantmentOffer; ++import org.bukkit.event.enchantment.EnchantItemEvent; ++import org.bukkit.event.enchantment.PrepareItemEnchantEvent; ++import org.bukkit.entity.Player; ++// CraftBukkit end + + public class EnchantmentMenu extends AbstractContainerMenu { + static final ResourceLocation EMPTY_SLOT_LAPIS_LAZULI = new ResourceLocation("item/empty_slot_lapis_lazuli"); +@@ -36,11 +45,15 @@ + } + }; + private final ContainerLevelAccess access; +- private final RandomSource random = RandomSource.create(); +- private final DataSlot enchantmentSeed = DataSlot.standalone(); +- public final int[] costs = new int[3]; +- public final int[] enchantClue = new int[]{-1, -1, -1}; +- public final int[] levelClue = new int[]{-1, -1, -1}; ++ private final RandomSource random; ++ private final DataSlot enchantmentSeed; ++ public final int[] costs; ++ public final int[] enchantClue; ++ public final int[] levelClue; ++ // CraftBukkit start ++ private CraftInventoryView bukkitEntity = null; ++ private Player player; ++ // CraftBukkit end + + public EnchantmentMenu(int containerId, Inventory playerInventory) { + this(containerId, playerInventory, ContainerLevelAccess.NULL); +@@ -48,6 +61,25 @@ + + public EnchantmentMenu(int containerId, Inventory playerInventory, ContainerLevelAccess access) { + super(MenuType.ENCHANTMENT, containerId); ++ this.enchantSlots = new SimpleContainer(2) { ++ @Override ++ public void setChanged() { ++ super.setChanged(); ++ EnchantmentMenu.this.slotsChanged(this); ++ } ++ ++ // CraftBukkit start ++ @Override ++ public Location getLocation() { ++ return access.getLocation(); ++ } ++ // CraftBukkit end ++ }; ++ this.random = RandomSource.create(); ++ this.enchantmentSeed = DataSlot.standalone(); ++ this.costs = new int[3]; ++ this.enchantClue = new int[]{-1, -1, -1}; ++ this.levelClue = new int[]{-1, -1, -1}; + this.access = access; + this.addSlot(new Slot(this.enchantSlots, 0, 15, 47) { + @Override +@@ -87,6 +121,9 @@ + this.addDataSlot(DataSlot.shared(this.levelClue, 0)); + this.addDataSlot(DataSlot.shared(this.levelClue, 1)); + this.addDataSlot(DataSlot.shared(this.levelClue, 2)); ++ // CraftBukkit start ++ player = (Player) playerInventory.player.getBukkitEntity(); ++ // CraftBukkit end + } + + @Override +@@ -97,9 +131,16 @@ + this.access.execute((level, blockPos) -> { + int i1 = 0; + +- for (BlockPos blockPos1 : EnchantmentTableBlock.BOOKSHELF_OFFSETS) { +- if (EnchantmentTableBlock.isValidBookShelf(level, blockPos, blockPos1)) { +- i1++; ++ if (!itemstack.isEmpty()) { // CraftBukkit - relax condition ++ this.access.execute((world, blockposition) -> { ++ int i = 0; ++ Iterator iterator = EnchantmentTableBlock.BOOKSHELF_OFFSETS.iterator(); ++ ++ while (iterator.hasNext()) { ++ BlockPos blockposition1 = (BlockPos) iterator.next(); ++ ++ if (EnchantmentTableBlock.isValidBookShelf(world, blockposition, blockposition1)) { ++ ++i; + } + } + +@@ -125,6 +170,41 @@ + } + } + ++ // CraftBukkit start ++ CraftItemStack item = CraftItemStack.asCraftMirror(itemstack); ++ org.bukkit.enchantments.EnchantmentOffer[] offers = new EnchantmentOffer[3]; ++ for (j = 0; j < 3; ++j) { ++ org.bukkit.enchantments.Enchantment enchantment = (this.enchantClue[j] >= 0) ? org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.ENCHANTMENT.getKey(BuiltInRegistries.ENCHANTMENT.byId(this.enchantClue[j])))) : null; ++ offers[j] = (enchantment != null) ? new EnchantmentOffer(enchantment, this.levelClue[j], this.costs[j]) : null; ++ } ++ ++ PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(player, this.getBukkitView(), access.getLocation().getBlock(), item, offers, i); ++ event.setCancelled(!itemstack.isEnchantable()); ++ world.getCraftServer().getPluginManager().callEvent(event); ++ ++ if (event.isCancelled()) { ++ for (j = 0; j < 3; ++j) { ++ this.costs[j] = 0; ++ this.enchantClue[j] = -1; ++ this.levelClue[j] = -1; ++ } ++ return; ++ } ++ ++ for (j = 0; j < 3; j++) { ++ EnchantmentOffer offer = event.getOffers()[j]; ++ if (offer != null) { ++ this.costs[j] = offer.getCost(); ++ this.enchantClue[j] = BuiltInRegistries.ENCHANTMENT.getId(BuiltInRegistries.ENCHANTMENT.get(CraftNamespacedKey.toMinecraft(offer.getEnchantment().getKey()))); ++ this.levelClue[j] = offer.getEnchantmentLevel(); ++ } else { ++ this.costs[j] = 0; ++ this.enchantClue[j] = -1; ++ this.levelClue[j] = -1; ++ } ++ } ++ // CraftBukkit end ++ + this.broadcastChanges(); + }); + } else { +@@ -145,35 +227,67 @@ + int i = id + 1; + if ((item1.isEmpty() || item1.getCount() < i) && !player.getAbilities().instabuild) { + return false; +- } else if (this.costs[id] <= 0 +- || item.isEmpty() +- || (player.experienceLevel < i || player.experienceLevel < this.costs[id]) && !player.getAbilities().instabuild) { +- return false; +- } else { +- this.access.execute((level, blockPos) -> { +- ItemStack itemStack = item; +- List<EnchantmentInstance> enchantmentList = this.getEnchantmentList(item, id, this.costs[id]); +- if (!enchantmentList.isEmpty()) { +- player.onEnchantmentPerformed(item, i); +- boolean isBook = item.is(Items.BOOK); +- if (isBook) { +- itemStack = new ItemStack(Items.ENCHANTED_BOOK); +- CompoundTag tag = item.getTag(); +- if (tag != null) { +- itemStack.setTag(tag.copy()); ++ } else if (this.costs[id] > 0 && !itemstack.isEmpty() && (player.experienceLevel >= j && player.experienceLevel >= this.costs[id] || player.getAbilities().instabuild)) { ++ this.access.execute((world, blockposition) -> { ++ ItemStack itemstack2 = itemstack; ++ List<EnchantmentInstance> list = this.getEnchantmentList(itemstack, id, this.costs[id]); ++ ++ // CraftBukkit start ++ if (true || !list.isEmpty()) { ++ // entityhuman.onEnchantmentPerformed(itemstack, j); // Moved down ++ Map<org.bukkit.enchantments.Enchantment, Integer> enchants = new java.util.HashMap<org.bukkit.enchantments.Enchantment, Integer>(); ++ for (EnchantmentInstance instance : list) { ++ enchants.put(org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.ENCHANTMENT.getKey(instance.enchantment))), instance.level); ++ } ++ CraftItemStack item = CraftItemStack.asCraftMirror(itemstack2); ++ ++ org.bukkit.enchantments.Enchantment hintedEnchantment = org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(BuiltInRegistries.ENCHANTMENT.getKey(Enchantment.byId(enchantClue[id])))); ++ int hintedEnchantmentLevel = levelClue[id]; ++ EnchantItemEvent event = new EnchantItemEvent((Player) player.getBukkitEntity(), this.getBukkitView(), access.getLocation().getBlock(), item, this.costs[id], enchants, hintedEnchantment, hintedEnchantmentLevel, id); ++ world.getCraftServer().getPluginManager().callEvent(event); ++ ++ int level = event.getExpLevelCost(); ++ if (event.isCancelled() || (level > player.experienceLevel && !player.getAbilities().instabuild) || event.getEnchantsToAdd().isEmpty()) { ++ return; ++ } ++ // CraftBukkit end ++ boolean flag = itemstack.is(Items.BOOK); ++ ++ if (flag) { ++ itemstack2 = new ItemStack(Items.ENCHANTED_BOOK); ++ CompoundTag nbttagcompound = itemstack.getTag(); ++ ++ if (nbttagcompound != null) { ++ itemstack2.setTag(nbttagcompound.copy()); + } + + this.enchantSlots.setItem(0, itemStack); + } + +- for (EnchantmentInstance enchantmentInstance : enchantmentList) { +- if (isBook) { +- EnchantedBookItem.addEnchantment(itemStack, enchantmentInstance); +- } else { +- itemStack.enchant(enchantmentInstance.enchantment, enchantmentInstance.level); ++ // CraftBukkit start ++ for (Map.Entry<org.bukkit.enchantments.Enchantment, Integer> entry : event.getEnchantsToAdd().entrySet()) { ++ try { ++ if (flag) { ++ NamespacedKey enchantId = entry.getKey().getKey(); ++ Enchantment nms = BuiltInRegistries.ENCHANTMENT.get(CraftNamespacedKey.toMinecraft(enchantId)); ++ if (nms == null) { ++ continue; ++ } ++ ++ EnchantmentInstance weightedrandomenchant = new EnchantmentInstance(nms, entry.getValue()); ++ EnchantedBookItem.addEnchantment(itemstack2, weightedrandomenchant); ++ } else { ++ item.addUnsafeEnchantment(entry.getKey(), entry.getValue()); ++ } ++ } catch (IllegalArgumentException e) { ++ /* Just swallow invalid enchantments */ + } + } + ++ player.onEnchantmentPerformed(itemstack, j); ++ // CraftBukkit end ++ ++ // CraftBukkit - TODO: let plugins change this + if (!player.getAbilities().instabuild) { + item1.shrink(i); + if (item1.isEmpty()) { +@@ -226,7 +349,8 @@ + } + + @Override +- public boolean stillValid(Player player) { ++ public boolean stillValid(net.minecraft.world.entity.player.Player player) { ++ if (!this.checkReachable) return true; // CraftBukkit + return stillValid(this.access, player, Blocks.ENCHANTING_TABLE); + } + +@@ -274,4 +401,17 @@ + + return itemStack; + } ++ ++ // CraftBukkit start ++ @Override ++ public CraftInventoryView getBukkitView() { ++ if (bukkitEntity != null) { ++ return bukkitEntity; ++ } ++ ++ CraftInventoryEnchanting inventory = new CraftInventoryEnchanting(this.enchantSlots); ++ bukkitEntity = new CraftInventoryView(this.player, inventory, this); ++ return bukkitEntity; ++ } ++ // CraftBukkit end + } |