Factions/src/com/massivecraft/factions/engine/EngineMoveChunk.java

138 lines
4.8 KiB
Java
Raw Normal View History

2017-01-03 11:47:51 +01:00
package com.massivecraft.factions.engine;
import com.massivecraft.factions.AccessStatus;
2017-01-03 11:47:51 +01:00
import com.massivecraft.factions.TerritoryAccess;
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.factions.util.AsciiMap;
2017-01-03 11:47:51 +01:00
import com.massivecraft.massivecore.Engine;
import com.massivecraft.massivecore.mixin.MixinTitle;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
2017-03-24 13:05:58 +01:00
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerMoveEvent;
import java.util.Collections;
2017-01-03 11:47:51 +01:00
public class EngineMoveChunk extends Engine
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
private static EngineMoveChunk i = new EngineMoveChunk();
public static EngineMoveChunk get() { return i; }
// -------------------------------------------- //
// MOVE CHUNK: DETECT
// -------------------------------------------- //
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void moveChunkDetect(PlayerMoveEvent event)
{
// If the player is moving from one chunk to another ...
if (MUtil.isSameChunk(event)) return;
Player player = event.getPlayer();
if (MUtil.isntPlayer(player)) return;
// ... gather info on the player and the move ...
MPlayer mplayer = MPlayer.get(player);
PS chunkFrom = PS.valueOf(event.getFrom()).getChunk(true);
PS chunkTo = PS.valueOf(event.getTo()).getChunk(true);
// ... send info onwards and try auto-claiming.
sendChunkInfo(mplayer, player, chunkFrom, chunkTo);
tryAutoClaim(mplayer, chunkTo);
2017-01-03 11:47:51 +01:00
}
// -------------------------------------------- //
// MOVE CHUNK: SEND CHUNK INFO
2017-01-03 11:47:51 +01:00
// -------------------------------------------- //
private static void sendChunkInfo(MPlayer mplayer, Player player, PS chunkFrom, PS chunkTo)
2017-01-03 11:47:51 +01:00
{
sendAutoMapUpdate(mplayer, player);
sendFactionTerritoryInfo(mplayer, player, chunkFrom, chunkTo);
sendTerritoryAccessMessage(mplayer, chunkFrom, chunkTo);
}
private static void sendAutoMapUpdate(MPlayer mplayer, Player player)
{
if (!mplayer.isMapAutoUpdating()) return;
AsciiMap map = new AsciiMap(mplayer, player, false);
mplayer.message(map.render());
}
private static void sendFactionTerritoryInfo(MPlayer mplayer, Player player, PS chunkFrom, PS chunkTo)
{
Faction factionFrom = BoardColl.get().getFactionAt(chunkFrom);
Faction factionTo = BoardColl.get().getFactionAt(chunkTo);
if (factionFrom == factionTo) return;
if (mplayer.isTerritoryInfoTitles())
2017-01-03 11:47:51 +01:00
{
String titleMain = parseTerritoryInfo(MConf.get().territoryInfoTitlesMain, mplayer, factionTo);
String titleSub = parseTerritoryInfo(MConf.get().territoryInfoTitlesSub, mplayer, factionTo);
int ticksIn = MConf.get().territoryInfoTitlesTicksIn;
int ticksStay = MConf.get().territoryInfoTitlesTicksStay;
int ticksOut = MConf.get().territoryInfoTitleTicksOut;
MixinTitle.get().sendTitleMessage(player, ticksIn, ticksStay, ticksOut, titleMain, titleSub);
2017-01-03 11:47:51 +01:00
}
else
2017-01-03 11:47:51 +01:00
{
String message = parseTerritoryInfo(MConf.get().territoryInfoChat, mplayer, factionTo);
player.sendMessage(message);
2017-01-03 11:47:51 +01:00
}
}
private static String parseTerritoryInfo(String string, MPlayer mplayer, Faction faction)
2017-01-03 11:47:51 +01:00
{
if (string == null) throw new NullPointerException("string");
if (faction == null) throw new NullPointerException("faction");
2017-01-03 11:47:51 +01:00
string = Txt.parse(string);
string = string.replace("{name}", faction.getName());
string = string.replace("{relcolor}", faction.getColorTo(mplayer).toString());
2017-05-09 10:59:38 +02:00
string = string.replace("{desc}", faction.getDescriptionDesc());
2017-01-03 11:47:51 +01:00
return string;
}
private static void sendTerritoryAccessMessage(MPlayer mplayer, PS chunkFrom, PS chunkTo)
{
// Get TerritoryAccess for from & to chunks
TerritoryAccess accessFrom = BoardColl.get().getTerritoryAccessAt(chunkFrom);
TerritoryAccess accessTo = BoardColl.get().getTerritoryAccessAt(chunkTo);
// See if the status has changed
AccessStatus statusFrom = accessFrom.getTerritoryAccess(mplayer);
AccessStatus statusTo = accessTo.getTerritoryAccess(mplayer);
if (statusFrom == statusTo) return;
// Inform
mplayer.message(statusTo.getStatusMessage());
}
2017-01-03 11:47:51 +01:00
// -------------------------------------------- //
// MOVE CHUNK: TRY AUTO CLAIM
2017-01-03 11:47:51 +01:00
// -------------------------------------------- //
private static void tryAutoClaim(MPlayer mplayer, PS chunkTo)
2017-01-03 11:47:51 +01:00
{
// If the player is auto claiming ...
Faction autoClaimFaction = mplayer.getAutoClaimFaction();
if (autoClaimFaction == null) return;
// ... try claim.
mplayer.tryClaim(autoClaimFaction, Collections.singletonList(chunkTo));
}
}