-
Notifications
You must be signed in to change notification settings - Fork 755
/
AmmoItem.java
118 lines (99 loc) · 4.15 KB
/
AmmoItem.java
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
package tconstruct.weaponry.library.weaponry;
import tconstruct.weaponry.TinkerWeaponry;
import tconstruct.weaponry.util.Reference;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import tconstruct.library.tools.ToolCore;
public abstract class AmmoItem extends ToolCore implements IAmmo {
public AmmoItem(int baseDamage, String name) {
super(baseDamage);
this.setUnlocalizedName(Reference.prefix(name));
this.setCreativeTab(TinkerWeaponry.creativeTab);
}
@Override
public int getAmmoCount(ItemStack stack) {
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
return tags.getInteger("Ammo");
}
@Override
public int getMaxAmmo(ItemStack stack) {
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
return getMaxAmmo(tags);
}
@Override
public int getMaxAmmo(NBTTagCompound tags) {
float dur = tags.getInteger("TotalDurability");
return (int)Math.ceil(dur*getAmmoModifier());
}
@Override
public int addAmmo(int toAdd, ItemStack stack) {
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
int oldCount = tags.getInteger("Ammo");
int newCount = Math.min(oldCount + toAdd, getMaxAmmo(stack));
tags.setInteger("Ammo", newCount);
return toAdd - (newCount - oldCount);
}
@Override
public int consumeAmmo(int toUse, ItemStack stack) {
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
int oldCount = tags.getInteger("Ammo");
int newCount = Math.max(oldCount - toUse, 0);
tags.setInteger("Ammo", newCount);
return toUse - (oldCount - newCount);
}
public float getAmmoModifier() { return 0.1f; }
public boolean pickupAmmo(ItemStack stack, ItemStack candidate, EntityPlayer player)
{
if(stack.getItem() == null || !(stack.getItem() instanceof IAmmo))
return false;
// check if our candidate fits
if(candidate != null)
{
// same item
if(testIfAmmoMatches(stack, candidate)) {
IAmmo pickedup = ((IAmmo) stack.getItem());
IAmmo ininventory = ((IAmmo) candidate.getItem());
// we can be sure that it's ammo, since stack is ammo and they're equal
ininventory.addAmmo(pickedup.getAmmoCount(stack), candidate);
return true;
}
}
// search the players inventory
for(ItemStack invItem : player.inventory.mainInventory) {
if (testIfAmmoMatches(stack, invItem)) {
IAmmo pickedup = ((IAmmo) stack.getItem());
IAmmo ininventory = ((IAmmo) invItem.getItem());
// we can be sure that it's ammo, since stack is ammo and they're equal
ininventory.addAmmo(pickedup.getAmmoCount(stack), invItem);
return true;
}
}
// couldn't find a matching thing.
return false;
}
private boolean testIfAmmoMatches(ItemStack reference, ItemStack candidate)
{
if(candidate == null)
return false;
if(!candidate.hasTagCompound() || !candidate.getTagCompound().hasKey("InfiTool"))
return false;
// create a stack to test against
ItemStack testsubject = candidate.copy();
// all NBT has to match, but the ammo-count obviously differs. So we set our testsubject to the same
// this ensures that it's the one to collect and the one we have matches.
testsubject.getTagCompound().getCompoundTag("InfiTool").setInteger("Ammo", getAmmoCount(reference));
return ItemStack.areItemStacksEqual(reference, testsubject);
}
@Override
public boolean onLeftClickEntity (ItemStack stack, EntityPlayer player, Entity entity)
{
// ammo doesn't hurt on smacking stuff with it
return false;
}
@Override
public String getDefaultTexturePath() {
return Reference.RESOURCE + ":" + getDefaultFolder();
}
}