2019-02-27 02:50:18 +01:00
package com.songoda.ultimatemoderation ;
import com.songoda.ultimatemoderation.command.CommandManager ;
2019-02-27 22:45:39 +01:00
import com.songoda.ultimatemoderation.listeners.* ;
2019-03-05 06:06:02 +01:00
import com.songoda.ultimatemoderation.punish.AppliedPunishment ;
import com.songoda.ultimatemoderation.punish.PunishmentNote ;
import com.songoda.ultimatemoderation.punish.PunishmentType ;
import com.songoda.ultimatemoderation.punish.player.PlayerPunishData ;
2019-03-03 22:33:44 +01:00
import com.songoda.ultimatemoderation.punish.player.PunishmentManager ;
2019-03-05 06:06:02 +01:00
import com.songoda.ultimatemoderation.punish.template.Template ;
2019-03-03 22:33:44 +01:00
import com.songoda.ultimatemoderation.punish.template.TemplateManager ;
2019-03-08 17:24:47 +01:00
import com.songoda.ultimatemoderation.staffchat.StaffChatManager ;
2019-03-05 06:06:02 +01:00
import com.songoda.ultimatemoderation.storage.Storage ;
import com.songoda.ultimatemoderation.storage.StorageRow ;
import com.songoda.ultimatemoderation.storage.types.StorageMysql ;
import com.songoda.ultimatemoderation.storage.types.StorageYaml ;
2019-03-07 05:31:30 +01:00
import com.songoda.ultimatemoderation.tickets.Ticket ;
import com.songoda.ultimatemoderation.tickets.TicketManager ;
import com.songoda.ultimatemoderation.tickets.TicketResponse ;
import com.songoda.ultimatemoderation.tickets.TicketStatus ;
2019-02-27 02:50:18 +01:00
import com.songoda.ultimatemoderation.utils.Methods ;
2019-03-09 18:58:44 +01:00
import com.songoda.ultimatemoderation.utils.Metrics ;
2019-02-27 02:50:18 +01:00
import com.songoda.ultimatemoderation.utils.SettingsManager ;
2019-03-05 06:06:02 +01:00
import com.songoda.ultimatemoderation.utils.gui.AbstractGUI ;
2019-02-27 02:50:18 +01:00
import org.bukkit.Bukkit ;
import org.bukkit.ChatColor ;
import org.bukkit.command.CommandSender ;
import org.bukkit.plugin.java.JavaPlugin ;
2019-03-05 06:06:02 +01:00
import java.util.UUID ;
2019-02-27 02:50:18 +01:00
public class UltimateModeration extends JavaPlugin {
private static CommandSender console = Bukkit . getConsoleSender ( ) ;
private static UltimateModeration INSTANCE ;
private References references ;
2019-03-03 22:33:44 +01:00
2019-03-07 05:31:30 +01:00
private TicketManager ticketManager ;
2019-03-03 22:33:44 +01:00
private TemplateManager templateManager ;
2019-02-27 02:50:18 +01:00
private SettingsManager settingsManager ;
private CommandManager commandManager ;
2019-03-03 22:33:44 +01:00
private PunishmentManager punishmentManager ;
2019-03-08 17:24:47 +01:00
private StaffChatManager staffChatManager ;
2019-03-03 22:33:44 +01:00
2019-02-27 02:50:18 +01:00
private Locale locale ;
2019-03-05 06:06:02 +01:00
private Storage storage ;
2019-02-27 02:50:18 +01:00
public static UltimateModeration getInstance ( ) {
return INSTANCE ;
}
private boolean checkVersion ( ) {
int workingVersion = 13 ;
int currentVersion = Integer . parseInt ( Bukkit . getServer ( ) . getClass ( )
. getPackage ( ) . getName ( ) . split ( " \\ . " ) [ 3 ] . split ( " _ " ) [ 1 ] ) ;
if ( currentVersion < workingVersion ) {
Bukkit . getScheduler ( ) . scheduleSyncDelayedTask ( this , ( ) - > {
Bukkit . getConsoleSender ( ) . sendMessage ( " " ) ;
Bukkit . getConsoleSender ( ) . sendMessage ( String . format ( " %sYou installed the 1.%s only version of %s on a 1.%s server. Since you are on the wrong version we disabled the plugin for you. Please install correct version to continue using %s. " , ChatColor . RED , workingVersion , this . getDescription ( ) . getName ( ) , currentVersion , this . getDescription ( ) . getName ( ) ) ) ;
Bukkit . getConsoleSender ( ) . sendMessage ( " " ) ;
} , 20L ) ;
return false ;
}
return true ;
}
@Override
public void onEnable ( ) {
INSTANCE = this ;
// Check to make sure the Bukkit version is compatible.
if ( ! checkVersion ( ) ) return ;
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
console . sendMessage ( Methods . formatText ( " &7UltimateModeration " + this . getDescription ( ) . getVersion ( ) + " by &5Songoda <3! " ) ) ;
console . sendMessage ( Methods . formatText ( " &7Action: &aEnabling&7... " ) ) ;
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
this . settingsManager = new SettingsManager ( this ) ;
this . setupConfig ( ) ;
// Setup language
String langMode = SettingsManager . Setting . LANGUGE_MODE . getString ( ) ;
Locale . init ( this ) ;
Locale . saveDefaultLocale ( " en_US " ) ;
this . locale = Locale . getLocale ( getConfig ( ) . getString ( " System.Language Mode " , langMode ) ) ;
this . references = new References ( ) ;
2019-03-05 06:06:02 +01:00
// Setup Managers
2019-03-07 05:31:30 +01:00
this . ticketManager = new TicketManager ( ) ;
2019-03-03 22:33:44 +01:00
this . templateManager = new TemplateManager ( ) ;
2019-02-27 02:50:18 +01:00
this . commandManager = new CommandManager ( this ) ;
2019-03-03 22:33:44 +01:00
this . punishmentManager = new PunishmentManager ( ) ;
2019-03-08 17:24:47 +01:00
this . staffChatManager = new StaffChatManager ( ) ;
2019-03-03 22:33:44 +01:00
2019-03-05 06:06:02 +01:00
// Load data
this . checkStorage ( ) ;
2019-03-09 04:55:02 +01:00
Bukkit . getScheduler ( ) . scheduleSyncDelayedTask ( this , this : : loadFromFile , 1L ) ;
2019-03-05 06:06:02 +01:00
2019-02-27 04:08:19 +01:00
// Register Listeners
2019-03-05 06:06:02 +01:00
AbstractGUI . initializeListeners ( this ) ;
2019-02-28 23:26:36 +01:00
Bukkit . getPluginManager ( ) . registerEvents ( new CommandListener ( this ) , this ) ;
2019-02-28 20:06:37 +01:00
Bukkit . getPluginManager ( ) . registerEvents ( new DeathListener ( this ) , this ) ;
2019-02-27 22:45:39 +01:00
Bukkit . getPluginManager ( ) . registerEvents ( new MoveListener ( this ) , this ) ;
Bukkit . getPluginManager ( ) . registerEvents ( new DropListener ( this ) , this ) ;
Bukkit . getPluginManager ( ) . registerEvents ( new InventoryListener ( this ) , this ) ;
2019-02-27 04:08:19 +01:00
Bukkit . getPluginManager ( ) . registerEvents ( new ChatListener ( this ) , this ) ;
Bukkit . getPluginManager ( ) . registerEvents ( new LoginListener ( this ) , this ) ;
2019-02-27 02:50:18 +01:00
2019-03-09 18:58:44 +01:00
// Starting Metrics
new Metrics ( this ) ;
2019-03-05 06:06:02 +01:00
int timeout = SettingsManager . Setting . AUTOSAVE . getInt ( ) * 60 * 20 ;
Bukkit . getScheduler ( ) . runTaskTimerAsynchronously ( this , ( ) - > storage . doSave ( ) , timeout , timeout ) ;
2019-02-27 02:50:18 +01:00
}
@Override
public void onDisable ( ) {
2019-03-05 06:06:02 +01:00
storage . doSave ( ) ;
this . storage . closeConnection ( ) ;
2019-02-27 02:50:18 +01:00
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
console . sendMessage ( Methods . formatText ( " &7UltimateModeration " + this . getDescription ( ) . getVersion ( ) + " by &5Songoda <3! " ) ) ;
console . sendMessage ( Methods . formatText ( " &7Action: &cDisabling&7... " ) ) ;
console . sendMessage ( Methods . formatText ( " &a============================= " ) ) ;
}
2019-03-05 06:06:02 +01:00
private void checkStorage ( ) {
if ( getConfig ( ) . getBoolean ( " Database.Activate Mysql Support " ) ) {
this . storage = new StorageMysql ( this ) ;
} else {
this . storage = new StorageYaml ( this ) ;
}
}
private void loadFromFile ( ) {
if ( storage . containsGroup ( " templates " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " templates " ) ) {
Template template = new Template ( PunishmentType . valueOf ( row . get ( " type " ) . asString ( ) ) ,
row . get ( " duration " ) . asLong ( ) ,
row . get ( " reason " ) . asString ( ) ,
UUID . fromString ( row . get ( " creator " ) . asString ( ) ) ,
row . get ( " name " ) . asString ( ) ,
UUID . fromString ( row . get ( " uuid " ) . asString ( ) ) ) ;
templateManager . addTemplate ( template ) ;
}
}
if ( storage . containsGroup ( " punishments " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " punishments " ) ) {
UUID playerUUID = UUID . fromString ( row . get ( " victim " ) . asString ( ) ) ;
AppliedPunishment appliedPunishment = new AppliedPunishment ( PunishmentType . valueOf ( row . get ( " type " ) . asString ( ) ) ,
row . get ( " duration " ) . asLong ( ) ,
row . get ( " reason " ) . asString ( ) ,
UUID . fromString ( row . get ( " victim " ) . asString ( ) ) ,
UUID . fromString ( row . get ( " punisher " ) . asString ( ) ) ,
row . get ( " expiration " ) . asLong ( ) ,
playerUUID ) ;
PlayerPunishData playerPunishData = getPunishmentManager ( ) . getPlayer ( playerUUID ) ;
playerPunishData . addPunishment ( appliedPunishment ) ;
playerPunishData . audit ( ) ;
}
}
if ( storage . containsGroup ( " notes " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " notes " ) ) {
UUID playerUUID = UUID . fromString ( row . get ( " subject " ) . asString ( ) ) ;
PunishmentNote note = new PunishmentNote ( UUID . fromString ( row . get ( " uuid " ) . asString ( ) ) ,
row . get ( " note " ) . asString ( ) ,
UUID . fromString ( row . get ( " author " ) . asString ( ) ) ,
UUID . fromString ( row . get ( " subject " ) . asString ( ) ) ,
row . get ( " creation " ) . asLong ( ) ) ;
PlayerPunishData playerPunishData = getPunishmentManager ( ) . getPlayer ( playerUUID ) ;
playerPunishData . addNotes ( note ) ;
}
}
2019-03-07 05:31:30 +01:00
if ( storage . containsGroup ( " tickets " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " tickets " ) ) {
int id = row . get ( " id " ) . asInt ( ) ;
Ticket ticket = new Ticket (
UUID . fromString ( row . get ( " player " ) . asString ( ) ) ,
2019-03-09 02:12:34 +01:00
row . get ( " subject " ) . asString ( ) ,
row . get ( " type " ) . asString ( ) ) ;
2019-03-07 05:31:30 +01:00
ticket . setTicketId ( id ) ;
2019-03-09 02:12:34 +01:00
ticket . setLocation ( Methods . unserializeLocation ( row . get ( " location " ) . asString ( ) ) ) ;
2019-03-07 05:31:30 +01:00
ticket . setStatus ( TicketStatus . valueOf ( row . get ( " status " ) . asString ( ) ) ) ;
ticketManager . addTicket ( ticket , id ) ;
}
}
if ( storage . containsGroup ( " ticketresponses " ) ) {
for ( StorageRow row : storage . getRowsByGroup ( " ticketresponses " ) ) {
int id = row . get ( " ticketid " ) . asInt ( ) ;
TicketResponse ticketResponse = new TicketResponse (
UUID . fromString ( row . get ( " author " ) . asString ( ) ) ,
row . get ( " message " ) . asString ( ) ,
row . get ( " posted " ) . asLong ( ) ) ;
ticketResponse . setTicketId ( id ) ;
ticketManager . getTicket ( id ) . addResponse ( ticketResponse ) ;
}
}
2019-03-05 06:06:02 +01:00
storage . doSave ( ) ;
}
2019-02-27 02:50:18 +01:00
private void setupConfig ( ) {
settingsManager . updateSettings ( ) ;
this . getConfig ( ) . options ( ) . copyDefaults ( true ) ;
this . saveConfig ( ) ;
}
public void reload ( ) {
locale . reloadMessages ( ) ;
references = new References ( ) ;
this . setupConfig ( ) ;
saveConfig ( ) ;
}
public CommandManager getCommandManager ( ) {
return commandManager ;
}
public SettingsManager getSettingsManager ( ) {
return settingsManager ;
}
public Locale getLocale ( ) {
return locale ;
}
public References getReferences ( ) {
return references ;
}
2019-03-03 22:33:44 +01:00
public TemplateManager getTemplateManager ( ) {
return templateManager ;
}
2019-02-27 02:50:18 +01:00
2019-03-03 22:33:44 +01:00
public PunishmentManager getPunishmentManager ( ) {
return punishmentManager ;
}
2019-03-07 05:31:30 +01:00
public TicketManager getTicketManager ( ) {
return ticketManager ;
}
2019-03-08 17:24:47 +01:00
public StaffChatManager getStaffChatManager ( ) {
return staffChatManager ;
}
2019-02-27 02:50:18 +01:00
}