Guest User

command class

a guest
Dec 14th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.55 KB | None | 0 0
  1. public class GlobalCommand implements CommandExecutor {
  2.  
  3.     private final LuckPerms luckPerms;
  4.     private final HashMap<UUID, Long> cooldown;
  5.  
  6.     public GlobalCommand(LuckPerms luckPerms) {
  7.         this.luckPerms = luckPerms;
  8.         this.cooldown = new HashMap<>();
  9.     }
  10.  
  11.     @Override
  12.     public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
  13.  
  14.         String serverPrefix = McCityChat.plugin.getConfig().getString("Server Prefix");
  15.         String noPerm = McCityChat.plugin.getConfig().getString("No Permission");
  16.  
  17.         if (!(sender instanceof Player player)) {
  18.             sender.sendMessage("Must be a player");
  19.             return true;
  20.         }
  21.         if (!(args.length >= 1)) {
  22.             sender.sendMessage("incorrect use msg");
  23.             return true;
  24.         }
  25.  
  26.         if (!sender.hasPermission("mccitychat.global")) {
  27.             sender.sendMessage(MiniMessage.miniMessage().deserialize(serverPrefix + " " + noPerm));
  28.             return true;
  29.         }
  30.  
  31.         StringBuilder builder = new StringBuilder();
  32.         for (String string : args) {
  33.             builder.append(string).append(" ");
  34.         }
  35.         String msg = builder.toString().trim();
  36.  
  37.         User user = luckPerms.getPlayerAdapter(Player.class).getUser(player);
  38.         String prefix = user.getCachedData().getMetaData().getPrefix();
  39.         String suffix = user.getCachedData().getMetaData().getSuffix();
  40.         String chatPrefix = McCityChat.plugin.getConfig().getString("Global Prefix");
  41.         String color = McCityChat.plugin.getConfig().getString("Global Chat Color");
  42.         String chatSep = McCityChat.plugin.getConfig().getString("Chat Separator");
  43.  
  44.         if (!sender.hasPermission("mccitychat.bypass")) {
  45.             if (!cooldown.containsKey(player.getUniqueId())) {
  46.                 cooldown.put(player.getUniqueId(), System.currentTimeMillis());
  47.  
  48.                 String message = chatPrefix + " " + prefix + sender.getName() + suffix + chatSep + color + msg;
  49.  
  50.                 ByteArrayDataOutput out = ByteStreams.newDataOutput();
  51.                 out.writeUTF(GsonComponentSerializer.gson().serialize(MiniMessage.miniMessage().deserialize(message)));
  52.  
  53.                 player.sendPluginMessage(McCityChat.plugin, "mccity:chat", out.toByteArray());
  54.  
  55.             }
  56.             long timeElapsed = System.currentTimeMillis() - cooldown.get(player.getUniqueId());
  57.             if (timeElapsed >= McCityChat.plugin.getConfig().getLong("Global Cooldown") * 1000) {
  58.                 String message = chatPrefix + " " + prefix + sender.getName() + suffix + chatSep + color + msg;
  59.  
  60.                 ByteArrayDataOutput out = ByteStreams.newDataOutput();
  61.                 out.writeUTF(GsonComponentSerializer.gson().serialize(MiniMessage.miniMessage().deserialize(message)));
  62.  
  63.                 player.sendPluginMessage(McCityChat.plugin, "mccity:chat", out.toByteArray());
  64.                 cooldown.put(player.getUniqueId(), System.currentTimeMillis());
  65.                 return true;
  66.             } else {
  67.                 String cooldownMsgStr = McCityChat.plugin.getConfig().getString("Cooldown Message");
  68.                 if (cooldownMsgStr.contains("%c%")) {
  69.                     Long timeRemaining = ((McCityChat.plugin.getConfig().getLong("Global Cooldown") * 1000) - timeElapsed) / 1000;
  70.                     String timeRemainingStr = timeRemaining.toString();
  71.                     String cooldownMsg = cooldownMsgStr.replaceAll("%c%", timeRemainingStr);
  72.                     String cooldownMsgSend = serverPrefix + " " + cooldownMsg;
  73.  
  74.                     sender.sendMessage(MiniMessage.miniMessage().deserialize(cooldownMsgSend));
  75.                     return true;
  76.                 }
  77.                 String cooldownMsgSend = serverPrefix + " " + cooldownMsgStr;
  78.  
  79.                 ByteArrayDataOutput out = ByteStreams.newDataOutput();
  80.                 out.writeUTF(GsonComponentSerializer.gson().serialize(MiniMessage.miniMessage().deserialize(cooldownMsgSend)));
  81.  
  82.                 player.sendPluginMessage(McCityChat.plugin, "mccity:chat", out.toByteArray());
  83.                 return true;
  84.             }
  85.         }
  86.         String message = chatPrefix + " " + prefix + sender.getName() + suffix + chatSep + color + msg;
  87.  
  88.         ByteArrayDataOutput out = ByteStreams.newDataOutput();
  89.         out.writeUTF(GsonComponentSerializer.gson().serialize(MiniMessage.miniMessage().deserialize(message)));
  90.  
  91.         player.sendPluginMessage(McCityChat.plugin, "mccity:chat", out.toByteArray());
  92.         return true;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment