Skip to content

Commit 597f186

Browse files
I-am-DDang_I-am-DDang_
I-am-DDang_
authored and
I-am-DDang_
committed
bookshelf power configurability and refreshing enchantment hint accordingly
1 parent 7e8f5fc commit 597f186

File tree

1 file changed

+134
-45
lines changed

1 file changed

+134
-45
lines changed

Diff for: paper-server/patches/sources/net/minecraft/world/inventory/EnchantmentMenu.java.patch

+134-45
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
this.access = access;
4949
this.addSlot(new Slot(this.enchantSlots, 0, 15, 47) {
5050
@Override
51-
@@ -80,13 +_,14 @@
51+
@@ -80,55 +_,105 @@
5252
this.addDataSlot(DataSlot.shared(this.levelClue, 0));
5353
this.addDataSlot(DataSlot.shared(this.levelClue, 1));
5454
this.addDataSlot(DataSlot.shared(this.levelClue, 2));
@@ -57,56 +57,145 @@
5757

5858
@Override
5959
public void slotsChanged(Container inventory) {
60-
if (inventory == this.enchantSlots) {
61-
ItemStack item = inventory.getItem(0);
60+
- if (inventory == this.enchantSlots) {
61+
- ItemStack item = inventory.getItem(0);
6262
- if (!item.isEmpty() && item.isEnchantable()) {
63-
+ if (!item.isEmpty()) { // CraftBukkit - relax condition
64-
this.access.execute((level, blockPos) -> {
65-
IdMap<Holder<Enchantment>> holderIdMap = level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).asHolderIdMap();
66-
int i1 = 0;
67-
@@ -119,6 +_,42 @@
68-
}
69-
}
70-
71-
+ // CraftBukkit start
72-
+ org.bukkit.craftbukkit.inventory.CraftItemStack craftItemStack = org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(item);
73-
+ org.bukkit.enchantments.EnchantmentOffer[] offers = new org.bukkit.enchantments.EnchantmentOffer[3];
74-
+ for (int j = 0; j < 3; ++j) {
75-
+ org.bukkit.enchantments.Enchantment enchantment = (this.enchantClue[j] >= 0) ? org.bukkit.craftbukkit.enchantments.CraftEnchantment.minecraftHolderToBukkit(holderIdMap.byId(this.enchantClue[j])) : null;
76-
+ offers[j] = (enchantment != null) ? new org.bukkit.enchantments.EnchantmentOffer(enchantment, this.levelClue[j], this.costs[j]) : null;
77-
+ }
63+
- this.access.execute((level, blockPos) -> {
64+
- IdMap<Holder<Enchantment>> holderIdMap = level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).asHolderIdMap();
65+
- int i1 = 0;
66+
-
67+
- for (BlockPos blockPos1 : EnchantingTableBlock.BOOKSHELF_OFFSETS) {
68+
- if (EnchantingTableBlock.isValidBookShelf(level, blockPos, blockPos1)) {
69+
- i1++;
70+
- }
71+
- }
72+
-
73+
- this.random.setSeed(this.enchantmentSeed.get());
74+
-
75+
- for (int i2 = 0; i2 < 3; i2++) {
76+
- this.costs[i2] = EnchantmentHelper.getEnchantmentCost(this.random, i2, i1, item);
77+
- this.enchantClue[i2] = -1;
78+
- this.levelClue[i2] = -1;
79+
- if (this.costs[i2] < i2 + 1) {
80+
- this.costs[i2] = 0;
81+
- }
82+
- }
83+
-
84+
- for (int i2x = 0; i2x < 3; i2x++) {
85+
- if (this.costs[i2x] > 0) {
86+
- List<EnchantmentInstance> enchantmentList = this.getEnchantmentList(level.registryAccess(), item, i2x, this.costs[i2x]);
87+
- if (enchantmentList != null && !enchantmentList.isEmpty()) {
88+
- EnchantmentInstance enchantmentInstance = enchantmentList.get(this.random.nextInt(enchantmentList.size()));
89+
- this.enchantClue[i2x] = holderIdMap.getId(enchantmentInstance.enchantment);
90+
- this.levelClue[i2x] = enchantmentInstance.level;
91+
- }
92+
- }
93+
- }
94+
-
95+
- this.broadcastChanges();
96+
- });
97+
- } else {
98+
+ if (inventory != this.enchantSlots) return;
7899
+
79-
+ org.bukkit.event.enchantment.PrepareItemEnchantEvent event = new org.bukkit.event.enchantment.PrepareItemEnchantEvent(this.player, this.getBukkitView(), this.access.getLocation().getBlock(), craftItemStack, offers, i1);
80-
+ event.setCancelled(!item.isEnchantable());
81-
+ level.getCraftServer().getPluginManager().callEvent(event);
100+
+ ItemStack item = inventory.getItem(0);
101+
+ if (item.isEmpty()) {
102+
+ for (int i = 0; i < 3; i++) {
103+
+ this.costs[i] = 0;
104+
+ this.enchantClue[i] = -1;
105+
+ this.levelClue[i] = -1;
106+
+ }
107+
+ return;
108+
+ }
82109
+
83-
+ if (event.isCancelled()) {
84-
+ for (int j = 0; j < 3; ++j) {
85-
+ this.costs[j] = 0;
86-
+ this.enchantClue[j] = -1;
87-
+ this.levelClue[j] = -1;
88-
+ }
89-
+ return;
90-
+ }
110+
+ // CraftBukkit - relax condition
111+
+ this.access.execute((level, blockPos) -> {
112+
+ IdMap<Holder<Enchantment>> holderIdMap = level.registryAccess().lookupOrThrow(Registries.ENCHANTMENT).asHolderIdMap();
91113
+
92-
+ for (int j = 0; j < 3; j++) {
93-
+ org.bukkit.enchantments.EnchantmentOffer offer = event.getOffers()[j];
94-
+ if (offer != null) {
95-
+ this.costs[j] = offer.getCost();
96-
+ this.enchantClue[j] = holderIdMap.getId(org.bukkit.craftbukkit.enchantments.CraftEnchantment
97-
+ .bukkitToMinecraftHolder(offer.getEnchantment()));
98-
+ this.levelClue[j] = offer.getEnchantmentLevel();
99-
+ } else {
100-
+ if (enchantClue[j] != -1) this.costs[j] = 0;
101-
+ this.enchantClue[j] = -1;
102-
+ this.levelClue[j] = -1;
103-
+ }
114+
+ int i1 = 0;
115+
+ for (BlockPos blockPos1 : EnchantingTableBlock.BOOKSHELF_OFFSETS) {
116+
+ if (EnchantingTableBlock.isValidBookShelf(level, blockPos, blockPos1)) i1++;
117+
+ }
118+
+
119+
+ this.random.setSeed(this.enchantmentSeed.get());
120+
+
121+
+ // CraftBukkit start
122+
+ org.bukkit.craftbukkit.inventory.CraftItemStack craftItemStack = org.bukkit.craftbukkit.inventory.CraftItemStack.asCraftMirror(item);
123+
+ org.bukkit.enchantments.EnchantmentOffer[] offers = new org.bukkit.enchantments.EnchantmentOffer[3];
124+
+
125+
+ // Paper Start - refreshing enchantment hint with configuring bookshelf power
126+
+ // prepare offers with natural bookshelf power before the event fire
127+
+ for (int i = 0; i < 3; i++) {
128+
+ org.bukkit.enchantments.Enchantment enchantment = (this.enchantClue[i] >= 0) ? org.bukkit.craftbukkit.enchantments.CraftEnchantment.minecraftHolderToBukkit(holderIdMap.byId(this.enchantClue[i])) : null;
129+
+ offers[i] = (enchantment != null) ? new org.bukkit.enchantments.EnchantmentOffer(enchantment, this.levelClue[i], this.costs[i]) : null;
130+
+
131+
+ this.costs[i] = EnchantmentHelper.getEnchantmentCost(this.random, i, i1, item);
132+
+ this.enchantClue[i] = -1;
133+
+ this.levelClue[i] = -1;
134+
+ if (this.costs[i] < i + 1) continue;
135+
+
136+
+ List<EnchantmentInstance> enchantmentList = this.getEnchantmentList(level.registryAccess(), item, i, this.costs[i]);
137+
+ if (!enchantmentList.isEmpty()) {
138+
+ EnchantmentInstance enchantmentInstance = enchantmentList.get(this.random.nextInt(enchantmentList.size()));
139+
+ this.enchantClue[i] = holderIdMap.getId(enchantmentInstance.enchantment);
140+
+ this.levelClue[i] = enchantmentInstance.level;
141+
+ }
142+
+ }
143+
+
144+
+ org.bukkit.event.enchantment.PrepareItemEnchantEvent event = new org.bukkit.event.enchantment.PrepareItemEnchantEvent(this.player, this.getBukkitView(), this.access.getLocation().getBlock(), craftItemStack, offers, i1);
145+
+
146+
+ // refreshing offers with event's bookshelf power if event's bookshelf power value is not equal to previous natural bookshelf power value.
147+
+ if (event.getEnchantmentBonus() != i1) {
148+
for (int i = 0; i < 3; i++) {
149+
- this.costs[i] = 0;
150+
+ org.bukkit.enchantments.Enchantment enchantment = (this.enchantClue[i] >= 0) ? org.bukkit.craftbukkit.enchantments.CraftEnchantment.minecraftHolderToBukkit(holderIdMap.byId(this.enchantClue[i])) : null;
151+
+ event.getOffers()[i] = (enchantment != null) ? new org.bukkit.enchantments.EnchantmentOffer(enchantment, this.levelClue[i], this.costs[i]) : null;
152+
+
153+
+ this.costs[i] = EnchantmentHelper.getEnchantmentCost(this.random, i, event.getEnchantmentBonus(), item);
154+
this.enchantClue[i] = -1;
155+
this.levelClue[i] = -1;
156+
- }
157+
- }
158+
- }
159+
+ if (this.costs[i] < i + 1) continue;
160+
+
161+
+ List<EnchantmentInstance> enchantmentList = this.getEnchantmentList(level.registryAccess(), item, i, this.costs[i]);
162+
+ if (!enchantmentList.isEmpty()) {
163+
+ EnchantmentInstance enchantmentInstance = enchantmentList.get(this.random.nextInt(enchantmentList.size()));
164+
+ this.enchantClue[i] = holderIdMap.getId(enchantmentInstance.enchantment);
165+
+ this.levelClue[i] = enchantmentInstance.level;
104166
+ }
105-
+ // CraftBukkit end
167+
+ }
168+
+ }
169+
+ // Paper end
106170
+
107-
this.broadcastChanges();
108-
});
109-
} else {
171+
+ event.setCancelled(!item.isEnchantable());
172+
+ level.getCraftServer().getPluginManager().callEvent(event);
173+
+ if (event.isCancelled()) {
174+
+ for (int j = 0; j < 3; ++j) {
175+
+ this.costs[j] = 0;
176+
+ this.enchantClue[j] = -1;
177+
+ this.levelClue[j] = -1;
178+
+ }
179+
+ return;
180+
+ }
181+
+ for (int j = 0; j < 3; j++) {
182+
+ org.bukkit.enchantments.EnchantmentOffer offer = event.getOffers()[j];
183+
+ if (offer != null) {
184+
+ this.costs[j] = offer.getCost();
185+
+ this.enchantClue[j] = holderIdMap.getId(org.bukkit.craftbukkit.enchantments.CraftEnchantment.bukkitToMinecraftHolder(offer.getEnchantment()));
186+
+ this.levelClue[j] = offer.getEnchantmentLevel();
187+
+ } else {
188+
+ if (enchantClue[j] != -1) this.costs[j] = 0;
189+
+ this.enchantClue[j] = -1;
190+
+ this.levelClue[j] = -1;
191+
+ }
192+
+ }
193+
+ // CraftBukkit end
194+
+ this.broadcastChanges();
195+
+ });
196+
}
197+
198+
@Override
110199
@@ -145,19 +_,53 @@
111200
return false;
112201
} else {

0 commit comments

Comments
 (0)