Jump to content

Recommended Posts

Posted

Hey guys I'm new here.

 

I am having some problems with my mod.

Here's the item code:

 

 

package melonslise.mod;

import java.util.List;
import java.util.Random;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

public class ItemSpell extends Item
{

protected Random rand;
private int ticks = 0;
private boolean cast = false;

public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{
	ticks = 0;
	cast = true;

        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }
        
        List<EntityLivingBase> entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 5, player.posY + 5, player.posZ + 5)); 

	for (EntityLivingBase entity : entities)
	{
		if (!(entity instanceof EntityTameable && ((EntityTameable)entity).getOwner() == player) && (entity != player))
		{
			entity.setFire(5);				
			entity.attackEntityFrom(DamageSource.onFire, 10F);
			entity.knockBack(entity, 0F, 1F, 0F);
		}
	}

	this.rand = new Random();

	if(ticks == 0)
	{
		for (int a = 0; a < 50; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), 0.0D, -0.7D + rand.nextGaussian() * 0.02, 0.0D);
		}
	}

	if(ticks == 10)
	{
		for (int b = 0; b < 50; ++b)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), 0.0D, -0.7D + rand.nextGaussian() * 0.02, 0.0D);
		}
	}

	if(ticks == 20)
	{
		for (int c = 0; c < 50; ++c)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), 0.0D, -0.7D + rand.nextGaussian() * 0.02, 0.0D);
		}
	}

	if(ticks == 30)
	{
		for (int b = 0; b < 50; ++b)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), 0.0D, -0.7D + rand.nextGaussian() * 0.02, 0.0D);
		}

	}

	if(ticks == 40)
	{
		for (int d = 0; d < 50; ++d)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), 0.0D, -0.7D + rand.nextGaussian() * 0.02, 0.0D);
		}

		cast = false;
	}

	return itemStack;

}

public void onUpdate()
{
    if(cast == true)
    {
    	++ticks;
    }
}

}

 

 

 

It seems to be spawning only one patch (or whatever) of particles. What did I miss?

Posted

Before you even try to fix logic, you should fix design.

 

Item is a SINGLETON - it cannot hold data (fields).

 

Item - description of what you are holding.

ItemStack - thing held.

 

You need to save data into ItemStack's NBT (or @Capability for 1.8.9+).

1.7.10 is no longer supported by forge, you are on your own.

Posted

Almost every method in Item class has ItemStack param. Those are the ItemStacks that fired Item's method. From ItemStack you can pull nbtTagCompound (there are getters/setters depending on version). Rest (and examples) can be found in NBT classes and other vanilla sources.

 

Also: Update to 1.9+, 1.7.10 is ANCIENT (it's more than 2 years old and developing anything for it is a waste of time, eveything is different on 1.9).

1.7.10 is no longer supported by forge, you are on your own.

Posted

Almost every method in Item class has ItemStack param. Those are the ItemStacks that fired Item's method. From ItemStack you can pull nbtTagCompound (there are getters/setters depending on version). Rest (and examples) can be found in NBT classes and other vanilla sources.

 

Also: Update to 1.9+, 1.7.10 is ANCIENT (it's more than 2 years old and developing anything for it is a waste of time, eveything is different on 1.9).

 

Alright, so I did this:

 

 

 

package melonslise.mod;

import java.util.List;
import java.util.Random;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

public class ItemSpell extends Item
{

protected Random rand;

public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{

    NBTTagCompound nbt;
    if (itemStack.hasTagCompound())
    {
        nbt = itemStack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }
    
    nbt.setBoolean("cast", true);
        
    nbt.setInteger("ticks", 0);

    
    
        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }
        
    itemStack.setTagCompound(nbt);
    
    
    
        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }
        
        List<EntityLivingBase> entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 5, player.posY + 5, player.posZ + 5)); 

	for (EntityLivingBase entity : entities)
	{
		if (!(entity instanceof EntityTameable && ((EntityTameable)entity).getOwner() == player) && (entity != player))
		{
			entity.setFire(5);				
			entity.attackEntityFrom(DamageSource.onFire, 10F);
			entity.knockBack(entity, 0F, 1F, 0F);
		}
	}

	this.rand = new Random();

	if(nbt.getInteger("ticks") == 0)
	{
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}
	}

	if(nbt.getInteger("ticks") == 10)
	{
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}
	}

	if(nbt.getInteger("ticks") == 20)
	{
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}
	}

	if(nbt.getInteger("ticks") == 30)
	{
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}
	}

	if(nbt.getInteger("ticks") == 40)
	{
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}
	}

	if(nbt.getInteger("ticks") == 50)
	{
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}
	}

	if(nbt.getInteger("ticks") == 60)
	{
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}

		nbt.setBoolean("cast", false);
	}

	return itemStack;

}

public void onUpdate(ItemStack itemStack)
{
    NBTTagCompound nbt;
    if (itemStack.hasTagCompound())
    {
        nbt = itemStack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }

	if ((nbt.getBoolean("cast") == true)&&(nbt.getInteger("ticks") <= 60))
	{
		nbt.setInteger("ticks", nbt.getInteger("ticks") + 1);
	}
}

}

 

 

 

And still no luck :/

 

Are you sure I should do it with nbts? Maybe there's another way like creating another class or something

Posted

You didn't put the written NBT into the itemstack tag when it was previously absent.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

What that means is you did not call #setTagCompound(newTag) when you created a new tag, so if the ItemStack didn't have a tag to begin with, it doesn't after your method, either.

 

Typical implementation to ensure non-null tag:

if (!stack.hasTagCompound()) {
stack.setTagCompound(new NBTTagCompound());
}
NBTTagCompound tag = stack.getTagCompound();

Posted

1. What he said.

 

Note: Overally - everything you do is wrong, but that later, 1st let's fix code itself (not logic).

 

2. Do you even for loops? Do you even math operators (like " % ")?

 

3. Whole things is a total mess.

 

4. Use damn @Override - you've overriden wrong methods (I don't think #onUpdate(ItemStack) exists, it's 1.7, so I don't have clue, so old...).

 

Fix your messed up code, use proper java conventions and operators. Then to the next point.

1.7.10 is no longer supported by forge, you are on your own.

Posted

1. What he said.

 

Note: Overally - everything you do is wrong, but that later, 1st let's fix code itself (not logic).

 

2. Do you even for loops? Do you even math operators (like " % ")?

 

3. Whole things is a total mess.

 

4. Use damn @Override - you've overriden wrong methods (I don't think #onUpdate(ItemStack) exists, it's 1.7, so I don't have clue, so old...).

 

Fix your messed up code, use proper java conventions and operators. Then to the next point.

 

 

 

2. I know what for loops are and I DO know what modulus is, but why?

 

3. How is it a total mess and how would that effect the way the code works. First you could explain rather than just stating that.

 

4. Override is only used the compiler to check mistakes. Why would I need that?

 

What that means is you did not call #setTagCompound(newTag) when you created a new tag, so if the ItemStack didn't have a tag to begin with, it doesn't after your method, either.

 

Typical implementation to ensure non-null tag:

if (!stack.hasTagCompound()) {
stack.setTagCompound(new NBTTagCompound());
}
NBTTagCompound tag = stack.getTagCompound();

 

Isn't this

if (!stack.hasTagCompound()) {
stack.setTagCompound(new NBTTagCompound());
}
NBTTagCompound tag = stack.getTagCompound();

 

the same as this? :

    else
    {
        nbt = new NBTTagCompound();
    }

itemStack.setTagCompound(nbt);

Posted

(Sorry for my blindness not catching the last line)

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted

Should I even be doing this through NBT? Maybe I should make some other handler class or something of the sort? Because I think that doing this through NBT maybe not be very stable (eg. someone uses the item and immediately throws it away then will the rest of particles work? or some other similar scenario)

Posted

2. I know what for loops are and I DO know what modulus is, but why?

3. How is it a total mess and how would that effect the way the code works. First you could explain rather than just stating that.

4. Override is only used the compiler to check mistakes. Why would I need that?

 

2. Because you can wrap all of your "if (tick == 10)" to "if (tick <= 60 && tick % 10 == 0)".

- Generally - if code appears more than once, it should be in loop or private method (with exceptions, obviously).

3. Precisely because above and the fact that you are doing stuff like this:

NBTTagCompound nbt;
    if (itemStack.hasTagCompound())
    {
        nbt = itemStack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }
    
    nbt.setBoolean("cast", true);
        
    nbt.setInteger("ticks", 0);

    
    
        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }
        
    itemStack.setTagCompound(nbt);
    
    
    
        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }

 

* Notice you are doing "--itemStack.stackSize;" twice.

* How about removing double and triple blank lines?

* The way you operate on NBT is NOT == to the one coolAlias proposed and yes - yours is worse (Mainly because it calls "itemStack.setTagCompound(nbt);" no matter what and is longer).

* You are using "--itemStack.stackSize;" without checking if stack size <= 0. Whenever stack size is reduced to 0 you need to take care of removing it from inventory. In this method's case you need to return null (At least I think so, you are seriously outdated).

 

4. Yeah, and also by people. I will just point out why you should use it:

* Because Java conventions.

* Because it's more readable, following:

* Because without superclass I/We can't tell if this method is ACTUALLY overriding (has super) something or not.

- Why can't we just look into code ouselves? Well - as said, 1.7 is super outdated, for one the method Item#onUpdate in 1.9 looks like this:

    /**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
    public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
    {
    }

So yeah...

 

My 1st thought was that you are not actually overriding super method when I looked at it (your code), and I am still not quite sure if you do.

 

Aside from all that:

Should I even be doing this through NBT? Maybe I should make some other handler class or something of the sort? Because I think that doing this through NBT maybe not be very stable (eg. someone uses the item and immediately throws it away then will the rest of particles work? or some other similar scenario)

 

Yes. Now the real problem appears in design part. I would alredy tell you step by step how-to, but since you are outdated I need to ask - does #onUpdate have ANY params (if not, you will have to use client evensts to spawn particles on per-tick basis)?

 

1.7.10 is no longer supported by forge, you are on your own.

Posted

4. Override is only used the compiler to check mistakes. Why would I need that?

Um, you may as well say "I want my car to go places, so why would I need brakes?"

 

Coding an exact match to achieve an override is tricky enough that it's one of the most common sources of error brought to this forum. Also, because a flawed override may leave a parental method running in place of what you expect, the runtime effects can be unpredictable and subtle, making them difficult to diagnose, especially at a distance when we try to help.

 

Finally, and perhaps most important, Minecraft's code does shift with each new version. When you port a mod to a new MC version, there will often be methods in your extended classes that were correctly overriding their parents in the prior version but are not proper matches in the new version. Believe me when I say that it is a blessing to have the Eclipse IDE call them all out in bright red rather than having to diagnose a whole bunch of subtle misbehavior at runtime.

 

Therefore, always always always put the @Override annotation on absolutely every method that you as designer intend as an override. Do not ever skimp on any of them, not even one, not ever.

 

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Posted

2. I know what for loops are and I DO know what modulus is, but why?

3. How is it a total mess and how would that effect the way the code works. First you could explain rather than just stating that.

4. Override is only used the compiler to check mistakes. Why would I need that?

 

2. Because you can wrap all of your "if (tick == 10)" to "if (tick <= 60 && tick % 10 == 0)".

- Generally - if code appears more than once, it should be in loop or private method (with exceptions, obviously).

3. Precisely because above and the fact that you are doing stuff like this:

NBTTagCompound nbt;
    if (itemStack.hasTagCompound())
    {
        nbt = itemStack.getTagCompound();
    }
    else
    {
        nbt = new NBTTagCompound();
    }
    
    nbt.setBoolean("cast", true);
        
    nbt.setInteger("ticks", 0);

    
    
        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }
        
    itemStack.setTagCompound(nbt);
    
    
    
        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }

 

* Notice you are doing "--itemStack.stackSize;" twice.

* How about removing double and triple blank lines?

* The way you operate on NBT is NOT == to the one coolAlias proposed and yes - yours is worse (Mainly because it calls "itemStack.setTagCompound(nbt);" no matter what and is longer).

* You are using "--itemStack.stackSize;" without checking if stack size <= 0. Whenever stack size is reduced to 0 you need to take care of removing it from inventory. In this method's case you need to return null (At least I think so, you are seriously outdated).

 

4. Yeah, and also by people. I will just point out why you should use it:

* Because Java conventions.

* Because it's more readable, following:

* Because without superclass I/We can't tell if this method is ACTUALLY overriding (has super) something or not.

- Why can't we just look into code ouselves? Well - as said, 1.7 is super outdated, for one the method Item#onUpdate in 1.9 looks like this:

    /**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
    public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
    {
    }

So yeah...

 

My 1st thought was that you are not actually overriding super method when I looked at it (your code), and I am still not quite sure if you do.

 

Aside from all that:

Should I even be doing this through NBT? Maybe I should make some other handler class or something of the sort? Because I think that doing this through NBT maybe not be very stable (eg. someone uses the item and immediately throws it away then will the rest of particles work? or some other similar scenario)

 

Yes. Now the real problem appears in design part. I would alredy tell you step by step how-to, but since you are outdated I need to ask - does #onUpdate have ANY params (if not, you will have to use client evensts to spawn particles on per-tick basis)?

 

Why would I want to swap == with %?

 

* --itemStack.stackSize; twice was a copy and paste accident.

* Blank lines do not effect the code in any way.

* Checking for the size of the stack is not necessary (At least in 1.7.10). Take a look at the itemSnowball, I copied the code from there.

 

 

As for onUpdate:

    /**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
    public void onUpdate(ItemStack p_77663_1_, World p_77663_2_, Entity p_77663_3_, int p_77663_4_, boolean p_77663_5_) {}

 

4. Override is only used the compiler to check mistakes. Why would I need that?

Um, you may as well say "I want my car to go places, so why would I need brakes?"

 

Coding an exact match to achieve an override is tricky enough that it's one of the most common sources of error brought to this forum. Also, because a flawed override may leave a parental method running in place of what you expect, the runtime effects can be unpredictable and subtle, making them difficult to diagnose, especially at a distance when we try to help.

 

Finally, and perhaps most important, Minecraft's code does shift with each new version. When you port a mod to a new MC version, there will often be methods in your extended classes that were correctly overriding their parents in the prior version but are not proper matches in the new version. Believe me when I say that it is a blessing to have the Eclipse IDE call them all out in bright red rather than having to diagnose a whole bunch of subtle misbehavior at runtime.

 

Therefore, always always always put the @Override annotation on absolutely every method that you as designer intend as an override. Do not ever skimp on any of them, not even one, not ever.

 

Alright

Posted

I am starting to feel like you don't want our help...

 

@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand)
{
	if (!stack.hasTagCompound())
	{
		stack.setTagCompound(new NBTTagCompound());
	}
	NBTTagCompound nbt = stack.getTagCompound();

	List<EntityLivingBase> entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, new AxisAlignedBB(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 5, player.posY + 5, player.posZ + 5));

	for (EntityLivingBase entity : entities)
	{
		if (!(entity instanceof EntityTameable && ((EntityTameable) entity).getOwner() == player) && entity != player)
		{
			entity.attackEntityFrom(DamageSource.onFire, 10F);
			entity.knockBack(entity, 0F, 1F, 0F);
			entity.setFire(5);

			nbt.setInteger("C", 61);
		}
	}

	if (!player.capabilities.isCreativeMode)
	{
		--stack.stackSize;
	}

	return new ActionResult(EnumActionResult.PASS, stack);
}

@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
{
	if (!stack.hasTagCompound())
	{
		stack.setTagCompound(new NBTTagCompound());
	}
	NBTTagCompound nbt = stack.getTagCompound();

	int casting = nbt.getInteger("C");

	if (casting > 0)
	{
		if (casting % 10 == 1)
		{
			for (int i = 0; i < 60; ++i)
			{
				entityIn.worldObj.spawnParticle(EnumParticleTypes.FLAME, entityIn.posX + (worldIn.rand.nextInt(10) - 5), entityIn.posY + 4.0D + worldIn.rand.nextInt(6) * 0.5, entityIn.posZ + (worldIn.rand.nextInt(10) - 5), (worldIn.rand.nextInt(10) - 5) * 0.02, -0.8D + worldIn.rand.nextGaussian() * 0.02, (worldIn.rand.nextInt(10) - 5) * 0.02);
			}
		}

		nbt.setInteger("C", --casting);
	}
}

 

Something like this. Idk, wrote it in notepad ;p

 

EDIT

 

Totally forgot - you need to wrap (pretty much everything) in !world.isRemote.

Only thing outside (in world.isRemote) should be spawning particles.

This is pretty much version-dependent because some stuff changed there.

1.7.10 is no longer supported by forge, you are on your own.

Posted

4. Override is only used the compiler to check mistakes. Why would I need that?

 

width=500 height=604http://i1.wp.com/www.sycmu.com/wp-content/uploads/2013/03/549794_516635841711565_2102887642_n.jpg[/img]

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I am starting to feel like you don't want our help...

 

@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand)
{
	if (!stack.hasTagCompound())
	{
		stack.setTagCompound(new NBTTagCompound());
	}
	NBTTagCompound nbt = stack.getTagCompound();

	List<EntityLivingBase> entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, new AxisAlignedBB(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 5, player.posY + 5, player.posZ + 5));

	for (EntityLivingBase entity : entities)
	{
		if (!(entity instanceof EntityTameable && ((EntityTameable) entity).getOwner() == player) && entity != player)
		{
			entity.attackEntityFrom(DamageSource.onFire, 10F);
			entity.knockBack(entity, 0F, 1F, 0F);
			entity.setFire(5);

			nbt.setInteger("C", 61);
		}
	}

	if (!player.capabilities.isCreativeMode)
	{
		--stack.stackSize;
	}

	return new ActionResult(EnumActionResult.PASS, stack);
}

@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
{
	if (!stack.hasTagCompound())
	{
		stack.setTagCompound(new NBTTagCompound());
	}
	NBTTagCompound nbt = stack.getTagCompound();

	int casting = nbt.getInteger("C");

	if (casting > 0)
	{
		if (casting % 10 == 1)
		{
			for (int i = 0; i < 60; ++i)
			{
				entityIn.worldObj.spawnParticle(EnumParticleTypes.FLAME, entityIn.posX + (worldIn.rand.nextInt(10) - 5), entityIn.posY + 4.0D + worldIn.rand.nextInt(6) * 0.5, entityIn.posZ + (worldIn.rand.nextInt(10) - 5), (worldIn.rand.nextInt(10) - 5) * 0.02, -0.8D + worldIn.rand.nextGaussian() * 0.02, (worldIn.rand.nextInt(10) - 5) * 0.02);
			}
		}

		nbt.setInteger("C", --casting);
	}
}

 

Something like this. Idk, wrote it in notepad ;p

 

EDIT

 

Totally forgot - you need to wrap (pretty much everything) in !world.isRemote.

Only thing outside (in world.isRemote) should be spawning particles.

This is pretty much version-dependent because some stuff changed there.

 

What do you mean I don't need your help? Of course I do.

 

I will try this, but wouldn't NBT cause problems (as stated above, if a player uses the item and immediately throws it away then the whole particle spawning thing would be screwed over)?

 

EDIT: Your code works (except for those enum thingies which I had to remove)! thanks a lot. You will be N1 in my credits list. Cheers

 

But still wouldn't NBT cause problems as stated above? This solution is also good though.

 

Here's the code if someone is interested:

 

 

import java.util.List;
import java.util.Random;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.passive.EntityTameable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

public class ItemSpell extends Item
{

public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
{   

	if (!itemStack.hasTagCompound())
	{
		itemStack.setTagCompound(new NBTTagCompound());
	}

	NBTTagCompound nbt = itemStack.getTagCompound();
      


        if (!player.capabilities.isCreativeMode)
        {
            --itemStack.stackSize;
        }
        
        List<EntityLivingBase> entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 5, player.posY + 5, player.posZ + 5)); 

	for (EntityLivingBase entity : entities)
	{
		if (!(entity instanceof EntityTameable && ((EntityTameable)entity).getOwner() == player) && (entity != player))
		{
			entity.setFire(5);				
			entity.attackEntityFrom(DamageSource.onFire, 10F);
			entity.knockBack(entity, 0F, 1F, 0F);

			nbt.setInteger("ticks", 60);
		}
	}

	/**
		for (int a = 0; a < 60; ++a)
		{
			player.worldObj.spawnParticle("flame", player.posX + (rand.nextInt(10) - 5), player.posY + 4.0D + rand.nextInt(6) * 0.5, player.posZ + (rand.nextInt(10) - 5), (rand.nextInt(10) - 5) * 0.02, -0.8D + rand.nextGaussian() * 0.02, (rand.nextInt(10) - 5) * 0.02);
		}
	**/

	return itemStack;
}

public void onUpdate(ItemStack itemStack, World world, Entity entity, int itemSlot, boolean isSelected)
{
	if (!itemStack.hasTagCompound())
	{
		itemStack.setTagCompound(new NBTTagCompound());
	}

	NBTTagCompound nbt = itemStack.getTagCompound();

	int ticks = nbt.getInteger("ticks");

	if (ticks > 0)
	{
		if (ticks % 10 == 1)
		{
			for (int i = 0; i < 60; ++i)
			{
				entity.worldObj.spawnParticle("flame", entity.posX + (world.rand.nextInt(10) - 5), entity.posY + 4.0D + world.rand.nextInt(6) * 0.5, entity.posZ + (world.rand.nextInt(10) - 5), (world.rand.nextInt(10) - 5) * 0.02, -0.8D + world.rand.nextGaussian() * 0.02, (world.rand.nextInt(10) - 5) * 0.02);
			}
		}

		nbt.setInteger("ticks", --ticks);
	}
}
}

 

 

 

Don't forget to wrap it in !(world.isRemote)

Posted

I am starting to feel like you don't want our help...

 

@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand)
{
	if (!stack.hasTagCompound())
	{
		stack.setTagCompound(new NBTTagCompound());
	}
	NBTTagCompound nbt = stack.getTagCompound();

	List<EntityLivingBase> entities = player.worldObj.getEntitiesWithinAABB(EntityLivingBase.class, new AxisAlignedBB(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 5, player.posY + 5, player.posZ + 5));

	for (EntityLivingBase entity : entities)
	{
		if (!(entity instanceof EntityTameable && ((EntityTameable) entity).getOwner() == player) && entity != player)
		{
			entity.attackEntityFrom(DamageSource.onFire, 10F);
			entity.knockBack(entity, 0F, 1F, 0F);
			entity.setFire(5);

			nbt.setInteger("C", 61);
		}
	}

	if (!player.capabilities.isCreativeMode)
	{
		--stack.stackSize;
	}

	return new ActionResult(EnumActionResult.PASS, stack);
}

@Override
public void onUpdate(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected)
{
	if (!stack.hasTagCompound())
	{
		stack.setTagCompound(new NBTTagCompound());
	}
	NBTTagCompound nbt = stack.getTagCompound();

	int casting = nbt.getInteger("C");

	if (casting > 0)
	{
		if (casting % 10 == 1)
		{
			for (int i = 0; i < 60; ++i)
			{
				entityIn.worldObj.spawnParticle(EnumParticleTypes.FLAME, entityIn.posX + (worldIn.rand.nextInt(10) - 5), entityIn.posY + 4.0D + worldIn.rand.nextInt(6) * 0.5, entityIn.posZ + (worldIn.rand.nextInt(10) - 5), (worldIn.rand.nextInt(10) - 5) * 0.02, -0.8D + worldIn.rand.nextGaussian() * 0.02, (worldIn.rand.nextInt(10) - 5) * 0.02);
			}
		}

		nbt.setInteger("C", --casting);
	}
}

 

Something like this. Idk, wrote it in notepad ;p

 

EDIT

 

Totally forgot - you need to wrap (pretty much everything) in !world.isRemote.

Only thing outside (in world.isRemote) should be spawning particles.

This is pretty much version-dependent because some stuff changed there.

 

Just one last question. When I only have one of those items and I use it, the particles don't spawn. I understand why, but have 0 ideas on how to fix that. Would be great if there was a solution.

Posted

I recon you mean "one item" as in stack size == 1.

In that case you should simply debug your code and check if particle spawning if reached and stuff.

1.7.10 is no longer supported by forge, you are on your own.

Posted

Yeah, sometimes behavioral (non-crash) problems are best investigated by running in the debugger and stepping through suspect sections of code (and stepping into method calls).

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.