Jump to content

Recommended Posts

Posted

Hello,

 

I'm trying to create a new Bow that instead of using arrows, it consumes energy from a crystal orb (item from the mod). I've hit a roadblock trying to figure out how to "damage" the orb every time the bow shoots. I've searched practically everywhere, but I cannot find a good tutorial on how one might go about that. I'm wondering if anyone else out there has encountered this or if they could offer any advice.

 

Edit: I'm relatively new with modding, but have some programming experience, mostly C#.

 

Here is the Bow.class:

 

package aceofkings.rangermod;

import java.util.List;

import aceofkings.rangermod.RangerMain;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.ArrowLooseEvent;
import net.minecraftforge.event.entity.player.ArrowNockEvent;

public class RangerBowMagic extends Item {

//Custom charge speed
public float charge;

//custom animation adjustments
public int anim_0;
public int anim_1;
public int anim_2;

//		public int b_type;

    public RangerBowMagic(float chargeTime, int anim_0, int anim_1, int anim_2)
    {
    	//You can't stack more than one bow.
        this.maxStackSize = 1;
        //Creative tab
        setCreativeTab(RangerMain.tabRangerMod);
        
        //allows custom chargeTime
        this.charge = chargeTime;
        
        //allows custom adjustment of animation
        this.anim_0 = anim_0;
        this.anim_1 = anim_1;
        this.anim_2 = anim_2;
        
		setMaxDamage(637);

     
    }
    
    public String[] bowPullIconNameArray;// = new String[] {this.t_0, this.t_1, this.t_2};
    @SideOnly(Side.CLIENT)
    private IIcon[] iconArray;
    
    //Fixes "FileNotFound...null.png"
    //allows custom texture for different bows
    public void getBowPullIconNameArray(){
    	
    	bowPullIconNameArray = new String[] {"bowCrystal_0", "bowCrystal_1", "bowCrystal_2" };
    }
// Not sure why i still have this... 
//	    public void onCreated(ItemStack par1ItemStack, World world, EntityPlayer entity){
//	    	
//	    	//Adds infinity to Crystal bow. Compensates for the Nether Star
//	    	if(b_type == 4){
//	    		par1ItemStack.addEnchantment(Enchantment.infinity, 1);
//	    	}
//	    }
    
    //
    public boolean hasEffect(ItemStack par1ItemStack){
    	return true;
    }
    
    public void addInformation(ItemStack par1ItemStack, EntityPlayer entityplayer, List list, boolean is){
    	
    }

    /**
     * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount
     */
    public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
    {
        int j = this.getMaxItemUseDuration(par1ItemStack) - par4;

        ArrowLooseEvent event = new ArrowLooseEvent(par3EntityPlayer, par1ItemStack, j);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return;
        }
        j = event.charge;
        
        boolean flag = par3EntityPlayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, par1ItemStack) > 0;
        
        if (flag || par3EntityPlayer.inventory.hasItem(RangerMain.crystal_orb))
        {

        	//Change 20.0F to increase/decrease charge speed.
            float f = (float)j / charge;
            f = (f * f + f * 2.0F) / 3.0F;

            if ((double)f < 0.1D)
            {
                return;
            }

            if (f > 1.0F)
            {
                f = 1.0F;
            }

            EntityArrow entityarrow = new EntityArrow(par2World, par3EntityPlayer, f * 2.0F);

            if (f == 1.0F)
            {
                entityarrow.setIsCritical(true);
            }
            
            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);

            if (k > 0)
            {
            	entityarrow.setDamage(entityarrow.getDamage() + (double)k * 0.5D + 0.5D);
            }

            int l = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, par1ItemStack);

            if (l > 0)
            {
                entityarrow.setKnockbackStrength(l);
            }

            if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, par1ItemStack) > 0)
            {
                entityarrow.setFire(100);
            }
            
            par1ItemStack.damageItem(1, par3EntityPlayer);
            par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);

            if (flag)
            {
                entityarrow.canBePickedUp = 2;
            }
            else
            {
            	ItemStack itemstack = new ItemStack(RangerMain.crystal_orb);
            	int d = itemstack.getItemDamage();
            	
            	if(d < itemstack.getMaxDamage()){
            		itemstack.setItemDamage(d + 1);
            		
            	}
            	itemstack.getItemDamageForDisplay();
            	entityarrow.canBePickedUp = 2;         	
            }

            if (!par2World.isRemote)
            {
                par2World.spawnEntityInWorld(entityarrow);
            }
        	
        }
    }

    public ItemStack onEaten(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        return par1ItemStack;
    }

    /**
     * How long it takes to use or consume an item
     */
    public int getMaxItemUseDuration(ItemStack par1ItemStack)
    {
        return 72000;
    }

    /**
     * returns the action that specifies what animation to play when the items is being used
     */
    public EnumAction getItemUseAction(ItemStack par1ItemStack)
    {
        return EnumAction.bow;
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
    	//debugging trying to figure out the getting and setting damage of the orb.
    	ItemStack itemstack = new ItemStack(RangerMain.crystal_orb);
    	int d = itemstack.getItemDamage();
    	
    	System.out.println(d);
    	
    	itemstack.setItemDamage(d + 1);
    	int dd = itemstack.getItemDamage();
    	
    	System.out.println(dd);
    	
    	
    	
    	
    	
    	
    	
    	
    	
        ArrowNockEvent event = new ArrowNockEvent(par3EntityPlayer, par1ItemStack);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return event.result;
        }

        if (par3EntityPlayer.capabilities.isCreativeMode || par3EntityPlayer.inventory.hasItem(RangerMain.crystal_orb))
        {
            par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
        }

        return par1ItemStack;
    }

    /**
     * Return the enchantability factor of the item, most of the time is based on material.
     */
    public int getItemEnchantability()
    {
    	return 1;
    }

    //Modified to allow custom texture use. maybe.
    @SideOnly(Side.CLIENT)
    public void registerIcons(IIconRegister par1IconRegister)
    {
    	//might change to (Ranger.MODID + ":" + this.t_s)
    	getBowPullIconNameArray(); //this solved the null texture!
        this.itemIcon = par1IconRegister.registerIcon(RangerMain.MODID + ":" + getUnlocalizedName().substring(5)); //.substring(5) removes "item."
        this.iconArray = new IIcon[bowPullIconNameArray.length];

        for (int i = 0; i < this.iconArray.length; ++i)
        {
            this.iconArray[i] = par1IconRegister.registerIcon(RangerMain.MODID + ":" + bowPullIconNameArray[i]);
        }
    }
    
    
    //Renders it in 3D, works, but not exactly like the regular bow. More like a sword.
    @SideOnly(Side.CLIENT)
    public boolean isFull3D()
    {
        return true;
    }

    /**
     * used to cycle through icons based on their used duration, i.e. for the bow
     */
    //Modified
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(ItemStack stack, int renderPass, EntityPlayer player, ItemStack usingItem, int useRemaining)
{
    	//Bottom to top
	if(player.getItemInUse() == null) return this.itemIcon;
	int Pulling = stack.getMaxItemUseDuration() - useRemaining;
	if (Pulling >= this.anim_2)
	{
		return iconArray[2];
	}
	else if (Pulling > this.anim_1)
	{
		return iconArray[1];
	}
	else if (Pulling > this.anim_0)
	{
		return iconArray[0];
	}              
	return itemIcon;
}
}

 

 

And the Crystal Orb.class:

 

package aceofkings.rangermod;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class RangerItemMagic extends Item {

public int m_type;
/**
 * @param maxStackSize is the max number items in a stack, ie 64
 * @param tab is the Creative tab ie, CreativeTabs.tabMisc
 * @param texture is the String for the item texture, ie "rangermod:item"
 * @param name is the String for the Unlocalized name, ie "itemName"
 * @param type is what kind of magic item, ie 0 for orb
 */
public RangerItemMagic(CreativeTabs tab, String texture, String name, int type){

	super();
	this.setMaxStackSize(1);
	this.setCreativeTab(tab);
	this.setUnlocalizedName(name);
	this.setTextureName(texture);
	this.setMaxDamage(320);
	this.isDamageable();
	}

}

 

 

Any help would be greatly appreciated! I've been stuck on this for a few days now.

Posted

Put this in your onPlayerStoppedUsing method to damage the itemstack.

 

ItemStack itemstack;
for (int index = 0; index < player.inventory.getSizeInventory() && (itemstack != null ? itemstack.getItem() == YourModClass.youritem) ; index++) { //You could use 35 instead of the size here but i like it that way.
    itemstack = player.inventory.getStackInSlot(index);
}
if (itemstack != null && itemstack.getItem() == YourModClass.youritem) {
    if (itemStack.attemptDamageItem(1, rand)) {
        itemStack.stackSize--;
        if (itemStack.stackSize < 0)                  //This will consume your item once it breaks.
            itemStack.stackSize = 0;
        itemStack.setItemDamage(0);
        itemStack = null;
    }
}

 

EDIT: You might need to return the Item to the players Inverntory

PM's regarding modding questions should belong in the Modder Support sub-forum and won't be answered.

Posted

Thanks! I'll try it out and see how it works.

 

Edit:

Okay, after modifying you solution to match my class

 

public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int par4)
    {
        int j = this.getMaxItemUseDuration(par1ItemStack) - par4;

        ArrowLooseEvent event = new ArrowLooseEvent(par3EntityPlayer, par1ItemStack, j);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return;
        }
        j = event.charge;
        
        boolean flag = par3EntityPlayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, par1ItemStack) > 0;
        
        if (flag || par3EntityPlayer.inventory.hasItem(RangerMain.crystal_orb))
        {

        	//Change 20.0F to increase/decrease charge speed.
            float f = (float)j / charge;
            f = (f * f + f * 2.0F) / 3.0F;

            if ((double)f < 0.1D)
            {
                return;
            }

            if (f > 1.0F)
            {
                f = 1.0F;
            }

            EntityArrow entityarrow = new EntityArrow(par2World, par3EntityPlayer, f * 2.0F);

            if (f == 1.0F)
            {
                entityarrow.setIsCritical(true);
            }
            
            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);

            if (k > 0)
            {
            	entityarrow.setDamage(entityarrow.getDamage() + (double)k * 0.5D + 0.5D);
            }

            int l = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, par1ItemStack);

            if (l > 0)
            {
                entityarrow.setKnockbackStrength(l);
            }

            if (EnchantmentHelper.getEnchantmentLevel(Enchantment.flame.effectId, par1ItemStack) > 0)
            {
                entityarrow.setFire(100);
            }
            
            par1ItemStack.damageItem(1, par3EntityPlayer);
            par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);

            if (flag)
            {
                entityarrow.canBePickedUp = 2;
            }
            else
            {
            	//New code
            	ItemStack itemstack;
            	for (int index = 0; index < par3EntityPlayer.inventory.getSizeInventory() && (itemstack != null ? itemstack.getItem() == RangerMain.crystal_orb : 35); index++) { //You could use 35 instead of the size here but i like it that way.
            	    itemstack = par3EntityPlayer.inventory.getStackInSlot(index);
            	}
            	
            	if (itemstack != null && itemstack.getItem() == RangerMain.crystal_orb) {
            	    if (itemstack.attemptDamageItem(1, itemRand)) {
            	        itemstack.stackSize--;
            	        if (itemstack.stackSize < 0)                  //This will consume your item once it breaks.
            	            itemstack.stackSize = 0;
            	        itemstack.setItemDamage(0);
            	        itemstack = null;
            	    }
            	} 
            	
            }

            if (!par2World.isRemote)
            {
                par2World.spawnEntityInWorld(entityarrow);
            }
        	
        }
    }

 

 

 

I get the following error:

 

"The operator && is undefined for the argument type(s) boolean, Object&Serializable&Comparable<?>"

 

This is what its underlining:

index < par3EntityPlayer.inventory.getSizeInventory() && (itemstack != null ? itemstack.getItem() == RangerMain.crystal_orb : 35);

 

I'm not 100% sure why either...

 

Posted

Whoops its meant to be: :D

index < par3EntityPlayer.inventory.getSizeInventory() && (itemstack != null ? itemstack.getItem() == RangerMain.crystal_orb : false);

PM's regarding modding questions should belong in the Modder Support sub-forum and won't be answered.

Posted

Thanks again, that seemed to fix all of the errors... But one.

 

After I fixed the code so it would stop "yelling" at me, it was complaining that the local variable itemstack might not have been initialized. I'm assuming putting "new ItemStack(mainmod.myitem)" or even "null" as Eclipse suggessts would break it.

 

Once again thank you for your assistance!

 

EDIT: "ItemStack itemstack = null;" didnt break it. I put a print line on the damage of the item, and it goes up each time i use it (yay!). The only obstacle to pass is displaying the damage on the hot-bar, similar to a pickaxe or bow. You mentioned something earlier about returning it to inventory?

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi,  I'm using Forge 47.3.0 for Minecraft 1.20.1 I apologise if this is obvious I am very new to modding for Minecraft. I sucessfully made a mod that launched without errors or crashes (without it doing anything) but in order to add the features I need, I need to add "Custom Portal API [Forge]" as a dependency. However no matter the way I've tried to acheive this, it crashes. I am pretty sure it's not the way I'm putting it in the repositories, the dependencies or the way I'm refrencing it, as I've a hundred diffrent combinations and multiple Maven methods. And on all those diffrent variations I still get this crash: pastebin.com/UhumzZCZ Any tips would be invaluable as I've been loosing my mind over this!
    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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