Skip to content

Commit

Permalink
Merge pull request NyaaCat#21 from Librazy/commandwarpper
Browse files Browse the repository at this point in the history
Teleports  NyaaCat#20 (Essentials command warpper)
  • Loading branch information
phoenixlzx committed Mar 7, 2017
2 parents 51a0a7d + 485155b commit a6e2c50
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 2 deletions.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ repositories {
name 'sk89q'
url 'http://maven.sk89q.com/artifactory/repo/'
}

maven {
name 'vault-repo'
url 'http://nexus.hc.to/content/repositories/pub_releases'
}

maven {
name 'ess-repo'
url 'http://repo.ess3.net/content/groups/essentials'
}
}

dependencies {
compile 'org.spigotmc:spigot-api:1.11.2-R0.1-SNAPSHOT'
compile 'net.milkbowl.vault:VaultAPI:1.6'
compile('net.ess3:Essentials:2.13-SNAPSHOT') {
transitive = false
}
compile files('lib/LocketteProAPI.jar')
compile 'com.sk89q.worldedit:worldedit-bukkit:6.1.5'
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/cat/nyaa/nyaautils/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ public class Configuration extends PluginConfigure {
@Serializable
public int timerCheckInterval = -1;

@Serializable(name = "teleport.enable")
public boolean teleportEnable = true;
@Serializable(name = "teleport.home.base")
public int homeBase = 10;
@Serializable(name = "teleport.home.world")
public int homeWorld = 20;
@Serializable(name = "teleport.home.distance")
public int homeDistance = 200;
@Serializable(name = "teleport.home.increment")
public int homeIncrement = 5;
@Serializable(name = "teleport.sethome.max")
public int setHomeMax = 100;
@Serializable(name = "teleport.sethome.min")
public int setHomeMin = 10;
@Serializable(name = "teleport.sethome.distance")
public int setHomeDistance = 200;
@Serializable(name = "teleport.sethome.decrement")
public int setHomeDecrement = 10;
@Serializable(name = "teleport.sethome.world")
public int setHomeWorld = 50;
@Serializable(name = "teleport.sethome.default_world")
public String setHomeDefaultWorld = "world";
@Serializable(name = "teleport.back.base")
public int backBase = 100;
@Serializable(name = "teleport.back.world")
public int backWorld = 20;
@Serializable(name = "teleport.back.distance")
public int backDistance = 200;
@Serializable(name = "teleport.back.increment")
public int backIncrement = 20;


@StandaloneConfig
public final MailboxLocations mailbox;
@StandaloneConfig
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/cat/nyaa/nyaautils/NyaaUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cat.nyaa.nyaautils;

import cat.nyaa.nyaautils.commandwarpper.Teleport;
import cat.nyaa.nyaautils.elytra.ElytraEnhanceListener;
import cat.nyaa.nyaautils.elytra.FuelManager;
import cat.nyaa.nyaautils.exhibition.ExhibitionListener;
Expand All @@ -22,6 +23,7 @@ public class NyaaUtils extends JavaPlugin {
public MailboxListener mailboxListener;
public VaultUtil vaultUtil;
public ElytraEnhanceListener elytraEnhanceListener;
public Teleport teleport;
public FuelManager fuelManager;
public TimerManager timerManager;
public TimerListener timerListener;
Expand All @@ -37,6 +39,7 @@ public void onEnable() {
lpListener = new LootProtectListener(this);
dsListener = new DamageStatListener(this);
elytraEnhanceListener = new ElytraEnhanceListener(this);
teleport = new Teleport(this);
exhibitionListener = new ExhibitionListener(this);
mailboxListener = new MailboxListener(this);
vaultUtil = new VaultUtil(this);
Expand Down
159 changes: 159 additions & 0 deletions src/main/java/cat/nyaa/nyaautils/commandwarpper/Teleport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package cat.nyaa.nyaautils.commandwarpper;

import cat.nyaa.nyaautils.I18n;
import cat.nyaa.nyaautils.NyaaUtils;
import cat.nyaa.nyaautils.api.events.HamsterEcoHelperTransactionApiEvent;

import java.text.DecimalFormat;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import net.ess3.api.IEssentials;
import net.ess3.api.IUser;
import org.bukkit.permissions.PermissionAttachment;

import java.util.List;

public class Teleport implements Listener {
private IEssentials ess;
private NyaaUtils plugin;

public Teleport(Object pl) {
this.plugin = (NyaaUtils) pl;
this.ess = (IEssentials) plugin.getServer().getPluginManager().getPlugin("Essentials");
if (plugin.cfg.teleportEnable) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onCommandPreProcess(PlayerCommandPreprocessEvent e) {
if (!plugin.cfg.teleportEnable) return;
String cmd = e.getMessage().toLowerCase();
Player p = e.getPlayer();
IUser iu = ess.getUser(p);
if (cmd.equals("/home") || cmd.startsWith("/home ")) {
e.setCancelled(true);
List<String> homes = iu.getHomes();
if (homes.size() < 1) {
msg(p, "user.teleport.not_set_yet");
} else if (homes.size() == 1 && cmd.equals("/home")) {
Location hl = null;
try {
hl = iu.getHome(homes.get(0));
} catch (Exception ex) {
ex.printStackTrace();
}
Location cl = p.getLocation();
callEssHome(p, hl, cl, null);
} else if (homes.size() > 1) {
String to = cmd.substring(5).trim();
for (String home : homes) {
if (home.equals(to)) {
Location hl = null;
try {
hl = iu.getHome(to);
} catch (Exception ex) {
ex.printStackTrace();
}
Location cl = p.getLocation();
callEssHome(p, hl, cl, to);
return;
}
}
PermissionAttachment attachment = p.addAttachment(NyaaUtils.instance, 1);
attachment.setPermission("essentials.home", true);
Bukkit.dispatchCommand(p, "essentials:home");
}
} else if (cmd.equals("/sethome") || cmd.startsWith("/sethome ")) {
e.setCancelled(true);
Location curLoc = p.getLocation();
World defaultWorld = Bukkit.getWorld(plugin.cfg.setHomeDefaultWorld);
if (defaultWorld == null) {
defaultWorld = Bukkit.getWorlds().get(0);
}
double fee = plugin.cfg.setHomeMax;
if (curLoc.getWorld() != defaultWorld) {
fee += plugin.cfg.setHomeWorld;
fee -= curLoc.distance(curLoc.getWorld().getSpawnLocation()) * (double) plugin.cfg.setHomeDecrement / plugin.cfg.setHomeDistance;
} else {
fee -= curLoc.distance(defaultWorld.getSpawnLocation()) * (double) plugin.cfg.setHomeDecrement / plugin.cfg.setHomeDistance;
}
if (fee < plugin.cfg.setHomeMin) fee = plugin.cfg.setHomeMin;
fee = Double.parseDouble(new DecimalFormat("#.00").format(fee));
if (!plugin.vaultUtil.enoughMoney(p, fee)) {
msg(p, "user.teleport.money_insufficient", fee);
return;
}
HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent(fee);
plugin.getServer().getPluginManager().callEvent(event);
msg(p, "user.teleport.ok", fee, I18n._("user.teleport.sethome"));
PermissionAttachment attachment = p.addAttachment(NyaaUtils.instance, 1);
attachment.setPermission("essentials.sethome", true);
Bukkit.dispatchCommand(p, cmd.substring(1).replace("sethome", "essentials:sethome"));
plugin.vaultUtil.withdraw(p, fee);
} else if (cmd.equals("/back")) {
e.setCancelled(true);
Location curLoc = p.getLocation();
Location lastLoc = iu.getLastLocation();
if (lastLoc == null) {
msg(p, "user.teleport.no_loc");
return;
}
double fee = plugin.cfg.backBase;
if (curLoc.getWorld() != lastLoc.getWorld()) {
fee += plugin.cfg.backWorld;
fee += lastLoc.distance(lastLoc.getWorld().getSpawnLocation()) * (double) plugin.cfg.backIncrement / plugin.cfg.backDistance;
} else {
fee += lastLoc.distance(curLoc) * (double) plugin.cfg.backIncrement / plugin.cfg.backDistance;
}
fee = Double.parseDouble(new DecimalFormat("#.00").format(fee));
if (!plugin.vaultUtil.enoughMoney(p, fee)) {
msg(p, "user.teleport.money_insufficient", fee);
return;
}
HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent(fee);
plugin.getServer().getPluginManager().callEvent(event);
msg(p, "user.teleport.ok", fee, I18n._("user.teleport.back"));
PermissionAttachment attachment = p.addAttachment(NyaaUtils.instance, 1);
attachment.setPermission("essentials.back", true);
Bukkit.dispatchCommand(p, "essentials:back");
plugin.vaultUtil.withdraw(p, fee);
}
}

private void callEssHome(Player p, Location homeLoc, Location curLoc, String home) {
double fee = plugin.cfg.homeBase;
if (homeLoc.getWorld() != curLoc.getWorld()) {
fee += plugin.cfg.homeWorld;
fee += homeLoc.distance(homeLoc.getWorld().getSpawnLocation()) * (double) plugin.cfg.homeIncrement / plugin.cfg.homeDistance;
} else {
fee += homeLoc.distance(curLoc) * (double) plugin.cfg.homeIncrement / plugin.cfg.homeDistance;
}
fee = Double.parseDouble(new DecimalFormat("#.00").format(fee));
if (!plugin.vaultUtil.enoughMoney(p, fee)) {
msg(p, "user.teleport.money_insufficient", fee);
return;
}
HamsterEcoHelperTransactionApiEvent event = new HamsterEcoHelperTransactionApiEvent(fee);
plugin.getServer().getPluginManager().callEvent(event);
msg(p, "user.teleport.ok", fee, I18n._("user.teleport.home"));
PermissionAttachment attachment = p.addAttachment(NyaaUtils.instance, 1);
attachment.setPermission("essentials.home", true);
attachment.setPermission("essentials.home.bed", true);
Bukkit.dispatchCommand(p, home == null ? "essentials:home" : "essentials:home " + home);
plugin.vaultUtil.withdraw(p, fee);
}

private void msg(CommandSender target, String template, Object... args) {
target.sendMessage(I18n._(template, args));
}
}
8 changes: 8 additions & 0 deletions src/main/resources/lang/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ user:
finish_1: "time to checkpoint %d [ %d min %.2fs ]"
finish_2: "time total [ %d min %.2fs ]"
list: "timers: %d"
teleport:
home: "home"
sethome: "set home"
back: "back"
ok: "You spend %.2f to %s"
not_set_yet: "You has not set a home"
no_loc: "No valid location found"
money_insufficient: "You don't have enough money(%.2f) to use this command"
manual:
no_description: "No description"
no_usage: "No usage"
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: NyaaUtils
main: cat.nyaa.nyaautils.NyaaUtils
description: "Helper/utilities plugin for Nyaacat Minecraft Server"
version: 2.0
depend: [Vault,WorldEdit]
depend: [Vault,WorldEdit,Essentials]
softdepend: [LockettePro]
authors: [RecursiveG]
authors: [RecursiveG,Librazy]
website: "https://github.com/NyaaCat/nyaautils"
database: false

Expand Down

0 comments on commit a6e2c50

Please sign in to comment.