Solved Writing multidimensional hashmap to config

Discussion in 'Plugin Development' started by Strahan, Sep 21, 2016.

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

    Strahan

    Hi. I searched, but I can't find an example of storing multidimensional hashmaps to a config file.

    My "bans" variable is HashMap<String, HashMap<String, Long>>. Primary index is player name then the sub-hash index is world name and value is the time when the ban expires. I tried this.getConfig().set("bans", bans); and it gave me:

    Code:
    bans:
      Strahan:
        kland: 1474512661515
        oasis: 1474516254663
      Gandalug:
        imperial: 1474681866815
    
    That broke it down nicely. Alas, I can't seem to figure out a quick and easy way to shove those data back into my hashmap. Before I wrote a reader function, I wanted to verify that there is no such easy peasy method...?

    ---------
    I was impatient so I went ahead and wrote this:

    Code:
      public void ReadConfig() {
          try {
              bans.clear();
              ConfigurationSection cfgBans = this.getConfig().getConfigurationSection("bans");
              for (String user : cfgBans.getKeys(false)) {
                  HashMap<String, Long> tmp = new HashMap<String, Long>();
                  ConfigurationSection cfgWorlds = cfgBans.getConfigurationSection(user);
                  for (String world : cfgWorlds.getKeys(false)) tmp.put(world, cfgWorlds.getLong(world));
                  bans.put(user, tmp);
              }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during ReadConfig!");
              ex.printStackTrace();
          }
      }
    
    Seems to work, hopefully it's not too much a kludge. Of course, I know I need to go change all the name related stuff to UUID, using name was a dumb idea.
     
    Last edited: Sep 21, 2016
  2. Offline

    HeartandSoul

    It's even more help if you paste the entire class! :)
     
  3. Offline

    Strahan

    Oh, sorry:

    Code:
    package com.sylvcraft;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    
    public class WorldAccess extends JavaPlugin implements Listener {
        HashMap<String, HashMap<String, Long>> bans = new HashMap<String, HashMap<String, Long>>();
        HashMap<String, HashMap<String, Long>> punishments = new HashMap<String, HashMap<String, Long>>();
        public Permission adminRights = new Permission("worldaccess.admin");
        Logger log = Logger.getLogger("Minecraft");
      
      
      @Override
      public void onEnable() {
          getServer().getPluginManager().registerEvents(this, this);
        PluginManager pm = getServer().getPluginManager();
        pm.addPermission(adminRights);
      }
    
      @EventHandler(priority = EventPriority.HIGHEST)
      public void onPlayerRespawn(PlayerRespawnEvent pre) {
          pre.setRespawnLocation(new Location(Bukkit.getServer().getWorld("imperial"), -404, 26, 655, -180f, -0.6f));
      }
      @EventHandler
      public void onPlayerWorldChange(PlayerChangedWorldEvent event) {
          Player p = event.getPlayer();
          if (bans.containsKey(p.getUniqueId().toString())) {
              HashMap<String, Long> tmp = bans.get(p.getUniqueId().toString());
              if (tmp.containsKey(p.getUniqueId().toString())) {
                  Long banlen = (tmp.get(p.getWorld().getName()) - (System.currentTimeMillis())) / 1000;
                  if (banlen <= 0) {
                      tmp.remove(p.getUniqueId().toString());
                      bans.put(p.getUniqueId().toString(), tmp);
                  } else {
                      p.teleport(event.getFrom().getSpawnLocation());
                      SendChat(p, ChatColor.RED + "You have been banned on that world!");
                      SendChat(p, ChatColor.RED + "Ban time left: " + timedisp(banlen));
                      punish(p, p.getWorld().getName());
                  }
              }
          }
      }
    
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        try {
          if (sender instanceof Player) {
            Player player = (Player)sender;
            switch (cmd.getName().toLowerCase()) {
            case "wa":
              if (args.length == 0) {
                  SendChat(player, "Usage: /wa ban <player> <world> <duration>");
                  SendChat(player, "Usage: /wa pardon <player> <world> <duration>");
                  SendChat(player, "Usage: /wa list");
                  SendChat(player, "Usage: /wa setspawn");
              } else {
                  switch (args[0].toLowerCase()) {
                  case "list":
                      listbans(player);
                      break;
                  case "ban":
                      if (args.length == 1) {
                          SendChat(player, "You need to tell me at least the player name.");
                      } else {
                          String banned = this.getServer().getPlayer(args[1]).getUniqueId().toString(), banworld = player.getWorld().getName(), duration = "1h";
                          if (args.length > 2) banworld = args[2];
                          if (args.length > 3) duration = args[3].toLowerCase();
                          switch (ban(banned, banworld, duration)) {
                          case 0:
                              SendChat(player, args[1] + " has been banned on " + banworld + "!");
                         this.getConfig().set("bans", bans);
                         this.saveConfig();
                              break;
                          case 1:
                              SendChat(player, args[1] + " is already banned on " + banworld + "!");
                              break;
                          }
                      }
                      break;
                  case "pardon":
                      if (args.length == 1) {
                          SendChat(player, "You need to tell me at least the player name.");
                      } else {
                          String banned = this.getServer().getPlayer(args[1]).getUniqueId().toString(), banworld = player.getWorld().getName();
                          if (args.length > 2) banworld = args[2];
                          switch (pardon(banned, banworld)) {
                          case 0:
                              SendChat(player, args[1] + " has been pardoned!");
                              break;
                          case 1:
                              SendChat(player, args[1] + " is not banned from " + banworld);
                              break;
                          case 2:
                              SendChat(player, args[1] + " has no bans with which to pardon.");
                              break;
                          }
                      }
                      break;
                  case "setspawn":
                      break;
                  }
              }
              break;
            }
          }
          return true;
        } catch (Exception ex) {
          return false;
        }
      }
    
      public void punish(Player p, String world) {
          if (punishments.containsKey(p.getUniqueId().toString())) {
              HashMap<String, Long> tmp = punishments.get(p.getUniqueId().toString());
              if (tmp.containsKey(world)) {
                  Long lasttry = (tmp.get(world) - (System.currentTimeMillis())) / 1000;
                  if (lasttry > 0) {
                      p.getWorld().strikeLightning(p.getLocation());
                      SendChat(p, ChatColor.RED + "You have been punished for trying again too soon!");
                  } else {
                      tmp.remove(world);
                      if (tmp.isEmpty()) {
                          punishments.remove(p.getUniqueId().toString());
                      } else {
                          punishments.put(p.getUniqueId().toString(), tmp);
                      }
                  }
              } else {
                  tmp.put(world, (System.currentTimeMillis() + 30000));
              }
          } else {
              HashMap<String, Long> tmp = new HashMap<String, Long>();
              tmp.put(world, (System.currentTimeMillis() + 30000));
              punishments.put(p.getName(), tmp);
          }
      }
      public void listbans(Player p) {
            Iterator<Map.Entry<String, HashMap<String, Long>>> it = bans.entrySet().iterator(); int printed = 0;
            while (it.hasNext()) {
                String buffer = "";
                Map.Entry<String, HashMap<String, Long>> pair = (Map.Entry<String, HashMap<String, Long>>)it.next();
                Iterator<Map.Entry<String, Long>> bi = pair.getValue().entrySet().iterator();
                while (bi.hasNext()) {
                    Map.Entry<String, Long> banpair = (Map.Entry<String, Long>)bi.next();
                    Long banlen = (banpair.getValue() - (System.currentTimeMillis())) / 1000;
                    if (banlen <= 0) {
                        pardon(this.getServer().getPlayer(pair.getKey()).toString(), banpair.getKey());
                    } else {
                        buffer += ChatColor.GREEN + "[" + ChatColor.DARK_GREEN + banpair.getKey() + ChatColor.GREEN + "]: " + ChatColor.YELLOW + timedisp(banlen) + " left\n";
                    }
                }
                if (!buffer.equals("")) {
                    SendChat(p, ChatColor.BLUE + "Active bans for " + ChatColor.WHITE + this.getServer().getPlayer(pair.getKey()).toString() + ChatColor.BLUE + ":\n" + buffer);
                    printed++;
                }
            }
            if (printed == 0) SendChat(p, ChatColor.BLUE + "No active bans!");
      }
      public int pardon(String player, String world) {
          // return 0 = ban removed
          // return 1 = not banned on that world
          // return 2 = not banned anywhere
            HashMap<String, Long> tmp; int ret = 0;
            if (bans.containsKey(player)) {
                tmp = bans.get(player);
                if (tmp.containsKey(world)) {
                    tmp.remove(world);
                } else {
                    ret = 1;
                }
                if (Bukkit.getWorld(world + "_nether") != null && tmp.containsKey(world + "_nether")) { tmp.remove(world + "_nether"); ret = 1; }
                if (Bukkit.getWorld(world + "_the_end") != null && tmp.containsKey(world + "_the_end")) { tmp.remove(world + "_the_end"); ret = 1; }
                if (tmp.isEmpty()) {
                    bans.remove(player);
                } else {
                    bans.put(player, tmp);
                }
                return ret;
            } else {
                return 2;
            }
      }
      public int ban(String player, String world, String duration) {
          // returns:
          // 0 = banned
          // 1 = already banned
            HashMap<String, Long> tmp;
            if (bans.containsKey(player)) {
                tmp = bans.get(player);
                if (tmp.containsKey(world)) return 1;
            } else {
                tmp = new HashMap<String, Long>();
            }
            tmp.put(world, banstop(duration));
            if (Bukkit.getWorld(world + "_nether") != null && !tmp.containsKey(world + "_nether")) tmp.put(world + "_nether", banstop(duration));
            if (Bukkit.getWorld(world + "_the_end") != null && !tmp.containsKey(world + "_the_end")) tmp.put(world + "_the_end", banstop(duration));
            bans.put(player,  tmp);
            return 0;
      }
      public Long banstop(String duration) {
          Long retval = 0l; String buffer = "";
          int[] multiplier = new int[116]; multiplier[100] = 86400; multiplier[104] = 3600; multiplier[109] = 60; multiplier[115] = 1;
          for (int x = 0; x < duration.length(); x++) {
              if ((int)duration.charAt(x) > 57) {
            retval += Long.parseLong(buffer) * multiplier[(int)duration.charAt(x)];
            buffer = "";
              } else {
                  buffer += duration.substring(x, x+1);
              }
          }
          return (retval*1000) + System.currentTimeMillis();
      }
      public String timedisp(Long secs) {
          String retval = "";
          Long days = 0l, hours = 0l, minutes = 0l;
          days = secs / 86400;
        hours = (secs - (days * 86400)) / 3600;
        minutes = (secs - ((days * 86400) + (hours * 3600))) / 60;
        secs = (secs - ((days * 86400) + (hours * 3600) + (minutes * 60))) % 60;
        if (secs == 60) { minutes++; secs = 0l; }
        if (minutes == 60) { hours++; minutes = 0l; }
        if (hours == 24) { days++; hours = 0l; }
        if (days > 0) retval += days.toString() + pluralize(" day!s, ", days);
        if (hours > 0) retval += hours.toString() + pluralize(" hour!s, ", hours);
        if (minutes > 0) retval += minutes.toString() + pluralize(" minute!s, ", minutes);
        if (secs > 0) retval += secs.toString() + pluralize(" second!s, ", secs);
        return retval.substring(0, retval.length()-2);
      }
      public String pluralize(String str, Long amount) {
          String ret = str.replace("!#", amount.toString());
          if (amount > 1 || amount == 0) {
              ret = ret.replace("!s", "s");
              ret = ret.replace("!ia", "are");
              ret = ret.replace("!ww", "were");
          } else {
              ret = ret.replace("!s", "");
              ret = ret.replace("!ia", "is");
              ret = ret.replace("!ww", "was");
          }
          return ret;
      }
        public void SendChat(Player player, String chatstring) {
            if (player == null) {
                log.log(Level.INFO, "[WorldAccess]: " + chatstring);
            } else {
                player.sendMessage(ChatColor.GREEN + "[" + ChatColor.LIGHT_PURPLE + "WorldAccess" + ChatColor.GREEN + "]: " + ChatColor.RESET + chatstring);
            }
      }
    
      public void ReadConfig() {
          try {
              bans.clear();
              ConfigurationSection cfgBans = this.getConfig().getConfigurationSection("bans");
              for (String user : cfgBans.getKeys(false)) {
                  HashMap<String, Long> tmp = new HashMap<String, Long>();
                  ConfigurationSection cfgWorlds = cfgBans.getConfigurationSection(user);
                  for (String world : cfgWorlds.getKeys(false)) tmp.put(world, cfgWorlds.getLong(world));
                  bans.put(user, tmp);
              }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during ReadConfig!");
              ex.printStackTrace();
          }
      }
    }
    
    The ReadConfig function works, I just wasn't sure if that's the best way to do it or not.
     
  4. Offline

    Tecno_Wizard

    @Strahan, I'd use ConfigurationSection#values(), which is a horribly named method that returns all the Entries in the config section. Just do it recursively with things that are also sections (or maybe they're treated like maps in this function? IDK)

    Why wasn't this named getEntrySet?
     
  5. Offline

    mythbusterma

    @Tecno_Wizard

    I think it's a very aptly named method, it gets all values given a key, using the sans "get" getter methods we've been using since Java 5.

    Because it's not an EntrySet.

    @Strahan

    In general, avoid nesting collections. Instead, create an Object that has a collection inside of it, in this case I would call it something along the lines of "BanData," so that it looks roughly like this:

    Code:
    HashMap<UUID, BanData> bans =....
    
    
    public class BanData implements ConfigurationSerializable {
          private HashMap<UUID, Long> worldBans  = ....
    
          // implement the contract requirements for ConfigurationSerializable
    }
    
    You'll find this to be much cleaner, and it allows you to use the ConfigurationSerializable, which effectively lets you make any time a valid configuration object, which you can get and set with ConfigurationSection#get() and ConfigurationSection#set().

    You'll also note I'm using UUIDs here, this way a user won't be able to change their username and circumvent your ban. It's also more convenient to store worlds as their UUID.
     
  6. Offline

    Strahan

    Thanks a lot, I'll read up on that stuff and work it into my program. Yea, I switched to UUIDs for players since I posted this:

    Code:
    package com.sylvcraft;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChangedWorldEvent;
    import org.bukkit.event.player.PlayerRespawnEvent;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Objects;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.UUID;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    
    public class WorldAccess extends JavaPlugin implements Listener {
        HashMap<String, HashMap<String, Long>> bans = new HashMap<String, HashMap<String, Long>>();
        HashMap<String, HashMap<String, Long>> punishments = new HashMap<String, HashMap<String, Long>>();
        HashMap<String, String> uuidCache = new HashMap<String, String>();
        public Permission adminRights = new Permission("worldaccess.admin");
        Logger log = Logger.getLogger("Minecraft");
        Timer timer = new Timer();
      
      
      @Override
      public void onEnable() {
          try {
            getServer().getPluginManager().registerEvents(this, this);
           PluginManager pm = getServer().getPluginManager();
           pm.addPermission(adminRights);
           String tmp = this.getConfig().getString("mainworld");
           if (tmp == null) {
              List<World> worlds = getServer().getWorlds();
              this.getConfig().set("mainworld", worlds.get(0).getName());
              this.getConfig().set("mainworld_spawn", worlds.get(0).getSpawnLocation().getBlockX() + "/" + worlds.get(0).getSpawnLocation().getBlockY() + "/" + worlds.get(0).getSpawnLocation().getBlockZ() + "/" + worlds.get(0).getSpawnLocation().getPitch() + "/" + worlds.get(0).getSpawnLocation().getYaw());
             this.saveConfig();
           }
           if (this.getConfig().getString("punishment_delay") == null) {
              this.getConfig().set("punishment_delay", 0);
              this.saveConfig();
           }
           ReadConfig();
           timer.scheduleAtFixedRate(new TimerTask() { 
             @Override
             public void run() {
                if (!bans.isEmpty()) {
                      Iterator<Map.Entry<String, HashMap<String, Long>>> it = bans.entrySet().iterator();
                      while (it.hasNext()) {
                          Map.Entry<String, HashMap<String, Long>> pair = (Map.Entry<String, HashMap<String, Long>>)it.next();
                          Iterator<Map.Entry<String, Long>> bi = pair.getValue().entrySet().iterator();
                          while (bi.hasNext()) {
                              Map.Entry<String, Long> banpair = (Map.Entry<String, Long>)bi.next();
                              Long banlen = (banpair.getValue() - (System.currentTimeMillis())) / 1000;
                              if (banlen <= 0) {
                                  Player p = Bukkit.getServer().getPlayer(UUID.fromString(pair.getKey()));
                                  if (p != null) {
                                      pardon(p.getUniqueId().toString(), banpair.getKey());
                                      SendChat(p, ChatColor.GREEN + "Congrats!  Your ban from " + banpair.getKey() + " has expired.");
                                  }
                              }
                          }
                      }
                }
             }
           }, 1000, 30000);
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during onEnable!");
              ex.printStackTrace();
          }
      }
    
      @EventHandler(priority = EventPriority.HIGHEST)
      public void onPlayerRespawn(PlayerRespawnEvent pre) {
          try {
            World respawnWorld = Bukkit.getServer().getWorld(this.getConfig().getString("mainworld"));
            String[] data = this.getConfig().getString("mainworld_spawn").split("/");
            if (respawnWorld == null) {
                log.log(Level.WARNING, "The mainworld configuration is set to an invalid or unloaded world!");
            } else {
                if (data.length < 5) {
                    log.log(Level.WARNING, "The mainworld_spawn configuration attribute is malformed!");
                } else {
                    pre.setRespawnLocation(new Location(Bukkit.getServer().getWorld(this.getConfig().getString("mainworld")), Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2]), Float.parseFloat(data[3]),Float.parseFloat(data[4])));
                }
            }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during onPlayerRespawn!");
              ex.printStackTrace();
          }
      }
      @EventHandler
      public void onPlayerWorldChange(PlayerChangedWorldEvent event) {
          try {
            Player p = event.getPlayer();
            if (bans.containsKey(p.getUniqueId().toString())) {
                HashMap<String, Long> tmp = bans.get(p.getUniqueId().toString());
                Iterator<Map.Entry<String, Long>> it = tmp.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Long> boof = (Map.Entry<String, Long>)it.next();
                    if (boof.getKey().equals(p.getWorld().getName())) {
                        Long banlen = (tmp.get(p.getWorld().getName()) - (System.currentTimeMillis())) / 1000;
                        if (banlen <= 0) {
                            tmp.remove(p.getUniqueId().toString());
                            bans.put(p.getUniqueId().toString(), tmp);
                        } else {
                            p.teleport(event.getFrom().getSpawnLocation());
                            SendChat(p, ChatColor.RED + "You have been banned on that world!");
                            SendChat(p, ChatColor.RED + "Ban time left: " + timedisp(banlen));
                            punish(p, p.getWorld().getName());
                        }
                    }
                }
            }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during onPlayerWorldChange!");
              ex.printStackTrace();
          }
      }
    
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        try {
          if (sender instanceof Player) {
            Player player = (Player)sender;
            switch (cmd.getName().toLowerCase()) {
            case "wa":
              if (args.length == 0) {
                  SendChat(player, "Usage: /wa ban <player> <world> <duration>");
                  SendChat(player, "Usage: /wa pardon <player> <world> <duration>");
                  SendChat(player, "Usage: /wa list");
                  SendChat(player, "Usage: /wa setspawn");
              } else {
                  switch (args[0].toLowerCase()) {
                  case "reload":
                      ReadConfig();
                      break;
                  case "punishdelay":
                      if (args.length == 1) {
                          SendChat(player, "Punishment delay is currently " + ((Integer.toString(this.getConfig().getInt("punishment_delay")).equals("0")?"disabled.":Integer.toString(this.getConfig().getInt("punishment_delay")).toString() + " seconds")));
                        } else {
                            this.getConfig().set("punishment_delay", Integer.parseInt(args[1]));
                            this.saveConfig();
                            SendChat(player, "Punishment delay set to " + ((args[1].equals("0")?" (disabled)":args[1] + " seconds.")));
                        }
                      break;
                  case "lookup":
                      SendChat(player, UUIDcache(args[1]));
                      break;
                  case "list":
                      String targetPlayer = ((args.length == 1)?"":args[1]);
                        listbans(player, targetPlayer);
                      break;
                  case "ban":
                      if (args.length == 1) {
                          SendChat(player, "You need to tell me at least the player name.");
                      } else {
                          String banned = getUUID(args[1]), banworld = player.getWorld().getName(), duration = "1h";
                          if (banned == "Unknown") {
                              SendChat(player, "That player doesn't appear to be valid.");
                              return true;
                          }
                          if (args.length > 2) banworld = args[2];
                          if (args.length > 3) duration = args[3].toLowerCase();
                          if (banworld.toLowerCase().equals(this.getConfig().getString("mainworld"))) {
                              SendChat(player, "You cannot ban someone from " + this.getConfig().getString("mainworld") + "!");
                              return true;
                          }
                          switch (ban(banned, banworld, duration)) {
                          case 0:
                              SendChat(player, args[1] + " has been banned on " + banworld + "!");
                              Player p = this.getServer().getPlayer(args[1]);
                              if (p != null) {
                                  if (p.getWorld().getName().toLowerCase().equals(banworld.toLowerCase())) {
                                  String[] data = this.getConfig().getString("mainworld_spawn").split("/");
                                  Location moveTo = new Location(Bukkit.getServer().getWorld(this.getConfig().getString("mainworld")), Integer.parseInt(data[0]), Integer.parseInt(data[1]), Integer.parseInt(data[2]), Float.parseFloat(data[3]),Float.parseFloat(data[4]));
                                      p.teleport(moveTo);
                                  }
                                  SendChat(p, ChatColor.RED + "You've been banned from " + banworld + " for " + timedisp((banstop(duration) - System.currentTimeMillis())/1000));
                              }
                         this.getConfig().set("bans", bans);
                         this.saveConfig();
                              break;
                          case 1:
                              SendChat(player, args[1] + " is already banned on " + banworld + "!");
                              break;
                          case 2:
                              SendChat(player, "Exception during ban processing.");
                              break;
                          }
                      }
                      break;
                  case "pardon":
                      if (args.length == 1) {
                          SendChat(player, "You need to tell me at least the player name.");
                      } else {
                          String banned = getUUID(args[1]), banworld = player.getWorld().getName();
                          if (banned == "Unknown") {
                              SendChat(player, "That player doesn't appear to be valid.");
                              return true;
                          }
                          if (args.length > 2) banworld = args[2];
                          switch (pardon(banned, banworld)) {
                          case 0:
                              SendChat(player, args[1] + " has been pardoned!");
                              Player p = this.getServer().getPlayer(args[1]);
                              if (p != null) SendChat(p, ChatColor.GREEN + "Your ban on " + banworld + " has been lifted!");
                         this.getConfig().set("bans", bans);
                         this.saveConfig();
                              break;
                          case 1:
                              SendChat(player, args[1] + " is not banned from " + banworld);
                              break;
                          case 2:
                              SendChat(player, args[1] + " has no bans with which to pardon.");
                              break;
                          case 3:
                              SendChat(player, "Exception during ban processing.");
                              break;
                          }
                      }
                      break;
                  case "setspawn":
                  this.getConfig().set("bans", bans);
                  this.saveConfig();
                      break;
                  }
              }
              break;
            }
          }
          return true;
        } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during onCommand!");
              ex.printStackTrace();
          return false;
        }
      }
      public String getPN(String strUUID) {
          String ret = "", cached = UUIDcache(strUUID);
          if (!cached.isEmpty()) {
              return cached;
          } else {
              Player p = getServer().getPlayer(strUUID);
              if (p == null) {
                NameFetcher nf = new NameFetcher(Arrays.asList(UUID.fromString(strUUID)));
                Map<UUID, String> response = null;
                try {
                    response = nf.call();
                        Iterator<Map.Entry<UUID, String>> bi = response.entrySet().iterator();
                        while (bi.hasNext()) {
                            Map.Entry<UUID, String> banpair = (Map.Entry<UUID, String>)bi.next();
                            if (banpair.getKey().toString().equals(strUUID)) ret = banpair.getValue();
                        }
                } catch (Exception e) {
                    getLogger().warning("Exception while running UUIDFetcher");
                    e.printStackTrace();
                }
                try {
                 Thread.sleep(300);
                 } catch (InterruptedException e) {
                 }      
                if (ret.equals("")) {
                        return "Unknown";
                    } else {
                        uuidCache.put(strUUID, ret);
                        this.getConfig().set("uuids", uuidCache); this.saveConfig();
                        return ret;
                    }
              } else {
                    uuidCache.put(strUUID, p.getName());
                    return p.getName();
              }
          }
      }
      public String getUUID(String player) {
          String cached = UUIDcache(player);
          if (!cached.isEmpty()) {
              return cached;
          } else {
              Player p = getServer().getPlayer(player); String ret = "";
              if (p == null) {
                UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList(player));
                Map<String, UUID> response = null;
                try {
                    response = fetcher.call();
                        Iterator<Map.Entry<String, UUID>> bi = response.entrySet().iterator();
                        while (bi.hasNext()) {
                            Map.Entry<String, UUID> banpair = (Map.Entry<String, UUID>)bi.next();
                            log.log(Level.INFO, "Banpair key: " + banpair.getKey() + ", value = " + banpair.getValue().toString());
                            if (banpair.getKey().toLowerCase().equals(player.toLowerCase())) { ret = banpair.getValue().toString(); break; }
                        }
                } catch (Exception e) {
                    getLogger().warning("Exception while running UUIDFetcher");
                    e.printStackTrace();
                }
                if (ret.equals("")) {
                    return "Unknown";
                } else {
                    String pl = getPN(ret); if (pl.equals("")) pl = player;
                    uuidCache.put(ret, pl);
                        this.getConfig().set("uuids", uuidCache); this.saveConfig();
                    return ret;
                }
              } else {
                  uuidCache.put(p.getUniqueId().toString(), player);
                  return p.getUniqueId().toString();
              }
          }
      }
    
      public void punish(Player p, String world) {
          try {
              if (this.getConfig().getInt("punishment_delay") > 0) {
                if (punishments.containsKey(p.getUniqueId().toString())) {
                    HashMap<String, Long> tmp = punishments.get(p.getUniqueId().toString());
                    if (tmp.containsKey(world)) {
                        Long lasttry = (tmp.get(world) - (System.currentTimeMillis())) / 1000;
                        if (lasttry > 0) {
                            p.getWorld().strikeLightning(p.getLocation());
                            SendChat(p, ChatColor.RED + "You have been punished for trying again too soon!");
                        } else {
                            tmp.remove(world);
                            if (tmp.isEmpty()) {
                                punishments.remove(p.getUniqueId().toString());
                            } else {
                                punishments.put(p.getUniqueId().toString(), tmp);
                            }
                        }
                    } else {
                        tmp.put(world, (System.currentTimeMillis() + (this.getConfig().getInt("punishment_delay") * 1000)));
                    }
                } else {
                    HashMap<String, Long> tmp = new HashMap<String, Long>();
                    tmp.put(world, (System.currentTimeMillis() + (this.getConfig().getInt("punishment_delay") * 1000)));
                    punishments.put(p.getUniqueId().toString(), tmp);
                }
              }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during punish!");
              ex.printStackTrace();
          }
      }
    
      public void listbans(Player p, String targetPlayer) {
          try {
                Iterator<Map.Entry<String, HashMap<String, Long>>> it = bans.entrySet().iterator(); int printed = 0;
                SendChat(p, ChatColor.YELLOW + "Active bans list" + ((targetPlayer.equals(""))?"":" for "+targetPlayer) + ":"); SendChat(p, "");
                while (it.hasNext()) {
                    String buffer = "";
                    Map.Entry<String, HashMap<String, Long>> pair = (Map.Entry<String, HashMap<String, Long>>)it.next();
                    String banned = pair.getKey();
                    Iterator<Map.Entry<String, Long>> bi = pair.getValue().entrySet().iterator();
                    while (bi.hasNext()) {
                        Map.Entry<String, Long> banpair = (Map.Entry<String, Long>)bi.next();
                        String banworld = banpair.getKey(); Long duration = banpair.getValue();
                        Long banlen = (duration - (System.currentTimeMillis())) / 1000;
                        if (banlen <= 0) {
                            //pardon(banned, banworld);
                        } else {
                            if (targetPlayer.equals("") || getUUID(targetPlayer).equals(banned)) {
                                buffer += ChatColor.GREEN + "  [" + ChatColor.DARK_GREEN + banworld + ChatColor.GREEN + "]: " + ChatColor.YELLOW + timedisp(banlen) + " left\n";
                            }
                        }
                    }
                    if (!buffer.equals("")) {
                        p.sendMessage(ChatColor.BLUE + getPN(banned) + "\n" + buffer);
                        p.sendMessage("");
                        printed++;
                    }
                }
                if (printed == 0) SendChat(p, ChatColor.BLUE + "No active bans!");
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during listbans!");
              ex.printStackTrace();
          }
      }
      public int pardon(String playerUUID, String world) {
          // return 0 = ban removed
          // return 1 = not banned on that world
          // return 2 = not banned anywhere
          // return 3 = exception
          try {
                HashMap<String, Long> tmp; int ret = 0;
                if (bans.containsKey(playerUUID)) {
                    tmp = bans.get(playerUUID);
                    if (tmp.containsKey(world)) {
                        tmp.remove(world);
                        if (tmp.containsKey(world + "_nether")) tmp.remove(world + "_nether");
                        if (tmp.containsKey(world + "_the_end")) tmp.remove(world + "_the_end");
                        ret = 0;
                    } else {
                        ret = 1;
                    }
                    if (Bukkit.getWorld(world + "_nether") != null && tmp.containsKey(world + "_nether")) { tmp.remove(world + "_nether"); ret = 1; }
                    if (Bukkit.getWorld(world + "_the_end") != null && tmp.containsKey(world + "_the_end")) { tmp.remove(world + "_the_end"); ret = 1; }
                    if (tmp.isEmpty()) {
                        bans.remove(playerUUID);
                    } else {
                        bans.put(playerUUID, tmp);
                    }
                    return ret;
                } else {
                    return 2;
                }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during pardon!");
              ex.printStackTrace(); return 3;
          }
      }
      public int ban(String playerUUID, String world, String duration) {
          // returns:
          // 0 = banned
          // 1 = already banned
          // 2 = exception
          try {
                HashMap<String, Long> tmp;
                if (bans.containsKey(playerUUID)) {
                    tmp = bans.get(playerUUID);
                    if (tmp.containsKey(world)) return 1;
                } else {
                    tmp = new HashMap<String, Long>();
                }
                tmp.put(world, banstop(duration));
                if (Bukkit.getWorld(world + "_nether") != null && !tmp.containsKey(world + "_nether")) tmp.put(world + "_nether", banstop(duration));
                if (Bukkit.getWorld(world + "_the_end") != null && !tmp.containsKey(world + "_the_end")) tmp.put(world + "_the_end", banstop(duration));
                bans.put(playerUUID, tmp);
                return 0;
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during ban!");
              ex.printStackTrace(); return 2;
          }
      }
      public Long banstop(String duration) {
          try {
            Long retval = 0l; String buffer = "";
            int[] multiplier = new int[116]; multiplier[100] = 86400; multiplier[104] = 3600; multiplier[109] = 60; multiplier[115] = 1;
            for (int x = 0; x < duration.length(); x++) {
                if ((int)duration.charAt(x) > 57) {
               retval += Long.parseLong(buffer) * multiplier[(int)duration.charAt(x)];
               buffer = "";
                } else {
                    buffer += duration.substring(x, x+1);
                }
            }
            return (retval*1000) + System.currentTimeMillis();
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during banstop!");
              ex.printStackTrace(); return 0l;
          }
      }
      public String timedisp(Long secs) {
          try {
            String retval = "";
            Long days = 0l, hours = 0l, minutes = 0l;
            days = secs / 86400;
           hours = (secs - (days * 86400)) / 3600;
           minutes = (secs - ((days * 86400) + (hours * 3600))) / 60;
           secs = (secs - ((days * 86400) + (hours * 3600) + (minutes * 60))) % 60;
           if (secs == 60) { minutes++; secs = 0l; }
           if (minutes == 60) { hours++; minutes = 0l; }
           if (hours == 24) { days++; hours = 0l; }
           if (days > 0) retval += days.toString() + pluralize(" day!s, ", days);
           if (hours > 0) retval += hours.toString() + pluralize(" hour!s, ", hours);
           if (minutes > 0) retval += minutes.toString() + pluralize(" minute!s, ", minutes);
           if (secs > 0) retval += secs.toString() + pluralize(" second!s, ", secs);
           return retval.substring(0, retval.length()-2);
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during timedisp!");
              ex.printStackTrace(); return "some amount of time";
          }
      }
      public String pluralize(String str, Long amount) {
          try {
            String ret = str.replace("!#", amount.toString());
            if (amount > 1 || amount == 0) {
                ret = ret.replace("!s", "s");
                ret = ret.replace("!ia", "are");
                ret = ret.replace("!ww", "were");
            } else {
                ret = ret.replace("!s", "");
                ret = ret.replace("!ia", "is");
                ret = ret.replace("!ww", "was");
            }
            return ret;
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during pluralize!");
              ex.printStackTrace(); return str;
          }
      }
        public void SendChat(Player player, String chatstring) {
            try {
                if (player == null) {
                    log.log(Level.INFO, "[WorldAccess]: " + chatstring);
                } else {
                    if (chatstring == null || chatstring.equals("")) {
                        player.sendMessage("");
                    } else {
                        player.sendMessage(ChatColor.GREEN + "[" + ChatColor.LIGHT_PURPLE + "WorldAccess" + ChatColor.GREEN + "]: " + ChatColor.RESET + chatstring);
                    }
                }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during SendChat!");
              ex.printStackTrace();
          }
      }
    
        public String UUIDcache(String lookup) {
            Iterator<Map.Entry<String, String>> it = uuidCache.entrySet().iterator(); String ret = "";
            while (it.hasNext()) {
                Map.Entry<String, String> tmp = (Map.Entry<String, String>)it.next();
                if (lookup.indexOf("-") > -1 && tmp.getKey().equals(lookup)) { ret = tmp.getValue(); break; }
                if (lookup.indexOf("-") == -1 && tmp.getValue().equals(lookup)) { ret = tmp.getKey(); break; }
            }
            return ret;
        }
      
      public void ReadConfig() {
          try {
              bans.clear(); uuidCache.clear();
              ConfigurationSection cfgBans = this.getConfig().getConfigurationSection("bans");
              ConfigurationSection cfgUUID = this.getConfig().getConfigurationSection("uuids");
              if (cfgBans != null) {
                for (String user : cfgBans.getKeys(false)) {
                    HashMap<String, Long> tmp = new HashMap<String, Long>();
                    ConfigurationSection cfgWorlds = cfgBans.getConfigurationSection(user);
                    for (String world : cfgWorlds.getKeys(false)) tmp.put(world, cfgWorlds.getLong(world));
                    bans.put(user, tmp);
                }
              }
              if (cfgUUID != null) {
                for (String uuid : cfgUUID.getKeys(false)) {
                    uuidCache.put(uuid, cfgUUID.getString(uuid));
                }
              }
          } catch (Exception ex) {
              log.log(Level.SEVERE, "[WorldAccess]: **** Exception during ReadConfig!");
              ex.printStackTrace();
          }
      }
    }
    
    I didn't know worlds had UUIDs too, that's cool. Thanks again!
     
Thread Status:
Not open for further replies.

Share This Page