Saber-Factions/src/main/java/com/massivecraft/factions/cmd/CmdStuck.java

112 lines
5.1 KiB
Java
Raw Normal View History

2015-05-13 05:16:47 +02:00
package com.massivecraft.factions.cmd;
import com.massivecraft.factions.*;
import com.massivecraft.factions.integration.Essentials;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.util.SpiralTask;
import com.massivecraft.factions.zcore.util.TL;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class CmdStuck extends FCommand {
public CmdStuck() {
super();
this.aliases.add("stuck");
this.aliases.add("halp!"); // halp!c:
2015-05-13 05:16:47 +02:00
this.permission = Permission.STUCK.node;
this.disableOnLock = true;
senderMustBePlayer = false;
senderMustBeMember = false;
senderMustBeModerator = false;
senderMustBeAdmin = false;
2018-12-21 22:46:10 +01:00
2015-05-13 05:16:47 +02:00
}
@Override
public void perform() {
final Player player = fme.getPlayer();
final Location sentAt = player.getLocation();
final FLocation chunk = fme.getLastStoodAt();
2018-11-07 06:38:43 +01:00
final long delay = SavageFactions.plugin.getConfig().getLong("hcf.stuck.delay", 30);
final int radius = SavageFactions.plugin.getConfig().getInt("hcf.stuck.radius", 10);
2015-05-13 05:16:47 +02:00
2018-11-07 06:38:43 +01:00
if (SavageFactions.plugin.getStuckMap().containsKey(player.getUniqueId())) {
long wait = SavageFactions.plugin.getTimers().get(player.getUniqueId()) - System.currentTimeMillis();
2015-05-13 05:16:47 +02:00
String time = DurationFormatUtils.formatDuration(wait, TL.COMMAND_STUCK_TIMEFORMAT.toString(), true);
msg(TL.COMMAND_STUCK_EXISTS, time);
} else {
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
if (!payForCommand(Conf.econCostStuck, TL.COMMAND_STUCK_TOSTUCK.format(fme.getName()), TL.COMMAND_STUCK_FORSTUCK.format(fme.getName()))) {
2015-05-13 05:16:47 +02:00
return;
}
2018-11-07 06:38:43 +01:00
final int id = Bukkit.getScheduler().runTaskLater(SavageFactions.plugin, new BukkitRunnable() {
2015-05-13 05:16:47 +02:00
@Override
public void run() {
if (!SavageFactions.plugin.getStuckMap().containsKey(player.getUniqueId())) {
2015-05-13 05:16:47 +02:00
return;
}
// check for world difference or radius exceeding
final World world = chunk.getWorld();
if (world.getUID() != player.getWorld().getUID() || sentAt.distance(player.getLocation()) > radius) {
msg(TL.COMMAND_STUCK_OUTSIDE.format(radius));
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.getTimers().remove(player.getUniqueId());
SavageFactions.plugin.getStuckMap().remove(player.getUniqueId());
2015-05-13 05:16:47 +02:00
return;
}
final Board board = Board.getInstance();
// spiral task to find nearest wilderness chunk
new SpiralTask(new FLocation(me), radius * 2) {
@Override
public boolean work() {
FLocation chunk = currentFLocation();
Faction faction = board.getFactionAt(chunk);
2018-11-07 06:38:43 +01:00
int buffer = SavageFactions.plugin.getConfig().getInt("world-border.buffer", 0);
if (faction.isWilderness() && !chunk.isOutsideWorldBorder(buffer)) {
2015-05-13 05:16:47 +02:00
int cx = FLocation.chunkToBlock((int) chunk.getX());
int cz = FLocation.chunkToBlock((int) chunk.getZ());
int y = world.getHighestBlockYAt(cx, cz);
Location tp = new Location(world, cx, y, cz);
msg(TL.COMMAND_STUCK_TELEPORT, tp.getBlockX(), tp.getBlockY(), tp.getBlockZ());
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.getTimers().remove(player.getUniqueId());
SavageFactions.plugin.getStuckMap().remove(player.getUniqueId());
if (!Essentials.handleTeleport(player, tp)) {
2015-05-13 05:16:47 +02:00
player.teleport(tp);
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.debug("/f stuck used regular teleport, not essentials!");
2015-05-13 05:16:47 +02:00
}
this.stop();
return false;
}
return true;
}
};
}
}, delay * 20).getTaskId();
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.getTimers().put(player.getUniqueId(), System.currentTimeMillis() + (delay * 1000));
long wait = SavageFactions.plugin.getTimers().get(player.getUniqueId()) - System.currentTimeMillis();
2015-05-13 05:16:47 +02:00
String time = DurationFormatUtils.formatDuration(wait, TL.COMMAND_STUCK_TIMEFORMAT.toString(), true);
msg(TL.COMMAND_STUCK_START, time);
2018-11-07 06:38:43 +01:00
SavageFactions.plugin.getStuckMap().put(player.getUniqueId(), id);
2015-05-13 05:16:47 +02:00
}
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_STUCK_DESCRIPTION;
}
}