2012-01-21 08:29:34 +01:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
// CraftBukkit start
|
|
|
|
import java.util.Map;
|
|
|
|
|
2012-02-29 19:56:35 +01:00
|
|
|
import org.bukkit.craftbukkit.inventory.CraftInventoryEnchanting;
|
|
|
|
import org.bukkit.craftbukkit.inventory.CraftInventoryView;
|
2012-01-21 08:29:34 +01:00
|
|
|
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 {
|
|
|
|
|
2012-03-10 21:39:29 +01:00
|
|
|
// CraftBukkit - make type specific (changed from IInventory)
|
2013-03-13 23:33:27 +01:00
|
|
|
public ContainerEnchantTableInventory enchantSlots = new ContainerEnchantTableInventory(this, "Enchant", true, 1);
|
2012-02-29 22:31:04 +01:00
|
|
|
private World world;
|
|
|
|
private int x;
|
|
|
|
private int y;
|
|
|
|
private int z;
|
2012-01-21 08:29:34 +01:00
|
|
|
private Random l = new Random();
|
2012-07-29 09:33:13 +02:00
|
|
|
public long f;
|
2012-02-29 22:31:04 +01:00
|
|
|
public int[] costs = new int[3];
|
2012-02-29 19:56:35 +01:00
|
|
|
// CraftBukkit start
|
|
|
|
private CraftInventoryView bukkitEntity = null;
|
|
|
|
private Player player;
|
|
|
|
// CraftBukkit end
|
2012-01-21 08:29:34 +01:00
|
|
|
|
|
|
|
public ContainerEnchantTable(PlayerInventory playerinventory, World world, int i, int j, int k) {
|
2012-02-29 22:31:04 +01:00
|
|
|
this.world = world;
|
|
|
|
this.x = i;
|
|
|
|
this.y = j;
|
|
|
|
this.z = k;
|
|
|
|
this.a((Slot) (new SlotEnchant(this, this.enchantSlots, 0, 25, 47)));
|
2012-01-21 08:29:34 +01:00
|
|
|
|
|
|
|
int l;
|
|
|
|
|
|
|
|
for (l = 0; l < 3; ++l) {
|
|
|
|
for (int i1 = 0; i1 < 9; ++i1) {
|
|
|
|
this.a(new Slot(playerinventory, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (l = 0; l < 9; ++l) {
|
|
|
|
this.a(new Slot(playerinventory, l, 8 + l * 18, 142));
|
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2012-03-14 03:50:09 +01:00
|
|
|
// CraftBukkit start
|
2012-07-29 09:33:13 +02:00
|
|
|
player = (Player) playerinventory.player.getBukkitEntity();
|
2012-03-14 03:50:09 +01:00
|
|
|
enchantSlots.player = player;
|
|
|
|
// CraftBukkit end
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
public void addSlotListener(ICrafting icrafting) {
|
|
|
|
super.addSlotListener(icrafting);
|
|
|
|
icrafting.setContainerData(this, 0, this.costs[0]);
|
|
|
|
icrafting.setContainerData(this, 1, this.costs[1]);
|
|
|
|
icrafting.setContainerData(this, 2, this.costs[2]);
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
public void b() {
|
|
|
|
super.b();
|
2012-01-21 08:29:34 +01:00
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
for (int i = 0; i < this.listeners.size(); ++i) {
|
|
|
|
ICrafting icrafting = (ICrafting) this.listeners.get(i);
|
2012-01-21 08:29:34 +01:00
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
icrafting.setContainerData(this, 0, this.costs[0]);
|
|
|
|
icrafting.setContainerData(this, 1, this.costs[1]);
|
|
|
|
icrafting.setContainerData(this, 2, this.costs[2]);
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void a(IInventory iinventory) {
|
2012-02-29 22:31:04 +01:00
|
|
|
if (iinventory == this.enchantSlots) {
|
2012-01-21 08:29:34 +01:00
|
|
|
ItemStack itemstack = iinventory.getItem(0);
|
|
|
|
int i;
|
|
|
|
|
2013-03-13 23:33:27 +01:00
|
|
|
if (itemstack != null && itemstack.w()) {
|
2012-07-29 09:33:13 +02:00
|
|
|
this.f = this.l.nextLong();
|
2012-02-29 22:31:04 +01:00
|
|
|
if (!this.world.isStatic) {
|
2012-01-21 08:29:34 +01:00
|
|
|
i = 0;
|
|
|
|
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = -1; j <= 1; ++j) {
|
|
|
|
for (int k = -1; k <= 1; ++k) {
|
2012-02-29 22:31:04 +01:00
|
|
|
if ((j != 0 || k != 0) && this.world.isEmpty(this.x + k, this.y, this.z + j) && this.world.isEmpty(this.x + k, this.y + 1, this.z + j)) {
|
|
|
|
if (this.world.getTypeId(this.x + k * 2, this.y, this.z + j * 2) == Block.BOOKSHELF.id) {
|
2012-01-21 08:29:34 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
if (this.world.getTypeId(this.x + k * 2, this.y + 1, this.z + j * 2) == Block.BOOKSHELF.id) {
|
2012-01-21 08:29:34 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k != 0 && j != 0) {
|
2012-02-29 22:31:04 +01:00
|
|
|
if (this.world.getTypeId(this.x + k * 2, this.y, this.z + j) == Block.BOOKSHELF.id) {
|
2012-01-21 08:29:34 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
if (this.world.getTypeId(this.x + k * 2, this.y + 1, this.z + j) == Block.BOOKSHELF.id) {
|
2012-01-21 08:29:34 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
if (this.world.getTypeId(this.x + k, this.y, this.z + j * 2) == Block.BOOKSHELF.id) {
|
2012-01-21 08:29:34 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
if (this.world.getTypeId(this.x + k, this.y + 1, this.z + j * 2) == Block.BOOKSHELF.id) {
|
2012-01-21 08:29:34 +01:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < 3; ++j) {
|
2012-02-29 22:31:04 +01:00
|
|
|
this.costs[j] = EnchantmentManager.a(this.l, j, i, itemstack);
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CraftBukkit start
|
2012-12-17 08:31:41 +01:00
|
|
|
CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
|
2012-02-29 22:31:04 +01:00
|
|
|
PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(player, this.getBukkitView(), this.world.getWorld().getBlockAt(this.x, this.y, this.z), item, this.costs, i);
|
|
|
|
this.world.getServer().getPluginManager().callEvent(event);
|
|
|
|
|
2012-01-21 08:29:34 +01:00
|
|
|
if (event.isCancelled()) {
|
|
|
|
for (i = 0; i < 3; ++i) {
|
2012-02-29 22:31:04 +01:00
|
|
|
this.costs[i] = 0;
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2012-07-29 09:33:13 +02:00
|
|
|
|
|
|
|
this.b();
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < 3; ++i) {
|
2012-02-29 22:31:04 +01:00
|
|
|
this.costs[i] = 0;
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean a(EntityHuman entityhuman, int i) {
|
2012-02-29 22:31:04 +01:00
|
|
|
ItemStack itemstack = this.enchantSlots.getItem(0);
|
2012-01-21 08:29:34 +01:00
|
|
|
|
2012-03-01 11:49:23 +01:00
|
|
|
if (this.costs[i] > 0 && itemstack != null && (entityhuman.expLevel >= this.costs[i] || entityhuman.abilities.canInstantlyBuild)) {
|
2012-02-29 22:31:04 +01:00
|
|
|
if (!this.world.isStatic) {
|
2012-03-01 11:49:23 +01:00
|
|
|
List list = EnchantmentManager.b(this.l, itemstack, this.costs[i]);
|
2012-12-20 05:03:52 +01:00
|
|
|
boolean flag = itemstack.id == Item.BOOK.id;
|
2012-01-21 08:29:34 +01:00
|
|
|
|
2012-03-03 11:59:20 +01:00
|
|
|
if (list != null) {
|
|
|
|
// CraftBukkit start
|
2012-07-22 08:18:00 +02:00
|
|
|
Map<org.bukkit.enchantments.Enchantment, Integer> enchants = new java.util.HashMap<org.bukkit.enchantments.Enchantment, Integer>();
|
2012-03-03 11:59:20 +01:00
|
|
|
for (Object obj : list) {
|
|
|
|
EnchantmentInstance instance = (EnchantmentInstance) obj;
|
|
|
|
enchants.put(org.bukkit.enchantments.Enchantment.getById(instance.enchantment.id), instance.level);
|
|
|
|
}
|
2012-12-17 08:31:41 +01:00
|
|
|
CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
|
2012-02-29 22:31:04 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
EnchantItemEvent event = new EnchantItemEvent((Player) entityhuman.getBukkitEntity(), this.getBukkitView(), this.world.getWorld().getBlockAt(this.x, this.y, this.z), item, this.costs[i], enchants, i);
|
2012-03-03 11:59:20 +01:00
|
|
|
this.world.getServer().getPluginManager().callEvent(event);
|
2012-01-21 08:29:34 +01:00
|
|
|
|
2012-03-03 11:59:20 +01:00
|
|
|
int level = event.getExpLevelCost();
|
2012-03-10 21:39:29 +01:00
|
|
|
if (event.isCancelled() || (level > entityhuman.expLevel && !entityhuman.abilities.canInstantlyBuild) || enchants.isEmpty()) {
|
2012-03-03 11:59:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2012-12-21 03:05:00 +01:00
|
|
|
boolean applied = !flag;
|
2012-02-29 22:31:04 +01:00
|
|
|
for (Map.Entry<org.bukkit.enchantments.Enchantment, Integer> entry : event.getEnchantsToAdd().entrySet()) {
|
2012-01-21 08:29:34 +01:00
|
|
|
try {
|
2012-12-21 03:05:00 +01:00
|
|
|
if (flag) {
|
|
|
|
int enchantId = entry.getKey().getId();
|
|
|
|
if (Enchantment.byId[enchantId] == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnchantmentInstance enchantment = new EnchantmentInstance(enchantId, entry.getValue());
|
|
|
|
Item.ENCHANTED_BOOK.a(itemstack, enchantment);
|
|
|
|
applied = true;
|
|
|
|
itemstack.id = Item.ENCHANTED_BOOK.id;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
item.addEnchantment(entry.getKey(), entry.getValue());
|
|
|
|
}
|
2012-02-29 22:31:04 +01:00
|
|
|
} catch (IllegalArgumentException e) {
|
2012-01-21 08:29:34 +01:00
|
|
|
/* Just swallow invalid enchantments */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-21 03:05:00 +01:00
|
|
|
// Only down level if we've applied the enchantments
|
|
|
|
if (applied) {
|
|
|
|
entityhuman.levelDown(-level);
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
this.a(this.enchantSlots);
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
public void b(EntityHuman entityhuman) {
|
|
|
|
super.b(entityhuman);
|
2012-02-29 22:31:04 +01:00
|
|
|
if (!this.world.isStatic) {
|
2012-03-01 11:49:23 +01:00
|
|
|
ItemStack itemstack = this.enchantSlots.splitWithoutUpdate(0);
|
2012-01-21 08:29:34 +01:00
|
|
|
|
|
|
|
if (itemstack != null) {
|
2012-02-29 22:31:04 +01:00
|
|
|
entityhuman.drop(itemstack);
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
public boolean a(EntityHuman entityhuman) {
|
2012-02-29 19:56:35 +01:00
|
|
|
if (!this.checkReachable) return true; // CraftBukkit
|
2012-02-29 22:31:04 +01:00
|
|
|
return this.world.getTypeId(this.x, this.y, this.z) != Block.ENCHANTMENT_TABLE.id ? false : entityhuman.e((double) this.x + 0.5D, (double) this.y + 0.5D, (double) this.z + 0.5D) <= 64.0D;
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
public ItemStack b(EntityHuman entityhuman, int i) {
|
2012-01-21 08:29:34 +01:00
|
|
|
ItemStack itemstack = null;
|
2012-11-06 13:05:28 +01:00
|
|
|
Slot slot = (Slot) this.c.get(i);
|
2012-01-21 08:29:34 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (slot != null && slot.d()) {
|
2012-01-21 08:29:34 +01:00
|
|
|
ItemStack itemstack1 = slot.getItem();
|
|
|
|
|
|
|
|
itemstack = itemstack1.cloneItemStack();
|
2012-07-29 09:33:13 +02:00
|
|
|
if (i == 0) {
|
|
|
|
if (!this.a(itemstack1, 1, 37, true)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
2012-11-06 13:05:28 +01:00
|
|
|
if (((Slot) this.c.get(0)).d() || !((Slot) this.c.get(0)).isAllowed(itemstack1)) {
|
2012-07-29 09:33:13 +02:00
|
|
|
return null;
|
|
|
|
}
|
2012-01-21 08:29:34 +01:00
|
|
|
|
2012-07-29 09:33:13 +02:00
|
|
|
if (itemstack1.hasTag() && itemstack1.count == 1) {
|
2012-11-06 13:05:28 +01:00
|
|
|
((Slot) this.c.get(0)).set(itemstack1.cloneItemStack());
|
2012-07-29 09:33:13 +02:00
|
|
|
itemstack1.count = 0;
|
|
|
|
} else if (itemstack1.count >= 1) {
|
2012-11-06 13:05:28 +01:00
|
|
|
((Slot) this.c.get(0)).set(new ItemStack(itemstack1.id, 1, itemstack1.getData()));
|
2012-07-29 09:33:13 +02:00
|
|
|
--itemstack1.count;
|
|
|
|
}
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (itemstack1.count == 0) {
|
2012-02-29 22:31:04 +01:00
|
|
|
slot.set((ItemStack) null);
|
2012-01-21 08:29:34 +01:00
|
|
|
} else {
|
2012-07-29 09:33:13 +02:00
|
|
|
slot.e();
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (itemstack1.count == itemstack.count) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-10-25 05:53:23 +02:00
|
|
|
slot.a(entityhuman, itemstack1);
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return itemstack;
|
|
|
|
}
|
2012-02-29 19:56:35 +01:00
|
|
|
|
|
|
|
// CraftBukkit start
|
|
|
|
public CraftInventoryView getBukkitView() {
|
|
|
|
if (bukkitEntity != null) {
|
|
|
|
return bukkitEntity;
|
|
|
|
}
|
2012-07-29 09:33:13 +02:00
|
|
|
|
2012-02-29 22:31:04 +01:00
|
|
|
CraftInventoryEnchanting inventory = new CraftInventoryEnchanting(this.enchantSlots);
|
2012-02-29 19:56:35 +01:00
|
|
|
bukkitEntity = new CraftInventoryView(this.player, inventory, this);
|
|
|
|
return bukkitEntity;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2012-01-21 08:29:34 +01:00
|
|
|
}
|