Codes from other class arent working!

Discussion in 'Plugin Development' started by Wazup93, Jan 8, 2015.

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

    Wazup93

    Hey all ...
    I stopped coding for a while like 6 months and i need some help to make a thing like getters and setter as i want to do like
    public Arenas{
    public void CreatArena(){

    }
    }
    And use this command with Arenas.CreateArena();
    in another class but its not working
    This is my code
    Code:
    public class Arenas {
        private static Survival plugin;
        public Arenas(Survival plugin)    {
            Arenas.plugin = plugin;
        }
       
        public static void CreateArena(String name)    {
            plugin.getConfig().set("Arenas." + name + ".Enabled", true);
            plugin.getConfig().set("Arenas." + name + ".MinPlayers", 2);
            plugin.getConfig().set("Arenas." + name + ".MaxPlayers", 10);
            plugin.getConfig().set("Arenas." + name + ".CountDown", 20);
            plugin.getConfig().set("Arenas." + name + ".GameLength", 5);
            plugin.saveConfig();
            plugin.reloadConfig();
        }
    So what can i do to fix it ? its gives an error when using
    I'm sure theres a problem in the code itself
    Any help would be appriated
    thanks to all..
     
  2. Offline

    NathanWolf

    What errors is it giving you, specifically?
     
  3. Offline

    Wazup93

    org.bukkit.command.CommandException: Unhandled exception executing command 'Survivals' in plugin Survival v1.0
    is it helpful ?
     
  4. @Wazup93 you should post the full stack trace (in a code block, of course)
     
    NathanWolf likes this.
  5. Offline

    NathanWolf

    Yeah, the stack trace is what we want- it will tell you exactly where the error is.

    http://bukkit.org/threads/how-to-re...ubleshoot-your-own-plugins-by-yourself.32457/

    Offhand, I'm guessing the problem here is the weird mixture of static and non-static. You probably never actually create an instance of Arenas, so the "plugin" static variable is never set, resulting in an NPE on createArena.

    Why not have a non-static Arena class instead, it could take a Plugin and String name in its constructor and handle creating the config?
     
    Datdenkikniet likes this.
  6. This how i do :
    Main class :
    Code:java
    1.  
    2. public static Plugin plugin;
    3. private Arenas arenas;
    4. public Arenas arenas() {
    5. return arenas;
    6. }
    7. @Override
    8. public void onEnable() {
    9. plugin = this;
    10. arenas = new Arenas(this);
    11. }
    12. @Override
    13. public void onDisable() {
    14. plugin = null;
    15. }
    16. //In your Arenas class :
    17. private final MainClass plugin;
    18. public Arenas(MainClass i) {
    19. plugin = i;
    20. }
    21.  

    Is not exactly what you want but works .
     
  7. Offline

    Wazup93

    The Arenas class :
    Code:
    package me.DarkWolves.GolemWars;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.IronGolem;
    import org.bukkit.entity.Player;
    
    public class Arenas {
        private static GolemWars plugin;
        public Arenas(GolemWars plugin)    {
            Arenas.plugin = plugin;
        }
       
        public static void CreateArena(String name)    {
            plugin.getConfig().set("Arenas." + name + ".Enabled", true);
            plugin.getConfig().set("Arenas." + name + ".MinPlayers", 2);
            plugin.getConfig().set("Arenas." + name + ".MaxPlayers", 10);
            plugin.getConfig().set("Arenas." + name + ".CountDown", 20);
            plugin.getConfig().set("Arenas." + name + ".GameLength", 5);
            plugin.saveConfig();
            plugin.reloadConfig();
        }
    }
    The Main Class :
    Code:
    package me.DarkWolves.GolemWars;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class GolemWars extends JavaPlugin implements Listener{   
       
        public static GolemWars plugin;
        Logger logger = Logger.getLogger("Minecraft");
           
        ChatColor Red = ChatColor.DARK_RED;
        ChatColor Black = ChatColor.BLACK;
        ChatColor Gray = ChatColor.GRAY;
       
        String GWsTag = Black + "[ " + Red + "Golem" + Gray + "Wars" + Black + " ]" + Gray;
        String GWsBanner = Black + "----------" + GWsTag + Black + "----------";
       
        @Override
        public void onEnable()    {
            getServer().getPluginManager().registerEvents(new GWsListener(this), this);
               
            new Arenas(this);
           
            Bukkit.getConsoleSender().sendMessage(Gray + "----------" + GWsTag + Gray + "----------");
            Bukkit.getConsoleSender().sendMessage(GWsTag + " Has been Enabled..!");
            Bukkit.getConsoleSender().sendMessage(Gray + "-----------------------------------------");
           
            saveDefaultConfig();
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        @Override
        public void onDisable()    {
            saveConfig();
            Bukkit.getConsoleSender().sendMessage(Gray + "----------" + GWsTag + Gray + "----------");
            Bukkit.getConsoleSender().sendMessage(GWsTag + " Has been Disabled..!");
            Bukkit.getConsoleSender().sendMessage(Gray + "-----------------------------------------");
        }
           
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player p = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("GWs") || cmd.getName().equalsIgnoreCase("GolemWars"))    {
                if(args.length == 0)    {
                    //Commands list
                    return true;
                }else if(p.isOp() && args[0].equalsIgnoreCase("CreateArena"))    {
                    if(args.length < 2)    {
                        p.sendMessage(GWsTag + " The right usage is " + Red + "/Gws CreateArena (Arena Name)" + Gray + "..!");
                        return true;
                    }
                    Arenas.CreateArena(args[1]);
                    p.sendMessage(GWsTag + " Arena " + Red + args[1] + Gray + " Has been created..!");
                }else if(p.isOp() && args[0].equalsIgnoreCase("SetArena"))    {
                    Arenas.SetArena(p, args[1]);
                }
            }
    }
     
  8. please send your main class
     
  9. Offline

    1Rogue

    You cast to player before checking if the CommandSender is one.
     
Thread Status:
Not open for further replies.

Share This Page