Tempban Time Problem

Discussion in 'Spigot Plugin Development' started by _xXProDudeXx_, May 28, 2016.

  1. Hello guys,

    I am trying to code a command for my BanManager to be able to ban players temporarily. I have tried some things and this is how my code is looking right now:

    Tempban-Code:

    Code (Text):
    package banmanager.Commands;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import banmanager.Strings.Strings;


    public class TempbanCommand implements CommandExecutor {

        @Override
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player s = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("bmtban")){
                if (!(s.hasPermission("bm.mod"))){
                    s.sendMessage(Strings.NoPermMsg);
                    return true;
                }
                if (args.length < 4){
                    s.sendMessage(Strings.TempbanUsage);
                    s.sendMessage(Strings.TempbanExample);
                    return true;
                }
                Player target = Bukkit.getPlayer(args[0]);
                if (target == null){
                    s.sendMessage(Strings.SpecifyMsg);
                    return true;
                }
                if (target.isBanned()){
                    s.sendMessage(Strings.IsBannedMsg);
                    return true;
                }
                target.kickPlayer("");
             
            }
            return true;
            }
    }
    This is what I have tried to make the times, in another package called Strings:

    Code (Text):
    public static long seconds = System.currentTimeMillis() * 1000L;
    public static long minutes = System.currentTimeMillis() * 1000L * 60;
    public static long hours = System.currentTimeMillis() * 1000L * 60 * 60;
    public static long days = System.currentTimeMillis() * 1000L * 60 * 60 * 24;
    public static long weeks = System.currentTimeMillis() * 1000L * 60 * 60 * 24 * 7;
    public static long months = System.currentTimeMillis() * 1000L * 60 * 60 * 24 * 4;
    Now there are 2 problems. I don't know if those time-formats are correct, and I also don't know how to make sure, I can use them in my command, so the correct time will be used in a command.

    So for example that /bmtban Peter 5 d will ban Peter for 5 days and not for 5 months.

    What should I do?

    Sincerely,

    ZeusReksYou_
     
  2. You aren't supposed to multiply the current time, that won't work.

    You need to get the time of the ban and store it some place. Then when they try to login, compare the stored time to the current time.

    Code (Text):
    long oldTime = getOldTime(Player varPlayer);
    long currentTime = System.currentTimeMillis();
    long lengthInSeconds = 86400; /* 1 day */
    long lengthOfBan = lengthInSeconds * 1000;

    if (currentTime-oldTime > lengthOfBan){
            return true;
    }

    return false;
     
  3. Or just save the current millis and add the millis until the ban finishes. Then you can always convert millis to the time
     
  4. Add to the current millis the millis of ban time, than store the result. When a player tries to connect, just check if:
    millis (now) < millis (stored)
    If yes, prevent player to connect.

    That's the easiest and simplest way...
     
  5. Yes but if he's making a ban manager wouldn't he want to know the length of previous bans?
     
  6. @Xilixir i didn't read anything about add ban time. But if he wan't to add a new ban time with the previous bantime, he just has to do the following:
    The previous bans has to be stored as i already said. Than he has to check, when a new ban append, if there exists already a tempban, if yes, he has to calculate nowMilis - oldBanMilis to get the millis of the previous ban, these he has to add to the new ban, example: nowMilis + banMilis + oldBanMilis to get the new milis of the ban.

    He want's also to say that "d" is standing for "day" so he has to check the Argument after the bantime, than calculate with the already given multiplicators. ;)

    If i missunderstand, please tell me! ยก
     
  7. @Xilixir I don't really know what I should edit to my code. I am pretty new to Java, and I know this might be a big task. I know how to make commands to Kick, Ban, Report, Warn and some more of that kind of commands. Also I don't really know how to store bans, I tried to but I didn't really understand it. Could you code a really simple boolean onCommand(){ with an example of how to do it? (Could you also show of how to add a time to a command?)
     
  8. You could store bans in a data.yml personally I would use a mysql database
     
  9. @Xilixir @BiastGaming I have looked trough a couple of threads about custom .yml files and this is how I created my Config class now

    Code (Text):
    package me.ZeusReksYou_.BanManager;

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class Config {
       
        public static BanManager plugin;
        @SuppressWarnings("static-access")
        public Config(BanManager plugin){
            this.plugin = plugin;
        }
       
        private static FileConfiguration BansConfig = null;
        private static File Bans = null;
       
        public static void reloadCustomConfig(){
            if (Bans == null){
                Bans = new File(plugin.getDataFolder(), "bans.yml");
            }
            BansConfig = YamlConfiguration.loadConfiguration(Bans);
        }
       
        public static FileConfiguration getCustomConfig() {
            if (BansConfig == null) {
            reloadCustomConfig();
            }
            return BansConfig;
            }
             
            public static void saveCustomConfig() {
            if (BansConfig == null || Bans == null) {
            return;
            }
            try {
            getCustomConfig().save(Bans);
            } catch (IOException ex) {
            plugin.getLogger().log(Level.SEVERE, "Could not save config to " + Bans, ex);
            }
            }
             
            }
       
     
    I hope this is a working file I can store bans in, but then I still don't know how to Tempban, or how to store it
     
  10. I exclusively use custom JSON configuration files, so I can't help you, sorry.
     
  11. @Xilixir That doesn't really matter as I am also definitly not familair with it. Maybe you could show me another way ?
     
  12. Honestly if I were you I'd pick up on JSON. It's extremely fluid and works like a charm. Extremely helpful.
    Using gson is your best bet, and the dependency is part of the spigot library, so no need for anything extra :p
    http://howtodoinjava.com/best-practices/google-gson-tutorial-convert-java-object-to-from-json/
    After learning gson, it's just a matter of knowing how to read/write from/to a file