Jump to content

Recommended Posts

Posted

So far, I have created an item that takes damage in a crafting table, but it doesn't get destroyed when all of the durability is used, and also crashes the game when it is used after shift-clicking an amount of items over the durability.

Here is the Item class:

Spoiler

package items;

import com.Cmonster.OreMod.Reference;
import com.Cmonster.OreMod.creativetabs.TabElements;

import init.ModItems;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class ItemOxygen_extractor extends Item {

    public ItemOxygen_extractor() {
        setUnlocalizedName(Reference.OreModItems.OXYGEN_EXTRACTOR.getUnlocalizedName());
        setRegistryName(Reference.OreModItems.OXYGEN_EXTRACTOR.getRegistryName());
        setCreativeTab(TabElements.tab_elements);
        maxStackSize = 1;
        setMaxDamage(10);
        setNoRepair();
    }
    
    @Override
    public boolean hasContainerItem(ItemStack stack) {
        return true;
    }
    
    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        if (itemStack.getMaxDamage() == itemStack.getItemDamage()) {
            return (ItemStack) null;
            
    } else {
        ItemStack newItemStack = itemStack.copy();
        newItemStack.setItemDamage(itemStack.getItemDamage() + 1);
        return newItemStack;
    }
}
}

And here is the crafting recipe code:

GameRegistry.addShapelessRecipe(new ItemStack(ModItems.lithium_carbonate), ModItems.lithium_ingot, Items.COAL, new ItemStack(ModItems.oxygen_extractor, 1, OreDictionary.WILDCARD_VALUE));

Coal is used as a placeholder here.

Posted (edited)

you need to return ItemStack.EMPTY rather than null. Parameterizing a null still gives a null variable, and looking at the ForgeHooks class (and the Shaped Recipe class, but this is more irrelevant), the passed in ItemStack from the getContainerItem method calls !stack.isEmpty(), which if the passed in stack is null, can't be done.

Edited by draganz
Posted (edited)

Thanks for the answers

It works, but after the first use, I can't use it again.

Code:

Spoiler

package items;

import com.Cmonster.OreMod.Reference;
import com.Cmonster.OreMod.creativetabs.TabElements;

import init.ModItems;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class ItemOxygen_extractor extends Item {

    public ItemOxygen_extractor() {
        setUnlocalizedName(Reference.OreModItems.OXYGEN_EXTRACTOR.getUnlocalizedName());
        setRegistryName(Reference.OreModItems.OXYGEN_EXTRACTOR.getRegistryName());
        setCreativeTab(TabElements.tab_elements);
        maxStackSize = 1;
        setMaxDamage(10);
        setNoRepair();
    }
    
    @Override
    public boolean hasContainerItem(ItemStack stack) {
        return true;
    }
    
    @Override
    public ItemStack getContainerItem(ItemStack itemStack) {
        if (itemStack.getMaxDamage() == itemStack.getItemDamage()) {
            return ItemStack.EMPTY;
            
    } else {
        ItemStack newItemStack = itemStack.copy();
        newItemStack.setItemDamage(itemStack.getItemDamage() + 1);
        return newItemStack;
    }
}
}

 

Edited by Cmonster
Posted (edited)

After I use the item in the craftin table the first time, it takes one durability value, but after I try to use the same item, the recipe does not work.

 

EDIT: Mistake on my part, I needed to save the code :/

Edited by Cmonster

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.