Factions3/src/main/java/com/massivecraft/factions/cmd/CmdFactionsHome.java

160 lines
4.4 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
2013-04-16 11:27:03 +02:00
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.Perm;
import com.massivecraft.factions.Rel;
2014-09-18 13:41:20 +02:00
import com.massivecraft.factions.cmd.arg.ARFaction;
import com.massivecraft.factions.entity.BoardColl;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.entity.Faction;
2014-06-04 16:47:01 +02:00
import com.massivecraft.factions.event.EventFactionsHomeTeleport;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.cmd.req.ReqHasPerm;
import com.massivecraft.massivecore.cmd.req.ReqIsPlayer;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.mixin.TeleporterException;
import com.massivecraft.massivecore.ps.PS;
2011-03-23 17:39:56 +01:00
2014-09-18 13:41:20 +02:00
public class CmdFactionsHome extends FactionsCommandHome
{
2013-11-11 09:31:04 +01:00
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
2013-04-10 13:12:22 +02:00
public CmdFactionsHome()
{
2013-11-11 09:31:04 +01:00
// Aliases
2013-04-16 10:11:59 +02:00
this.addAliases("home");
2014-09-18 13:41:20 +02:00
// Args
this.addOptionalArg("faction", "you");
2013-11-11 09:31:04 +01:00
// Requirements
this.addRequirements(ReqHasPerm.get(Perm.HOME.node));
this.addRequirements(ReqIsPlayer.get());
2011-03-23 17:39:56 +01:00
}
2014-09-18 13:41:20 +02:00
2013-11-11 09:31:04 +01:00
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
2011-03-23 17:39:56 +01:00
@Override
public void perform()
{
if ( ! MConf.get().homesTeleportCommandEnabled)
{
2014-09-18 13:41:20 +02:00
msender.msg("<b>Sorry, the ability to teleport to Faction homes is disabled on this server.");
return;
}
2011-03-23 17:39:56 +01:00
2014-09-18 13:41:20 +02:00
// Args
Faction faction = this.arg(0, ARFaction.get(), msenderFaction);
if (faction == null) return;
boolean other = faction != msenderFaction;
PS home = faction.getHome();
String homeDesc = "home for " + faction.describeTo(msender, false);
// Other Perm
if (other && !Perm.HOME_OTHER.has(sender, true)) return;
if (home == null)
{
2014-09-18 13:41:20 +02:00
msender.msg("<b>%s <b>does not have a home.", faction.describeTo(msender, true));
if (MPerm.getSethome().has(msender, faction, false))
2014-09-18 13:41:20 +02:00
{
msender.msg("<i>You should:");
msender.sendMessage(Factions.get().getOuterCmdFactions().cmdFactionsSethome.getUseageTemplate());
}
2011-03-23 17:39:56 +01:00
return;
}
2014-09-18 13:41:20 +02:00
if ( ! MConf.get().homesTeleportAllowedFromEnemyTerritory && msender.isInEnemyTerritory())
{
2014-09-18 13:41:20 +02:00
msender.msg("<b>You cannot teleport to %s <b>while in the territory of an enemy faction.", homeDesc);
return;
}
2014-09-18 13:41:20 +02:00
if ( ! MConf.get().homesTeleportAllowedFromDifferentWorld && !me.getWorld().getName().equalsIgnoreCase(home.getWorld()))
{
2014-09-18 13:41:20 +02:00
msender.msg("<b>You cannot teleport to %s <b>while in a different world.", homeDesc);
return;
}
2014-09-18 13:41:20 +02:00
Faction factionHere = BoardColl.get().getFactionAt(PS.valueOf(me));
Location locationHere = 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
(
MConf.get().homesTeleportAllowedEnemyDistance > 0
&&
factionHere.getFlag(MFlag.getPvp())
&&
(
2014-09-18 13:41:20 +02:00
! msender.isInOwnTerritory()
||
(
2014-09-18 13:41:20 +02:00
msender.isInOwnTerritory()
&&
! MConf.get().homesTeleportIgnoreEnemiesIfInOwnTerritory
)
)
)
{
2014-09-18 13:41:20 +02:00
World w = locationHere.getWorld();
double x = locationHere.getX();
double y = locationHere.getY();
double z = locationHere.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;
MPlayer fp = MPlayer.get(p);
2014-09-18 13:41:20 +02:00
if (msender.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());
double max = MConf.get().homesTeleportAllowedEnemyDistance;
// box-shaped distance check
if (dx > max || dy > max || dz > max)
continue;
2014-09-18 13:41:20 +02:00
msender.msg("<b>You cannot teleport to %s <b>while an enemy is within %f blocks of you.", homeDesc, MConf.get().homesTeleportAllowedEnemyDistance);
return;
}
}
// Event
2014-06-04 16:47:01 +02:00
EventFactionsHomeTeleport event = new EventFactionsHomeTeleport(sender);
event.run();
if (event.isCancelled()) return;
// Apply
try
{
2014-09-18 13:41:20 +02:00
Mixin.teleport(me, home, homeDesc, sender);
}
catch (TeleporterException e)
{
me.sendMessage(e.getMessage());
}
2011-03-23 17:39:56 +01:00
}
}