-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleSkills.java
More file actions
248 lines (223 loc) · 12.5 KB
/
SimpleSkills.java
File metadata and controls
248 lines (223 loc) · 12.5 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package dansplugins.simpleskills;
import dansplugins.simpleskills.bstats.Metrics;
import dansplugins.simpleskills.commands.*;
import dansplugins.simpleskills.commands.tab.TabCommand;
import dansplugins.simpleskills.listeners.PlayerJoinEventListener;
import dansplugins.simpleskills.playerrecord.PlayerRecordRepository;
import dansplugins.simpleskills.config.ConfigService;
import dansplugins.simpleskills.message.MessageService;
import dansplugins.simpleskills.services.StorageService;
import dansplugins.simpleskills.skill.SkillRepository;
import dansplugins.simpleskills.skill.abs.AbstractSkill;
import dansplugins.simpleskills.chance.ChanceCalculator;
import dansplugins.simpleskills.experience.ExperienceCalculator;
import dansplugins.simpleskills.logging.Log;
import dansplugins.simpleskills.skill.skills.*;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.jetbrains.annotations.NotNull;
import preponderous.ponder.minecraft.bukkit.PonderMC;
import preponderous.ponder.minecraft.bukkit.abs.AbstractPluginCommand;
import preponderous.ponder.minecraft.bukkit.abs.PonderBukkitPlugin;
import preponderous.ponder.minecraft.bukkit.nms.NMSAssistant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
/**
* @author Daniel Stephenson
*/
public class SimpleSkills extends PonderBukkitPlugin {
private final String pluginVersion = "v" + getDescription().getVersion();
private PonderMC ponder;
private final ConfigService configService = new ConfigService(this);
private final Log log = new Log(this, configService);
private final MessageService messageService = new MessageService(this);
private final ExperienceCalculator experienceCalculator = new ExperienceCalculator();
private final SkillRepository skillRepository = new SkillRepository();
private final PlayerRecordRepository playerRecordRepository = new PlayerRecordRepository(log, messageService, skillRepository, configService, experienceCalculator);
private final StorageService storageService = new StorageService(playerRecordRepository, skillRepository, messageService, configService, experienceCalculator, log);
private final ChanceCalculator chanceCalculator = new ChanceCalculator(playerRecordRepository, configService, skillRepository, messageService, experienceCalculator, log);
private final PlayerJoinEventListener playerJoinEventListener = new PlayerJoinEventListener(playerRecordRepository, log);
private Timer autosaveTimer;
/**
* This runs when the server starts.
*/
@Override
public void onEnable() {
this.ponder = new PonderMC(this);
configService.createConfig();
performNMSChecks();
setTabCompleterForCoreCommands();
setupMetrics();
log.debug("Loading files.");
storageService.load();
log.debug("Creating language files.");
messageService.createlang();
initializeSkills();
registerEventListeners();
initializeCommandService();
checkFilesVersion();
startAutosave();
}
/**
* This runs when the server stops.
*/
@Override
public void onDisable() {
stopAutosave();
log.debug("Saving files.");
storageService.save();
log.debug("Saving language files.");
messageService.savelang();
log.debug("Saving config files.");
configService.saveConfig();
}
/**
* This method handles commands sent to the minecraft server and interprets them if the label matches one of the core commands.
*
* @param sender The sender of the command.
* @param cmd The command that was sent. This is unused.
* @param label The core command that has been invoked.
* @param args Arguments of the core command. Often sub-commands.
* @return A boolean indicating whether the execution of the command was successful.
*/
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
if (args.length == 0) {
DefaultCommand defaultCommand = new DefaultCommand(messageService, this);
return defaultCommand.execute(sender);
}
return ponder.getCommandService().interpretAndExecuteCommand(sender, label, args);
}
public String getVersion() {
return pluginVersion;
}
private void checkFilesVersion() {
log.debug("Checking config and message files for version compatibility.");
if (messageService.getlang().getDouble("message-version") != 0.2) {
log.error("Outdated message.yml! Please backup & update message.yml file and restart server again!!");
}
if (configService.getConfig().getDouble("config-version") != 0.1) {
log.error("Outdated config.yml! Please backup & update config.yml file and restart server again!!");
}
}
private void performNMSChecks() {
try {
final NMSAssistant nmsAssistant = new NMSAssistant();
if (nmsAssistant.isVersionGreaterThan(12)) {
log.info("Loading data for NMS " + nmsAssistant.getNMSVersion().toString());
} else {
log.warning("The server version is not suitable to load the plugin");
log.warning("This plugin is tested on a 1.21.4 server.");
Bukkit.getServer().getPluginManager().disablePlugin(this);
}
} catch(NumberFormatException e) {
log.warning("Failed to determine NMS version due to NumberFormatException. Some features may not work correctly.");
} catch (Exception e) {
log.warning("Failed to determine NMS version due to an exception. Some features may not work correctly. Error: " + e.getMessage());
}
}
private void setupMetrics() {
log.debug("Setting up bStats metrics for SimpleSkills.");
int pluginId = 13470;
Metrics metrics = new Metrics(this, pluginId);
double configVersion = configService.getConfig().getDouble("config-version");
int defaultMaxLevel = configService.getConfig().getInt("defaultMaxLevel");
int defaultBaseExperienceRequirement = configService.getConfig().getInt("defaultBaseExperienceRequirement");
double defaultExperienceIncreaseFactor = configService.getConfig().getDouble("defaultExperienceIncreaseFactor");
boolean levelUpAlert = configService.getConfig().getBoolean("levelUpAlert");
boolean benefitAlert = configService.getConfig().getBoolean("benefitAlert");
metrics.addCustomChart(new Metrics.SimplePie("config_version", () -> String.valueOf(configVersion)));
metrics.addCustomChart(new Metrics.SimplePie("default_max_level", () -> String.valueOf(defaultMaxLevel)));
metrics.addCustomChart(new Metrics.SimplePie("default_base_experience_requirement", () -> String.valueOf(defaultBaseExperienceRequirement)));
metrics.addCustomChart(new Metrics.SimplePie("default_experience_increase_factor", () -> String.valueOf(defaultExperienceIncreaseFactor)));
metrics.addCustomChart(new Metrics.SimplePie("level_up_alert", () -> String.valueOf(levelUpAlert)));
metrics.addCustomChart(new Metrics.SimplePie("benefit_alert", () -> String.valueOf(benefitAlert)));
}
private void setTabCompleterForCoreCommands() {
log.debug("Setting up tab completers for core commands.");
for (String key : getDescription().getCommands().keySet()) {
PluginCommand command = getCommand(key);
if (command == null) {
continue;
}
command.setTabCompleter(new TabCommand());
}
}
private void registerEventListeners() {
log.debug("Registering events...");
for (AbstractSkill skill : skillRepository.getSkills()) {
log.debug("Registering events for skill: " + skill.getName());
skill.register();
}
Bukkit.getPluginManager().registerEvents(playerJoinEventListener, this);
}
private void initializeCommandService() {
log.debug("Initializing command service...");
ArrayList<AbstractPluginCommand> commands = new ArrayList<>(Arrays.asList(
new HelpCommand(messageService),
new InfoCommand(playerRecordRepository, messageService, skillRepository, configService, experienceCalculator, log),
new StatsCommand(messageService, playerRecordRepository, skillRepository),
new ForceCommand(playerRecordRepository, skillRepository),
new SkillCommand(messageService, skillRepository),
new TopCommand(playerRecordRepository, messageService, skillRepository),
new ReloadCommand(messageService, configService)
));
ponder.getCommandService().initialize(commands, "That command wasn't found.");
}
private void initializeSkills() {
log.debug("Initializing skills...");
skillRepository.addSkill(new Athlete(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Boating(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Breeding(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Cardio(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Crafting(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Digging(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Dueling(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Enchanting(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Farming(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Fishing(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Floriculture(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Gliding(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Hardiness(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Woodcutting(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Mining(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new MonsterHunting(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Pyromaniac(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Quarrying(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Riding(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
skillRepository.addSkill(new Strength(configService, log, playerRecordRepository, this, messageService, chanceCalculator));
}
/**
* Starts the autosave scheduler which saves data every 5 minutes.
*/
private void startAutosave() {
log.debug("Starting autosave task.");
autosaveTimer = new Timer(true);
autosaveTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
if (!isEnabled()) {
autosaveTimer.cancel();
return;
}
Bukkit.getScheduler().runTask(SimpleSkills.this, () -> {
log.debug("Autosaving files.");
storageService.save();
});
}
}, 1000 * 60 * 5, 1000 * 60 * 5);
}
/**
* Stops the autosave scheduler.
*/
private void stopAutosave() {
if (autosaveTimer != null) {
log.debug("Stopping autosave task.");
autosaveTimer.cancel();
autosaveTimer = null;
}
}
}