2012-10-25 05:53:23 +02:00
|
|
|
package net.minecraft.server;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
// CraftBukkit start
|
|
|
|
import org.bukkit.craftbukkit.inventory.CraftInventory;
|
|
|
|
import org.bukkit.craftbukkit.inventory.CraftInventoryAnvil;
|
|
|
|
import org.bukkit.craftbukkit.inventory.CraftInventoryView;
|
|
|
|
// CraftBukkit end
|
|
|
|
|
|
|
|
public class ContainerAnvil extends Container {
|
|
|
|
|
|
|
|
private IInventory f = new InventoryCraftResult();
|
2013-03-13 23:33:27 +01:00
|
|
|
private IInventory g = new ContainerAnvilInventory(this, "Repair", true, 2);
|
2012-10-25 05:53:23 +02:00
|
|
|
private World h;
|
|
|
|
private int i;
|
|
|
|
private int j;
|
|
|
|
private int k;
|
2012-11-06 13:05:28 +01:00
|
|
|
public int a = 0;
|
2012-10-25 05:53:23 +02:00
|
|
|
private int l = 0;
|
|
|
|
private String m;
|
|
|
|
private final EntityHuman n;
|
|
|
|
// CraftBukkit start
|
|
|
|
private CraftInventoryView bukkitEntity = null;
|
|
|
|
private PlayerInventory player;
|
|
|
|
// CraftBukkit end
|
|
|
|
|
|
|
|
public ContainerAnvil(PlayerInventory playerinventory, World world, int i, int j, int k, EntityHuman entityhuman) {
|
|
|
|
this.player = playerinventory; // CraftBukkit
|
|
|
|
this.h = world;
|
|
|
|
this.i = i;
|
|
|
|
this.j = j;
|
|
|
|
this.k = k;
|
|
|
|
this.n = entityhuman;
|
|
|
|
this.a(new Slot(this.g, 0, 27, 47));
|
|
|
|
this.a(new Slot(this.g, 1, 76, 47));
|
|
|
|
this.a((Slot) (new SlotAnvilResult(this, this.f, 2, 134, 47, world, i, j, k)));
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void a(IInventory iinventory) {
|
|
|
|
super.a(iinventory);
|
|
|
|
if (iinventory == this.g) {
|
2013-03-13 23:33:27 +01:00
|
|
|
this.e();
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:33:27 +01:00
|
|
|
public void e() {
|
2012-10-25 05:53:23 +02:00
|
|
|
ItemStack itemstack = this.g.getItem(0);
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
this.a = 0;
|
2012-10-25 05:53:23 +02:00
|
|
|
int i = 0;
|
|
|
|
byte b0 = 0;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
if (itemstack == null) {
|
|
|
|
this.f.setItem(0, (ItemStack) null);
|
2012-11-06 13:05:28 +01:00
|
|
|
this.a = 0;
|
2012-10-25 05:53:23 +02:00
|
|
|
} else {
|
|
|
|
ItemStack itemstack1 = itemstack.cloneItemStack();
|
|
|
|
ItemStack itemstack2 = this.g.getItem(1);
|
|
|
|
Map map = EnchantmentManager.a(itemstack1);
|
2012-12-20 05:03:52 +01:00
|
|
|
boolean flag = false;
|
2012-10-25 05:53:23 +02:00
|
|
|
int k = b0 + itemstack.getRepairCost() + (itemstack2 == null ? 0 : itemstack2.getRepairCost());
|
|
|
|
|
|
|
|
this.l = 0;
|
|
|
|
int l;
|
|
|
|
int i1;
|
|
|
|
int j1;
|
|
|
|
int k1;
|
2012-12-20 05:03:52 +01:00
|
|
|
int l1;
|
2012-10-25 05:53:23 +02:00
|
|
|
Iterator iterator;
|
2012-12-20 05:03:52 +01:00
|
|
|
Enchantment enchantment;
|
2012-10-25 05:53:23 +02:00
|
|
|
|
|
|
|
if (itemstack2 != null) {
|
2012-12-20 05:03:52 +01:00
|
|
|
flag = itemstack2.id == Item.ENCHANTED_BOOK.id && Item.ENCHANTED_BOOK.g(itemstack2).size() > 0;
|
2013-03-13 23:33:27 +01:00
|
|
|
if (itemstack1.g() && Item.byId[itemstack1.id].a(itemstack, itemstack2)) {
|
|
|
|
l = Math.min(itemstack1.j(), itemstack1.l() / 4);
|
2012-10-25 05:53:23 +02:00
|
|
|
if (l <= 0) {
|
|
|
|
this.f.setItem(0, (ItemStack) null);
|
2012-11-06 13:05:28 +01:00
|
|
|
this.a = 0;
|
2012-10-25 05:53:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i1 = 0; l > 0 && i1 < itemstack2.count; ++i1) {
|
2013-03-13 23:33:27 +01:00
|
|
|
j1 = itemstack1.j() - l;
|
2012-10-25 05:53:23 +02:00
|
|
|
itemstack1.setData(j1);
|
|
|
|
i += Math.max(1, l / 100) + map.size();
|
2013-03-13 23:33:27 +01:00
|
|
|
l = Math.min(itemstack1.j(), itemstack1.l() / 4);
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.l = i1;
|
|
|
|
} else {
|
2013-03-13 23:33:27 +01:00
|
|
|
if (!flag && (itemstack1.id != itemstack2.id || !itemstack1.g())) {
|
2012-10-25 05:53:23 +02:00
|
|
|
this.f.setItem(0, (ItemStack) null);
|
2012-11-06 13:05:28 +01:00
|
|
|
this.a = 0;
|
2012-10-25 05:53:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:33:27 +01:00
|
|
|
if (itemstack1.g() && !flag) {
|
|
|
|
l = itemstack.l() - itemstack.j();
|
|
|
|
i1 = itemstack2.l() - itemstack2.j();
|
|
|
|
j1 = i1 + itemstack1.l() * 12 / 100;
|
2012-12-20 05:03:52 +01:00
|
|
|
int i2 = l + j1;
|
2012-10-25 05:53:23 +02:00
|
|
|
|
2013-03-13 23:33:27 +01:00
|
|
|
k1 = itemstack1.l() - i2;
|
2012-10-25 05:53:23 +02:00
|
|
|
if (k1 < 0) {
|
|
|
|
k1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (k1 < itemstack1.getData()) {
|
|
|
|
itemstack1.setData(k1);
|
|
|
|
i += Math.max(1, j1 / 100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Map map1 = EnchantmentManager.a(itemstack2);
|
|
|
|
|
|
|
|
iterator = map1.keySet().iterator();
|
|
|
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
j1 = ((Integer) iterator.next()).intValue();
|
|
|
|
enchantment = Enchantment.byId[j1];
|
|
|
|
k1 = map.containsKey(Integer.valueOf(j1)) ? ((Integer) map.get(Integer.valueOf(j1))).intValue() : 0;
|
2012-12-20 05:03:52 +01:00
|
|
|
l1 = ((Integer) map1.get(Integer.valueOf(j1))).intValue();
|
2012-10-25 05:53:23 +02:00
|
|
|
int j2;
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
if (k1 == l1) {
|
|
|
|
++l1;
|
|
|
|
j2 = l1;
|
2012-10-25 05:53:23 +02:00
|
|
|
} else {
|
2012-12-20 05:03:52 +01:00
|
|
|
j2 = Math.max(l1, k1);
|
|
|
|
}
|
|
|
|
|
|
|
|
l1 = j2;
|
|
|
|
int k2 = l1 - k1;
|
|
|
|
boolean flag1 = enchantment.canEnchant(itemstack);
|
|
|
|
|
|
|
|
if (this.n.abilities.canInstantlyBuild) {
|
|
|
|
flag1 = true;
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator iterator1 = map.keySet().iterator();
|
|
|
|
|
|
|
|
while (iterator1.hasNext()) {
|
|
|
|
int l2 = ((Integer) iterator1.next()).intValue();
|
|
|
|
|
|
|
|
if (l2 != j1 && !enchantment.a(Enchantment.byId[l2])) {
|
2012-12-20 05:03:52 +01:00
|
|
|
flag1 = false;
|
2012-10-25 05:53:23 +02:00
|
|
|
i += k2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
if (flag1) {
|
|
|
|
if (l1 > enchantment.getMaxLevel()) {
|
|
|
|
l1 = enchantment.getMaxLevel();
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
map.put(Integer.valueOf(j1), Integer.valueOf(l1));
|
|
|
|
int i3 = 0;
|
2012-10-25 05:53:23 +02:00
|
|
|
|
|
|
|
switch (enchantment.getRandomWeight()) {
|
|
|
|
case 1:
|
2012-12-20 05:03:52 +01:00
|
|
|
i3 = 8;
|
2012-10-25 05:53:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2012-12-20 05:03:52 +01:00
|
|
|
i3 = 4;
|
2012-10-25 05:53:23 +02:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
case 9:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2012-12-20 05:03:52 +01:00
|
|
|
i3 = 2;
|
2012-10-25 05:53:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
2012-12-20 05:03:52 +01:00
|
|
|
i3 = 1;
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
2013-01-25 01:10:03 +01:00
|
|
|
if (flag) {
|
|
|
|
i3 = Math.max(1, i3 / 2);
|
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
i += i3 * k2;
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:33:27 +01:00
|
|
|
if (this.m != null && !this.m.equalsIgnoreCase(itemstack.getName()) && this.m.length() > 0) {
|
|
|
|
j = itemstack.g() ? 7 : itemstack.count * 5;
|
2012-10-25 05:53:23 +02:00
|
|
|
i += j;
|
2013-03-13 23:33:27 +01:00
|
|
|
if (itemstack.hasName()) {
|
2012-10-25 05:53:23 +02:00
|
|
|
k += j / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemstack1.c(this.m);
|
|
|
|
}
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
for (iterator = map.keySet().iterator(); iterator.hasNext(); k += l + k1 * l1) {
|
2012-10-25 05:53:23 +02:00
|
|
|
j1 = ((Integer) iterator.next()).intValue();
|
|
|
|
enchantment = Enchantment.byId[j1];
|
|
|
|
k1 = ((Integer) map.get(Integer.valueOf(j1))).intValue();
|
2012-12-20 05:03:52 +01:00
|
|
|
l1 = 0;
|
2012-10-25 05:53:23 +02:00
|
|
|
++l;
|
|
|
|
switch (enchantment.getRandomWeight()) {
|
|
|
|
case 1:
|
2012-12-20 05:03:52 +01:00
|
|
|
l1 = 8;
|
2012-10-25 05:53:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2012-12-20 05:03:52 +01:00
|
|
|
l1 = 4;
|
2012-10-25 05:53:23 +02:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
case 9:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
2012-12-20 05:03:52 +01:00
|
|
|
l1 = 2;
|
2012-10-25 05:53:23 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
2012-12-20 05:03:52 +01:00
|
|
|
l1 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
l1 = Math.max(1, l1 / 2);
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-20 05:03:52 +01:00
|
|
|
if (flag) {
|
|
|
|
k = Math.max(1, k / 2);
|
|
|
|
}
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
this.a = k + i;
|
2012-10-25 05:53:23 +02:00
|
|
|
if (i <= 0) {
|
|
|
|
itemstack1 = null;
|
|
|
|
}
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
if (j == i && j > 0 && this.a >= 40) {
|
2013-03-13 23:33:27 +01:00
|
|
|
// this.h.getLogger().info("Naming an item only, cost too high; giving discount to cap cost to 39 levels"); // CraftBukkit - remove debug
|
2012-11-06 13:05:28 +01:00
|
|
|
this.a = 39;
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
if (this.a >= 40 && !this.n.abilities.canInstantlyBuild) {
|
2012-10-25 05:53:23 +02:00
|
|
|
itemstack1 = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemstack1 != null) {
|
|
|
|
i1 = itemstack1.getRepairCost();
|
|
|
|
if (itemstack2 != null && i1 < itemstack2.getRepairCost()) {
|
|
|
|
i1 = itemstack2.getRepairCost();
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:33:27 +01:00
|
|
|
if (itemstack1.hasName()) {
|
2012-11-06 13:05:28 +01:00
|
|
|
i1 -= 9;
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i1 < 0) {
|
|
|
|
i1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
i1 += 2;
|
|
|
|
itemstack1.setRepairCost(i1);
|
|
|
|
EnchantmentManager.a(map, itemstack1);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.f.setItem(0, itemstack1);
|
|
|
|
this.b();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addSlotListener(ICrafting icrafting) {
|
|
|
|
super.addSlotListener(icrafting);
|
2012-11-06 13:05:28 +01:00
|
|
|
icrafting.setContainerData(this, 0, this.a);
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
public void b(EntityHuman entityhuman) {
|
|
|
|
super.b(entityhuman);
|
2012-10-25 05:53:23 +02:00
|
|
|
if (!this.h.isStatic) {
|
|
|
|
for (int i = 0; i < this.g.getSize(); ++i) {
|
|
|
|
ItemStack itemstack = this.g.splitWithoutUpdate(i);
|
|
|
|
|
|
|
|
if (itemstack != null) {
|
|
|
|
entityhuman.drop(itemstack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-06 13:05:28 +01:00
|
|
|
public boolean a(EntityHuman entityhuman) {
|
2012-10-25 05:53:23 +02:00
|
|
|
if (!this.checkReachable) return true; // CraftBukkit
|
|
|
|
return this.h.getTypeId(this.i, this.j, this.k) != Block.ANVIL.id ? false : entityhuman.e((double) this.i + 0.5D, (double) this.j + 0.5D, (double) this.k + 0.5D) <= 64.0D;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack b(EntityHuman entityhuman, int i) {
|
|
|
|
ItemStack itemstack = null;
|
2012-11-06 13:05:28 +01:00
|
|
|
Slot slot = (Slot) this.c.get(i);
|
2012-10-25 05:53:23 +02:00
|
|
|
|
|
|
|
if (slot != null && slot.d()) {
|
|
|
|
ItemStack itemstack1 = slot.getItem();
|
|
|
|
|
|
|
|
itemstack = itemstack1.cloneItemStack();
|
|
|
|
if (i == 2) {
|
|
|
|
if (!this.a(itemstack1, 3, 39, true)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
slot.a(itemstack1, itemstack);
|
|
|
|
} else if (i != 0 && i != 1) {
|
|
|
|
if (i >= 3 && i < 39 && !this.a(itemstack1, 0, 2, false)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else if (!this.a(itemstack1, 3, 39, false)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemstack1.count == 0) {
|
|
|
|
slot.set((ItemStack) null);
|
|
|
|
} else {
|
|
|
|
slot.e();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemstack1.count == itemstack.count) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
slot.a(entityhuman, itemstack1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemstack;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void a(String s) {
|
|
|
|
this.m = s;
|
|
|
|
if (this.getSlot(2).d()) {
|
|
|
|
this.getSlot(2).getItem().c(this.m);
|
|
|
|
}
|
|
|
|
|
2013-03-13 23:33:27 +01:00
|
|
|
this.e();
|
2012-10-25 05:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static IInventory a(ContainerAnvil containeranvil) {
|
|
|
|
return containeranvil.g;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int b(ContainerAnvil containeranvil) {
|
|
|
|
return containeranvil.l;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CraftBukkit start
|
|
|
|
public CraftInventoryView getBukkitView() {
|
|
|
|
if (bukkitEntity != null) {
|
|
|
|
return bukkitEntity;
|
|
|
|
}
|
|
|
|
|
|
|
|
CraftInventory inventory = new CraftInventoryAnvil(this.g);
|
|
|
|
bukkitEntity = new CraftInventoryView(this.player.player.getBukkitEntity(), inventory, this);
|
|
|
|
return bukkitEntity;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
|
|
|
}
|