3
3
import cat .nyaa .nyaautils .I18n ;
4
4
import cat .nyaa .nyaautils .NyaaUtils ;
5
5
import cat .nyaa .nyaautils .api .events .HamsterEcoHelperTransactionApiEvent ;
6
+ import com .earth2me .essentials .Trade ;
7
+ import com .earth2me .essentials .User ;
8
+ import com .earth2me .essentials .utils .LocationUtil ;
9
+ import com .earth2me .essentials .utils .NumberUtil ;
6
10
import net .ess3 .api .IEssentials ;
7
- import net .ess3 .api .IUser ;
8
11
import net .ess3 .api .InvalidWorldException ;
9
12
import org .bukkit .Bukkit ;
10
13
import org .bukkit .Location ;
15
18
import org .bukkit .event .EventPriority ;
16
19
import org .bukkit .event .Listener ;
17
20
import org .bukkit .event .player .PlayerCommandPreprocessEvent ;
21
+ import org .bukkit .event .player .PlayerTeleportEvent ;
18
22
import org .bukkit .permissions .PermissionAttachment ;
19
23
20
24
import java .text .DecimalFormat ;
@@ -38,7 +42,8 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
38
42
if (!plugin .cfg .teleportEnable ) return ;
39
43
String cmd = e .getMessage ().toLowerCase ().trim ();
40
44
Player p = e .getPlayer ();
41
- IUser iu = ess .getUser (p );
45
+ User iu = ess .getUser (p );
46
+ Location curLoc = p .getLocation ();
42
47
if (cmd .equals ("/home" ) || cmd .startsWith ("/home " )) {
43
48
e .setCancelled (true );
44
49
List <String > homes = iu .getHomes ();
@@ -49,7 +54,7 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
49
54
msg (p , "user.teleport.bed_not_set_yet" );
50
55
return ;
51
56
}
52
- callEssHome (p , bedLoc , p . getLocation (), "bed" );
57
+ doHome (p , iu , bedLoc , curLoc );
53
58
return ;
54
59
}
55
60
@@ -67,8 +72,7 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
67
72
msg (p , "user.teleport.error" );
68
73
return ;
69
74
}
70
- Location curLoc = p .getLocation ();
71
- callEssHome (p , homeLoc , curLoc , null );
75
+ doHome (p , iu , homeLoc , curLoc );
72
76
} else {
73
77
String to = cmd .substring (5 ).trim ();
74
78
for (String home : homes ) {
@@ -84,74 +88,110 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
84
88
msg (p , "user.teleport.error" );
85
89
return ;
86
90
}
87
- Location cl = p .getLocation ();
88
- callEssHome (p , homeLoc , cl , to );
91
+ doHome (p , iu , homeLoc , curLoc );
89
92
return ;
90
93
}
91
94
}
95
+ //The command will just list home the player have, won't actually teleports player home.
92
96
PermissionAttachment attachment = p .addAttachment (NyaaUtils .instance , 1 );
93
97
attachment .setPermission ("essentials.home" , true );
94
98
Bukkit .dispatchCommand (p , "essentials:home" );
95
99
}
96
100
} else if (cmd .equals ("/sethome" ) || cmd .startsWith ("/sethome " )) {
97
101
e .setCancelled (true );
98
- Location curLoc = p .getLocation ();
99
- World defaultWorld = Bukkit .getWorld (plugin .cfg .setHomeDefaultWorld );
100
- if (defaultWorld == null ) {
101
- defaultWorld = Bukkit .getWorlds ().get (0 );
102
+ String name = cmd .replace ("/sethome" , "" ).trim ();
103
+ if (name .equals ("" )) {
104
+ name = "home" ;
102
105
}
103
- double fee = plugin .cfg .setHomeMax ;
104
- if (curLoc .getWorld () != defaultWorld ) {
105
- fee += plugin .cfg .setHomeWorld ;
106
- fee -= curLoc .distance (curLoc .getWorld ().getSpawnLocation ()) * (double ) plugin .cfg .setHomeDecrement / plugin .cfg .setHomeDistance ;
107
- } else {
108
- fee -= curLoc .distance (defaultWorld .getSpawnLocation ()) * (double ) plugin .cfg .setHomeDecrement / plugin .cfg .setHomeDistance ;
109
- }
110
- if (fee < plugin .cfg .setHomeMin ) fee = plugin .cfg .setHomeMin ;
111
- fee = Double .parseDouble (new DecimalFormat ("#.00" ).format (fee ));
112
- if (!plugin .vaultUtil .enoughMoney (p , fee )) {
113
- msg (p , "user.teleport.money_insufficient" , fee );
114
- return ;
115
- }
116
- HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent (fee );
117
- plugin .getServer ().getPluginManager ().callEvent (event );
118
- msg (p , "user.teleport.ok" , fee , I18n ._ ("user.teleport.sethome" ));
119
- PermissionAttachment attachment = p .addAttachment (NyaaUtils .instance , 1 );
120
- attachment .setPermission ("essentials.sethome" , true );
121
- Bukkit .dispatchCommand (p , cmd .substring (1 ).replace ("sethome" , "essentials:sethome" ));
122
- plugin .vaultUtil .withdraw (p , fee );
106
+ doSetHome (p , iu , curLoc , name );
123
107
} else if (cmd .equals ("/back" )) {
124
108
e .setCancelled (true );
125
- Location curLoc = p .getLocation ();
126
109
Location lastLoc = iu .getLastLocation ();
127
110
if (lastLoc == null ) {
128
111
msg (p , "user.teleport.no_loc" );
129
112
return ;
130
113
}
131
- double fee = plugin .cfg .backBase ;
132
- if (curLoc .getWorld () != lastLoc .getWorld ()) {
133
- fee += plugin .cfg .backWorld ;
134
- fee += lastLoc .distance (lastLoc .getWorld ().getSpawnLocation ()) * (double ) plugin .cfg .backIncrement / plugin .cfg .backDistance ;
135
- } else {
136
- fee += lastLoc .distance (curLoc ) * (double ) plugin .cfg .backIncrement / plugin .cfg .backDistance ;
137
- }
138
- if (fee > plugin .cfg .backMax ) fee = plugin .cfg .backMax ;
139
- fee = Double .parseDouble (new DecimalFormat ("#.00" ).format (fee ));
140
- if (!plugin .vaultUtil .enoughMoney (p , fee )) {
141
- msg (p , "user.teleport.money_insufficient" , fee );
142
- return ;
143
- }
114
+ doBack (p , iu , curLoc , lastLoc );
115
+ }
116
+ }
117
+
118
+ private void doSetHome (Player p , User iu , Location curLoc , String name ) {
119
+ int n = checkHomeLimit (iu , name );
120
+ System .out .println (n );
121
+ if (n == 1 ) {
122
+ name = "home" ;
123
+ } else if (n != 0 ) {
124
+ msg (p , "user.teleport.home_limit" , n );
125
+ return ;
126
+ }
127
+ if ("bed" .equals (name ) || NumberUtil .isInt (name )) {
128
+ msg (p , "user.teleport.invalid_name" );
129
+ return ;
130
+ }
131
+ if (!ess .getSettings ().isTeleportSafetyEnabled () && LocationUtil .isBlockUnsafeForUser (iu , curLoc .getWorld (), curLoc .getBlockX (), curLoc .getBlockY (), curLoc .getBlockZ ())) {
132
+ msg (p , "user.teleport.unsafe" );
133
+ return ;
134
+ }
135
+
136
+ double fee = plugin .cfg .setHomeMax ;
137
+ World defaultWorld = Bukkit .getWorld (plugin .cfg .setHomeDefaultWorld );
138
+ if (defaultWorld == null ) {
139
+ defaultWorld = Bukkit .getWorlds ().get (0 );
140
+ }
141
+ if (curLoc .getWorld () != defaultWorld ) {
142
+ fee += plugin .cfg .setHomeWorld ;
143
+ fee -= curLoc .distance (curLoc .getWorld ().getSpawnLocation ()) * (double ) plugin .cfg .setHomeDecrement / plugin .cfg .setHomeDistance ;
144
+ } else {
145
+ fee -= curLoc .distance (defaultWorld .getSpawnLocation ()) * (double ) plugin .cfg .setHomeDecrement / plugin .cfg .setHomeDistance ;
146
+ }
147
+ if (fee < plugin .cfg .setHomeMin ) fee = plugin .cfg .setHomeMin ;
148
+ fee = Double .parseDouble (new DecimalFormat ("#.00" ).format (fee ));
149
+ if (!plugin .vaultUtil .withdraw (p , fee )) {
150
+ msg (p , "user.teleport.money_insufficient" , fee );
151
+ return ;
152
+ }
153
+ iu .setHome (name , curLoc );
154
+ msg (p , "user.teleport.ok" , fee , I18n ._ ("user.teleport.sethome" ));
155
+ HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent (fee );
156
+ plugin .getServer ().getPluginManager ().callEvent (event );
157
+ }
158
+
159
+ private void doBack (Player p , User iu , Location curLoc , Location lastLoc ) {
160
+ if (iu .getWorld () != lastLoc .getWorld () && ess .getSettings ().isWorldTeleportPermissions () && !iu .isAuthorized ("essentials.worlds." + lastLoc .getWorld ().getName ())) {
161
+ msg (p , "internal.error.no_required_permission" , "essentials.worlds." + lastLoc .getWorld ().getName ());
162
+ return ;
163
+ }
164
+
165
+ double fee = plugin .cfg .backBase ;
166
+ if (curLoc .getWorld () != lastLoc .getWorld ()) {
167
+ fee += plugin .cfg .backWorld ;
168
+ fee += lastLoc .distance (lastLoc .getWorld ().getSpawnLocation ()) * (double ) plugin .cfg .backIncrement / plugin .cfg .backDistance ;
169
+ } else {
170
+ fee += lastLoc .distance (curLoc ) * (double ) plugin .cfg .backIncrement / plugin .cfg .backDistance ;
171
+ }
172
+ if (fee > plugin .cfg .backMax ) fee = plugin .cfg .backMax ;
173
+ fee = Double .parseDouble (new DecimalFormat ("#.00" ).format (fee ));
174
+ if (!plugin .vaultUtil .withdraw (p , fee )) {
175
+ msg (p , "user.teleport.money_insufficient" , fee );
176
+ return ;
177
+ }
178
+ try {
179
+ iu .getTeleport ().back (new Trade (0 , ess ));
180
+ msg (p , "user.teleport.ok" , fee , I18n ._ ("user.teleport.back" ));
144
181
HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent (fee );
145
182
plugin .getServer ().getPluginManager ().callEvent (event );
146
- msg (p , "user.teleport.ok" , fee , I18n ._ ("user.teleport.back" ));
147
- PermissionAttachment attachment = p .addAttachment (NyaaUtils .instance , 1 );
148
- attachment .setPermission ("essentials.back" , true );
149
- Bukkit .dispatchCommand (p , "essentials:back" );
150
- plugin .vaultUtil .withdraw (p , fee );
183
+ } catch (Exception e ) {
184
+ plugin .vaultUtil .deposit (p , fee );
185
+ p .sendMessage (e .getMessage ());
151
186
}
152
187
}
153
188
154
- private void callEssHome (Player p , Location homeLoc , Location curLoc , String home ) {
189
+ private void doHome (Player p , User iu , Location homeLoc , Location curLoc ) {
190
+ if (iu .getWorld () != homeLoc .getWorld () && ess .getSettings ().isWorldHomePermissions () && !iu .isAuthorized ("essentials.worlds." + homeLoc .getWorld ().getName ())) {
191
+ msg (p , "internal.error.no_required_permission" , "essentials.worlds." + homeLoc .getWorld ().getName ());
192
+ return ;
193
+ }
194
+
155
195
double fee = plugin .cfg .homeBase ;
156
196
if (homeLoc .getWorld () != curLoc .getWorld ()) {
157
197
fee += plugin .cfg .homeWorld ;
@@ -161,17 +201,35 @@ private void callEssHome(Player p, Location homeLoc, Location curLoc, String hom
161
201
}
162
202
if (fee > plugin .cfg .homeMax ) fee = plugin .cfg .homeMax ;
163
203
fee = Double .parseDouble (new DecimalFormat ("#.00" ).format (fee ));
164
- if (!plugin .vaultUtil .enoughMoney (p , fee )) {
204
+ if (!plugin .vaultUtil .withdraw (p , fee )) {
165
205
msg (p , "user.teleport.money_insufficient" , fee );
166
206
return ;
167
207
}
168
- HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent (fee );
169
- plugin .getServer ().getPluginManager ().callEvent (event );
170
- msg (p , "user.teleport.ok" , fee , I18n ._ ("user.teleport.home" ));
171
- PermissionAttachment attachment = p .addAttachment (NyaaUtils .instance , 1 );
172
- attachment .setPermission ("essentials.home" , true );
173
- Bukkit .dispatchCommand (p , home == null ? "essentials:home" : "essentials:home " + home );
174
- plugin .vaultUtil .withdraw (p , fee );
208
+ try {
209
+ iu .getTeleport ().teleport (homeLoc , new Trade (0 , ess ), PlayerTeleportEvent .TeleportCause .PLUGIN );
210
+ msg (p , "user.teleport.ok" , fee , I18n ._ ("user.teleport.home" ));
211
+ HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent (fee );
212
+ plugin .getServer ().getPluginManager ().callEvent (event );
213
+ } catch (Exception e ) {
214
+ plugin .vaultUtil .deposit (p , fee );
215
+ p .sendMessage (e .getMessage ());
216
+ }
217
+ }
218
+
219
+ private int checkHomeLimit (final User user , String name ) {
220
+ if (!user .isAuthorized ("essentials.sethome.multiple.unlimited" )) {
221
+ int limit = ess .getSettings ().getHomeLimit (user );
222
+ if (user .getHomes ().size () == limit && user .getHomes ().contains (name )) {
223
+ return 0 ;
224
+ }
225
+ if (user .getHomes ().size () >= limit ) {
226
+ return limit ;
227
+ }
228
+ if (limit == 1 ) {
229
+ return 1 ;
230
+ }
231
+ }
232
+ return 0 ;
175
233
}
176
234
177
235
private void msg (CommandSender target , String template , Object ... args ) {
0 commit comments