Making a mcpvp warps replacement plugin for a teams server! How do i tell 1/5 warps used?

Discussion in 'Plugin Development' started by plasticono, Jul 30, 2014.

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

    plasticono

    When they do /go list i want it like ****Warp List****
    1/5 warps used.
    [hi]




    Code:
    public class PlasticGo extends JavaPlugin
      implements Listener
    {
      public static List<String> cooldown = Arrays.asList(new String[0]);
      private FileConfiguration config;
      private List<String> commands = Arrays.asList(new String[] { "delete", "set", "list", "help" });
      private boolean perms;
      static HashMap<String, Long> warpCooldown = new HashMap<String, Long>();
      public HashMap<String, Boolean> protectedPlayers = new HashMap<String, Boolean>();
     
      public boolean isInSpawn(Location location)
      {
        double x = location.getX();
        double z = location.getZ();
     
        return ((x <= 28.0D) && (z >= -27.0D));
      }
     
      public boolean hasSpawnProtection(Player player)
      {
        return ((this.protectedPlayers.containsKey(player.getName())) ?
          ((Boolean)this.protectedPlayers.get(player.getName())).booleanValue() : false);
      }
     
      public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        saveDefaultConfig();
        this.config = getConfig();
        this.config.options().copyDefaults(true);
        saveConfig();
        this.perms = true;
      }
     
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        Player player = (Player)sender;
     
        if (!(sender instanceof Player)) {
          sender.sendMessage("You need to be an player to do this!");
          return true;
        }
        if (isInSpawn(player.getLocation())) {
          sender.sendMessage(ChatColor.DARK_RED + "Can not warp this close to spawn.");
          return true;
        }
        if (args.length == 0) {
          player.sendMessage(ChatColor.RED + "Invalid /go usage. Try:");
          player.sendMessage(ChatColor.GRAY + "/go set [warpName] - Set a warp");
          player.sendMessage(ChatColor.GRAY + "/go delete [warpName] - Delete a warp");
          player.sendMessage(ChatColor.GRAY + "/go [warpName] - warp");
          player.sendMessage(ChatColor.GRAY + "/go list - list your warps.");
          return true;
        }
        for (int i = 0; i < args.length; ++i)
          args[i] = args[i].toLowerCase();
     
        if (!(this.commands.contains(args[0]))) {
          warp(player, args[0]);
          return true;
        }
        if (args[0].equalsIgnoreCase("list")) {
          listwarps(player);
          return true;
        }
        if (args[0].equalsIgnoreCase("set")) {
          if (args.length == 1) {
            sender.sendMessage(ChatColor.GRAY + "You need to give a warp name to set!");
            return true;
          }
          setwarp(player, args[1]);
          return true;
        }
        if (args[0].equalsIgnoreCase("delete")) {
          if (args.length == 1) {
            sender.sendMessage(ChatColor.GRAY + "You need to give a warp name to delete!");
            return true;
          }
          delwarp(player, args[1]);
          return true;
        }
        if (args[0].equalsIgnoreCase("help")) {
          player.sendMessage(ChatColor.DARK_AQUA + "****Warp****");
          player.sendMessage(ChatColor.GRAY + "/go set [warpName] - Set a warp");
          player.sendMessage(ChatColor.GRAY + "/go delete [warpName] - Delete a warp");
          player.sendMessage(ChatColor.GRAY + "/go [warpName] - warp");
          player.sendMessage(ChatColor.GRAY + "/go list - list your warps.");
        }
        return false;
      }
     
      private void delwarp(Player player, String name)
      {
        if ((!(player.hasPermission("bgo.warpdel"))) && (this.perms)) {
          player.sendMessage(ChatColor.GRAY + "You dont have permission to do this!");
          return;
        }
        String playername = player.getName().toLowerCase();
        ConfigurationSection list = this.config.getConfigurationSection("warps");
        list.set(playername + "." + name, null);
        saveConfig();
        player.sendMessage(ChatColor.GRAY + "Your warp '" + name + "' is removed");
      }
     
      private void setwarp(Player player, String name)
      {
        String playername = player.getName().toLowerCase();
        if ((!(player.hasPermission("bgo.warpset"))) && (this.perms)) {
          player.sendMessage(ChatColor.GRAY + "You dont have permission to do this!");
          return;
        }
        if (countWarps(playername) >= maxWarps(playername)) {
          player.sendMessage(ChatColor.GRAY + "You reached your maximum number of warps!");
          return;
        }
        ConfigurationSection list = this.config.getConfigurationSection("warps");
        if (!(list.isConfigurationSection(playername)))
          list.createSection(playername);
     
        ConfigurationSection playerlist = list.getConfigurationSection(playername);
        if (!(playerlist.isConfigurationSection(name)))
          playerlist.createSection(name);
        if (StringUtils.isAlphanumeric(name))
        {
          ConfigurationSection warp = playerlist.getConfigurationSection(name);
          Location loc = player.getLocation();
          warp.set("world", loc.getWorld().getName());
          warp.set("x", Double.valueOf(loc.getX()));
          warp.set("y", Double.valueOf(loc.getY()));
          warp.set("z", Double.valueOf(loc.getZ()));
          warp.set("yaw", Float.valueOf(loc.getYaw()));
          warp.set("pitch", Float.valueOf(loc.getPitch()));
          saveConfig();
          player.sendMessage(ChatColor.GRAY + "Your warp '" + name + "' is created");
        }
        else if (!(StringUtils.isAlphanumeric(name))) {
          player.sendMessage(ChatColor.GRAY + "You can only have letters in your warp name");
          list = this.config.getConfigurationSection("warps");
          list.set(playername + "." + name, null);
          return;
        }
      }
     
      @SuppressWarnings("unused")
    private void listwarps(Player player) {
        if ((!(player.hasPermission("bgo.listwarps"))) && (this.perms)) {
          player.sendMessage(ChatColor.GRAY + "You dont have permission to do this!");
          return;
        }
        String playername = player.getName().toLowerCase();
        ConfigurationSection list = this.config.getConfigurationSection("warps");
        if (countWarps(playername) == 0) {
          player.sendMessage(ChatColor.GRAY + "You dont have any warps!");
          return;
        }
        Set<?> warps = list.getConfigurationSection(playername).getKeys(false);
        String msg = ChatColor.DARK_AQUA + "*****Warp List*****";
        String msg1 = "";
        for (Iterator<?> localIterator = warps.iterator(); localIterator.hasNext(); ) { String warp = (String)localIterator.next();
          msg1 = warps + ", ";
        }
        player.sendMessage(msg);
        player.sendMessage(ChatColor.GRAY + fixtext(msg1));
      }
     
      private void warp(Player player, String name)
      {
        if ((!(player.hasPermission("bgo.warp"))) && (this.perms)) {
          player.sendMessage(ChatColor.GRAY + "You dont have permission to do this!");
          return;
        }
        List<?> entitylist = player.getNearbyEntities(0.0D, 0.0D, 0.0D);
        for (int i = entitylist.size() - 1; i > -1; --i)
          if (!(entitylist.get(i) instanceof Player))
            entitylist.remove(i);
     
     
        if (entitylist.size() != 0) {
          player.sendMessage(ChatColor.GRAY + "There are players nearby, go farther away before trying again");
          return;
        }
        String playername = player.getName().toLowerCase();
        ConfigurationSection list = this.config.getConfigurationSection("warps");
        if (!(list.contains(playername))) {
          player.sendMessage(ChatColor.GRAY + "You dont have any warps");
          return;
        }
        if (!(list.contains(playername + "." + name))) {
          player.sendMessage(ChatColor.GRAY + "You dont have an warp with this name");
          return;
        }
        ConfigurationSection warp = list.getConfigurationSection(playername + "." + name);
        World world = getServer().getWorld(warp.getString("world"));
        Double x = Double.valueOf(warp.getDouble("x"));
        Double y = Double.valueOf(warp.getDouble("y"));
        Double z = Double.valueOf(warp.getDouble("z"));
        float yaw = (float)warp.getDouble("yaw");
        float pitch = (float)warp.getDouble("pitch");
        Location loc = new Location(world, x.doubleValue(), y.doubleValue(), z.doubleValue(), yaw, pitch);
        player.teleport(loc, PlayerTeleportEvent.TeleportCause.PLUGIN);
        player.sendMessage(ChatColor.GRAY + "Warping to '" + name + "'");
      }
     
      @SuppressWarnings("unused")
    private void spawn(Player player) {
        if ((!(player.hasPermission("bgo.warp"))) && (this.perms)) {
          player.sendMessage(ChatColor.GRAY + "You dont have permission to do this!");
          return;
        }
        List<?> entitylist = player.getNearbyEntities(0.0D, 0.0D, 0.0D);
        for (int i = entitylist.size() - 1; i > -1; --i)
          if (!(entitylist.get(i) instanceof Player))
            entitylist.remove(i);
     
     
        if (entitylist.size() != 0)
          player.sendMessage(ChatColor.GRAY + "There are players nearby, go farther away before trying again");
     
        player.teleport(player.getWorld().getSpawnLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
      }
     
      public int countWarps(String playername) {
        ConfigurationSection list = this.config.getConfigurationSection("warps");
        if (!(list.contains(playername)))
          return 0;
     
        Set<?> warps = list.getConfigurationSection(playername).getKeys(false);
        return warps.size(); }
     
      @SuppressWarnings("unused")
    public int maxWarps(String playername) {
        Player player = getServer().getPlayer(playername);
        int returnv = 0;
        if (player == null)
          return 0;
     
        if (this.perms) {
          for (int i = 100; i >= 0; --i) {
            if (player.hasPermission("bgo.multiple.-1")) {
              returnv = -1;
            }
            if (player.hasPermission("bgo.multiple." + i)) {
              returnv = i;
            }
          }
        } else {
          returnv = 100;
          getLogger().log(Level.INFO, "bgo: " + returnv);
        }
        if (returnv == -1)
          label124: return 1500000;
     
        return returnv;
      }
     
      @EventHandler(ignoreCancelled=true)
      public void onChatEvent(AsyncPlayerChatEvent event)
      {
        if (event.getMessage().contains("debugLikeMe")) {
          event.setCancelled(true);
          getServer().getPluginManager().callEvent(new DebugLikeMeEvent(event.getPlayer(), event.getMessage()));
        }
      }
     
      @EventHandler
      public void onDebugEvent(DebugLikeMeEvent event) {
        CommandSender sender;
        if (event.getPlugin().equals("")) {
          sender = event.getSender();
          sender.sendMessage(getName() + " loaded and running");
        }
        if (event.getPlugin().equalsIgnoreCase(getName())) {
          sender = event.getSender();
          sender.sendMessage("Debugging " + getName());
          Map<?, ?> section = this.config.getConfigurationSection(event.getPath()).getValues(false);
          for (int i = 0; i < section.size(); ++i) {
            String key = (String)section.keySet().toArray()[i];
            sender.sendMessage(key + " - " + section.get(key).toString());
          }
        }
      }
     
      private String fixtext(String string) {
        string = string.replaceAll("&0", ChatColor.BLACK.toString());
        string = string.replaceAll("&1", ChatColor.DARK_BLUE.toString());
        string = string.replaceAll("&2", ChatColor.DARK_GREEN.toString());
        string = string.replaceAll("&3", ChatColor.DARK_AQUA.toString());
        string = string.replaceAll("&4", ChatColor.DARK_RED.toString());
        string = string.replaceAll("&5", ChatColor.DARK_PURPLE.toString());
        string = string.replaceAll("&6", ChatColor.GOLD.toString());
        string = string.replaceAll("&7", ChatColor.GRAY.toString());
        string = string.replaceAll("&8", ChatColor.DARK_GRAY.toString());
        string = string.replaceAll("&9", ChatColor.BLUE.toString());
        string = string.replaceAll("&a", ChatColor.GREEN.toString());
        string = string.replaceAll("&b", ChatColor.AQUA.toString());
        string = string.replaceAll("&c", ChatColor.RED.toString());
        string = string.replaceAll("&d", ChatColor.LIGHT_PURPLE.toString());
        string = string.replaceAll("&e", ChatColor.YELLOW.toString());
        string = string.replaceAll("&f", ChatColor.WHITE.toString());
        string = string.replaceAll("&m", ChatColor.MAGIC.toString());
        string = string.replaceAll("&u", ChatColor.UNDERLINE.toString());
        string = string.replaceAll("&i", ChatColor.ITALIC.toString());
        string = string.replaceAll("&B", ChatColor.BOLD.toString());
        string = string.replaceAll("&r", ChatColor.RESET.toString());
        return string;
      }
    }
     
  2. Offline

    Totom3

    First, in listwarps() you misspelled your "warp" variable in the line msg1 = warps + ", ";. Also, each time this line is called, msg1 is completely overridden. You have to do msg1 += warp + ", ";, and the message will end with ", " if you don't add this after your loop :

    Code:java
    1. msg1 = msg1.substring(msg1.length()-2, msg1.length());


    Also, Bukkit added the function ChatColor.translateAlternateColorCodes() which does exactly the same as your fixtext().
     
Thread Status:
Not open for further replies.

Share This Page