1.8.8 Code does not run completely

Discussion in 'Spigot Plugin Development' started by devxyz, Jun 29, 2020 at 7:06 AM.

  1. When the player is not OP, the code runs until line 68 and then no longer executes the commands. When the player is OP, everything works perfectly. I'm using LuckPerms. If you can help me find out why it happens, I appreciate it.
    Code (Java):

    package com.devxyz.ryuautoclick;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Animals;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Monster;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener {
     
        protected ArrayList<String> enabledPlayers;
     
        private void msgCS(ChatColor color,String msg) {
            Bukkit.getConsoleSender().sendMessage(color+msg);
        }
     
        public void onEnable() {
            msgCS(ChatColor.GREEN, "--------------------");
            msgCS(ChatColor.YELLOW, "RyuAutoclick Ativado");
            msgCS(ChatColor.GREEN, "--------------------");
         
            this.enabledPlayers = new ArrayList<String>();
         
            getServer().getPluginManager().registerEvents( this, (Plugin)this);
        }
     
        public void onDisable() {
            msgCS(ChatColor.RED, "--------------------");
            msgCS(ChatColor.YELLOW, "RyuAutoclick Desativado");
            msgCS(ChatColor.RED, "--------------------");
        }

     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("autoclick")) {
                if(sender instanceof Player == false) {
                    sender.sendMessage(ChatColor.RED + "Esse comando pode ser utilizado somente no server." );
                    return true;
                }
             
                if(sender instanceof Player) {
                    Player player = (Player) sender;
                    String playerName = player.getName();
                 
                    if(enabledPlayers.contains(playerName)) {
                        enabledPlayers.remove(playerName);
                        sender.sendMessage(ChatColor.RED + "Autoclick desativado.");
                    }else{
                     
                        if(player.getWorld().getName().equalsIgnoreCase("cidade")) {
                            if(player.hasPermission("rac.usar")) {
                                enabledPlayers.add(playerName);
                                sender.sendMessage(ChatColor.GREEN + "Autoclick ativado. ");
                             
                                for(Entity entity : player.getWorld().getEntities()) {
                                    if(entity instanceof Monster || entity instanceof Animals) {
                                        if(entity.getLocation().distance(player.getLocation()) < 7) {
                                                ((LivingEntity) entity).damage(200.0D, player);
                                        }
                                    }
                                }
                            }else{
                                player.sendMessage(ChatColor.YELLOW + "Voce não tem permissão. Compre vip em nosso site:"+ChatColor.RED+" /site"+ChatColor.YELLOW+".");
                            }
                        }else {
                            player.sendMessage(ChatColor.RED + "Esse comando só pode ser utilizado no mundo de Plots.");
                        }
                    }
                }
            }
         
            return false;
        }
     
        @EventHandler
        public void onSpawn(CreatureSpawnEvent event) throws InterruptedException {

            Entity en = event.getEntity();
         
            if(!(en instanceof Monster) && !(en instanceof Animals)) {
                return;
            }
            if(en.getWorld().getName().equalsIgnoreCase("cidade")) {
                List<Entity> near = en.getNearbyEntities(7.0D, 7.0D, 7.0D);
                for(Entity entity : near) {
                    if(entity instanceof Player) {
                        Player nearPlayer = (Player) entity;
                        if(enabledPlayers.contains(nearPlayer.getName())) {
                            if(en.getLocation().distance(nearPlayer.getLocation())<7) {
                                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    public void run() {
                                        ((LivingEntity)en).damage(200.0D, nearPlayer);
                                    }
                                },2*20L);
                            }
                        }
                    }
                }
            }
         
         
         
        }  
    }
     
    #1 devxyz, Jun 29, 2020 at 7:06 AM
    Last edited: Jun 29, 2020 at 7:15 AM
  2. Sure, imma start counting the lines.
     
    • Like Like x 1
  3. I'm not gonna really count the lines, if you can tell us where that line is we can help, however when you use
    Code (Java):
    } else {
    I would recommend you use
    Code (Java):
    } else if (condition) {
    instead so the code is easier to understand.
     
  4. i like how you checked for the type twice.

    ?? why? his code would look like this which is pretty messy ngl

    Code (Text):
    if(player.hasPermission("rac.usar")) {

    } else if(!player.hasPermission("rac.usar")){

    }
     
  5. You're using what I said wrong, you wouldn't do it if its one if statement, you would do it if you had many other if statements inside, so that its not just like:
    Code (Java):
    if(sender instanceof Player) {
                    Player player = (Player) sender;
                    String playerName = player.getName();
               
                    if(enabledPlayers.contains(playerName)) {
                        enabledPlayers.remove(playerName);
                        sender.sendMessage(ChatColor.RED + "Autoclick desativado.");
                    }else{
                   
                        if(player.getWorld().getName().equalsIgnoreCase("cidade")) {
                            if(player.hasPermission("rac.usar")) {
                                enabledPlayers.add(playerName);
                                sender.sendMessage(ChatColor.GREEN + "Autoclick ativado. ");
                           
                                for(Entity entity : player.getWorld().getEntities()) {
                                    if(entity instanceof Monster || entity instanceof Animals) {
                                        if(entity.getLocation().distance(player.getLocation()) < 7) {
                                                ((LivingEntity) entity).damage(200.0D, player);
                                        }
                                    }
                                }
                            }else{
                                player.sendMessage(ChatColor.YELLOW + "Voce não tem permissão. Compre vip em nosso site:"+ChatColor.RED+" /site"+ChatColor.YELLOW+".");
                            }
                        }else {
                            player.sendMessage(ChatColor.RED + "Esse comando só pode ser utilizado no mundo de Plots.");
                        }
                    }
                }
    You would use it so that you can understand why e.g a player is being sent a message, which makes the code easier to work with and make better. If you just have } else { everywhere you won't understand what its for, making you waste time trying to find what it is actually for, especially when there is an error and you have to remove things, having else does not help anybody.
     
  6. still, you’re repeating the condition check again. If the boolean expression is expensive, the worst case is that you perform the check more than you need to. That slows down your program unnecessarily. Why not just use comments?
     
  7. Use comments then, but he wasn't, so I suggested using else if. Use comments if you really want to. Makes no difference, just use either of them.
     
  8. Not always but ok.
     
  9. @doontcare sorry for my way of expressing. I'm a beginner in bukk. The line I referred to was this:

    if(player.hasPermission("rac.usar")) {
    enabledPlayers.add(playerName);
    sender.sendMessage(ChatColor.GREEN + "Autoclick ativado. ");
     
  10. The condition is false unless the player is op because the player simply does not have the said permission.
    (Opped players have all permissions)
     
  11. Yes, for that it's fine because it doesnt have if statements inside, but if there was, I would suggest doing else if or commenting as it is easier to understand
     
  12. But I added this permission to the group through luckperms, so much so that it prints the message that is within the condition.
     
  13. @doontcare I'll try this
     
  14. @doontcare I did what you said, but without OP the plugin only prints "Autoclick ativado", the rest of the function does not happen. I'm using LuckPerms and the permission is already set.

    Code (Java):
    package com.devxyz.ryuautoclick;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Animals;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Monster;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener {
       
        protected ArrayList<String> enabledPlayers;
       
        private void msgCS(ChatColor color,String msg) {
            Bukkit.getConsoleSender().sendMessage(color+msg);
        }
       
        public void onEnable() {
            msgCS(ChatColor.GREEN, "--------------------");
            msgCS(ChatColor.YELLOW, "RyuAutoclick Ativado");
            msgCS(ChatColor.GREEN, "--------------------");
           
            this.enabledPlayers = new ArrayList<String>();
           
            getServer().getPluginManager().registerEvents( this, (Plugin)this);
        }
       
        public void onDisable() {
            msgCS(ChatColor.RED, "--------------------");
            msgCS(ChatColor.YELLOW, "RyuAutoclick Desativado");
            msgCS(ChatColor.RED, "--------------------");
        }

       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("autoclick")) {
                if(sender instanceof Player == false) { //use command at the console
                    sender.sendMessage(ChatColor.RED + "Esse comando pode ser utilizado somente no server." );
                    return true;
                }else{
                    Player player = (Player) sender; //command player sender
                    String playerName = player.getName();    //command sender nick
                   
                    if(enabledPlayers.contains(playerName)) {
                        enabledPlayers.remove(playerName);//remove player from list
                        sender.sendMessage(ChatColor.RED + "Autoclick desativado.");
                        return true;
                    }else if(!enabledPlayers.contains(playerName)){
                       
                        if(player.getWorld().getName().equalsIgnoreCase("cidade")) {//specific world
                            if(player.hasPermission("rac.vip")) {//permission needed
                                enabledPlayers.add(playerName);//add player to list
                                sender.sendMessage(ChatColor.GREEN + "Autoclick ativado. ");
                                for(Entity entity : player.getWorld().getEntities()) {
                                    if(entity instanceof Monster || entity instanceof Animals) {
                                        if(entity.getLocation().distance(player.getLocation()) < 7) {
                                                ((LivingEntity) entity).damage(200.0D, player);//kill all the near mobs
                                        }
                                    }
                                }
                                return true;
                            }else if(!player.hasPermission("rac.usar")){
                                player.sendMessage(ChatColor.YELLOW + "Voce não tem permissão. Compre vip em nosso site:"+ChatColor.RED+" /site"+ChatColor.YELLOW+".");
                                return false;
                            }
                        }else if(!player.getWorld().getName().equalsIgnoreCase("cidade")){
                            player.sendMessage(ChatColor.RED + "Esse comando só pode ser utilizado no mundo de Plots.");
                            return false;
                        }
                    }
                }
            }
           
            return false;
        }
       
        @EventHandler
        public void onSpawn(CreatureSpawnEvent event) throws InterruptedException {
            Entity en = event.getEntity();
           
            if(!(en instanceof Monster) && !(en instanceof Animals)) {
                return;
            }
            if(en.getWorld().getName().equalsIgnoreCase("cidade")) {
                List<Entity> near = en.getNearbyEntities(7.0D, 7.0D, 7.0D);
                for(Entity entity : near) {
                    if(entity instanceof Player) {  
                        Player nearPlayer = (Player) entity;
                        if(enabledPlayers.contains(nearPlayer.getName())) {
                            if(en.getLocation().distance(nearPlayer.getLocation())<7) {
                                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    public void run() {
                                        ((LivingEntity)en).damage(200.0D, nearPlayer);
                                    }
                                },2*20L);
                            }
                        }
                    }
                }
                return;  
            }
            return;
        }  
    }
     
     
  15. did you mean rac.vip?


    also, just use an else instead of else if. then you can write a comment to say what the else means. why sacrifice efficiency just to make your code more readable when comments exist to make your code readable without any costs?
     
    #15 Plochem, Jun 29, 2020 at 7:25 PM
    Last edited: Jun 29, 2020 at 7:34 PM
  16. [QUOTE = "Plochem, post: 3859182, membro: 498621"] você quis dizer rac.vip?




    Além disso, basta usar um outro em vez de outro se. então você pode escrever um comentário para dizer o que o outro significa. por que sacrificar a eficiência apenas para tornar seu código mais legível quando existem comentários para torná-lo legível sem nenhum custo? [/ QUOTE]
    @Plochem Exactly, I changed the perm to test if it worked but it still didn't work. As for else, I added else if at the suggestion of a member here, it was only else
     
  17. then try adding the permission in your plugin.

    PluginManager#addPermission()
     
  18. @Plochem I did what you asked for, but you still keep giving the same thing. From what I could see, you need some other permission, even though I didn't put it in the code, because when I add the permission *
    Code (Java):
    package com.devxyz.ryuautoclick;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Animals;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Monster;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener, CommandExecutor {
       
        protected ArrayList<String> enabledPlayers;
       
        private PluginManager pm;
        public Permission acPerm = new Permission("rac.vip");
       
        private void msgCS(ChatColor color,String msg) {
            Bukkit.getConsoleSender().sendMessage(color+msg);
        }
       
        public void onEnable() {
            msgCS(ChatColor.GREEN, "--------------------");
            msgCS(ChatColor.YELLOW, "RyuAutoclick Ativado");
            msgCS(ChatColor.GREEN, "--------------------");
           
            this.enabledPlayers = new ArrayList<String>();
           
            getServer().getPluginManager().registerEvents( this, (Plugin)this);
           
            pm = this.getServer().getPluginManager();
            pm.addPermission(acPerm);
        }
       
        public void onDisable() {
            msgCS(ChatColor.RED, "--------------------");
            msgCS(ChatColor.YELLOW, "RyuAutoclick Desativado");
            msgCS(ChatColor.RED, "--------------------");
        }
       
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(commandLabel.equalsIgnoreCase("autoclick")) {
                Player player = (Player) sender;
                if(!sender.hasPermission(acPerm)) {
                    player.sendMessage(ChatColor.YELLOW + "Benefício exclusico VIP. Adquira um em nosso site: " + ChatColor.RED +  "/site" + ChatColor.YELLOW + ".");
                    return true;
                }
                if(sender instanceof Player == false) { //use command at the console
                    sender.sendMessage(ChatColor.RED + "Esse comando pode ser utilizado somente no server." );
                    return true;
                }else{
                     //command player sender
                    String playerName = player.getName();    //command sender nick
                    if(enabledPlayers.contains(playerName)) {
                        enabledPlayers.remove(playerName);//remove player from list
                        sender.sendMessage(ChatColor.RED + "Autoclick desativado.");
                        return true;
                    }else if(!enabledPlayers.contains(playerName)){
                        if(player.getWorld().getName().equalsIgnoreCase("cidade")) {//specific world
                                enabledPlayers.add(playerName);//add player to list
                                sender.sendMessage(ChatColor.GREEN + "Autoclick ativado. ");
                                for(Entity entity : player.getWorld().getEntities()) {
                                    if(entity instanceof Monster || entity instanceof Animals) {
                                        if(entity.getLocation().distance(player.getLocation()) < 7) {
                                                ((LivingEntity) entity).damage(200.0D, player);//kill all the near mobs
                                        }
                                    }
                                }
                                return true;
                        }else if(!player.getWorld().getName().equalsIgnoreCase("cidade")){
                            player.sendMessage(ChatColor.RED + "Esse comando só pode ser utilizado no mundo de Plots.");
                            return false;
                        }
                    }
                }
            }
           
            return false;
        }
       
        @EventHandler
        public void onSpawn(CreatureSpawnEvent event) throws InterruptedException {
            Entity en = event.getEntity();
           
            if(!(en instanceof Monster) && !(en instanceof Animals)) {
                return;
            }
            if(en.getWorld().getName().equalsIgnoreCase("cidade")) {
                List<Entity> near = en.getNearbyEntities(7.0D, 7.0D, 7.0D);
                for(Entity entity : near) {
                    if(entity instanceof Player) {  
                        Player nearPlayer = (Player) entity;
                        if(enabledPlayers.contains(nearPlayer.getName())) {
                            if(en.getLocation().distance(nearPlayer.getLocation())<7) {
                                Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                    public void run() {
                                        ((LivingEntity)en).damage(200.0D, nearPlayer);
                                    }
                                },2*20L);
                            }
                        }
                    }
                }
                return;  
            }
            return;
        }  
    }
     
    , it runs normally, even though I don't have OP.