diff options
Diffstat (limited to 'patch-remap/mache-spigotflower/net/minecraft/world/inventory/EnchantmentMenu.java.patch')
-rw-r--r-- | patch-remap/mache-spigotflower/net/minecraft/world/inventory/EnchantmentMenu.java.patch | 395 |
1 files changed, 395 insertions, 0 deletions
diff --git a/patch-remap/mache-spigotflower/net/minecraft/world/inventory/EnchantmentMenu.java.patch b/patch-remap/mache-spigotflower/net/minecraft/world/inventory/EnchantmentMenu.java.patch new file mode 100644 index 0000000000..71b40a29a1 --- /dev/null +++ b/patch-remap/mache-spigotflower/net/minecraft/world/inventory/EnchantmentMenu.java.patch @@ -0,0 +1,395 @@ +--- a/net/minecraft/world/inventory/EnchantmentMenu.java ++++ b/net/minecraft/world/inventory/EnchantmentMenu.java +@@ -18,14 +18,27 @@ + import net.minecraft.world.Container; + import net.minecraft.world.SimpleContainer; + import net.minecraft.world.entity.player.Inventory; +-import net.minecraft.world.entity.player.Player; + import net.minecraft.world.item.EnchantedBookItem; + import net.minecraft.world.item.ItemStack; + import net.minecraft.world.item.Items; +-import net.minecraft.world.item.enchantment.EnchantmentHelper; +-import net.minecraft.world.item.enchantment.EnchantmentInstance; + 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 { + +@@ -37,43 +50,50 @@ + 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 i, Inventory inventory) { +- this(i, inventory, ContainerLevelAccess.NULL); ++ public EnchantmentMenu(int containerId, Inventory playerInventory) { ++ this(containerId, playerInventory, ContainerLevelAccess.NULL); + } + +- public EnchantmentMenu(int i, Inventory inventory, ContainerLevelAccess containerlevelaccess) { +- super(MenuType.ENCHANTMENT, i); ++ public EnchantmentMenu(int containerId, Inventory playerInventory, ContainerLevelAccess access) { ++ super(MenuType.ENCHANTMENT, containerId); + this.enchantSlots = new SimpleContainer(2) { + @Override +- @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 = containerlevelaccess; ++ this.access = access; + this.addSlot(new Slot(this.enchantSlots, 0, 15, 47) { + @Override +- @Override + public int getMaxStackSize() { + return 1; + } + }); + this.addSlot(new Slot(this.enchantSlots, 1, 35, 47) { + @Override +- @Override +- public boolean mayPlace(ItemStack itemstack) { +- return itemstack.is(Items.LAPIS_LAZULI); ++ public boolean mayPlace(ItemStack stack) { ++ return stack.is(Items.LAPIS_LAZULI); + } + + @Override +- @Override + public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() { + return Pair.of(InventoryMenu.BLOCK_ATLAS, EnchantmentMenu.EMPTY_SLOT_LAPIS_LAZULI); + } +@@ -83,41 +103,43 @@ + + for (j = 0; j < 3; ++j) { + for (int k = 0; k < 9; ++k) { +- this.addSlot(new Slot(inventory, k + j * 9 + 9, 8 + k * 18, 84 + j * 18)); ++ this.addSlot(new Slot(playerInventory, k + j * 9 + 9, 8 + k * 18, 84 + j * 18)); + } + } + + for (j = 0; j < 9; ++j) { +- this.addSlot(new Slot(inventory, j, 8 + j * 18, 142)); ++ this.addSlot(new Slot(playerInventory, j, 8 + j * 18, 142)); + } + + this.addDataSlot(DataSlot.shared(this.costs, 0)); + this.addDataSlot(DataSlot.shared(this.costs, 1)); + this.addDataSlot(DataSlot.shared(this.costs, 2)); +- this.addDataSlot(this.enchantmentSeed).set(inventory.player.getEnchantmentSeed()); ++ this.addDataSlot(this.enchantmentSeed).set(playerInventory.player.getEnchantmentSeed()); + this.addDataSlot(DataSlot.shared(this.enchantClue, 0)); + this.addDataSlot(DataSlot.shared(this.enchantClue, 1)); + this.addDataSlot(DataSlot.shared(this.enchantClue, 2)); + 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 +- @Override +- public void slotsChanged(Container container) { +- if (container == this.enchantSlots) { +- ItemStack itemstack = container.getItem(0); ++ public void slotsChanged(Container inventory) { ++ if (inventory == this.enchantSlots) { ++ ItemStack itemstack = inventory.getItem(0); + +- if (!itemstack.isEmpty() && itemstack.isEnchantable()) { +- this.access.execute((level, blockpos) -> { ++ if (!itemstack.isEmpty()) { // CraftBukkit - relax condition ++ this.access.execute((world, blockposition) -> { + int i = 0; + Iterator iterator = EnchantmentTableBlock.BOOKSHELF_OFFSETS.iterator(); + + while (iterator.hasNext()) { +- BlockPos blockpos1 = (BlockPos) iterator.next(); ++ BlockPos blockposition1 = (BlockPos) iterator.next(); + +- if (EnchantmentTableBlock.isValidBookShelf(level, blockpos, blockpos1)) { ++ if (EnchantmentTableBlock.isValidBookShelf(world, blockposition, blockposition1)) { + ++i; + } + } +@@ -140,14 +162,49 @@ + List<EnchantmentInstance> list = this.getEnchantmentList(itemstack, j, this.costs[j]); + + if (list != null && !list.isEmpty()) { +- EnchantmentInstance enchantmentinstance = (EnchantmentInstance) list.get(this.random.nextInt(list.size())); ++ EnchantmentInstance weightedrandomenchant = (EnchantmentInstance) list.get(this.random.nextInt(list.size())); + +- this.enchantClue[j] = BuiltInRegistries.ENCHANTMENT.getId(enchantmentinstance.enchantment); +- this.levelClue[j] = enchantmentinstance.level; ++ this.enchantClue[j] = BuiltInRegistries.ENCHANTMENT.getId(weightedrandomenchant.enchantment); ++ this.levelClue[j] = weightedrandomenchant.level; + } + } + } + ++ // 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 { +@@ -162,47 +219,75 @@ + } + + @Override +- @Override +- public boolean clickMenuButton(Player player, int i) { +- if (i >= 0 && i < this.costs.length) { ++ public boolean clickMenuButton(net.minecraft.world.entity.player.Player player, int id) { ++ if (id >= 0 && id < this.costs.length) { + ItemStack itemstack = this.enchantSlots.getItem(0); + ItemStack itemstack1 = this.enchantSlots.getItem(1); +- int j = i + 1; ++ int j = id + 1; + + if ((itemstack1.isEmpty() || itemstack1.getCount() < j) && !player.getAbilities().instabuild) { + return false; +- } else if (this.costs[i] > 0 && !itemstack.isEmpty() && (player.experienceLevel >= j && player.experienceLevel >= this.costs[i] || player.getAbilities().instabuild)) { +- this.access.execute((level, blockpos) -> { ++ } 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, i, this.costs[i]); ++ List<EnchantmentInstance> list = this.getEnchantmentList(itemstack, id, this.costs[id]); + +- if (!list.isEmpty()) { +- player.onEnchantmentPerformed(itemstack, j); ++ // 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 compoundtag = itemstack.getTag(); ++ CompoundTag nbttagcompound = itemstack.getTag(); + +- if (compoundtag != null) { +- itemstack2.setTag(compoundtag.copy()); ++ if (nbttagcompound != null) { ++ itemstack2.setTag(nbttagcompound.copy()); + } + + this.enchantSlots.setItem(0, itemstack2); + } + +- Iterator iterator = list.iterator(); ++ // 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; ++ } + +- while (iterator.hasNext()) { +- EnchantmentInstance enchantmentinstance = (EnchantmentInstance) iterator.next(); +- +- if (flag) { +- EnchantedBookItem.addEnchantment(itemstack2, enchantmentinstance); +- } else { +- itemstack2.enchant(enchantmentinstance.enchantment, enchantmentinstance.level); ++ 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) { + itemstack1.shrink(j); + if (itemstack1.isEmpty()) { +@@ -218,7 +303,7 @@ + this.enchantSlots.setChanged(); + this.enchantmentSeed.set(player.getEnchantmentSeed()); + this.slotsChanged(this.enchantSlots); +- level.playSound((Player) null, blockpos, SoundEvents.ENCHANTMENT_TABLE_USE, SoundSource.BLOCKS, 1.0F, level.random.nextFloat() * 0.1F + 0.9F); ++ world.playSound((net.minecraft.world.entity.player.Player) null, blockposition, SoundEvents.ENCHANTMENT_TABLE_USE, SoundSource.BLOCKS, 1.0F, world.random.nextFloat() * 0.1F + 0.9F); + } + + }); +@@ -227,18 +312,18 @@ + return false; + } + } else { +- Component component = player.getName(); ++ Component ichatbasecomponent = player.getName(); + +- Util.logAndPauseIfInIde(component + " pressed invalid button id: " + i); ++ Util.logAndPauseIfInIde(ichatbasecomponent + " pressed invalid button id: " + id); + return false; + } + } + +- private List<EnchantmentInstance> getEnchantmentList(ItemStack itemstack, int i, int j) { +- this.random.setSeed((long) (this.enchantmentSeed.get() + i)); +- List<EnchantmentInstance> list = EnchantmentHelper.selectEnchantment(this.random, itemstack, j, false); ++ private List<EnchantmentInstance> getEnchantmentList(ItemStack stack, int enchantSlot, int level) { ++ this.random.setSeed((long) (this.enchantmentSeed.get() + enchantSlot)); ++ List<EnchantmentInstance> list = EnchantmentHelper.selectEnchantment(this.random, stack, level, false); + +- if (itemstack.is(Items.BOOK) && list.size() > 1) { ++ if (stack.is(Items.BOOK) && list.size() > 1) { + list.remove(this.random.nextInt(list.size())); + } + +@@ -256,35 +341,33 @@ + } + + @Override +- @Override +- public void removed(Player player) { ++ public void removed(net.minecraft.world.entity.player.Player player) { + super.removed(player); +- this.access.execute((level, blockpos) -> { ++ this.access.execute((world, blockposition) -> { + this.clearContainer(player, this.enchantSlots); + }); + } + + @Override +- @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); + } + + @Override +- @Override +- public ItemStack quickMoveStack(Player player, int i) { ++ public ItemStack quickMoveStack(net.minecraft.world.entity.player.Player player, int index) { + ItemStack itemstack = ItemStack.EMPTY; +- Slot slot = (Slot) this.slots.get(i); ++ Slot slot = (Slot) this.slots.get(index); + + if (slot != null && slot.hasItem()) { + ItemStack itemstack1 = slot.getItem(); + + itemstack = itemstack1.copy(); +- if (i == 0) { ++ if (index == 0) { + if (!this.moveItemStackTo(itemstack1, 2, 38, true)) { + return ItemStack.EMPTY; + } +- } else if (i == 1) { ++ } else if (index == 1) { + if (!this.moveItemStackTo(itemstack1, 2, 38, true)) { + return ItemStack.EMPTY; + } +@@ -318,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 + } |