-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathEntityDetection.java
More file actions
208 lines (177 loc) · 8.31 KB
/
EntityDetection.java
File metadata and controls
208 lines (177 loc) · 8.31 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
package de.themoep.entitydetection;
import com.tcoded.folialib.FoliaLib;
import com.tcoded.folialib.impl.PlatformScheduler;
import de.themoep.entitydetection.commands.ListSubCommand;
import de.themoep.entitydetection.commands.PluginCommandExecutor;
import de.themoep.entitydetection.commands.SearchSubCommand;
import de.themoep.entitydetection.commands.StopSubCommand;
import de.themoep.entitydetection.commands.TpSubCommand;
import de.themoep.entitydetection.searcher.EntitySearch;
import de.themoep.entitydetection.searcher.SearchResult;
import de.themoep.entitydetection.searcher.SearchResultEntry;
import de.themoep.entitydetection.searcher.SearchType;
import de.themoep.minedown.adventure.MineDown;
import de.themoep.minedown.adventure.Replacer;
import de.themoep.utils.lang.bukkit.LanguageManager;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Copyright 2016 Max Lee (https://github.com/Phoenix616/)
* <p/>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License as published by
* the Mozilla Foundation, version 2.
* <p/>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public License v2.0 for more details.
* <p/>
* You should have received a copy of the Mozilla Public License v2.0
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
*/
public class EntityDetection extends JavaPlugin {
private PlatformScheduler scheduler;
private LanguageManager lang;
private EntitySearch currentSearch;
private Map<SearchType, SearchResult<?>> results = new HashMap<>();
private Map<String, SearchResult<?>> customResults = new HashMap<>();
private Map<String, SearchResult<?>> lastResultViewed = new HashMap<>();
public PlatformScheduler getScheduler() {
return scheduler;
}
public void onEnable() {
FoliaLib foliaLib = new FoliaLib(this);
scheduler = foliaLib.getScheduler();
lang = new LanguageManager(this, System.getProperty("de.themoep.entitydetection.default-language", "en"));
PluginCommandExecutor cmdEx = new PluginCommandExecutor(this);
cmdEx.register(new SearchSubCommand(this));
cmdEx.register(new TpSubCommand(this));
cmdEx.register(new ListSubCommand(this));
cmdEx.register(new StopSubCommand(this));
}
public String getRawMessage(CommandSender sender, String key, String... replacements) {
return lang.getConfig(sender).get(key, replacements);
}
public Component getMessage(CommandSender sender, String key, String... replacements) {
return MineDown.parse(getRawMessage(sender, key, replacements));
}
public boolean startSearch(EntitySearch search) {
if (currentSearch != null && currentSearch.isRunning()) {
return false;
}
currentSearch = search;
search.start();
return true;
}
public boolean stopSearch(String stopper) {
if (currentSearch == null || !currentSearch.isRunning()) {
return false;
}
currentSearch.stop(stopper);
clearCurrentSearch();
return true;
}
public void addResult(SearchResult<?> result) {
if (result.getType() == SearchType.CUSTOM && result.getSearched().size() == 1) {
Set<String> searchedEntities = result.getSearched();
customResults.put(searchedEntities.toArray(new String[0])[0], result);
} else {
results.put(result.getType(), result);
}
}
public void clearCurrentSearch() {
currentSearch = null;
}
public EntitySearch getCurrentSearch() {
return currentSearch;
}
public void send(CommandSender sender, SearchResult<?> result) {
send(sender, result, "", 0);
}
public void send(CommandSender sender, SearchResult<?> result, String resultType, int page) {
lastResultViewed.put(sender.getName(), result);
String dateStr = new SimpleDateFormat(getRawMessage(sender, "result.time-format")).format(new Date(result.getEndTime()));
int start = page * 10;
Component searchedTypes = getMessage(sender, "result.searched-types.head");
Iterator<String> typeIter = result.getSearched().iterator();
while (typeIter.hasNext()) {
searchedTypes = searchedTypes.append(Component.newline())
.append(getMessage(sender, "result.searched-types.entry", "type", Utils.enumToHumanName(typeIter.next())));
}
Component message = getMessage(sender, "result.head", "type", Utils.enumToHumanName(result.getType()), "timestamp", dateStr, "duration", String.valueOf(result.getDuration()));
message = Replacer.replaceIn(message, "searchedtypes", searchedTypes);
List<? extends SearchResultEntry<?>> results = result.getSortedEntries();
if (results.size() > 0) {
for (int line = start; line < start + 10 && line < results.size(); line++) {
SearchResultEntry<?> entry = results.get(line);
Component resultLine = getMessage(sender, "result.entry",
"line", String.valueOf(line + 1),
"location", String.valueOf(entry.getLocation()),
"size", String.valueOf(entry.getSize())
);
Component entityCounts = Component.empty();
int entitiesListed = 0;
for (Entry<String, Integer> entityEntry : entry.getEntryCount()) {
entityCounts = entityCounts.append(getMessage(sender, "result.entity-count",
"type", Utils.enumToHumanName(entityEntry.getKey()),
"count", String.valueOf(entityEntry.getValue())
));
entitiesListed++;
if (entitiesListed >= 3) {
break;
}
}
resultLine = Replacer.replaceIn(resultLine, "entitycounts", entityCounts);
message = message.append(Component.newline()).append(resultLine);
}
if (results.size() > start + 10) {
if (page == 0) {
message = message.append(Component.newline()).append(getMessage(sender, "result.footer.next-page-only",
"page", String.valueOf(page + 1),
"nextpage", String.valueOf(page + 2),
"resulttype", resultType
));
} else {
message = message.append(Component.newline()).append(getMessage(sender, "result.footer.pagination",
"page", String.valueOf(page + 1),
"previouspage", String.valueOf(page),
"nextpage", String.valueOf(page + 2),
"resulttype", resultType
));
}
} else if (page > 0) {
message = message.append(Component.newline()).append(getMessage(sender, "result.footer.previous-page-only",
"page", String.valueOf(page + 1),
"previouspage", String.valueOf(page),
"resulttype", resultType
));
} else {
message = message.append(Component.newline()).append(getMessage(sender, "result.footer.no-pages",
"page", String.valueOf(page + 1)
));
}
} else {
message = message.append(Component.newline()).append(getMessage(sender, "result.no-entries"));
}
sender.sendMessage(message);
}
public SearchResult<?> getResult(CommandSender sender) {
return lastResultViewed.get(sender.getName());
}
public SearchResult<?> getResult(String type) {
return customResults.get(type);
}
public SearchResult<?> getResult(SearchType type) {
return results.get(type);
}
}