Solved Running all particle from a config instead of just 1

Discussion in 'Plugin Development' started by ItsComits, Feb 18, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    ItsComits

    So the problem is that I cannot seem to give a specific player 1 type of particle effect and if I do the player seems to get them all and sometimes all the players online.
     
    Last edited: Mar 3, 2017
  2. Offline

    Drkmaster83

    You're looping as opposed to providing which specific one you want to give the player, from there, you can have it be "String baseParticle" and do baseParticle + ".Particle.Type", etc. Where baseParticle is "NumberOne" or "NumberTwo"
     
  3. Offline

    ItsComits

    @Drkmaster83 How would I go about doing this. As in the past I have looped through them and got the contents of the args as I was doing this for a command. Any ideas?
     
    Last edited: Feb 19, 2017
  4. Offline

    Drkmaster83

    Erase your most outer loop, and make a String called "Masks", and "Masks" will be either "ParticleOne" or "ParticleTwo". But also, you should move that if statement for the Particle.Enabled outside of the inner loop.
    Code:
    String masks = "ParticleOne";
    String particle = <config>.getString(masks + ".Particle.Type");
    //Repeating task code here
    if(<config>.getBoolean(masks+".Particle.Enabled")) {
      //all player for-loop here
    }
    //End repeating task code
     
  5. Offline

    ItsComits

    @Drkmaster83 Thank you, I got each particle to work. The only problem is. The scheduler only works for one player and not all. I have researched for many hours and cannot find a fix. Here is the addParticle method.
    Code:
        public void addParticles(final Player p, final String Masks) {
            if (MasksYML.getMasksYML().getBoolean(Masks + ".Particle.Enabled")) {
                final String particle = MasksYML.getMasksYML().getString(Masks + ".Particle.Type");
                particleTick = Bukkit.getScheduler().scheduleSyncRepeatingTask(ExteriaCore.plugin, new Runnable() {
                    @Override
                    public void run() {
                        String locationString = MasksYML.getMasksYML().getString(Masks + ".Particle.Location");
                        String[] values = locationString.split(",");
                        float x = Float.parseFloat(values[0]);
                        float y = Float.parseFloat(values[1]);
                        float z = Float.parseFloat(values[2]);
                        float speed = Float.parseFloat(values[3]);
                        int amount = Integer.parseInt(values[4]);
                        for (Player AllP : Bukkit.getServer().getOnlinePlayers()) {
                            ParticleEffect.valueOf(particle).display(x, y, z, speed, amount, p.getLocation(),
                                    new Player[] { AllP });
                        }
                    }
                }, 0L, 0L);
            }
        }
     
  6. Offline

    Drkmaster83

    @ItsComits Well.. right..
    Your repeating task only spawns the particle on the player that's given in the parameter (p.getLocation()). You could loop through all players online and then whoever additional who joins and run this method, or just create a repeating task in the onEnable that does it for all of them.
     
  7. Offline

    ItsComits

    @Drkmaster83 I have already looped through all players online so that other players are able to see that players particles. My problem is that when the method is executed. The method runs for that players but if another player does the method while the other player is running it. The scheduler will stop for one player and start on the other.
     
  8. Offline

    Drkmaster83

    Ah, I see, you only want one instance of the runnable. Your system currently has a glitch, then. So, are you progressively adding people to a list that have particles on them, or is every single online player supposed to have the particles on them?
     
  9. Offline

    ItsComits

    @Drkmaster83 Here is the whole class. So you can have a good look through it as I have never experienced this glitch.
    Code:
    package me.ItsComits.ExteriaCore.Masks;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.inventory.meta.SkullMeta;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    import me.ItsComits.ExteriaCore.Libraries.ParticleEffect;
    import me.ItsComits.ExteriaCore.ExteriaCore;
    import me.ItsComits.ExteriaCore.Masks.Files.MasksConfigYML;
    import me.ItsComits.ExteriaCore.Masks.Files.MasksYML;
    import me.ItsComits.ExteriaCore.Extras.Motd;
    
    public class ExteriaMasks implements Listener {
        ArrayList<Player> Particle = new ArrayList<>();
        int particleTick;
        int itemTick;
        String finalMask = "";
    
        @EventHandler
        public void onPlayerQuitEvent(PlayerQuitEvent e) {
            Motd.playerCount--;
        }
    
        @EventHandler
        public void onPlayerJoinEvent(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            Motd.playerCount++;
            checkAll(p);
        }
    
        @EventHandler
        public void onInvClickEvent(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            checkAll(p);
        }
    
        public void checkAll(final Player p) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(ExteriaCore.plugin, new Runnable() {
    
                @Override
                public void run() {
    
                    hasMask(p);
                }
            }, 0L);
        }
    
        public void hasMask(final Player p) {
            Bukkit.getScheduler().scheduleSyncDelayedTask(ExteriaCore.plugin, new Runnable() {
                @Override
                public void run() {
                    String DisplayName = "";
                    PlayerInventory inv = p.getInventory();
                    ItemStack helmet = inv.getHelmet();
                    String HelmetName = "";
    
                    for (String Masks : MasksYML.getMasksYML().getKeys(false)) {
                        DisplayName = MasksYML.getMasksYML().getString(Masks + ".DisplayName").replaceAll("&", "");
                        if ((helmet == null) || (helmet.getItemMeta() == null) || (helmet.getType() == Material.AIR)) {
                            removePotions(p, Masks);
                            removeParticle(p, Masks);
                            removeItems(p, Masks);
                            Particle.remove(p);
                        } else {
                            try {
                                HelmetName = helmet.getItemMeta().getDisplayName().replaceAll("§", "");
                            } catch (Exception e) {
                                HelmetName = helmet.getItemMeta().getDisplayName();
                            }
                            if (DisplayName.equals(HelmetName)) {
                                finalMask = Masks;
                                //When a player puts the helmet on with another helmet on.
                                Particle.remove(p);
                                removeParticle(p, Masks);
                                removePotions(p, Masks);
                                removeItems(p, Masks);
                              
                                //
                                Particle.add(p);
                                addPotions(p, Masks);
                                addParticles(p, Masks);
                                addItems(p, Masks);
                                despawn(p);
                            }
    
                        }
    
                    }
                }
            }, 20L);
    
        }
    
        public void addParticles(final Player p, final String Masks) {
            if (MasksYML.getMasksYML().getBoolean(Masks + ".Particle.Enabled")) {
                final String particle = MasksYML.getMasksYML().getString(Masks + ".Particle.Type");
                particleTick = Bukkit.getScheduler().scheduleSyncRepeatingTask(ExteriaCore.plugin, new Runnable() {
                    @Override
                    public void run() {
                        for (Player PList : Particle) {
                            String locationString = MasksYML.getMasksYML().getString(Masks + ".Particle.Location");
                            String[] values = locationString.split(",");
                            float x = Float.parseFloat(values[0]);
                            float y = Float.parseFloat(values[1]);
                            float z = Float.parseFloat(values[2]);
                            float speed = Float.parseFloat(values[3]);
                            int amount = Integer.parseInt(values[4]);
                            for (Player AllP : Bukkit.getServer().getOnlinePlayers()) {
                                ParticleEffect.valueOf(particle).display(x, y, z, speed, amount, PList.getLocation(),
                                        new Player[] { AllP });
                            }
                        }
                    }
                }, 0L, 0L);
            }
        }
    
        public void removeParticle(Player p, String Masks) {
            if (MasksYML.getMasksYML().getBoolean(Masks + ".Particle.Enabled")) {
                Bukkit.getScheduler().cancelTask(particleTick);
            }
        }
    
        public void addPotions(Player p, String Masks) {
            for (String effectString : MasksYML.getMasksYML().getStringList(Masks + ".Effects")) {
                String[] values = effectString.split(",");
                PotionEffectType type;
                int duration = 30;
                int strength = 1;
                type = PotionEffectType.getByName(values[0].toUpperCase());
                if (values.length > 1)
                    duration = Integer.parseInt(values[1]);
                if (values.length > 2)
                    strength = Integer.parseInt(values[2]);
                PotionEffect effect = type.createEffect(duration * 20, strength);
                p.addPotionEffect(effect);
            }
        }
    
        public void removePotions(Player p, String Masks) {
            for (String effectString : MasksYML.getMasksYML().getStringList(Masks + ".Effects")) {
                String[] values = effectString.split(",");
                PotionEffectType type;
                type = PotionEffectType.getByName(values[0].toUpperCase());
                p.removePotionEffect(type);
            }
        }
    
        public void addItems(final Player p, final String Masks) {
            if (MasksYML.getMasksYML().getBoolean(Masks + ".EntityDrop.Enabled")) {
                int time = MasksYML.getMasksYML().getInt(Masks + ".EntityDrop.Time");
                itemTick = Bukkit.getScheduler().scheduleSyncRepeatingTask(ExteriaCore.plugin, new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ItemStack is = new ItemStack(
                                    Material.valueOf(MasksYML.getMasksYML().getString(Masks + ".EntityDrop.Item")), 1);
                            ItemMeta im = is.getItemMeta();
                            im.setDisplayName("ItsComits");
                            is.setAmount(1);
                            is.setItemMeta(im);
    
                            Item item = p.getWorld().dropItem(p.getLocation().add(0, 1.8, 0), is);
                            item.setVelocity(new Vector(randomInRange(-0.2, 0.2), 0.53D, randomInRange(-0.2, 0.2)));
                            Entity entity = item;
                            entity.setCustomNameVisible(true);
                            String hologram = "";
                            try {
                                hologram = MasksYML.getMasksYML().getString(Masks + ".EntityDrop.Hologram").replaceAll("&",
                                        "§");
                            } catch (Exception e) {
                                hologram = MasksYML.getMasksYML().getString(Masks + ".EntityDrop.Hologram");
    
                            }
                            entity.setCustomName(hologram);
                        } catch (NullPointerException localNullPointerException) {
                        }
                    }
                }, 0L, 20 * time);
            }
        }
    
        public void removeItems(Player p, String Masks) {
            Bukkit.getScheduler().cancelTask(itemTick);
            itemTick = 0;
        }
    
        public void despawn(final Player p) {
            Bukkit.getScheduler().scheduleSyncRepeatingTask(ExteriaCore.plugin, new Runnable() {
                @Override
                public void run() {
                    for (Entity entity : p.getWorld().getEntities()) {
                        if (entity.getTicksLived() >= 60) {
                            if (entity.getType().equals(EntityType.DROPPED_ITEM)) {
                                if (entity.isCustomNameVisible() == true) {
                                    Location loc = entity.getLocation();
                                    for (Player AllP : Bukkit.getServer().getOnlinePlayers()) {
                                        ParticleEffect.CLOUD.display(0.4F, 0.4F, 0.4F, 0, 1, loc, AllP);
                                        entity.remove();
                                    }
    
                                }
                            }
                        }
                    }
                }
            }, 0L, 40L);
        }
    
        public static double randomInRange(double min, double max) {
            Random random = new Random();
            double range = max - min;
            double scaled = random.nextDouble() * range;
            double shifted = scaled + min;
            return shifted;
        }
    
        @EventHandler
        public void playerDrop(PlayerPickupItemEvent event) {
            if (event.getItem().isCustomNameVisible() == false) {
                event.setCancelled(false);
            } else {
                event.setCancelled(true);
            }
        }
    
        // Mask
        public static void giveMask(Player p, String MaskName) {
            String SkullOwner = "";
            String DisplayName = "";
            try {
                DisplayName = ChatColor.translateAlternateColorCodes('&',
                        MasksYML.getMasksYML().getString(MaskName + ".DisplayName"));
            } catch (Exception e) {
                DisplayName = MasksYML.getMasksYML().getString(MaskName + ".DisplayName");
            }
            SkullOwner = MasksYML.getMasksYML().getString(MaskName + ".SkullOwner");
            ItemStack is = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
            SkullMeta sm = (SkullMeta) Bukkit.getItemFactory().getItemMeta(Material.SKULL_ITEM);
    
            for (String effectString : MasksYML.getMasksYML().getStringList(MaskName + ".Enchantments")) {
                String[] values = effectString.split(",");
                Enchantment type;
                int strength = 1;
                type = Enchantment.getByName(values[0].toUpperCase());
                strength = Integer.parseInt(values[1]);
                sm.addEnchant(type, strength, true);
            }
            ArrayList<String> lores = new ArrayList<String>();
            for (String LoreList : MasksYML.getMasksYML().getStringList(MaskName + ".Lore")) {
                lores.add(LoreList.replaceAll("&", "§"));
            }
            sm.setOwner(SkullOwner);
            sm.setDisplayName(DisplayName);
            sm.setLore(lores);
            is.setItemMeta(sm);
            p.getInventory().addItem(is);
    
        }
    
        @SuppressWarnings("deprecation")
        public static void giveMaskItem(Player p) {
            String displayName = "";
            try {
                displayName = ChatColor.translateAlternateColorCodes('&',
                        MasksConfigYML.getConfigYML().getString("MaskItem.DisplayName"));
            } catch (Exception e) {
                displayName = MasksConfigYML.getConfigYML().getString("MaskItem.DisplayName");
            }
            String IdString = MasksConfigYML.getConfigYML().getString("MaskItem.ID");
            String[] IdInfo = IdString.split(":");
            if (IdInfo.length == 2) {
                int itemID = Integer.parseInt(IdInfo[0]);
                int itemData = Integer.parseInt(IdInfo[1]);
                ItemStack is = new ItemStack(itemID, 1, (short) itemData);
                ItemMeta im = is.getItemMeta();
                ArrayList<String> lores = new ArrayList<String>();
                for (String lorelist : MasksConfigYML.getConfigYML().getStringList("MaskItem.Lore")) {
                    lores.add(lorelist.replaceAll("&", "§"));
                }
                im.setLore(lores);
                im.setDisplayName(displayName);
                is.setItemMeta(im);
                p.getInventory().addItem(is);
            }
        }
    }
    
     
  10. Offline

    ItsComits

  11. Offline

    MrGeneralQ

    What you can so is creating a new HashMap and put a new runnable in there.
     
  12. Offline

    ItsComits

    Sorry for the late reply but thank you. Much appreciated. :D
     
  13. Offline

    MrGeneralQ

    Did it help You out?
     
Thread Status:
Not open for further replies.

Share This Page