Command used, remove other command?

Discussion in 'Plugin Development' started by Treeline1, Jan 23, 2015.

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

    Treeline1

    So basically I'm making it so people can choose particle effects to have. What I want, is so that when someone chooses a particle, and then later chooses another, it then removes their current particle and replaces it with the ones they selected.
    This is what I have:
    Code:
    package me.liam.commands;
    
    import java.util.ArrayList;
    
    import me.liam.main.Kitpvp;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class ParticleEffects implements CommandExecutor {
    
       private Kitpvp plugin;
    
       public ParticleEffects(Kitpvp instance){
         this.plugin = instance;
       }
       
       public static ArrayList<String> particleused = new ArrayList<String>();
    
       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    
         if(!(sender instanceof Player)){
           return false;
         }
    
         final Player player = (Player)sender;
    
         if(cmd.getName().equalsIgnoreCase("flames")){
           if(!sender.hasPermission("flames.set")){
             sender.sendMessage(ChatColor.RED + "You do not have permission to use this particle!");
             return true;
           }else if(ParticleEffects.particleused.contains(player.getName())){
             ParticleEffects.particleused.remove(sender.getName());
           }else if(!(ParticleEffects.particleused.contains(sender.getName()))){
             ParticleEffects.particleused.add(sender.getName());
           
             Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
               public void run() {
                  ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2.2, 0), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0.5, 0), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0.5, 0.5), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0, 0.5, 0.5), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0.5, 0), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0.5, -0.5), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0, 0.5, -0.5), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0.5, -0.5), 10);
                   ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0.5, 0.5), 10);
                  ParticleEffect.LAVA.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2, 0), 10);
                  ParticleEffect.DRIP_LAVA.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2, 0), 10);
               }
             }, 2, 20);
         }
         }
       
         if(cmd.getName().equalsIgnoreCase("smoke")){
           if(!sender.hasPermission("smoke.set")){
             sender.sendMessage(ChatColor.RED + "You do not have permission to use this particle!");
             return true;
           }else if(ParticleEffects.particleused.contains(player.getName())){
             ParticleEffects.particleused.remove(sender.getName());
           }else if(!(ParticleEffects.particleused.contains(sender.getName()))){
             ParticleEffects.particleused.add(sender.getName());
             Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
               public void run() {
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2.3, 0), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 2.3, 0), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 2.3, 0.5), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2.3, 0.5), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 2.3, 0), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 2.3, -0.5), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2.3, -0.5), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 2.3, -0.5), 3);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 2.3, 0.5), 3);
                 
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0, 0), 2);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0, 0.5), 2);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0, 0, 0.5), 2);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0, 0), 2);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0, -0.5), 2);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0, 0, -0.5), 2);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0, -0.5), 2);
                 ParticleEffect.SMOKE_LARGE.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0, 0.5), 2);
               
               }
             }, 2, 20);
         }
         }
         if(cmd.getName().equalsIgnoreCase("heart")){
           if(!sender.hasPermission("heart.set")){
             sender.sendMessage(ChatColor.RED + "You do not have permission to use this particle!");
             return true;
           }else if(ParticleEffects.particleused.contains(player.getName())){
             ParticleEffects.particleused.remove(sender.getName());
           }else if(!(ParticleEffects.particleused.contains(sender.getName()))){
             ParticleEffects.particleused.add(sender.getName());
             Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
               public void run() {
                 Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                   public void run() {
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 2.3, 0), 3);
                     ParticleEffect.HEART.display(10, 3, 0, 0, 0, player.getLocation().add(0.5, 2.3, 0), 2);
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2.3, 0.5), 3);
                   }
                 }, 10, 20);
                 Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                   public void run() {
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 2.3, -0.5), 3);
    
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 2.3, -0.5), 3);
         
                   }
                 }, 17, 20);
                 Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                   public void run() {
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 2.3, 0.5), 3);
    
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 2.3, 0.5), 3);
                   }
                 }, 25, 20);
                   
                 Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                   public void run() {
                     ParticleEffect.HEART.display(10, 2, 0, 0, 0, player.getLocation().add(0.5, 0, 0.5), 4);
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0, -0.5), 2);
                     ParticleEffect.HEART.display(10, 2, 0, 0, 0, player.getLocation().add(-0.5, 0, -0.5), 4);
                   }
                 }, 18, 20);
                 Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                   public void run() {
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0, 0.5), 2);
                     ParticleEffect.HEART.display(10, 1, 0, 0, 0, player.getLocation().add(0, 0, 0.5), 2);
                   }
                 }, 10, 20);
                 
                 Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                   public void run() {
                     ParticleEffect.HEART.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0, 0), 2);
                     ParticleEffect.HEART.display(10, 1, 0, 0, 0, player.getLocation().add(0, 0, -0.5), 3);
                   }
                 }, 25, 20);
    
               }
             }, 5, 20);
           }
         }
         return true;
       }
       
    }
    
    
    
    At the moment it still gives the person both particle effects.
     
    Last edited: Jan 23, 2015
  2. Offline

    mythbusterma

    @Treeline1

    Print out the contents of the List when you execute the command and see what's going wrong, also stop having a public static arraylist.
     
  3. Offline

    Treeline1

    What else am I suppose to use instead of public static array? And print our statements after each command you mean?
     
  4. Offline

    mythbusterma

    @Treeline1

    Private, not static, and yes, that is what I mean.
     
  5. Offline

    Treeline1

    So atm when I do that. I do /flames for example. I then type /heart and it goes to
    Code:
    }else if(ParticleEffects.particleused.contains(player.getName())){
             ParticleEffects.particleused.remove(sender.getName());
    then stops!
    Then I have to do /heart again to get the effect but it doesnt remove it atm...

    So how would be the way to remove it? Cause I dunnno :/
     
  6. I think you're using a single arraylist for everything, you can create one for every effect.
    You can also check first if the player has a effect after add a new effect .
     
  7. Offline

    Treeline1

    If I create an array for every particle, and then checked if any of those arrays were active then my code per particle would be significantly longer. And Im hoping to prevent that
     
  8. So use a map where you store the particles for every player.
     
  9. Offline

    Treeline1

    Well right now what I am trying to do, is when they select a particle, it removes the player name name if its there and then goes to the particle they selected. Although if they dont have their name in the array yet it just goes to the particle and adds their name. Im not sure why that isnt working :/
     
  10. Offline

    PreFiXAUT

    @Treeline1 Create a Map which takes the Players UUID and then a List if Strings. Then check if everything is able to use and then check if the Player's UUID is already in the Map. If so, remove him.
    Then create the Particles and add the Player with the new Particles again.
     
  11. Offline

    Treeline1

    Ok so I decided to make it so when the player dies the particles get removed, and he can't choose another particle unless he has died. Now most works fine, although on death it doesnt remove the original particle... I've set it up the same way that my kits work but for some reason it doesn't work with particles, and Im assuming its because of the timers maybe? But can someone help with that?

    Code:
    package me.liam.commands;
    
    import me.liam.main.Kitpvp;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Flames implements CommandExecutor {
    
        private Kitpvp plugin;
        public Flames(Kitpvp instance){
            this.plugin = instance;
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    
            if(!(sender instanceof Player)){
                return false;
            }
    
            final Player player = (Player)sender;
    
    
            if(cmd.getName().equalsIgnoreCase("flames")){
                if(!sender.hasPermission("flames.set")){
                    sender.sendMessage(ChatColor.RED + "You do not have permission to use this particle!");
                    return true;
                }else if(!(this.plugin.Flames.contains(player.getName()))){
                    if(!(this.plugin.particleused.contains(player.getName()))){
                        this.plugin.Flames.add(player.getName());
                        this.plugin.particleused.add(player.getName());
    
                        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                            public void run() {
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2.2, 0), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0.5, 0), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0.5, 0.5), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0, 0.5, 0.5), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0.5, 0), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0.5, -0.5), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0, 0.5, -0.5), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(0.5, 0.5, -0.5), 10);
                                ParticleEffect.FLAME.display(10, 15, 0, 0, 0, player.getLocation().add(-0.5, 0.5, 0.5), 10);
                                ParticleEffect.LAVA.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2, 0), 10);
                                ParticleEffect.DRIP_LAVA.display(10, 15, 0, 0, 0, player.getLocation().add(0, 2, 0), 10);
                            }
                        }, 2, 20);
    
                    }
                }
            }
    
            return true;
        }
    
    }
    Code:
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event){
            Player player = event.getEntity().getPlayer();
            String msg = event.getDeathMessage();
            msg = ChatColor.RED + msg;
            if(this.plugin.kitused.contains(player.getName())){
                this.plugin.kitused.remove(player.getName());
                if(this.plugin.KitCommand.contains(player.getName())){
                    this.plugin.KitCommand.remove(player.getName());
                }else if(this.plugin.ArcherCommand.contains(player.getName())){
                    this.plugin.ArcherCommand.remove(player.getName());
                }else if(this.plugin.ScoutCommand.contains(player.getName())){
                    this.plugin.ScoutCommand.remove(player.getName());
                }else if(this.plugin.SquashCommand.contains(player.getName())){
                    this.plugin.SquashCommand.remove(player.getName());
                }else if(this.plugin.HopperCommand.contains(player.getName())){
                    this.plugin.HopperCommand.remove(player.getName());
                }else if(this.plugin.MarioCommand.contains(player.getName())){
                    this.plugin.MarioCommand.remove(player.getName());
                }else if(this.plugin.RageCommand.contains(player.getName())){
                    this.plugin.RageCommand.remove(player.getName());
                }else if(this.plugin.FishermanCommand.contains(player.getName())){
                    this.plugin.FishermanCommand.remove(player.getName());
                }else if(this.plugin.PilotCommand.contains(player.getName())){
                    this.plugin.PilotCommand.remove(player.getName());
                }else if(this.plugin.JuggernautCommand.contains(player.getName())){
                    this.plugin.JuggernautCommand.remove(player.getName());
                }else if(this.plugin.SniperCommand.contains(player.getName())){
                    this.plugin.SniperCommand.remove(player.getName());
                }else if(this.plugin.SquidCommand.contains(player.getName())){
                    this.plugin.SquidCommand.remove(player.getName());
                }else if(this.plugin.GravitatorCommand.contains(player.getName())){
                    this.plugin.GravitatorCommand.remove(player.getName());
                }else if(this.plugin.RemoverCommand.contains(player.getName())){
                    this.plugin.RemoverCommand.remove(player.getName());
                }
            }
            if(this.plugin.particleused.contains(player.getName())){
                this.plugin.particleused.remove(player.getName());
                if(this.plugin.Hearts.contains(player.getName())){
                    this.plugin.Hearts.remove(player.getName());
                }else if(this.plugin.Smoke.contains(player.getName())){
                    this.plugin.Smoke.remove(player.getName());
                }else if(this.plugin.Flames.contains(player.getName())){
                    this.plugin.Flames.remove(player.getName());
                }
            }
        }
       
       
    
    Its removing them from the list, but just not getting rid of the particles.

    Im thinking its because of the time scheduler but I am not 100% sure :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  12. Offline

    PreFiXAUT

    @Treeline1 Yes it is because of the scheduler...you start the tasks, but you stop them, so they will go on infinitly (except you stop the server/your plugin)

    You need to store them with the Player, and when he chooses another one, get the Task which is associated with the Player and stop the Task.
     
  13. Offline

    Treeline1

    Store them with the player? How does one do this...
     
  14. Offline

    PreFiXAUT

Thread Status:
Not open for further replies.

Share This Page