Factions3/src/com/massivecraft/factions/util/RelationUtil.java

146 lines
4.2 KiB
Java
Raw Normal View History

2011-10-12 17:25:01 +02:00
package com.massivecraft.factions.util;
import com.massivecraft.factions.Rel;
2013-04-22 09:37:53 +02:00
import com.massivecraft.factions.RelationParticipator;
import com.massivecraft.factions.entity.Faction;
2013-04-22 09:41:48 +02:00
import com.massivecraft.factions.entity.MConf;
2017-03-24 13:05:58 +01:00
import com.massivecraft.factions.entity.MFlag;
import com.massivecraft.factions.entity.MPlayer;
2014-06-04 14:02:23 +02:00
import com.massivecraft.massivecore.util.Txt;
2017-03-24 13:05:58 +01:00
import org.bukkit.ChatColor;
2011-10-12 17:25:01 +02:00
public class RelationUtil
{
2017-03-08 19:43:39 +01:00
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
private static final String UNKNOWN_RELATION_OTHER = "A server admin";
private static final String UNDEFINED_FACTION_OTHER = "ERROR";
private static final String OWN_FACTION = "your faction";
private static final String SELF = "you";
// -------------------------------------------- //
// DESCRIBE
// -------------------------------------------- //
2011-10-12 17:25:01 +02:00
public static String describeThatToMe(RelationParticipator that, RelationParticipator me, boolean ucfirst)
{
String ret = "";
2011-10-12 18:48:47 +02:00
2017-03-08 19:43:39 +01:00
if (that == null) return UNKNOWN_RELATION_OTHER;
2011-10-23 20:50:49 +02:00
2011-10-12 18:48:47 +02:00
Faction thatFaction = getFaction(that);
2017-03-08 19:43:39 +01:00
if (thatFaction == null) return UNDEFINED_FACTION_OTHER; // ERROR
2011-10-12 18:48:47 +02:00
Faction myFaction = getFaction(me);
2017-03-08 19:43:39 +01:00
boolean isSameFaction = thatFaction == myFaction;
2011-10-12 17:25:01 +02:00
if (that instanceof Faction)
{
2017-03-08 19:43:39 +01:00
String thatFactionName = thatFaction.getName();
2014-09-18 13:41:20 +02:00
if (thatFaction.isNone())
{
2017-03-08 19:43:39 +01:00
ret = thatFactionName;
2014-09-18 13:41:20 +02:00
}
2017-03-08 19:43:39 +01:00
else if (me instanceof MPlayer && isSameFaction)
2011-10-12 17:25:01 +02:00
{
2017-03-08 19:43:39 +01:00
ret = OWN_FACTION;
2011-10-12 17:25:01 +02:00
}
else
{
2017-03-08 19:43:39 +01:00
ret = thatFactionName;
2011-10-12 17:25:01 +02:00
}
}
else if (that instanceof MPlayer)
2011-10-12 17:25:01 +02:00
{
2014-09-17 13:29:58 +02:00
MPlayer mplayerthat = (MPlayer) that;
2011-10-12 17:25:01 +02:00
if (that == me)
{
2017-03-08 19:43:39 +01:00
ret = SELF;
2011-10-12 17:25:01 +02:00
}
2017-03-08 19:43:39 +01:00
else if (isSameFaction)
2011-10-12 17:25:01 +02:00
{
2014-09-17 13:29:58 +02:00
ret = mplayerthat.getNameAndTitle(myFaction);
2011-10-12 17:25:01 +02:00
}
else
{
2014-09-17 13:29:58 +02:00
ret = mplayerthat.getNameAndFactionName();
2011-10-12 17:25:01 +02:00
}
}
2011-10-12 18:48:47 +02:00
2017-03-08 19:43:39 +01:00
if (ucfirst) ret = Txt.upperCaseFirst(ret);
2011-10-12 18:48:47 +02:00
2017-03-08 19:43:39 +01:00
return getColorOfThatToMe(that, me).toString() + ret;
2011-10-12 17:25:01 +02:00
}
2011-10-12 18:48:47 +02:00
public static String describeThatToMe(RelationParticipator that, RelationParticipator me)
2011-10-12 17:25:01 +02:00
{
return describeThatToMe(that, me, false);
}
2017-03-08 19:43:39 +01:00
// -------------------------------------------- //
// RELATION
// -------------------------------------------- //
2011-10-12 18:48:47 +02:00
2011-10-24 11:07:06 +02:00
public static Rel getRelationOfThatToMe(RelationParticipator that, RelationParticipator me)
2011-10-12 17:25:01 +02:00
{
2011-10-24 13:02:48 +02:00
return getRelationOfThatToMe(that, me, false);
2011-10-12 17:25:01 +02:00
}
2011-10-12 18:48:47 +02:00
2011-10-24 11:07:06 +02:00
public static Rel getRelationOfThatToMe(RelationParticipator that, RelationParticipator me, boolean ignorePeaceful)
2011-10-12 17:25:01 +02:00
{
2011-10-24 13:02:48 +02:00
Faction myFaction = getFaction(me);
if (myFaction == null) return Rel.NEUTRAL; // ERROR
2011-10-12 18:48:47 +02:00
2011-10-24 13:02:48 +02:00
Faction thatFaction = getFaction(that);
if (thatFaction == null) return Rel.NEUTRAL; // ERROR
2011-10-24 11:07:06 +02:00
2011-10-24 13:02:48 +02:00
if (myFaction.equals(thatFaction))
2011-10-12 17:25:01 +02:00
{
2017-03-08 19:43:39 +01:00
if (that instanceof MPlayer) return ((MPlayer) that).getRole();
return Rel.MEMBER;
2011-10-12 17:25:01 +02:00
}
2017-03-08 19:43:39 +01:00
MFlag flagPeaceful = MFlag.getFlagPeaceful();
if (!ignorePeaceful && (thatFaction.getFlag(flagPeaceful) || myFaction.getFlag(flagPeaceful))) return Rel.TRUCE;
// The faction with the lowest wish "wins"
Rel theirWish = thatFaction.getRelationWish(myFaction);
Rel myWish = myFaction.getRelationWish(thatFaction);
return theirWish.isLessThan(myWish) ? theirWish : myWish;
2011-10-12 17:25:01 +02:00
}
2017-03-08 19:43:39 +01:00
// -------------------------------------------- //
// FACTION
// -------------------------------------------- //
2011-10-12 18:48:47 +02:00
2011-10-12 17:25:01 +02:00
public static Faction getFaction(RelationParticipator rp)
{
2017-03-08 19:43:39 +01:00
if (rp instanceof Faction) return (Faction) rp;
2011-10-12 18:48:47 +02:00
2017-03-08 19:43:39 +01:00
if (rp instanceof MPlayer) return ((MPlayer) rp).getFaction();
2011-10-12 18:48:47 +02:00
2011-10-12 17:25:01 +02:00
// ERROR
return null;
}
2017-03-08 19:43:39 +01:00
// -------------------------------------------- //
// COLOR
// -------------------------------------------- //
2011-10-12 18:48:47 +02:00
public static ChatColor getColorOfThatToMe(RelationParticipator that, RelationParticipator me)
2011-10-12 17:25:01 +02:00
{
Faction thatFaction = getFaction(that);
if (thatFaction != null && thatFaction != getFaction(me))
{
2017-03-08 19:43:39 +01:00
if (thatFaction.getFlag(MFlag.getFlagFriendlyire())) return MConf.get().colorFriendlyFire;
2017-03-08 19:43:39 +01:00
if (!thatFaction.getFlag(MFlag.getFlagPvp())) return MConf.get().colorNoPVP;
}
2011-10-24 11:07:06 +02:00
return getRelationOfThatToMe(that, me).getColor();
2011-10-12 17:25:01 +02:00
}
2017-03-08 19:43:39 +01:00
2011-10-12 17:25:01 +02:00
}