11package simplexity .simplehomes .commands ;
22
3+ import net .kyori .adventure .text .minimessage .tag .resolver .Placeholder ;
34import org .bukkit .Bukkit ;
5+ import org .bukkit .Location ;
46import org .bukkit .OfflinePlayer ;
57import org .bukkit .command .Command ;
68import org .bukkit .command .CommandExecutor ;
911import org .bukkit .configuration .ConfigurationSection ;
1012import org .bukkit .configuration .file .YamlConfiguration ;
1113import org .jetbrains .annotations .NotNull ;
14+ import simplexity .simplehomes .configs .LocaleHandler ;
15+ import simplexity .simplehomes .saving .SQLHandler ;
1216
1317import java .io .File ;
1418import java .util .UUID ;
@@ -21,49 +25,38 @@ public class ImportHomes implements CommandExecutor {
2125 @ Override
2226 public boolean onCommand (@ NotNull CommandSender sender , @ NotNull Command command , @ NotNull String label , @ NotNull String [] args ) {
2327 if (!(sender instanceof ConsoleCommandSender )) {
24- sender .sendRichMessage ("<red>This command can only be used by the Console." );
28+ sender .sendRichMessage (LocaleHandler . getInstance (). getOnlyConsole () );
2529 return true ;
2630 }
2731
2832 if (args .length == 0 ) {
29- sender .sendRichMessage ("<red>Not enough arguments." );
33+ sender .sendRichMessage (LocaleHandler . getInstance (). getImportNotEnoughArgs () );
3034 return true ;
3135 }
3236
33- if (args .length == 1 ) {
34- if (args [0 ].equalsIgnoreCase ("help" )) {
35- sender .sendRichMessage ("<aqua>This is a Console exclusive command used to import homes to SimpleHomes from other plugins." );
36- sender .sendRichMessage ("<aqua>Not all home plugins are supported, to add support submit an issue on GitHub" );
37- sender .sendRichMessage (" <aqua>https://github.com/Simplexity-Development/SimpleHomes/issues" );
38- sender .sendRichMessage ("<gold>This command is <red>DESTRUCTIVE</red> and will overwrite homes with the same name." );
39- sender .sendRichMessage ("<gold>This command is <red>UNSUPPORTED</red> and may <red>CORRUPT SAVE DATA</red>." );
40- sender .sendRichMessage ("<red>>>>>> USE AT YOUR OWN RISK <<<<<</red>." );
41- sender .sendRichMessage ("<aqua>Usage:</aqua> <yellow>/importhomes <plugin> [username]" );
42- sender .sendRichMessage (" <yellow>username <aqua>is an optional argument to import specifically that user's homes." );
43- sender .sendRichMessage ("<green>Valid Plugins: Essentials" );
37+ if (args [0 ].equalsIgnoreCase ("help" )) {
38+ sender .sendRichMessage (LocaleHandler .getInstance ().getImportHelp ());
39+ return true ;
40+ }
41+ if (args [0 ].equalsIgnoreCase ("confirm" )) {
42+ if (this .lastUsed == null || this .args == null ) {
43+ sender .sendRichMessage (LocaleHandler .getInstance ().getCannotConfirm ());
4444 return true ;
4545 }
46- if (args [0 ].equalsIgnoreCase ("confirm" )) {
47- if (this .lastUsed == null || this .args == null ) {
48- sender .sendRichMessage ("<red>No command was executed recently to confirm." );
49- return true ;
50- }
51- if (System .currentTimeMillis () - this .lastUsed > 15000 ) {
52- this .lastUsed = null ;
53- this .args = null ;
54- sender .sendRichMessage ("<red>Command timed out, please run again." );
55- return true ;
56- }
57- return executeCommand ();
46+ if (System .currentTimeMillis () - this .lastUsed > 15000 ) {
47+ this .lastUsed = null ;
48+ this .args = null ;
49+ sender .sendRichMessage (LocaleHandler .getInstance ().getTimedOut ());
50+ return true ;
5851 }
52+ return executeCommand ();
5953 }
6054
55+
6156 this .lastUsed = System .currentTimeMillis ();
6257 this .args = args ;
63- sender .sendRichMessage ("<gold>You are about to run: <yellow>/" + label + " " + String .join (" " , args ));
64- sender .sendRichMessage ("<red><bold>THIS ACTION IS DESTRUCTIVE AND WILL OVERWRITE EXISTING HOMES IN SIMPLEHOMES." );
65- sender .sendRichMessage ("<red><bold>THIS ACTION IS NOT SUPPORTED AND MAY CORRUPT SAVE DATA, USE AT YOUR OWN RISK." );
66- sender .sendRichMessage ("<gold>Please confirm this command using <yellow>/" + label + " confirm" );
58+ sender .sendRichMessage (LocaleHandler .getInstance ().getUnsupportedDestructive (),
59+ Placeholder .parsed ("command" , label ));
6760 return true ;
6861 }
6962
@@ -75,19 +68,19 @@ public boolean executeCommand() {
7568
7669 String playerName = args .length > 1 ? args [1 ] : null ;
7770
78- switch (args [0 ].toLowerCase ()) {
79- case "essentials" -> importEssentialsHomes (playerName );
80- default -> sender .sendRichMessage ("<red>No valid plugin was found." );
71+ if (args [0 ].equalsIgnoreCase ("essentials" )) {
72+ importEssentialsHomes (playerName );
73+ } else {
74+ sender .sendRichMessage (LocaleHandler .getInstance ().getNoValidPlugin ());
8175 }
82-
8376 return true ;
8477 }
8578
8679 private void importEssentialsHomes (String playerName ) {
8780 CommandSender sender = Bukkit .getServer ().getConsoleSender ();
8881 File userdataFolder = new File (Bukkit .getServer ().getPluginsFolder () + "/Essentials/userdata" );
8982 if (!userdataFolder .isDirectory ()) {
90- sender .sendRichMessage ("<gold>There is no /plugins/Essentials/userdata folder!" );
83+ sender .sendRichMessage (LocaleHandler . getInstance (). getEssentialsNotExist () );
9184 return ;
9285 }
9386
@@ -96,46 +89,75 @@ private void importEssentialsHomes(String playerName) {
9689 UUID uuid = player .getUniqueId ();
9790 File playerDataFile = new File (userdataFolder , uuid + ".yml" );
9891 if (!playerDataFile .exists ()) {
99- sender .sendRichMessage ("<gold>This player does not exist or does not have an Essentials/userdata file!" );
100- sender . sendRichMessage ( "<yellow>Player Name: </yellow>" + playerName );
101- sender . sendRichMessage ( "<yellow>Retrieved UUID: </yellow>" + uuid );
102- sender . sendRichMessage ( "<yellow>Searched File: </yellow>" + playerDataFile );
92+ sender .sendRichMessage (LocaleHandler . getInstance (). getPlayerNotExist (),
93+ Placeholder . parsed ( "name" , playerName ),
94+ Placeholder . parsed ( "uuid" , uuid . toString ()),
95+ Placeholder . parsed ( "file" , playerDataFile . getAbsolutePath ()) );
10396 return ;
10497 }
10598 YamlConfiguration playerData = YamlConfiguration .loadConfiguration (playerDataFile );
10699 ConfigurationSection homes = playerData .getConfigurationSection ("homes" );
107100 if (homes == null || homes .getKeys (false ).isEmpty ()) {
108- sender .sendRichMessage ("<gold>This player does not have any homes in their Essentials/userdata file!" );
109- sender . sendRichMessage ( "<yellow>Player Name: </yellow>" + playerName );
110- sender . sendRichMessage ( "<yellow>Retrieved UUID: </yellow>" + uuid );
111- sender . sendRichMessage ( "<yellow>Searched File: </yellow>" + playerDataFile );
101+ sender .sendRichMessage (LocaleHandler . getInstance (). getPlayerNotExist (),
102+ Placeholder . parsed ( "name" , playerName ),
103+ Placeholder . parsed ( "uuid" , uuid . toString ()),
104+ Placeholder . parsed ( "file" , playerDataFile . getAbsolutePath ()) );
112105 return ;
113106 }
114107 for (String key : homes .getKeys (false )) {
115- // TODO: Rewrite SQLHandler to support this.
116108 ConfigurationSection home = homes .getConfigurationSection (key );
117- sender .sendRichMessage ("Found Home: " + key + " " + home .getInt ("x" ) + " " + home .getInt ("z" )); // TODO: Remove after testing.
109+ Location location = getLocationFromHome (home );
110+ if (location == null ) continue ;
111+ SQLHandler .getInstance ().setHome (uuid , location , key .toLowerCase ());
118112 }
113+ sender .sendRichMessage (LocaleHandler .getInstance ().getImportedHomes (),
114+ Placeholder .parsed ("name" , playerName ));
119115 return ;
120116 }
121117
122118 File [] files = userdataFolder .listFiles ();
123119 assert files != null ; // Already did an isDirectory() check earlier.
124120 if (files .length == 0 ) {
125- sender .sendRichMessage ("<gold>There is nothing inside of the /plugins/Essentials/userdata folder." );
121+ sender .sendRichMessage (LocaleHandler . getInstance (). getNothingInsideFolder () );
126122 return ;
127123 }
128124 for (File userFile : files ) {
129125 YamlConfiguration playerData = YamlConfiguration .loadConfiguration (userFile );
130- sender . sendRichMessage ( "Found File: " + userFile .getName ()); // TODO: Remove after testing.
131- sender . sendRichMessage ( "Found Name: " + playerData . getString ( "last-account-name" )); // TODO: Remove after testing.
126+ String uuidString = userFile .getName (). replace ( ".yml" , "" );
127+ UUID uuid = UUID . fromString ( uuidString );
132128 ConfigurationSection homes = playerData .getConfigurationSection ("homes" );
133129 if (homes == null || homes .getKeys (false ).isEmpty ()) continue ;
134130 for (String key : homes .getKeys (false )) {
135- // TODO: Rewrite SQLHandler to support this.
136131 ConfigurationSection home = homes .getConfigurationSection (key );
137- sender .sendRichMessage ("Found Home: " + key + " " + home .getInt ("x" ) + " " + home .getInt ("z" )); // TODO: Remove after testing.
132+ Location location = getLocationFromHome (home );
133+ if (location == null ) continue ;
134+ SQLHandler .getInstance ().setHome (uuid , location , key );
138135 }
136+ OfflinePlayer player = Bukkit .getServer ().getOfflinePlayer (uuid );
137+ String userName = player .getName ();
138+ if (userName == null ) userName = "" ;
139+ sender .sendRichMessage (LocaleHandler .getInstance ().getImportedHomes (),
140+ Placeholder .parsed ("name" , userName ));
141+ }
142+ sender .sendRichMessage (LocaleHandler .getInstance ().getImportComplete ());
143+ }
144+
145+ private Location getLocationFromHome (ConfigurationSection home ) {
146+ String worldString ;
147+ try {
148+ worldString = home .getString ("world" );
149+ } catch (NullPointerException e ) {
150+ return null ;
151+ }
152+ if (worldString == null || worldString .isEmpty ()) {
153+ return null ;
139154 }
155+ UUID worldUUID = UUID .fromString (worldString );
156+ double x = home .getDouble ("x" );
157+ double y = home .getDouble ("y" );
158+ double z = home .getDouble ("z" );
159+ float yaw = (float ) home .getDouble ("yaw" );
160+ float pitch = (float ) home .getDouble ("pitch" );
161+ return new Location (Bukkit .getServer ().getWorld (worldUUID ), x , y , z , yaw , pitch );
140162 }
141163}
0 commit comments