Skip to content

make bookshelf power configurable and refresh enchantment hint of menu after editing bookshelf power #12345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public class PrepareItemEnchantEvent extends InventoryEvent implements Cancellab
private final Block table;
private final ItemStack item;
private final EnchantmentOffer[] offers;
private final int bonus;

private int bonus;
private boolean cancelled;

@ApiStatus.Internal
Expand Down Expand Up @@ -111,6 +110,13 @@ public int getEnchantmentBonus() {
return this.bonus;
}

/**
* set enchantment bonus in effect - corresponds to number of bookshelves
*
* @param bonus enchantment bonus
*/
public void setEnchantmentBonus(final int bonus) { this.bonus = bonus; }

@Override
public boolean isCancelled() {
return this.cancelled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
this.access.execute((level, blockPos) -> {
IdMap<Holder<Enchantment>> holderIdMap = level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).asHolderIdMap();
int i1 = 0;
@@ -119,6 +_,42 @@
@@ -119,6 +_,64 @@
}
}

Expand All @@ -89,6 +89,28 @@
+ return;
+ }
+
+ // Paper Start - refreshing enchantment hint with configuring bookshelf power
+ // refreshing offers with event's bookshelf power if event's bookshelf power value is not equal to previous natural bookshelf power value.
+ if (event.getEnchantmentBonus() != i1) {
+ for (int i = 0; i < 3; i++) {
+ org.bukkit.enchantments.Enchantment enchantment = (this.enchantClue[i] >= 0) ? org.bukkit.craftbukkit.enchantments.CraftEnchantment.minecraftHolderToBukkit(holderIdMap.byId(this.enchantClue[i])) : null;
+ event.getOffers()[i] = (enchantment != null) ? new org.bukkit.enchantments.EnchantmentOffer(enchantment, this.levelClue[i], this.costs[i]) : null;
+
+ this.costs[i] = EnchantmentHelper.getEnchantmentCost(this.random, i, event.getEnchantmentBonus(), item);
+ this.enchantClue[i] = -1;
+ this.levelClue[i] = -1;
+ if (this.costs[i] < i + 1) continue;
+
+ List<EnchantmentInstance> enchantmentList = this.getEnchantmentList(level.registryAccess(), item, i, this.costs[i]);
+ if (!enchantmentList.isEmpty()) {
+ EnchantmentInstance enchantmentInstance = enchantmentList.get(this.random.nextInt(enchantmentList.size()));
+ this.enchantClue[i] = holderIdMap.getId(enchantmentInstance.enchantment);
+ this.levelClue[i] = enchantmentInstance.level;
+ }
+ }
+ }
+ // Paper end
+
+ for (int j = 0; j < 3; j++) {
+ org.bukkit.enchantments.EnchantmentOffer offer = event.getOffers()[j];
+ if (offer != null) {
Expand Down