Jump to content

Recommended Posts

Posted

My ZeusCannon mod is  confusing me and I don't know java all that well. I would like to know if I should make a separate class file for each weapon. Or should I keep it all in the main file. But no matter what I do, I can't get the code for shooting a gun without errors. Too many errors and confusion. I have looked at the Blaster Rifle tutorial and I want to put the code from that tutorial in my mod but changing names and such. Is there any way to do that or do I have to make separate class files for each weapon(which I do not want to do if possible). The main class contains blocks, items, recipes, creative tab, and smelting. If you need the code, just ask. All help is appreciated. Thanks in advance,

 

ZapterX

Posted

You should make a new class file for every item/block that behaves differently than the ones you already have. Otherwise you just need to change the parameters in the constructor.

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

Posted

I'm working on something similar (instead of guns I'm adding a new bow or two). I pretty much copied the vanilla bow class into one of my own that I created (NewBow.class). I recommend creating a new class for your Cannon, rather than putting it in the MainMod.class.

 

As far as i know, that is mostly for declaring/creating new items/blocks along with recipes.

 

This might help:

 

package yourname.yourpackage;

import java.util.List;

import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
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.ItemBow;
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;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class YourCannon extends Item {

//custom charge time
public float charge;

//custom animation adjustments
public int anim_0;
public int anim_1;
public int anim_2;  
    /**
 * @param texture is String of item texture, ie itemName.png
 * 		also sets the UnlocalizedName to avoid render issues
 * @param texture_0 is String of item animation texture 0, ie itemName_0.png
 * @param texture_1 is String of item animation texture 1, ie itemName_1.png
 * @param texture_2 is String of item animation texture 2, ie itemName_2.png
 * @param chargeTime is Float of how fast the bow charges, ie 21.0F where 20.0F is
 * the default bow speed
 * @param anim_0 is used for syncing charge time with pull_0 animation,
 *  recommended left at 0 
 * @param anim_1 is used for syncing charge time with pull_1 animation, ie 9 where
 * 8 is default bow
 * @param anim_2 is used for syncing charge time with pull_2 animation, ie 17 where
 * 16 is default bow
 * @param bowType used for determining MaxDamage, ie 3, where 0 is Stone, 1 is Iron,
 * 2 is Gold, 3 is Diamond
 * 
 * Notes: adjust anim_0-2 whenever chargeTime is changed for smoother animation flow
 */
    public RangerBow(float chargeTime, int anim_0, int anim_1, int anim_2, int type)
    {
    	//You can't stack more than one cannon.
        this.maxStackSize = 1;
        //Creative tab
        setCreativeTab(CreativeTabs.tabCombat);//Change this to change what creative tab its in
        
        //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;
        
       
        //Sets max damage based on type.
        if(type == 0){ setMaxDamage(220);
        }
        else if (type== 1){ setMaxDamage(466);  
        }
        
	else if (type== 2){ setMaxDamage(138);	
	}
        
	else if (type == 3){ setMaxDamage(527);
	}
        
	else if (type== 4){ 
		setMaxDamage(637);
        }
        
        //Default is the same as regular bow.
	else{ setMaxDamage(384);
	}  
    }
    
    public String[] bowPullIconNameArray;
    @SideOnly(Side.CLIENT)
    private IIcon[] iconArray;
    
    //Fixes "FileNotFound...null.png"
    //allows custom texture
    public void getBowPullIconNameArray(){

    	bowPullIconNameArray = new String[] {"cannon_0", "cannon_1", "cannon_2" };
    	
    }
    
    
    //
    public boolean hasEffect(ItemStack par1ItemStack){
            return false; //true if you want your item to have the enchanted look to it (shimmer)
    }
    
//allows you to add infomation on mouseover.
    public void addInformation(ItemStack par1ItemStack, EntityPlayer entityplayer, List list, boolean is){
        list.add("your info here")
    }

    /**
     * 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(Items.arrow))
        {
        	//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 you create your own entity, replace the arrow with it.

            if (f == 1.0F)
            {
                entityarrow.setIsCritical(true);
            }
            
            //modified arrow damages based on type
            //you dont need these either, unless you want
            if(type == 0){
        		entityarrow.setDamage(entityarrow.getDamage() + 3.0D);

        	}
            
        	if(type == 1){
        		entityarrow.setDamage(entityarrow.getDamage() + 4.0D);

        	}
        	
        	if(type == 2){
        		entityarrow.setDamage(entityarrow.getDamage() + 2.0D);

        	}
        	
        	if(type == 3){
        		entityarrow.setDamage(entityarrow.getDamage() + 5.0D);

        	}
        	
        	if(b_type == 4){
        		entityarrow.setDamage(entityarrow.getDamage() + 7.0D);

        	}
        	//Overpowered item is Overpowered.
        	else if(type  == -1){
        		entityarrow.setDamage(entityarrow.getDamage() + 18.0D);

        	}

            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, par1ItemStack);

            if (k > 0)
            {
            	//Ideally, this should take into consideration the type,
            	//depending on the type, add some damage along with the Power buff.
            	if(type == 0){
            		entityarrow.setDamage(entityarrow.getDamage() + 3.0D + (double)k * 0.5D + 0.5D);
            	}
            	else if(type == 1){
            		entityarrow.setDamage(entityarrow.getDamage() + 4.0D + (double)k * 0.5D + 0.5D);
            	}
            	else if(type == 2){
            		entityarrow.setDamage(entityarrow.getDamage() + 2.0D + (double)k * 0.5D + 0.5D);
            	}
            	else if(type == 3){
            		entityarrow.setDamage(entityarrow.getDamage() + 5.0D + (double)k * 0.5D + 0.5D);
            	}
            	else if(type == 4){
            		entityarrow.setDamage(entityarrow.getDamage() + 7.0D + (double)k * 0.5D + 0.5D);
            	}
            	//Overpowered item is Overpowered.
            	else if(type == -1){
            		entityarrow.setDamage(entityarrow.getDamage() + 18.0D + (double)k * 0.5D + 0.5D);
            	}
            	//if the bow type is not stone, iron, gold, or diamond, just do the standard enchantment damage.
            	else{
            		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
            {
                par3EntityPlayer.inventory.consumeInventoryItem(Items.arrow); //change this if your cannon consumes something 
//else, like fire charges, for example.
            }

            if (!par2World.isRemote)
            {
                par2World.spawnEntityInWorld(entityarrow); //if you create your own entity, replace the arrow with it.
            }
        }
    }

    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)
    {
    	
        ArrowNockEvent event = new ArrowNockEvent(par3EntityPlayer, par1ItemStack);
        MinecraftForge.EVENT_BUS.post(event);
        if (event.isCanceled())
        {
            return event.result;
        }

        if (par3EntityPlayer.capabilities.isCreativeMode || par3EntityPlayer.inventory.hasItem(Items.arrow))
        {
            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.
    @SideOnly(Side.CLIENT)
    public void registerIcons(IIconRegister par1IconRegister)
    {
    	//might change to (Ranger.MODID + ":" + this.t_s)
    	getBowPullIconNameArray(this.b_type); //this solved the null texture!
        this.itemIcon = par1IconRegister.registerIcon(yourmod.yourmodID + ":" + 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(yourmod.yourmodID + ":" + 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)
{
    	//the game uses 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;
}
}

 

 

This class is essentially the vanilla ItemBow.class, but you can modify it to fit your needs. With this example, the time it takes for the "bow" (or whatever) to charge (pull) can be changed, and the animations adjusted accordingly. I made something like this so that I could have multiple bows in one class, rather than 1 class per bow.

 

I hope this makes sense.

 

EDIT: I guess the code demonstrates what Busti was saying.

Posted

Thanks guys, that is very useful. But I also have another question. How would I link items to class files for crafting and such. For example, when I made a new file for an item and created the item and textures and registered everything, it wouldn't recognize the creative tab that I made in a different class(everything explained was in the same package). Do I have to recreate the tab for each one?

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.