2011-10-08 22:03:44 +02:00
package com.massivecraft.factions.zcore ;
import java.lang.reflect.Modifier ;
import java.lang.reflect.Type ;
import java.util.* ;
2011-10-10 13:31:25 +02:00
import java.util.Map.Entry ;
2011-10-08 22:03:44 +02:00
import java.util.logging.Level ;
import org.bukkit.Bukkit ;
import org.bukkit.command.CommandSender ;
import org.bukkit.plugin.java.JavaPlugin ;
2012-08-26 08:02:21 +02:00
import org.bukkit.craftbukkit.libs.com.google.gson.Gson ;
import org.bukkit.craftbukkit.libs.com.google.gson.GsonBuilder ;
import org.bukkit.craftbukkit.libs.com.google.gson.reflect.TypeToken ;
2011-10-08 22:03:44 +02:00
import com.massivecraft.factions.zcore.persist.EM ;
import com.massivecraft.factions.zcore.persist.SaveTask ;
import com.massivecraft.factions.zcore.util.LibLoader ;
import com.massivecraft.factions.zcore.util.PermUtil ;
import com.massivecraft.factions.zcore.util.Persist ;
import com.massivecraft.factions.zcore.util.TextUtil ;
public abstract class MPlugin extends JavaPlugin
{
// Some utils
public Persist persist ;
public TextUtil txt ;
public LibLoader lib ;
public PermUtil perm ;
// Persist related
public Gson gson ;
private Integer saveTask = null ;
2011-10-09 21:57:43 +02:00
private boolean autoSave = true ;
2012-03-09 21:44:18 +01:00
protected boolean loadSuccessful = false ;
2011-10-09 21:57:43 +02:00
public boolean getAutoSave ( ) { return this . autoSave ; }
public void setAutoSave ( boolean val ) { this . autoSave = val ; }
2012-03-11 17:41:56 +01:00
public String refCommand = " " ;
2011-10-08 22:03:44 +02:00
// Listeners
private MPluginSecretPlayerListener mPluginSecretPlayerListener ;
private MPluginSecretServerListener mPluginSecretServerListener ;
// Our stored base commands
private List < MCommand < ? > > baseCommands = new ArrayList < MCommand < ? > > ( ) ;
public List < MCommand < ? > > getBaseCommands ( ) { return this . baseCommands ; }
// -------------------------------------------- //
// ENABLE
// -------------------------------------------- //
private long timeEnableStart ;
public boolean preEnable ( )
{
log ( " === ENABLE START === " ) ;
timeEnableStart = System . currentTimeMillis ( ) ;
// Ensure basefolder exists!
this . getDataFolder ( ) . mkdirs ( ) ;
// Create Utility Instances
this . perm = new PermUtil ( this ) ;
this . persist = new Persist ( this ) ;
this . lib = new LibLoader ( this ) ;
2012-03-11 13:39:31 +01:00
// GSON 2.1 is now embedded in CraftBukkit, used by the auto-updater: https://github.com/Bukkit/CraftBukkit/commit/0ed1d1fdbb1e0bc09a70bc7bfdf40c1de8411665
// if ( ! lib.require("gson.jar", "http://search.maven.org/remotecontent?filepath=com/google/code/gson/gson/2.1/gson-2.1.jar")) return false;
2011-10-08 22:03:44 +02:00
this . gson = this . getGsonBuilder ( ) . create ( ) ;
2011-10-10 13:31:25 +02:00
this . txt = new TextUtil ( ) ;
2011-10-08 22:03:44 +02:00
initTXT ( ) ;
2012-03-11 17:41:56 +01:00
// attempt to get first command defined in plugin.yml as reference command, if any commands are defined in there
// reference command will be used to prevent "unknown command" console messages
try
{
Map < String , Map < String , Object > > refCmd = this . getDescription ( ) . getCommands ( ) ;
if ( refCmd ! = null & & ! refCmd . isEmpty ( ) )
this . refCommand = ( String ) ( refCmd . keySet ( ) . toArray ( ) [ 0 ] ) ;
}
catch ( ClassCastException ex ) { }
2011-10-08 22:03:44 +02:00
// Create and register listeners
this . mPluginSecretPlayerListener = new MPluginSecretPlayerListener ( this ) ;
this . mPluginSecretServerListener = new MPluginSecretServerListener ( this ) ;
2012-02-08 00:02:53 +01:00
getServer ( ) . getPluginManager ( ) . registerEvents ( this . mPluginSecretPlayerListener , this ) ;
getServer ( ) . getPluginManager ( ) . registerEvents ( this . mPluginSecretServerListener , this ) ;
2011-10-08 22:03:44 +02:00
// Register recurring tasks
long saveTicks = 20 * 60 * 30 ; // Approximately every 30 min
if ( saveTask = = null )
{
2011-10-09 18:35:39 +02:00
saveTask = Bukkit . getServer ( ) . getScheduler ( ) . scheduleSyncRepeatingTask ( this , new SaveTask ( this ) , saveTicks , saveTicks ) ;
2011-10-08 22:03:44 +02:00
}
2012-03-09 21:44:18 +01:00
loadSuccessful = true ;
2011-10-08 22:03:44 +02:00
return true ;
}
public void postEnable ( )
{
log ( " === ENABLE DONE (Took " + ( System . currentTimeMillis ( ) - timeEnableStart ) + " ms) === " ) ;
}
public void onDisable ( )
{
if ( saveTask ! = null )
{
this . getServer ( ) . getScheduler ( ) . cancelTask ( saveTask ) ;
saveTask = null ;
}
2012-03-09 21:44:18 +01:00
// only save data if plugin actually loaded successfully
if ( loadSuccessful )
EM . saveAllToDisc ( ) ;
2011-10-08 22:03:44 +02:00
log ( " Disabled " ) ;
}
public void suicide ( )
{
log ( " Now I suicide! " ) ;
this . getServer ( ) . getPluginManager ( ) . disablePlugin ( this ) ;
}
// -------------------------------------------- //
// Some inits...
// You are supposed to override these in the plugin if you aren't satisfied with the defaults
// The goal is that you always will be satisfied though.
// -------------------------------------------- //
public GsonBuilder getGsonBuilder ( )
{
return new GsonBuilder ( )
. setPrettyPrinting ( )
. disableHtmlEscaping ( )
. serializeNulls ( )
. excludeFieldsWithModifiers ( Modifier . TRANSIENT , Modifier . VOLATILE ) ;
}
// -------------------------------------------- //
// LANG AND TAGS
// -------------------------------------------- //
// These are not supposed to be used directly.
// They are loaded and used through the TextUtil instance for the plugin.
2011-10-10 13:31:25 +02:00
public Map < String , String > rawTags = new LinkedHashMap < String , String > ( ) ;
2011-10-08 22:03:44 +02:00
2011-10-10 13:31:25 +02:00
public void addRawTags ( )
2011-10-08 22:03:44 +02:00
{
2011-10-10 13:31:25 +02:00
this . rawTags . put ( " l " , " <green> " ) ; // logo
this . rawTags . put ( " a " , " <gold> " ) ; // art
this . rawTags . put ( " n " , " <silver> " ) ; // notice
this . rawTags . put ( " i " , " <yellow> " ) ; // info
this . rawTags . put ( " g " , " <lime> " ) ; // good
this . rawTags . put ( " b " , " <rose> " ) ; // bad
this . rawTags . put ( " h " , " <pink> " ) ; // highligh
this . rawTags . put ( " c " , " <aqua> " ) ; // command
this . rawTags . put ( " p " , " <teal> " ) ; // parameter
2011-10-08 22:03:44 +02:00
}
public void initTXT ( )
{
2011-10-10 13:31:25 +02:00
this . addRawTags ( ) ;
2011-10-08 22:03:44 +02:00
Type type = new TypeToken < Map < String , String > > ( ) { } . getType ( ) ;
Map < String , String > tagsFromFile = this . persist . load ( type , " tags " ) ;
2011-10-10 13:31:25 +02:00
if ( tagsFromFile ! = null ) this . rawTags . putAll ( tagsFromFile ) ;
this . persist . save ( this . rawTags , " tags " ) ;
2011-10-08 22:03:44 +02:00
2011-10-10 13:31:25 +02:00
for ( Entry < String , String > rawTag : this . rawTags . entrySet ( ) )
{
this . txt . tags . put ( r awTag . getKey ( ) , TextUtil . parseColor ( rawTag . getValue ( ) ) ) ;
}
2011-10-08 22:03:44 +02:00
}
// -------------------------------------------- //
// COMMAND HANDLING
// -------------------------------------------- //
2012-03-10 02:26:42 +01:00
// can be overridden by P method, to provide option
public boolean logPlayerCommands ( )
{
return true ;
}
2011-10-09 20:10:19 +02:00
public boolean handleCommand ( CommandSender sender , String commandString , boolean testOnly )
2011-10-08 22:03:44 +02:00
{
2011-10-14 15:03:23 +02:00
boolean noSlash = true ;
2011-10-08 22:03:44 +02:00
if ( commandString . startsWith ( " / " ) )
{
2011-10-14 15:03:23 +02:00
noSlash = false ;
2011-10-08 22:03:44 +02:00
commandString = commandString . substring ( 1 ) ;
}
for ( MCommand < ? > command : this . getBaseCommands ( ) )
{
if ( noSlash & & ! command . allowNoSlashAccess ) continue ;
for ( String alias : command . aliases )
{
2012-03-19 14:24:01 +01:00
// disallow double-space after alias, so specific commands can be prevented (preventing "f home" won't prevent "f home")
if ( commandString . startsWith ( alias + " " ) ) return false ;
2011-10-10 01:21:05 +02:00
if ( commandString . startsWith ( alias + " " ) | | commandString . equals ( alias ) )
2011-10-08 22:03:44 +02:00
{
List < String > args = new ArrayList < String > ( Arrays . asList ( commandString . split ( " \\ s+ " ) ) ) ;
args . remove ( 0 ) ;
2011-10-09 20:10:19 +02:00
if ( testOnly ) return true ;
2011-10-08 22:03:44 +02:00
command . execute ( sender , args ) ;
return true ;
}
}
}
return false ;
}
2011-10-09 20:10:19 +02:00
public boolean handleCommand ( CommandSender sender , String commandString )
{
return this . handleCommand ( sender , commandString , false ) ;
}
2011-10-09 18:35:39 +02:00
// -------------------------------------------- //
// HOOKS
// -------------------------------------------- //
2011-10-09 21:57:43 +02:00
public void preAutoSave ( )
2011-10-09 18:35:39 +02:00
{
}
2011-10-09 21:57:43 +02:00
public void postAutoSave ( )
2011-10-09 18:35:39 +02:00
{
}
2011-10-08 22:03:44 +02:00
// -------------------------------------------- //
// LOGGING
// -------------------------------------------- //
public void log ( Object msg )
{
log ( Level . INFO , msg ) ;
}
2012-02-26 19:29:46 +01:00
public void log ( String str , Object . . . args )
{
log ( Level . INFO , this . txt . parse ( str , args ) ) ;
}
public void log ( Level level , String str , Object . . . args )
{
log ( level , this . txt . parse ( str , args ) ) ;
}
2011-10-08 22:03:44 +02:00
public void log ( Level level , Object msg )
{
2012-03-10 04:24:58 +01:00
Bukkit . getLogger ( ) . log ( level , " [ " + this . getDescription ( ) . getFullName ( ) + " ] " + msg ) ;
2011-10-08 22:03:44 +02:00
}
}