Skip to content

Commit 8983852

Browse files
author
Librazy
committed
use ess api directly in teleport
1 parent ec80d70 commit 8983852

File tree

5 files changed

+122
-67
lines changed

5 files changed

+122
-67
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@ gradle
7171
build
7272

7373
!lib/LocketteProAPI.jar
74+
!lib/EssentialsX-2.0.1-468.jar

build.gradle

+1-8
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,13 @@ repositories {
2626
name 'vault-repo'
2727
url 'http://nexus.hc.to/content/repositories/pub_releases'
2828
}
29-
30-
maven {
31-
name 'ess-repo'
32-
url 'http://repo.ess3.net/content/groups/essentials'
33-
}
3429
}
3530

3631
dependencies {
3732
compile 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
3833
compile 'net.milkbowl.vault:VaultAPI:1.6'
39-
compile('net.ess3:Essentials:2.13-SNAPSHOT') {
40-
transitive = false
41-
}
4234
compile files('lib/LocketteProAPI.jar')
35+
compile files('lib/EssentialsX-2.0.1-468.jar')
4336
compile 'com.sk89q.worldedit:worldedit-bukkit:6.1.5'
4437
}
4538

lib/EssentialsX-2.0.1-468.jar

1.08 MB
Binary file not shown.

src/main/java/cat/nyaa/nyaautils/commandwarpper/Teleport.java

+117-59
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import cat.nyaa.nyaautils.I18n;
44
import cat.nyaa.nyaautils.NyaaUtils;
55
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;
610
import net.ess3.api.IEssentials;
7-
import net.ess3.api.IUser;
811
import net.ess3.api.InvalidWorldException;
912
import org.bukkit.Bukkit;
1013
import org.bukkit.Location;
@@ -15,6 +18,7 @@
1518
import org.bukkit.event.EventPriority;
1619
import org.bukkit.event.Listener;
1720
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
21+
import org.bukkit.event.player.PlayerTeleportEvent;
1822
import org.bukkit.permissions.PermissionAttachment;
1923

2024
import java.text.DecimalFormat;
@@ -38,7 +42,8 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
3842
if (!plugin.cfg.teleportEnable) return;
3943
String cmd = e.getMessage().toLowerCase().trim();
4044
Player p = e.getPlayer();
41-
IUser iu = ess.getUser(p);
45+
User iu = ess.getUser(p);
46+
Location curLoc = p.getLocation();
4247
if (cmd.equals("/home") || cmd.startsWith("/home ")) {
4348
e.setCancelled(true);
4449
List<String> homes = iu.getHomes();
@@ -49,7 +54,7 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
4954
msg(p, "user.teleport.bed_not_set_yet");
5055
return;
5156
}
52-
callEssHome(p, bedLoc, p.getLocation(), "bed");
57+
doHome(p, iu, bedLoc, curLoc);
5358
return;
5459
}
5560

@@ -67,8 +72,7 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
6772
msg(p, "user.teleport.error");
6873
return;
6974
}
70-
Location curLoc = p.getLocation();
71-
callEssHome(p, homeLoc, curLoc, null);
75+
doHome(p, iu, homeLoc, curLoc);
7276
} else {
7377
String to = cmd.substring(5).trim();
7478
for (String home : homes) {
@@ -84,74 +88,110 @@ public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
8488
msg(p, "user.teleport.error");
8589
return;
8690
}
87-
Location cl = p.getLocation();
88-
callEssHome(p, homeLoc, cl, to);
91+
doHome(p, iu, homeLoc, curLoc);
8992
return;
9093
}
9194
}
95+
//The command will just list home the player have, won't actually teleports player home.
9296
PermissionAttachment attachment = p.addAttachment(NyaaUtils.instance, 1);
9397
attachment.setPermission("essentials.home", true);
9498
Bukkit.dispatchCommand(p, "essentials:home");
9599
}
96100
} else if (cmd.equals("/sethome") || cmd.startsWith("/sethome ")) {
97101
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";
102105
}
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);
123107
} else if (cmd.equals("/back")) {
124108
e.setCancelled(true);
125-
Location curLoc = p.getLocation();
126109
Location lastLoc = iu.getLastLocation();
127110
if (lastLoc == null) {
128111
msg(p, "user.teleport.no_loc");
129112
return;
130113
}
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"));
144181
HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent(fee);
145182
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());
151186
}
152187
}
153188

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+
155195
double fee = plugin.cfg.homeBase;
156196
if (homeLoc.getWorld() != curLoc.getWorld()) {
157197
fee += plugin.cfg.homeWorld;
@@ -161,17 +201,35 @@ private void callEssHome(Player p, Location homeLoc, Location curLoc, String hom
161201
}
162202
if (fee > plugin.cfg.homeMax) fee = plugin.cfg.homeMax;
163203
fee = Double.parseDouble(new DecimalFormat("#.00").format(fee));
164-
if (!plugin.vaultUtil.enoughMoney(p, fee)) {
204+
if (!plugin.vaultUtil.withdraw(p, fee)) {
165205
msg(p, "user.teleport.money_insufficient", fee);
166206
return;
167207
}
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;
175233
}
176234

177235
private void msg(CommandSender target, String template, Object... args) {

src/main/resources/lang/en_US.yml

+3
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ user:
242242
error: "That home seems to be broken, seek pm for help"
243243
invalid_home: "That home is in a removed world"
244244
bed_not_set_yet: "You has not set a bed"
245+
unsafe: "The teleport destination is unsafe"
246+
invalid_name: "Invalid home name"
247+
home_limit: "You cannot set more than %d homes"
245248
manual:
246249
no_description: "No description"
247250
no_usage: "No usage"

0 commit comments

Comments
 (0)