Java Code Examples for net.minecraftforge.event.AnvilUpdateEvent#setMaterialCost()

The following examples show how to use net.minecraftforge.event.AnvilUpdateEvent#setMaterialCost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnvilUpdateEventHandler.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void fullyRepairItem(AnvilUpdateEvent event, IAnvilRepairable repairable, int materialCost, int xpCost)
{
    ItemStack repaired = event.getLeft().copy();

    if (repairable.repairItem(repaired, -1))
    {
        event.setMaterialCost(materialCost);
        event.setCost(xpCost);
        event.setOutput(repaired);

        this.updateItemName(event, repaired);
    }
    else
    {
        event.setCanceled(true);
    }
}
 
Example 2
Source File: ToolMetaItemListener.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SubscribeEvent
public static void onAnvilChange(AnvilUpdateEvent event) {
    ItemStack firstStack = event.getLeft();
    ItemStack secondStack = event.getRight();
    if (!firstStack.isEmpty() && !secondStack.isEmpty() && firstStack.getItem() instanceof ToolMetaItem) {
        ToolMetaItem<?> toolMetaItem = (ToolMetaItem<?>) firstStack.getItem();
        MetaToolValueItem toolValueItem = toolMetaItem.getItem(firstStack);
        if (toolValueItem == null) {
            return;
        }
        SolidMaterial toolMaterial = ToolMetaItem.getToolMaterial(firstStack);
        OrePrefix solidPrefix = getSolidPrefix(toolMaterial);
        UnificationEntry unificationEntry = OreDictUnifier.getUnificationEntry(secondStack);
        double toolDamage = toolMetaItem.getItemDamage(firstStack) / (toolMetaItem.getMaxItemDamage(firstStack) * 1.0);
        double materialForFullRepair = toolValueItem.getAmountOfMaterialToRepair(firstStack);
        int durabilityPerUnit = (int) Math.ceil(toolMetaItem.getMaxItemDamage(firstStack) / materialForFullRepair);
        int materialUnitsRequired = Math.min(secondStack.getCount(), (int) Math.ceil(toolDamage * materialForFullRepair));
        int repairCost = (MathHelper.clamp(toolMaterial.harvestLevel, 2, 3) - 1) * materialUnitsRequired;

        if (toolDamage > 0.0 && materialUnitsRequired > 0 && unificationEntry != null &&
            unificationEntry.material == toolMaterial && unificationEntry.orePrefix == solidPrefix) {
            int durabilityToRegain = durabilityPerUnit * materialUnitsRequired;
            ItemStack resultStack = firstStack.copy();
            toolMetaItem.regainItemDurability(resultStack, durabilityToRegain);
            event.setMaterialCost(materialUnitsRequired);
            event.setCost(repairCost);
            event.setOutput(resultStack);
        }
    }
}