Now buildable.

This commit is contained in:
Paul Reilly 2023-03-30 20:36:00 -05:00
parent 8a58782d99
commit ee39e6f534
15 changed files with 258 additions and 248 deletions

View File

@ -2,7 +2,6 @@ package me.totalfreedom.totalfreedommod;
import me.totalfreedom.totalfreedommod.banning.IndefiniteBanList;
import me.totalfreedom.totalfreedommod.config.YamlConfig;
import me.totalfreedom.totalfreedommod.permissions.PermissionConfig;
import me.totalfreedom.totalfreedommod.punishments.PunishmentList;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
@ -32,7 +31,6 @@ public class BackupManager extends FreedomService
{
createBackups(TotalFreedomMod.CONFIG_FILENAME, true);
createBackups(IndefiniteBanList.CONFIG_FILENAME);
createBackups(PermissionConfig.PERMISSIONS_FILENAME, true);
createBackups(PunishmentList.CONFIG_FILENAME);
createBackups("database.db");
}

View File

@ -8,30 +8,23 @@ import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.rank.Displayable;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FSync;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.StyleBuilderApplicable;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import java.util.Arrays;
import static me.totalfreedom.totalfreedommod.util.FUtil.playerMsg;
public class ChatManager extends FreedomService
{
@Override
@ -81,8 +74,7 @@ public class ChatManager extends FreedomService
}
// Tag
Component tag = Strings.isNullOrEmpty(fPlayer.getTag()) ? Component.empty() : FUtil.miniMessage(fPlayer.getTag())
.append(Component.space());
Component tag = fPlayer.getTag().append(Component.space());
// Nickname
Component nickname = player.displayName();
@ -102,7 +94,7 @@ public class ChatManager extends FreedomService
// Pinging
Arrays.stream(steamrolled.split(" ")).filter(string -> string.startsWith("@")).forEach(possiblePlayer ->
{
Player potential = server.getPlayer(possiblePlayer.replaceAll("@", ""));
Player potential = server.getPlayer(possiblePlayer.replace("@", ""));
// Ping only that particular player
if (potential != null)
@ -116,16 +108,20 @@ public class ChatManager extends FreedomService
}
});
// Did this because sonarlint was complaining about doing the unboxing in the if statement.
// Something about returning null because it was boxed... I'm not sure.
boolean unboxed = ConfigEntry.FOURCHAN_ENABLED.getBoolean();
// Chat colorization
// -- 4chan mode --
if (steamrolled.startsWith("> ") && ConfigEntry.FOURCHAN_ENABLED.getBoolean())
if (steamrolled.startsWith("> ") && unboxed)
{
message.append(Component.text(steamrolled, NamedTextColor.GREEN));
}
// -- Legacy chat colors --
else if (FUtil.containsChatColor(steamrolled))
{
message.append(FUtil.colorizeAsComponent(steamrolled.replaceAll("&k", "")));
message.append(FUtil.colorizeAsComponent(steamrolled.replace("&k", "")));
}
// -- MiniMessage --
else
@ -144,15 +140,14 @@ public class ChatManager extends FreedomService
Placeholder.component("message", filtered)));
}
public ChatColor getColor(Displayable display)
public TextColor getColor(Displayable display)
{
return display.getColor();
}
public String getColoredTag(Displayable display)
public Component getColoredTag(Displayable display)
{
ChatColor color = display.getColor();
return color + display.getAbbr();
return display.getColoredTag();
}
public void adminChat(CommandSender sender, String message)
@ -171,17 +166,21 @@ public class ChatManager extends FreedomService
if (!Strings.isNullOrEmpty(admin.getAcFormat()))
{
String format = admin.getAcFormat();
String flatAbv = FUtil.miniMessage(display.getAbbr());
player.sendMessage(FUtil.miniMessage(format,
Placeholder.unparsed("name", sender.getName()),
Placeholder.unparsed("rank", display.getAbbr()),
TagResolver.resolver("rankcolor", Tag.styling(TextColor.color(getColor(display).getColor().getRGB()))),
Placeholder.unparsed("rank", flatAbv),
TagResolver.resolver("rankcolor", Tag.styling(getColor(display))),
Placeholder.unparsed("message", message)));
}
else
} else
{
// OH GOD, WHY ARE WE DOING THIS
player.sendMessage("[" + ChatColor.AQUA + "ADMIN" + ChatColor.WHITE + "] " + ChatColor.DARK_RED + sender.getName() + ChatColor.DARK_GRAY + " [" + getColoredTag(display) + ChatColor.DARK_GRAY + "]" + ChatColor.WHITE + ": " + ChatColor.GOLD + FUtil.colorize(message));
Component defaultFormat = FUtil.miniMessage("<dark_gray>[<aqua>ADMIN<dark_gray>] <dark_red><name> <dark_gray><tag><dark_gray>]<white>: <gold><message>",
Placeholder.unparsed("<name>", sender.getName()),
Placeholder.component("<tag>", getColoredTag(display)),
Placeholder.unparsed("<message>", message));
player.sendMessage(defaultFormat);
}
});
}

View File

@ -168,8 +168,8 @@ public class Command_myadmin extends FreedomCommand
msgNew("Set admin chat format to \"<format>\"", Placeholder.unparsed("format", format));
msgNew("Example:");
msgNew(format, Placeholder.unparsed("name", "ExampleAdmin"),
Placeholder.unparsed("rank", GroupProvider.ADMIN.getGroup().getAbbr()),
TagResolver.resolver("rankcolor", Tag.styling(styler -> styler.color(TextColor.color(GroupProvider.ADMIN.getGroup().getColor().getColor().getRGB())))),
Placeholder.unparsed("rank", GroupProvider.ADMIN.getGroup().getAbbr().toString()),
TagResolver.resolver("rankcolor", Tag.styling(styler -> styler.color(GroupProvider.ADMIN.getGroup().getColor()))),
Placeholder.unparsed("message", "The quick brown fox jumped over the lazy dog."));
return true;
}

View File

@ -107,7 +107,7 @@ public class Command_saconfig extends FreedomCommand
plugin.dc.syncRoles(admin, plugin.pl.getData(admin.getName()).getDiscordID());
}
msgNew("Set <admin>'s rank to <rank>.", Placeholder.unparsed("admin", admin.getName()), Placeholder.unparsed("rank", rank.getName()));
msgNew("Set <admin>'s rank to <rank>.", Placeholder.unparsed("admin", admin.getName()), Placeholder.component("rank", rank.getName()));
return true;
}

View File

@ -4,6 +4,7 @@ import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.admin.AdminList;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -70,23 +71,10 @@ public class Module_list extends HTTPDModule
if (hasSpecialTitle(player) && plugin.al.isAdmin(player) && !plugin.al.isVanished(player.getUniqueId()))
{
Admin admin = plugin.al.getAdmin(player);
switch (admin.getRank())
{
case ADMIN:
{
admins.add(player.getName());
break;
}
case SENIOR_ADMIN:
{
senioradmins.add(player.getName());
break;
}
default:
{
// Do nothing
break;
}
if (admin.getRank().equals(GroupProvider.ADMIN.getGroup())) {
admins.add(player.getName());
} else if (admin.getRank().equals(GroupProvider.SENIOR_ADMIN.getGroup())) {
senioradmins.add(player.getName());
}
}
}

View File

@ -3,6 +3,7 @@ package me.totalfreedom.totalfreedommod.httpd.module;
import com.google.gson.Gson;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD;
import me.totalfreedom.totalfreedommod.rank.GroupProvider;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.entity.HumanEntity;
import org.json.simple.JSONArray;
@ -32,14 +33,13 @@ public class Module_players extends HTTPDModule
plugin.al.getActiveAdmins().stream().filter(admin -> admin.getName() != null).forEach(admin ->
{
switch (admin.getRank())
// Do nothing, keeps Codacy quiet
if (admin.getRank().equals(GroupProvider.ADMIN.getGroup()))
{
case ADMIN -> admins.add(admin.getName());
case SENIOR_ADMIN -> senioradmins.add(admin.getName());
default ->
{
// Do nothing, keeps Codacy quiet
}
admins.add(admin.getName());
} else if (admin.getRank().equals(GroupProvider.SENIOR_ADMIN.getGroup()))
{
senioradmins.add(admin.getName());
}
});

View File

@ -1,47 +1,31 @@
package me.totalfreedom.totalfreedommod.rank;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import net.kyori.adventure.text.format.TextColor;
public interface Displayable
{
Component getArticle();
String getArticle();
Component getName();
String getName();
Component getTag();
String getTag();
Component getAbbr();
String getAbbr();
Component getPlural();
String getPlural();
ChatColor getColor();
TextColor getColor();
org.bukkit.ChatColor getTeamColor();
String getColoredName();
Component getColoredName();
Component getColoredTag();
String getColoredLoginMessage();
Component getColoredLoginMessage();
boolean hasTeam();
boolean hasDefaultLoginMessage();
default Component generateColoredTag(String colorAndTag)
{
Component open = Component.text("[").color(NamedTextColor.DARK_GRAY);
Component close = Component.text("]").color(NamedTextColor.DARK_GRAY);
Component tagComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(colorAndTag);
return Component.join(JoinConfiguration
.builder()
.separator(Component.text(" "))
.build(), open, tagComponent, close);
}
}

View File

@ -1,13 +1,16 @@
package me.totalfreedom.totalfreedommod.rank;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.luckperms.api.model.data.DataType;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.node.types.PrefixNode;
import net.luckperms.api.node.types.WeightNode;
import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang.StringUtils;
import org.bukkit.util.FileUtil;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
@ -17,18 +20,18 @@ public class DisplayableGroup implements Displayable
{
private final Group group;
private final String name;
private final Component name;
private final String abbr;
private final String plural;
private final String article;
private final Component abbr;
private final Component plural;
private final Component article;
private final int weight;
private final String tag;
private final Component tag;
private final Component coloredTag;
private final ChatColor color;
private final TextColor color;
private final org.bukkit.ChatColor teamColor;
@ -37,10 +40,10 @@ public class DisplayableGroup implements Displayable
private final boolean hasDefaultLoginMessage;
public DisplayableGroup(String group,
String plural,
String tag,
Component plural,
Component tag,
int weight,
ChatColor color,
TextColor color,
org.bukkit.ChatColor teamColor,
boolean hasTeam,
boolean hasDefaultLoginMessage)
@ -56,7 +59,7 @@ public class DisplayableGroup implements Displayable
cfg.thenAcceptAsync(g -> {
WeightNode weightNode = WeightNode.builder(weight).build();
PrefixNode prefixNode = PrefixNode.builder().prefix(ChatColor.DARK_GRAY + "[" + color + tag + ChatColor.DARK_GRAY + "]" + getColor()).build();
PrefixNode prefixNode = PrefixNode.builder().prefix(FUtil.miniMessage(GroupProvider.OPEN.append(tag.color(color)).append(GroupProvider.CLOSE))).build();
g.getData(DataType.NORMAL).add(prefixNode);
g.getData(DataType.NORMAL).add(weightNode);
}).join(); // Block until the group is created and loaded.
@ -67,16 +70,16 @@ public class DisplayableGroup implements Displayable
}
this.group = matched;
this.name = (matched.getDisplayName() != null) ? matched.getDisplayName() : matched.getName();
this.name = (matched.getDisplayName() != null) ? FUtil.miniMessage(matched.getDisplayName()) : FUtil.miniMessage(matched.getName());
this.plural = plural;
this.article = StringUtils.startsWithAny(this.name.toLowerCase(Locale.ROOT), new String[]{"a", "e", "i", "o", "u"}) ? "an" : "a";
this.article = StringUtils.startsWithAny(this.name.toString().toLowerCase(Locale.ROOT), new String[]{"a", "e", "i", "o", "u"}) ? Component.text("an") : Component.text("a");
this.abbr = tag;
this.weight = weight;
this.tag = "[" + tag + "]";
this.tag = GroupProvider.OPEN.append(tag).append(GroupProvider.CLOSE);
this.color = color;
this.teamColor = teamColor;
this.hasTeam = hasTeam;
this.coloredTag = generateColoredTag(color + tag);
this.coloredTag = tag.color(color);
this.hasDefaultLoginMessage = hasDefaultLoginMessage;
}
@ -96,37 +99,37 @@ public class DisplayableGroup implements Displayable
}
@Override
public String getArticle()
public Component getArticle()
{
return this.article;
}
@Override
public String getName()
public Component getName()
{
return name;
}
@Override
public String getTag()
public Component getTag()
{
return tag;
}
@Override
public String getAbbr()
public Component getAbbr()
{
return abbr;
}
@Override
public String getPlural()
public Component getPlural()
{
return plural;
}
@Override
public ChatColor getColor()
public TextColor getColor()
{
return color;
}
@ -138,9 +141,9 @@ public class DisplayableGroup implements Displayable
}
@Override
public String getColoredName()
public Component getColoredName()
{
return color + name;
return name.color(color);
}
@Override
@ -150,9 +153,9 @@ public class DisplayableGroup implements Displayable
}
@Override
public String getColoredLoginMessage()
public Component getColoredLoginMessage()
{
return article + ' ' + color + name;
return article.append(Component.text(" ")).append(name.color(color));
}
@Override

View File

@ -1,8 +1,10 @@
package me.totalfreedom.totalfreedommod.rank;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.luckperms.api.model.user.User;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.util.Set;
@ -10,11 +12,14 @@ import java.util.Set;
@FunctionalInterface
public interface GroupProvider<T extends DisplayableGroup>
{
GroupProvider<DisplayableGroup> NON_OP = () -> new DisplayableGroup("non_op", "Non-Ops", "", 0, ChatColor.WHITE, null, false, false);
GroupProvider<DisplayableGroup> OP = () -> new DisplayableGroup("op", "Operators", "Op", 1, ChatColor.GREEN, null, false, false);
GroupProvider<DisplayableGroup> MASTER_BUILDER = () -> new DisplayableGroup("builder", "Master Builders", "MB", 2, ChatColor.DARK_AQUA, org.bukkit.ChatColor.DARK_AQUA, true, true);
GroupProvider<DisplayableGroup> ADMIN = () -> new DisplayableGroup("admin", "Administrators", "Admin", 3, ChatColor.DARK_GREEN, org.bukkit.ChatColor.DARK_GREEN, true, true);
GroupProvider<DisplayableGroup> SENIOR_ADMIN = () -> new DisplayableGroup("senior", "Senior Administrators", "SrA", 4, ChatColor.GOLD, org.bukkit.ChatColor.GOLD, true, true);
Component OPEN = Component.text("[", NamedTextColor.DARK_GRAY);
Component CLOSE = Component.text("]", NamedTextColor.DARK_GRAY);
GroupProvider<DisplayableGroup> NON_OP = () -> new DisplayableGroup("non_op", Component.text("Non-Ops"), Component.empty(), 0, NamedTextColor.WHITE, null, false, false);
GroupProvider<DisplayableGroup> OP = () -> new DisplayableGroup("op", Component.text("Operators"), Component.text("Op"), 1, NamedTextColor.GREEN, null, false, false);
GroupProvider<DisplayableGroup> MASTER_BUILDER = () -> new DisplayableGroup("builder", Component.text("Master Builders"), Component.text("MB"), 2, NamedTextColor.DARK_AQUA, ChatColor.DARK_AQUA, true, true);
GroupProvider<DisplayableGroup> ADMIN = () -> new DisplayableGroup("admin", Component.text("Administrators"), Component.text("Admin"), 3, NamedTextColor.DARK_GREEN, ChatColor.DARK_GREEN, true, true);
GroupProvider<DisplayableGroup> SENIOR_ADMIN = () -> new DisplayableGroup("senior", Component.text("Senior Administrators"), Component.text("SrA"), 4, NamedTextColor.GOLD, ChatColor.GOLD, true, true);
static User getUser(Player player)
{
@ -36,7 +41,8 @@ public interface GroupProvider<T extends DisplayableGroup>
};
}
static Set<GroupProvider<DisplayableGroup>> providerSet() {
static Set<GroupProvider<DisplayableGroup>> providerSet()
{
return Set.of(NON_OP, OP, MASTER_BUILDER, ADMIN, SENIOR_ADMIN);
}

View File

@ -61,7 +61,7 @@ public class RankManager extends FreedomService
if (ConfigEntry.SERVER_ASSISTANT_EXECUTIVES.getList().contains(player.getName()) && plugin.al.isAdmin(player))
{
return Title.ASSTEXEC;
return Title.ASST_EXEC;
}
// Master builders show up if they are not an admin
@ -236,9 +236,9 @@ public class RankManager extends FreedomService
{
return FUtil.colorize(ChatColor.AQUA + (message.contains("%name%") ? "" : player.getName() + " is ")
+ FUtil.colorize(message).replace("%name%", player.getName())
.replace("%rank%", display.getName())
.replace("%coloredrank%", display.getColoredName())
.replace("%art%", display.getArticle()));
.replace("%rank%", FUtil.miniMessage(display.getName()))
.replace("%coloredrank%", FUtil.miniMessage(display.getColoredName()))
.replace("%art%", FUtil.miniMessage(display.getArticle())));
}
return null;

View File

@ -1,47 +1,80 @@
package me.totalfreedom.totalfreedommod.rank;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import net.kyori.adventure.text.format.TextColor;
public enum Title implements Displayable
{
MASTER_BUILDER("a", "Master Builder", "Master Builders", ChatColor.DARK_AQUA, org.bukkit.ChatColor.DARK_AQUA, "MB", true, true),
EXECUTIVE("an", "Executive", "Executives", ChatColor.RED, org.bukkit.ChatColor.RED, "Exec", true, true),
ASSTEXEC("an", "Assistant Executive", "Assistant Executives", ChatColor.RED, org.bukkit.ChatColor.RED, "Asst Exec", true, true),
DEVELOPER("a", "Developer", "Developers", ChatColor.DARK_PURPLE, org.bukkit.ChatColor.DARK_PURPLE, "Dev", true, true),
OWNER("the", "Owner", "Owners", ChatColor.DARK_RED, org.bukkit.ChatColor.DARK_RED, "Owner", true, true);
MASTER_BUILDER(Component.text("a"),
Component.text("Master Builder"),
Component.text("Master Builders"),
NamedTextColor.DARK_AQUA,
org.bukkit.ChatColor.DARK_AQUA,
Component.text("MB"),
true,
true),
EXECUTIVE(Component.text("an"),
Component.text("Executive"),
Component.text("Executives"),
NamedTextColor.RED,
org.bukkit.ChatColor.RED,
Component.text("Exec"),
true,
true),
ASST_EXEC(Component.text("an"),
Component.text("Assistant Executive"),
Component.text("Assistant Executives"),
NamedTextColor.RED,
org.bukkit.ChatColor.RED,
Component.text("Asst Exec"),
true,
true),
DEVELOPER(Component.text("a"),
Component.text("Developer"),
Component.text("Developers"),
NamedTextColor.DARK_PURPLE,
org.bukkit.ChatColor.DARK_PURPLE,
Component.text("Dev"),
true,
true),
OWNER(Component.text("an"),
Component.text("Owner"),
Component.text("Owners"),
NamedTextColor.DARK_RED,
org.bukkit.ChatColor.DARK_RED,
Component.text("Owner"),
true,
true);
private final String article;
private final Component article;
private final String name;
private final Component name;
private final String abbr;
private final String plural;
private final Component abbr;
private final Component plural;
private final String tag;
private final Component tag;
private final Component coloredTag;
private final ChatColor color;
private final TextColor color;
private final org.bukkit.ChatColor teamColor;
private final boolean hasTeam;
private final boolean hasDefaultLoginMessage;
Title(String article, String name, String plural, ChatColor color, org.bukkit.ChatColor teamColor, String tag, Boolean hasTeam, Boolean hasDefaultLoginMessage)
Title(Component article, Component name, Component plural, TextColor color, org.bukkit.ChatColor teamColor, Component tag, Boolean hasTeam, Boolean hasDefaultLoginMessage)
{
this.article = article;
this.name = name;
this.plural = plural;
this.coloredTag = generateColoredTag(color + tag);
this.coloredTag = tag.color(color);
this.abbr = tag;
this.tag = "[" + tag + "]";
this.tag = GroupProvider.OPEN.append(tag).append(GroupProvider.CLOSE);
this.color = color;
this.teamColor = teamColor;
this.hasTeam = hasTeam;
@ -49,9 +82,9 @@ public enum Title implements Displayable
}
@Override
public String getColoredName()
public Component getColoredName()
{
return color + name;
return name.color(color);
}
@Override
@ -67,37 +100,37 @@ public enum Title implements Displayable
}
@Override
public String getColoredLoginMessage()
public Component getColoredLoginMessage()
{
return article + " " + color + name;
return article.append(Component.text(" ").append(name.color(color)));
}
@Override
public String getArticle()
public Component getArticle()
{
return article;
}
@Override
public String getName()
public Component getName()
{
return name;
}
@Override
public String getAbbr()
public Component getAbbr()
{
return abbr;
}
@Override
public String getPlural()
public Component getPlural()
{
return plural;
}
@Override
public String getTag()
public Component getTag()
{
return tag;
}
@ -109,7 +142,7 @@ public enum Title implements Displayable
}
@Override
public ChatColor getColor()
public TextColor getColor()
{
return color;
}

View File

@ -1,14 +1,5 @@
package me.totalfreedom.totalfreedommod.sql;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.banning.Ban;
@ -16,6 +7,10 @@ import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import java.sql.*;
import java.text.MessageFormat;
import java.util.UUID;
public class SQLite extends FreedomService
{
private final String FILE_NAME = "database.db";
@ -41,8 +36,7 @@ public class SQLite extends FreedomService
{
connection = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder() + "/" + FILE_NAME);
FLog.info("Successfully connected to the database.");
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to connect to the database: " + e.getMessage());
}
@ -56,8 +50,7 @@ public class SQLite extends FreedomService
{
connection.close();
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to disconnect from the database: " + e.getMessage());
}
@ -68,54 +61,65 @@ public class SQLite extends FreedomService
try
{
DatabaseMetaData meta = connection.getMetaData();
if (tableExists(meta, "bans"))
if (tableNotExists(meta, "bans"))
{
try
{
connection.createStatement().execute("CREATE TABLE `bans` (`name` VARCHAR, `uuid` VARCHAR, `ips` VARCHAR, `by` VARCHAR NOT NULL, `at` LONG NOT NULL, `expires` LONG, `reason` VARCHAR);");
}
catch (SQLException e)
{
FLog.severe("Failed to create the bans table: " + e.getMessage());
}
createBanTable();
}
if (tableExists(meta, "admins"))
if (tableNotExists(meta, "admins"))
{
try
{
connection.createStatement().execute("CREATE TABLE `admins` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `rank` VARCHAR NOT NULL, `active` BOOLEAN NOT NULL, `last_login` LONG NOT NULL, `command_spy` BOOLEAN NOT NULL, `potion_spy` BOOLEAN NOT NULL, `ac_format` VARCHAR);");
}
catch (SQLException e)
{
FLog.severe("Failed to create the admins table: " + e.getMessage());
}
createAdminsTable();
}
if (tableExists(meta, "players"))
if (tableNotExists(meta, "players"))
{
try
{
connection.createStatement().execute("CREATE TABLE `players` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `master_builder` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);");
}
catch (SQLException e)
{
FLog.severe("Failed to create the players table: " + e.getMessage());
}
createPlayersTable();
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to check tables on database: " + e.getMessage());
}
}
private void createPlayersTable()
{
try (PreparedStatement statement = connection.prepareStatement("CREATE TABLE `players` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `notes` VARCHAR, `tag` VARCHAR, `discord_id` VARCHAR, `master_builder` BOOLEAN NOT NULL, `ride_mode` VARCHAR NOT NULL, `coins` INT, `items` VARCHAR, `total_votes` INT NOT NULL, `display_discord` BOOLEAN NOT NULL, `login_message` VARCHAR, `inspect` BOOLEAN NOT NULL);"))
{
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to create the players table: " + e.getMessage());
}
}
private void createAdminsTable()
{
try (PreparedStatement statement = connection.prepareStatement("CREATE TABLE `admins` (`uuid` VARCHAR NOT NULL, `ips` VARCHAR NOT NULL, `rank` VARCHAR NOT NULL, `active` BOOLEAN NOT NULL, `last_login` LONG NOT NULL, `command_spy` BOOLEAN NOT NULL, `potion_spy` BOOLEAN NOT NULL, `ac_format` VARCHAR);"))
{
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to create the admins table: " + e.getMessage());
}
}
private void createBanTable()
{
try (PreparedStatement statement = connection.prepareStatement("CREATE TABLE `bans` (`name` VARCHAR, `uuid` VARCHAR, `ips` VARCHAR, `by` VARCHAR NOT NULL, `at` LONG NOT NULL, `expires` LONG, `reason` VARCHAR);"))
{
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to create the bans table: " + e.getMessage());
}
}
public void truncate(String table)
{
try
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM ?"))
{
connection.createStatement().execute("DELETE FROM " + table);
}
catch (SQLException e)
statement.setString(1, table);
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to truncate " + table + ": " + e.getMessage());
}
@ -123,12 +127,18 @@ public class SQLite extends FreedomService
public ResultSet getBanList() throws SQLException
{
return connection.createStatement().executeQuery("SELECT * FROM bans");
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM bans"))
{
return statement.executeQuery();
}
}
public ResultSet getAdminList() throws SQLException
{
return connection.createStatement().executeQuery("SELECT * FROM admins");
try (PreparedStatement statement = connection.prepareStatement("SELECT * FROM admins"))
{
return statement.executeQuery();
}
}
public void setAdminValue(Admin admin, String key, Object value)
@ -140,8 +150,7 @@ public class SQLite extends FreedomService
statement = setUnknownType(statement, 1, value);
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to update admin value:");
FLog.severe(e);
@ -157,8 +166,7 @@ public class SQLite extends FreedomService
statement = setUnknownType(statement, 1, value);
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to update player value: " + e.getMessage());
}
@ -169,25 +177,21 @@ public class SQLite extends FreedomService
if (value == null)
{
statement.setString(index, null);
}
else if (value.getClass().equals(String.class))
} else if (value.getClass().equals(String.class))
{
String v = (String)value;
String v = (String) value;
statement.setString(index, v);
}
else if (value.getClass().equals(Integer.class))
} else if (value.getClass().equals(Integer.class))
{
int v = (int)value;
int v = (int) value;
statement.setInt(index, v);
}
else if (value.getClass().equals(Boolean.class))
} else if (value.getClass().equals(Boolean.class))
{
boolean v = (boolean)value;
boolean v = (boolean) value;
statement.setBoolean(index, v);
}
else if (value.getClass().equals(Long.class))
} else if (value.getClass().equals(Long.class))
{
long v = (long)value;
long v = (long) value;
statement.setLong(index, v);
}
return statement;
@ -199,16 +203,13 @@ public class SQLite extends FreedomService
if (value instanceof String)
{
result = resultSet.getString(key);
}
else if (value instanceof Integer)
} else if (value instanceof Integer)
{
result = resultSet.getInt(key);
}
else if (value instanceof Boolean)
} else if (value instanceof Boolean)
{
result = resultSet.getObject(key);
}
else if (value instanceof Long)
} else if (value instanceof Long)
{
result = resultSet.getLong(key);
}
@ -217,9 +218,9 @@ public class SQLite extends FreedomService
public void addAdmin(Admin admin)
{
try
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO admins VALUES (?, ?, ?, ?, ?, ?, ?, ?)"))
{
PreparedStatement statement = connection.prepareStatement("INSERT INTO admins VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, admin.getUuid().toString());
statement.setString(2, FUtil.listToString(admin.getIps()));
statement.setString(3, admin.getRank().toString());
@ -229,8 +230,7 @@ public class SQLite extends FreedomService
statement.setBoolean(7, admin.getPotionSpy());
statement.setString(8, admin.getAcFormat());
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to add admin:");
FLog.severe(e);
@ -239,13 +239,12 @@ public class SQLite extends FreedomService
public void addPlayer(PlayerData player)
{
try
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
{
PreparedStatement statement = connection.prepareStatement("INSERT INTO players VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, player.getUuid().toString());
statement.setString(2, FUtil.listToString(player.getIps()));
statement.setString(3, FUtil.listToString(player.getNotes()));
statement.setString(4, player.getTag());
statement.setString(4, FUtil.miniMessage(player.getTag()));
statement.setString(5, player.getDiscordID());
statement.setBoolean(6, player.isMasterBuilder());
statement.setString(7, player.getRideMode().name());
@ -256,8 +255,7 @@ public class SQLite extends FreedomService
statement.setString(12, player.getLoginMessage());
statement.setBoolean(13, player.hasInspection());
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to add player:");
FLog.severe(e);
@ -266,15 +264,13 @@ public class SQLite extends FreedomService
public ResultSet getAdminByUuid(UUID uuid)
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM admins WHERE uuid=''{0}''", uuid.toString())))
{
ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM admins WHERE uuid=''{0}''", uuid.toString()));
if (resultSet.next())
{
return resultSet;
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to get admin by name:");
FLog.severe(e);
@ -285,15 +281,13 @@ public class SQLite extends FreedomService
public ResultSet getPlayerByUuid(UUID uuid)
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE uuid=''{0}''", uuid.toString())))
{
ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE uuid=''{0}''", uuid.toString()));
if (resultSet.next())
{
return resultSet;
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to get player by UUID:");
FLog.severe(e);
@ -304,11 +298,10 @@ public class SQLite extends FreedomService
public ResultSet getMasterBuilders()
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM players WHERE master_builder=true"))
{
return connection.createStatement().executeQuery("SELECT * FROM players WHERE master_builder=true");
}
catch (SQLException e)
return resultSet;
} catch (SQLException e)
{
FLog.severe("Failed to get Master Builders:");
FLog.severe(e);
@ -319,15 +312,13 @@ public class SQLite extends FreedomService
public ResultSet getPlayerByIp(String ip)
{
try
try (ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE ips LIKE ''%{0}%''", ip)))
{
ResultSet resultSet = connection.createStatement().executeQuery(MessageFormat.format("SELECT * FROM players WHERE ips LIKE ''%{0}%''", ip));
if (resultSet.next())
{
return resultSet;
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to get player by ip:");
FLog.severe(e);
@ -338,11 +329,11 @@ public class SQLite extends FreedomService
public void removeAdmin(Admin admin)
{
try
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM admins where name=?"))
{
connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM admins where name=''{0}''", admin.getName()));
}
catch (SQLException e)
statement.setString(1, admin.getName());
statement.executeUpdate();
} catch (SQLException e)
{
FLog.severe("Failed to remove admin:");
FLog.severe(e);
@ -351,9 +342,8 @@ public class SQLite extends FreedomService
public void addBan(Ban ban)
{
try
try (PreparedStatement statement = connection.prepareStatement("INSERT INTO bans VALUES (?, ?, ?, ?, ?, ?, ?)"))
{
PreparedStatement statement = connection.prepareStatement("INSERT INTO bans VALUES (?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, ban.getUsername());
String uuid = null;
if (ban.hasUUID())
@ -367,8 +357,7 @@ public class SQLite extends FreedomService
statement.setLong(6, ban.getExpiryUnix());
statement.setString(7, ban.getReason());
statement.executeUpdate();
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to add ban: " + e.getMessage());
}
@ -378,19 +367,28 @@ public class SQLite extends FreedomService
{
try
{
connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM bans WHERE name=''{0}''", ban.getUsername()));
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM bans WHERE name=?"))
{
statement.setString(1, ban.getUsername());
statement.executeUpdate();
}
for (String ip : ban.getIps())
{
connection.createStatement().executeUpdate(MessageFormat.format("DELETE FROM bans WHERE ips LIKE ''%{0}%''", ip));
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM bans WHERE ips LIKE %?%"))
{
statement.setString(1, ip);
statement.executeUpdate();
}
}
}
catch (SQLException e)
} catch (SQLException e)
{
FLog.severe("Failed to remove ban: " + e.getMessage());
}
}
public boolean tableExists(DatabaseMetaData meta, String name) throws SQLException
// We've changed this to read tableNotExists because it's more accurate in context.
public boolean tableNotExists(DatabaseMetaData meta, String name) throws SQLException
{
return !meta.getTables(null, null, name, null).next();
}

View File

@ -478,6 +478,7 @@ disable:
# Enable misc. features
random_teleport_max_distance: 10000
landmines_enabled: false
mp44_enabled: false
tossmob_enabled: false

View File

@ -141,7 +141,7 @@ public class AdminChatListener
return Title.EXECUTIVE;
} else if (role.getId().equals(SnowflakeEntry.ASSISTANT_EXECUTIVE_ROLE_ID.getSnowflake()))
{
return Title.ASSTEXEC;
return Title.ASST_EXEC;
} else if (role.getId().equals(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake()))
{
return GroupProvider.SENIOR_ADMIN.getGroup();

View File

@ -40,7 +40,7 @@ public class TFM_Bridge
return FUtil.miniMessage(Title.EXECUTIVE.getColoredTag());
} else if (role.getId().equals(SnowflakeEntry.ASSISTANT_EXECUTIVE_ROLE_ID.getSnowflake()))
{
return FUtil.miniMessage(Title.ASSTEXEC.getColoredTag());
return FUtil.miniMessage(Title.ASST_EXEC.getColoredTag());
} else if (role.getId().equals(SnowflakeEntry.SENIOR_ADMIN_ROLE_ID.getSnowflake()))
{
return FUtil.miniMessage(GroupProvider.SENIOR_ADMIN.getGroup().getColoredTag());