Search in sources :

Example 1 with MetaToolValueItem

use of gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem in project GregTech by GregTechCE.

the class ToolMetaItemListener method onAnvilChange.

@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);
        }
    }
}
Also used : SolidMaterial(gregtech.api.unification.material.type.SolidMaterial) RoughSolidMaterial(gregtech.api.unification.material.type.RoughSolidMaterial) OrePrefix(gregtech.api.unification.ore.OrePrefix) UnificationEntry(gregtech.api.unification.stack.UnificationEntry) MetaToolValueItem(gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 2 with MetaToolValueItem

use of gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem in project GregTech by GregTechCE.

the class CommandHand method execute.

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
    if (sender instanceof EntityPlayerMP) {
        EntityPlayerMP player = (EntityPlayerMP) sender;
        ItemStack stackInHand = player.inventory.getCurrentItem();
        if (stackInHand.isEmpty()) {
            throw new CommandException("gregtech.command.util.hand.no_item");
        }
        String registryName = stackInHand.getItem().getRegistryName().toString();
        ClickEvent itemNameEvent = new ClickEvent(Action.OPEN_URL, registryName);
        player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.item_id", registryName, stackInHand.getItemDamage()).setStyle(new Style().setClickEvent(itemNameEvent)));
        IElectricItem electricItem = stackInHand.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
        IFluidHandlerItem fluidHandlerItem = stackInHand.getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null);
        if (electricItem != null) {
            player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.electric", electricItem.getCharge(), electricItem.getMaxCharge(), electricItem.getTier(), Boolean.toString(electricItem.canProvideChargeExternally())));
        }
        if (fluidHandlerItem != null) {
            for (IFluidTankProperties properties : fluidHandlerItem.getTankProperties()) {
                FluidStack contents = properties.getContents();
                String fluidName = contents == null ? "empty" : contents.getFluid().getName();
                ClickEvent fluidClickEvent = new ClickEvent(Action.OPEN_URL, fluidName);
                player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.fluid", fluidName, contents == null ? 0 : contents.amount, properties.getCapacity(), Boolean.toString(properties.canFill()), Boolean.toString(properties.canDrain())).setStyle(new Style().setClickEvent(fluidClickEvent)));
            }
        }
        if (stackInHand.getItem() instanceof MetaItem) {
            MetaItem<?> metaItem = (MetaItem<?>) stackInHand.getItem();
            MetaValueItem metaValueItem = metaItem.getItem(stackInHand);
            if (metaValueItem == null) {
                if (metaItem instanceof MaterialMetaItem) {
                    Material material = ((MaterialMetaItem) metaItem).getMaterial(stackInHand);
                    OrePrefix orePrefix = ((MaterialMetaItem) metaItem).getOrePrefix(stackInHand);
                    String oreDictName = new UnificationEntry(orePrefix, material).toString();
                    player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.material_meta_item", orePrefix, material).setStyle(new Style().setClickEvent(new ClickEvent(Action.OPEN_URL, oreDictName))));
                }
            } else {
                if (metaValueItem instanceof ToolMetaItem.MetaToolValueItem) {
                    IToolStats toolStats = ((MetaToolValueItem) metaValueItem).getToolStats();
                    player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.tool_stats", toolStats.getClass().getName()));
                }
                ClipboardUtil.copyToClipboard(player, metaValueItem.unlocalizedName);
                ClickEvent metaItemEvent = new ClickEvent(Action.OPEN_URL, metaValueItem.unlocalizedName);
                player.sendMessage(new TextComponentTranslation("gregtech.command.util.hand.meta_item", metaValueItem.unlocalizedName, metaValueItem).setStyle(new Style().setClickEvent(metaItemEvent)));
            }
        }
    } else {
        throw new CommandException("gregtech.command.util.hand.not_a_player");
    }
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) IElectricItem(gregtech.api.capability.IElectricItem) MetaValueItem(gregtech.api.items.metaitem.MetaItem.MetaValueItem) MaterialMetaItem(gregtech.api.items.materialitem.MaterialMetaItem) MetaItem(gregtech.api.items.metaitem.MetaItem) ToolMetaItem(gregtech.api.items.toolitem.ToolMetaItem) FluidStack(net.minecraftforge.fluids.FluidStack) ClickEvent(net.minecraft.util.text.event.ClickEvent) OrePrefix(gregtech.api.unification.ore.OrePrefix) UnificationEntry(gregtech.api.unification.stack.UnificationEntry) Material(gregtech.api.unification.material.type.Material) MetaToolValueItem(gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem) CommandException(net.minecraft.command.CommandException) IToolStats(gregtech.api.items.toolitem.IToolStats) MaterialMetaItem(gregtech.api.items.materialitem.MaterialMetaItem) IFluidTankProperties(net.minecraftforge.fluids.capability.IFluidTankProperties) IFluidHandlerItem(net.minecraftforge.fluids.capability.IFluidHandlerItem) Style(net.minecraft.util.text.Style) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(net.minecraft.item.ItemStack)

Example 3 with MetaToolValueItem

use of gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem in project GregTech by GregTechCE.

the class TreeChopTask method run.

@Override
public boolean run() {
    ItemStack itemInMainHand = this.player.getHeldItemMainhand();
    boolean isPlayerNear = player.world == world && currentPos.distanceSq(player.posX, currentPos.getY(), player.posZ) <= 1024;
    boolean isPlayerConnected = player.connection.netManager.isChannelOpen() && isPlayerNear;
    if (!isPlayerConnected || itemInMainHand.isEmpty() || !isItemEqual(itemInMainHand)) {
        return false;
    }
    ToolMetaItem<?> toolMetaItem = (ToolMetaItem<?>) itemStack.getItem();
    MetaToolValueItem toolValueItem = toolMetaItem.getItem(itemStack);
    if (toolValueItem == null) {
        return false;
    }
    IToolStats toolStats = toolValueItem.getToolStats();
    int damagePerBlockBreak = toolStats.getToolDamagePerBlockBreak(itemStack);
    if (!finishedSearchingBlocks) {
        this.finishedSearchingBlocks = !attemptSearchWoodBlocks() || this.visitedBlockPos.size() >= MAX_BLOCKS_TO_SEARCH;
        if (finishedSearchingBlocks) {
            this.woodBlockPos.sort(new WoodBlockComparator());
        }
        return true;
    }
    if (toolMetaItem.isUsable(itemInMainHand, damagePerBlockBreak) && tryBreakAny()) {
        toolMetaItem.damageItem(itemInMainHand, damagePerBlockBreak, false);
        return true;
    }
    return false;
}
Also used : IToolStats(gregtech.api.items.toolitem.IToolStats) ToolMetaItem(gregtech.api.items.toolitem.ToolMetaItem) MetaToolValueItem(gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem) ItemStack(net.minecraft.item.ItemStack)

Aggregations

MetaToolValueItem (gregtech.api.items.toolitem.ToolMetaItem.MetaToolValueItem)3 ItemStack (net.minecraft.item.ItemStack)3 IToolStats (gregtech.api.items.toolitem.IToolStats)2 ToolMetaItem (gregtech.api.items.toolitem.ToolMetaItem)2 OrePrefix (gregtech.api.unification.ore.OrePrefix)2 UnificationEntry (gregtech.api.unification.stack.UnificationEntry)2 IElectricItem (gregtech.api.capability.IElectricItem)1 MaterialMetaItem (gregtech.api.items.materialitem.MaterialMetaItem)1 MetaItem (gregtech.api.items.metaitem.MetaItem)1 MetaValueItem (gregtech.api.items.metaitem.MetaItem.MetaValueItem)1 Material (gregtech.api.unification.material.type.Material)1 RoughSolidMaterial (gregtech.api.unification.material.type.RoughSolidMaterial)1 SolidMaterial (gregtech.api.unification.material.type.SolidMaterial)1 CommandException (net.minecraft.command.CommandException)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 Style (net.minecraft.util.text.Style)1 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)1 ClickEvent (net.minecraft.util.text.event.ClickEvent)1 FluidStack (net.minecraftforge.fluids.FluidStack)1 IFluidHandlerItem (net.minecraftforge.fluids.capability.IFluidHandlerItem)1