Help me!

Discussion in 'Plugin Development' started by boss86741, Feb 19, 2013.

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

    boss86741

    I just made a tempbanning plugin and look:

    17:54:41 [SEVERE] Could not load 'plugins/TempBan.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NoSuchMethodException: com.boss86741.plugins.CustomBans.CustomBans.<init>()
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
    at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugins(CraftServer.java:235)
    at org.bukkit.craftbukkit.v1_4_6.CraftServer.<init>(CraftServer.java:213)
    at net.minecraft.server.v1_4_6.PlayerList.<init>(PlayerList.java:52)
    at net.minecraft.server.v1_4_6.DedicatedPlayerList.<init>(SourceFile:11)
    at net.minecraft.server.v1_4_6.DedicatedServer.init(DedicatedServer.java:104)
    at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:399)
    at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.NoSuchMethodException: com.boss86741.plugins.CustomBans.CustomBans.<init>()
    at java.lang.Class.getConstructor0(Class.java:2721)
    at java.lang.Class.getConstructor(Class.java:1674)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:176)
    ... 9 more
    This is the source of the classes:
    main:
    Code:
    package com.boss86741.plugins.CustomBans;
     
    import java.io.File;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    import com.boss86741.plugins.CustomBans.commands.BanExecutor;
    import com.boss86741.plugins.CustomBans.util.ListStore;
     
    public class CustomBans extends JavaPlugin {
        public ListStore bannedPlayers;
       
        public static CustomBans plugin;
       
        public CustomBans(CustomBans plugin) {
            this.plugin = plugin;
        }
       
        BanExecutor b = new BanExecutor(plugin);   
     
        public String target = b.getTarget();
       
        @Override
        public void onEnable() {
           
            String pluginFolder = this.getDataFolder().getAbsolutePath();
           
            (new File(pluginFolder)).mkdirs();
           
            this.bannedPlayers = new ListStore(new File(pluginFolder + File.separator + "bannedPlayers.yml"));
           
            this.bannedPlayers.load();
           
            getLogger().info("Ban Tracker Version 1.0!");
            getLogger().warning("This version is custom made!");
           
            this.getCommand("tempban").setExecutor(new BanExecutor(this));
        }
       
        @Override
        public void onDisable() {
            this.bannedPlayers.save();
        }
     
        public void setBanTimeout(long banTimestamp) {
            try {
                plugin.wait(banTimestamp);
                bannedPlayers.remove(target);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    ban executor:
    Code:
    package com.boss86741.plugins.CustomBans.commands;
     
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import com.boss86741.plugins.CustomBans.CustomBans;
    import com.boss86741.plugins.CustomBans.util.DateUtil;
     
    public class BanExecutor implements CommandExecutor {
        static Player player = null;
       
        public static CustomBans plugin;
       
        public BanExecutor(CustomBans plugin) {
            BanExecutor.plugin = plugin;
        }
       
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        String d = "1 Day";
        static String target;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("tempban")) {
                if (sender instanceof Player) {
                    player = (Player) sender;
                }
                    if (player == null) {
                        sender.sendMessage(ChatColor.RED + "Can't be used from the console.");
                    } else {
                        if (args.length <= 1) {
                            sender.sendMessage(ChatColor.RED + "Not enough arguments! Try /tempban <player> <number_of_days>");
                            return true;
                        } else {
                            if (player.hasPermission("custombans.admin.tempban")) {
                                String plName = player.getName();
                               
                                target = args[0];
                                Player target1 = plugin.getServer().getPlayer(args[0]);
                               
                                long banTimestamp = 0;
                                try {
                                    banTimestamp = DateUtil().parseDateDiff(args[1], true);
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                               
                                plugin.setBanTimeout(banTimestamp);
                                plugin.bannedPlayers.add(target);
                               
                                if (target != null) {
                                    target1.kickPlayer("You were banned by " + plName + " for " + banTimestamp + ".");
                                    Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "Player " + ChatColor.RED + target + ChatColor.GOLD + " was banned by " + ChatColor.RED + plName + ChatColor.GOLD + " for " + banTimestamp + ".");
                                }
                        }
                    }
                }
            return true;
            }
        return false;
        }
       
        public String getTarget() {
            return target;
        }
       
        private DateUtil DateUtil() {
            return DateUtil();
        }
    }
    
    List store:
    Code:
    package com.boss86741.plugins.CustomBans.util;
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
     
    import com.boss86741.plugins.CustomBans.CustomBans;
    import com.boss86741.plugins.CustomBans.commands.BanExecutor;
     
    public class ListStore {
        private File storageFile;
       
        private ArrayList<String> values;
     
        private CustomBans plugin;
       
        public ListStore(CustomBans plugin) {
            this.plugin = plugin;
        }
     
        BanExecutor b = new BanExecutor(plugin);   
     
        public String target = b.getTarget();
       
        public boolean tempban() {
            while (plugin.bannedPlayers.add(target) == true) {
               
            return true;
            }
        return false;
        }
       
        CommandSender sender;
        public void error() {
            sender.sendMessage(ChatColor.GREEN + "This person is not banned!");
        }
       
        public ListStore(File file) {
            this.storageFile = file;
            this.values = new ArrayList<String>();
           
            if (this.storageFile.exists() == false) {
                try {
                    this.storageFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        public void load() {
            try {
                DataInputStream input = new DataInputStream(new FileInputStream(this.storageFile));
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
               
                String line, value;
               
                while ((line = reader.readLine()) != null) {
                    if (this.values.contains(line) == false) {
                        this.values.add(line);
                    }
                }
               
                reader.close();
                input.close();
               
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public void save() {
            try {
                FileWriter stream = new FileWriter(this.storageFile);
                BufferedWriter out = new BufferedWriter(stream);
               
                for (String value : this.values) {
                    out.write(value);
                    out.newLine();
                }
               
                out.close();
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        public boolean contains (String target) {
            return this.values.contains(target);
        }
       
        public boolean add(String value) {
            if (this.contains(value) == false) {
                this.values.add(value);
            return true;
            }
        return false;
        }
       
        public void remove(String target) {
            if (this.contains(target) == true) {
                this.values.remove(target);
            } else {
                error();
            }
        }
       
        public ArrayList<String> getValues() {
            return this.values;
        }
    }
    
     
  2. Offline

    RainoBoy97

    remove the constructor in the main class
     
  3. Offline

    boss86741

  4. Offline

    RealDope

    He told you exactly what to do and then you bumped again twice? How does that constructor even make sense? You're trying to have an instance of the class passed into it's own constructor?
     
  5. Offline

    Birdgeek3

    I found it easy enough :p
     
  6. Offline

    boss86741

    I need replys before tomorrow.

    error:
    Code:
    19:01:38 [INFO] boss86741 issued server command: /tempban boss86741 1days
    19:01:38 [SEVERE] java.lang.Exception: illegalDate
    19:01:38 [SEVERE]    at com.boss86741.plugins.CustomBans.commands.DateUtil.parseDateDiff(DateUtil.java:138)
    19:01:38 [SEVERE]    at com.boss86741.plugins.CustomBans.commands.BanExecutor.onCommand(BanExecutor.java:55)
    19:01:38 [SEVERE]    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    19:01:38 [SEVERE]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    19:01:38 [SEVERE]    at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:510)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:980)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:898)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:853)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
    19:01:38 [SEVERE]    at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    19:01:38 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tempban' in plugin CustomBans v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:510)
        at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:980)
        at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:898)
        at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:853)
        at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
        at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
        at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
        at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.IllegalMonitorStateException
        at java.lang.Object.wait(Native Method)
        at com.boss86741.plugins.CustomBans.CustomBans.setBanTimeout(CustomBans.java:41)
        at com.boss86741.plugins.CustomBans.commands.BanExecutor.onCommand(BanExecutor.java:61)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    new source for main:
    Code:
    package com.boss86741.plugins.CustomBans;
     
    import java.io.File;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    import com.boss86741.plugins.CustomBans.commands.BanExecutor;
    import com.boss86741.plugins.CustomBans.util.ListStore;
     
    public class CustomBans extends JavaPlugin {
        public ListStore bannedPlayers;
     
        BanExecutor b = new BanExecutor(this); 
     
        public String target = b.getTarget();
     
        @Override
        public void onEnable() {
         
            String pluginFolder = this.getDataFolder().getAbsolutePath();
         
            (new File(pluginFolder)).mkdirs();
         
            this.bannedPlayers = new ListStore(new File(pluginFolder + File.separator + "bannedPlayers.yml"));
         
            this.bannedPlayers.load();
         
            getLogger().info("Ban Tracker Version 1.0!");
            getLogger().warning("This version is custom made!");
         
            this.getCommand("tempban").setExecutor(new BanExecutor(this));
        }
     
        @Override
        public void onDisable() {
            this.bannedPlayers.save();
        }
     
        public void setBanTimeout(long banTimestamp) {
            try {
                this.wait(banTimestamp);
                bannedPlayers.remove(target);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    New ban executor code (no other class changed):
    Code:
    package com.boss86741.plugins.CustomBans.commands;
     
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
     
    import com.boss86741.plugins.CustomBans.CustomBans;
     
    public class BanExecutor implements CommandExecutor {
        DateUtil dateUtil = new DateUtil();
     
        static Player player = null;
     
        public static CustomBans plugin;
     
        public BanExecutor(CustomBans plugin) {
            BanExecutor.plugin = plugin;
        }
     
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        String d = "1 Day";
        static String target;
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("tempban")) {
                if (sender instanceof Player) {
                    player = (Player) sender;
                }
                    if (player == null) {
                        sender.sendMessage(ChatColor.RED + "Can't be used from the console.");
                    } else {
                        if (args.length <= 1) {
                            sender.sendMessage(ChatColor.RED + "Not enough arguments! Try /tempban <player> <number_of_days>");
                            return true;
                        } else {
                            if (player.hasPermission("custombans.admin.tempban")) {
                                String plName = player.getName();
                             
                                target = args[0];
                                Player target1 = plugin.getServer().getPlayer(args[0]);
                             
                                long banTimestamp = 0;
                                try {
                                    banTimestamp = DateUtil.parseDateDiff(args[1], true);
                                } catch (Exception e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                             
                                plugin.setBanTimeout(banTimestamp);
                                plugin.bannedPlayers.add(target);
                             
                                if (target != null) {
                                    target1.kickPlayer("You were banned by " + plName + " for " + banTimestamp + ".");
                                    Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "Player " + ChatColor.RED + target + ChatColor.GOLD + " was banned by " + ChatColor.RED + plName + ChatColor.GOLD + " for " + banTimestamp + ".");
                                }
                        }
                    }
                }
            return true;
            }
        return false;
        }
     
        public String getTarget() {
            return target;
        }
    }
    
     
  7. Offline

    Tirelessly

    public void setBanTimeout(long banTimestamp) {
    try {
    this.wait(banTimestamp);
    bannedPlayers.remove(target);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    Why
     
  8. Offline

    boss86741

    StackTrace Exception catch. That isn't the problem. It is the invalid date exception I don't get.
     
  9. Offline

    CubixCoders

    Im assuming the problem is that you tried to set "1days" to a date, try removing "Days"
    Edit: What's line 41? in the method setBanTimeout?
     
  10. Offline

    Mango

  11. Offline

    boss86741

    It is NOT a null pointer exception!!!! It is illegal date.

    Yeah, line 41 is setBanTimeout().

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

    Deckerz

    What line is it? Not is in in that method
     
  13. Offline

    boss86741

    What do you mean?
     
  14. Offline

    Mango

    I didn't say it was. That tutorial works for your exception to because it tells you where the problem is occurring.
     
  15. Offline

    boss86741

    I don't see how.

    Also, no one has actually given a solution!
     
  16. Offline

    Mango

    boss86741 Post DateUtil.java. The problem is there.
     
  17. Offline

    boss86741

    Code:
    package com.boss86741.plugins.CustomBans.commands;
     
    import java.util.*;
    import java.util.logging.Logger;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
     
    public class DateUtil
    {
        DateUtil()
        {
        }
        private final static Logger logger = Logger.getLogger("Minecraft");
        private final static Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9]");
        private final static Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]");
     
        //Used to clean file names before saving to disk
        public static String sanitizeFileName(final String name)
        {
            return safeString(name);
        }
     
        //Used to clean strings/names before saving as filenames/permissions
        public static String safeString(final String string)
        {
            return INVALIDFILECHARS.matcher(string.toLowerCase(Locale.ENGLISH)).replaceAll("_");
        }
     
        //Less restrictive string sanitizing, when not used as perm or filename
        public static String sanitizeString(final String string)
        {
            return INVALIDCHARS.matcher(string).replaceAll("");
        }
     
        public static String formatDateDiff(long date)
        {
            Calendar c = new GregorianCalendar();
            c.setTimeInMillis(date);
            Calendar now = new GregorianCalendar();
            return DateUtil.formatDateDiff(now, c);
        }
     
        public static String formatDateDiff(Calendar fromDate, Calendar toDate)
        {
            boolean future = false;
            if (toDate.equals(fromDate))
            {
                return ("now");
            }
            if (toDate.after(fromDate))
            {
                future = true;
            }
     
            StringBuilder sb = new StringBuilder();
            int[] types = new int[]
            {
                Calendar.YEAR,
                Calendar.MONTH,
                Calendar.DAY_OF_MONTH,
                Calendar.HOUR_OF_DAY,
                Calendar.MINUTE,
                Calendar.SECOND
            };
            String[] names = new String[]
            {
                ("year"),
                ("years"),
                ("month"),
                ("months"),
                ("day"),
                ("days"),
                ("hour"),
                ("hours"),
                ("minute"),
                ("minutes"),
                ("second"),
                ("seconds")
            };
            int accuracy = 0;
            for (int i = 0; i < types.length; i++)
            {
                if (accuracy > 2)
                {
                    break;
                }
                int diff = dateDiff(types[i], fromDate, toDate, future);
                if (diff > 0)
                {
                    accuracy++;
                    sb.append(" ").append(diff).append(" ").append(names[i * 2 + (diff > 1 ? 1 : 0)]);
                }
            }
            if (sb.length() == 0)
            {
                return "now";
            }
            return sb.toString().trim();
        }
     
        private static int dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
        {
            int diff = 0;
            long savedDate = fromDate.getTimeInMillis();
            while ((future && !fromDate.after(toDate)) || (!future && !fromDate.before(toDate)))
            {
                savedDate = fromDate.getTimeInMillis();
                fromDate.add(type, future ? 1 : -1);
                diff++;
            }
            diff--;
            fromDate.setTimeInMillis(savedDate);
            return diff;
        }
     
        public static long parseDateDiff(String time, boolean future) throws Exception
        {
            Pattern timePattern = Pattern.compile(
                    "(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?"
                    + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?"
                    + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?"
                    + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?"
                    + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?"
                    + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?"
                    + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE);
            Matcher m = timePattern.matcher(time);
            int years = 0;
            int months = 0;
            int weeks = 0;
            int days = 0;
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            boolean found = false;
            if (!found)
            {
                throw new Exception(("illegalDate"));
            }
            Calendar c = new GregorianCalendar();
            if (years > 0)
            {
                c.add(Calendar.YEAR, years * (future ? 1 : -1));
            }
            if (months > 0)
            {
                c.add(Calendar.MONTH, months * (future ? 1 : -1));
            }
            if (weeks > 0)
            {
                c.add(Calendar.WEEK_OF_YEAR, weeks * (future ? 1 : -1));
            }
            if (days > 0)
            {
                c.add(Calendar.DAY_OF_MONTH, days * (future ? 1 : -1));
            }
            if (hours > 0)
            {
                c.add(Calendar.HOUR_OF_DAY, hours * (future ? 1 : -1));
            }
            if (minutes > 0)
            {
                c.add(Calendar.MINUTE, minutes * (future ? 1 : -1));
            }
            if (seconds > 0)
            {
                c.add(Calendar.SECOND, seconds * (future ? 1 : -1));
            }
     
            Calendar max = new GregorianCalendar();
            max.add(Calendar.YEAR, 10);
            if (c.after(max))
            {
                return max.getTimeInMillis();
            }
            return c.getTimeInMillis();
        }
    }
     
  18. Offline

    Lolmewn Retired Staff

    Tirelessly Very, very bad idea as it's probably called from the main thread.
    Instead, should make a scheduler which fires after some time.
     
  19. Offline

    boss86741

    I am still a fairly new coder and am not exactly familiar with the craftbukkit scheduler. There is a tutorial though.

    But is that the error?

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

    Tirelessly

    That segment was copy/pasted from his code
     
  21. Offline

    Lolmewn Retired Staff

    Ah, that explains :)
     
  22. Offline

    boss86741

    What do you mean?

    Guys, is the solution using my own scheduler?

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

    boss86741

    Doesn't anyone know how to do this?
     
  24. Offline

    hapm

    Little bit off topic, but i thought you should know it: your BanExecutor class has a memory leak. Do not save your plugins instance in a static variable, as long as you don't know how static works. It will cause your plugin instance to never be deleted by the garbage collector as long as you don't set the static field to null explicitly. Try to avoid static anyway, or clean static fields in your plugins onDisable method.
     
  25. Offline

    boss86741

    I am new to java obviously. How do I do that?
     
  26. Offline

    hapm

    Simply remove the static keyword from any field in your BanExecutor class and change
    Code:
    BanExecutor.plugin = plugin
    to
    Code:
    this.plugin = plugin;
    Btw: do you ever use the BanExecutor.getTarget() method in your code? And if so, where do you use it. You save a player instance in target, what'll run into problems like memory leaks and other.
    Code:
    plugin.bannedPlayers.add(target);
    is a bad idea as well, as you save the Player instance again. Only save the name of the Player instead, as you can always get the current active Player instance from Bukkit, when you have the players name. Player objects get invalid when the player disconnects from the server.
     
  27. Offline

    boss86741

    getTarget() is used here:
    Code:
    public class ListStore {
        private File storageFile;
       
        private ArrayList<String> values;
     
        private CustomBans plugin;
       
        public ListStore(CustomBans plugin) {
            this.plugin = plugin;
        }
     
        BanExecutor b = new BanExecutor(plugin);   
     
        public String target = b.getTarget();
       
        public boolean tempban() {
            while (plugin.bannedPlayers.add(target) == true) {
               
            return true;
            }
        return false;
        }
    I don't understand how I am not already saving the name of the player only.
    Code:
    target = args[0];
    and args[0] are the player target's username.
     
  28. Offline

    hapm

    Not sure where I were when I read your code dude, sorry. You are doing it totally right...
     
  29. Offline

    boss86741

    Wow, I can't believe I was doing right. There is still this though:
    Code:
    18:19:15 [INFO] boss86741 issued server command: /tempban boss86741 1d
    18:19:15 [SEVERE] java.lang.Exception: illegalDate
    18:19:15 [SEVERE]    at com.boss86741.plugins.CustomBans.commands.DateUtil.parseDateDiff(DateUtil.java:138)
    18:19:15 [SEVERE]    at com.boss86741.plugins.CustomBans.commands.BanExecutor.onCommand(BanExecutor.java:55)
    18:19:15 [SEVERE]    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    18:19:15 [SEVERE]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
    18:19:15 [SEVERE]    at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:510)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:980)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:898)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:853)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
    18:19:15 [SEVERE]    at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    18:19:15 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'tempban' in plugin CustomBans v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:186)
        at org.bukkit.craftbukkit.v1_4_6.CraftServer.dispatchCommand(CraftServer.java:510)
        at net.minecraft.server.v1_4_6.PlayerConnection.handleCommand(PlayerConnection.java:980)
        at net.minecraft.server.v1_4_6.PlayerConnection.chat(PlayerConnection.java:898)
        at net.minecraft.server.v1_4_6.PlayerConnection.a(PlayerConnection.java:853)
        at net.minecraft.server.v1_4_6.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_4_6.NetworkManager.b(NetworkManager.java:290)
        at net.minecraft.server.v1_4_6.PlayerConnection.d(PlayerConnection.java:112)
        at net.minecraft.server.v1_4_6.ServerConnection.b(SourceFile:39)
        at net.minecraft.server.v1_4_6.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_4_6.MinecraftServer.r(MinecraftServer.java:598)
        at net.minecraft.server.v1_4_6.DedicatedServer.r(DedicatedServer.java:224)
        at net.minecraft.server.v1_4_6.MinecraftServer.q(MinecraftServer.java:494)
        at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:427)
        at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.IllegalMonitorStateException
        at java.lang.Object.wait(Native Method)
        at com.boss86741.plugins.CustomBans.CustomBans.setBanTimeout(CustomBans.java:41)
        at com.boss86741.plugins.CustomBans.commands.BanExecutor.onCommand(BanExecutor.java:61)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    and I am new to schedules and dates...
     
  30. Offline

    hapm

    Well, I don't really get what you wan't to do in setBanTimeout, as .wait() normally is for syncing two different threads (let one thread wait for a signal from another thread), and I don't think you wan't to do that. As you are waiting in the main thread here, it would get the whole server to stop anyway. So it is a bad idea to do that. What you really want is to let the server going for x seconds, and than execute
    Code:
    bannedPlayers.remove(target);
    don't you?

    If so, use a sync scheduler to do it:
    Code:
    getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    {
      public void run() {
        bannedPlayers.remove(target);
      }
    }, banTimestamp*20);
     
Thread Status:
Not open for further replies.

Share This Page