summaryrefslogtreecommitdiffstats
path: root/nms-patches/ContainerEnchantTable.patch
blob: 40ddb90845c7f87e2653aae8f75c715f168b9a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
--- a/net/minecraft/server/ContainerEnchantTable.java
+++ b/net/minecraft/server/ContainerEnchantTable.java
@@ -3,9 +3,22 @@
 import java.util.List;
 import java.util.Random;
 
+// CraftBukkit start
+import java.util.Map;
+import org.bukkit.Location;
+
+import org.bukkit.craftbukkit.inventory.CraftInventoryEnchanting;
+import org.bukkit.craftbukkit.inventory.CraftInventoryView;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.event.enchantment.EnchantItemEvent;
+import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
+import org.bukkit.entity.Player;
+// CraftBukkit end
+
 public class ContainerEnchantTable extends Container {
 
-    public IInventory enchantSlots = new InventorySubcontainer("Enchant", true, 2) {
+    // CraftBukkit - make type specific (changed from IInventory)
+    public InventorySubcontainer enchantSlots = new InventorySubcontainer("Enchant", true, 2) {
         public int getMaxStackSize() {
             return 64;
         }
@@ -14,6 +27,11 @@
             super.update();
             ContainerEnchantTable.this.a((IInventory) this);
         }
+
+        @Override
+        public Location getLocation() {
+            return new org.bukkit.Location(world.getWorld(), position.getX(), position.getY(), position.getZ());
+        }
     };
     private World world;
     private BlockPosition position;
@@ -22,6 +40,10 @@
     public int[] costs = new int[3];
     public int[] h = new int[] { -1, -1, -1};
     public int[] i = new int[] { -1, -1, -1};
+    // CraftBukkit start
+    private CraftInventoryView bukkitEntity = null;
+    private Player player;
+    // CraftBukkit end
 
     public ContainerEnchantTable(PlayerInventory playerinventory, World world, BlockPosition blockposition) {
         this.world = world;
@@ -54,6 +76,9 @@
             this.a(new Slot(playerinventory, i, 8 + i * 18, 142));
         }
 
+        // CraftBukkit start
+        player = (Player) playerinventory.player.getBukkitEntity();
+        // CraftBukkit end
     }
 
     protected void c(ICrafting icrafting) {
@@ -90,7 +115,7 @@
             ItemStack itemstack = iinventory.getItem(0);
             int i;
 
-            if (itemstack != null && itemstack.v()) {
+            if (itemstack != null) { // CraftBukkit - relax condition
                 if (!this.world.isClientSide) {
                     i = 0;
 
@@ -139,6 +164,20 @@
                         }
                     }
 
+                    // CraftBukkit start
+                    CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
+                    PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(player, this.getBukkitView(), this.world.getWorld().getBlockAt(position.getX(), position.getY(), position.getZ()), item, this.costs, i);
+                    event.setCancelled(!itemstack.v());
+                    this.world.getServer().getPluginManager().callEvent(event);
+
+                    if (event.isCancelled()) {
+                        for (i = 0; i < 3; ++i) {
+                            this.costs[i] = 0;
+                        }
+                        return;
+                    }
+                    // CraftBukkit end
+
                     for (j = 0; j < 3; ++j) {
                         if (this.costs[j] > 0) {
                             List list = this.a(itemstack, j, this.costs[j]);
@@ -175,24 +214,55 @@
         } else if (this.costs[i] > 0 && itemstack != null && (entityhuman.expLevel >= j && entityhuman.expLevel >= this.costs[i] || entityhuman.abilities.canInstantlyBuild)) {
             if (!this.world.isClientSide) {
                 List list = this.a(itemstack, i, this.costs[i]);
+                // CraftBukkit start - Provide an empty enchantment list
+                if (list == null) {
+                    list = new java.util.ArrayList<WeightedRandomEnchant>();
+                }
+                // CraftBukkit end
                 boolean flag = itemstack.getItem() == Items.BOOK;
 
                 if (list != null) {
-                    entityhuman.enchantDone(j);
+                    // CraftBukkit start
+                    Map<org.bukkit.enchantments.Enchantment, Integer> enchants = new java.util.HashMap<org.bukkit.enchantments.Enchantment, Integer>();
+                    for (Object obj : list) {
+                        WeightedRandomEnchant instance = (WeightedRandomEnchant) obj;
+                        enchants.put(org.bukkit.enchantments.Enchantment.getById(Enchantment.getId(instance.enchantment)), instance.level);
+                    }
+                    CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
+
+                    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);
+
+                    int level = event.getExpLevelCost();
+                    if (event.isCancelled() || (level > entityhuman.expLevel && !entityhuman.abilities.canInstantlyBuild) || event.getEnchantsToAdd().isEmpty()) {
+                        return false;
+                    }
                     if (flag) {
                         itemstack.setItem(Items.ENCHANTED_BOOK);
                     }
 
-                    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) {
+                                int enchantId = entry.getKey().getId();
+                                if (Enchantment.c(enchantId) == null) {
+                                    continue;
+                                }
 
-                        if (flag) {
-                            Items.ENCHANTED_BOOK.a(itemstack, weightedrandomenchant);
-                        } else {
-                            itemstack.addEnchantment(weightedrandomenchant.enchantment, weightedrandomenchant.level);
+                                WeightedRandomEnchant weightedrandomenchant = new WeightedRandomEnchant(Enchantment.c(enchantId), entry.getValue());
+                                Items.ENCHANTED_BOOK.a(itemstack, weightedrandomenchant);
+                            } else {
+                                item.addUnsafeEnchantment(entry.getKey(), entry.getValue());
+                            }
+                        } catch (IllegalArgumentException e) {
+                            /* Just swallow invalid enchantments */
                         }
                     }
 
+                    entityhuman.enchantDone(j);
+                    // CraftBukkit end
+
+                    // CraftBukkit - TODO: let plugins change this
                     if (!entityhuman.abilities.canInstantlyBuild) {
                         itemstack1.count -= j;
                         if (itemstack1.count <= 0) {
@@ -226,6 +296,11 @@
 
     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
+        if(this.world == null) {
+            this.world = entityhuman.getWorld();
+        }
+        // CraftBukkit end
         if (!this.world.isClientSide) {
             for (int i = 0; i < this.enchantSlots.getSize(); ++i) {
                 ItemStack itemstack = this.enchantSlots.splitWithoutUpdate(i);
@@ -239,6 +314,7 @@
     }
 
     public boolean a(EntityHuman entityhuman) {
+        if (!this.checkReachable) return true; // CraftBukkit
         return this.world.getType(this.position).getBlock() != Blocks.ENCHANTING_TABLE ? false : entityhuman.e((double) this.position.getX() + 0.5D, (double) this.position.getY() + 0.5D, (double) this.position.getZ() + 0.5D) <= 64.0D;
     }
 
@@ -291,4 +367,17 @@
 
         return itemstack;
     }
+
+    // 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;
+    }
+    // CraftBukkit end
 }