Skip to content

Instantly share code, notes, and snippets.

@Daomephsta
Created November 10, 2018 00:25
Show Gist options
  • Save Daomephsta/388a8cb6a63af9a6c75afcdc0b71d97a to your computer and use it in GitHub Desktop.
Save Daomephsta/388a8cb6a63af9a6c75afcdc0b71d97a to your computer and use it in GitHub Desktop.
/* Adds loot functions equivalent to the damage, stacksize and NBT of the
* input stack to the passed in array, if loot functions of the same type
* are not present. */
public static LootFunction[] addStackFunctions(IItemStack iStack, LootFunction[] existingFunctions)
{
ItemStack stack = CraftTweakerMC.getItemStack(iStack);
boolean sizeFuncExists = false, damageFuncExists = false, nbtFuncExists = false;
for (LootFunction lootFunction : existingFunctions)
{
if (lootFunction instanceof SetCount) sizeFuncExists = true;
if (lootFunction instanceof SetDamage || lootFunction instanceof SetMetadata) damageFuncExists = true;
if (lootFunction instanceof SetNBT) nbtFuncExists = true;
}
int capacityRequired = existingFunctions.length + (sizeFuncExists ? 0 : 1) + (damageFuncExists ? 0 : 1)
+ (nbtFuncExists ? 0 : 1);
List<LootFunction> retList = Lists.newArrayListWithCapacity(capacityRequired);
Collections.addAll(retList, existingFunctions);
if (iStack.getAmount() > 1 && !sizeFuncExists)
{
retList.add(new SetCount(NO_CONDITIONS, new RandomValueRange(iStack.getAmount())));
}
if (iStack.getDamage() > 0 && !damageFuncExists)
{
if (stack.isItemStackDamageable())
{
// SetDamage takes a percentage, not a number
retList.add(new SetDamage(NO_CONDITIONS,
new RandomValueRange((float) stack.getItemDamage() / (float) stack.getMaxDamage())));
}
else
{
retList.add(new SetMetadata(NO_CONDITIONS, new RandomValueRange(iStack.getDamage())));
}
}
IData stackData = iStack.getTag();
if (stackData != DataMap.EMPTY && !nbtFuncExists)
{
retList.add(new SetNBT(NO_CONDITIONS, CraftTweakerMC.getNBTCompound(stackData)));
}
return retList.toArray(LootUtils.NO_FUNCTIONS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment