-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDemocracyPost.java
More file actions
196 lines (180 loc) · 7.53 KB
/
Copy pathDemocracyPost.java
File metadata and controls
196 lines (180 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package io.github.md5sha256.democracypost;
import io.github.md5sha256.democracypost.command.PostCommand;
import io.github.md5sha256.democracypost.database.DatabaseAdapter;
import io.github.md5sha256.democracypost.heads.HeadDatabaseListener;
import io.github.md5sha256.democracypost.localization.MessageContainer;
import io.github.md5sha256.democracypost.model.PostalPackageFactory;
import io.github.md5sha256.democracypost.model.SimplePostalPackageFactory;
import io.github.md5sha256.democracypost.serializer.Serializers;
import io.github.md5sha256.democracypost.ui.PostOfficeMenu;
import io.github.md5sha256.democracypost.ui.UiItemFactory;
import io.papermc.paper.util.Tick;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.yaml.NodeStyle;
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.sql.SQLException;
import java.time.Duration;
import java.util.Optional;
public final class DemocracyPost extends JavaPlugin {
private DatabaseAdapter databaseAdapter;
private PostalPackageFactory postalPackageFactory;
private PostOfficeMenu postOfficeMenu;
private MessageContainer messageContainer;
private EssentialsMailService mailService;
private Settings settings;
private UiItemFactory itemFactory;
@Override
public void onLoad() {
try {
initDataFolder();
this.messageContainer = loadMessages();
this.settings = loadSettings();
this.databaseAdapter = initDatabase();
} catch (IOException ex) {
ex.printStackTrace();
getLogger().severe("Failed to initialize!");
getServer().getPluginManager().disablePlugin(this);
}
}
@Override
public void onEnable() {
if (!isEnabled()) {
return;
}
if (this.databaseAdapter == null) {
getLogger().severe("Database was not initialized (onLoad failed). Plugin will not enable.");
getServer().getPluginManager().disablePlugin(this);
return;
}
if (getServer().getPluginManager().getPlugin("Vault") == null) {
getLogger().severe("Missing Vault!");
getServer().getPluginManager().disablePlugin(this);
return;
}
Optional<Economy> economy = getEconomy();
if (economy.isEmpty()) {
getLogger().severe("Missing economy provider!");
getServer().getPluginManager().disablePlugin(this);
return;
}
this.mailService = new EssentialsMailService(this.messageContainer);
this.postalPackageFactory = initPostalPackageFactory();
this.itemFactory = new UiItemFactory(this.settings.uiSettings());
this.postOfficeMenu = new PostOfficeMenu(
this,
this.databaseAdapter,
this.postalPackageFactory,
this.messageContainer,
this.itemFactory,
this.settings.postSettings(),
economy.get()
);
if (getServer().getPluginManager().isPluginEnabled("HeadDatabase")) {
// Listen for the database load event to re-cache the heads in the item factory
getServer().getPluginManager().registerEvents(new HeadDatabaseListener(this.itemFactory), this);
}
// Plugin startup logic
int saveDurationTicks = Tick.tick().fromDuration(this.settings.savePeriodDuration());
Duration returnPackageExpiryDuration = this.settings.postSettings().returnPackageExpiryDuration();
getServer().getScheduler().runTaskTimerAsynchronously(
this,
() -> {
if (this.databaseAdapter == null) {
return;
}
getLogger().fine("Transferring expired packages...");
try {
this.databaseAdapter.transferExpiredPackages(returnPackageExpiryDuration);
} catch (SQLException ex) {
ex.printStackTrace();
}
getLogger().fine("Changes saved!");
},
0,
saveDurationTicks
);
new PostCommand(this, this.postOfficeMenu);
}
private PostalPackageFactory initPostalPackageFactory() {
PostSettings postSettings = this.settings.postSettings();
return new SimplePostalPackageFactory(
this,
this.mailService,
this.databaseAdapter,
postSettings.packageExpiryDuration(),
postSettings.returnPackageExpiryDuration()
);
}
private ConfigurationNode copyDefaultsYaml(@Nonnull String resourceName) throws IOException {
String fileName = resourceName + ".yml";
YamlConfigurationLoader defaultLoader = yamlLoader()
.source(() -> {
Reader reader = getTextResource(fileName);
if (reader == null) {
throw new IllegalStateException("Could not find text resource: " + fileName);
}
return new BufferedReader(reader);
})
.build();
YamlConfigurationLoader existingLoader = yamlLoader()
.file(new File(getDataFolder(), fileName))
.build();
ConfigurationNode defaults = defaultLoader.load();
ConfigurationNode existing = existingLoader.load();
existing.mergeFrom(defaults);
existingLoader.save(existing);
return existing;
}
private YamlConfigurationLoader.Builder yamlLoader() {
return YamlConfigurationLoader.builder()
.defaultOptions(options -> options.serializers(Serializers.defaults()))
.nodeStyle(NodeStyle.BLOCK);
}
@Nonnull
private MessageContainer loadMessages() throws IOException {
MessageContainer container = new MessageContainer();
ConfigurationNode existing = copyDefaultsYaml("en");
container.load(existing);
return container;
}
private Settings loadSettings() throws IOException {
ConfigurationNode settingsRoot = copyDefaultsYaml("settings");
return settingsRoot.get(Settings.class);
}
@Override
public void onDisable() {
// Plugin shutdown logic
if (this.databaseAdapter != null) {
this.databaseAdapter.close();
}
}
@Nonnull
private DatabaseAdapter initDatabase() throws IOException {
DatabaseAdapter adapter = new DatabaseAdapter(this.settings.databaseSettings(), getLogger(), this.settings.postSettings().skipUndeserializableItems());
try {
adapter.init();
} catch (SQLException ex) {
throw new IOException(ex);
}
return adapter;
}
private void initDataFolder() throws IOException {
File dataFolder = getDataFolder();
if (!dataFolder.isDirectory()) {
Files.createDirectory(dataFolder.toPath());
}
}
private Optional<Economy> getEconomy() {
RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
return Optional.ofNullable(rsp).map(RegisteredServiceProvider::getProvider);
}
}