Nicola Uetz
Nicola Uetz

Reputation: 858

Spigot infinite Firework

I'm currently working on a plugin that should freeze entitys. I already did most of them but I'm now really messed up with fireworks. I already have a function that teleports all the "moving" entitys back to their location every tick.

Bukkit.getScheduler().runTaskTimer(instance, () -> {
    for (Entity e : entities) {
        //teleporting and setting velocity
        if (e instanceof Firework) {
            Firework f = (Firework) e;
            //TODO how can I make it NOT disappear after one or two seconds
        }
    }
});

Now the problem with fireworks is, that they automatically get removed after they triggered some sort of lifespan and detonate. I just don't want that if the entities are frozen.

I already tried f.setTicksLived(1); but sadly this won't change anything at all. (I really don't think this function works how it is supposed to do)

My next approach was to change the power of the firework.

FireworkMeta fm = f.getFireworkMeta();
fm.setPower(127);
f.setFireworkMeta(fm);

But since 127 is the maxial allowed number for .setPower() the firework will still vanish after a minute or two.

I really want the firework to be visible for an indefinite timespan. It should not disappear at all and launching a new firework every like 10 seconds is not an option since it would always play the launch-sound which I simply don't want.

Upvotes: 1

Views: 1063

Answers (1)

otoomey
otoomey

Reputation: 999

According to the Entity Data section of the Minecraft Firework Rocket page, Firework Rockets have the following NBT data (among others):

  • integer Life is the number of ticks the rocket has been flying for.

  • integer LifeTime is the number of ticks Life must be greater than or equal to in order to explode.

AFAIK neither of these values can be modified using the Firework entity or FireworkMeta classes provided by Bukkit.

However, by modifying the NBT data of the Firework Rocket entity directly, we are able to change these values:

net.minecraft.server.v1_5_R1.Entity mcFireworkEntity = ((CraftEntity) bukkitFireworkEntity).getHandle();
NBTTagCompound tag = new NBTTagCompound();

mcFireworkEntity.c(tag); // gets the firework to dump nbt data into our 'tag' object

// set the entity life flag to 1.
tag.setInt("Life", 1);
// you can optionally also set the `LifeTime` value to the maximum setting as well
// tag.setInt("LifeTime", 2147483647)

// write the tag back into the entity. This needs to happen every game tick
// because minecraft will increase this value by 1 every tick
((EntityLiving)mcFireworkEntity).a(tag); // 

NBTTagCompound is part of the decompiled minecraft server repository provided by bukkit (not sure if by default, may need some messing around on your part).

Upvotes: 1

Related Questions