|
1 | 1 | package me.morpheus.dtpunishment;
|
2 | 2 |
|
| 3 | +import me.morpheus.dtpunishment.configuration.*; |
| 4 | +import me.morpheus.dtpunishment.data.ChatOffenceData; |
| 5 | +import me.morpheus.dtpunishment.data.DataStore; |
| 6 | +import me.morpheus.dtpunishment.data.FileDataStore; |
| 7 | +import me.morpheus.dtpunishment.data.MySqlDataStore; |
| 8 | +import ninja.leaping.configurate.objectmapping.GuiceObjectMapperFactory; |
3 | 9 | import org.slf4j.Logger;
|
4 | 10 | import org.spongepowered.api.Sponge;
|
| 11 | +import org.spongepowered.api.config.ConfigDir; |
5 | 12 | import org.spongepowered.api.event.Listener;
|
| 13 | +import org.spongepowered.api.event.game.GameReloadEvent; |
6 | 14 | import org.spongepowered.api.event.game.state.GameInitializationEvent;
|
7 | 15 | import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
|
8 | 16 | import org.spongepowered.api.plugin.Plugin;
|
|
11 | 19 | import com.google.inject.Injector;
|
12 | 20 |
|
13 | 21 | import me.morpheus.dtpunishment.commands.CommandManager;
|
14 |
| -import me.morpheus.dtpunishment.configuration.ConfigurationManager; |
15 |
| -import me.morpheus.dtpunishment.configuration.MainConfig; |
16 |
| -import me.morpheus.dtpunishment.configuration.PunishmentLength; |
17 |
| -import me.morpheus.dtpunishment.configuration.PunishmentLengthSerializer; |
18 | 22 | import me.morpheus.dtpunishment.listeners.PlayerListener;
|
19 | 23 | import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
|
20 | 24 |
|
| 25 | +import java.io.File; |
| 26 | +import java.nio.file.Path; |
| 27 | + |
21 | 28 | @Plugin(id = "dtpunishment", name = "DTPunishment", description = "Dragon Tech Punishment: a unique chat safeguard plugin")
|
22 | 29 | public class DTPunishment {
|
23 | 30 |
|
24 |
| - private Logger logger; |
| 31 | + @Inject |
| 32 | + public Logger logger; |
25 | 33 |
|
26 |
| - private Injector injector; |
| 34 | + public static Logger getLogger() { |
| 35 | + return instance.logger; |
| 36 | + } |
27 | 37 |
|
28 |
| - private Injector childInjector; |
| 38 | + @Inject |
| 39 | + @ConfigDir(sharedRoot = false) |
| 40 | + public File configDir; |
29 | 41 |
|
30 |
| - private MainConfig config; |
| 42 | + public File dataDir; |
31 | 43 |
|
32 |
| - private CommandManager commandManager; |
| 44 | + @Inject |
| 45 | + public GuiceObjectMapperFactory mapper; |
33 | 46 |
|
34 |
| - @Inject |
35 |
| - public DTPunishment(Logger logger, Injector injector, MainConfig mainConfig) { |
36 |
| - this.logger = logger; |
37 |
| - this.injector = injector; |
38 |
| - this.config = mainConfig; |
39 |
| - registerSerializers(); |
40 |
| - } |
| 47 | + public DataStore dataStore; |
41 | 48 |
|
42 |
| - private void registerSerializers() { |
43 |
| - TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(PunishmentLength.class), |
44 |
| - new PunishmentLengthSerializer()); |
45 |
| - } |
| 49 | + private static DTPunishment instance; |
| 50 | + |
| 51 | + private ConfigManager<ChatConfig> chatConfig; |
| 52 | + private ConfigManager<MainConfig> mainConfig; |
| 53 | + private ConfigManager<Messages> messages; |
46 | 54 |
|
47 | 55 | @Listener
|
48 | 56 | public void onServerPreInit(GamePreInitializationEvent event) {
|
49 |
| - logger.info("Enabling DTPunishment..."); |
| 57 | + instance = this; |
| 58 | + |
| 59 | + TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(PunishmentLength.class), |
| 60 | + new PunishmentLengthSerializer()); |
| 61 | + |
| 62 | + this.chatConfig = new ConfigManager<>(ChatConfig.class, "chat.conf", configDir, mapper); |
| 63 | + this.mainConfig = new ConfigManager<>(MainConfig.class, "DTPunishments.conf", configDir, mapper); |
| 64 | + this.messages = new ConfigManager<>(Messages.class, "Messages.conf", configDir, mapper); |
| 65 | + |
| 66 | + File file; |
| 67 | + if (getConfig().useCustomDataDirectory && !getConfig().customDataDirectory.isEmpty()) { |
| 68 | + file = new File(new File(getConfig().customDataDirectory), "data"); |
| 69 | + } else { |
| 70 | + file = new File(this.configDir, "data"); |
| 71 | + } |
| 72 | + if (!file.exists()) file.mkdirs(); |
| 73 | + this.dataDir = file; |
| 74 | + |
| 75 | + if (this.mainConfig.getConfig().database.enabled) { |
| 76 | + this.dataStore = new MySqlDataStore(); |
| 77 | + } else { |
| 78 | + this.dataStore = new FileDataStore(); |
| 79 | + } |
50 | 80 |
|
51 |
| - // Create the child injector for the plugin |
52 |
| - childInjector = injector.createChildInjector(new DTPunishmentModule(config)); |
53 |
| - |
54 |
| - // Get config manager and init |
55 |
| - ConfigurationManager configurationManager = childInjector.getInstance(ConfigurationManager.class); |
56 |
| - configurationManager.intialise(); |
| 81 | + logger.info("Enabling DTPunishment..."); |
57 | 82 |
|
58 | 83 | // Get command manager from child injector
|
59 |
| - commandManager = childInjector.getInstance(CommandManager.class); |
| 84 | + commandManager = new CommandManager(); |
| 85 | + wordChecker = new WordChecker(); |
| 86 | + this.offenceData = new ChatOffenceData(); |
60 | 87 | }
|
61 | 88 |
|
62 | 89 | @Listener
|
63 | 90 | public void onServerInit(GameInitializationEvent event) {
|
64 | 91 | logger.info("Registering listeners and commands...");
|
65 |
| - Sponge.getEventManager().registerListeners(this, childInjector.getInstance(PlayerListener.class)); |
| 92 | + Sponge.getEventManager().registerListeners(this, new PlayerListener()); |
66 | 93 |
|
67 | 94 | // Register any commands
|
68 | 95 | commandManager.registerCommands();
|
69 | 96 | }
|
70 | 97 |
|
| 98 | + @Listener |
| 99 | + public void onReload(GameReloadEvent e) { |
| 100 | + this.mainConfig.load(); |
| 101 | + this.chatConfig.load(); |
| 102 | + } |
| 103 | + |
| 104 | + public static DTPunishment getInstance() { |
| 105 | + return instance; |
| 106 | + } |
| 107 | + |
| 108 | + public static MainConfig getConfig() { |
| 109 | + return instance.mainConfig.getConfig(); |
| 110 | + } |
| 111 | + |
| 112 | + public static Messages getMessages() { |
| 113 | + return instance.messages.getConfig(); |
| 114 | + } |
| 115 | + |
| 116 | + public static ChatConfig getChatConfig() { |
| 117 | + return instance.chatConfig.getConfig(); |
| 118 | + } |
| 119 | + |
| 120 | + public static DataStore getDataStore() { |
| 121 | + return instance.dataStore; |
| 122 | + } |
| 123 | + |
| 124 | + private CommandManager commandManager; |
| 125 | + public static CommandManager getCommandManager() { |
| 126 | + return instance.commandManager; |
| 127 | + } |
| 128 | + |
| 129 | + private WordChecker wordChecker; |
| 130 | + public static WordChecker getWordChecker() { |
| 131 | + return instance.wordChecker; |
| 132 | + } |
| 133 | + |
| 134 | + private ChatOffenceData offenceData; |
| 135 | + public static ChatOffenceData getOffenceData() { |
| 136 | + return instance.offenceData; |
| 137 | + } |
| 138 | + |
| 139 | + public static File getDataDirectory() { |
| 140 | + return instance.dataDir; |
| 141 | + } |
| 142 | + |
71 | 143 | }
|
0 commit comments