Solved Get player world and put it in the config.

Discussion in 'Plugin Development' started by Bladibudies, Nov 21, 2015.

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

    Bladibudies

    Hello! I'm making a plugin and need help.
    I want that when a player use /getworld, take the world where the player is and place it in the configuration.

    Thanks.
     
  2. Offline

    mcdorli

    Player#getWorld()
    http://wiki.bukkit.org/Configuration_API_Reference
     
  3. Offline

    Bladibudies

    @mcdorli I'm trying to make all things that there says, but it doesn't work.
    Here's my code:
    Code:
    if (cmd.getName().equalsIgnoreCase("getworld") && sender instanceof Player){
              this.getConfig().set("worlds", List);
    And the "List" is:
    Code:
    List<World> List= Arrays.asList(p.getWorld());
     
  4. @Bladibudies Why create a new list just for that one world? Also please follow naming conventions.
     
  5. Offline

    Bladibudies

    Okay. I already resolved to place the world.
    Now I need to know how I can check if the player is in the world that says in the config.

    Thanks.
     
  6. Offline

    mcdorli

    player.getWorld()

    and then check if it equals with the string in the config (getConfig().getString(String path))
     
  7. Offline

    Bladibudies

    I'm making this:
    Code:
    for (String world : this.getConfig().getStringList("worlds"))
    
        if (p.getWorld() == Bukkit.getWorld(world)){ CODE
    And it doesn't work.
     
  8. Offline

    mcdorli

    .equals() instead of ==, because == is only usable for objects, learn java
     
  9. Offline

    Bladibudies

    Ok, sorry.

    It doesn't work

    90% of them are like 10 year olds who are asking for class, kill rank, pvp, or hunger game plugins

    like I said, the majority of those requests are little kids or stupid people asking for plugins that have been created in one form factor or another.
     
    Last edited by a moderator: Nov 30, 2015
  10. Offline

    Scimiguy

    @mcdorli
    Wrong way

    == is for enums and primitives mostly
     
  11. Offline

    mcdorli

    Yes I messed it up I wanted to say that
     
  12. Offline

    Bladibudies

    I still need help :(
     
  13. Offline

    mcdorli

    Post a screenshot of your screen while you're looking in the config file. I need to whole screen
     
  14. Offline

    Bladibudies

  15. Offline

    mcdorli

    Please post the error code and the full class
     
  16. Offline

    Bladibudies

    @mcdorli
    Here is the console error: http://pastebin.com/655rNVJU
    And, the full class:
    - NoFall class:
    Code:
    package bladibudies.nofall.main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import bladibudies.nofall.main.Main;
    
    public class NoFall implements Listener{
      
        private Main plugin;
        public void NoFallMainImport(Main pl){
            plugin = pl;
        }
      
      
        @EventHandler
        public void onFallDamage(EntityDamageEvent event){
            Player p = (Player) event.getEntity();
            for (String mundo : this.plugin.getConfig().getStringList("worlds"))
                if (p.getWorld().equals(Bukkit.getWorld(mundo))){
                    if (p.hasPermission("nofallplus.nofall")){
                        if(event.getEntity() instanceof Player && event.getCause() == DamageCause.FALL)
                            event.setCancelled(true);
                }
            } else {}
        }
    }
    - Main class:
    Code:
    package bladibudies.nofall.main;
    
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin{
      
        public Permission Admin = new Permission("nofallplus.admin");
        public Permission NoFall = new Permission("nofallplus.nofall");
        private static Main instance;
        public void log(String string) {
            System.out.println(string);
            }
        public static Main getInstance(){
            return instance;
        }
      
        @Override
        public void onEnable(){
            getLogger().info("has been enabled successfully!");
            instance = this;
            PluginManager pm = getServer().getPluginManager();
            pm.addPermission(Admin);
            pm.addPermission(NoFall);
            this.saveDefaultConfig();
            this.getConfig().options().copyDefaults(true);
            this.saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(new NoFall(), this);
        }
      
        @Override
        public void onDisable(){
            getLogger().info("has been disabled successfully");
            instance = null;
          
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[]args){
        Player p = (Player) sender;
        String setworldsuccesfully = this.getConfig().getString("messages.world-set").replaceAll("&", "§");
        String nopermissionmessage = this.getConfig().getString("messages.no-permission").replaceAll("&", "§");
        String reloadmessage = this.getConfig().getString("messages.config-reload").replaceAll("&", "§");
        String prefix = "§7[§6§lNoFallPlus§7] ";
      
        if (cmd.getName().equalsIgnoreCase("nfp") && sender instanceof Player){
            int len = args.length;
            if(len == 1){
                if(args[0].equalsIgnoreCase("setworld")){
                    if(p.hasPermission("nofallplus.admin")){
                        this.getConfig().set("worlds", Arrays.asList(p.getWorld().getName()));
                        this.saveConfig();
                        p.sendMessage(prefix + setworldsuccesfully);
                    } else {
                        p.sendMessage(prefix + nopermissionmessage);
                    }
                } if(args[0].equalsIgnoreCase("reload")){
                    if(p.hasPermission("nofallplus.admin")){
                        this.reloadConfig();
                        this.saveConfig();
                        p.sendMessage(prefix + reloadmessage);
                    } else{
                        p.sendMessage(prefix + nopermissionmessage);
                    }}
            } else{
                p.sendMessage("§6§l§m--------§6§l[NoFallPlus]§6§l§m--------");
                p.sendMessage("§8§l>>§7/nfp setworld - §fSet the no fall damage world.");
                p.sendMessage("§8§l>>§7/nfp reload - §fReload the config.");
            }}
        return true;
    }}
     
  17. Offline

    mcdorli

    You never actually set the plugin field in the NoFall class, create a constructor, and request an instance of it.
     
  18. Offline

    Xerox262

    Player p = (Player) event.getEntity();

    Do not blindly cast your entity to a player... If any mob that is not a player gets damaged (punched, falls or even if it gets caught in a block) you will have errors on your hands.

    and you do not need to use a for loop for this. You could just do plugin.getConfig().getStringList("worlds").contains(Player#getWorld().getName())
     
    Last edited: Nov 24, 2015
  19. Offline

    Bladibudies

Thread Status:
Not open for further replies.

Share This Page