Skip to content

Commit

Permalink
Improve player data creation/loading and Add menu item override per tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Refrac committed Mar 17, 2024
1 parent 2361b08 commit f9bcc95
Show file tree
Hide file tree
Showing 22 changed files with 333 additions and 202 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.refracdevelopment</groupId>
<artifactId>SimpleTags</artifactId>
<version>1.4</version>
<version>1.5</version>
<packaging>jar</packaging>

<name>SimpleTags</name>
Expand Down
56 changes: 19 additions & 37 deletions src/main/java/me/refracdevelopment/simpletags/SimpleTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Getter
public final class SimpleTags extends JavaPlugin {
Expand Down Expand Up @@ -144,14 +145,11 @@ public void onEnable() {
@Override
public void onDisable() {
// Plugin shutdown logic
switch (dataType) {
case MYSQL:
getMySQLManager().shutdown();
break;
default:
getSqLiteManager().shutdown();
break;
}
if (Objects.requireNonNull(dataType) == DataType.MYSQL)
getMySQLManager().shutdown();
else
getSqLiteManager().shutdown();

getServer().getScheduler().cancelTasks(this);
}

Expand All @@ -168,19 +166,10 @@ public void loadFiles() {
tags = new Tags();
menus = new Menus();
commands = new Commands();
}

public void reloadFiles() {
getConfigFile().reload();
getTagsFile().reload();
getMenusFile().reload();
getLocaleFile().reload();
getCommandsFile().reload();

getSettings().loadConfig();
getTags().loadConfig();
getMenus().loadConfig();
getCommands().loadConfig();
Color.log("&c==========================================");
Color.log("&eAll files have been loaded correctly!");
Color.log("&c==========================================");
}

private void loadManagers() {
Expand All @@ -189,14 +178,10 @@ private void loadManagers() {
case "MYSQL":
dataType = DataType.MYSQL;
mySQLManager = new MySQLManager();
getMySQLManager().connect();
getMySQLManager().createT();
break;
default:
dataType = DataType.SQLITE;
sqLiteManager = new SQLiteManager();
getSqLiteManager().connect(getDataFolder().getAbsolutePath() + File.separator + "tags.db");
getSqLiteManager().createT();
sqLiteManager = new SQLiteManager(getDataFolder().getAbsolutePath() + File.separator + "tags.db");
break;
}

Expand Down Expand Up @@ -247,7 +232,6 @@ private void loadCommands() {
} catch (NoSuchFieldException | IllegalAccessException e) {
Color.log("&aFailed to load commands.");
e.printStackTrace();
return;
}
}

Expand All @@ -267,32 +251,30 @@ public void updateCheck(CommandSender sender, boolean console) {
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String input;
StringBuffer response = new StringBuffer();
StringBuilder response = new StringBuilder();
while ((input = reader.readLine()) != null) {
response.append(input);
}
reader.close();
JsonObject object = new JsonParser().parse(response.toString()).getAsJsonObject();
JsonObject object = JsonParser.parseString(response.toString()).getAsJsonObject();

if (object.has("plugins")) {
JsonObject plugins = object.get("plugins").getAsJsonObject();
JsonObject info = plugins.get(getDescription().getName()).getAsJsonObject();
String version = info.get("version").getAsString();
if (version.equals(getDescription().getVersion())) {
boolean archived = info.get("archived").getAsBoolean();

if (archived) {
sender.sendMessage(Color.translate("&cThis plugin has been marked as &e&l'Archived' &cby RefracDevelopment."));
sender.sendMessage(Color.translate("&cThis version will continue to work but will not receive updates or support."));
} else if (version.equals(getDescription().getVersion())) {
if (console) {
sender.sendMessage(Color.translate("&a" + getDescription().getName() + " is on the latest version."));
}
} else {
sender.sendMessage(Color.translate(""));
sender.sendMessage(Color.translate("&cYour " + getDescription().getName() + " version &7(" + getDescription().getVersion() + ") &cis out of date! Newest: &e&lv" + version));
sender.sendMessage(Color.translate(""));
sender.sendMessage(Color.translate("&cYour " + getDescription().getName() + " version is out of date!"));
sender.sendMessage(Color.translate("&cWe recommend updating ASAP!"));
sender.sendMessage(Color.translate(""));
sender.sendMessage(Color.translate("&cYour Version: &e" + getDescription().getVersion()));
sender.sendMessage(Color.translate("&aNewest Version: &e" + version));
sender.sendMessage(Color.translate(""));
sender.sendMessage(Color.translate(""));
return;
}
} else {
sender.sendMessage(Color.translate("&cWrong response from update API, contact plugin developer!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public void perform(CommandSender commandSender, String[] args) {
String configName = args[1];
String tagName = args[2];
String tagPrefix = args[3];
String material = "NAME_TAG";

if (SimpleTags.getInstance().getTagManager().getCachedTag(configName) != null) {
Color.sendMessage(commandSender, "tag-already-exists");
Expand All @@ -79,7 +80,7 @@ public void perform(CommandSender commandSender, String[] args) {
tagsFile.reload();
SimpleTags.getInstance().getTags().loadConfig();

SimpleTags.getInstance().getTagManager().addTag(new Tag(configName, tagName, tagPrefix));
SimpleTags.getInstance().getTagManager().addTag(new Tag(configName, tagName, tagPrefix, material));

StringPlaceholders placeholders = StringPlaceholders.builder()
.addAll(Placeholders.setPlaceholders(commandSender))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void perform(CommandSender commandSender, String[] args) {
return;
}

SimpleTags.getInstance().reloadFiles();
reloadFiles();
Tasks.runAsync(() -> {
SimpleTags.getInstance().getTagManager().loadTags();
SimpleTags.getInstance().getTagManager().updateTags();
Expand All @@ -72,4 +72,21 @@ public void perform(CommandSender commandSender, String[] args) {
public List<String> getSubcommandArguments(Player player, String[] args) {
return null;
}

private void reloadFiles() {
SimpleTags.getInstance().getConfigFile().reload();
SimpleTags.getInstance().getTagsFile().reload();
SimpleTags.getInstance().getMenusFile().reload();
SimpleTags.getInstance().getLocaleFile().reload();
SimpleTags.getInstance().getCommandsFile().reload();

SimpleTags.getInstance().getSettings().loadConfig();
SimpleTags.getInstance().getTags().loadConfig();
SimpleTags.getInstance().getMenus().loadConfig();
SimpleTags.getInstance().getCommands().loadConfig();

Color.log("&c==========================================");
Color.log("&eAll files have been reloaded correctly!");
Color.log("&c==========================================");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package me.refracdevelopment.simpletags.commands;

import me.refracdevelopment.simpletags.SimpleTags;
import me.refracdevelopment.simpletags.utilities.Permissions;
import me.refracdevelopment.simpletags.utilities.Tasks;
import me.refracdevelopment.simpletags.utilities.chat.Color;
import me.refracdevelopment.simpletags.utilities.command.SubCommand;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

public class ResetCommand extends SubCommand {

@Override
public String getName() {
return SimpleTags.getInstance().getCommands().RESET_COMMAND_NAME;
}

@Override
public List<String> getAliases() {
return SimpleTags.getInstance().getCommands().RESET_COMMAND_ALIASES;
}

@Override
public String getDescription() {
return SimpleTags.getInstance().getLocaleFile().getString("command-reset-description");
}

@Override
public String getSyntax() {
return "[player]";
}

@Override
public void perform(CommandSender commandSender, String[] args) {
if (!commandSender.hasPermission(Permissions.RESET_COMMAND)) {
Color.sendMessage(commandSender, "no-permission");
return;
}

if (args.length == 1) {
Tasks.runAsync(() -> {
switch (SimpleTags.getInstance().getDataType()) {
case MYSQL:
SimpleTags.getInstance().getMySQLManager().delete();

Tasks.run(() -> Bukkit.getOnlinePlayers().forEach(player -> {
player.kickPlayer(Color.translate(player, SimpleTags.getInstance().getLocaleFile().getString("kick-messages-error")));
}));
break;
case SQLITE:
SimpleTags.getInstance().getSqLiteManager().delete();

Tasks.run(() -> Bukkit.getOnlinePlayers().forEach(player -> {
player.kickPlayer(Color.translate(player, SimpleTags.getInstance().getLocaleFile().getString("kick-messages-error")));
}));
break;
default:
Tasks.run(() -> {
Color.sendCustomMessage(commandSender, "This command is only available for MySQL, MariaDB and SQLite.");
});
break;
}
});
} else if (args.length == 2) {
OfflinePlayer target = Bukkit.getOfflinePlayer(args[1]);

if (target.getPlayer() != null && target.getPlayer().isOnline()) {
Tasks.runAsync(() -> {
switch (SimpleTags.getInstance().getDataType()) {
case MYSQL:
SimpleTags.getInstance().getMySQLManager().deletePlayer(target.getPlayer().getUniqueId().toString());

Tasks.run(() -> {
Color.sendMessage(commandSender, "tags-reset-player");

target.getPlayer().kickPlayer(Color.translate(target.getPlayer(), SimpleTags.getInstance().getLocaleFile().getString("kick-messages-error")));
});
break;
case SQLITE:
SimpleTags.getInstance().getSqLiteManager().deletePlayer(target.getPlayer().getUniqueId().toString());

Tasks.run(() -> {
Color.sendMessage(commandSender, "tags-reset-player");

target.getPlayer().kickPlayer(Color.translate(target.getPlayer(), SimpleTags.getInstance().getLocaleFile().getString("kick-messages-error")));
});
break;
default:
Tasks.run(() -> {
Color.sendCustomMessage(commandSender, "This command is only available for MySQL, MariaDB and SQLite.");
});
break;
}
});
} else if (target.hasPlayedBefore()) {
switch (SimpleTags.getInstance().getDataType()) {
case MYSQL:
SimpleTags.getInstance().getMySQLManager().deletePlayer(target.getPlayer().getUniqueId().toString());

Tasks.run(() -> Color.sendMessage(commandSender, "gems-reset-player"));
break;
case SQLITE:
SimpleTags.getInstance().getSqLiteManager().deletePlayer(target.getPlayer().getUniqueId().toString());

Tasks.run(() -> Color.sendMessage(commandSender, "gems-reset-player"));
break;
default:
Tasks.run(() -> {
Color.sendCustomMessage(commandSender, "This command is only available for MySQL, MariaDB and SQLite.");
});
break;
}
}
}
}

@Override
public List<String> getSubcommandArguments(Player player, String[] args) {
List<String> names = new ArrayList<>();

Bukkit.getOnlinePlayers().forEach(p -> names.add(p.getName()));

if (args.length == 2) {
return names;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void perform(CommandSender commandSender, String[] args) {

profile.setTag(configName);
profile.setTagPrefix(tag.getTagPrefix());
Tasks.runAsync(profile::save);
Tasks.runAsync(() -> profile.save(player));

Color.sendMessage(player, "tag-updated", placeholders);
return;
Expand All @@ -119,17 +119,17 @@ public void perform(CommandSender commandSender, String[] args) {

StringPlaceholders placeholders = StringPlaceholders.builder()
.addAll(Placeholders.setPlaceholders(commandSender))
.add("player", profile.getPlayer().getName())
.add("player", target.getPlayer().getName())
.add("tag-name", tag.getTagName())
.add("tag-prefix", tag.getTagPrefix())
.build();

profile.setTag(configName);
profile.setTagPrefix(tag.getTagPrefix());
Tasks.runAsync(profile::save);
Tasks.runAsync(() -> profile.save(target.getPlayer()));

Color.sendMessage(commandSender, "tag-set", placeholders);
Color.sendMessage(profile.getPlayer(), "tag-updated", placeholders);
Color.sendMessage(target.getPlayer(), "tag-updated", placeholders);
} else if (target.hasPlayedBefore()) {
Tag tag = SimpleTags.getInstance().getTagManager().getCachedTag(configName);

Expand All @@ -140,7 +140,7 @@ public void perform(CommandSender commandSender, String[] args) {
.add("tag-prefix", tag.getTagPrefix())
.build();

Tasks.runAsync(() -> Utilities.saveOfflinePlayer(target.getUniqueId(), tag.getTagName(), tag.getTagPrefix()));
Tasks.runAsync(() -> Utilities.saveOfflinePlayer(target.getUniqueId().toString(), tag.getTagName(), tag.getTagPrefix()));

Color.sendMessage(commandSender, "tag-set", placeholders);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Profile profile = SimpleTags.getInstance().getProfileManager().getProfile(player.getUniqueId());

Tasks.runAsync(() -> profile.getData().load());
Tasks.runAsync(() -> profile.getData().load(player));

if (profile == null || profile.getData() == null) {
player.kickPlayer(Color.translate(SimpleTags.getInstance().getLocaleFile().getString("kick-messages-error")));
Expand All @@ -49,7 +49,7 @@ public void onQuit(PlayerQuitEvent event) {
if (profile == null) return;
if (profile.getData() == null) return;

Tasks.runAsync(() -> profile.getData().save());
Tasks.runAsync(() -> profile.getData().save(player));
SimpleTags.getInstance().getProfileManager().getProfiles().remove(player.getUniqueId());
}

Expand Down
Loading

0 comments on commit f9bcc95

Please sign in to comment.