2015-05-25 18:37:24 +08:00
|
|
|
--- a/net/minecraft/server/ContainerEnchantTable.java
|
|
|
|
+++ b/net/minecraft/server/ContainerEnchantTable.java
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -3,6 +3,21 @@
|
2016-11-17 09:41:03 +08:00
|
|
|
import java.util.List;
|
2014-11-26 05:32:16 +08:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
+// CraftBukkit start
|
2016-12-11 08:10:38 +08:00
|
|
|
+import java.util.Collections;
|
2014-11-26 05:32:16 +08:00
|
|
|
+import java.util.Map;
|
2016-03-01 05:32:46 +08:00
|
|
|
+import org.bukkit.Location;
|
2018-07-15 08:00:00 +08:00
|
|
|
+import org.bukkit.NamespacedKey;
|
2014-11-26 05:32:16 +08:00
|
|
|
+import org.bukkit.craftbukkit.inventory.CraftInventoryEnchanting;
|
|
|
|
+import org.bukkit.craftbukkit.inventory.CraftInventoryView;
|
|
|
|
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
|
2018-07-15 08:00:00 +08:00
|
|
|
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
|
2016-12-11 08:10:38 +08:00
|
|
|
+import org.bukkit.enchantments.EnchantmentOffer;
|
2014-11-26 05:32:16 +08:00
|
|
|
+import org.bukkit.event.enchantment.EnchantItemEvent;
|
|
|
|
+import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
|
|
|
|
+import org.bukkit.entity.Player;
|
|
|
|
+// CraftBukkit end
|
|
|
|
+
|
|
|
|
public class ContainerEnchantTable extends Container {
|
|
|
|
|
2018-07-15 08:00:00 +08:00
|
|
|
public IInventory enchantSlots = new InventorySubcontainer(new ChatComponentText("Enchant"), 2) {
|
|
|
|
@@ -14,6 +29,13 @@
|
2016-03-01 05:32:46 +08:00
|
|
|
super.update();
|
|
|
|
ContainerEnchantTable.this.a((IInventory) this);
|
|
|
|
}
|
|
|
|
+
|
2016-12-02 06:29:33 +08:00
|
|
|
+ // CraftBukkit start
|
2016-03-01 05:32:46 +08:00
|
|
|
+ @Override
|
|
|
|
+ public Location getLocation() {
|
|
|
|
+ return new org.bukkit.Location(world.getWorld(), position.getX(), position.getY(), position.getZ());
|
|
|
|
+ }
|
2016-12-02 06:29:33 +08:00
|
|
|
+ // CraftBukkit end
|
2016-03-01 05:32:46 +08:00
|
|
|
};
|
2016-06-09 09:43:49 +08:00
|
|
|
public World world;
|
|
|
|
private final BlockPosition position;
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -22,6 +44,10 @@
|
2014-11-26 05:32:16 +08:00
|
|
|
public int[] costs = new int[3];
|
|
|
|
public int[] h = new int[] { -1, -1, -1};
|
2016-03-01 05:32:46 +08:00
|
|
|
public int[] i = new int[] { -1, -1, -1};
|
2014-11-26 05:32:16 +08:00
|
|
|
+ // CraftBukkit start
|
|
|
|
+ private CraftInventoryView bukkitEntity = null;
|
|
|
|
+ private Player player;
|
|
|
|
+ // CraftBukkit end
|
|
|
|
|
|
|
|
public ContainerEnchantTable(PlayerInventory playerinventory, World world, BlockPosition blockposition) {
|
|
|
|
this.world = world;
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -54,6 +80,9 @@
|
2014-11-26 05:32:16 +08:00
|
|
|
this.a(new Slot(playerinventory, i, 8 + i * 18, 142));
|
|
|
|
}
|
2015-02-27 06:41:06 +08:00
|
|
|
|
2014-11-26 05:32:16 +08:00
|
|
|
+ // CraftBukkit start
|
|
|
|
+ player = (Player) playerinventory.player.getBukkitEntity();
|
|
|
|
+ // CraftBukkit end
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:32:46 +08:00
|
|
|
protected void c(ICrafting icrafting) {
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -90,7 +119,7 @@
|
2014-11-26 05:32:16 +08:00
|
|
|
ItemStack itemstack = iinventory.getItem(0);
|
|
|
|
int i;
|
|
|
|
|
2016-11-17 09:41:03 +08:00
|
|
|
- if (!itemstack.isEmpty() && itemstack.canEnchant()) {
|
|
|
|
+ if (!itemstack.isEmpty()) { // CraftBukkit - relax condition
|
2015-02-27 06:41:06 +08:00
|
|
|
if (!this.world.isClientSide) {
|
2014-11-26 05:32:16 +08:00
|
|
|
i = 0;
|
|
|
|
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -146,12 +175,47 @@
|
|
|
|
if (list != null && !list.isEmpty()) {
|
|
|
|
WeightedRandomEnchant weightedrandomenchant = (WeightedRandomEnchant) list.get(this.l.nextInt(list.size()));
|
|
|
|
|
2018-08-26 10:00:00 +08:00
|
|
|
- this.h[j] = IRegistry.ENCHANTMENT.a((Object) weightedrandomenchant.enchantment);
|
|
|
|
+ this.h[j] = IRegistry.ENCHANTMENT.a(weightedrandomenchant.enchantment); // CraftBukkit - decompile error
|
2018-07-15 08:00:00 +08:00
|
|
|
this.i[j] = weightedrandomenchant.level;
|
|
|
|
}
|
2014-11-26 05:32:16 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-27 06:41:06 +08:00
|
|
|
|
2014-11-26 05:32:16 +08:00
|
|
|
+ // CraftBukkit start
|
|
|
|
+ CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
|
2016-12-11 08:10:38 +08:00
|
|
|
+ org.bukkit.enchantments.EnchantmentOffer[] offers = new EnchantmentOffer[3];
|
2016-12-27 08:53:09 +08:00
|
|
|
+ for (j = 0; j < 3; ++j) {
|
2018-08-26 10:00:00 +08:00
|
|
|
+ org.bukkit.enchantments.Enchantment enchantment = (this.h[j] >= 0) ? org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(IRegistry.ENCHANTMENT.getKey(IRegistry.ENCHANTMENT.fromId(this.h[j])))) : null;
|
2016-12-27 08:53:09 +08:00
|
|
|
+ offers[j] = (enchantment != null) ? new EnchantmentOffer(enchantment, this.i[j], this.costs[j]) : null;
|
2016-12-11 08:10:38 +08:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(player, this.getBukkitView(), this.world.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()), item, offers, i);
|
2016-11-17 09:41:03 +08:00
|
|
|
+ event.setCancelled(!itemstack.canEnchant());
|
2014-11-26 05:32:16 +08:00
|
|
|
+ this.world.getServer().getPluginManager().callEvent(event);
|
|
|
|
+
|
|
|
|
+ if (event.isCancelled()) {
|
2016-12-27 08:53:09 +08:00
|
|
|
+ for (j = 0; j < 3; ++j) {
|
|
|
|
+ this.costs[j] = 0;
|
|
|
|
+ this.h[j] = -1;
|
|
|
|
+ this.i[j] = -1;
|
2014-11-26 05:32:16 +08:00
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
2016-12-11 08:10:38 +08:00
|
|
|
+
|
2016-12-27 08:53:09 +08:00
|
|
|
+ for (j = 0; j < 3; j++) {
|
|
|
|
+ EnchantmentOffer offer = event.getOffers()[j];
|
2016-12-11 08:10:38 +08:00
|
|
|
+ if (offer != null) {
|
2016-12-27 08:53:09 +08:00
|
|
|
+ this.costs[j] = offer.getCost();
|
2018-08-26 10:00:00 +08:00
|
|
|
+ this.h[j] = IRegistry.ENCHANTMENT.a(IRegistry.ENCHANTMENT.get(CraftNamespacedKey.toMinecraft(offer.getEnchantment().getKey())));
|
2016-12-27 08:53:09 +08:00
|
|
|
+ this.i[j] = offer.getEnchantmentLevel();
|
2016-12-11 08:10:38 +08:00
|
|
|
+ } else {
|
2016-12-27 08:53:09 +08:00
|
|
|
+ this.costs[j] = 0;
|
|
|
|
+ this.h[j] = -1;
|
|
|
|
+ this.i[j] = -1;
|
2016-12-11 08:10:38 +08:00
|
|
|
+ }
|
|
|
|
+ }
|
2014-11-26 05:32:16 +08:00
|
|
|
+ // CraftBukkit end
|
2015-02-27 06:41:06 +08:00
|
|
|
+
|
2016-12-11 08:10:38 +08:00
|
|
|
this.b();
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -176,25 +240,53 @@
|
2016-12-14 11:38:55 +08:00
|
|
|
if (!this.world.isClientSide) {
|
2016-12-13 05:44:50 +08:00
|
|
|
List list = this.a(itemstack, i, this.costs[i]);
|
|
|
|
|
2016-12-14 11:38:55 +08:00
|
|
|
- if (!list.isEmpty()) {
|
2017-05-14 10:00:00 +08:00
|
|
|
- entityhuman.enchantDone(itemstack, j);
|
2016-12-14 11:38:55 +08:00
|
|
|
+ // CraftBukkit start
|
|
|
|
+ if (true || !list.isEmpty()) {
|
2017-05-14 10:00:00 +08:00
|
|
|
+ // entityhuman.enchantDone(itemstack, j); // Moved down
|
2016-12-13 05:44:50 +08:00
|
|
|
boolean flag = itemstack.getItem() == Items.BOOK;
|
2014-11-26 05:32:16 +08:00
|
|
|
+ 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 10:00:00 +08:00
|
|
|
+ enchants.put(org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(IRegistry.ENCHANTMENT.getKey(instance.enchantment))), instance.level);
|
2014-11-26 05:32:16 +08:00
|
|
|
+ }
|
|
|
|
+ CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
|
2015-02-27 06:41:06 +08:00
|
|
|
+
|
2014-11-26 05:32:16 +08:00
|
|
|
+ EnchantItemEvent event = new EnchantItemEvent((Player) entityhuman.getBukkitEntity(), this.getBukkitView(), this.world.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()), item, this.costs[i], enchants, i);
|
|
|
|
+ this.world.getServer().getPluginManager().callEvent(event);
|
2017-05-14 10:00:00 +08:00
|
|
|
+
|
2014-11-26 05:32:16 +08:00
|
|
|
+ int level = event.getExpLevelCost();
|
|
|
|
+ if (event.isCancelled() || (level > entityhuman.expLevel && !entityhuman.abilities.canInstantlyBuild) || event.getEnchantsToAdd().isEmpty()) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
2017-05-14 10:00:00 +08:00
|
|
|
|
2014-11-26 05:32:16 +08:00
|
|
|
if (flag) {
|
2016-11-17 09:41:03 +08:00
|
|
|
itemstack = new ItemStack(Items.ENCHANTED_BOOK);
|
|
|
|
this.enchantSlots.setItem(0, itemstack);
|
2014-11-26 05:32:16 +08:00
|
|
|
}
|
2015-02-27 06:41:06 +08:00
|
|
|
|
|
|
|
- for (int k = 0; k < list.size(); ++k) {
|
|
|
|
- WeightedRandomEnchant weightedrandomenchant = (WeightedRandomEnchant) list.get(k);
|
2014-11-26 05:32:16 +08:00
|
|
|
+ for (Map.Entry<org.bukkit.enchantments.Enchantment, Integer> entry : event.getEnchantsToAdd().entrySet()) {
|
|
|
|
+ try {
|
|
|
|
+ if (flag) {
|
2018-07-15 08:00:00 +08:00
|
|
|
+ NamespacedKey enchantId = entry.getKey().getKey();
|
2018-08-26 10:00:00 +08:00
|
|
|
+ Enchantment nms = IRegistry.ENCHANTMENT.get(CraftNamespacedKey.toMinecraft(enchantId));
|
2018-07-15 08:00:00 +08:00
|
|
|
+ if (nms == null) {
|
2014-11-26 05:32:16 +08:00
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (flag) {
|
2017-05-19 19:00:13 +08:00
|
|
|
- ItemEnchantedBook.a(itemstack, weightedrandomenchant);
|
2014-11-26 05:32:16 +08:00
|
|
|
- } else {
|
|
|
|
- itemstack.addEnchantment(weightedrandomenchant.enchantment, weightedrandomenchant.level);
|
2018-07-15 08:00:00 +08:00
|
|
|
+ WeightedRandomEnchant weightedrandomenchant = new WeightedRandomEnchant(nms, entry.getValue());
|
2017-05-19 19:00:13 +08:00
|
|
|
+ ItemEnchantedBook.a(itemstack, weightedrandomenchant);
|
2014-11-26 05:32:16 +08:00
|
|
|
+ } else {
|
|
|
|
+ item.addUnsafeEnchantment(entry.getKey(), entry.getValue());
|
|
|
|
+ }
|
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
|
+ /* Just swallow invalid enchantments */
|
|
|
|
}
|
|
|
|
}
|
2015-02-27 06:41:06 +08:00
|
|
|
|
2017-05-14 10:00:00 +08:00
|
|
|
+ entityhuman.enchantDone(itemstack, j);
|
2014-11-26 05:32:16 +08:00
|
|
|
+ // CraftBukkit end
|
2015-02-27 06:41:06 +08:00
|
|
|
+
|
2014-11-26 05:32:16 +08:00
|
|
|
+ // CraftBukkit - TODO: let plugins change this
|
|
|
|
if (!entityhuman.abilities.canInstantlyBuild) {
|
2016-11-17 09:41:03 +08:00
|
|
|
itemstack1.subtract(j);
|
|
|
|
if (itemstack1.isEmpty()) {
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -233,12 +325,18 @@
|
2015-02-01 00:43:37 +08:00
|
|
|
|
|
|
|
public void b(EntityHuman entityhuman) {
|
|
|
|
super.b(entityhuman);
|
|
|
|
+ // CraftBukkit Start - If an enchantable was opened from a null location, set the world to the player's world, preventing a crash
|
2016-06-09 09:43:49 +08:00
|
|
|
+ if (this.world == null) {
|
2015-02-01 00:43:37 +08:00
|
|
|
+ this.world = entityhuman.getWorld();
|
|
|
|
+ }
|
|
|
|
+ // CraftBukkit end
|
2015-02-27 06:41:06 +08:00
|
|
|
if (!this.world.isClientSide) {
|
2017-05-14 10:00:00 +08:00
|
|
|
this.a(entityhuman, entityhuman.world, this.enchantSlots);
|
|
|
|
}
|
2014-11-26 05:32:16 +08:00
|
|
|
}
|
|
|
|
|
2017-09-18 18:00:00 +08:00
|
|
|
public boolean canUse(EntityHuman entityhuman) {
|
2014-11-26 05:32:16 +08:00
|
|
|
+ if (!this.checkReachable) return true; // CraftBukkit
|
2016-11-17 09:41:03 +08:00
|
|
|
return this.world.getType(this.position).getBlock() != Blocks.ENCHANTING_TABLE ? false : entityhuman.d((double) this.position.getX() + 0.5D, (double) this.position.getY() + 0.5D, (double) this.position.getZ() + 0.5D) <= 64.0D;
|
2014-11-26 05:32:16 +08:00
|
|
|
}
|
|
|
|
|
2018-07-15 08:00:00 +08:00
|
|
|
@@ -291,4 +389,17 @@
|
2014-11-26 05:32:16 +08:00
|
|
|
|
|
|
|
return itemstack;
|
2015-02-27 06:41:06 +08:00
|
|
|
}
|
2014-11-26 05:32:16 +08: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-27 06:41:06 +08:00
|
|
|
+ }
|
2014-11-26 05:32:16 +08:00
|
|
|
+ // CraftBukkit end
|
|
|
|
}
|