Jump to content

Recommended Posts

Posted

Hello, 

I have a tile entity that produces explosions around an area every so often. However, I want to spawn in particles, wait until those particles decay (20ish ticks), then create the explosion. I already have this working, however, I'm not too sure if I should be scheduling tasks, or if there is an another, better way to do this.

Here is my current code:

    public void explosionFX(int x, int y, int z)
    {
        ((WorldServer) world).spawnParticle(EnumParticleTypes.ENCHANTMENT_TABLE, true, x, y, z, 100, 0, 0, 0, 3D);

        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                world.createExplosion(null, x, y, z, 100, false);
            }
        }, 1000); //1 second
    }

I could not base this event off tile entity ticks, as this function is already run every 100 ticks.

Thanks.

Posted
20 minutes ago, unassigned said:

I could not base this event off tile entity ticks

Why? Using TE's update is the preferred way of doing this.

 

21 minutes ago, unassigned said:

Timer

Since you are using timer your code will crash the game at some point without any warning with a ConcurrentModificationException, because Timer utilizes a different thread to run the passed runnable afaik. And the game isn't thread safe, especially when it comes to the world.

Posted (edited)
12 minutes ago, V0idWa1k3r said:

Why? Using TE's update is the preferred way of doing this.

  

Since you are using timer your code will crash the game at some point without any warning with a ConcurrentModificationException, because Timer utilizes a different thread to run the passed runnable afaik. And the game isn't thread safe, especially when it comes to the world.

Well, for some reason, I was trying to execute both these tasks at the same rate (5 seconds), when in reality I could just separate the if statements, and make the explosion go off after 6 seconds.

However, (maybe this is just my poor understanding of what's actually happening when modulating my TE's ticks) I get a weird phenomenon in which the explosion seems to sync up during the first few calls, but then they become gradually more unsynced for a few more calls, then returns back to being synced again. Here is what I have:

        if(ticksAlive % 100 == 0) //every 5 seconds
        {
            randX = this.pos.getX()+(world.rand.nextInt(8 + 1 + 8) - 8);
            randZ = this.pos.getZ()+(world.rand.nextInt(8 + 1 + 8) - 8);
            groundY = (world.getHeight(randX, randZ));
            strength = world.rand.nextInt(5 + 1 - 2) + 2;

            ((WorldServer) world).spawnParticle(EnumParticleTypes.ENCHANTMENT_TABLE, true, randX, groundY, randZ, 1000, 0, 0, 0, 3D);
        }
        if(ticksAlive % 120 == 0) //every 6 seconds
        {
            world.newExplosion(null, randX, groundY, randZ, strength , false, true);
        }

  

Edited by unassigned
grammar
Posted
4 minutes ago, unassigned said:

I get a weird phenomenon in which the explosion seems to sync up during the first few calls, but then they become gradually more unsynced for a few more calls, then returns back to being synced again.

Well, think about it. You have 2 events, one happening every 5 seconds the other every 6 seconds. Let's chart the timestamps of the events:

5, 10, 15, 20, 25, 30, 35, 40, ...

6, 12, 18, 24, 30, 36, 42, 48, ...

Note how there are timestamps where both events will occure(30 is one, 60 would be the other one, etc).

Posted
2 minutes ago, V0idWa1k3r said:

Well, think about it. You have 2 events, one happening every 5 seconds the other every 6 seconds. Let's chart the timestamps of the events:

5, 10, 15, 20, 25, 30, 35, 40, ...

 6, 12, 18, 24, 30, 36, 42, 48, ...

Note how there are timestamps where both events will occure(30 is one, 60 would be the other one, etc).

Oh, I see. So I'd just have to create another tick counter and reset that once task 2 is complete? Or is there a better way to do this?

Posted
1 minute ago, V0idWa1k3r said:

What do you want to happen? What behaviour do you want? Having 2 counters will not change anything.

What I'm looking for is this:

> Spawn in the particle, (its currently an enchantment particle) let it move into its final target position, then decay

> after the particle decay (which is about 1.5 seconds) create the explosion at that target position

I have everything but the timing down.

Posted

Well, there are two options I see.

The first one would be to split the process - have a separate Entity that behaves like your particle and renders like one(but is an Entity, not a Particle). Since you have full control over that entity you know when it decays and when it does - spawn your explosion from the entity's class.

The second one is to create a task system. Basically have a task class that contains a counter and the data needed to spawn the explosion - position, strength etc. Then when you spawn the particle add a new task to a collection. Every tick iterate the collection and increment/decrement/compare the counter. When the condition is satisfied spawn the explosion and remove the task from the collection. Something like this:

class ExplosionTask implements INBTSerializable<NBTTagCompound>
{
    int timestamp;
    BlockPos spawnPos;
    float strength;
    
    public static ExplosionTask newTask(int currentTick, int delay, BlockPos pos, float strength)
    {
        ExplosionTask ret = new ExplosionTask();
        ret.timestamp = currentTick + delay;
        ret.spawnPos = pos;
        ret.strength = strength;
    }
    
    public boolean shouldFire(int ticks)
    {
        return ticks >= this.timestamp;
    }
}

class TE extends TileEntity implements ITickable
{
    IList<ExplosionTask> tasks = Lists.newArrayList();
    
    void onUpdate()
    {
        if (counter % 100 == 0)
        {
            ...
            // Spawn particle
            ...
        
            tasks.add(ExplosionTask.newTask(counter, 120, pos, strength));
        }
        
        Iterator<ExplosionTask> it = tasks.getIterator();
        while(it.hasNext())
        {
            ExplosionTask task = it.next();
            if (task.shouldFire(counter))
            {
                world.newExplosion(null, task.spawnPos, strength, ...);
                it.remove();
            }
        }
    }
}

 

Posted
Spoiler
18 minutes ago, V0idWa1k3r said:

Well, there are two options I see.

The first one would be to split the process - have a separate Entity that behaves like your particle and renders like one(but is an Entity, not a Particle). Since you have full control over that entity you know when it decays and when it does - spawn your explosion from the entity's class.

The second one is to create a task system. Basically have a task class that contains a counter and the data needed to spawn the explosion - position, strength etc. Then when you spawn the particle add a new task to a collection. Every tick iterate the collection and increment/decrement/compare the counter. When the condition is satisfied spawn the explosion and remove the task from the collection. Something like this:



class ExplosionTask implements INBTSerializable<NBTTagCompound>
{
    int timestamp;
    BlockPos spawnPos;
    float strength;
    
    public static ExplosionTask newTask(int currentTick, int delay, BlockPos pos, float strength)
    {
        ExplosionTask ret = new ExplosionTask();
        ret.timestamp = currentTick + delay;
        ret.spawnPos = pos;
        ret.strength = strength;
    }
    
    public boolean shouldFire(int ticks)
    {
        return ticks >= this.timestamp;
    }
}

class TE extends TileEntity implements ITickable
{
    IList<ExplosionTask> tasks = Lists.newArrayList();
    
    void onUpdate()
    {
        if (counter % 100 == 0)
        {
            ...
            // Spawn particle
            ...
        
            tasks.add(ExplosionTask.newTask(counter, 120, pos, strength));
        }
        
        Iterator<ExplosionTask> it = tasks.getIterator();
        while(it.hasNext())
        {
            ExplosionTask task = it.next();
            if (task.shouldFire(counter))
            {
                world.newExplosion(null, task.spawnPos, strength, ...);
                it.remove();
            }
        }
    }
}

 

 

 

The task method worked flawlessly. Thank you very much for your help.

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

    • UPDATE: this seems to be an Arch-specific issue. Switching to Ubuntu server fixed EVERYTHING.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
    • Yes, Attapoll offers a $20 Sign-up bonus for new users using a code (AOVCQ). Enter the Attapoll Referral Code “AOVCQ” and submit. Yes, Attapoll offers $20 Referral Code {AOVCQ} For New Customers. If you are who wish to join Attapoll, then you should use this exclusive Attapoll referral code $20 Signup Bonus Use this Code (AOVCQ) and get $20 Welcome Bonus. You can get a $20 Signup bonus use this referral code {AOVCQ}. You can get a $20 Attpoll Referral Code {AOVCQ}. This Attapoll $20 Referral code is specifically for existing customers and can be redeemed to receive a $20. Enter $20 Attapoll Referral Code {AOVCQ} at checkout. Enjoy $20Welcome Bonus. Use the best Attapoll referral code $20 (AOVCQ) to get up to $20 first time survey as a new user. Complete the survey and get $20 credited on your Attapoll account. Plus, refer your friend to earn 10% commission on their earning If you're on the hunt for a little extra cash or some cool rewards without too much hassle, you've landed in the right place. Today, we're diving into the world of Attapoll referral codes, a nifty way to boost your earnings while taking surveys. Use this Attapoll Referral Link or code {{AOVCQ}} to get a $20 sign up bonus.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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