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

165 lines
4.7 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;
import com.massivecraft.factions.cmd.type.TypeFaction;
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;
2015-02-12 12:00:55 +01:00
import com.massivecraft.massivecore.MassiveException;
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;
2015-04-09 11:55:44 +02:00
import com.massivecraft.massivecore.teleport.Destination;
import com.massivecraft.massivecore.teleport.DestinationSimple;
2015-05-16 12:19:36 +02:00
import com.massivecraft.massivecore.util.MUtil;
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
// Parameters
this.addParameter(TypeFaction.get(), "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
2015-02-12 12:00:55 +01:00
public void perform() throws MassiveException
{
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
2015-05-06 17:04:35 +02:00
Faction faction = this.readArg(msenderFaction);
2014-09-18 13:41:20 +02:00
PS home = faction.getHome();
String homeDesc = "home for " + faction.describeTo(msender, false);
// Any and MPerm
if ( ! MPerm.getPermHome().has(msender, faction, true)) return;
2014-09-18 13:41:20 +02:00
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.getPermSethome().has(msender, faction, false))
2014-09-18 13:41:20 +02:00
{
msender.msg("<i>You should:");
msender.message(Factions.get().getOuterCmdFactions().cmdFactionsSethome.getTemplate());
2014-09-18 13:41:20 +02:00
}
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;
}
Faction factionHere = BoardColl.get().getFactionAt(PS.valueOf(me.getLocation()));
2014-09-18 13:41:20 +02:00
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.getFlagPvp())
&&
(
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())
{
2015-05-16 14:30:49 +02:00
if (MUtil.isntPlayer(p)) continue;
2015-05-16 12:19:36 +02:00
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
{
2015-04-09 11:55:44 +02:00
Destination destination = new DestinationSimple(home, homeDesc);
Mixin.teleport(me, destination, sender);
}
catch (TeleporterException e)
{
me.sendMessage(e.getMessage());
}
2011-03-23 17:39:56 +01:00
}
}