Skip to content

Commit

Permalink
Changed the logic behind picking up ammo on the ground. Basically the…
Browse files Browse the repository at this point in the history
… base stats and composition has to be the same for an entity to be able to be picked up.

Small changes to the NBT that have nothing to do with the actual tool don't prevent picking them up anymore that way.
  • Loading branch information
bonii-xx committed Apr 3, 2015
1 parent 7488b48 commit 5d4d646
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/main/java/tconstruct/library/weaponry/AmmoItem.java
Expand Up @@ -37,7 +37,7 @@ public int getMaxAmmo(ItemStack stack) {
@Override
public int getMaxAmmo(NBTTagCompound tags) {
float dur = tags.getInteger("TotalDurability");
return Math.max(1, (int)Math.ceil(dur*getAmmoModifier()));
return Math.max(1, (int) Math.ceil(dur * getAmmoModifier()));
}

@Override
Expand Down Expand Up @@ -83,9 +83,9 @@ public boolean pickupAmmo(ItemStack stack, ItemStack candidate, EntityPlayer pla
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;
int count = pickedup.getAmmoCount(stack);
if(count != ininventory.addAmmo(count, candidate))
return true;
}
}

Expand All @@ -95,9 +95,9 @@ public boolean pickupAmmo(ItemStack stack, ItemStack candidate, EntityPlayer pla
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;
int count = pickedup.getAmmoCount(stack);
if(count != ininventory.addAmmo(count, invItem))
return true;
}
}

Expand All @@ -112,29 +112,42 @@ private boolean testIfAmmoMatches(ItemStack reference, ItemStack candidate)
if(!candidate.hasTagCompound() || !candidate.getTagCompound().hasKey("InfiTool"))
return false;

// create a stack to test against
ItemStack testsubject = candidate.copy();
reference = reference.copy();
// all NBT has to match, but the ammo-count obviously differs.
// we strip known tags that have no impact

NBTTagCompound tags = testsubject.getTagCompound().getCompoundTag("InfiTool");
tags.removeTag("Ammo");
tags.removeTag("ToolEXP");
tags.removeTag("ToolLevel");
tags.removeTag("HeadEXP");
tags.removeTag("Damage");

tags = reference.getTagCompound().getCompoundTag("InfiTool");
tags.removeTag("Ammo");
tags.removeTag("ToolEXP");
tags.removeTag("ToolLevel");
tags.removeTag("HeadEXP");
tags.removeTag("Damage");

return ItemStack.areItemStacksEqual(reference, testsubject);
if(reference.getItem() != candidate.getItem())
return false;

NBTTagCompound referenceTags = getComparisonTags(reference);
NBTTagCompound testTags = getComparisonTags(candidate);

return referenceTags.equals(testTags);
}

private NBTTagCompound getComparisonTags(ItemStack stack) {
NBTTagCompound tags = stack.getTagCompound().getCompoundTag("InfiTool");
NBTTagCompound out = new NBTTagCompound();

copyTag(out, tags, "Head");
copyTag(out, tags, "Handle");
copyTag(out, tags, "Accessory");
copyTag(out, tags, "Extra");
copyTag(out, tags, "RenderHead");
copyTag(out, tags, "RenderHandle");
copyTag(out, tags, "RenderAccessory");
copyTag(out, tags, "RenderExtra");
copyTag(out, tags, "TotalDurability");
copyTag(out, tags, "Attack");
copyTag(out, tags, "MiningSpeed");
copyTag(out, tags, "HarvestLevel");
copyTag(out, tags, "Modifiers");

return out;
}

private void copyTag(NBTTagCompound out, NBTTagCompound in, String tag) {
if(in.hasKey(tag))
out.setInteger(tag, in.getInteger(tag));
}


@Override
public boolean onLeftClickEntity (ItemStack stack, EntityPlayer player, Entity entity)
{
Expand Down

0 comments on commit 5d4d646

Please sign in to comment.