#Keep original values on object creation

14 messages · Page 1 of 1 (latest)

unreal trout
#

Hello!
I need to call every once in a while a certain function with all values in a list, and when defining this list I use some loops
Here's my code for it below for example ```java
for (int i = 0; i < particleCount; i++) {
final float ratio = (float) i / (particleCount - 1);
final double x = interpolate(aX, bX, ratio);
final double y = startY + 0.25;
final double z = interpolate(aZ, bZ, ratio);

particles.add(new Particles(Particle.DUST, x, y, z, 1, 0, 0, 0, 0, new Particle.DustOptions(Color.RED, 1))); // did the same with Runnables before, but it did the same issue

}My issue with it, is that when calling all particles, they all have the same values `x`, `y` and `z`, from the last particle added, as you can see in the logs below:
[20:05:06 INFO]: X:4.5 , Y:4.5 , Z:100.25
[20:05:06 INFO]: X:4.5 , Y:4.5 , Z:100.25
[20:05:06 INFO]: X:4.5 , Y:4.5 , Z:100.25
[20:05:06 INFO]: X:4.5 , Y:4.5 , Z:100.25
[20:05:06 INFO]: X:4.5 , Y:4.5 , Z:100.25
[20:05:06 INFO]: X:4.5 , Y:4.5 , Z:100.25
... some more lines saying the same thing

Thanks in advance!
opal summitBOT
#

This post has been reserved for your question.

Hey @unreal trout! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

opal summitBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

opal summitBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

fathom copper
#

are you sure you are printing right objects and not the same everytime? @unreal trout

unreal trout
fathom copper
#

show more code

unreal trout
#

Added a reminder

opal summitBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

unreal trout
#
private static final ArrayList<Particles> particles = new ArrayList<>();

public static void execute(World world) {
    particles.clear();

    final double startX = 0.5, startY = 100, startZ = 0.5;

    final int particleCount = 20;
    Bukkit.getScheduler().runTaskTimer(Plugin.getInstance(), new Consumer<>() {
        int currentOffset = 0;

        @Override
        public void accept(BukkitTask task) {
            // offsets is just a list of Vector2d
            Vector2d a = offsets.get(currentOffset), b = offsets.get((currentOffset++) % offsets.size());
            double aX = a.x + startX, aZ = a.y + startZ;
            double bX = b.x + startX, bZ = b.y + startZ;

            for (int i = 0; i < particleCount; i++) {
                final float ratio = (float) i / (particleCount - 1);
                final double x = interpolate(aX, bX, ratio);
                final double y = startY + 0.25;
                final double z = interpolate(aZ, bZ, ratio);

                // particles.add(() -> world.spawnParticle(Particle.DUST, x, y, z, 1, 0, 0,  0, 0, new Particle.DustOptions(Color.RED, 1)));
                particles.add(new Particles(Particle.DUST, x, y, z, 1, 0, 0, 0, 0, new Particle.DustOptions(Color.RED, 1)));
            }

            if (currentOffset >= offsets.size()) {
                task.cancel();
                return;
            }
        }
    }, 20L, 20L);```
#
    Bukkit.getScheduler().runTaskTimer(Plugin.getInstance(), (task) -> {
        particles.forEach(p -> {
            Bukkit.broadcastMessage(String.format("X:%s , Y:%s , Z:%s", p.x, p.z, p.y));
            world.spawnParticle(p.particle, p.x, p.y, p.z, p.count, p.offsetX, p.offsetY, p.offsetZ, p.extra, p.data, true);
        });
        // particles.forEach(Runnable::run);
    }, 0L, 10L);
}

public static class Particles {
    private final Particle particle;
    private final double x, y, z;
    private final int count;
    private final double offsetX, offsetY, offsetZ, extra;
    private final Object data;

    /**
     * @param particle the particle to spawn
     * @param x        the position on the x axis to spawn at
     * @param y        the position on the y axis to spawn at
     * @param z        the position on the z axis to spawn at
     * @param count    the number of particles
     * @param offsetX  the maximum random offset on the X axis
     * @param offsetY  the maximum random offset on the Y axis
     * @param offsetZ  the maximum random offset on the Z axis
     * @param extra    the extra data for this particle, depends on the
     *                 particle used (normally speed)
     * @param data     the data to use for the particle or null,
     *                 the type of this depends on {@link Particle#getDataType()}
     */
    public Particles(Particle particle, double x, double y, double z, int count, double offsetX, double offsetY, double offsetZ, double extra, Object data) {
        this.particle = particle;
        this.x = x;
        this.y = y;
        this.z = z;
        this.count = count;
        this.offsetX = offsetX;
        this.offsetY = offsetY;
        this.offsetZ = offsetZ;
        this.extra = extra;
        this.data = data;
    }
}