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

236 lines
7.3 KiB
Java
Raw Normal View History

2018-03-04 23:13:32 +01:00
package com.massivecraft.factions.cmd;
2018-03-26 23:43:15 +02:00
import com.massivecraft.factions.*;
2018-03-04 23:13:32 +01:00
import com.massivecraft.factions.struct.Permission;
2018-03-26 23:43:15 +02:00
import com.massivecraft.factions.struct.Relation;
import com.massivecraft.factions.util.Particles.ParticleEffect;
import com.massivecraft.factions.util.WarmUpUtil;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
2018-03-04 23:13:32 +01:00
import com.massivecraft.factions.zcore.util.TL;
2018-03-26 23:43:15 +02:00
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
2018-07-29 21:34:40 +02:00
import org.bukkit.Particle;
2018-03-26 23:43:15 +02:00
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
2018-03-04 23:13:32 +01:00
public class CmdFly extends FCommand {
2018-03-26 23:43:15 +02:00
public static ConcurrentHashMap<String, Boolean> flyMap = new ConcurrentHashMap<String, Boolean>();
public static int id = -1;
public static int flyid = -1;
public CmdFly() {
super();
this.aliases.add("fly");
this.optionalArgs.put("on/off", "flip");
this.permission = Permission.FLY.node;
this.senderMustBeMember = true;
this.senderMustBeModerator = false;
}
public static void startParticles() {
// Just a secondary check.
2019-08-02 23:01:33 +02:00
if (!P.p.getConfig().getBoolean("ffly.Particles.Enabled")) {
return;
}
2019-08-02 23:01:33 +02:00
id = Bukkit.getScheduler().scheduleSyncRepeatingTask(P.p, () -> {
2019-03-27 15:54:32 +01:00
for (String name : flyMap.keySet()) {
Player player = Bukkit.getPlayer(name);
if (player == null) {
continue;
}
if (!player.isFlying()) {
continue;
}
2019-08-02 23:01:33 +02:00
if (!P.p.mc17) {
2019-03-27 15:54:32 +01:00
if (player.getGameMode() == GameMode.SPECTATOR) {
continue;
}
}
if (FPlayers.getInstance().getByPlayer(player).isVanished()) {
// Actually, vanished players (such as admins) should not display particles to prevent others from knowing their vanished assistance for moderation.
// But we can keep it as a config.
2019-08-02 23:01:33 +02:00
if (P.p.getConfig().getBoolean("ffly.Particles.Enable-While-Vanished")) {
2019-03-27 15:54:32 +01:00
return;
}
continue;
}
2019-08-02 23:01:33 +02:00
if (P.p.useNonPacketParticles) {
2019-03-27 15:54:32 +01:00
// 1.9+ based servers will use the built in particleAPI instead of packet based.
// any particle amount higher than 0 made them go everywhere, and the offset at 0 was not working.
// So setting the amount to 0 spawns 1 in the precise location
player.getWorld().spawnParticle(Particle.CLOUD, player.getLocation().add(0, -0.35, 0), 0);
} else {
ParticleEffect.CLOUD.display((float) 0, (float) 0, (float) 0, (float) 0, 3, player.getLocation().add(0, -0.35, 0), 16);
}
}
if (flyMap.keySet().size() == 0) {
Bukkit.getScheduler().cancelTask(id);
id = -1;
}
}, 10L, 3L);
}
public static void startFlyCheck() {
2019-08-02 23:01:33 +02:00
flyid = Bukkit.getScheduler().scheduleSyncRepeatingTask(P.p, () -> { //threw the exception for now, until I recode fly :( Cringe.
2019-03-27 15:54:32 +01:00
checkTaskState();
if (flyMap.keySet().size() != 0) {
for (String name : flyMap.keySet()) {
2019-03-27 15:54:32 +01:00
if (name == null) {
continue;
}
Player player = Bukkit.getPlayer(name);
2019-05-23 23:08:18 +02:00
if (player == null
|| !player.isFlying()
|| player.getGameMode() == GameMode.CREATIVE
2019-08-02 23:01:33 +02:00
|| !P.p.mc17 && player.getGameMode() == GameMode.SPECTATOR) {
continue;
}
2019-03-27 15:54:32 +01:00
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(player);
Faction myFaction = fPlayer.getFaction();
if (myFaction.isWilderness()) {
fPlayer.setFlying(false);
flyMap.remove(name);
continue;
}
if (fPlayer.checkIfNearbyEnemies()) {
continue;
}
FLocation myFloc = new FLocation(player.getLocation());
if (Board.getInstance().getFactionAt(myFloc) != myFaction) {
2019-05-23 23:08:18 +02:00
if (!checkBypassPerms(fPlayer, player, Board.getInstance().getFactionAt(myFloc))) {
fPlayer.setFlying(false);
flyMap.remove(name);
}
}
2019-03-27 15:54:32 +01:00
}
}
2019-03-27 15:54:32 +01:00
}, 20L, 20L);
}
private static boolean checkBypassPerms(FPlayer fme, Player me, Faction toFac) {
if (toFac != fme.getFaction()) {
if (!me.hasPermission("factions.fly.wilderness") && toFac.isWilderness() || !me.hasPermission("factions.fly.safezone") && toFac.isSafeZone() || !me.hasPermission("factions.fly.warzone") && toFac.isWarZone()) {
fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme));
return false;
}
Access access = toFac.getAccess(fme, PermissableAction.FLY);
if ((!(me.hasPermission("factions.fly.enemy") || access == Access.ALLOW)) && toFac.getRelationTo(fme.getFaction()) == Relation.ENEMY) {
fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme));
return false;
}
if (!(me.hasPermission("factions.fly.ally") || access == Access.ALLOW) && toFac.getRelationTo(fme.getFaction()) == Relation.ALLY) {
fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme));
return false;
}
if (!(me.hasPermission("factions.fly.truce") || access == Access.ALLOW) && toFac.getRelationTo(fme.getFaction()) == Relation.TRUCE) {
fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme));
return false;
}
if (!(me.hasPermission("factions.fly.neutral") || access == Access.ALLOW) && toFac.getRelationTo(fme.getFaction()) == Relation.NEUTRAL && !isSystemFaction(toFac)) {
fme.msg(TL.COMMAND_FLY_NO_ACCESS, toFac.getTag(fme));
return false;
}
return me.hasPermission("factions.fly") && access != Access.DENY;
}
return true;
}
public static Boolean isSystemFaction(Faction faction) {
return faction.isSafeZone() ||
2019-05-23 23:08:18 +02:00
faction.isWarZone() ||
faction.isWilderness();
}
public static void checkTaskState() {
if (flyMap.keySet().size() == 0) {
Bukkit.getScheduler().cancelTask(flyid);
flyid = -1;
}
}
public boolean isInFlightChecker(Player player) {
return flyMap.containsKey(player.getName());
}
@Override
public void perform() {
// Disabled by default.
2019-08-02 23:01:33 +02:00
if (!P.p.getConfig().getBoolean("enable-faction-flight", false)) {
fme.msg(TL.COMMAND_FLY_DISABLED);
return;
}
FLocation myfloc = new FLocation(me.getLocation());
Faction toFac = Board.getInstance().getFactionAt(myfloc);
if (!checkBypassPerms(fme, me, toFac)) return;
List<Entity> entities = this.me.getNearbyEntities(16.0D, 256.0D, 16.0D);
for (int i = 0; i <= entities.size() - 1; ++i) {
if (entities.get(i) instanceof Player) {
Player eplayer = (Player) entities.get(i);
FPlayer efplayer = FPlayers.getInstance().getByPlayer(eplayer);
if (efplayer.getRelationTo(this.fme) == Relation.ENEMY && !efplayer.isStealthEnabled()) {
this.fme.msg(TL.COMMAND_FLY_CHECK_ENEMY);
return;
}
}
}
2019-05-23 23:08:18 +02:00
if (args.size() == 0) {
2019-05-20 19:27:45 +02:00
toggleFlight(fme.isFlying(), me);
} else if (args.size() == 1) {
toggleFlight(argAsBool(0), me);
}
}
private void toggleFlight(final boolean toggle, final Player player) {
2019-05-20 19:27:45 +02:00
if (toggle) {
fme.setFlying(false);
flyMap.remove(player.getName());
return;
}
if (fme.canFlyAtLocation())
2019-03-27 15:54:32 +01:00
this.doWarmUp(WarmUpUtil.Warmup.FLIGHT, TL.WARMUPS_NOTIFY_FLIGHT, "Fly", () -> {
fme.setFlying(true);
flyMap.put(player.getName(), true);
if (id == -1) {
2019-08-02 23:01:33 +02:00
if (P.p.getConfig().getBoolean("ffly.Particles.Enabled")) {
2019-03-27 15:54:32 +01:00
startParticles();
}
}
2019-03-27 15:54:32 +01:00
if (flyid == -1) {
startFlyCheck();
}
}, this.p.getConfig().getLong("warmups.f-fly", 0));
}
public static void disableFlight(final FPlayer fme) {
fme.setFlying(false);
flyMap.remove(fme.getPlayer().getName());
}
@Override
public TL getUsageTranslation() {
return TL.COMMAND_FLY_DESCRIPTION;
}
2018-03-04 23:13:32 +01:00
2019-05-23 23:08:18 +02:00
}