From e4f0b8e3dec05e2bfb3331c3836dc8bb8c8f33d9 Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Wed, 15 Jan 2020 11:32:14 +0100 Subject: [PATCH] Add ability to block chorus fruit and ender pearl teleportation. Fixes #51 --- src/com/massivecraft/factions/Factions.java | 2 + .../factions/engine/EngineDenyTeleport.java | 130 ++++++++++++++++++ .../massivecraft/factions/entity/MConf.java | 18 ++- 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 src/com/massivecraft/factions/engine/EngineDenyTeleport.java diff --git a/src/com/massivecraft/factions/Factions.java b/src/com/massivecraft/factions/Factions.java index 9cee196d..39fc6cde 100644 --- a/src/com/massivecraft/factions/Factions.java +++ b/src/com/massivecraft/factions/Factions.java @@ -26,6 +26,7 @@ import com.massivecraft.factions.engine.EngineChat; import com.massivecraft.factions.engine.EngineChunkChange; import com.massivecraft.factions.engine.EngineCleanInactivity; import com.massivecraft.factions.engine.EngineDenyCommands; +import com.massivecraft.factions.engine.EngineDenyTeleport; import com.massivecraft.factions.engine.EngineEcon; import com.massivecraft.factions.engine.EngineExploit; import com.massivecraft.factions.engine.EngineFlagEndergrief; @@ -218,6 +219,7 @@ public class Factions extends MassivePlugin EngineChunkChange.class, EngineCleanInactivity.class, EngineDenyCommands.class, + EngineDenyTeleport.class, EngineExploit.class, EngineFlagEndergrief.class, EngineFlagExplosion.class, diff --git a/src/com/massivecraft/factions/engine/EngineDenyTeleport.java b/src/com/massivecraft/factions/engine/EngineDenyTeleport.java new file mode 100644 index 00000000..5488e178 --- /dev/null +++ b/src/com/massivecraft/factions/engine/EngineDenyTeleport.java @@ -0,0 +1,130 @@ +package com.massivecraft.factions.engine; + +import com.massivecraft.factions.Rel; +import com.massivecraft.factions.entity.BoardColl; +import com.massivecraft.factions.entity.Faction; +import com.massivecraft.factions.entity.MConf; +import com.massivecraft.factions.entity.MPlayer; +import com.massivecraft.massivecore.Couple; +import com.massivecraft.massivecore.Engine; +import com.massivecraft.massivecore.mixin.MixinMessage; +import com.massivecraft.massivecore.ps.PS; +import com.massivecraft.massivecore.util.MUtil; +import com.massivecraft.massivecore.util.Txt; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; + +import java.util.List; +import java.util.Map.Entry; + +public class EngineDenyTeleport extends Engine +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static EngineDenyTeleport i = new EngineDenyTeleport(); + public static EngineDenyTeleport get() { return i; } + + // -------------------------------------------- // + // CAN COMBAT DAMAGE HAPPEN + // -------------------------------------------- // + + private enum TerritoryType + { + ENEMY, + WILDERNESS, + OWN, + OTHER + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void canTeleportHappen(PlayerTeleportEvent event) + { + Entry entry = shouldBeCancelled(event); + if (entry == null) return; + + event.setCancelled(true); + + TeleportCause cause = entry.getKey(); + TerritoryType deny = entry.getValue(); + + String teleportDesc = Txt.getNicedEnum(cause); + String denyDesc = ""; + if (deny == TerritoryType.ENEMY) denyDesc = "enemy"; + if (deny == TerritoryType.WILDERNESS) denyDesc = "wilderness"; + if (deny == TerritoryType.OWN) denyDesc = "your own"; + if (deny == TerritoryType.OTHER) denyDesc = "other faction's"; + + Player player = event.getPlayer(); + MixinMessage.get().msgOne(player, "Teleportation with %s is not allowed in %s territory.", teleportDesc, denyDesc); + } + + private Entry shouldBeCancelled(PlayerTeleportEvent event) + { + Player player = event.getPlayer(); + if (MUtil.isntPlayer(player)) return null; + + MPlayer mplayer = MPlayer.get(player); + + PS from = PS.valueOf(event.getFrom()); + PS to = PS.valueOf(event.getTo()); + + TerritoryType typeFrom = getTerritoryType(mplayer, from); + TerritoryType typeTo = getTerritoryType(mplayer, to); + List types = MUtil.list(typeFrom, typeTo); + + TeleportCause cause = event.getCause(); + MConf mconf = MConf.get(); + + if (cause == TeleportCause.CHORUS_FRUIT) + { + if (!mconf.allowChorusFruitInEnemyTerritory && types.contains(TerritoryType.ENEMY)) + return Couple.valueOf(cause, TerritoryType.ENEMY); + + if (!mconf.allowChorusFruitInWildernessTerritory && types.contains(TerritoryType.WILDERNESS)) + return Couple.valueOf(cause, TerritoryType.WILDERNESS); + + if (!mconf.allowChorusFruitInOwnTerritory && types.contains(TerritoryType.OWN)) + return Couple.valueOf(cause, TerritoryType.OWN); + + if (!mconf.allowChorusFruitInOtherTerritory && types.contains(TerritoryType.OTHER)) + return Couple.valueOf(cause, TerritoryType.OTHER); + } + else if (cause == TeleportCause.ENDER_PEARL) + { + if (!mconf.allowEnderPearlInEnemyTerritory && types.contains(TerritoryType.ENEMY)) + return Couple.valueOf(cause, TerritoryType.ENEMY); + + if (!mconf.allowEnderPearlInWildernessTerritory && types.contains(TerritoryType.WILDERNESS)) + return Couple.valueOf(cause, TerritoryType.WILDERNESS); + + if (!mconf.allowEnderPearlInOwnTerritory && types.contains(TerritoryType.OWN)) + return Couple.valueOf(cause, TerritoryType.OWN); + + if (!mconf.allowEnderPearlInOtherTerritory && types.contains(TerritoryType.OTHER)) + return Couple.valueOf(cause, TerritoryType.OTHER); + } + else + { + // Don't cancel other kinds of teleports + } + + return null; + } + + private TerritoryType getTerritoryType(MPlayer mplayer, PS territory) + { + Faction territoryFaction = BoardColl.get().getFactionAt(territory); + Rel relation = territoryFaction.getRelationTo(mplayer); + + if (territoryFaction.isNone()) return TerritoryType.WILDERNESS; + if (relation == Rel.ENEMY) return TerritoryType.ENEMY; + if (relation == Rel.FACTION) return TerritoryType.OWN; + return TerritoryType.OTHER; + } + +} diff --git a/src/com/massivecraft/factions/entity/MConf.java b/src/com/massivecraft/factions/entity/MConf.java index f99d71e8..5a38bc8e 100644 --- a/src/com/massivecraft/factions/entity/MConf.java +++ b/src/com/massivecraft/factions/entity/MConf.java @@ -477,8 +477,22 @@ public class MConf extends Entity // -------------------------------------------- // public int unstuckSeconds = 30; - public int unstuckChunkRadius = 10; - + public int unstuckChunkRadius = 10; + + // -------------------------------------------- // + // ENDER PEARL AND CHORUS FRUIT + // -------------------------------------------- // + + public boolean allowEnderPearlInEnemyTerritory = true; + public boolean allowEnderPearlInWildernessTerritory = true; + public boolean allowEnderPearlInOwnTerritory = true; + public boolean allowEnderPearlInOtherTerritory = true; + + public boolean allowChorusFruitInEnemyTerritory = true; + public boolean allowChorusFruitInWildernessTerritory = true; + public boolean allowChorusFruitInOwnTerritory = true; + public boolean allowChorusFruitInOtherTerritory = true; + // -------------------------------------------- // // LOGGING // -------------------------------------------- //