2013-12-06 16:05:11 -06:00
package com.graywolf336.jail ;
import java.io.File ;
2013-12-06 21:47:03 -06:00
import java.io.IOException ;
2013-12-07 14:16:16 -06:00
import java.util.Set ;
2013-12-06 16:05:11 -06:00
2013-12-09 14:28:38 -06:00
import org.bukkit.Location ;
2013-12-06 16:05:11 -06:00
import org.bukkit.configuration.file.FileConfiguration ;
import org.bukkit.configuration.file.YamlConfiguration ;
2013-12-09 14:28:38 -06:00
import com.graywolf336.jail.beans.Cell ;
2013-12-06 21:47:03 -06:00
import com.graywolf336.jail.beans.Jail ;
2013-12-09 14:28:38 -06:00
import com.graywolf336.jail.beans.Prisoner ;
2013-12-07 14:16:16 -06:00
import com.graywolf336.jail.beans.SimpleLocation ;
2013-12-23 14:31:27 -06:00
import com.graywolf336.jail.enums.LangString ;
2013-12-06 21:47:03 -06:00
2013-12-16 12:26:38 -06:00
/ * *
* Handles all the saving and loading of the plugin ' s data .
*
* @author graywolf336
2013-12-28 19:50:55 -06:00
* @since 2 . x . x
* @version 3 . 0 . 0
2013-12-16 12:26:38 -06:00
*
* /
2013-12-06 16:05:11 -06:00
public class JailIO {
private JailMain pl ;
2013-12-23 14:31:27 -06:00
private FileConfiguration flat , lang ;
2013-12-06 16:05:11 -06:00
private int storage ; //0 = flatfile, 1 = sqlite, 2 = mysql
public JailIO ( JailMain plugin ) {
this . pl = plugin ;
String st = pl . getConfig ( ) . getString ( " storage.type " , " flatfile " ) ;
if ( st . equalsIgnoreCase ( " sqlite " ) ) {
storage = 1 ;
} else if ( st . equalsIgnoreCase ( " mysql " ) ) {
storage = 2 ;
} else {
storage = 0 ;
}
}
2013-12-23 14:31:27 -06:00
/** Loads the language file from disk, if there is none then we save the default one. */
public void loadLanguage ( ) {
String language = pl . getConfig ( ) . getString ( " system.language " ) ;
boolean save = false ;
File langFile = new File ( pl . getDataFolder ( ) , language + " .yml " ) ;
//File or folder already exists, let's check
if ( langFile . exists ( ) ) {
if ( langFile . isFile ( ) ) {
lang = YamlConfiguration . loadConfiguration ( langFile ) ;
2013-12-23 14:51:04 -06:00
pl . getLogger ( ) . info ( " Loaded the language: " + language ) ;
2013-12-23 14:31:27 -06:00
} else {
2014-01-14 18:28:02 -06:00
pl . getLogger ( ) . severe ( " The language file can not be a folder. " ) ;
pl . getLogger ( ) . severe ( " As a result, we are reverting back to English as the language. " ) ;
2013-12-23 14:31:27 -06:00
lang = YamlConfiguration . loadConfiguration ( pl . getResource ( " en.yml " ) ) ;
save = true ;
}
} else {
2014-01-14 18:28:02 -06:00
pl . getLogger ( ) . warning ( " Loading the default language of: en " ) ;
pl . getLogger ( ) . warning ( " If you wish to change this, please rename 'en.yml' to whatever you wish and set the config value to the name of the file. " ) ;
2013-12-23 14:31:27 -06:00
lang = YamlConfiguration . loadConfiguration ( pl . getResource ( " en.yml " ) ) ;
save = true ;
}
//If we have flagged to save the language file, let's save it as en.yml as this flag usually means they didn't have it loaded.
if ( save ) {
try {
lang . save ( new File ( pl . getDataFolder ( ) , " en.yml " ) ) ;
} catch ( IOException e ) {
pl . getLogger ( ) . severe ( " Unable to save the language file: " + e . getMessage ( ) ) ;
}
}
}
/** Returns the message in the language, no variables are replaced.*/
public String getLanguageString ( LangString langString ) {
return getLanguageString ( langString , new String [ ] { } ) ;
}
2014-02-04 13:30:12 -06:00
/** Returns the message in the language, no variables are replaced.*/
public String getLanguageString ( LangString langString , LangString langString2 ) {
return getLanguageString ( langString , getLanguageString ( langString2 , new String [ ] { } ) ) ;
}
2013-12-23 14:31:27 -06:00
/ * *
* Returns the message in the language , with the provided variables being replaced .
*
* @param langString Which { @link LangString } we should be getting to send .
* @param variables All the variables to replace , in order from 0 to however many .
2013-12-25 22:41:01 -06:00
* @return The message as a colorful message or an empty message if that isn ' t defined in the language file .
2013-12-23 14:31:27 -06:00
* /
public String getLanguageString ( LangString langString , String . . . variables ) {
2014-01-01 18:13:31 -06:00
String message = lang . getString ( " language. " + langString . getSection ( ) + " . " + langString . getName ( ) ) ;
2013-12-23 14:31:27 -06:00
2013-12-25 22:41:01 -06:00
if ( message = = null ) return " " ;
2013-12-23 14:31:27 -06:00
for ( int i = 0 ; i < variables . length ; i + + ) {
message = message . replaceAll ( " % " + i + " % " , variables [ i ] ) ;
}
return Util . getColorfulMessage ( message ) ;
}
2013-12-16 12:26:38 -06:00
/ * *
* Prepares the storage engine to be used .
* /
2013-12-06 16:05:11 -06:00
public void prepareStorage ( ) {
switch ( storage ) {
case 1 :
//prepare sqlite, I need to research this
break ;
case 2 :
//prepare mysql, research this as well
break ;
default :
flat = YamlConfiguration . loadConfiguration ( new File ( pl . getDataFolder ( ) , " data.yml " ) ) ;
break ;
}
}
2013-12-16 12:26:38 -06:00
/ * *
* Loads the jails , this should < strong > only < / strong > be called after { @link # prepareStorage ( ) } .
* /
2013-12-06 16:05:11 -06:00
public void loadJails ( ) {
switch ( storage ) {
case 1 :
//load the jails from sqlite
break ;
case 2 :
//load the jails from mysql
break ;
default :
//load the jails from flatfile
2013-12-07 14:16:16 -06:00
if ( flat . isConfigurationSection ( " jails " ) ) {
Set < String > jails = flat . getConfigurationSection ( " jails " ) . getKeys ( false ) ;
if ( ! jails . isEmpty ( ) ) {
for ( String name : jails ) {
loadJail ( name ) ;
}
}
}
2013-12-06 16:05:11 -06:00
break ;
}
2013-12-07 14:16:16 -06:00
int s = pl . getJailManager ( ) . getJails ( ) . size ( ) ;
pl . getLogger ( ) . info ( " Loaded " + s + ( s = = 1 ? " jail. " : " jails. " ) ) ;
2013-12-06 16:05:11 -06:00
}
2013-12-06 21:47:03 -06:00
2013-12-16 12:26:38 -06:00
/ * *
* Saves the provided { @link Jail jail } to the storage system we are using .
*
* @param j The jail to save .
* /
2013-12-06 21:47:03 -06:00
public void saveJail ( Jail j ) {
switch ( storage ) {
case 1 :
case 2 :
break ;
default :
if ( flat ! = null ) {
2013-12-07 13:02:38 -06:00
String node = " jails. " + j . getName ( ) + " . " ;
2013-12-06 21:56:00 -06:00
//Corners
2013-12-07 14:16:16 -06:00
flat . set ( node + " world " , j . getWorldName ( ) ) ;
2013-12-06 21:47:03 -06:00
flat . set ( node + " top.x " , j . getMaxPoint ( ) . getBlockX ( ) ) ;
flat . set ( node + " top.y " , j . getMaxPoint ( ) . getBlockY ( ) ) ;
flat . set ( node + " top.z " , j . getMaxPoint ( ) . getBlockZ ( ) ) ;
flat . set ( node + " bottom.x " , j . getMinPoint ( ) . getBlockX ( ) ) ;
flat . set ( node + " bottom.y " , j . getMinPoint ( ) . getBlockY ( ) ) ;
flat . set ( node + " bottom.z " , j . getMinPoint ( ) . getBlockZ ( ) ) ;
2013-12-06 21:56:00 -06:00
//Tele in
flat . set ( node + " tps.in.x " , j . getTeleportIn ( ) . getX ( ) ) ;
flat . set ( node + " tps.in.y " , j . getTeleportIn ( ) . getY ( ) ) ;
flat . set ( node + " tps.in.z " , j . getTeleportIn ( ) . getZ ( ) ) ;
flat . set ( node + " tps.in.yaw " , j . getTeleportIn ( ) . getYaw ( ) ) ;
2013-12-07 14:16:16 -06:00
flat . set ( node + " tps.in.pitch " , j . getTeleportIn ( ) . getPitch ( ) ) ;
2013-12-06 21:56:00 -06:00
//Tele out
2013-12-07 14:16:16 -06:00
flat . set ( node + " tps.free.world " , j . getTeleportFree ( ) . getWorld ( ) . getName ( ) ) ;
2013-12-06 21:56:00 -06:00
flat . set ( node + " tps.free.x " , j . getTeleportFree ( ) . getX ( ) ) ;
flat . set ( node + " tps.free.y " , j . getTeleportFree ( ) . getY ( ) ) ;
flat . set ( node + " tps.free.z " , j . getTeleportFree ( ) . getZ ( ) ) ;
flat . set ( node + " tps.free.yaw " , j . getTeleportFree ( ) . getYaw ( ) ) ;
2013-12-07 14:16:16 -06:00
flat . set ( node + " tps.free.pitch " , j . getTeleportFree ( ) . getPitch ( ) ) ;
2013-12-06 21:56:00 -06:00
2014-01-21 16:14:26 -06:00
//Set all the cells to nothing, then we save each of them so no cells are left behind
flat . set ( node + " .cells " , null ) ;
2013-12-09 14:28:38 -06:00
for ( Cell c : j . getCells ( ) ) {
String cNode = node + " .cells. " + c . getName ( ) + " . " ;
if ( c . getTeleport ( ) ! = null ) {
flat . set ( cNode + " tp.x " , c . getTeleport ( ) . getX ( ) ) ;
flat . set ( cNode + " tp.y " , c . getTeleport ( ) . getY ( ) ) ;
flat . set ( cNode + " tp.z " , c . getTeleport ( ) . getZ ( ) ) ;
flat . set ( cNode + " tp.yaw " , c . getTeleport ( ) . getYaw ( ) ) ;
flat . set ( cNode + " tp.pitch " , c . getTeleport ( ) . getPitch ( ) ) ;
}
if ( c . getChestLocation ( ) ! = null ) {
flat . set ( cNode + " chest.x " , c . getChestLocation ( ) . getBlockX ( ) ) ;
flat . set ( cNode + " chest.y " , c . getChestLocation ( ) . getBlockY ( ) ) ;
flat . set ( cNode + " chest.z " , c . getChestLocation ( ) . getBlockZ ( ) ) ;
}
2013-12-11 17:36:17 -06:00
String [ ] signs = new String [ c . getSigns ( ) . size ( ) ] ;
int count = 0 ;
for ( SimpleLocation loc : c . getSigns ( ) ) {
signs [ count ] = loc . toString ( ) ;
count + + ;
}
flat . set ( cNode + " signs " , signs ) ;
2013-12-09 14:28:38 -06:00
if ( c . getPrisoner ( ) ! = null ) {
Prisoner p = c . getPrisoner ( ) ;
flat . set ( cNode + " prisoner.name " , p . getName ( ) ) ;
flat . set ( cNode + " prisoner.muted " , p . isMuted ( ) ) ;
flat . set ( cNode + " prisoner.time " , p . getRemainingTime ( ) ) ;
2013-12-24 23:42:53 -06:00
flat . set ( cNode + " prisoner.offlinePending " , p . isOfflinePending ( ) ) ;
2014-02-04 13:30:12 -06:00
flat . set ( cNode + " prisoner.toBeTransferred " , p . isToBeTransferred ( ) ) ;
2014-01-21 19:56:14 -06:00
flat . set ( cNode + " prisoner.jailer " , p . getJailer ( ) ) ;
2013-12-25 21:56:01 -06:00
flat . set ( cNode + " prisoner.reason " , p . getReason ( ) ) ;
2013-12-27 18:19:47 -06:00
flat . set ( cNode + " prisoner.inventory " , p . getInventory ( ) ) ;
flat . set ( cNode + " prisoner.armor " , p . getArmor ( ) ) ;
2013-12-25 23:06:00 -06:00
if ( p . getPreviousLocationString ( ) ! = null )
flat . set ( cNode + " prisoner.previousLocation " , p . getPreviousLocationString ( ) ) ;
2013-12-25 23:01:56 -06:00
if ( p . getPreviousGameMode ( ) ! = null )
flat . set ( cNode + " prisoner.previousGameMode " , p . getPreviousGameMode ( ) . toString ( ) ) ;
2013-12-09 14:28:38 -06:00
}
}
2014-01-21 16:14:26 -06:00
//Null all the prisoners out before we save them again, this way no prisoners are left behind
flat . set ( node + " prisoners " , null ) ;
2013-12-24 23:42:53 -06:00
for ( Prisoner p : j . getPrisonersNotInCells ( ) ) {
String pNode = node + " prisoners. " + p . getName ( ) + " . " ;
flat . set ( pNode + " muted " , p . isMuted ( ) ) ;
flat . set ( pNode + " time " , p . getRemainingTime ( ) ) ;
flat . set ( pNode + " offlinePending " , p . isOfflinePending ( ) ) ;
2014-02-04 13:30:12 -06:00
flat . set ( pNode + " toBeTransferred " , p . isToBeTransferred ( ) ) ;
2014-01-21 19:56:14 -06:00
flat . set ( pNode + " jailer " , p . getJailer ( ) ) ;
2013-12-25 21:56:01 -06:00
flat . set ( pNode + " reason " , p . getReason ( ) ) ;
2013-12-27 18:19:47 -06:00
flat . set ( pNode + " inventory " , p . getInventory ( ) ) ;
flat . set ( pNode + " armor " , p . getArmor ( ) ) ;
2013-12-25 23:06:00 -06:00
if ( p . getPreviousLocationString ( ) ! = null )
flat . set ( pNode + " previousLocation " , p . getPreviousLocationString ( ) ) ;
2013-12-25 23:01:56 -06:00
if ( p . getPreviousGameMode ( ) ! = null )
flat . set ( pNode + " previousGameMode " , p . getPreviousGameMode ( ) . toString ( ) ) ;
2013-12-24 23:42:53 -06:00
}
2013-12-06 21:47:03 -06:00
try {
flat . save ( new File ( pl . getDataFolder ( ) , " data.yml " ) ) ;
} catch ( IOException e ) {
pl . getLogger ( ) . severe ( " Unable to save the Jail data: " + e . getMessage ( ) ) ;
}
} else {
pl . getLogger ( ) . severe ( " Storage not enabled, could not save the jail " + j . getName ( ) ) ;
}
break ;
}
}
2013-12-07 14:16:16 -06:00
private void loadJail ( String name ) {
switch ( storage ) {
case 1 :
case 2 :
break ;
default :
String node = " jails. " + name + " . " ;
2013-12-09 14:28:38 -06:00
String cNode = node + " cells. " ;
2013-12-07 14:16:16 -06:00
Jail j = new Jail ( pl , name ) ;
2013-12-16 12:36:14 -06:00
j . setWorld ( flat . getString ( node + " world " ) ) ;
2013-12-07 14:16:16 -06:00
j . setMaxPoint ( new int [ ] { flat . getInt ( node + " top.x " ) , flat . getInt ( node + " top.y " ) , flat . getInt ( node + " top.z " ) } ) ;
j . setMinPoint ( new int [ ] { flat . getInt ( node + " bottom.x " ) , flat . getInt ( node + " bottom.y " ) , flat . getInt ( node + " bottom.z " ) } ) ;
j . setTeleportIn ( new SimpleLocation (
flat . getString ( node + " world " ) ,
flat . getDouble ( node + " tps.in.x " ) ,
flat . getDouble ( node + " tps.in.y " ) ,
flat . getDouble ( node + " tps.in.z " ) ,
( float ) flat . getDouble ( node + " tps.in.yaw " ) ,
( float ) flat . getDouble ( node + " tps.in.pitch " ) ) ) ;
j . setTeleportFree ( new SimpleLocation (
flat . getString ( node + " tps.free.world " ) ,
flat . getDouble ( node + " tps.free.x " ) ,
flat . getDouble ( node + " tps.free.y " ) ,
flat . getDouble ( node + " tps.free.z " ) ,
( float ) flat . getDouble ( node + " tps.free.yaw " ) ,
( float ) flat . getDouble ( node + " tps.free.pitch " ) ) ) ;
2013-12-09 14:28:38 -06:00
if ( flat . isConfigurationSection ( node + " cells " ) ) {
Set < String > cells = flat . getConfigurationSection ( node + " cells " ) . getKeys ( false ) ;
if ( ! cells . isEmpty ( ) ) {
for ( String cell : cells ) {
Cell c = new Cell ( cell ) ;
2013-12-28 12:12:57 -06:00
String cellNode = cNode + cell + " . " ;
2013-12-09 14:28:38 -06:00
c . setTeleport ( new SimpleLocation ( j . getTeleportIn ( ) . getWorld ( ) . getName ( ) ,
flat . getDouble ( cellNode + " tp.x " ) ,
flat . getDouble ( cellNode + " tp.y " ) ,
flat . getDouble ( cellNode + " tp.z " ) ,
( float ) flat . getDouble ( cellNode + " tp.yaw " ) ,
( float ) flat . getDouble ( cellNode + " tp.pitch " ) ) ) ;
c . setChestLocation ( new Location ( j . getTeleportIn ( ) . getWorld ( ) ,
flat . getInt ( cellNode + " chest.x " ) ,
flat . getInt ( cellNode + " chest.y " ) ,
2013-12-28 13:20:09 -06:00
flat . getInt ( cellNode + " chest.z " ) ) ) ;
2013-12-09 14:28:38 -06:00
for ( String sign : flat . getStringList ( cellNode + " signs " ) ) {
String [ ] arr = sign . split ( " , " ) ;
c . addSign ( new SimpleLocation ( arr [ 0 ] ,
Double . valueOf ( arr [ 1 ] ) ,
Double . valueOf ( arr [ 2 ] ) ,
Double . valueOf ( arr [ 3 ] ) ,
Float . valueOf ( arr [ 4 ] ) ,
Float . valueOf ( arr [ 5 ] ) ) ) ;
}
if ( flat . contains ( cellNode + " prisoner " ) ) {
2013-12-25 21:56:01 -06:00
Prisoner p = new Prisoner ( flat . getString ( cellNode + " prisoner.name " ) ,
flat . getBoolean ( cellNode + " prisoner.muted " ) ,
flat . getLong ( cellNode + " prisoner.time " ) ,
2014-01-21 19:56:14 -06:00
flat . getString ( cellNode + " prisoner.jailer " ) ,
2013-12-25 21:56:01 -06:00
flat . getString ( cellNode + " prisoner.reason " ) ) ;
2013-12-24 23:42:53 -06:00
p . setOfflinePending ( flat . getBoolean ( cellNode + " prisoner.offlinePending " ) ) ;
2014-02-04 13:30:12 -06:00
p . setToBeTransferred ( flat . getBoolean ( cellNode + " prisoner.toBeTransferred " ) ) ;
2013-12-25 21:56:01 -06:00
p . setPreviousPosition ( flat . getString ( cellNode + " prisoner.previousLocation " ) ) ;
2013-12-25 22:57:09 -06:00
p . setPreviousGameMode ( flat . getString ( cellNode + " prisoner.previousGameMode " ) ) ;
2013-12-27 18:19:47 -06:00
p . setInventory ( flat . getString ( cellNode + " prisoner.inventory " , " " ) ) ;
2013-12-27 18:22:13 -06:00
p . setArmor ( flat . getString ( cellNode + " prisoner.armor " , " " ) ) ;
2013-12-24 23:42:53 -06:00
c . setPrisoner ( p ) ;
2013-12-09 14:28:38 -06:00
}
2013-12-09 16:35:10 -06:00
j . addCell ( c , false ) ;
2013-12-09 14:28:38 -06:00
}
}
}
2013-12-24 23:42:53 -06:00
if ( flat . isConfigurationSection ( node + " prisoners " ) ) {
Set < String > prisoners = flat . getConfigurationSection ( node + " prisoners " ) . getKeys ( false ) ;
if ( ! prisoners . isEmpty ( ) ) {
for ( String prisoner : prisoners ) {
String pNode = node + " prisoners. " + prisoner + " . " ;
2014-01-21 19:56:14 -06:00
Prisoner pris = new Prisoner ( prisoner ,
flat . getBoolean ( pNode + " muted " ) ,
flat . getLong ( pNode + " time " ) ,
flat . getString ( pNode + " jailer " ) ,
flat . getString ( pNode + " reason " ) ) ;
2013-12-24 23:42:53 -06:00
pris . setOfflinePending ( flat . getBoolean ( pNode + " offlinePending " ) ) ;
2014-02-04 13:30:12 -06:00
pris . setToBeTransferred ( flat . getBoolean ( pNode + " toBeTransferred " ) ) ;
2013-12-25 21:56:01 -06:00
pris . setPreviousPosition ( flat . getString ( pNode + " previousLocation " ) ) ;
2013-12-25 22:57:09 -06:00
pris . setPreviousGameMode ( flat . getString ( pNode + " previousGameMode " ) ) ;
2013-12-27 18:19:47 -06:00
pris . setInventory ( flat . getString ( pNode + " inventory " , " " ) ) ;
2013-12-27 18:22:13 -06:00
pris . setArmor ( flat . getString ( pNode + " armor " , " " ) ) ;
2013-12-24 23:42:53 -06:00
j . addPrisoner ( pris ) ;
}
}
}
if ( pl . getServer ( ) . getWorld ( j . getWorldName ( ) ) ! = null ) {
pl . getJailManager ( ) . addJail ( j , false ) ;
2013-12-28 12:01:56 -06:00
pl . getLogger ( ) . info ( " Loaded jail " + j . getName ( ) + " with " + j . getAllPrisoners ( ) . size ( ) + " prisoners and " + j . getCellCount ( ) + " cells. " ) ;
2013-12-24 23:42:53 -06:00
} else
pl . getLogger ( ) . severe ( " Failed to load the jail " + j . getName ( ) + " as the world ' " + j . getWorldName ( ) + " ' does not exist (is null). Did you remove this world? " ) ;
2013-12-07 14:16:16 -06:00
break ;
}
}
2013-12-28 15:39:14 -06:00
/ * *
* Removes the prisoner from the storage system .
*
* @param j the jail which the prisoner is in .
* @param p the prisoner data
* /
public void removePrisoner ( Jail j , Prisoner p ) {
this . removePrisoner ( j , null , p ) ;
}
/ * *
* Removes the prisoner from the storage system .
*
* @param j the jail which the prisoner is in .
* @param c the cell which the prisoner is in , null if none
* @param p the prisoner data
* /
public void removePrisoner ( Jail j , Cell c , Prisoner p ) {
switch ( storage ) {
case 1 :
case 2 :
break ;
default :
if ( c = = null )
flat . set ( " jails. " + j . getName ( ) + " .prisoners. " + p . getName ( ) , null ) ;
else
flat . set ( " jails. " + j . getName ( ) + " . " + c . getName ( ) + " .prisoner " , null ) ;
2013-12-28 15:42:06 -06:00
try {
flat . save ( new File ( pl . getDataFolder ( ) , " data.yml " ) ) ;
} catch ( IOException e ) {
pl . getLogger ( ) . severe ( " Unable to save the Jail data: " + e . getMessage ( ) ) ;
}
2013-12-28 15:39:14 -06:00
break ;
}
}
2014-02-13 11:26:42 -06:00
/ * *
* Removes a jail from the storage system .
*
* @param name of the jail to remove .
* /
public void removeJail ( String name ) {
switch ( storage ) {
case 1 :
case 2 :
break ;
default :
flat . set ( " jails. " + name , null ) ;
try {
flat . save ( new File ( pl . getDataFolder ( ) , " data.yml " ) ) ;
} catch ( IOException e ) {
pl . getLogger ( ) . severe ( " Unable to remove the jail " + name + " from the storage: " + e . getMessage ( ) ) ;
}
break ;
}
}
2013-12-06 16:05:11 -06:00
}