TFGuilds/src/main/java/me/totalfreedom/tfguilds/util/GUtil.java

249 lines
8.1 KiB
Java
Raw Normal View History

2020-06-18 22:24:02 +00:00
package me.totalfreedom.tfguilds.util;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import me.totalfreedom.tfguilds.TFGuilds;
2020-06-20 21:55:33 +00:00
import org.bukkit.scheduler.BukkitRunnable;
2020-06-18 22:24:02 +00:00
2020-06-22 17:51:32 +00:00
import java.text.DateFormat;
import java.text.SimpleDateFormat;
2020-06-18 22:24:02 +00:00
import java.util.Arrays;
2020-06-22 17:51:32 +00:00
import java.util.Date;
import java.util.HashMap;
2020-06-18 22:24:02 +00:00
import java.util.List;
2020-07-12 03:34:30 +00:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2020-06-18 22:24:02 +00:00
public class GUtil
{
public static TFGuilds plugin = TFGuilds.plugin;
2020-06-20 21:55:33 +00:00
public static HashMap<String, String> invitedPlayers = new HashMap<>();
2020-06-18 22:24:02 +00:00
public static boolean isConsole(CommandSender sender)
{
return sender instanceof ConsoleCommandSender;
}
public static void createGuild(CommandSender owner, String guildName)
{
// Set guild name & owner
2020-06-18 22:24:02 +00:00
plugin.guilds.set("guilds." + guildName, guildName);
plugin.guilds.set("guilds." + guildName + ".owner", owner.getName());
// Set guild moderator
2020-06-28 17:46:49 +00:00
List<String> moderators = plugin.guilds.getStringList("guilds." + guildName + ".moderators");
moderators.add(owner.getName());
plugin.guilds.set("guilds." + guildName + ".moderators", moderators);
// Set guild player
2020-06-18 22:24:02 +00:00
List<String> players = plugin.guilds.getStringList("guilds." + guildName + ".members");
players.add(owner.getName());
plugin.guilds.set("guilds." + guildName + ".members", players);
2020-06-22 17:51:32 +00:00
plugin.guilds.set("guilds." + guildName + ".tag", GUtil.color("&8[&7" + guildName + "&8]&r "));
// Set time guild was created
2020-06-22 17:51:32 +00:00
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
plugin.guilds.set("guilds." + guildName + ".created", dateFormat.format(date));
2020-06-18 22:24:02 +00:00
// Add guild to guild list
List<String> guilds = plugin.guilds.getStringList("list");
guilds.add(guildName);
plugin.guilds.set("list", guilds);
// Save everything & log guild creation
2020-06-18 22:24:02 +00:00
plugin.guilds.save();
GLog.info(owner.getName() + " has created a new guild: " + guildName);
}
public static void deleteGuild(CommandSender owner, String guildName)
{
GLog.info("Removing guilds.yml data for " + guildName);
plugin.guilds.set("guilds." + guildName, null);
List<String> guilds = plugin.guilds.getStringList("list");
guilds.remove(guildName);
plugin.guilds.set("list", guilds);
plugin.guilds.save();
}
2020-06-24 00:37:02 +00:00
public static void deleteGuild(String guildName)
{
GLog.info("Removing guilds.yml data for " + guildName);
plugin.guilds.set("guilds." + guildName, null);
List<String> guilds = plugin.guilds.getStringList("list");
guilds.remove(guildName);
plugin.guilds.set("list", guilds);
2020-06-24 00:37:02 +00:00
plugin.guilds.save();
}
2020-06-20 21:55:33 +00:00
public static void invitePlayer(Player player, String guild, int seconds)
{
if (seconds > 0)
{
2020-06-20 21:55:33 +00:00
invitedPlayers.put(player.getName(), guild);
player.sendMessage(ChatColor.GREEN + "To accept or decline, type /inviteguild accept or /inviteguild deny");
player.sendMessage(ChatColor.GREEN + "You have " + seconds + " seconds to accept the request before it gets declined automatically.");
}
2020-06-20 21:55:33 +00:00
new BukkitRunnable()
{
2020-06-20 21:55:33 +00:00
public void run()
{
if (!invitedPlayers.containsKey(player.getName()))
{
return;
}
2020-06-20 21:55:33 +00:00
invitedPlayers.remove(player.getName());
player.sendMessage(ChatColor.RED + "Your invitation has expired.");
}
}.runTaskLater(plugin, seconds * 20);
}
2020-06-18 22:24:02 +00:00
public static void setTag(String tag, String guildName)
{
plugin.guilds.set("guilds." + guildName + ".tag", tag);
plugin.guilds.save();
}
public static String getTag(String guildName)
{
return plugin.guilds.getString("guilds." + guildName + ".tag");
}
2020-06-22 17:51:32 +00:00
public static String getTimeCreated(String guildName)
{
return plugin.guilds.getString("guilds." + guildName + ".created");
}
2020-06-18 22:24:02 +00:00
public static boolean hasTag(String guildName)
{
return plugin.guilds.contains("guilds." + guildName + ".tag");
}
public static String getGuild(Player player)
{
String g = "";
boolean a = false;
ConfigurationSection guildMembers = plugin.guilds.getConfigurationSection("guilds");
if (guildMembers != null)
{
try
{
for (String guild : guildMembers.getKeys(false))
{
List<String> members = plugin.guilds.getStringList("guilds." + guild + ".members");
if (members.contains(player.getName()))
{
a = true;
g = guild;
}
}
}
catch (Exception e)
{
e.fillInStackTrace();
}
}
if (!a)
return null;
return g;
}
2020-06-22 21:48:42 +00:00
public static String getGuild(String arg)
{
String g = "";
boolean a = false;
ConfigurationSection guildMembers = plugin.guilds.getConfigurationSection("guilds");
if (guildMembers != null)
{
try
{
for (String guild : guildMembers.getKeys(false))
{
if (guild.equals(arg))
{
a = true;
g = guild;
}
}
}
catch (Exception e)
{
e.fillInStackTrace();
}
}
if (!a)
return null;
return g;
}
public static List<String> getGuilds()
{
return plugin.guilds.getStringList("list");
}
2020-06-18 22:24:02 +00:00
public static String getOwner(String guildName)
{
return plugin.guilds.getString("guilds." + guildName + ".owner");
}
2020-06-28 17:46:49 +00:00
public static List<String> getModerators(String guildName)
{
return plugin.guilds.getStringList("guilds." + guildName + ".moderators");
}
2020-06-18 22:24:02 +00:00
public static List<String> getMember(String guildName)
{
return plugin.guilds.getStringList("guilds." + guildName + ".members");
}
public static boolean isGuildMember(Player player, String guildName)
{
2020-06-28 17:46:49 +00:00
return getMember(guildName).contains(player.getName());
}
public static boolean isGuildModerator(Player player, String guildName)
{
return getModerators(guildName).contains(player.getName());
2020-06-18 22:24:02 +00:00
}
public static void guildChat(CommandSender sender, String message, String guildName)
{
String sent = ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "GC" + ChatColor.DARK_GRAY + "] " + getTag(guildName) + ChatColor.BLUE + sender
.getName() + ChatColor.GRAY + ": " + ChatColor.AQUA + message;
GLog.info(sent);
for (Player player : Bukkit.getOnlinePlayers())
{
if (isGuildMember(player, guildName))
{
player.sendMessage(sent);
}
}
}
public static List<String> BLACKLISTED_NAMES_AND_TAGS = Arrays.asList(
"admin", "owner", "moderator", "developer", "console", "dev", "staff", "mod", "sra", "tca", "sta", "sa");
public static String color(String s)
{
return ChatColor.translateAlternateColorCodes('&', s);
}
2020-07-12 03:34:30 +00:00
public static String translateHexColorCodes(String message)
{
Pattern pattern = Pattern.compile("&#[a-f0-9A-F]{6}");
Matcher matcher = pattern.matcher(message);
while (matcher.find())
{
String color = matcher.group().replace("&", "");
message = message.replace("&" + color, net.md_5.bungee.api.ChatColor.of(color) + "");
}
message = ChatColor.translateAlternateColorCodes('&', message);
return message;
}
2020-06-15 21:39:52 +00:00
}