Merge pull request #1 from superischroma/sql

move player and guild data to a SQL database
This commit is contained in:
speed 2020-12-08 16:25:23 -05:00 committed by GitHub
commit ffd0e1185f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 963 additions and 288 deletions

View file

@ -19,12 +19,23 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-codec:commons-codec:1.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-io:commons-io:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.16.4-R0.1-SNAPSHOT" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.spigotmc:spigot-api:1.16.4-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.16-R0.3" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.16-R0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.26" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.26" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-io:commons-io:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.commons:commons-lang3:3.11" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-codec:commons-codec:1.15" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: me.telesphoreo:mojangson:1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bstats:bstats-bukkit:1.7" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: io.papermc:paperlib:1.0.5" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: me.rayzr522:jsonmessage:1.0.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.reflections:reflections:0.9.12" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.javassist:javassist:3.27.0-GA" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.AtlasMediaGroup:TotalFreedomMod:development-0be2aa718f-1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: com.github.AtlasMediaGroup:TotalFreedomMod:development-0be2aa718f-1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.plugins:maven-checkstyle-plugin:3.1.1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.plugins:maven-checkstyle-plugin:3.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-artifact:3.0" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven:maven-artifact:3.0" level="project" />
@ -55,7 +66,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: xml-apis:xml-apis:1.3.04" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: xml-apis:xml-apis:1.3.04" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.httpcomponents:httpclient:4.0.2" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.httpcomponents:httpclient:4.0.2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.httpcomponents:httpcore:4.0.1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.httpcomponents:httpcore:4.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-codec:commons-codec:1.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-validator:commons-validator:1.3.1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: commons-validator:commons-validator:1.3.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-digester:commons-digester:1.6" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: commons-digester:commons-digester:1.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-logging:commons-logging:1.0.4" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: commons-logging:commons-logging:1.0.4" level="project" />
@ -75,7 +85,6 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.struts:struts-taglib:1.3.8" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.struts:struts-taglib:1.3.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.struts:struts-tiles:1.3.8" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.struts:struts-tiles:1.3.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-integration-tools:1.6" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.maven.doxia:doxia-integration-tools:1.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-io:commons-io:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-container-default:1.0-alpha-9" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: org.codehaus.plexus:plexus-container-default:1.0-alpha-9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: junit:junit:3.8.1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: junit:junit:3.8.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: classworlds:classworlds:1.1-alpha-2" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Maven: classworlds:classworlds:1.1-alpha-2" level="project" />

View file

@ -2,6 +2,7 @@
<project version="4"> <project version="4">
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<annotationProcessing> <annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true"> <profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" /> <sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />

View file

@ -8,6 +8,11 @@ import me.totalfreedom.tfguilds.command.TFGuildsCommand;
import me.totalfreedom.tfguilds.config.Config; import me.totalfreedom.tfguilds.config.Config;
import me.totalfreedom.tfguilds.listener.ChatListener; import me.totalfreedom.tfguilds.listener.ChatListener;
import me.totalfreedom.tfguilds.listener.JoinListener; import me.totalfreedom.tfguilds.listener.JoinListener;
import me.totalfreedom.tfguilds.sql.SQLDatabase;
import me.totalfreedom.tfguilds.sql.SQLGuildData;
import me.totalfreedom.tfguilds.sql.SQLRankData;
import me.totalfreedom.tfguilds.sql.SQLUserData;
import me.totalfreedom.tfguilds.sql.SQLWorldData;
import me.totalfreedom.tfguilds.util.GLog; import me.totalfreedom.tfguilds.util.GLog;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
@ -22,18 +27,24 @@ public final class TFGuilds extends JavaPlugin
} }
public Config config; public Config config;
public Config guilds;
public Config players;
public TFMBridge bridge; public TFMBridge bridge;
public SQLDatabase sql;
public SQLGuildData guildData;
public SQLRankData rankData;
public SQLWorldData worldData;
public SQLUserData userData;
@Override @Override
public void onEnable() public void onEnable()
{ {
plugin = this; plugin = this;
config = new Config("config.yml"); config = new Config("config.yml");
guilds = new Config("guilds.yml");
players = new Config("players.yml");
bridge = new TFMBridge(); bridge = new TFMBridge();
sql = new SQLDatabase();
guildData = new SQLGuildData();
rankData = new SQLRankData();
worldData = new SQLWorldData();
userData = new SQLUserData();
loadCommands(); loadCommands();
loadListeners(); loadListeners();
GLog.info("Enabled " + this.getDescription().getFullName()); GLog.info("Enabled " + this.getDescription().getFullName());
@ -44,8 +55,6 @@ public final class TFGuilds extends JavaPlugin
{ {
plugin = null; plugin = null;
config.save(); config.save();
guilds.save();
players.save();
GLog.info("Disabled " + this.getDescription().getFullName()); GLog.info("Disabled " + this.getDescription().getFullName());
} }

View file

@ -62,6 +62,11 @@ public class TFMBridge
public boolean isVanished(Player player) public boolean isVanished(Player player)
{ {
if (getTFM() == null)
{
GLog.warn("TFM not detected on the server.");
return false;
}
return getTFM().al.isVanished(player.getName()); return getTFM().al.isVanished(player.getName());
} }
} }

View file

@ -44,7 +44,7 @@ public class AddModSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasMember(player.getName())) if (!guild.hasMember(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!"); sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!");
return true; return true;
@ -56,13 +56,13 @@ public class AddModSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (guild.hasModerator(player.getName())) if (guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This member is already a moderator for this guild!"); sender.sendMessage(ChatColor.RED + "This member is already a moderator for this guild!");
return true; return true;
} }
guild.addModerator(player.getName()); guild.addModerator(player.getUniqueId());
sender.sendMessage(tl(PREFIX + "Added %s%" + player.getName() + "%p% as a moderator for %s%" + GUtil.colorize(guild.getName()) + "%p%.")); sender.sendMessage(tl(PREFIX + "Added %s%" + player.getName() + "%p% as a moderator for %s%" + GUtil.colorize(guild.getName()) + "%p%."));
guild.broadcast(tl("%s%" + player.getName() + " %p%has been made a moderator of your guild.")); guild.broadcast(tl("%s%" + player.getName() + " %p%has been made a moderator of your guild."));
guild.save(); guild.save();
@ -83,7 +83,7 @@ public class AddModSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't change who is a moderator in your guild!"); sender.sendMessage(ChatColor.RED + "You can't change who is a moderator in your guild!");
return true; return true;
@ -96,25 +96,25 @@ public class AddModSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (guild.getOwner().equals(n.getName())) if (guild.getOwner().equals(n.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "No need to make yourself a moderator!"); sender.sendMessage(ChatColor.RED + "No need to make yourself a moderator!");
return true; return true;
} }
if (!guild.hasMember(n.getName())) if (!guild.hasMember(n.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in your guild!"); sender.sendMessage(ChatColor.RED + "This player is not in your guild!");
return true; return true;
} }
if (guild.hasModerator(n.getName())) if (guild.hasModerator(n.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This member is already a moderator for your guild!"); sender.sendMessage(ChatColor.RED + "This member is already a moderator for your guild!");
return true; return true;
} }
guild.addModerator(n.getName()); guild.addModerator(n.getUniqueId());
sender.sendMessage(tl(PREFIX + "Added %s%" + n.getName() + "%p% as a moderator for your guild.")); sender.sendMessage(tl(PREFIX + "Added %s%" + n.getName() + "%p% as a moderator for your guild."));
guild.broadcast(tl("%s%" + n.getName() + " %p%has been made a moderator of your guild.")); guild.broadcast(tl("%s%" + n.getName() + " %p%has been made a moderator of your guild."));
guild.save(); guild.save();

View file

@ -38,7 +38,7 @@ public class CreateRankSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't create ranks for your guild!"); sender.sendMessage(ChatColor.RED + "You can't create ranks for your guild!");
return true; return true;

View file

@ -1,10 +1,11 @@
package me.totalfreedom.tfguilds.command; package me.totalfreedom.tfguilds.command;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.totalfreedom.tfguilds.Common; import me.totalfreedom.tfguilds.Common;
import me.totalfreedom.tfguilds.guild.Guild; import me.totalfreedom.tfguilds.guild.Guild;
import me.totalfreedom.tfguilds.util.GUtil; import me.totalfreedom.tfguilds.util.GUtil;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -12,9 +13,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CreateSubcommand extends Common implements CommandExecutor public class CreateSubcommand extends Common implements CommandExecutor
{ {
@Override @Override
@ -32,7 +30,7 @@ public class CreateSubcommand extends Common implements CommandExecutor
return true; return true;
} }
Player player = (Player)sender; Player player = (Player) sender;
String name = StringUtils.join(args, " ", 1, args.length); String name = StringUtils.join(args, " ", 1, args.length);
String identifier = GUtil.flatten(name); String identifier = GUtil.flatten(name);
@ -76,7 +74,6 @@ public class CreateSubcommand extends Common implements CommandExecutor
} }
Guild.createGuild(identifier, name, player); Guild.createGuild(identifier, name, player);
plugin.players.set(player.getName() + ".tag", true);
sender.sendMessage(tl(PREFIX + "Created a guild named \"" + GUtil.colorize(name) + "%p%\"!")); sender.sendMessage(tl(PREFIX + "Created a guild named \"" + GUtil.colorize(name) + "%p%\"!"));
broadcast(GUtil.colorize(tl("%p%" + sender.getName() + " has created guild %p%&l" + name))); broadcast(GUtil.colorize(tl("%p%" + sender.getName() + " has created guild %p%&l" + name)));
return true; return true;

View file

@ -35,7 +35,7 @@ public class DeleteRankSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't delete ranks from your guild!"); sender.sendMessage(ChatColor.RED + "You can't delete ranks from your guild!");
return true; return true;

View file

@ -5,7 +5,6 @@ import me.totalfreedom.tfguilds.guild.Guild;
import me.totalfreedom.tfguilds.util.GLog; import me.totalfreedom.tfguilds.util.GLog;
import me.totalfreedom.tfguilds.util.GUtil; import me.totalfreedom.tfguilds.util.GUtil;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@ -40,7 +39,7 @@ public class DisbandSubcommand extends Common implements CommandExecutor
} }
String n = guild.getName(); String n = guild.getName();
GLog.info("Removing guilds.yml data for " + n); GLog.info("Removing guild data for " + n);
guild.disband(); guild.disband();
GLog.info(sender.getName() + " deleted guild " + guild.getName()); GLog.info(sender.getName() + " deleted guild " + guild.getName());
sender.sendMessage(tl(PREFIX + "Disbanded \"" + GUtil.colorize(n) + "%p%\".")); sender.sendMessage(tl(PREFIX + "Disbanded \"" + GUtil.colorize(n) + "%p%\"."));
@ -62,13 +61,13 @@ public class DisbandSubcommand extends Common implements CommandExecutor
} }
Guild guild = Guild.getGuild(player); Guild guild = Guild.getGuild(player);
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You are not the owner of this guild!"); sender.sendMessage(ChatColor.RED + "You are not the owner of this guild!");
return true; return true;
} }
GLog.info("Removing guilds.yml data for " + guild.getName()); GLog.info("Removing guild data for " + guild.getName());
guild.disband(); guild.disband();
GLog.info(player.getName() + " deleted guild " + guild.getName()); GLog.info(player.getName() + " deleted guild " + guild.getName());
sender.sendMessage(tl(PREFIX + "You have disbanded your guild!")); sender.sendMessage(tl(PREFIX + "You have disbanded your guild!"));

View file

@ -10,6 +10,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter; import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
public class GuildCommand extends Common implements CommandExecutor, TabCompleter public class GuildCommand extends Common implements CommandExecutor, TabCompleter
{ {
@ -84,7 +85,6 @@ public class GuildCommand extends Common implements CommandExecutor, TabComplete
@Override @Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args)
{ {
Guild guild = Guild.getGuild(sender);
if (args.length == 1) if (args.length == 1)
{ {
return Arrays.asList("addmod", "chat", "createrank", "create", return Arrays.asList("addmod", "chat", "createrank", "create",
@ -139,15 +139,25 @@ public class GuildCommand extends Common implements CommandExecutor, TabComplete
case "deleterank": case "deleterank":
case "setdefaultrank": case "setdefaultrank":
{ {
if (guild.getOwner().equals(sender.getName())) if (sender instanceof Player)
{ {
return guild.getRankNames(); Player player = (Player) sender;
Guild guild = Guild.getGuild(player);
if (guild.getOwner().equals(player.getUniqueId()))
{
return guild.getRankNames();
}
} }
} }
case "tp": case "tp":
{ {
return guild.getMembers(); if (sender instanceof Player)
{
Player player = (Player) sender;
Guild guild = Guild.getGuild(player);
return guild.getOnlyMembers();
}
} }
case "disband": case "disband":
@ -162,9 +172,14 @@ public class GuildCommand extends Common implements CommandExecutor, TabComplete
case "kick": case "kick":
{ {
if (guild.hasModerator(sender.getName())) if (sender instanceof Player)
{ {
return guild.getOnlyMembers(); Player player = (Player) sender;
Guild guild = Guild.getGuild(player);
if (guild.hasModerator(player.getUniqueId()))
{
return guild.getOnlyMembers();
}
} }
} }
@ -172,9 +187,14 @@ public class GuildCommand extends Common implements CommandExecutor, TabComplete
case "addmod": case "addmod":
case "setowner": case "setowner":
{ {
if (guild.getOwner().equals(sender.getName())) if (sender instanceof Player)
{ {
return guild.getMembers(); Player player = (Player) sender;
Guild guild = Guild.getGuild(player);
if (guild.getOwner().equals(player.getUniqueId()))
{
return guild.getOnlyMembers();
}
} }
} }
} }

View file

@ -38,7 +38,7 @@ public class HomeSubcommand extends Common implements CommandExecutor
{ {
if (args[1].equalsIgnoreCase("set")) if (args[1].equalsIgnoreCase("set"))
{ {
if (!guild.hasModerator(player.getName())) if (!guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't modify your guild's home!"); sender.sendMessage(ChatColor.RED + "You can't modify your guild's home!");
return true; return true;

View file

@ -59,7 +59,7 @@ public class JoinSubcommand extends Common implements CommandExecutor
INVITES.remove(player); INVITES.remove(player);
} }
guild.addMember(player.getName()); guild.addMember(player.getUniqueId());
guild.broadcast(tl("%s%" + player.getName() + " %p%has joined the guild.")); guild.broadcast(tl("%s%" + player.getName() + " %p%has joined the guild."));
guild.save(); guild.save();
return true; return true;

View file

@ -44,19 +44,19 @@ public class KickSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasMember(player.getName())) if (!guild.hasMember(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!"); sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!");
return true; return true;
} }
if (guild.getOwner().equals(player.getName()) || guild.hasModerator(player.getName())) if (guild.getOwner().equals(player.getUniqueId()) || guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You cannot kick the owner/moderator(s) of a guild!"); sender.sendMessage(ChatColor.RED + "You cannot kick the owner/moderator(s) of a guild!");
return true; return true;
} }
guild.removeMember(player.getName()); guild.removeMember(player.getUniqueId());
sender.sendMessage(tl(PREFIX + "Kicked %s%" + player.getName() + "%p% from %s%" + GUtil.colorize(guild.getName()) + "%p%.")); sender.sendMessage(tl(PREFIX + "Kicked %s%" + player.getName() + "%p% from %s%" + GUtil.colorize(guild.getName()) + "%p%."));
player.sendMessage(tl("%s%You have been kicked from your guild.")); player.sendMessage(tl("%s%You have been kicked from your guild."));
guild.broadcast(tl("%s%" + player.getName() + " %p%has been kicked from your guild.")); guild.broadcast(tl("%s%" + player.getName() + " %p%has been kicked from your guild."));
@ -78,7 +78,7 @@ public class KickSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasModerator(player.getName())) if (!guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't kick people from your guild!"); sender.sendMessage(ChatColor.RED + "You can't kick people from your guild!");
return true; return true;
@ -91,19 +91,19 @@ public class KickSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasMember(n.getName())) if (!guild.hasMember(n.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in your guild!"); sender.sendMessage(ChatColor.RED + "This player is not in your guild!");
return true; return true;
} }
if (guild.getOwner().equals(n.getName()) || guild.hasModerator(n.getName()) && !guild.getOwner().equals(player.getName())) if (guild.getOwner().equals(n.getUniqueId()) || guild.hasModerator(n.getUniqueId()) && !guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You cannot kick the owner/moderator(s) of a guild!"); sender.sendMessage(ChatColor.RED + "You cannot kick the owner/moderator(s) of a guild!");
return true; return true;
} }
guild.removeMember(n.getName()); guild.removeMember(n.getUniqueId());
sender.sendMessage(tl(PREFIX + "Kicked %s%" + n.getName() + "%p% from your guild.")); sender.sendMessage(tl(PREFIX + "Kicked %s%" + n.getName() + "%p% from your guild."));
n.sendMessage(tl("%s%You have been kicked from your guild.")); n.sendMessage(tl("%s%You have been kicked from your guild."));
guild.broadcast(tl("%s%" + n.getName() + " %p%has been kicked from your guild.")); guild.broadcast(tl("%s%" + n.getName() + " %p%has been kicked from your guild."));

View file

@ -34,14 +34,14 @@ public class LeaveSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (guild.getOwner().equals(player.getName())) if (guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You cannot leave as you are the owner!"); sender.sendMessage(ChatColor.RED + "You cannot leave as you are the owner!");
return true; return true;
} }
guild.removeModerator(player.getName()); guild.removeModerator(player.getUniqueId());
guild.removeMember(player.getName()); guild.removeMember(player.getUniqueId());
guild.save(); guild.save();
sender.sendMessage(tl("%p%You left your guild.")); sender.sendMessage(tl("%p%You left your guild."));
guild.broadcast(tl("%s%" + sender.getName() + " %p%left the guild.")); guild.broadcast(tl("%s%" + sender.getName() + " %p%left the guild."));

View file

@ -36,7 +36,7 @@ public class MOTDSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasModerator(player.getName())) if (!guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't modify your guild's MOTD!"); sender.sendMessage(ChatColor.RED + "You can't modify your guild's MOTD!");
return true; return true;

View file

@ -44,25 +44,28 @@ public class RemoveModSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (guild.getModerators().contains(sender.getName())) if (sender instanceof Player)
{ {
sender.sendMessage(ChatColor.RED + "You may not demote yourself."); if (guild.getModerators().contains(((Player) sender).getUniqueId()))
return true; {
sender.sendMessage(ChatColor.RED + "You may not demote yourself.");
return true;
}
} }
if (!guild.hasMember(player.getName())) if (!guild.hasMember(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!"); sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!");
return true; return true;
} }
if (!guild.hasModerator(player.getName())) if (!guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This member is not a moderator for this guild!"); sender.sendMessage(ChatColor.RED + "This member is not a moderator for this guild!");
return true; return true;
} }
guild.removeModerator(player.getName()); guild.removeModerator(player.getUniqueId());
sender.sendMessage(tl(PREFIX + "Removed %s%" + player.getName() + "%p% as a moderator for %s%" + GUtil.colorize(guild.getName()) + "%p%.")); sender.sendMessage(tl(PREFIX + "Removed %s%" + player.getName() + "%p% as a moderator for %s%" + GUtil.colorize(guild.getName()) + "%p%."));
guild.broadcast(tl("%s%" + player.getName() + " %p%has been made a member of your guild.")); guild.broadcast(tl("%s%" + player.getName() + " %p%has been made a member of your guild."));
guild.save(); guild.save();
@ -83,7 +86,7 @@ public class RemoveModSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't change who is a moderator in your guild!"); sender.sendMessage(ChatColor.RED + "You can't change who is a moderator in your guild!");
return true; return true;
@ -102,19 +105,19 @@ public class RemoveModSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasMember(n.getName())) if (!guild.hasMember(n.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in your guild!"); sender.sendMessage(ChatColor.RED + "This player is not in your guild!");
return true; return true;
} }
if (!guild.hasModerator(n.getName())) if (!guild.hasModerator(n.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This member is not a moderator for your guild!"); sender.sendMessage(ChatColor.RED + "This member is not a moderator for your guild!");
return true; return true;
} }
guild.removeModerator(n.getName()); guild.removeModerator(n.getUniqueId());
sender.sendMessage(tl(PREFIX + "Removed %s%" + n.getName() + "%p% as a moderator for your guild.")); sender.sendMessage(tl(PREFIX + "Removed %s%" + n.getName() + "%p% as a moderator for your guild."));
guild.broadcast(tl("%s%" + n.getName() + " %p%has been made a member of your guild.")); guild.broadcast(tl("%s%" + n.getName() + " %p%has been made a member of your guild."));
guild.save(); guild.save();

View file

@ -1,5 +1,7 @@
package me.totalfreedom.tfguilds.command; package me.totalfreedom.tfguilds.command;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.totalfreedom.tfguilds.Common; import me.totalfreedom.tfguilds.Common;
import me.totalfreedom.tfguilds.guild.Guild; import me.totalfreedom.tfguilds.guild.Guild;
import me.totalfreedom.tfguilds.util.GUtil; import me.totalfreedom.tfguilds.util.GUtil;
@ -11,9 +13,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RenameSubcommand extends Common implements CommandExecutor public class RenameSubcommand extends Common implements CommandExecutor
{ {
@Override @Override
@ -41,7 +40,7 @@ public class RenameSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't change the name of your guild!"); sender.sendMessage(ChatColor.RED + "You can't change the name of your guild!");
return true; return true;
@ -80,11 +79,7 @@ public class RenameSubcommand extends Common implements CommandExecutor
} }
} }
guild.disband(); guild.rename(newName);
guild.setIdentifier(GUtil.flatten(newName));
guild.setName(newName);
guild.updateRankIdentifiers();
guild.setTag(GUtil.colorize("&8[&7" + newName + "&8]"));
sender.sendMessage(tl(PREFIX + "Set %s%" + GUtil.colorize(newName) + "%p% as the new name of your guild%p%.")); sender.sendMessage(tl(PREFIX + "Set %s%" + GUtil.colorize(newName) + "%p% as the new name of your guild%p%."));
guild.broadcast(tl("%p%Your guild has been renamed to " + GUtil.colorize(newName) + "%p%.")); guild.broadcast(tl("%p%Your guild has been renamed to " + GUtil.colorize(newName) + "%p%."));
guild.save(); guild.save();

View file

@ -35,7 +35,7 @@ public class SetDefaultRankSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You do not have permissions to manage ranks in your guild!"); sender.sendMessage(ChatColor.RED + "You do not have permissions to manage ranks in your guild!");
return true; return true;

View file

@ -46,7 +46,7 @@ public class SetMemberSubcommand extends Common implements CommandExecutor
return true; return true;
} }
guild.addMember(player.getName()); guild.addMember(player.getUniqueId());
guild.broadcast(tl("%s%" + player.getName() + " %p%has joined the guild.")); guild.broadcast(tl("%s%" + player.getName() + " %p%has joined the guild."));
guild.save(); guild.save();
return true; return true;

View file

@ -44,20 +44,20 @@ public class SetOwnerSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (guild.getOwner().contains(player.getName())) if (guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is already the owner of that guild!"); sender.sendMessage(ChatColor.RED + "This player is already the owner of that guild!");
return true; return true;
} }
if (!guild.hasMember(player.getName())) if (!guild.hasMember(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!"); sender.sendMessage(ChatColor.RED + "This player is not in the specified guild!");
return true; return true;
} }
guild.removeModerator(player.getName()); guild.removeModerator(player.getUniqueId());
guild.setOwner(player.getName()); guild.setOwner(player.getUniqueId());
sender.sendMessage(tl(PREFIX + "Ownership has been transferred to %s%" + player.getName() + "%p% in %s%" + GUtil.colorize(guild.getName()) + "%p%.")); sender.sendMessage(tl(PREFIX + "Ownership has been transferred to %s%" + player.getName() + "%p% in %s%" + GUtil.colorize(guild.getName()) + "%p%."));
guild.broadcast(tl("%s%" + player.getName() + " %p%has been made the owner of your guild.")); guild.broadcast(tl("%s%" + player.getName() + " %p%has been made the owner of your guild."));
guild.save(); guild.save();
@ -78,7 +78,7 @@ public class SetOwnerSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't change who is the owner of your guild!"); sender.sendMessage(ChatColor.RED + "You can't change who is the owner of your guild!");
return true; return true;
@ -97,14 +97,14 @@ public class SetOwnerSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasMember(n.getName())) if (!guild.hasMember(n.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in your guild!"); sender.sendMessage(ChatColor.RED + "This player is not in your guild!");
return true; return true;
} }
guild.removeModerator(n.getName()); guild.removeModerator(n.getUniqueId());
guild.setOwner(n.getName()); guild.setOwner(n.getUniqueId());
sender.sendMessage(tl(PREFIX + "Ownership has been transferred to %s%" + n.getName() + "%p% in your guild.")); sender.sendMessage(tl(PREFIX + "Ownership has been transferred to %s%" + n.getName() + "%p% in your guild."));
guild.broadcast(tl("%s%" + n.getName() + " %p%has been made the owner of your guild.")); guild.broadcast(tl("%s%" + n.getName() + " %p%has been made the owner of your guild."));
guild.save(); guild.save();

View file

@ -37,7 +37,7 @@ public class SetRankSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasModerator(player.getName())) if (!guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't change the ranks of your guild members!"); sender.sendMessage(ChatColor.RED + "You can't change the ranks of your guild members!");
return true; return true;
@ -50,7 +50,7 @@ public class SetRankSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasMember(r.getName())) if (!guild.hasMember(r.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "This player is not in your guild!"); sender.sendMessage(ChatColor.RED + "This player is not in your guild!");
return true; return true;
@ -61,7 +61,7 @@ public class SetRankSubcommand extends Common implements CommandExecutor
{ {
for (GuildRank gr : guild.getRanks()) for (GuildRank gr : guild.getRanks())
{ {
gr.getMembers().remove(r.getName()); gr.getMembers().remove(r.getUniqueId());
} }
sender.sendMessage(tl(PREFIX + "Removed the rank of %s%" + r.getName() + "%p% in your guild.")); sender.sendMessage(tl(PREFIX + "Removed the rank of %s%" + r.getName() + "%p% in your guild."));
@ -79,10 +79,11 @@ public class SetRankSubcommand extends Common implements CommandExecutor
for (GuildRank gr : guild.getRanks()) for (GuildRank gr : guild.getRanks())
{ {
gr.getMembers().remove(r.getName()); gr.getMembers().remove(r.getUniqueId());
} }
rank.getMembers().add(r.getName()); rank.getMembers().add(r.getUniqueId());
rank.save();
sender.sendMessage(tl(PREFIX + "Set the rank of %s%" + r.getName() + "%p% in your guild to %s%" + rank.getName() + "%p%.")); sender.sendMessage(tl(PREFIX + "Set the rank of %s%" + r.getName() + "%p% in your guild to %s%" + rank.getName() + "%p%."));
r.sendMessage(tl("%p%Your rank in your guild has been set to %s%" + rank.getName() + "%p%.")); r.sendMessage(tl("%p%Your rank in your guild has been set to %s%" + rank.getName() + "%p%."));
guild.save(); guild.save();

View file

@ -65,7 +65,7 @@ public class SetStateSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.hasModerator(player.getName())) if (!guild.hasModerator(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't change the state of the guild!"); sender.sendMessage(ChatColor.RED + "You can't change the state of the guild!");
return true; return true;

View file

@ -32,8 +32,6 @@ public class TFGuildsCommand extends Common implements CommandExecutor
try try
{ {
plugin.config.load(); plugin.config.load();
plugin.guilds.load();
plugin.players.load();
GLog.info("All configs reloaded successfully"); GLog.info("All configs reloaded successfully");
sender.sendMessage(tl(PREFIX + "All configuration files have been reloaded successfully.")); sender.sendMessage(tl(PREFIX + "All configuration files have been reloaded successfully."));
return true; return true;

View file

@ -42,7 +42,7 @@ public class TPSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getMembers().contains(to.getName())) if (!guild.getMembers().contains(to.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "That player is not in your guild."); sender.sendMessage(ChatColor.RED + "That player is not in your guild.");
return true; return true;

View file

@ -36,7 +36,7 @@ public class TagSubcommand extends Common implements CommandExecutor
return true; return true;
} }
if (!guild.getOwner().equals(player.getName())) if (!guild.getOwner().equals(player.getUniqueId()))
{ {
sender.sendMessage(ChatColor.RED + "You can't modify your guild's tag!"); sender.sendMessage(ChatColor.RED + "You can't modify your guild's tag!");
return true; return true;

View file

@ -2,7 +2,7 @@ package me.totalfreedom.tfguilds.command;
import me.totalfreedom.tfguilds.Common; import me.totalfreedom.tfguilds.Common;
import me.totalfreedom.tfguilds.guild.Guild; import me.totalfreedom.tfguilds.guild.Guild;
import me.totalfreedom.tfguilds.util.GUtil; import me.totalfreedom.tfguilds.user.User;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -36,19 +36,10 @@ public class ToggleTagSubcommand extends Common implements CommandExecutor
sender.sendMessage(PNF); sender.sendMessage(PNF);
return true; return true;
} }
User user = plugin.userData.get(player.getUniqueId());
boolean enabled = plugin.players.getBoolean(player.getName() + ".tag"); user.setTag(!user.isTag());
if (!plugin.players.contains(player.getName()) || enabled) user.save();
{ sender.sendMessage(tl(PREFIX + (user.isTag() ? "Enabled" : "Disabled") + " personal guild tag for " + player.getName() + "."));
plugin.players.set(player.getName() + ".tag", false);
plugin.players.save();
sender.sendMessage(tl(PREFIX + "Disabled personal guild tag for " + player.getName() + "."));
return true;
}
plugin.players.set(player.getName() + ".tag", true);
plugin.players.save();
sender.sendMessage(tl(PREFIX + "Enabled personal guild tag for " + player.getName() + "."));
return true; return true;
} }
@ -66,18 +57,10 @@ public class ToggleTagSubcommand extends Common implements CommandExecutor
return true; return true;
} }
boolean enabled = plugin.players.getBoolean(player.getName() + ".tag"); User user = plugin.userData.get(player.getUniqueId());
if (!plugin.players.contains(player.getName()) || enabled) user.setTag(!user.isTag());
{ user.save();
plugin.players.set(player.getName() + ".tag", false); sender.sendMessage(tl(PREFIX + (user.isTag() ? "Enabled" : "Disabled") + " personal guild tag."));
plugin.players.save();
sender.sendMessage(tl(PREFIX + "Disabled personal guild tag."));
return true;
}
plugin.players.set(player.getName() + ".tag", true);
plugin.players.save();
sender.sendMessage(tl(PREFIX + "Enabled personal guild tag."));
return true; return true;
} }
} }

View file

@ -1,10 +1,9 @@
package me.totalfreedom.tfguilds.config; package me.totalfreedom.tfguilds.config;
import java.io.File;
import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.TFGuilds;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
public class Config extends YamlConfiguration public class Config extends YamlConfiguration
{ {
private final TFGuilds plugin; private final TFGuilds plugin;

View file

@ -1,12 +1,12 @@
package me.totalfreedom.tfguilds.guild; package me.totalfreedom.tfguilds.guild;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import me.totalfreedom.tfguilds.TFGuilds;
import me.totalfreedom.tfguilds.Common; import me.totalfreedom.tfguilds.Common;
import me.totalfreedom.tfguilds.TFGuilds;
import me.totalfreedom.tfguilds.config.ConfigEntry; import me.totalfreedom.tfguilds.config.ConfigEntry;
import me.totalfreedom.tfguilds.util.GLog; import me.totalfreedom.tfguilds.util.GLog;
import me.totalfreedom.tfguilds.util.GUtil; import me.totalfreedom.tfguilds.util.GUtil;
@ -14,8 +14,7 @@ import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.CommandSender; import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class Guild public class Guild
@ -32,13 +31,13 @@ public class Guild
@Getter @Getter
@Setter @Setter
private String owner; private UUID owner;
@Getter @Getter
private final List<String> moderators; private final List<UUID> moderators;
@Getter @Getter
private final List<String> members; private final List<UUID> members;
@Getter @Getter
@Setter @Setter
@ -68,9 +67,9 @@ public class Guild
public Guild(String identifier, public Guild(String identifier,
String name, String name,
String owner, UUID owner,
List<String> members, List<UUID> members,
List<String> moderators, List<UUID> moderators,
String tag, String tag,
GuildState state, GuildState state,
List<GuildRank> ranks, List<GuildRank> ranks,
@ -95,77 +94,63 @@ public class Guild
public void save() public void save()
{ {
plugin.guilds.set(identifier + ".name", name); plugin.guildData.save(this);
plugin.guilds.set(identifier + ".owner", owner);
plugin.guilds.set(identifier + ".members", members);
plugin.guilds.set(identifier + ".moderators", moderators);
plugin.guilds.set(identifier + ".tag", tag);
plugin.guilds.set(identifier + ".state", state.name());
for (GuildRank rank : ranks)
{
rank.set();
}
plugin.guilds.set(identifier + ".motd", motd);
plugin.guilds.set(identifier + ".home", home);
plugin.guilds.set(identifier + ".creation", creation);
plugin.guilds.set(identifier + ".defaultrank", defaultRank);
plugin.guilds.save();
} }
public void addMember(String name) public void addMember(UUID uuid)
{ {
if (hasDefaultRank()) if (hasDefaultRank())
{ {
getRank(defaultRank).getMembers().add(name); getRank(defaultRank).getMembers().add(uuid);
} }
members.add(name); members.add(uuid);
} }
public void removeMember(String name) public void removeMember(UUID uuid)
{ {
Player player = Bukkit.getPlayer(name); Player player = Bukkit.getPlayer(uuid);
if (player != null) if (player != null)
{ {
Common.IN_GUILD_CHAT.remove(player); Common.IN_GUILD_CHAT.remove(player);
} }
members.remove(name); members.remove(uuid);
for (GuildRank gr : getRanks()) for (GuildRank gr : getRanks())
{ {
getRank(gr.getName()).getMembers().remove(name); getRank(gr.getName()).getMembers().remove(uuid);
} }
moderators.remove(name); moderators.remove(uuid);
} }
public boolean hasMember(String name) public boolean hasMember(UUID uuid)
{ {
return members.contains(name); return members.contains(uuid);
} }
public void addModerator(String name) public void addModerator(UUID uuid)
{ {
moderators.add(name); moderators.add(uuid);
} }
public void removeModerator(String name) public void removeModerator(UUID uuid)
{ {
moderators.remove(name); moderators.remove(uuid);
} }
public boolean hasModerator(String name) public boolean hasModerator(UUID uuid)
{ {
if (owner.equals(name)) if (owner.equals(uuid))
{ {
return true; return true;
} }
return moderators.contains(name); return moderators.contains(uuid);
} }
public void addRank(String name) public void addRank(String name)
{ {
ranks.add(new GuildRank(identifier, GUtil.flatten(name), name, new ArrayList<>())); ranks.add(GuildRank.createGuildRank(identifier, GUtil.flatten(name), name));
} }
public void removeRank(String name) public void removeRank(String name)
@ -236,23 +221,37 @@ public class Guild
{ {
for (Player player : Bukkit.getOnlinePlayers()) for (Player player : Bukkit.getOnlinePlayers())
{ {
if (members.contains(player.getName())) if (members.contains(player.getUniqueId()))
{ {
player.sendMessage(message); player.sendMessage(message);
} }
} }
} }
public List<String> getModeratorNames()
{
List<String> only = new ArrayList<>();
for (UUID moderator : moderators)
{
if (moderator.equals(owner))
continue;
OfflinePlayer player = Bukkit.getOfflinePlayer(moderator);
only.add(player.getName());
}
return only;
}
public List<String> getOnlyMembers() public List<String> getOnlyMembers()
{ {
List<String> only = new ArrayList<>(); List<String> only = new ArrayList<>();
for (String member : members) for (UUID member : members)
{ {
if (member.equals(owner) || moderators.contains(member)) if (member.equals(owner) || moderators.contains(member))
{ {
continue; continue;
} }
only.add(member); OfflinePlayer player = Bukkit.getOfflinePlayer(member);
only.add(player.getName());
} }
return only; return only;
} }
@ -270,14 +269,14 @@ public class Guild
public String getRoster() public String getRoster()
{ {
StringBuilder list = new StringBuilder(Common.PREFIX + "Guild Roster for " + name + "\n" + StringBuilder list = new StringBuilder(Common.PREFIX + "Guild Roster for " + name + "\n" +
"%s%Owner%p% - " + owner + "\n" + "%s%Owner%p% - " + Bukkit.getOfflinePlayer(owner).getName() + "\n" +
"%s%Moderators%p% - " + StringUtils.join(moderators, ", ") + "\n"); "%s%Moderators%p% - " + StringUtils.join(getModeratorNames(), ", ") + "\n");
for (GuildRank rank : ranks) for (GuildRank rank : ranks)
{ {
list.append("%s%") list.append("%s%")
.append(rank.getName()).append("%p% - ") .append(rank.getName()).append("%p% - ")
.append(StringUtils.join(rank.getMembers(), ", ")) .append(StringUtils.join(rank.getMemberNames(), ", "))
.append("\n"); .append("\n");
} }
@ -288,11 +287,8 @@ public class Guild
public static List<String> getGuildList() public static List<String> getGuildList()
{ {
List<String> g = new ArrayList<>(); List<String> g = new ArrayList<>();
for (String key : plugin.guilds.getKeys(false)) for (Guild guild : plugin.guildData.getAll())
{
Guild guild = getGuild(key);
g.add(guild.getName()); g.add(guild.getName());
}
return g; return g;
} }
@ -300,8 +296,8 @@ public class Guild
{ {
return Common.tl(Common.PREFIX + "Guild Information\n" + return Common.tl(Common.PREFIX + "Guild Information\n" +
"%s%Name%p%: " + name + "\n" + "%s%Name%p%: " + name + "\n" +
"%s%Owner%p%: " + owner + "\n" + "%s%Owner%p%: " + Bukkit.getOfflinePlayer(owner).getName() + "\n" +
"%s%Moderators%p%: " + StringUtils.join(moderators, ", ") + "\n" + "%s%Moderators%p%: " + StringUtils.join(getModeratorNames(), ", ") + "\n" +
"%s%Members%p%: " + StringUtils.join(getOnlyMembers(), ", ") + "\n" + "%s%Members%p%: " + StringUtils.join(getOnlyMembers(), ", ") + "\n" +
"%s%Tag%p%: " + (tag == null ? "None" : GUtil.colorize(tag)) + "\n" + "%s%Tag%p%: " + (tag == null ? "None" : GUtil.colorize(tag)) + "\n" +
"%s%State%p%: " + state.getDisplay() + "\n" + "%s%State%p%: " + state.getDisplay() + "\n" +
@ -339,129 +335,59 @@ public class Guild
public void disband() public void disband()
{ {
for (String member : members) for (UUID member : members)
{ {
Player player = Bukkit.getPlayer(member); Player player = Bukkit.getPlayer(member);
if (player == null) if (player == null)
{
continue; continue;
}
Common.IN_GUILD_CHAT.remove(player); Common.IN_GUILD_CHAT.remove(player);
} }
plugin.guilds.set(identifier, null); plugin.guildData.delete(this);
plugin.guilds.save(); }
public void rename(String name)
{
String oldIdentifier = this.identifier;
this.identifier = GUtil.flatten(name);
updateRankIdentifiers();
this.name = name;
this.tag = GUtil.colorize("&8[&7" + name + "&8]");
plugin.guildData.save(this, oldIdentifier);
} }
public void updateRankIdentifiers() public void updateRankIdentifiers()
{ {
for (GuildRank rank : ranks) for (GuildRank rank : ranks)
{ rank.updateGuildIdentifier(identifier);
rank.delete();
rank.setIguild(identifier);
rank.set();
plugin.guilds.save();
}
} }
public static Guild createGuild(String identifier, String name, Player owner) public static Guild createGuild(String identifier, String name, Player owner)
{ {
if (plugin.guilds.contains(identifier)) if (plugin.guildData.exists(identifier))
{
return getGuild(identifier); return getGuild(identifier);
}
Guild guild = new Guild(identifier, Guild guild = plugin.guildData.create(identifier, name, owner);
name,
owner.getName(),
Collections.singletonList(owner.getName()),
new ArrayList<>(),
ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + name + ChatColor.DARK_GRAY + "]",
GuildState.INVITE_ONLY,
new ArrayList<>(),
null,
null,
System.currentTimeMillis(),
null);
guild.save();
GLog.info(owner.getName() + " has created guild " + name); GLog.info(owner.getName() + " has created guild " + name);
return guild; return guild;
} }
public static Guild getGuild(String identifier) public static Guild getGuild(String identifier)
{ {
if (!plugin.guilds.contains(identifier)) return plugin.guildData.get(identifier);
{
return null;
}
List<GuildRank> ranks = new ArrayList<>();
ConfigurationSection rankcs = plugin.guilds.getConfigurationSection(identifier + ".ranks");
if (rankcs != null)
{
for (String key : rankcs.getKeys(false))
{
ranks.add(new GuildRank(identifier, key, plugin.guilds.getString(identifier + ".ranks." + key + ".name"),
plugin.guilds.getStringList(identifier + ".ranks." + key + ".members")));
}
}
return new Guild(identifier,
plugin.guilds.getString(identifier + ".name"),
plugin.guilds.getString(identifier + ".owner"),
plugin.guilds.getStringList(identifier + ".members"),
plugin.guilds.getStringList(identifier + ".moderators"),
plugin.guilds.getString(identifier + ".tag"),
GuildState.findState(plugin.guilds.getString(identifier + ".state")),
ranks,
plugin.guilds.getString(identifier + ".motd"),
plugin.guilds.getLocation(identifier + ".home"),
plugin.guilds.getLong(identifier + ".creation"),
plugin.guilds.getString(identifier + ".defaultrank"));
} }
public static Guild getGuild(Player player) public static Guild getGuild(Player player)
{ {
Guild guild = null; return plugin.guildData.get(player);
for (String key : plugin.guilds.getKeys(false))
{
Guild kg = getGuild(key);
if (kg.getMembers().contains(player.getName()))
{
guild = kg;
}
}
return guild;
}
public static Guild getGuild(CommandSender sender)
{
Guild guild = null;
for (String key : plugin.guilds.getKeys(false))
{
Guild kg = getGuild(key);
if (kg.getMembers().contains(sender.getName()))
{
guild = kg;
}
}
return guild;
} }
public static boolean guildExists(String identifier) public static boolean guildExists(String identifier)
{ {
return plugin.guilds.contains(identifier); return plugin.guildData.exists(identifier);
} }
public static boolean isInGuild(Player player) public static boolean isInGuild(Player player)
{ {
for (String key : plugin.guilds.getKeys(false)) return getGuild(player) != null;
{
Guild guild = getGuild(key);
if (guild.getMembers().contains(player.getName()))
{
return true;
}
}
return false;
} }
} }

View file

@ -1,46 +1,69 @@
package me.totalfreedom.tfguilds.guild; package me.totalfreedom.tfguilds.guild;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.tfguilds.TFGuilds; import me.totalfreedom.tfguilds.TFGuilds;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
public class GuildRank public class GuildRank
{ {
private static TFGuilds plugin = TFGuilds.getPlugin(); private static final TFGuilds plugin = TFGuilds.getPlugin();
// owning guild's identifier @Getter
@Setter
private String iguild; private String iguild;
// identifier // identifier
@Getter @Getter
private String identifier; private final String identifier;
// name // name
@Getter @Getter
private String name; private final String name;
// members of this rank // members of this rank
@Getter @Getter
private List<String> members; private final List<UUID> members;
public GuildRank(String iguild, String identifier, String name, List<String> members) public GuildRank(String iguild, String identifier, String name, List<UUID> members)
{ {
this.identifier = identifier;
this.iguild = iguild; this.iguild = iguild;
this.identifier = identifier;
this.name = name; this.name = name;
this.members = members; this.members = members;
} }
public void set() public List<String> getMemberNames()
{ {
plugin.guilds.set(iguild + ".ranks." + identifier + ".name", name); List<String> only = new ArrayList<>();
plugin.guilds.set(iguild + ".ranks." + identifier + ".members", members); for (UUID member : members)
{
OfflinePlayer player = Bukkit.getOfflinePlayer(member);
only.add(player.getName());
}
return only;
}
public static GuildRank createGuildRank(String guildIdentifier, String identifier, String name)
{
return plugin.rankData.create(guildIdentifier, identifier, name);
}
public void save()
{
plugin.rankData.save(this);
}
public void updateGuildIdentifier(String newIdentifier)
{
plugin.rankData.updateGuildIdentifier(this, newIdentifier);
this.iguild = newIdentifier;
} }
public void delete() public void delete()
{ {
plugin.guilds.set(iguild + ".ranks." + identifier, null); plugin.rankData.delete(this);
} }
} }

View file

@ -5,6 +5,7 @@ import me.totalfreedom.tfguilds.TFGuilds;
import me.totalfreedom.tfguilds.config.ConfigEntry; import me.totalfreedom.tfguilds.config.ConfigEntry;
import me.totalfreedom.tfguilds.guild.Guild; import me.totalfreedom.tfguilds.guild.Guild;
import me.totalfreedom.tfguilds.guild.GuildRank; import me.totalfreedom.tfguilds.guild.GuildRank;
import me.totalfreedom.tfguilds.user.User;
import me.totalfreedom.tfguilds.util.GUtil; import me.totalfreedom.tfguilds.util.GUtil;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -46,11 +47,11 @@ public class ChatListener implements Listener
String display; String display;
if (rank == null) if (rank == null)
{ {
if (guild.getOwner().equals(player.getName())) if (guild.getOwner().equals(player.getUniqueId()))
{ {
display = "Guild Owner"; display = "Guild Owner";
} }
else if (guild.hasModerator(player.getName())) else if (guild.hasModerator(player.getUniqueId()))
{ {
display = "Guild Moderator"; display = "Guild Moderator";
} }
@ -69,13 +70,8 @@ public class ChatListener implements Listener
return; return;
} }
if (plugin.players.contains(player.getName())) User user = plugin.userData.get(player.getUniqueId());
{ if (!user.isTag()) return;
if (!plugin.players.getBoolean(player.getName() + ".tag"))
{
return;
}
}
if (guild.hasTag()) if (guild.hasTag())
{ {

View file

@ -0,0 +1,80 @@
package me.totalfreedom.tfguilds.sql;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import me.totalfreedom.tfguilds.TFGuilds;
public class SQLDatabase
{
private static final String DATABASE_FILENAME = "database.db";
private static final TFGuilds plugin = TFGuilds.getPlugin();
private final File file;
public SQLDatabase()
{
File file = new File(plugin.getDataFolder(), DATABASE_FILENAME);
if (!file.exists())
{
try
{
file.createNewFile();
plugin.saveResource(DATABASE_FILENAME, false);
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
this.file = file;
}
public Connection getConnection()
{
try
{
Connection connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `users` (\n" +
"\t`id` INT,\n" +
"\t`uuid` TINYTEXT,\n" +
"\t`tag` BOOL\n" +
");").execute();
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `worlds` (\n" +
"\t`id` SMALLINT,\n" +
"\t`name` TINYTEXT\n" +
");").execute();
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `ranks` (\n" +
"\t`guild_identifier` TEXT,\n" +
"\t`identifier` TEXT,\n" +
"\t`name` TEXT,\n" +
"\t`members` TEXT\n" +
");").execute();
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `guilds` (\n" +
"\t`identifier` TEXT,\n" +
"\t`name` TEXT,\n" +
"\t`owner` INT,\n" +
"\t`moderators` TEXT,\n" +
"\t`members` TEXT,\n" +
"\t`tag` TEXT,\n" +
"\t`state` TINYINT,\n" +
"\t`ranks` TEXT,\n" +
"\t`motd` TEXT,\n" +
"\t`x` DOUBLE,\n" +
"\t`y` DOUBLE,\n" +
"\t`z` DOUBLE,\n" +
"\t`world` SMALLINT,\n" +
"\t`default_rank` TEXT,\n" +
"\t`creation` BIGINT\n" +
");").execute();
return connection;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
}

View file

@ -0,0 +1,213 @@
package me.totalfreedom.tfguilds.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import me.totalfreedom.tfguilds.TFGuilds;
import me.totalfreedom.tfguilds.guild.Guild;
import me.totalfreedom.tfguilds.guild.GuildRank;
import me.totalfreedom.tfguilds.guild.GuildState;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class SQLGuildData
{
private static final TFGuilds plugin = TFGuilds.getPlugin();
private static final String TABLE_NAME = "guilds";
private final String SELECT = "SELECT * FROM `" + TABLE_NAME + "` WHERE identifier=?";
private final String SELECT_MEMBER = "SELECT * FROM `" + TABLE_NAME + "` WHERE members LIKE ?";
private final String SELECT_ALL = "SELECT * FROM `" + TABLE_NAME + "`";
private final String UPDATE = "UPDATE `" + TABLE_NAME + "` SET identifier=?, name=?, owner=?, members=?, moderators=?, tag=?, state=?, ranks=?, motd=?, x=?, y=?, z=?, world=?, default_rank=? WHERE identifier=?";
private final String DELETE = "DELETE FROM `" + TABLE_NAME + "` WHERE identifier=?";
private final String INSERT = "INSERT INTO `" + TABLE_NAME + "` (`identifier`, `name`, `owner`, `members`, " +
"`moderators`, `tag`, `state`, `ranks`, `motd`, `x`, `y`, `z`, `world`, `default_rank`, `creation`) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
public boolean exists(String identifier)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, identifier);
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return false;
}
public Guild get(String identifier)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, identifier);
ResultSet set = statement.executeQuery();
boolean next = set.next();
if (!next) return null;
String name = set.getString("name");
UUID owner = plugin.userData.get(set.getInt("owner")).getUuid();
List<UUID> members = new ArrayList<>();
for (String stringMember : set.getString("members").split(","))
members.add(plugin.userData.get(Integer.parseInt(stringMember)).getUuid());
List<UUID> moderators = new ArrayList<>();
if (set.getString("moderators") != null)
{
for (String stringModerator : set.getString("moderators").split(","))
moderators.add(plugin.userData.get(Integer.parseInt(stringModerator)).getUuid());
}
String tag = set.getString("tag");
GuildState state = GuildState.values()[set.getInt("state")];
List<GuildRank> ranks = new ArrayList<>();
if (set.getString("ranks") != null)
{
for (String rankString : set.getString("ranks").split(","))
ranks.add(plugin.rankData.get(identifier, rankString));
}
String motd = set.getString("motd");
Location home = new Location(plugin.worldData.getWorld(set.getInt("world")),
set.getDouble("x"),
set.getDouble("y"),
set.getDouble("z"));
String defaultRank = set.getString("default_rank");
long creation = set.getLong("creation");
return new Guild(identifier, name, owner, members, moderators, tag, state, ranks, motd, home, creation, defaultRank);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public Guild get(Player player)
{
for (Guild guild : getAll())
{
if (guild.getMembers().contains(player.getUniqueId()))
return guild;
}
return null;
}
public List<Guild> getAll()
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT_ALL);
ResultSet set = statement.executeQuery();
List<Guild> guilds = new ArrayList<>();
while (set.next())
guilds.add(get(set.getString("identifier")));
return guilds;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public Guild create(String identifier, String name, Player owner)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(INSERT);
statement.setString(1, identifier);
statement.setString(2, name);
int ownerID = plugin.userData.get(owner.getUniqueId()).getId();
statement.setInt(3, ownerID);
statement.setString(4, "" + ownerID);
statement.setString(5, null);
statement.setString(6, "&8[&7" + name + "&8]");
statement.setInt(7, GuildState.INVITE_ONLY.ordinal());
statement.setString(8, null);
statement.setString(9, null);
statement.setDouble(10, 0.0);
statement.setDouble(11, 100.0);
statement.setDouble(12, 0.0);
statement.setInt(13, plugin.worldData.getWorldID(Bukkit.getWorlds().get(0)));
statement.setString(14, null);
long creation = System.currentTimeMillis();
statement.setLong(15, creation);
statement.execute();
return new Guild(identifier, name, owner.getUniqueId(), Collections.singletonList(owner.getUniqueId()), new ArrayList<>(),
ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + name + ChatColor.DARK_GRAY + "]",
GuildState.INVITE_ONLY, new ArrayList<>(), null, new Location(Bukkit.getWorlds().get(0), 0.0, 100.0, 0.0), creation, null);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public void save(Guild guild, String identifier)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(UPDATE);
statement.setString(1, guild.getIdentifier());
statement.setString(2, guild.getName());
statement.setString(3, "" + plugin.userData.get(guild.getOwner()).getId());
List<String> members = new ArrayList<>();
for (UUID member : guild.getMembers())
members.add("" + plugin.userData.get(member).getId());
statement.setString(4, members.size() == 0 ? null : StringUtils.join(members, ","));
List<String> moderators = new ArrayList<>();
for (UUID moderator : guild.getModerators())
moderators.add("" + plugin.userData.get(moderator).getId());
statement.setString(5, moderators.size() == 0 ? null : StringUtils.join(moderators, ","));
statement.setString(6, guild.getTag());
statement.setInt(7, guild.getState().ordinal());
List<String> stringRanks = new ArrayList<>();
for (GuildRank rank : guild.getRanks())
stringRanks.add(rank.getIdentifier());
statement.setString(8, stringRanks.size() == 0 ? null : StringUtils.join(stringRanks, ","));
statement.setString(9, guild.getMotd());
Location home = guild.getHome();
statement.setDouble(10, home == null ? 0.0 : home.getX());
statement.setDouble(11, home == null ? 100.0 : home.getY());
statement.setDouble(12, home == null ? 0.0 : home.getZ());
statement.setInt(13, home == null ? plugin.worldData.getWorldID(Bukkit.getWorlds().get(0)) : plugin.worldData.getWorldID(home.getWorld()));
statement.setString(14, guild.getDefaultRank());
statement.setString(15, identifier);
statement.execute();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
public void save(Guild guild)
{
save(guild, guild.getIdentifier());
}
public void delete(Guild guild)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(DELETE);
statement.setString(1, guild.getIdentifier());
statement.execute();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}

View file

@ -0,0 +1,137 @@
package me.totalfreedom.tfguilds.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import me.totalfreedom.tfguilds.TFGuilds;
import me.totalfreedom.tfguilds.guild.GuildRank;
import org.apache.commons.lang.StringUtils;
public class SQLRankData
{
private static final TFGuilds plugin = TFGuilds.getPlugin();
private static final String TABLE_NAME = "ranks";
private final String SELECT = "SELECT * FROM `" + TABLE_NAME + "` WHERE guild_identifier=? AND identifier=?";
private final String UPDATE = "UPDATE `" + TABLE_NAME + "` SET name=?, members=? WHERE guild_identifier=? AND identifier=?";
private final String UPDATE_GUILD = "UPDATE `" + TABLE_NAME + "` SET guild_identifier=? WHERE guild_identifier=? AND identifier=?";
private final String DELETE = "DELETE FROM `" + TABLE_NAME + "` WHERE guild_identifier=? AND identifier=?";
private final String INSERT = "INSERT INTO `" + TABLE_NAME + "` (`guild_identifier`, `identifier`, `name`, `members`) " +
"VALUES (?, ?, ?, ?);";
public boolean exists(String guildIdentifier, String identifier)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, guildIdentifier);
statement.setString(2, identifier);
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return false;
}
public GuildRank get(String guildIdentifier, String identifier)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, guildIdentifier);
statement.setString(2, identifier);
ResultSet set = statement.executeQuery();
set.next();
String name = set.getString("name");
List<UUID> members = new ArrayList<>();
if (set.getString("members") != null)
{
for (String stringMember : set.getString("members").split(","))
members.add(plugin.userData.get(Integer.parseInt(stringMember)).getUuid());
}
return new GuildRank(guildIdentifier, identifier, name, members);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public GuildRank create(String guildIdentifier, String identifier, String name)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(INSERT);
statement.setString(1, guildIdentifier);
statement.setString(2, identifier);
statement.setString(3, name);
statement.setString(4, null);
statement.execute();
return new GuildRank(guildIdentifier, identifier, name, new ArrayList<>());
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public void save(GuildRank rank)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(UPDATE);
statement.setString(1, rank.getName());
List<String> members = new ArrayList<>();
for (UUID member : rank.getMembers())
members.add("" + plugin.userData.get(member).getId());
statement.setString(2, members.size() == 0 ? null : StringUtils.join(members, ","));
statement.setString(3, rank.getIguild());
statement.setString(4, rank.getIdentifier());
statement.execute();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
public void updateGuildIdentifier(GuildRank rank, String newIdentifier)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(UPDATE_GUILD);
statement.setString(1, newIdentifier);
statement.setString(2, rank.getIguild());
statement.setString(3, rank.getIdentifier());
statement.execute();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
public void delete(GuildRank rank)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(DELETE);
statement.setString(1, rank.getIguild());
statement.setString(2, rank.getIdentifier());
statement.execute();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
}

View file

@ -0,0 +1,145 @@
package me.totalfreedom.tfguilds.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import me.totalfreedom.tfguilds.TFGuilds;
import me.totalfreedom.tfguilds.user.User;
public class SQLUserData
{
private static final TFGuilds plugin = TFGuilds.getPlugin();
private static final String TABLE_NAME = "users";
private final String SELECT = "SELECT * FROM `" + TABLE_NAME + "` WHERE uuid=?";
private final String SELECT_ID = "SELECT * FROM `" + TABLE_NAME + "` WHERE id=?";
private final String UPDATE = "UPDATE `" + TABLE_NAME + "` SET tag=? WHERE uuid=?";
private final String INSERT = "INSERT INTO `" + TABLE_NAME + "` (`id`, `uuid`, `tag`) " +
"VALUES (?, ?, ?);";
private final String COUNT = "SELECT COUNT(*) AS rows FROM `" + TABLE_NAME + "`";
public boolean exists(UUID uuid)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return false;
}
public boolean existsID(int id)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT_ID);
statement.setInt(1, id);
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return false;
}
public User get(UUID uuid)
{
if (!exists(uuid)) create(uuid);
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery();
set.next();
int id = set.getInt("id");
boolean tag = set.getBoolean("tag");
return new User(id, uuid, tag);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public User get(int id)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT_ID);
statement.setInt(1, id);
ResultSet set = statement.executeQuery();
set.next();
UUID uuid = UUID.fromString(set.getString("uuid"));
boolean tag = set.getBoolean("tag");
return new User(id, uuid, tag);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public User create(UUID uuid)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(INSERT);
int id = getUserCount() + 1;
statement.setInt(1, id);
statement.setString(2, uuid.toString());
statement.setBoolean(3, true);
statement.execute();
return new User(id, uuid, true);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public void save(User user)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(UPDATE);
statement.setBoolean(1, user.isTag());
statement.setString(2, user.getUuid().toString());
statement.execute();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
public int getUserCount()
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(COUNT);
ResultSet set = statement.executeQuery();
set.next();
int count = set.getInt("rows");
set.close();
return count;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return 0;
}
}

View file

@ -0,0 +1,115 @@
package me.totalfreedom.tfguilds.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import me.totalfreedom.tfguilds.TFGuilds;
import org.bukkit.Bukkit;
import org.bukkit.World;
public class SQLWorldData
{
private static final TFGuilds plugin = TFGuilds.getPlugin();
private final String SELECT = "SELECT * FROM `worlds` WHERE name=?";
private final String SELECT_ID = "SELECT * FROM `worlds` WHERE id=?";
private final String INSERT = "INSERT INTO `worlds` (`id`, `name`) VALUES (?, ?);";
private final String COUNT = "SELECT COUNT(*) AS rows FROM `worlds`";
public boolean exists(World world)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, world.getName());
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return false;
}
public boolean existsID(int id)
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(SELECT_ID);
statement.setInt(1, id);
ResultSet set = statement.executeQuery();
return set.next();
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return false;
}
public int getWorldID(World world)
{
try (Connection connection = plugin.sql.getConnection())
{
if (!exists(world))
{
PreparedStatement statement = connection.prepareStatement(INSERT);
statement.setInt(1, getWorldCount() + 1);
statement.setString(2, world.getName());
statement.execute();
}
PreparedStatement statement = connection.prepareStatement(SELECT);
statement.setString(1, world.getName());
ResultSet set = statement.executeQuery();
set.next();
int id = set.getInt("id");
set.close();
return id;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return -1;
}
public World getWorld(int id)
{
try (Connection connection = plugin.sql.getConnection())
{
if (!existsID(id)) return null;
PreparedStatement statement = connection.prepareStatement(SELECT_ID);
statement.setInt(1, id);
ResultSet set = statement.executeQuery();
set.next();
String name = set.getString("name");
set.close();
return Bukkit.getWorld(name);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return null;
}
public int getWorldCount()
{
try (Connection connection = plugin.sql.getConnection())
{
PreparedStatement statement = connection.prepareStatement(COUNT);
ResultSet set = statement.executeQuery();
set.next();
int count = set.getInt("rows");
set.close();
return count;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
return 0;
}
}

View file

@ -0,0 +1,27 @@
package me.totalfreedom.tfguilds.user;
import java.util.UUID;
import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.tfguilds.TFGuilds;
@Getter
public class User
{
private final int id;
private final UUID uuid;
@Setter
private boolean tag;
public User(int id, UUID uuid, boolean tag)
{
this.id = id;
this.uuid = uuid;
this.tag = tag;
}
public void save()
{
TFGuilds.getPlugin().userData.save(this);
}
}

View file

View file

@ -1,3 +0,0 @@
#
# Guild data for TFGuilds
#

View file

@ -1,3 +0,0 @@
#
# Player data for TFGuilds
#