Config on sign help?

Discussion in 'Plugin Development' started by MattexFilms, Sep 16, 2012.

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

    MattexFilms

    I am trying to pull a value from the config file onto a sign when a certain phrase is typed on the first line.

    PHP:
    package com.tammcd.troubleinminecraft;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
     
    public class 
    SignListener implements Listener{
     
        public static 
    Main plugin;
       
        public 
    SignListener(Main instance){
            
    plugin instance;
        }
       
        @
    EventHandler
        
    public void onSignCreate(SignChangeEvent sign){
            
    Player player sign.getPlayer();
            if(
    sign.getLine(0).equalsIgnoreCase("example")){
                
    player.sendMessage(ChatColor.GOLD "The sign has been changed");
                
    sign.setLine(0, .getConfig().getString("motd"));
     
            }
        }
    }
    But I get an error under the .getConfig() part. Saying: "The method .getConfig() is undefined for the type SignListener."

    Any ideas how to get it working?
     
  2. Offline

    CoKoC

    Code:
    sign.setLine(0, .getConfig().getString("motd"));
    Either add a "this" pointer in front of the ".getConfig()" method or remove the dot. Local methods don't need a dot. Becomes :

    Code:
    sign.setLine(0, this.getConfig().getString("motd"));
    or
    Code:
    sign.setLine(0, getConfig().getString("motd"));
     
  3. Offline

    MattexFilms

    When I add the 'this' or remove the '.' I still get the same error saying "The method .getConfig() is undefined for the type SignListener."
     
  4. Offline

    CRAZYxMUNK3Y

    You need to point it to where you originally defined the getConfig(), it is probably in your main, so use;

    Main:
    Code:java
    1.  
    2. public void onEnable(){
    3. FileConfiguration config = getConfig();
    4. }
    5.  


    Listener:
    Code:java
    1.  
    2. sign.setLine(0, config.getString("motd"));
    3.  


    Or something like that.
     
  5. Offline

    MattexFilms

    Okay I have added the FileConfiguration config = getConfig(); line to the Main class and change the SignListener line.

    I get an error below the word config on this line:

    Code:
     sign.setLine(0, config.getString("motd"));
    Saying: "config cannot be resolved" and there are fixes such as create local variable 'config' and create field 'config'
     
  6. Offline

    CRAZYxMUNK3Y

    plugin.config.getString();

    You need to tell it that config is in the main class, which is given the variable of plugin (If that makes sense).

    Sorry.
     
  7. Offline

    MattexFilms

    Yes that makes sense, I have added plugin to that line to it is now:

    Code:
    sign.setLine(0, plugin.config.getString("motd"));
    But I get an error under the config still, this time saying: config cannot be resolved or is not a field. And there are two fixes: create type 'config' in type 'Main' and create constant 'config' in type 'Main'

    When ever I try and use either one of the fixes it gets rid of the error under config and the.getString() part has an error saying: The method getString(String) is undefined for the type Object and the fix is: Add cast to 'plugin.config'.
     
  8. Offline

    CRAZYxMUNK3Y

    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
     
    public class SignListener implements Listener{
     
        String motd = plugin.getConfig().getString("motd");
     
        public static Main plugin;
     
        public SignListener(Main instance){
            plugin = instance;
        }
     
        @EventHandler
        public void onSignCreate(SignChangeEvent sign){
            Player player = sign.getPlayer();
            if(sign.getLine(0).equalsIgnoreCase("example")){
                player.sendMessage(ChatColor.GOLD + "The sign has been changed");
                sign.setLine(0, motd);
     
            }
        }
    }
    
    I have it different in my plugin, can't remember how though.

    Way above works though, tested it. You can do it with or without the motd variable.
     
  9. Offline

    MattexFilms

    I tried doing that but I get a lovely error in my console:

    [SEVERE] Could not load 'plugins\TroubleInMinecraft.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NullPoitnerException at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin<JavaPluginLoader.java:153>

    The error does reference some of my classes:

    Caused by: java.lang.NullPointerException
    at com.tammcd.troubleinminecraft.SignListsner.<init><SignListsner.java:1>
    at com.tammcd.troubleinminecraft.Main<init><Main.java:54>

    This is what is on line 54 of the Main class:
    PHP:
    public final SignListener sl = new SignListener(this);
     
  10. Offline

    CRAZYxMUNK3Y

    Try;
    Code:
    public SignListener sl;
     
    public void onEnable(){
    sl = new SignListener(this);
    //Everything else here
    }
    
     
  11. Offline

    MattexFilms

    I did that, I then got another error in the console:

    [SEVERE] Error occurred while enabling Trouble In Minacraft <Is it up to date?>
    java.lang.NullPointerException
    at com.tammcd.troubleinminecraft.SignListener.<init><SignListener.java:11>
    at com.tammcd.troubleinminecraft.Main.onEnable<Main.java:71>

    This code is at line 11 in the SignListener class:
    PHP:
    String motd plugin.getConfig().getString("motd");
    This code is at line 71 in the Main class:
    PHP:
    sl = new SignListener(this);
     
  12. Offline

    CRAZYxMUNK3Y

    Mind posting everything then? (Both classes), will take a better look at it with eclipse.
     
  13. Offline

    MattexFilms

    Sure.

    Here is the main class:

    PHP:
    Here is the SignListener class.

    PHP:
    package com.tammcd.troubleinminecraft;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
     
    public class 
    SignListener implements Listener{
     
            
    String motd plugin.getConfig().getString("motd");
     
            public static 
    Main plugin;
     
            public 
    SignListener(Main instance){
                
    plugin instance;
            }
     
            @
    EventHandler
            
    public void onSignCreate(SignChangeEvent sign){
                
    Player player sign.getPlayer();
                if(
    sign.getLine(0).equalsIgnoreCase("example")){
                    
    player.sendMessage(ChatColor.GOLD "The sign has been changed");
                    
    sign.setLine(0motd);
                }
            }
    }
    Any luck?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  14. Offline

    CRAZYxMUNK3Y

    The only error i am getting now is "NoClassDefFound - SQLite"

    Main (open)

    Code:
    package com.tammcd.troubleinminecraft;
     
    import java.io.File;
    import java.io.IOException;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Random;
    import java.util.logging.Logger;
     
    import lib.PatPeter.SQLibrary.SQLite;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    //import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
        public File configFile;
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Main plugin;
        public SQLite db;
        private boolean gameRunning = false;
        //public final SignListener sl = new SignListener(this);
        public final PlayerHitListener phl = new PlayerHitListener(this);
        public final Location[] warpLocations = new Location[100];
        public final String[] warpName = new String[100];
        public int warpCounter = 0;
        //public Object config;
        public Object config;
        File configDir;
     
        public SignListener sl;
     
        // STARTUP
        public void onEnable() {
            sl = new SignListener(this);
            this.getDataFolder().mkdir();
            this.saveDefaultConfig();
     
            
     
            sqlConnection();
            sqlTableCheck();
            logConstant();
     
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(sl, this);
            pm.registerEvents(phl, this);
     
            PluginDescriptionFile pdfFile = this.getDescription();
     
            this.getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled!");
     
           /* try {
                Metrics metrics = new Metrics(this);
                metrics.start();
                } catch (IOException e) {
            }*/
     
            getServer().getPluginManager().registerEvents(new Listener() {
                @SuppressWarnings("unused")
                @EventHandler
                public void playerJoin(PlayerJoinEvent event) {
                    event.getPlayer().sendMessage(ChatColor.DARK_PURPLE + Main.this.getConfig().getString("motd"));
                    putPlayerConstant(event.getPlayer().getName());
                }
         
                @SuppressWarnings("unused")
                @EventHandler
                public void playerLeave(PlayerQuitEvent event) {
                    String playerName = event.getPlayer().getName();
                    playerLeaveGame(playerName);
                }
            }, this);
        }
     
        // SHUTDOWN
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " (version: " + pdfFile.getVersion() + ") has been disabled!");
            db.close();
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            String playerName = player.getName();
     
            int alreadyPlayer = 0;
            try {
                ResultSet isPlayerInGame = db.query("SELECT EXISTS(SELECT * FROM game WHERE playername='" + playerName + "')");
                while (isPlayerInGame.next()) {
                    alreadyPlayer = alreadyPlayer + isPlayerInGame.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            // START (Refer to EXPLINATIONS in the to do list) NUMBER 1
     
            if (label.equalsIgnoreCase("timhelp")) {
                if (player.isOp()) {
                    // Might set the help as a list in config and pull it from
                    // there. Easier to edit when a plugin.
                    player.sendMessage(ChatColor.GOLD + "Trouble in Minecraft Help " + ChatColor.RED + "(Admin)" + ChatColor.RED + ":");
                    player.sendMessage(ChatColor.GOLD + "----------------------------------------------");
                    player.sendMessage(ChatColor.GOLD + "/join - Player will join ongoing match.");
                    player.sendMessage(ChatColor.GOLD + "/stats - Look at your stats (Not implemented)");
                    player.sendMessage(ChatColor.GOLD + "/timfo - Displays the version of TIM.");
                    player.sendMessage(ChatColor.GOLD + "/timspawn - Sets the spawn for matches.");
                    player.sendMessage(ChatColor.GOLD + "/timdeath - Sets the death location.");
                    player.sendMessage(ChatColor.GOLD + "/timarrest - Sets the jail location.");
     
                } else {
                    player.sendMessage(ChatColor.GOLD + "Trouble in Minecraft Help (Reg):");
                    player.sendMessage(ChatColor.GOLD + "----------------------------------------------");
                    player.sendMessage(ChatColor.GOLD + "/join - Player will join ongoing match.");
                    player.sendMessage(ChatColor.GOLD + "/stats - Look at your stats (Not implemented)");
                }
            }
     
            if (label.equalsIgnoreCase("timspawn"))
                if (player.isOp()) {
                    if (args.length == 0) {
                        player.sendMessage(ChatColor.RED + "/timspawn <spawnname>");
                    } else {
                        Location location = player.getLocation();
                        if (!(warpCounter > 100)) {
                            warpLocations[warpCounter] = location;
                            warpName[warpCounter] = args[0];
                            warpCounter++;
                            player.sendMessage(ChatColor.RED + "Spawn " + args[0] + " has been set.");
                        } else {
                            player.sendMessage(ChatColor.RED + "Spawn limmit exceeded, unable to create spawn.");
                        }
                    }
     
                } else {
                    player.sendMessage(ChatColor.GOLD + "You do not have the permissions to use that command.");
                }
     
            if (label.equalsIgnoreCase("timdeath")) {
                if (player.isOp()) {
                    player.sendMessage(ChatColor.RED + "Death point has been set.");
                } else {
                    player.sendMessage(ChatColor.GOLD + "You do not have the permissions to use that command.");
                }
                if (label.equalsIgnoreCase("timarrest")) {
                    if (player.isOp()) {
                        player.sendMessage(ChatColor.RED + "Jail spawn has been set.");
                    } else {
                        player.sendMessage(ChatColor.GOLD + "You do not have the permissions to use that command.");
                    }
                }
            }
     
            if (label.equalsIgnoreCase("timfo")) {
                PluginDescriptionFile pdfFile = this.getDescription();
                player.sendMessage(ChatColor.RED + "Trouble In Minecraft Version " + pdfFile.getVersion());
                player.sendMessage(ChatColor.RED + "Code: Tamfoolery, Emmsii");
            }
     
            if (label.equalsIgnoreCase("stats")) {
                player.sendMessage(ChatColor.GOLD + "-----Stats-----");
                player.sendMessage(ChatColor.GOLD + "Kills: 0");
                player.sendMessage(ChatColor.GOLD + "Deaths: 0");
                player.sendMessage(ChatColor.GOLD + "Arrests: 0");
     
            }
     
            if (label.equalsIgnoreCase("leave")) {
                player.sendMessage(ChatColor.GOLD + "You have left the game.");
                // USE removePlayer method.
            }
            // END
     
            if (label.equalsIgnoreCase("join")) {
                if (alreadyPlayer != 1) {
                    if (gameRunning) {
                        player.sendMessage(ChatColor.GOLD + "You have joined a game in progress.");
                        Random object = new Random();
                        int test;
                        for (int counter = 1; counter <= 1; counter++) {
                            test = 1 + object.nextInt(3);
                            if (test == 1) {
                                player.sendMessage(ChatColor.GREEN + "You are innocent.");
                            } else if (test == 2) {
                                player.sendMessage(ChatColor.RED + "You are a roughian");
                            } else if (test == 3) {
                                player.sendMessage(ChatColor.GOLD + "You are a sheriff");
                            }
                        }
                    } else {
                        player.sendMessage(ChatColor.GOLD + "You have joined! The game will start shortly...");
                        Random object = new Random();
                        int test;
                        for (int counter = 1; counter <= 1; counter++) {
                            test = 1 + object.nextInt(3);
                            if (test == 1) {
                                player.sendMessage(ChatColor.GREEN + "You are innocent.");
                            } else if (test == 2) {
                                player.sendMessage(ChatColor.RED + "You are a roughian");
                            } else if (test == 3) {
                                player.sendMessage(ChatColor.GOLD + "You are a sheriff");
                            }
                        }
                    }
                    addPlayer(player, playerName);
                    if (gamePlayerCount() == 1) {
                        getServer().broadcastMessage("There is now " + gamePlayerCount() + " player in game.");
                    } else {
                        getServer().broadcastMessage("There are now " + gamePlayerCount() + " players in game.");
                    }
     
                } else {
                    player.sendMessage(ChatColor.RED + "You have already joined the game!");
                }
            }
     
            return false;
        }
     
        public void addPlayer(Player player, String playerName) {
            int playerCount = 0;
     
            try {
                ResultSet totalPlayers = db.query("SELECT COUNT(*) FROM game");
                while (totalPlayers.next()) {
                    playerCount = playerCount + totalPlayers.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
     
            db.query("INSERT INTO game (id, playername, isSheriff, isDead, isRoughian) VALUES(" + (playerCount + 1) + ", '" + playerName + "', 'false', 'false', 'false')");
        }
     
        public void removePlayer(Player player, String playerName) {
            playerLeaveGame(playerName);
        }
     
        public int gamePlayerCount() {
            int playerCount = 0;
     
            try {
                ResultSet totalPlayers = db.query("SELECT COUNT(*) FROM game");
                while (totalPlayers.next()) {
                    playerCount = playerCount + totalPlayers.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
     
            return (playerCount);
        }
     
        public void sqlConnection() {
            db = new SQLite(this.getLogger(), "Trouble in Minecraft", "game", this.getDataFolder().getPath());
     
            try {
                db.open();
            } catch (Exception e) {
                this.getLogger().info(e.getMessage());
                getPluginLoader().disablePlugin(plugin);
            }
        }
     
        public void sqlTableCheck() {
            if (db.checkTable("game") && db.checkTable("constant")) {
                return;
            } else {
                db.query("CREATE TABLE game (id INT PRIMARY KEY, playername VARCHAR(255), isSheriff VARCHAR(5), isDead VARCHAR(5), isRoughian VARCHAR(5))");
                this.getLogger().info("[Trouble in Minecraft] 'game' table has been created!");
                db.query("CREATE TABLE constant (id INT PRIMARY KEY, playername VARCHAR(255), karma INT, playCount INT, sheriffCount INT, roughianCount INT, deathCount INT)");
                this.getLogger().info("[Trouble in Minecraft] 'constant' table has been created!");
            }
        }
     
        public void putPlayerConstant(String playerName) {
            int alreadyPlayer = 0;
            int playerCount = 0;
     
            try {
                ResultSet isPlayerInGame = db.query("SELECT EXISTS(SELECT * FROM constant WHERE playername='" + playerName + "')");
                while (isPlayerInGame.next()) {
                    alreadyPlayer = alreadyPlayer + isPlayerInGame.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
     
            if (alreadyPlayer != 1) {
                try {
                    ResultSet totalPlayers = db.query("SELECT COUNT(*) FROM constant");
                    while (totalPlayers.next()) {
                        playerCount = playerCount + totalPlayers.getInt(1);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
     
                db.query("INSERT INTO constant (id, playername, karma, playCount, sheriffCount, roughianCount, deathCount) VALUES(" + (playerCount + 1) + ", '" + playerName + "', 1000, 0, 0, 0, 0)");
                this.getLogger().info("Player " + playerName + " has been added to constant.db");
            } else {
                this.getLogger().info("Player " + playerName + " has been found in constant.db, not adding.");
            }
            this.getLogger().info("There are " + (playerCount + 1) + " players stored in constant.db.");
        }
     
        public void logConstant() {
            int playerCount = 0;
            try {
                ResultSet totalPlayers = db.query("SELECT COUNT(*) FROM constant");
                while (totalPlayers.next()) {
                    playerCount = playerCount + totalPlayers.getInt(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            this.getLogger().info("There are " + playerCount + " players stored in constant.db.");
        }
     
        // Starting of the stick Sheriff's get.
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            int itemId = player.getItemInHand().getType().getId();
            if (itemId == 280) {
                arrestPlayer();
            }
        }
     
        public void arrestPlayer() {
     
        }
     
        public void playerDeath() {
     
        }
     
        public void playerLeaveGame(String playerName) {
            db.query("DELETE FROM game WHERE playername='" + playerName + "'");
            if (gamePlayerCount() == 1) {
                getServer().broadcastMessage("Player " + playerName + " left the game, leaving " + gamePlayerCount() + " player left!");
                } else {
                getServer().broadcastMessage("Player " + playerName + " left the game, leaving " + gamePlayerCount() + " players left.");
            }
        }
    }
    


    SignListener (open)

    Code:
    package com.tammcd.troubleinminecraft;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.SignChangeEvent;
    
    public class SignListener implements Listener {
    
        public static Main plugin;
    
        public SignListener(Main instance) {
            plugin = instance;
        }
        
        @EventHandler
        public void onSignCreate(SignChangeEvent sign) {
            Player player = sign.getPlayer();
            if (sign.getLine(0).equalsIgnoreCase("example")) {
                player.sendMessage(ChatColor.GOLD + "The sign has been changed");
                sign.setLine(0, plugin.getConfig().getString("motd"));
            }
        }
    }
    
     
  15. Offline

    MattexFilms

    Oh yeah we are using a msql database. I think my friend, who codes anything to do with the database referenced a msql jar.
     
Thread Status:
Not open for further replies.

Share This Page