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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Version 1.19 - Forge 41.0.63 I want to create a wolf entity that I can ride, so far it seems to be working, but the problem is that when I get on the wolf, I can’t control it. I then discovered that the issue is that the server doesn’t detect that I’m riding the wolf, so I’m struggling with synchronization. However, it seems to not be working properly. As I understand it, the server receives the packet but doesn’t register it correctly. I’m a bit new to Java, and I’ll try to provide all the relevant code and prints *The comments and prints are translated by chatgpt since they were originally in Spanish* Thank you very much in advance No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. No player is mounted, or the passenger is not a player. MountableWolfEntity package com.vals.valscraft.entity; import com.vals.valscraft.network.MountSyncPacket; import com.vals.valscraft.network.NetworkHandler; import net.minecraft.client.Minecraft; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.Mob; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.animal.Wolf; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.Entity; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.event.TickEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.network.PacketDistributor; public class MountableWolfEntity extends Wolf { private boolean hasSaddle; private static final EntityDataAccessor<Byte> DATA_ID_FLAGS = SynchedEntityData.defineId(MountableWolfEntity.class, EntityDataSerializers.BYTE); public MountableWolfEntity(EntityType<? extends Wolf> type, Level level) { super(type, level); this.hasSaddle = false; } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_FLAGS, (byte)0); } public static AttributeSupplier.Builder createAttributes() { return Wolf.createAttributes() .add(Attributes.MAX_HEALTH, 20.0) .add(Attributes.MOVEMENT_SPEED, 0.3); } @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { ItemStack itemstack = player.getItemInHand(hand); if (itemstack.getItem() == Items.SADDLE && !this.hasSaddle()) { if (!player.isCreative()) { itemstack.shrink(1); } this.setSaddle(true); return InteractionResult.SUCCESS; } else if (!level.isClientSide && this.hasSaddle()) { player.startRiding(this); MountSyncPacket packet = new MountSyncPacket(true); // 'true' means the player is mounted NetworkHandler.CHANNEL.sendToServer(packet); // Ensure the server handles the packet return InteractionResult.SUCCESS; } return InteractionResult.PASS; } @Override public void travel(Vec3 travelVector) { if (this.isVehicle() && this.getControllingPassenger() instanceof Player) { System.out.println("The wolf has a passenger."); System.out.println("The passenger is a player."); Player player = (Player) this.getControllingPassenger(); // Ensure the player is the controller this.setYRot(player.getYRot()); this.yRotO = this.getYRot(); this.setXRot(player.getXRot() * 0.5F); this.setRot(this.getYRot(), this.getXRot()); this.yBodyRot = this.getYRot(); this.yHeadRot = this.yBodyRot; float forward = player.zza; float strafe = player.xxa; if (forward <= 0.0F) { forward *= 0.25F; } this.flyingSpeed = this.getSpeed() * 0.1F; this.setSpeed((float) this.getAttributeValue(Attributes.MOVEMENT_SPEED) * 1.5F); this.setDeltaMovement(new Vec3(strafe, travelVector.y, forward).scale(this.getSpeed())); this.calculateEntityAnimation(this, false); } else { // The wolf does not have a passenger or the passenger is not a player System.out.println("No player is mounted, or the passenger is not a player."); super.travel(travelVector); } } public boolean hasSaddle() { return this.hasSaddle; } public void setSaddle(boolean hasSaddle) { this.hasSaddle = hasSaddle; } @Override protected void dropEquipment() { super.dropEquipment(); if (this.hasSaddle()) { this.spawnAtLocation(Items.SADDLE); this.setSaddle(false); } } @SubscribeEvent public static void onServerTick(TickEvent.ServerTickEvent event) { if (event.phase == TickEvent.Phase.START) { MinecraftServer server = net.minecraftforge.server.ServerLifecycleHooks.getCurrentServer(); if (server != null) { for (ServerPlayer player : server.getPlayerList().getPlayers()) { if (player.isPassenger() && player.getVehicle() instanceof MountableWolfEntity) { MountableWolfEntity wolf = (MountableWolfEntity) player.getVehicle(); System.out.println("Tick: " + player.getName().getString() + " is correctly mounted on " + wolf); } } } } } private boolean lastMountedState = false; @Override public void tick() { super.tick(); if (!this.level.isClientSide) { // Only on the server boolean isMounted = this.isVehicle() && this.getControllingPassenger() instanceof Player; // Only print if the state changed if (isMounted != lastMountedState) { if (isMounted) { Player player = (Player) this.getControllingPassenger(); // Verify the passenger is a player System.out.println("Server: Player " + player.getName().getString() + " is now mounted."); } else { System.out.println("Server: The wolf no longer has a passenger."); } lastMountedState = isMounted; } } } @Override public void addPassenger(Entity passenger) { super.addPassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(true)); } } } @Override public void removePassenger(Entity passenger) { super.removePassenger(passenger); if (passenger instanceof Player) { Player player = (Player) passenger; if (!this.level.isClientSide && player instanceof ServerPlayer) { // Send the packet to the server to indicate the player is no longer mounted NetworkHandler.CHANNEL.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) player), new MountSyncPacket(false)); } } } @Override public boolean isControlledByLocalInstance() { Entity entity = this.getControllingPassenger(); return entity instanceof Player; } @Override public void positionRider(Entity passenger) { if (this.hasPassenger(passenger)) { double xOffset = Math.cos(Math.toRadians(this.getYRot() + 90)) * 0.4; double zOffset = Math.sin(Math.toRadians(this.getYRot() + 90)) * 0.4; passenger.setPos(this.getX() + xOffset, this.getY() + this.getPassengersRidingOffset() + passenger.getMyRidingOffset(), this.getZ() + zOffset); } } } MountSyncPacket package com.vals.valscraft.network; import com.vals.valscraft.entity.MountableWolfEntity; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class MountSyncPacket { private final boolean isMounted; public MountSyncPacket(boolean isMounted) { this.isMounted = isMounted; } public void encode(FriendlyByteBuf buffer) { buffer.writeBoolean(isMounted); } public static MountSyncPacket decode(FriendlyByteBuf buffer) { return new MountSyncPacket(buffer.readBoolean()); } public void handle(NetworkEvent.Context context) { context.enqueueWork(() -> { ServerPlayer player = context.getSender(); // Get the player from the context if (player != null) { // Verifies if the player has dismounted if (!isMounted) { Entity vehicle = player.getVehicle(); if (vehicle instanceof MountableWolfEntity wolf) { // Logic to remove the player as a passenger wolf.removePassenger(player); System.out.println("Server: Player " + player.getName().getString() + " is no longer mounted."); } } } }); context.setPacketHandled(true); // Marks the packet as handled } } networkHandler package com.vals.valscraft.network; import com.vals.valscraft.valscraft; import net.minecraft.resources.ResourceLocation; import net.minecraftforge.network.NetworkRegistry; import net.minecraftforge.network.simple.SimpleChannel; import net.minecraftforge.network.NetworkEvent; import java.util.function.Supplier; public class NetworkHandler { private static final String PROTOCOL_VERSION = "1"; public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel( new ResourceLocation(valscraft.MODID, "main"), () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals ); public static void init() { int packetId = 0; // Register the mount synchronization packet CHANNEL.registerMessage( packetId++, MountSyncPacket.class, MountSyncPacket::encode, MountSyncPacket::decode, (msg, context) -> msg.handle(context.get()) // Get the context with context.get() ); } }  
    • Do you use features of inventory profiles next (ipnext) or is there a change without it?
    • Remove rubidium - you are already using embeddium, which is a fork of rubidium
  • Topics

×
×
  • Create New...

Important Information

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