11package simplexity .simplehomes .commands ;
22
3- import net .kyori .adventure .text .minimessage .tag .resolver .Placeholder ;
4- import org .bukkit .Bukkit ;
5- import org .bukkit .Location ;
6- import org .bukkit .command .Command ;
7- import org .bukkit .command .CommandSender ;
8- import org .bukkit .command .TabExecutor ;
9- import org .bukkit .entity .Player ;
10- import org .bukkit .scheduler .BukkitTask ;
11- import org .jetbrains .annotations .NotNull ;
12- import org .jetbrains .annotations .Nullable ;
13- import simplexity .simplehomes .Home ;
14- import simplexity .simplehomes .SafetyCheck ;
15- import simplexity .simplehomes .SafetyFlags ;
16- import simplexity .simplehomes .SimpleHomes ;
17- import simplexity .simplehomes .configs .ConfigHandler ;
18- import simplexity .simplehomes .configs .LocaleHandler ;
19- import simplexity .simplehomes .saving .Cache ;
20- import simplexity .simplehomes .saving .SQLHandler ;
21-
22- import java .util .ArrayList ;
23- import java .util .HashMap ;
24- import java .util .List ;
25-
26- public class HomeCommand implements TabExecutor {
27-
28- private static final String SAFETY_BYPASS = "homes.safety.bypass" ;
29- private static final String DELAY_BYPASS = "homes.delay.bypass" ;
30- public static HashMap <Player , Location > teleportRequests = new HashMap <>();
31- public static HashMap <Player , BukkitTask > teleportTasks = new HashMap <>();
32-
33-
34- @ Override
35- public boolean onCommand (@ NotNull CommandSender sender , @ NotNull Command command , @ NotNull String s , @ NotNull String [] args ) {
36- if (!(sender instanceof Player player )) {
37- sender .sendRichMessage (LocaleHandler .getInstance ().getMustBePlayer ());
38- return false ;
39- }
40- List <Home > playerHomesList = Cache .getInstance ().getPlayerHomes (player .getUniqueId ());
41- //Check for lockout
42- if (CommandUtils .isLockedOut (player )) {
43- player .sendRichMessage (LocaleHandler .getInstance ().getCannotUseCommand (),
44- Placeholder .parsed ("value" , String .valueOf (CommandUtils .maxHomesPermission (player ))),
45- Placeholder .parsed ("command" , "/home" ));
46- return false ;
47- }
48- // Get the home to teleport the player to
49- Home playerHome ;
50- if (args .length == 0 ) {
51- playerHome = handleNoArgs (player , playerHomesList );
52- if (playerHome == null ) {
53- player .sendRichMessage (LocaleHandler .getInstance ().getListNoHomes ());
54- return false ;
55- }
56- } else {
57- playerHome = handleHomeSelection (player , playerHomesList , args [0 ]);
58- if (playerHome == null ) {
59- player .sendRichMessage (LocaleHandler .getInstance ().getHomeNotFound (),
60- Placeholder .parsed ("name" , args [0 ]));
61- return false ;
62- }
63- }
64- // Check that it's safe
65- if (!shouldTeleport (player , args , playerHome )) return false ;
66- handleTeleport (player , playerHome );
67- return true ;
68- }
69-
70- // If player has a bed home and supplied no args, return a new home from that location and the configured bed home name.
71- // Otherwise, if they only have one home, return that one.
72- // Otherwise, return null
73-
74- private Home handleNoArgs (Player player , List <Home > homesList ) {
75- Location bedHome = getBedLocation (player );
76- if (bedHome != null ) return new Home (ConfigHandler .getInstance ().getBedHomesName (), bedHome );
77- if (homesList .size () == 1 ) {
78- return homesList .get (0 );
79- }
80- return null ;
81- }
82-
83- private Home handleHomeSelection (Player player , List <Home > homesList , String suppliedName ) {
84- Location bedLocation = getBedLocation (player );
85- if (suppliedName .equalsIgnoreCase (ConfigHandler .getInstance ().getBedHomesName ()) && bedLocation != null ) {
86- return new Home (ConfigHandler .getInstance ().getBedHomesName (), bedLocation );
87- }
88- return CommandUtils .getHomeFromList (homesList , suppliedName );
89- }
90-
91- // Do config, permission, and API checks for bed location
92- private Location getBedLocation (Player player ) {
93- if (player .getPotentialBedLocation () == null ) return null ;
94- if (!ConfigHandler .getInstance ().areBedHomesEnabled ()) return null ;
95- if (!player .hasPermission (CommandUtils .BED_PERMISSION )) return null ;
96- return player .getPotentialBedLocation ();
97- }
98-
99- // Safety Check
100- private boolean shouldTeleport (Player player , String [] args , Home home ) {
101- if (player .hasPermission (SAFETY_BYPASS )) return true ;
102- if (CommandUtils .shouldOverride (args )) return true ;
103- int safetyFlags = SafetyCheck .checkSafetyFlags (home .location (), ConfigHandler .getInstance ().getBlacklistedBlocks ());
104- if (safetyFlags == 0 ) return true ;
105- String safetyWarning = getSafetyWarning (safetyFlags );
106- if (safetyWarning == null ) {
107- player .sendRichMessage (LocaleHandler .getInstance ().getErrorHasOccurred ());
108- return false ;
109- }
110- player .sendRichMessage (safetyWarning );
111- return false ;
112- }
113-
114- // Gets the configured messages for the safety warnings
115- private String getSafetyWarning (int safetyFlags ) {
116- String warning = "" ;
117- if (safetyFlags == 0 ) return null ;
118- if (SafetyFlags .DAMAGE_RISK .matches (safetyFlags )) {
119- warning = LocaleHandler .getInstance ().getBlacklistedWarning ();
120- }
121- if (SafetyFlags .FALLING .matches (safetyFlags )) {
122- warning = LocaleHandler .getInstance ().getVoidWarning ();
123- }
124- if (SafetyFlags .FIRE .matches (safetyFlags )) {
125- warning = LocaleHandler .getInstance ().getFireWarning ();
126- }
127- if (SafetyFlags .LAVA .matches (safetyFlags )) {
128- warning = LocaleHandler .getInstance ().getLavaWarning ();
129- }
130- if (SafetyFlags .NOT_SOLID .matches (safetyFlags )) {
131- warning = LocaleHandler .getInstance ().getVoidWarning ();
132- }
133- if (SafetyFlags .SUFFOCATION .matches (safetyFlags )) {
134- warning = LocaleHandler .getInstance ().getBlocksWarning ();
135- }
136- if (SafetyFlags .UNDERWATER .matches (safetyFlags )) {
137- warning = LocaleHandler .getInstance ().getWaterWarning ();
138- }
139- if (SafetyFlags .UNSTABLE .matches (safetyFlags )) {
140- warning = LocaleHandler .getInstance ().getVoidWarning ();
141- }
142- warning = warning + LocaleHandler .getInstance ().getInsertOverride ();
143- return warning ;
144- }
145-
146- private void handleTeleport (Player player , Home home ) {
147- if (!ConfigHandler .getInstance ().isDelayEnabled ()) {
148- normalTeleport (player , home );
149- return ;
150- }
151- if (player .hasPermission (DELAY_BYPASS )) {
152- normalTeleport (player , home );
153- return ;
154- }
155- delayTeleport (player , home );
156- }
157-
158- private void normalTeleport (Player player , Home home ) {
159- player .teleportAsync (home .location ());
160- player .sendRichMessage (LocaleHandler .getInstance ().getHomeTeleported (),
161- Placeholder .parsed ("name" , home .name ()));
162- }
163-
164- // Runnable that allows delaying the teleport, tied into the player move listener
165- private void delayTeleport (Player player , Home home ) {
166- player .sendRichMessage (LocaleHandler .getInstance ().getPleaseWait (),
167- Placeholder .parsed ("value" , String .valueOf (ConfigHandler .getInstance ().getTimeInSeconds ())));
168- teleportRequests .put (player , player .getLocation ());
169- BukkitTask teleportTask = Bukkit .getScheduler ().runTaskLater (SimpleHomes .getInstance (), () -> {
170- if (!teleportRequests .containsKey (player )) return ;
171- player .teleportAsync (home .location ());
172- teleportRequests .remove (player );
173- player .sendRichMessage (LocaleHandler .getInstance ().getHomeTeleported (),
174- Placeholder .parsed ("name" , home .name ()));
175- }, ConfigHandler .getInstance ().getTimeInSeconds () * 20L );
176- teleportTasks .put (player , teleportTask );
177- }
178-
179- @ Override
180- public @ Nullable List <String > onTabComplete (@ NotNull CommandSender commandSender , @ NotNull Command command , @ NotNull String s , @ NotNull String [] strings ) {
181- if (!(commandSender instanceof Player player )) return List .of ();
182- List <Home > homesList = SQLHandler .getInstance ().getHomes (player .getUniqueId ());
183- List <String > stringList = new ArrayList <>();
184- for (Home home : homesList ) {
185- stringList .add (home .name ());
186- }
187- if (player .hasPermission (CommandUtils .BED_PERMISSION ) && getBedLocation (player ) != null ) {
188- stringList .add (ConfigHandler .getInstance ().getBedHomesName ());
189- }
190- return stringList ;
191- }
192- }
3+ public class HomeCommand {
4+ }
0 commit comments