use of slimeknights.tconstruct.library.tools.nbt.ToolStack in project TinkersConstruct by SlimeKnights.
the class ModifierLootingHandler method onLooting.
/**
* Applies the looting bonus for modifiers
*/
private static void onLooting(LootingLevelEvent event) {
// must be an attacker with our tool
DamageSource damageSource = event.getDamageSource();
if (damageSource == null) {
return;
}
Entity source = damageSource.getEntity();
if (source instanceof LivingEntity) {
// TODO: consider bow usage, as the attack time is not the same as the death time
// TODO: extend to armor eventually
LivingEntity holder = ((LivingEntity) source);
EquipmentSlot slotType = getLootingSlot(holder);
ItemStack held = holder.getItemBySlot(slotType);
int level = event.getLootingLevel();
if (TinkerTags.Items.MODIFIABLE.contains(held.getItem())) {
ToolStack tool = ToolStack.from(held);
level = ModifierUtil.getLootingLevel(tool, holder, event.getEntityLiving(), damageSource);
// ignore default looting if we are looting from another slot
} else if (slotType != EquipmentSlot.MAINHAND) {
level = 0;
}
// boot looting with pants
level = ModifierUtil.getLeggingsLootingLevel(holder, event.getEntityLiving(), damageSource, level);
event.setLootingLevel(level);
}
}
use of slimeknights.tconstruct.library.tools.nbt.ToolStack in project TinkersConstruct by SlimeKnights.
the class ModifierUtil method applyHarvestEnchantments.
/**
* Adds all enchantments from tools. Separate method as tools don't have enchants all the time.
* Typically called before actions which involve loot, such as breaking blocks or attacking mobs.
* @param tool Tool instance
* @param stack Base stack instance
* @param context Tool harvest context
* @return Old tag if enchants were applied
*/
@Nullable
public static ListTag applyHarvestEnchantments(ToolStack tool, ItemStack stack, ToolHarvestContext context) {
ListTag originalEnchants = null;
Player player = context.getPlayer();
if (player == null || !player.isCreative()) {
Map<Enchantment, Integer> enchantments = new HashMap<>();
BiConsumer<Enchantment, Integer> enchantmentConsumer = (ench, add) -> {
if (ench != null && add != null) {
Integer level = enchantments.get(ench);
if (level != null) {
add += level;
}
enchantments.put(ench, add);
}
};
for (ModifierEntry entry : tool.getModifierList()) {
entry.getModifier().applyHarvestEnchantments(tool, entry.getLevel(), context, enchantmentConsumer);
}
// lucky pants
if (player != null) {
ItemStack pants = player.getItemBySlot(EquipmentSlot.LEGS);
if (TinkerTags.Items.LEGGINGS.contains(pants.getItem())) {
ToolStack pantsTool = ToolStack.from(pants);
for (ModifierEntry entry : pantsTool.getModifierList()) {
IArmorLootModifier leggingLuck = entry.getModifier().getModule(IArmorLootModifier.class);
if (leggingLuck != null) {
leggingLuck.applyHarvestEnchantments(tool, entry.getLevel(), context, enchantmentConsumer);
}
}
}
}
if (!enchantments.isEmpty()) {
// note this returns a new list if there is no tag, this is intentional as we need non-null to tell the tool to remove the tag
originalEnchants = stack.getEnchantmentTags();
EnchantmentHelper.setEnchantments(enchantments, stack);
}
}
return originalEnchants;
}
use of slimeknights.tconstruct.library.tools.nbt.ToolStack in project TinkersConstruct by SlimeKnights.
the class ModifierRepairCraftingRecipe method getRelevantInputs.
/**
* Gets the tool stack and the repair kit material from the crafting grid
* @param inv Crafting inventory
* @return Relevant inputs, or null if invalid
*/
@Nullable
protected Pair<ToolStack, Integer> getRelevantInputs(CraftingContainer inv) {
ToolStack tool = null;
int itemsFound = 0;
int modifierLevel = 0;
for (int i = 0; i < inv.getContainerSize(); i++) {
ItemStack stack = inv.getItem(i);
if (stack.isEmpty()) {
continue;
}
// repair kit - update material
if (TinkerTags.Items.DURABILITY.contains(stack.getItem())) {
// cannot repair multiple tools
if (tool != null) {
return null;
}
// tool must be damaged
tool = ToolStack.from(stack);
if (!tool.isBroken() && tool.getDamage() == 0) {
return null;
}
// tool must have the modifier
modifierLevel = tool.getModifierLevel(modifier);
if (modifierLevel == 0) {
return null;
}
// if we found a stack, add it to our count
} else if (ingredient.test(stack)) {
itemsFound++;
} else {
// unknown item input
return null;
}
}
// failed to find a tool or item? failed
if (tool == null || itemsFound == 0) {
return null;
}
return Pair.of(tool, repairAmount * itemsFound * modifierLevel);
}
use of slimeknights.tconstruct.library.tools.nbt.ToolStack in project TinkersConstruct by SlimeKnights.
the class ModifierRepairCraftingRecipe method assemble.
@Override
public ItemStack assemble(CraftingContainer inv) {
Pair<ToolStack, Integer> inputs = getRelevantInputs(inv);
if (inputs == null) {
TConstruct.LOG.error("Recipe repair on {} failed to find items after matching", getId());
return ItemStack.EMPTY;
}
// scale the repair based on the modifiers
float repairAmount = inputs.getSecond();
ToolStack tool = inputs.getFirst();
for (ModifierEntry entry : tool.getModifierList()) {
repairAmount = entry.getModifier().getRepairFactor(tool, entry.getLevel(), repairAmount);
if (repairAmount <= 0) {
// failed to repair
return ItemStack.EMPTY;
}
}
// repair the tool
tool = tool.copy();
ToolDamageUtil.repair(tool, (int) repairAmount);
return tool.createStack();
}
use of slimeknights.tconstruct.library.tools.nbt.ToolStack in project TinkersConstruct by SlimeKnights.
the class ModifierRepairTinkerStationRecipe method updateInputs.
@Override
public void updateInputs(ItemStack result, IMutableTinkerStationContainer inv, boolean isServer) {
ToolStack tool = ToolStack.from(inv.getTinkerableStack());
// rescale the amount based on modifiers
float repairFactor = 1.0f;
for (ModifierEntry entry : tool.getModifierList()) {
repairFactor = entry.getModifier().getRepairFactor(tool, entry.getLevel(), repairFactor);
if (repairFactor <= 0) {
return;
}
}
// also scale by relevant modifier level
int amountPerItem = (int) (tool.getModifierLevel(modifier) * repairAmount * repairFactor);
if (amountPerItem < 0) {
return;
}
// how much do we need to subtract from our inputs still
int repairRemaining = tool.getDamage() - ToolStack.from(result).getDamage();
IncrementalModifierRecipe.updateInputs(inv, ingredient, repairRemaining, amountPerItem, ItemStack.EMPTY);
}
Aggregations