Factions/src/com/massivecraft/factions/cmd/CmdFactionsHome.java

135 lines
3.8 KiB
Java
Raw Normal View History

2011-10-09 21:57:43 +02:00
package com.massivecraft.factions.cmd;
2011-03-23 17:39:56 +01:00
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
2011-07-18 22:06:02 +02:00
import com.massivecraft.factions.BoardColl;
2013-04-09 13:15:25 +02:00
import com.massivecraft.factions.ConfServer;
import com.massivecraft.factions.FFlag;
2011-07-18 22:06:02 +02:00
import com.massivecraft.factions.FPlayer;
2013-04-09 13:22:23 +02:00
import com.massivecraft.factions.FPlayerColl;
2011-07-18 22:06:02 +02:00
import com.massivecraft.factions.Faction;
2013-04-16 11:27:03 +02:00
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.req.ReqRoleIsAtLeast;
2013-04-16 10:11:59 +02:00
import com.massivecraft.mcore.cmd.req.ReqHasPerm;
import com.massivecraft.mcore.cmd.req.ReqIsPlayer;
import com.massivecraft.mcore.mixin.Mixin;
import com.massivecraft.mcore.mixin.TeleporterException;
import com.massivecraft.mcore.ps.PS;
2011-03-23 17:39:56 +01:00
2013-04-10 13:12:22 +02:00
public class CmdFactionsHome extends FCommand
{
2013-04-10 13:12:22 +02:00
public CmdFactionsHome()
{
2013-04-16 10:11:59 +02:00
this.addAliases("home");
2011-03-23 17:39:56 +01:00
2013-04-16 10:11:59 +02:00
this.addRequirements(ReqHasPerm.get(Perm.HOME.node));
this.addRequirements(ReqIsPlayer.get());
this.addRequirements(ReqRoleIsAtLeast.get(Rel.RECRUIT));
2011-03-23 17:39:56 +01:00
}
@Override
public void perform()
{
// TODO: Hide this command on help also.
2013-04-09 13:15:25 +02:00
if ( ! ConfServer.homesEnabled)
{
2011-10-10 13:40:24 +02:00
fme.msg("<b>Sorry, Faction homes are disabled on this server.");
2011-03-23 17:39:56 +01:00
return;
}
2013-04-09 13:15:25 +02:00
if ( ! ConfServer.homesTeleportCommandEnabled)
{
2011-10-10 13:40:24 +02:00
fme.msg("<b>Sorry, the ability to teleport to Faction homes is disabled on this server.");
return;
}
2011-03-23 17:39:56 +01:00
if ( ! myFaction.hasHome())
{
fme.msg("<b>Your faction does not have a home. " + (fme.getRole().isLessThan(Rel.OFFICER) ? "<i> Ask your leader to:" : "<i>You should:"));
2013-04-16 11:27:03 +02:00
fme.sendMessage(Factions.get().getOuterCmdFactions().cmdFactionsSethome.getUseageTemplate());
2011-03-23 17:39:56 +01:00
return;
}
2013-04-09 13:15:25 +02:00
if ( ! ConfServer.homesTeleportAllowedFromEnemyTerritory && fme.isInEnemyTerritory())
{
2011-10-10 13:40:24 +02:00
fme.msg("<b>You cannot teleport to your faction home while in the territory of an enemy faction.");
return;
}
if (!ConfServer.homesTeleportAllowedFromDifferentWorld && !me.getWorld().getName().equalsIgnoreCase(myFaction.getHome().getWorld()))
{
2011-10-10 13:40:24 +02:00
fme.msg("<b>You cannot teleport to your faction home while in a different world.");
return;
}
Faction faction = BoardColl.get().getFactionAt(PS.valueOf(me));
Location loc = me.getLocation().clone();
// if player is not in a safe zone or their own faction territory, only allow teleport if no enemies are nearby
if
(
2013-04-09 13:15:25 +02:00
ConfServer.homesTeleportAllowedEnemyDistance > 0
&&
faction.getFlag(FFlag.PVP)
&&
(
! fme.isInOwnTerritory()
||
(
fme.isInOwnTerritory()
&&
2013-04-09 13:15:25 +02:00
! ConfServer.homesTeleportIgnoreEnemiesIfInOwnTerritory
)
)
)
{
World w = loc.getWorld();
double x = loc.getX();
double y = loc.getY();
double z = loc.getZ();
2011-10-08 23:22:02 +02:00
for (Player p : me.getServer().getOnlinePlayers())
{
if (p == null || !p.isOnline() || p.isDead() || p == me || p.getWorld() != w)
continue;
2013-04-12 08:56:26 +02:00
FPlayer fp = FPlayerColl.get().get(p);
if (fme.getRelationTo(fp) != Rel.ENEMY)
continue;
Location l = p.getLocation();
double dx = Math.abs(x - l.getX());
double dy = Math.abs(y - l.getY());
double dz = Math.abs(z - l.getZ());
2013-04-09 13:15:25 +02:00
double max = ConfServer.homesTeleportAllowedEnemyDistance;
// box-shaped distance check
if (dx > max || dy > max || dz > max)
continue;
2013-04-09 13:15:25 +02:00
fme.msg("<b>You cannot teleport to your faction home while an enemy is within " + ConfServer.homesTeleportAllowedEnemyDistance + " blocks of you.");
return;
}
}
try
{
Mixin.teleport(me, myFaction.getHome(), "your faction home", sender);
// if economy is enabled, they're not on the bypass list, and this command has a cost set, make 'em pay
2013-04-19 09:50:33 +02:00
if (!payForCommand(ConfServer.econCostHome)) return;
}
catch (TeleporterException e)
{
me.sendMessage(e.getMessage());
}
2011-03-23 17:39:56 +01:00
}
}