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

137 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 com.massivecraft.factions.Rel;
import com.massivecraft.factions.cmd.type.TypeFaction;
2019-01-26 15:04:47 +01:00
import com.massivecraft.factions.cmd.type.TypeWarp;
import com.massivecraft.factions.entity.BoardColl;
2017-02-10 16:38:36 +01:00
import com.massivecraft.factions.entity.Faction;
import com.massivecraft.factions.entity.MConf;
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPerm;
import com.massivecraft.factions.entity.MPlayer;
2019-01-26 15:04:47 +01:00
import com.massivecraft.factions.entity.Warp;
import com.massivecraft.factions.event.EventFactionsWarpTeleport;
2015-02-12 12:00:55 +01:00
import com.massivecraft.massivecore.MassiveException;
2015-11-06 02:10:29 +01:00
import com.massivecraft.massivecore.command.requirement.RequirementIsPlayer;
import com.massivecraft.massivecore.mixin.MixinMessage;
2016-05-13 12:32:05 +02:00
import com.massivecraft.massivecore.mixin.MixinTeleport;
2014-06-04 14:02:23 +02:00
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;
2019-01-26 15:04:47 +01:00
import com.massivecraft.massivecore.util.Txt;
2017-03-24 13:05:58 +01:00
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
2011-03-23 17:39:56 +01:00
2019-01-26 15:04:47 +01:00
public class CmdFactionsWarpGo extends FactionsCommandWarp
{
2013-11-11 09:31:04 +01:00
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
2019-01-26 15:04:47 +01:00
public CmdFactionsWarpGo()
{
// Parameters
2019-01-26 15:04:47 +01:00
this.addParameter(TypeWarp.get(), "warp");
this.addParameter(TypeFaction.get(), "faction", "you");
2013-11-11 09:31:04 +01:00
// Requirements
2015-11-06 02:10:29 +01:00
this.addRequirements(RequirementIsPlayer.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
{
2014-09-18 13:41:20 +02:00
// Args
2019-01-26 15:04:47 +01:00
Faction faction = this.readArgAt(1, msenderFaction);
Warp warp = TypeWarp.get(faction).read(this.argAt(0), sender);
String warpDesc = Txt.parse("<h>%s <i>in <reset>%s<i>.", warp.getName(), faction.describeTo(msender, false));
2014-09-18 13:41:20 +02:00
// Any and MPerm
if ( ! MPerm.getPermWarp().has(msender, faction, true)) return;
2014-09-18 13:41:20 +02:00
2019-01-26 15:04:47 +01:00
if ( ! MConf.get().warpsTeleportAllowedFromEnemyTerritory && msender.isInEnemyTerritory())
{
2019-01-26 15:04:47 +01:00
throw new MassiveException().addMsg("<b>You cannot teleport to %s <b>while in the territory of an enemy faction.", warp);
2011-03-23 17:39:56 +01:00
}
2019-01-26 15:04:47 +01:00
if ( ! MConf.get().warpsTeleportAllowedFromDifferentWorld && !me.getWorld().getName().equalsIgnoreCase(warp.getWorld()))
{
2019-01-26 15:04:47 +01:00
throw new MassiveException().addMsg("<b>You cannot teleport to %s <b>while in a different world.", warpDesc);
}
2019-01-26 15:04:47 +01:00
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
(
2019-01-26 15:04:47 +01:00
MConf.get().warpsTeleportAllowedEnemyDistance > 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()
&&
2019-01-26 15:04:47 +01:00
! MConf.get().warpsTeleportIgnoreEnemiesIfInOwnTerritory
)
)
)
{
2014-09-18 13:41:20 +02:00
World w = locationHere.getWorld();
double x = locationHere.getX();
double y = locationHere.getY();
double z = locationHere.getZ();
2019-01-26 15:04:47 +01:00
for (Player p : w.getPlayers())
{
2015-05-16 14:30:49 +02:00
if (MUtil.isntPlayer(p)) continue;
2015-05-16 12:19:36 +02:00
2019-01-26 15:04:47 +01:00
if (p == null || !p.isOnline() || p.isDead() || p == me || p.getWorld() != w) continue;
MPlayer fp = MPlayer.get(p);
2019-01-26 15:04:47 +01: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());
2019-01-26 15:04:47 +01:00
double max = MConf.get().warpsTeleportAllowedEnemyDistance;
// box-shaped distance check
if (dx > max || dy > max || dz > max)
continue;
2019-01-26 15:04:47 +01:00
throw new MassiveException().addMsg("<b>You cannot teleport to %s <b>while an enemy is within %f blocks of you.", warpDesc, max);
}
}
// Event
2019-01-26 15:04:47 +01:00
EventFactionsWarpTeleport event = new EventFactionsWarpTeleport(sender, warp);
event.run();
if (event.isCancelled()) return;
// Apply
try
{
2019-01-26 15:04:47 +01:00
Destination destination = new DestinationSimple(warp.getLocation(), warpDesc);
2016-05-13 12:32:05 +02:00
MixinTeleport.get().teleport(me, destination, sender);
}
catch (TeleporterException e)
{
String message = e.getMessage();
MixinMessage.get().messageOne(me, message);
}
2011-03-23 17:39:56 +01:00
}
}