-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWildPets.java
More file actions
168 lines (149 loc) · 6.84 KB
/
WildPets.java
File metadata and controls
168 lines (149 loc) · 6.84 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
package dansplugins.wildpets;
import dansplugins.wildpets.bstats.Metrics;
import dansplugins.wildpets.commands.*;
import dansplugins.wildpets.data.EphemeralData;
import dansplugins.wildpets.pet.list.PetListRepository;
import dansplugins.wildpets.listeners.*;
import dansplugins.wildpets.config.ConfigService;
import dansplugins.wildpets.config.EntityConfigService;
import dansplugins.wildpets.pet.record.PetRecordRepository;
import dansplugins.wildpets.storage.StorageService;
import dansplugins.wildpets.scheduler.Scheduler;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
/**
* @author Daniel McCoy Stephenson
*/
public final class WildPets extends JavaPlugin {
private final String pluginVersion = "v" + getDescription().getVersion();
private final CommandService commandService = new CommandService(this);
private final EphemeralData ephemeralData = new EphemeralData();
private final EntityConfigService entityConfigService = new EntityConfigService(this);
private final ConfigService configService = new ConfigService(this, entityConfigService);
private final PetListRepository petListRepository = new PetListRepository(configService);
private final PetRecordRepository petRecordRepository = new PetRecordRepository();
private final StorageService storageService = new StorageService(configService, this, petListRepository, petRecordRepository);
private final Scheduler scheduler = new Scheduler(this, ephemeralData, storageService);
/**
* This runs when the server starts.
*/
@Override
public void onEnable() {
registerEventHandlers();
initializeCommandService();
initializeConfig();
scheduler.scheduleAutosave();
storageService.load();
handlebStatsIntegration();
}
private void initializeConfig() {
if (configFileExists()) {
performCompatibilityChecks();
}
else {
entityConfigService.initializeWithDefaults();
configService.saveMissingConfigDefaultsIfNotPresent();
}
}
private boolean configFileExists() {
return new File("./plugins/" + getName() + "/config.yml").exists();
}
private void performCompatibilityChecks() {
if (isVersionMismatched()) {
configService.saveMissingConfigDefaultsIfNotPresent();
}
reloadConfig();
entityConfigService.initializeWithConfig();
}
/**
* This runs when the server stops.
*/
@Override
public void onDisable() {
storageService.save();
}
/**
* 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.
*/
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (args.length == 0) {
DefaultCommand defaultCommand = new DefaultCommand(this);
return defaultCommand.execute(sender);
}
if (!sender.hasPermission("wp")) {
sender.sendMessage(ChatColor.RED + "You do not have permission to use Wild Pets.");
return false;
}
return commandService.interpretAndExecuteCommand(sender, label, args);
}
/**
* This can be used to get the version of the plugin.
* @return A string containing the version preceded by 'v'
*/
public String getVersion() {
return pluginVersion;
}
/**
* Checks if debug is enabled.
* @return Whether debug is enabled.
*/
public boolean isDebugEnabled() {
return configService.getBoolean("debugMode");
}
/**
* Checks if the version is mismatched.
* @return A boolean indicating if the version is mismatched.
*/
public boolean isVersionMismatched() {
String configVersion = this.getConfig().getString("version");
if (configVersion == null || this.getVersion() == null) {
return false;
} else {
return !configVersion.equalsIgnoreCase(this.getVersion());
}
}
private void handlebStatsIntegration() {
int pluginId = 12332;
new Metrics(this, pluginId);
}
/**
* Registers the event handlers of the plugin.
*/
private void registerEventHandlers() {
ArrayList<Listener> listeners = new ArrayList<>();
listeners.add(new DamageEffectsAndDeathHandler(configService, petListRepository, this));
listeners.add(new InteractionHandler(entityConfigService, petListRepository, petRecordRepository, ephemeralData, this, configService, scheduler));
listeners.add(new JoinAndQuitHandler(petListRepository, ephemeralData));
listeners.add(new MoveHandler(petListRepository));
listeners.add(new BreedEventHandler(petListRepository, petRecordRepository, configService, ephemeralData));
EventHandlerRegistry eventHandlerRegistry = new EventHandlerRegistry();
eventHandlerRegistry.registerEventHandlers(listeners, this);
}
/**
* Initializes the command service with the plugin's commands.
*/
private void initializeCommandService() {
ArrayList<AbstractPluginCommand> commands = new ArrayList<>(Arrays.asList(
new CallCommand(ephemeralData), new CheckAccessCommand(ephemeralData), new ConfigCommand(configService),
new FollowCommand(ephemeralData), new HelpCommand(configService),
new InfoCommand(ephemeralData, configService, petRecordRepository), new ListCommand(petListRepository), new LocateCommand(ephemeralData),
new LockCommand(ephemeralData), new RenameCommand(ephemeralData, petListRepository, petRecordRepository, configService),
new SelectCommand(configService, ephemeralData, petListRepository), new SetFreeCommand(ephemeralData, petListRepository), new StatsCommand(petListRepository),
new TameCommand(ephemeralData), new TradeCommand(ephemeralData, petListRepository, petRecordRepository), new UnlockCommand(ephemeralData), new WanderCommand(ephemeralData),
new StayCommand(ephemeralData), new GatherCommand(petListRepository)
));
commandService.initialize(commands, "That command wasn't found.");
}
}