1.16.5 AtomicRefrence returnng null

Discussion in 'Spigot Plugin Development' started by AlexNomad, Jun 9, 2021.

  1. I'm trying to remove armor stand after 2 seconds, but AtomicRefrence returning null. How to fix it. Or there's another ways to remove it?

    Code (Java):
    package dev.alexnomad.cringe.bubles;

    import dev.alexnomad.cringe.Cringe;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;

    import java.util.concurrent.atomic.AtomicLong;
    import java.util.concurrent.atomic.AtomicReference;

    public class Buble {

        public Buble(Location location, String message){

            AtomicReference<Entity> a = null;
            AtomicLong creationTimestamp = new AtomicLong();

            Bukkit.getScheduler().runTask(Cringe.getPlugin(Cringe.class),
                    () -> {
                        Entity armor = location.getWorld().spawn(location, EntityType.ARMOR_STAND.getEntityClass());
                        armor.setCustomName(message);
                        armor.setCustomNameVisible(true);
                        armor.setGravity(false);
                        armor.setInvulnerable(true);
                        System.out.println(message);
                        creationTimestamp.set(System.currentTimeMillis());
                        a.set(armor);
                        });
            while (true){
                if ((System.currentTimeMillis() - creationTimestamp.get()) >= 2000){
                    a.get().remove();
                    break;
                }
            }

        }
    }
     
     
  2. Code (Java):
    Bukkit.getScheduler().runTaskLater(plugin, armorstand::remove, 40L);
    40 ticks = 2 seconds
     
  3. You're getting null because you haven't declared the AtomicRefrence variable.
     
  4. SteelPhoenix

    Moderator

    I honestly don't know what you expect to happen doing AtomicReference<Entity> a = null;...
    Of course it'll be null.
     
  5. I think you ment to do
    Code (Java):
    AtomicReference<Entity> a = new AtomicReference(null);
    Which creates an AtomicReference carrying the value null