Solved player.getWorld().getName vs player.getWorld()

Discussion in 'Plugin Development' started by Xp10d3, Jan 23, 2020.

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

    Xp10d3

    I hope this isn't a dumb question, but if I am getting a list of worlds from a config (List<String> targetWorld = targetPlugin.getStringList(key);) and trying to sense whether the world the player is in is equal to that list and I'm looping that list, I obviously get an error if I try to do getWorld().getName() but don't if I do .getWorld(). What is the difference between the two? If I am checking whether the world is not equal to the list will .getWorld() work just fine? I have pasted my class if it helps.
    Code:
    package play.corelia.online;
    
    import java.util.List;
    
    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.configuration.ConfigurationSection;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor {
       
        public Commands(Core core) {
            this.core = core;
            Bukkit.getPluginCommand("dp").setExecutor(this);
        }
       
        private Core core;
       
        FileConfiguration config = core.getConfig();
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You must be a player to send commands!");
            }
            Player player = (Player) sender;
            ConfigurationSection targetPlugin = config.getConfigurationSection("location.world");
            for (String key : targetPlugin.getKeys(false)) {
                List<String> targetWorld = targetPlugin.getStringList(key);   
                if (!player.hasPermission("dp.bypasscmd") && player.getWorld() != targetWorld) {
                    if (cmd.getName().equalsIgnoreCase("dp") && player.hasPermission("dp.all")) {
                        if (args.length == 1) {
                            if (args[0] == "disableall" && player.hasPermission("dp.disableall")) {
                                Bukkit.getServer().getPluginManager().disablePlugins();
                                player.sendMessage(ChatColor.GREEN + "Successfully disabled all plugins!");
                            } else if (!player.hasPermission("dp.disableall")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.disableall! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            } else if (args[0] == "enableall" && player.hasPermission("dp.enableall")) {
                                Bukkit.getServer().reload();
                                player.sendMessage(ChatColor.GREEN + "Successfully enabled/reloaded!");
                            } else if (!player.hasPermission("dp.enableall")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.enableall! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            } else if (args[0] == "help" && player.hasPermission("dp.help")) {
                                player.sendMessage(
                                        ChatColor.DARK_GREEN + "|COMMANDS| \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp disableall | " + ChatColor.RESET + ChatColor.AQUA + "Disables all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp enableall | " + ChatColor.RESET + ChatColor.AQUA + "Reloads the server/enables all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp help | " + ChatColor.RESET + ChatColor.AQUA + "Shows the help menu. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "/dp permissions | " + ChatColor.RESET + ChatColor.AQUA + "Shows the permissions for each command."
                                        );
                            } else if (!player.hasPermission("dp.help")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.help! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            } else if (args[0] == "permissions" && player.hasPermission("dp.permissions")) {
                                player.sendMessage(
                                        ChatColor.DARK_GREEN + "|PERMISSIONS \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.all | " + ChatColor.RESET + ChatColor.AQUA + "Gives access to all commands. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.disableall | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to disabling all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.enableall | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to reload the server or enable all plugins. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.help | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to the help menu. \n" +
                                        ChatColor.BOLD + ChatColor.BLUE + "dp.permissions | " + ChatColor.RESET + ChatColor.AQUA + "Allows access to the permissions menu."
                                        );
                            } else if (!player.hasPermission("dp.permissions")) {
                                player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.permissions! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                            }
                        } else {
                            player.sendMessage(ChatColor.RED + "Incorrect usage! Do /dp help to see the commands.");
                        }
                    } else if (!player.hasPermission("dp.all")) {
                        player.sendMessage(ChatColor.RED + "You do not have the permission " + ChatColor.BOLD + "dp.all! " + ChatColor.RESET + ChatColor.RED + "Contact Administrators if you think this is a bug.");
                    }
                } else {
                    player.sendMessage(ChatColor.RED + "You don't have permission to talk in this world! Contact Administrators if you think this is a bug.");
                }
            }
            return false;
        }
    
    }
    
     
  2. Offline

    KarimAKL

    @Xp10d3 "player.getWorld() != targetWorld" will always return true.
    You're trying to compare a world object with a list object.
    You need to use "targetWorld.contains(player.getWorld())"
     
  3. Offline

    Xp10d3

    Alright. Thanks. I'll try that :)
     
  4. Offline

    Strahan

    Well, if targetWorld is a List<String> of world names you'd want the contains check to be for player.getWorld().getName() otherwise it's checking if the World object exists in a String list.

    Also, this is personal preference, but I'd consider restructuring that to something cleaner like a switch and do negative check/return to cut down on the indentation. You also should validate your data before operating on it, as anything pulled external could be problematic. For example, you set ConfigurationSection targetPlugin = config.getConfigurationSection("location.world"); then immediately loop on the getKeys, but targetPlugin could potentially be null, crashing it.
     
    KarimAKL likes this.
  5. Offline

    Xp10d3

    Thanks; that was helpful :) Yeah it crashed the server twice already xD
     
    Last edited: Jan 25, 2020
Thread Status:
Not open for further replies.

Share This Page