2014-10-02 11:45:06 +02:00
package com.massivecraft.factions.entity ;
import java.util.List ;
2014-11-19 11:24:36 +01:00
import com.massivecraft.factions.event.EventFactionsCreateFlags ;
2015-11-06 02:10:29 +01:00
import com.massivecraft.massivecore.Named ;
2015-10-21 19:35:41 +02:00
import com.massivecraft.massivecore.PredicateIsRegistered ;
2014-10-02 11:45:06 +02:00
import com.massivecraft.massivecore.Prioritized ;
2015-08-03 16:17:38 +02:00
import com.massivecraft.massivecore.PriorityComparator ;
2014-10-02 11:45:06 +02:00
import com.massivecraft.massivecore.Registerable ;
2014-10-18 18:30:13 +02:00
import com.massivecraft.massivecore.collections.MassiveList ;
2014-10-02 11:45:06 +02:00
import com.massivecraft.massivecore.store.Entity ;
import com.massivecraft.massivecore.util.Txt ;
2015-11-06 02:10:29 +01:00
public class MFlag extends Entity < MFlag > implements Prioritized , Registerable , Named
2014-10-02 11:45:06 +02:00
{
// -------------------------------------------- //
// CONSTANTS
// -------------------------------------------- //
public final static transient String ID_OPEN = " open " ;
public final static transient String ID_MONSTERS = " monsters " ;
2015-09-08 05:06:57 +02:00
public final static transient String ID_ANIMALS = " animals " ;
2014-10-02 11:45:06 +02:00
public final static transient String ID_POWERLOSS = " powerloss " ;
public final static transient String ID_PVP = " pvp " ;
public final static transient String ID_FRIENDLYFIRE = " friendlyfire " ;
public final static transient String ID_EXPLOSIONS = " explosions " ;
public final static transient String ID_OFFLINEEXPLOSIONS = " offlineexplosions " ;
public final static transient String ID_FIRESPREAD = " firespread " ;
public final static transient String ID_ENDERGRIEF = " endergrief " ;
public final static transient String ID_PERMANENT = " permanent " ;
public final static transient String ID_PEACEFUL = " peaceful " ;
public final static transient String ID_INFPOWER = " infpower " ;
public final static transient int PRIORITY_OPEN = 1000 ;
public final static transient int PRIORITY_MONSTERS = 2000 ;
2015-09-08 05:06:57 +02:00
public final static transient int PRIORITY_ANIMALS = 2500 ;
2014-10-02 11:45:06 +02:00
public final static transient int PRIORITY_POWERLOSS = 3000 ;
public final static transient int PRIORITY_PVP = 4000 ;
public final static transient int PRIORITY_FRIENDLYFIRE = 5000 ;
public final static transient int PRIORITY_EXPLOSIONS = 6000 ;
public final static transient int PRIORITY_OFFLINEEXPLOSIONS = 7000 ;
public final static transient int PRIORITY_FIRESPREAD = 8000 ;
public final static transient int PRIORITY_ENDERGRIEF = 9000 ;
public final static transient int PRIORITY_PERMANENT = 10000 ;
public final static transient int PRIORITY_PEACEFUL = 11000 ;
public final static transient int PRIORITY_INFPOWER = 12000 ;
// -------------------------------------------- //
// META: CORE
// -------------------------------------------- //
public static MFlag get ( Object oid )
{
return MFlagColl . get ( ) . get ( oid ) ;
}
public static List < MFlag > getAll ( )
{
2014-11-19 11:24:36 +01:00
return getAll ( false ) ;
}
public static List < MFlag > getAll ( boolean isAsync )
{
setupStandardFlags ( ) ;
new EventFactionsCreateFlags ( isAsync ) . run ( ) ;
2015-10-21 19:35:41 +02:00
return MFlagColl . get ( ) . getAll ( PredicateIsRegistered . get ( ) , PriorityComparator . get ( ) ) ;
2014-10-02 11:45:06 +02:00
}
public static void setupStandardFlags ( )
{
2014-10-07 12:30:44 +02:00
getFlagOpen ( ) ;
getFlagMonsters ( ) ;
2015-09-08 05:06:57 +02:00
getFlagAnimals ( ) ;
2014-10-07 12:30:44 +02:00
getFlagPowerloss ( ) ;
getFlagPvp ( ) ;
getFlagFriendlyire ( ) ;
getFlagExplosions ( ) ;
getFlagOfflineexplosions ( ) ;
getFlagFirespread ( ) ;
getFlagEndergrief ( ) ;
getFlagPermanent ( ) ;
getFlagPeaceful ( ) ;
getFlagInfpower ( ) ;
2014-10-02 11:45:06 +02:00
}
2014-11-24 07:56:10 +01:00
public static MFlag getFlagOpen ( ) { return getCreative ( PRIORITY_OPEN , ID_OPEN , ID_OPEN , " Can the faction be joined without an invite? " , " Anyone can join. No invite required. " , " An invite is required to join. " , false , true , true ) ; }
2014-10-18 18:30:13 +02:00
public static MFlag getFlagMonsters ( ) { return getCreative ( PRIORITY_MONSTERS , ID_MONSTERS , ID_MONSTERS , " Can monsters spawn in this territory? " , " Monsters can spawn in this territory. " , " Monsters can NOT spawn in this territory. " , false , true , true ) ; }
2015-09-08 05:06:57 +02:00
public static MFlag getFlagAnimals ( ) { return getCreative ( PRIORITY_ANIMALS , ID_ANIMALS , ID_ANIMALS , " Can animals spawn in this territory? " , " Animals can spawn in this territory. " , " Animals can NOT spawn in this territory. " , true , true , true ) ; }
2014-10-18 18:30:13 +02:00
public static MFlag getFlagPowerloss ( ) { return getCreative ( PRIORITY_POWERLOSS , ID_POWERLOSS , ID_POWERLOSS , " Is power lost on death in this territory? " , " Power is lost on death in this territory. " , " Power is NOT lost on death in this territory. " , true , false , true ) ; }
public static MFlag getFlagPvp ( ) { return getCreative ( PRIORITY_PVP , ID_PVP , ID_PVP , " Can you PVP in territory? " , " You can PVP in this territory. " , " You can NOT PVP in this territory. " , true , false , true ) ; }
2015-01-09 22:36:12 +01:00
public static MFlag getFlagFriendlyire ( ) { return getCreative ( PRIORITY_FRIENDLYFIRE , ID_FRIENDLYFIRE , ID_FRIENDLYFIRE , " Can friends hurt eachother in this territory? " , " Friendly fire is on here. " , " Friendly fire is off here. " , false , false , true ) ; }
2014-10-18 18:30:13 +02:00
public static MFlag getFlagExplosions ( ) { return getCreative ( PRIORITY_EXPLOSIONS , ID_EXPLOSIONS , ID_EXPLOSIONS , " Can explosions occur in this territory? " , " Explosions can occur in this territory. " , " Explosions can NOT occur in this territory. " , true , false , true ) ; }
2015-01-09 22:36:12 +01:00
public static MFlag getFlagOfflineexplosions ( ) { return getCreative ( PRIORITY_OFFLINEEXPLOSIONS , ID_OFFLINEEXPLOSIONS , ID_OFFLINEEXPLOSIONS , " Can explosions occur if faction is offline? " , " Explosions if faction is offline. " , " No explosions if faction is offline. " , false , false , true ) ; }
2014-10-18 18:30:13 +02:00
public static MFlag getFlagFirespread ( ) { return getCreative ( PRIORITY_FIRESPREAD , ID_FIRESPREAD , ID_FIRESPREAD , " Can fire spread in territory? " , " Fire can spread in this territory. " , " Fire can NOT spread in this territory. " , true , false , true ) ; }
2015-02-12 12:00:55 +01:00
public static MFlag getFlagEndergrief ( ) { return getCreative ( PRIORITY_ENDERGRIEF , ID_ENDERGRIEF , ID_ENDERGRIEF , " Can endermen grief in this territory? " , " Endermen can grief in this territory. " , " Endermen can NOT grief in this territory. " , false , false , true ) ; }
2014-10-18 18:30:13 +02:00
public static MFlag getFlagPermanent ( ) { return getCreative ( PRIORITY_PERMANENT , ID_PERMANENT , ID_PERMANENT , " Is the faction immune to deletion? " , " The faction can NOT be deleted. " , " The faction can be deleted. " , false , false , true ) ; }
public static MFlag getFlagPeaceful ( ) { return getCreative ( PRIORITY_PEACEFUL , ID_PEACEFUL , ID_PEACEFUL , " Is the faction in truce with everyone? " , " The faction is in truce with everyone. " , " The faction relations work as usual. " , false , false , true ) ; }
public static MFlag getFlagInfpower ( ) { return getCreative ( PRIORITY_INFPOWER , ID_INFPOWER , ID_INFPOWER , " Does the faction have infinite power? " , " The faction has infinite power. " , " The faction power works as usual. " , false , false , true ) ; }
public static MFlag getCreative ( int priority , String id , String name , String desc , String descYes , String descNo , boolean standard , boolean editable , boolean visible )
2014-10-02 11:45:06 +02:00
{
MFlag ret = MFlagColl . get ( ) . get ( id , false ) ;
if ( ret ! = null )
{
ret . setRegistered ( true ) ;
return ret ;
}
2014-10-18 18:30:13 +02:00
ret = new MFlag ( priority , name , desc , descYes , descNo , standard , editable , visible ) ;
2014-10-02 11:45:06 +02:00
MFlagColl . get ( ) . attach ( ret , id ) ;
ret . setRegistered ( true ) ;
ret . sync ( ) ;
return ret ;
}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public MFlag load ( MFlag that )
{
this . priority = that . priority ;
this . name = that . name ;
this . desc = that . desc ;
2014-10-18 18:30:13 +02:00
this . descYes = that . descYes ;
this . descNo = that . descNo ;
2014-10-02 11:45:06 +02:00
this . standard = that . standard ;
this . editable = that . editable ;
this . visible = that . visible ;
return this ;
}
// -------------------------------------------- //
// TRANSIENT FIELDS (Registered)
// -------------------------------------------- //
private transient boolean registered = false ;
public boolean isRegistered ( ) { return this . registered ; }
public void setRegistered ( boolean registered ) { this . registered = registered ; }
// -------------------------------------------- //
// FIELDS
// -------------------------------------------- //
2014-11-18 09:49:11 +01:00
// The sort priority. Low values appear first in sorted lists.
// 1 is high up, 99999 is far down.
// Standard Faction flags use "thousand values" like 1000, 2000, 3000 etc to allow adding new flags inbetween.
// So 1000 might sound like a lot but it's actually the priority for the first flag.
2014-10-02 11:45:06 +02:00
private int priority = 0 ;
@Override public int getPriority ( ) { return this . priority ; }
public MFlag setPriority ( int priority ) { this . priority = priority ; this . changed ( ) ; return this ; }
2014-11-18 09:49:11 +01:00
// The name of the flag. According to standard it should be fully lowercase just like the flag id.
// In fact the name and the id of all standard flags are the same.
// I just added the name in case anyone feel like renaming their flags for some reason.
2014-10-02 11:45:06 +02:00
// Example: "monsters"
private String name = " defaultName " ;
2015-11-06 02:10:29 +01:00
@Override public String getName ( ) { return this . name ; }
2014-10-02 11:45:06 +02:00
public MFlag setName ( String name ) { this . name = name ; this . changed ( ) ; return this ; }
2014-11-18 09:49:11 +01:00
// The flag function described as a question.
2014-10-02 11:45:06 +02:00
// Example: "Can monsters spawn in this territory?"
private String desc = " defaultDesc " ;
public String getDesc ( ) { return this . desc ; }
public MFlag setDesc ( String desc ) { this . desc = desc ; this . changed ( ) ; return this ; }
2014-11-18 09:49:11 +01:00
// The flag function described when true.
2014-10-18 18:30:13 +02:00
// Example: "Monsters can spawn in this territory."
private String descYes = " defaultDescYes " ;
public String getDescYes ( ) { return this . descYes ; }
public MFlag setDescYes ( String descYes ) { this . descYes = descYes ; this . changed ( ) ; return this ; }
2014-11-18 09:49:11 +01:00
// The flag function described when false.
2014-10-18 18:30:13 +02:00
// Example: "Monsters can NOT spawn in this territory."
private String descNo = " defaultDescNo " ;
public String getDescNo ( ) { return this . descNo ; }
public MFlag setDescNo ( String descNo ) { this . descNo = descNo ; this . changed ( ) ; return this ; }
2014-11-18 09:49:11 +01:00
// What is the standard (aka default) flag value?
// This value will be set for factions from the beginning.
2014-10-02 11:45:06 +02:00
// Example: false (per default monsters do not spawn in faction territory)
private boolean standard = true ;
public boolean isStandard ( ) { return this . standard ; }
public MFlag setStandard ( boolean standard ) { this . standard = standard ; this . changed ( ) ; return this ; }
2014-11-18 09:49:11 +01:00
// Is this flag editable by players?
// With this we mean standard non administrator players.
// All flags can be changed using /f admin.
2014-10-02 11:45:06 +02:00
// Example: true (if players want to turn mob spawning on I guess they should be able to)
private boolean editable = false ;
public boolean isEditable ( ) { return this . editable ; }
public MFlag setEditable ( boolean editable ) { this . editable = editable ; this . changed ( ) ; return this ; }
2014-11-18 09:49:11 +01:00
// Is this flag visible to players?
// With this we mean standard non administrator players.
// All flags can be seen using /f admin.
// Some flags can be rendered meaningless by settings in Factions or external plugins.
// Say we set "editable" to false and "standard" to true for the "open" flag to force all factions being open.
// In such case we might want to hide the open flag by setting "visible" false.
// If it can't be changed, why bother showing it?
2014-10-02 11:45:06 +02:00
// Example: true (yeah we need to see this flag)
private boolean visible = true ;
public boolean isVisible ( ) { return this . visible ; }
public MFlag setVisible ( boolean visible ) { this . visible = visible ; this . changed ( ) ; return this ; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public MFlag ( )
{
// No argument constructor for GSON
}
2014-10-18 18:30:13 +02:00
public MFlag ( int priority , String name , String desc , String descYes , String descNo , boolean standard , boolean editable , boolean visible )
2014-10-02 11:45:06 +02:00
{
this . priority = priority ;
this . name = name ;
this . desc = desc ;
2014-10-18 18:30:13 +02:00
this . descYes = descYes ;
this . descNo = descNo ;
2014-10-02 11:45:06 +02:00
this . standard = standard ;
this . editable = editable ;
this . visible = visible ;
}
// -------------------------------------------- //
// EXTRAS
// -------------------------------------------- //
2014-10-15 14:35:50 +02:00
public boolean isInteresting ( boolean value )
{
if ( ! this . isVisible ( ) ) return false ;
if ( this . isEditable ( ) ) return true ;
return this . isStandard ( ) ! = value ;
}
2014-10-18 18:30:13 +02:00
public String getStateDesc ( boolean value , boolean withValue , boolean monospaceValue , boolean withName , boolean withDesc , boolean specificDesc )
2014-10-02 11:45:06 +02:00
{
2014-11-07 08:38:26 +01:00
List < String > parts = new MassiveList < String > ( ) ;
2014-10-02 11:45:06 +02:00
2014-10-18 18:30:13 +02:00
if ( withValue )
2014-10-02 11:45:06 +02:00
{
2014-10-18 18:30:13 +02:00
if ( monospaceValue )
{
parts . add ( Txt . parse ( value ? " <g>YES " : " <b>NOO " ) ) ;
}
else
{
parts . add ( Txt . parse ( value ? " <g>YES " : " <b>NO " ) ) ;
}
2014-10-02 11:45:06 +02:00
}
2014-10-18 18:30:13 +02:00
if ( withName )
2014-10-02 11:45:06 +02:00
{
2014-10-18 18:30:13 +02:00
String nameFormat ;
if ( ! this . isVisible ( ) )
{
nameFormat = " <silver>%s " ;
}
else if ( this . isEditable ( ) )
{
nameFormat = " <pink>%s " ;
}
else
{
nameFormat = " <aqua>%s " ;
}
String name = this . getName ( ) ;
String nameDesc = Txt . parse ( nameFormat , name ) ;
parts . add ( nameDesc ) ;
2014-10-02 11:45:06 +02:00
}
2014-10-18 18:30:13 +02:00
if ( withDesc )
{
String desc ;
if ( specificDesc )
{
desc = value ? this . getDescYes ( ) : this . getDescNo ( ) ;
}
else
{
desc = this . getDesc ( ) ;
}
String descDesc = Txt . parse ( " <i>%s " , desc ) ;
parts . add ( descDesc ) ;
}
2014-10-02 11:45:06 +02:00
2014-10-18 18:30:13 +02:00
return Txt . implode ( parts , " " ) ;
}
@Deprecated
public String getStateInfo ( boolean value , boolean withDesc )
{
return this . getStateDesc ( value , true , true , true , true , false ) ;
2014-10-02 11:45:06 +02:00
}
}