-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAdHocCommandsManager.java
More file actions
149 lines (127 loc) · 5 KB
/
AdHocCommandsManager.java
File metadata and controls
149 lines (127 loc) · 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
package me.playbosswar.com.tasks;
import me.playbosswar.com.CommandTimerPlugin;
import me.playbosswar.com.enums.Gender;
import me.playbosswar.com.utils.CommandExecutor;
import me.playbosswar.com.utils.Files;
import me.playbosswar.com.utils.Messages;
import me.playbosswar.com.utils.gson.GsonConverter;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class AdHocCommandsManager {
private List<AdHocCommand> loadedCommands = new ArrayList<>();
private Thread runnerThread;
public boolean stopRunner = false;
public AdHocCommandsManager(Plugin plugin) {
loadedCommands.addAll(loadAdHocCommandsFromFiles());
startRunner();
}
public AdHocCommand scheduleCommand(String command, Gender gender, ZonedDateTime scheduledTime) {
AdHocCommand adHocCommand = new AdHocCommand(command, gender, scheduledTime);
loadedCommands.add(adHocCommand);
storeCommand(adHocCommand);
return adHocCommand;
}
@Nullable
public AdHocCommand getCommandById(UUID id) {
return loadedCommands.stream()
.filter(cmd -> cmd.getId().equals(id))
.findFirst()
.orElse(null);
}
public void removeCommand(AdHocCommand command) throws IOException {
loadedCommands.remove(command);
java.nio.file.Files.deleteIfExists(Paths.get(Files.getAdHocCommandFile(command.getId())));
}
public List<AdHocCommand> getLoadedCommands() {
return loadedCommands;
}
public List<AdHocCommand> getPendingCommands() {
return loadedCommands.stream()
.filter(cmd -> !cmd.isExecuted())
.collect(Collectors.toList());
}
private void startRunner() {
Runnable runner = new AdHocCommandRunner(this);
Thread thread = new Thread(runner);
thread.start();
this.runnerThread = thread;
}
public List<AdHocCommand> getCommandsToExecute() {
ZonedDateTime now = ZonedDateTime.now();
return loadedCommands.stream()
.filter(cmd -> !cmd.isExecuted() && cmd.getScheduledTime().toInstant().toEpochMilli() <= now.toInstant().toEpochMilli())
.collect(Collectors.toList());
}
public void processCommandExecution(AdHocCommand adHocCommand) {
CommandTimerPlugin.getScheduler().runTaskAsynchronously(() -> {
CommandExecutor.ExecutionContext context = new CommandExecutor.ExecutionContext.Builder()
.command(adHocCommand.getCommand())
.gender(adHocCommand.getGender())
.build();
CommandExecutor.execute(context);
adHocCommand.setExecuted(true);
storeCommand(adHocCommand);
});
}
private List<AdHocCommand> loadAdHocCommandsFromFiles() {
List<AdHocCommand> commands = new ArrayList<>();
File dir = new File(Files.getAdHocCommandsDirectory());
if (!dir.exists()) {
dir.mkdirs();
return commands;
}
File[] directoryListing = dir.listFiles(file -> file.getName().endsWith(".json"));
JSONParser jsonParser = new JSONParser();
GsonConverter gson = new GsonConverter();
if (directoryListing != null) {
for (File file : directoryListing) {
try {
FileReader fr = new FileReader(file.getPath());
AdHocCommand command = gson.fromJson(jsonParser.parse(fr).toString(), AdHocCommand.class);
if (command != null && !command.isExecuted()) {
commands.add(command);
}
} catch (IOException | ParseException e) {
Messages.sendConsole("Failed to load ad-hoc command from " + file.getName() + ": " + e.getMessage());
}
}
}
return commands;
}
private void storeCommand(AdHocCommand command) {
GsonConverter gson = new GsonConverter();
String json = gson.toJson(command);
try {
String path;
try {
path = Files.getAdHocCommandFile(command.getId());
} catch (IllegalStateException e) {
path = Files.getNewAdHocCommandFile(command.getId());
}
try (FileWriter jsonFile = new FileWriter(path)) {
jsonFile.write(json);
jsonFile.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void disable() {
stopRunner = true;
if (runnerThread != null && runnerThread.isAlive()) {
runnerThread.interrupt();
}
}
}