Skip to content

Commit

Permalink
Bump version, add locale stuff for importhomes command
Browse files Browse the repository at this point in the history
  • Loading branch information
RhythmicSys committed Nov 17, 2024
1 parent 1742c48 commit bbc8184
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 77 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>simplexity</groupId>
<artifactId>SimpleHomes</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
<packaging>jar</packaging>

<name>SimpleHomes</name>
Expand Down
13 changes: 13 additions & 0 deletions src/main/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,16 @@ provides a straightforward interface for home management commands.
| `homes.count.bypass` | op | Allows for setting infinite homes regardless of how many you have set as the max |
| `homes.reload` | op | Allows reloading the config |
| `homes.safety.bypass` | false | Allows bypassing the safety checks |

## Importing from other plugins

**THIS COMMAND IS IRREVERSIBLE AND WILL DELETE ANY HOMES YOU HAVE ALREADY SET IN SIMPLEHOMES**
it is **HIGHLY** recommended you do not run this command while anyone is online, to reduce the risk of any save corruption.
**USE AT YOUR OWN RISK**

To import from another plugin, you need to go to console, as the command is console-only. Command syntax is as follows:
`importhomes <plugin> [player]`
The player argument is optional, if you want to import all homes that are saved in the plugin specified, leave the player argument out.

Currently supported plugins to import from:
- Essentials
14 changes: 8 additions & 6 deletions src/main/java/simplexity/simplehomes/SimpleHomes.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import simplexity.simplehomes.configs.LocaleHandler;
import simplexity.simplehomes.saving.SQLHandler;

import java.util.Objects;

public final class SimpleHomes extends JavaPlugin {

private static final MiniMessage miniMessage = MiniMessage.miniMessage();
Expand Down Expand Up @@ -39,11 +41,11 @@ public void onDisable() {
}

private void registerCommands() {
this.getCommand("sethome").setExecutor(new SetHome());
this.getCommand("delhome").setExecutor(new DeleteHome());
this.getCommand("home").setExecutor(new Home());
this.getCommand("homelist").setExecutor(new HomeList());
this.getCommand("homesreload").setExecutor(new HomesReload());
this.getCommand("importhomes").setExecutor(new ImportHomes()); // TODO: Add command into plugin.yml
Objects.requireNonNull(this.getCommand("sethome")).setExecutor(new SetHome());
Objects.requireNonNull(this.getCommand("delhome")).setExecutor(new DeleteHome());
Objects.requireNonNull(this.getCommand("home")).setExecutor(new Home());
Objects.requireNonNull(this.getCommand("homelist")).setExecutor(new HomeList());
Objects.requireNonNull(this.getCommand("homesreload")).setExecutor(new HomesReload());
Objects.requireNonNull(this.getCommand("importhomes")).setExecutor(new ImportHomes());
}
}
1 change: 0 additions & 1 deletion src/main/java/simplexity/simplehomes/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ public static int maxHomesPermission(Player player){
}
return maxHomes;
}

}
120 changes: 71 additions & 49 deletions src/main/java/simplexity/simplehomes/commands/ImportHomes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package simplexity.simplehomes.commands;

import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
Expand All @@ -9,6 +11,8 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import simplexity.simplehomes.configs.LocaleHandler;
import simplexity.simplehomes.saving.SQLHandler;

import java.io.File;
import java.util.UUID;
Expand All @@ -21,49 +25,38 @@ public class ImportHomes implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof ConsoleCommandSender)) {
sender.sendRichMessage("<red>This command can only be used by the Console.");
sender.sendRichMessage(LocaleHandler.getInstance().getOnlyConsole());
return true;
}

if (args.length == 0) {
sender.sendRichMessage("<red>Not enough arguments.");
sender.sendRichMessage(LocaleHandler.getInstance().getImportNotEnoughArgs());
return true;
}

if (args.length == 1) {
if (args[0].equalsIgnoreCase("help")) {
sender.sendRichMessage("<aqua>This is a Console exclusive command used to import homes to SimpleHomes from other plugins.");
sender.sendRichMessage("<aqua>Not all home plugins are supported, to add support submit an issue on GitHub");
sender.sendRichMessage(" <aqua>https://github.com/Simplexity-Development/SimpleHomes/issues");
sender.sendRichMessage("<gold>This command is <red>DESTRUCTIVE</red> and will overwrite homes with the same name.");
sender.sendRichMessage("<gold>This command is <red>UNSUPPORTED</red> and may <red>CORRUPT SAVE DATA</red>.");
sender.sendRichMessage("<red>>>>>> USE AT YOUR OWN RISK <<<<<</red>.");
sender.sendRichMessage("<aqua>Usage:</aqua> <yellow>/importhomes <plugin> [username]");
sender.sendRichMessage(" <yellow>username <aqua>is an optional argument to import specifically that user's homes.");
sender.sendRichMessage("<green>Valid Plugins: Essentials");
if (args[0].equalsIgnoreCase("help")) {
sender.sendRichMessage(LocaleHandler.getInstance().getImportHelp());
return true;
}
if (args[0].equalsIgnoreCase("confirm")) {
if (this.lastUsed == null || this.args == null) {
sender.sendRichMessage(LocaleHandler.getInstance().getCannotConfirm());
return true;
}
if (args[0].equalsIgnoreCase("confirm")) {
if (this.lastUsed == null || this.args == null) {
sender.sendRichMessage("<red>No command was executed recently to confirm.");
return true;
}
if (System.currentTimeMillis() - this.lastUsed > 15000) {
this.lastUsed = null;
this.args = null;
sender.sendRichMessage("<red>Command timed out, please run again.");
return true;
}
return executeCommand();
if (System.currentTimeMillis() - this.lastUsed > 15000) {
this.lastUsed = null;
this.args = null;
sender.sendRichMessage(LocaleHandler.getInstance().getTimedOut());
return true;
}
return executeCommand();
}


this.lastUsed = System.currentTimeMillis();
this.args = args;
sender.sendRichMessage("<gold>You are about to run: <yellow>/" + label + " " + String.join(" ", args));
sender.sendRichMessage("<red><bold>THIS ACTION IS DESTRUCTIVE AND WILL OVERWRITE EXISTING HOMES IN SIMPLEHOMES.");
sender.sendRichMessage("<red><bold>THIS ACTION IS NOT SUPPORTED AND MAY CORRUPT SAVE DATA, USE AT YOUR OWN RISK.");
sender.sendRichMessage("<gold>Please confirm this command using <yellow>/" + label + " confirm");
sender.sendRichMessage(LocaleHandler.getInstance().getUnsupportedDestructive(),
Placeholder.parsed("command", label));
return true;
}

Expand All @@ -75,19 +68,19 @@ public boolean executeCommand() {

String playerName = args.length > 1 ? args[1] : null;

switch (args[0].toLowerCase()) {
case "essentials" -> importEssentialsHomes(playerName);
default -> sender.sendRichMessage("<red>No valid plugin was found.");
if (args[0].equalsIgnoreCase("essentials")) {
importEssentialsHomes(playerName);
} else {
sender.sendRichMessage(LocaleHandler.getInstance().getNoValidPlugin());
}

return true;
}

private void importEssentialsHomes(String playerName) {
CommandSender sender = Bukkit.getServer().getConsoleSender();
File userdataFolder = new File(Bukkit.getServer().getPluginsFolder() + "/Essentials/userdata");
if (!userdataFolder.isDirectory()) {
sender.sendRichMessage("<gold>There is no /plugins/Essentials/userdata folder!");
sender.sendRichMessage(LocaleHandler.getInstance().getEssentialsNotExist());
return;
}

Expand All @@ -96,46 +89,75 @@ private void importEssentialsHomes(String playerName) {
UUID uuid = player.getUniqueId();
File playerDataFile = new File(userdataFolder, uuid + ".yml");
if (!playerDataFile.exists()) {
sender.sendRichMessage("<gold>This player does not exist or does not have an Essentials/userdata file!");
sender.sendRichMessage("<yellow>Player Name: </yellow>" + playerName);
sender.sendRichMessage("<yellow>Retrieved UUID: </yellow>" + uuid);
sender.sendRichMessage("<yellow>Searched File: </yellow>" + playerDataFile);
sender.sendRichMessage(LocaleHandler.getInstance().getPlayerNotExist(),
Placeholder.parsed("name", playerName),
Placeholder.parsed("uuid", uuid.toString()),
Placeholder.parsed("file", playerDataFile.getAbsolutePath()));
return;
}
YamlConfiguration playerData = YamlConfiguration.loadConfiguration(playerDataFile);
ConfigurationSection homes = playerData.getConfigurationSection("homes");
if (homes == null || homes.getKeys(false).isEmpty()) {
sender.sendRichMessage("<gold>This player does not have any homes in their Essentials/userdata file!");
sender.sendRichMessage("<yellow>Player Name: </yellow>" + playerName);
sender.sendRichMessage("<yellow>Retrieved UUID: </yellow>" + uuid);
sender.sendRichMessage("<yellow>Searched File: </yellow>" + playerDataFile);
sender.sendRichMessage(LocaleHandler.getInstance().getPlayerNotExist(),
Placeholder.parsed("name", playerName),
Placeholder.parsed("uuid", uuid.toString()),
Placeholder.parsed("file", playerDataFile.getAbsolutePath()));
return;
}
for (String key : homes.getKeys(false)) {
// TODO: Rewrite SQLHandler to support this.
ConfigurationSection home = homes.getConfigurationSection(key);
sender.sendRichMessage("Found Home: " + key + " " + home.getInt("x") + " " + home.getInt("z")); // TODO: Remove after testing.
Location location = getLocationFromHome(home);
if (location == null) continue;
SQLHandler.getInstance().setHome(uuid, location, key.toLowerCase());
}
sender.sendRichMessage(LocaleHandler.getInstance().getImportedHomes(),
Placeholder.parsed("name", playerName));
return;
}

File[] files = userdataFolder.listFiles();
assert files != null; // Already did an isDirectory() check earlier.
if (files.length == 0) {
sender.sendRichMessage("<gold>There is nothing inside of the /plugins/Essentials/userdata folder.");
sender.sendRichMessage(LocaleHandler.getInstance().getNothingInsideFolder());
return;
}
for (File userFile : files) {
YamlConfiguration playerData = YamlConfiguration.loadConfiguration(userFile);
sender.sendRichMessage("Found File: " + userFile.getName()); // TODO: Remove after testing.
sender.sendRichMessage("Found Name: " + playerData.getString("last-account-name")); // TODO: Remove after testing.
String uuidString = userFile.getName().replace(".yml", "");
UUID uuid = UUID.fromString(uuidString);
ConfigurationSection homes = playerData.getConfigurationSection("homes");
if (homes == null || homes.getKeys(false).isEmpty()) continue;
for (String key : homes.getKeys(false)) {
// TODO: Rewrite SQLHandler to support this.
ConfigurationSection home = homes.getConfigurationSection(key);
sender.sendRichMessage("Found Home: " + key + " " + home.getInt("x") + " " + home.getInt("z")); // TODO: Remove after testing.
Location location = getLocationFromHome(home);
if (location == null) continue;
SQLHandler.getInstance().setHome(uuid, location, key);
}
OfflinePlayer player = Bukkit.getServer().getOfflinePlayer(uuid);
String userName = player.getName();
if (userName == null) userName = "";
sender.sendRichMessage(LocaleHandler.getInstance().getImportedHomes(),
Placeholder.parsed("name", userName));
}
sender.sendRichMessage(LocaleHandler.getInstance().getImportComplete());
}

private Location getLocationFromHome(ConfigurationSection home) {
String worldString;
try {
worldString = home.getString("world");
} catch (NullPointerException e) {
return null;
}
if (worldString == null || worldString.isEmpty()) {
return null;
}
UUID worldUUID = UUID.fromString(worldString);
double x = home.getDouble("x");
double y = home.getDouble("y");
double z = home.getDouble("z");
float yaw = (float) home.getDouble("yaw");
float pitch = (float) home.getDouble("pitch");
return new Location(Bukkit.getServer().getWorld(worldUUID), x, y, z, yaw, pitch);
}
}
Loading

0 comments on commit bbc8184

Please sign in to comment.