Development Assistance Need help with chat plugin

Discussion in 'Plugin Help/Development/Requests' started by yourmaster01, Mar 1, 2015.

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

    yourmaster01

    Guys I am making a plugin that needs to look at all chat sent and first check if that player is in a database and if so do something before sending the chat out to everyone. if you can help i could use it ASAP. thanks. Need more detail please ask.

    ok I have done some more work but getting an error can anyone please help me? http://prntscr.com/6bg66e
    Code:
    package me.unrealdimension.CompanyChat;
    
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.UUID;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import code.husky.Database;
    import code.husky.mysql.MySQL;
    
    public class CompanyChat extends JavaPlugin implements Listener {
        private Database db;
        public static CompanyChat plugin;
       
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
            // config
            getConfig().options().copyDefaults(true);
            saveConfig();
    
            PluginManager pm = getServer().getPluginManager();
    
            if (pm.getPlugin("CompanyPlugin").isEnabled()) {
                getLogger()
                        .severe("[CompanyChat] The plugin CompanyPlugin was found successfully");
            } else {
                getLogger().severe("The plugin CompanyPlugin could not be found!");
                this.onDisable();
            }
    
            // initialize database
            this.db = new MySQL(this, "localhost", "3306", "companies_test", "root", "");
    
            // test database connection
            try {
                db.openConnection();
            } catch (Exception e) {
                getLogger().severe("Connection to MySQL database failed!");
                e.printStackTrace();
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
    
            // initialize command
            //getCommand("CompanyChat").setExecutor(new Commands(this));
    
            // done
            getLogger().info("v0.1-dev Enabled!");
        }
    
        @Override
        public void onDisable() {
            // chat save list
            saveConfig();
            getLogger().info("CompanyChat has been disabled!");
        }
    
        //doing this because this is for minecraft version 1.2.5 current error http://prntscr.com/6bg66e
        @EventHandler
        public void onChat(PlayerChatEvent event) {
            Player player = event.getPlayer();
           
            player.sendMessage("GOT IT");
           
            UUID plrId = player.getUniqueId();
           
            String message = event.getMessage();
           
            if (CompanyCheck(plrId)) {
                String cName = companyName(plrId);
                if(cName != null){
                    plugin.getServer().broadcastMessage(ChatColor.GOLD+"["+cName+"] "+message);
                }else{
                    plugin.getServer().broadcastMessage(ChatColor.RED+message);
                }
               
            }else{plugin.getServer().broadcastMessage(ChatColor.BLUE+message);}
    
        }
       
        public boolean CompanyCheck(UUID player){
            //see if user is in a company
            PreparedStatement ps = null;
            ResultSet rs = null;
           
            String plr = player.toString();
           
            try {
                String sql = "SELECT `player_uid` FROM `employees` WHERE `player_uid` = ?";
                ps = db.openConnection().prepareStatement(sql);
                ps.setString(1, plr);
                rs = ps.executeQuery();
                return rs.next();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                db.close(ps, rs);
            }
        }
       
        public String companyName(UUID player){
            //get company name
            PreparedStatement ps = null;
            ResultSet rs = null;
           
            String id = "0";
            String plr = player.toString();
           
            try {
                String sql = "SELECT `company_id` FROM `employees` WHERE `player_uid` = ?";
                ps = db.openConnection().prepareStatement(sql);
                ps.setString(1, plr);
                rs = ps.executeQuery();
                id = sql;
            } catch (Exception e) {
                e.printStackTrace();
               
            } finally {
                db.close(ps, rs);
            }
           
            try {
                String sql = "SELECT `name` FROM `companys` WHERE `id` = ?";
                ps = db.openConnection().prepareStatement(sql);
                ps.setString(1, id);
                rs = ps.executeQuery();
                return sql;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                db.close(ps, rs);
            }
           
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            return false;
        }
       
    }// end of main
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  2. Offline

    crolemol

    @yourmaster01
    as first step in your onEnable call: this.plugin= this;
     
  3. @yourmaster10 what is the error? can you post it on pastebin?
     
  4. Offline

    crolemol

  5. Im stumped xD
     
Thread Status:
Not open for further replies.

Share This Page