mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-30 04:02:50 +01:00
[Bleeding] Correctly enchant books. Fixes BUKKIT-5302
Books can now recieve more than one enchantment. As such, breaking out of the loop after only 1 enchantment is applied is not the correct behavior. This commit also reworks some of the logic surrounding the application of enchantments to the item. By checking if the event doesn't add any enchantments rather than if the original enchantments list is empty, the application code is only reached if enchantments are applied, rendering the "applied" boolean no longer necessary. The ItemStack's Item should only be set once, so it is now set outside of the loop based upon whether or not "flag" is true (with "flag" being whether or not the ItemStack's Item is a book).
This commit is contained in:
parent
068d8a807b
commit
66471a5326
1 changed files with 6 additions and 9 deletions
|
@ -167,11 +167,14 @@ public class ContainerEnchantTable extends Container {
|
||||||
this.world.getServer().getPluginManager().callEvent(event);
|
this.world.getServer().getPluginManager().callEvent(event);
|
||||||
|
|
||||||
int level = event.getExpLevelCost();
|
int level = event.getExpLevelCost();
|
||||||
if (event.isCancelled() || (level > entityhuman.expLevel && !entityhuman.abilities.canInstantlyBuild) || enchants.isEmpty()) {
|
if (event.isCancelled() || (level > entityhuman.expLevel && !entityhuman.abilities.canInstantlyBuild) || event.getEnchantsToAdd().isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean applied = !flag;
|
if (flag) {
|
||||||
|
itemstack.setItem(Items.ENCHANTED_BOOK);
|
||||||
|
}
|
||||||
|
|
||||||
for (Map.Entry<org.bukkit.enchantments.Enchantment, Integer> entry : event.getEnchantsToAdd().entrySet()) {
|
for (Map.Entry<org.bukkit.enchantments.Enchantment, Integer> entry : event.getEnchantsToAdd().entrySet()) {
|
||||||
try {
|
try {
|
||||||
if (flag) {
|
if (flag) {
|
||||||
|
@ -182,9 +185,6 @@ public class ContainerEnchantTable extends Container {
|
||||||
|
|
||||||
EnchantmentInstance enchantment = new EnchantmentInstance(enchantId, entry.getValue());
|
EnchantmentInstance enchantment = new EnchantmentInstance(enchantId, entry.getValue());
|
||||||
Items.ENCHANTED_BOOK.a(itemstack, enchantment);
|
Items.ENCHANTED_BOOK.a(itemstack, enchantment);
|
||||||
applied = true;
|
|
||||||
itemstack.setItem((Item) Items.ENCHANTED_BOOK);
|
|
||||||
break;
|
|
||||||
} else {
|
} else {
|
||||||
item.addEnchantment(entry.getKey(), entry.getValue());
|
item.addEnchantment(entry.getKey(), entry.getValue());
|
||||||
}
|
}
|
||||||
|
@ -193,10 +193,7 @@ public class ContainerEnchantTable extends Container {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only down level if we've applied the enchantments
|
|
||||||
if (applied) {
|
|
||||||
entityhuman.levelDown(-level);
|
entityhuman.levelDown(-level);
|
||||||
}
|
|
||||||
// CraftBukkit end
|
// CraftBukkit end
|
||||||
|
|
||||||
this.a(this.enchantSlots);
|
this.a(this.enchantSlots);
|
||||||
|
|
Loading…
Reference in a new issue