Particle Effects on ONLY this player who clicked

Discussion in 'Plugin Development' started by tedwurd, Jul 28, 2014.

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

    tedwurd

    Heej dear forum

    I need your help once.
    I'm just here to write a plugin which allows one when he makes a right click on a NetherStar opens an inventory.
    In this inventory are Items and if you click on this you get a Particle Effect on set.
    This folding up to this point.
    But if you use in multiplayer it changes the effect of each player not only from that of it has clicked ...


    Code:
    package me.tedwurd;
     
    import me.tedwurd.MainClass;
    import me.tedwurd.ParticleEffect;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    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.PlayerQuitEvent;
    import org.bukkit.plugin.Plugin;
     
    public class InventoryListener implements Listener {
     
        private MainClass plugin;
     
        public InventoryListener(MainClass info) {
            this.plugin = info;
            this.plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        @EventHandler
        public void onInvClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
     
            if (e.getInventory().getName().equalsIgnoreCase("ยง6Particles")) {
                e.setCancelled(true);
     
                // - Herzen
                if (e.getCurrentItem().getType() == Material.APPLE) {
                    p.closeInventory();
                    p.playSound(p.getLocation(), Sound.CHEST_CLOSE, 1, 1);
                    Bukkit.getScheduler().cancelAllTasks();
     
                   
                    Bukkit.getServer()
                            .getScheduler()
                            .scheduleSyncRepeatingTask(this.plugin = plugin,
                                    new Runnable() {
                                        public void run() {
                                            for (Player p : Bukkit.getServer()
                                                    .getOnlinePlayers()) {
                                                ParticleEffect.HEART.display(
                                                        p.getLocation()
                                                                .add(0, 2, 0), 15,
                                                        0, 0, 0, 10, 10);
                                            }
                                        }
                                    }, 0, 25);
                }
     
                // - Lava
                if (e.getCurrentItem().getType() == Material.LAVA_BUCKET) {
                    p.closeInventory();
                    p.playSound(p.getLocation(), Sound.CHEST_CLOSE, 1, 1);
                    Bukkit.getScheduler().cancelAllTasks();
     
                    Bukkit.getServer()
                            .getScheduler()
                            .scheduleSyncRepeatingTask(this.plugin = plugin,
                                    new Runnable() {
                                        public void run() {
                                            for (Player p : Bukkit.getServer()
                                                    .getOnlinePlayers()) {
                                                ParticleEffect.LAVA.display(
                                                        p.getLocation()
                                                                .add(0, 2, 0), 15,
                                                        0, 0, 0, 10, 10);
                                            }
                                        }
                                    }, 0, 50);
                }
     
                // - WitchMagic
                if (e.getCurrentItem().getType() == Material.EXP_BOTTLE) {
                    p.closeInventory();
                    p.playSound(p.getLocation(), Sound.CHEST_CLOSE, 1, 1);
                    Bukkit.getScheduler().cancelAllTasks();
     
                    Bukkit.getServer()
                            .getScheduler()
                            .scheduleSyncRepeatingTask(this.plugin = plugin,
                                    new Runnable() {
                                        public void run() {
                                            for (Player p : Bukkit.getServer()
                                                    .getOnlinePlayers()) {
                                                ParticleEffect.WITCH_MAGIC.display(
                                                        p.getLocation()
                                                                .add(0, 2, 0), 15,
                                                        0, 0, 0, 10, 10);
                                            }
                                        }
                                    }, 0, 1);
                }
     
            }
     
        }
    }

    I hope you can help me.
    Greetings! :)
     
  2. Offline

    Monkey_Swag

    if you only want to effects to run to a single player, why are you looping for every player online in your schedulers?
     
  3. Offline

    tedwurd

    What i need to do ?

    Code:
     (Player p : Bukkit.getServer()
                            .getScheduler()
                            .scheduleSyncRepeatingTask(this.plugin = plugin,
                                    new Runnable() {
    Dont work
     
  4. Offline

    zDylann

    Hes saying to remove the loop. You are looping through every player on the server in what it seems like you only want to display to the player clicking it.
     
  5. Offline

    tedwurd

    and what i need to write ?
    im "new" in programming and i know the scheduler not so long.
     
  6. Offline

    GameplayJDK

    tedwurd Make p final and remove the for(...) loop. That should work.

    Alternatively you can add the players name to a public ArrayList and loop through that list instead of the list of all online players. If you decide to do that, you should move the shedule part to the onEnable method to save resources (currently you create a new syncRepeatingTask every time the event is fired!).
     
Thread Status:
Not open for further replies.

Share This Page