bungee ban-plugin

Discussion in 'BungeeCord Plugin Development' started by eliteSCHW31N, May 28, 2017.

  1. hey there i have some Problems with my ban-plugin that i made for bungeecord:
    the tempban function:

    at me.eliteSCHW31N.PRban.banunit.getUnitsAsString(banunit.java:41)
    at me.eliteSCHW31N.commands.COMMAND_ban.execute(COMMAND_ban.java:53)

    and the perma ban function:

    at me.eliteSCHW31N.PRban.banmanager.getReaminingTime(banmanager.java:77)
    at me.eliteSCHW31N.PRban.banmanager.ban(banmanager.java:38 )
    at me.eliteSCHW31N.commands.COMMAND_ban.execute(COMMAND_ban.java:34)

    and now the classes

    command_ban:
    Code (Text):
    package me.eliteSCHW31N.commands;

    import java.util.List;

    import me.eliteSCHW31N.PRban.*;
    import net.md_5.bungee.api.CommandSender;
    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.chat.TextComponent;
    import net.md_5.bungee.api.connection.ProxiedPlayer;
    import net.md_5.bungee.api.plugin.Command;

    public class COMMAND_ban extends Command{

        public COMMAND_ban(String name) {
            super(name);
            // TODO Auto-generated constructor stub
        }

        [USER=32110]@Override[/USER]
        public void execute(CommandSender sender, String[] args) {
            if(sender instanceof ProxiedPlayer){
             
                ProxiedPlayer p = (ProxiedPlayer) sender;
             
                if(!p.hasPermission("PRban.ban")){
                 
                }else{
                if(args.length == 2){
                    if(banmanager.isBanned(ProxyServer.getInstance().getPlayer(args[0]).getUniqueId().toString())){
                        p.sendMessage(new TextComponent("[PR-Ban] "+args[0]+" ist bereits gebannt"));
                    }
                    String playername = args[0];
                    String reason = args[1];
                    banmanager.ban(ProxyServer.getInstance().getPlayer(playername).getUniqueId().toString(), playername, reason, -1);
                    p.sendMessage(new TextComponent("[PR-ban] Du hast "+args[0]+" für" + args[1] + "permanent gebannt"));
                    return;
                 
                }else if(args.length == 4){
                    long value;
                    String playername = args[0];
                    try{
                        value = Integer.valueOf(args[3]);
                    }catch(NumberFormatException e){
                        p.sendMessage(new TextComponent("[PR-ban]Die zeitangabe muss eine Zahl sein"));
                        return;
                    }
                    if(value >= 500){
                        p.sendMessage(new TextComponent("[PR-ban]Die Zahl muss unter 500 liegen"));
                        return;
                    }
                    String unitString = args[2];
                    String reason = args[1];
                    List<String> unitList = banunit.getUnitsAsString();
                    if(unitList.contains(unitString.toLowerCase())){
                        banunit unit = banunit.getUnit(unitString);
                        long seconds = value * unit.getToSecond();
                        banmanager.ban(ProxyServer.getInstance().getPlayer(playername).getUniqueId().toString(), playername, reason, seconds);
                        p.sendMessage(new TextComponent("[PR-ban] Du hast "+args[0]+" für" + args[1] + "für" + value + unit.getName() + "gebannt"));
                     
                        return;
                    }
                    p.sendMessage(new TextComponent("[PR-ban]Diese Zeiteinheit gibt es nicht"));
                    return;
                }else{
                 
                }}
            }else{
                sender.sendMessage(new TextComponent(""));
            }
         
        }
     
    }
    banunit:
    Code (Text):

    package me.eliteSCHW31N.PRban;

    import java.util.ArrayList;
    import java.util.List;

    public enum banunit {
     
        SECOND("Sekunde(n)", 1, "sec"),
        MINUTE("Minute(n)", 60, "min"),
        HOUR("Stunde(n)", 60*60, "hour"),
        DAY("Tag(e)", 24*60*60, "day"),
        WEEK("Woche(n)", 7*24*60*60, "week"),
        MONTH("Monat(e)", 31*7*24*60*60, "month"),
        YEAR("Jahr(e)", 12*31*7*24*60*60, "year");
     
        private String name;
        private int toSecond;
        private String shortcut;
        private banunit(String name, int toSecond, String shortcut) {
            this.name = name;
            this.toSecond = toSecond;
        }
        public int getToSecond(){
         
         
            return toSecond;
        }
        public String getName(){
         
         
            return name;
        }
        public String getShortcut(){
         
            return shortcut;
        }
     
        public static List<String> getUnitsAsString(){
            List<String> units = new ArrayList<String>();
            for(banunit unit : banunit.values()) {
                units.add(unit.getShortcut().toLowerCase());
             
            }
            return units;
        }
        public static banunit getUnit(String unit){
            for(banunit units : banunit.values()){
                if(units.getShortcut().toLowerCase().equals(unit.toLowerCase())){
                    return units;
                }
            }
            return null;
         
        }
    }
     
    and the banmanager class
    Code (Text):

    package me.eliteSCHW31N.PRban;

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;

    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.api.chat.TextComponent;

    public class banmanager {
        public static void ban(String uuid, String playername, String reason, long seconds){
            long end = 0;
            if (seconds == 1){
                end = -1;
                mysql.update("INSERT INTO PRban (Spielername, UUID, Ende, Grund) Values ('"+playername+"' , '"+uuid+"' , '"+end+"' , '"+reason+"')");
                if(ProxyServer.getInstance().getPlayer(playername) != null){
                    ProxyServer.getInstance().getPlayer(playername).disconnect(new TextComponent("Du wurdest gebannt!\n"
                            +"\n"
                            +"Grund:" + getReason(uuid).replace("_", " ")
                            + "\n"
                            + "Verbleibende Zeit:" + "Permanent"
                            + "\n"
                            + "Entbannungs Anträge kannst du auf forum.pureresolution.net stellen"
                            ));
                }
            }else{
         
            long millis = seconds*1000;
            long current = System.currentTimeMillis();
            end = current+millis;
            mysql.update("INSERT INTO PRban (Spielername, UUID, Ende, Grund) Values ('"+playername+"' , '"+uuid+"' , '"+end+"' , '"+reason+"')");
            if(ProxyServer.getInstance().getPlayer(playername) != null){
                ProxyServer.getInstance().getPlayer(playername).disconnect(new TextComponent("Du wurdest gebannt!\n"
                        +"\n"
                        +"Grund:" + getReason(uuid).replace("_", " ")
                        + "\n"
                        + "Verbleibende Zeit:" + getReaminingTime(uuid)
                        + "\n"
                        + "Entbannungs Anträge kannst du auf forum.pureresolution.net stellen"
                        ));
            }}
         
            }
        public static void unban(String uuid){
            mysql.update("DELETE FROM PRban WHERE UUID='"+uuid+"'");
        }
        public static boolean isBanned(String uuid){
            ResultSet rs = mysql.getResult("SELECT Ende FROM PRban WHERE UUID='"+uuid+"'");
            try {
    //            while(rs.next()){
    //                if(rs.getInt("Ende") == -1){
    //                    return true;
    //                }
    //            }
                return rs.next();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
         
        }
        public static String getReason(String uuid){
            ResultSet rs = mysql.getResult("SELECT * FROM PRban WHERE UUID='"+uuid+"'");
            try{
                while(rs.next()){
                    return rs.getString("Grund");
                }
            } catch(SQLException e){
                e.printStackTrace();
            }
            return "";
         
        }
        public static String getReaminingTime(String uuid){
            long current = System.currentTimeMillis();
            long end = getEnd(uuid);
            if(end == -1){
                return "Permanent";
            }
            long different = end - current;
            int seconds = 0;
            int minutes = 0;
            int hours = 0;
            int days = 0;
            int weeks = 0;
            int months = 0;
            int years = 0;
            while(different > 1000){
                different-=1000;
                seconds++;
            }
            while(seconds > 60){
                seconds-=60;
                minutes++;
             
            }
            while(minutes > 60){
                minutes-=60;
                hours++;
            }
            while(hours >24){
                hours-=24;
                days++;
            }
            while(days >7){
                days-=7;
                weeks++;
            }
            while(weeks >31){
                weeks-=31;
                months++;
            }
            while(months >12){
                months-=12;
                years++;
            }
            return "" + years + months + weeks + days + hours + minutes;
        }
        public static Long getEnd(String uuid){
            ResultSet rs = mysql.getResult("SELECT * FROM PRban WHERE UUID='"+uuid+"'");
            try{
                while(rs.next()){
                    return rs.getLong("Ende");
                }
            } catch(SQLException e){
                e.printStackTrace();
            }
            return null;
         
        }
        public static List<String> getBannedPlayers(){
            List<String> list = new ArrayList<String>();
            ResultSet rs = mysql.getResult("SELECT * FROM PRban");
            try {
                while (rs.next()){
                    list.add(rs.getString("Spielername"));
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
         
        }
    }
     
    pls help me with that problems....
     
    #1 eliteSCHW31N, May 28, 2017
    Last edited: May 28, 2017
  2. To keep it clean, before and after code, put [ code] and [ /code] (W/o spaces) to make it look more neat
     
  3. Oh thank You For the hint!!
    Better?
     
  4. What error is it throwing?? Null pointer?
     
  5. I will sent the full errorlog at each Command if am back at home
     
  6. tempban:
    Code (Text):

    19:57:48 [WARNUNG] Error in dispatching command
    java.lang.NullPointerException
        at me.eliteSCHW31N.PRban.banunit.getUnitsAsString(banunit.java:41)
        at me.eliteSCHW31N.commands.COMMAND_ban.execute(COMMAND_ban.java:53)
        at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:169)
        at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:116)
        at net.md_5.bungee.connection.UpstreamBridge.handle(UpstreamBridge.java:120)
        at net.md_5.bungee.protocol.packet.Chat.handle(Chat.java:50)
        at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:78)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:280)
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:396)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:248)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:287)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
        at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:1017)
        at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:394)
        at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:299)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
        at java.lang.Thread.run(Thread.java:745)
    ban:
    Code (Text):


    19:57:24 [WARNUNG] Error in dispatching command
    java.lang.NullPointerException
        at me.eliteSCHW31N.PRban.banmanager.getReaminingTime(banmanager.java:77)
        at me.eliteSCHW31N.PRban.banmanager.ban(banmanager.java:38)
        at me.eliteSCHW31N.commands.COMMAND_ban.execute(COMMAND_ban.java:34)
        at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:169)
        at net.md_5.bungee.api.plugin.PluginManager.dispatchCommand(PluginManager.java:116)
        at net.md_5.bungee.connection.UpstreamBridge.handle(UpstreamBridge.java:120)
        at net.md_5.bungee.protocol.packet.Chat.handle(Chat.java:50)
        at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:78)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
        at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:280)
        at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:396)
        at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:248)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:287)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
        at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
        at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
        at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:1017)
        at io.netty.channel.epoll.EpollEventLoop.processReady(EpollEventLoop.java:394)
        at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:299)
        at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
        at java.lang.Thread.run(Thread.java:745)
     
    these are the error logs...
     
  7. For the first one, your List of Strings is not getting filled, so it's a null (or empty) list.

    For the second one, your database checker is probably not returning anything if they aren't banned. (you're also returning null which is a bad idea, you should return something like 0 long and process that in the getReaminingTime method)
     
  8. so i try something and my problem is now the mysql function doesnt work :-?
     
    #8 eliteSCHW31N, May 30, 2017
    Last edited: May 30, 2017
  9. Code (Text):

    package me.eliteSCHW31N.PRban;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class mysql{
    public static String host = "localhost";
    public static String port = "3306";
    public static String database = "nope";
    public static String username = "nope";
    public static String password = "nope";
    public static Connection con;

    public static void connect(){
        if(!isConnected()){
            try {
                con = DriverManager.getConnection("jdbc:mysql://localhost/PRban", username, password);// + host + "/" + database, username, password);
                System.out.println("[PRban-Mysql]verbunden");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
        }
       
    }
    public static void disconnect(){
        if(isConnected()){
            try {
                con.close();
                System.out.println("[PRban-Mysql]getrennt");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static boolean isConnected(){
       
        return (con == null ? false : true);
    }
    public static void update(String qry){
        try {
            con.createStatement().executeUpdate(qry);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static ResultSet getResult(String qry){
       
        try {
            con.createStatement().executeQuery(qry);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    public static Connection getConnection(){
        return con;
    }
    }
    i tried the mysql code manuelly with phpmyadmin and it works BUT not with my plugin why?
     
  10. #push

    please i need it for my network