3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-16 04:50:05 +01:00
Paper/nms-patches/ContainerEnchantTable.patch

206 Zeilen
9.5 KiB
Diff

2015-05-25 12:37:24 +02:00
--- a/net/minecraft/server/ContainerEnchantTable.java
+++ b/net/minecraft/server/ContainerEnchantTable.java
2018-07-15 02:00:00 +02:00
@@ -3,6 +3,21 @@
2016-11-17 02:41:03 +01:00
import java.util.List;
import java.util.Random;
+// CraftBukkit start
+import java.util.Collections;
+import java.util.Map;
2016-02-29 22:32:46 +01:00
+import org.bukkit.Location;
2018-07-15 02:00:00 +02:00
+import org.bukkit.NamespacedKey;
+import org.bukkit.craftbukkit.inventory.CraftInventoryEnchanting;
+import org.bukkit.craftbukkit.inventory.CraftInventoryView;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
2018-07-15 02:00:00 +02:00
+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 ContainerEnchantTable extends Container {
2019-04-23 04:00:00 +02:00
private final IInventory enchantSlots;
@@ -12,6 +27,10 @@
public final int[] costs;
public final int[] enchantments;
public final int[] levels;
+ // CraftBukkit start
+ private CraftInventoryView bukkitEntity = null;
+ private Player player;
+ // CraftBukkit end
2019-04-23 04:00:00 +02:00
public ContainerEnchantTable(int i, PlayerInventory playerinventory) {
this(i, playerinventory, ContainerAccess.a);
@@ -25,6 +44,13 @@
super.update();
ContainerEnchantTable.this.a((IInventory) this);
}
+
+ // CraftBukkit start
+ @Override
+ public Location getLocation() {
+ return containeraccess.getLocation();
+ }
+ // CraftBukkit end
};
this.h = new Random();
this.i = ContainerProperty.a();
@@ -72,6 +98,9 @@
this.a(ContainerProperty.a(this.levels, 0));
this.a(ContainerProperty.a(this.levels, 1));
this.a(ContainerProperty.a(this.levels, 2));
+ // CraftBukkit start
+ player = (Player) playerinventory.player.getBukkitEntity();
+ // CraftBukkit end
}
2019-04-23 04:00:00 +02:00
@Override
@@ -79,7 +108,7 @@
if (iinventory == this.enchantSlots) {
ItemStack itemstack = iinventory.getItem(0);
2016-11-17 02:41:03 +01:00
- if (!itemstack.isEmpty() && itemstack.canEnchant()) {
+ if (!itemstack.isEmpty()) { // CraftBukkit - relax condition
2019-04-23 04:00:00 +02:00
this.containerAccess.a((world, blockposition) -> {
int i = 0;
2019-04-23 04:00:00 +02:00
@@ -135,12 +164,47 @@
2018-07-15 02:00:00 +02:00
if (list != null && !list.isEmpty()) {
2019-04-23 04:00:00 +02:00
WeightedRandomEnchant weightedrandomenchant = (WeightedRandomEnchant) list.get(this.h.nextInt(list.size()));
2018-07-15 02:00:00 +02:00
2019-04-23 04:00:00 +02:00
- this.enchantments[j] = IRegistry.ENCHANTMENT.a((Object) weightedrandomenchant.enchantment);
+ this.enchantments[j] = IRegistry.ENCHANTMENT.a(weightedrandomenchant.enchantment); // CraftBukkit - decompile error
this.levels[j] = weightedrandomenchant.level;
2018-07-15 02:00:00 +02:00
}
}
}
2015-02-26 23:41:06 +01:00
+ // CraftBukkit start
+ CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
+ org.bukkit.enchantments.EnchantmentOffer[] offers = new EnchantmentOffer[3];
+ for (j = 0; j < 3; ++j) {
2019-04-23 04:00:00 +02:00
+ org.bukkit.enchantments.Enchantment enchantment = (this.enchantments[j] >= 0) ? org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(IRegistry.ENCHANTMENT.getKey(IRegistry.ENCHANTMENT.fromId(this.enchantments[j])))) : null;
+ offers[j] = (enchantment != null) ? new EnchantmentOffer(enchantment, this.levels[j], this.costs[j]) : null;
+ }
+
2019-04-23 04:00:00 +02:00
+ PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(player, this.getBukkitView(), containerAccess.getLocation().getBlock(), item, offers, i);
2016-11-17 02:41:03 +01:00
+ event.setCancelled(!itemstack.canEnchant());
2019-04-23 04:00:00 +02:00
+ world.getServer().getPluginManager().callEvent(event);
+
+ if (event.isCancelled()) {
+ for (j = 0; j < 3; ++j) {
+ this.costs[j] = 0;
2019-04-23 04:00:00 +02:00
+ this.enchantments[j] = -1;
+ this.levels[j] = -1;
+ }
+ return;
+ }
+
+ for (j = 0; j < 3; j++) {
+ EnchantmentOffer offer = event.getOffers()[j];
+ if (offer != null) {
+ this.costs[j] = offer.getCost();
2019-04-23 04:00:00 +02:00
+ this.enchantments[j] = IRegistry.ENCHANTMENT.a(IRegistry.ENCHANTMENT.get(CraftNamespacedKey.toMinecraft(offer.getEnchantment().getKey())));
+ this.levels[j] = offer.getEnchantmentLevel();
+ } else {
+ this.costs[j] = 0;
2019-04-23 04:00:00 +02:00
+ this.enchantments[j] = -1;
+ this.levels[j] = -1;
+ }
+ }
+ // CraftBukkit end
2015-02-26 23:41:06 +01:00
+
2019-04-23 04:00:00 +02:00
this.c();
});
} else {
2019-04-23 04:00:00 +02:00
@@ -167,25 +231,53 @@
ItemStack itemstack2 = itemstack;
2018-12-25 22:00:00 +01:00
List<WeightedRandomEnchant> list = this.a(itemstack, i, this.costs[i]);
- if (!list.isEmpty()) {
2017-05-14 04:00:00 +02:00
- entityhuman.enchantDone(itemstack, j);
+ // CraftBukkit start
+ if (true || !list.isEmpty()) {
2017-05-14 04:00:00 +02:00
+ // entityhuman.enchantDone(itemstack, j); // Moved down
boolean flag = itemstack.getItem() == Items.BOOK;
+ Map<org.bukkit.enchantments.Enchantment, Integer> enchants = new java.util.HashMap<org.bukkit.enchantments.Enchantment, Integer>();
+ for (Object obj : list) {
+ WeightedRandomEnchant instance = (WeightedRandomEnchant) obj;
2018-08-26 04:00:00 +02:00
+ enchants.put(org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(IRegistry.ENCHANTMENT.getKey(instance.enchantment))), instance.level);
+ }
2019-04-23 04:00:00 +02:00
+ CraftItemStack item = CraftItemStack.asCraftMirror(itemstack2);
2015-02-26 23:41:06 +01:00
+
2019-04-23 04:00:00 +02:00
+ EnchantItemEvent event = new EnchantItemEvent((Player) entityhuman.getBukkitEntity(), this.getBukkitView(), containerAccess.getLocation().getBlock(), item, this.costs[i], enchants, i);
+ world.getServer().getPluginManager().callEvent(event);
2017-05-14 04:00:00 +02:00
+
+ int level = event.getExpLevelCost();
+ if (event.isCancelled() || (level > entityhuman.expLevel && !entityhuman.abilities.canInstantlyBuild) || event.getEnchantsToAdd().isEmpty()) {
2019-04-23 04:00:00 +02:00
+ return;
+ }
2017-05-14 04:00:00 +02:00
if (flag) {
2019-04-23 04:00:00 +02:00
itemstack2 = new ItemStack(Items.ENCHANTED_BOOK);
this.enchantSlots.setItem(0, itemstack2);
}
2015-02-26 23:41:06 +01:00
- for (int k = 0; k < list.size(); ++k) {
- WeightedRandomEnchant weightedrandomenchant = (WeightedRandomEnchant) list.get(k);
+ for (Map.Entry<org.bukkit.enchantments.Enchantment, Integer> entry : event.getEnchantsToAdd().entrySet()) {
+ try {
+ if (flag) {
2018-07-15 02:00:00 +02:00
+ NamespacedKey enchantId = entry.getKey().getKey();
2018-08-26 04:00:00 +02:00
+ Enchantment nms = IRegistry.ENCHANTMENT.get(CraftNamespacedKey.toMinecraft(enchantId));
2018-07-15 02:00:00 +02:00
+ if (nms == null) {
+ continue;
+ }
- if (flag) {
2019-04-23 04:00:00 +02:00
- ItemEnchantedBook.a(itemstack2, weightedrandomenchant);
- } else {
2019-04-23 04:00:00 +02:00
- itemstack2.addEnchantment(weightedrandomenchant.enchantment, weightedrandomenchant.level);
2018-07-15 02:00:00 +02:00
+ WeightedRandomEnchant weightedrandomenchant = new WeightedRandomEnchant(nms, entry.getValue());
2019-04-23 04:00:00 +02:00
+ ItemEnchantedBook.a(itemstack2, weightedrandomenchant);
+ } else {
+ item.addUnsafeEnchantment(entry.getKey(), entry.getValue());
+ }
+ } catch (IllegalArgumentException e) {
+ /* Just swallow invalid enchantments */
}
}
2015-02-26 23:41:06 +01:00
2017-05-14 04:00:00 +02:00
+ entityhuman.enchantDone(itemstack, j);
+ // CraftBukkit end
2015-02-26 23:41:06 +01:00
+
+ // CraftBukkit - TODO: let plugins change this
if (!entityhuman.abilities.canInstantlyBuild) {
2016-11-17 02:41:03 +01:00
itemstack1.subtract(j);
if (itemstack1.isEmpty()) {
2019-04-23 04:00:00 +02:00
@@ -232,6 +324,7 @@
2019-04-23 04:00:00 +02:00
@Override
2017-09-18 12:00:00 +02:00
public boolean canUse(EntityHuman entityhuman) {
+ if (!this.checkReachable) return true; // CraftBukkit
2019-04-23 04:00:00 +02:00
return a(this.containerAccess, entityhuman, Blocks.ENCHANTING_TABLE);
}
2019-04-23 04:00:00 +02:00
@@ -285,4 +378,17 @@
return itemstack;
2015-02-26 23:41:06 +01:00
}
+
+ // 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;
2015-02-26 23:41:06 +01:00
+ }
+ // CraftBukkit end
}