|
9 | 9 | import de.themoep.entitydetection.searcher.SearchResult; |
10 | 10 | import de.themoep.entitydetection.searcher.SearchResultEntry; |
11 | 11 | import de.themoep.entitydetection.searcher.SearchType; |
12 | | -import net.md_5.bungee.api.chat.ClickEvent; |
13 | | -import net.md_5.bungee.api.chat.ComponentBuilder; |
14 | | -import net.md_5.bungee.api.chat.HoverEvent; |
15 | | -import net.md_5.bungee.api.chat.TextComponent; |
16 | | -import org.bukkit.Bukkit; |
17 | | -import org.bukkit.ChatColor; |
| 12 | +import de.themoep.minedown.adventure.MineDown; |
| 13 | +import de.themoep.minedown.adventure.Replacer; |
| 14 | +import de.themoep.utils.lang.bukkit.LanguageManager; |
| 15 | +import net.kyori.adventure.text.Component; |
18 | 16 | import org.bukkit.command.CommandSender; |
19 | | -import org.bukkit.entity.Player; |
20 | 17 | import org.bukkit.plugin.java.JavaPlugin; |
21 | 18 |
|
22 | 19 | import java.text.SimpleDateFormat; |
23 | | -import java.util.ArrayList; |
24 | 20 | import java.util.Date; |
25 | 21 | import java.util.HashMap; |
26 | 22 | import java.util.Iterator; |
|
45 | 41 | * along with this program. If not, see <http://mozilla.org/MPL/2.0/>. |
46 | 42 | */ |
47 | 43 | public class EntityDetection extends JavaPlugin { |
| 44 | + private LanguageManager lang; |
48 | 45 |
|
49 | 46 | private EntitySearch currentSearch; |
50 | 47 |
|
51 | 48 | private Map<SearchType, SearchResult<?>> results = new HashMap<>(); |
52 | 49 | private Map<String, SearchResult<?>> customResults = new HashMap<>(); |
53 | 50 | private Map<String, SearchResult<?>> lastResultViewed = new HashMap<>(); |
54 | 51 |
|
55 | | - private boolean serverIsSpigot = true; |
56 | | - |
57 | 52 | public void onEnable() { |
58 | | - try { |
59 | | - Bukkit.class.getMethod("spigot"); |
60 | | - } catch (NoSuchMethodException noSpigot) { |
61 | | - serverIsSpigot = false; |
62 | | - } |
| 53 | + lang = new LanguageManager(this, System.getProperty("de.themoep.entitydetection.default-language", "en")); |
63 | 54 | PluginCommandExecutor cmdEx = new PluginCommandExecutor(this); |
64 | 55 | cmdEx.register(new SearchSubCommand(this)); |
65 | 56 | cmdEx.register(new TpSubCommand(this)); |
66 | 57 | cmdEx.register(new ListSubCommand(this)); |
67 | 58 | cmdEx.register(new StopSubCommand(this)); |
68 | 59 | } |
69 | 60 |
|
| 61 | + public String getRawMessage(CommandSender sender, String key, String... replacements) { |
| 62 | + return lang.getConfig(sender).get(key, replacements); |
| 63 | + } |
| 64 | + |
| 65 | + public Component getMessage(CommandSender sender, String key, String... replacements) { |
| 66 | + return MineDown.parse(getRawMessage(sender, key, replacements)); |
| 67 | + } |
| 68 | + |
70 | 69 | public boolean startSearch(EntitySearch search) { |
71 | 70 | if(currentSearch != null && currentSearch.isRunning()) { |
72 | 71 | return false; |
@@ -109,94 +108,52 @@ public void send(CommandSender sender, SearchResult<?> result) { |
109 | 108 | public void send(CommandSender sender, SearchResult<?> result, int page) { |
110 | 109 | lastResultViewed.put(sender.getName(), result); |
111 | 110 |
|
112 | | - String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(result.getEndTime())); |
| 111 | + String dateStr = new SimpleDateFormat(getRawMessage(sender, "result.time-format")).format(new Date(result.getEndTime())); |
113 | 112 |
|
114 | 113 | int start = page * 10; |
115 | | - if(serverIsSpigot && sender instanceof Player) { |
116 | | - String searchedTypes = ChatColor.YELLOW + "Entity Types:\n"; |
117 | | - Iterator<String> typeIter = result.getSearched().iterator(); |
118 | | - while(typeIter.hasNext()) { |
119 | | - searchedTypes += ChatColor.DARK_PURPLE + Utils.enumToHumanName(typeIter.next()); |
120 | | - if(typeIter.hasNext()) { |
121 | | - searchedTypes += "\n"; |
122 | | - } |
123 | | - } |
| 114 | + Component searchedTypes = getMessage(sender, "result.searched-types.head"); |
| 115 | + Iterator<String> typeIter = result.getSearched().iterator(); |
| 116 | + while (typeIter.hasNext()) { |
| 117 | + searchedTypes = searchedTypes.append(Component.newline()) |
| 118 | + .append(getMessage(sender, "result.searched-types.entry", "type", Utils.enumToHumanName(typeIter.next()))); |
| 119 | + } |
124 | 120 |
|
125 | | - ComponentBuilder builder = new ComponentBuilder(Utils.enumToHumanName(result.getType()) + " search ") |
126 | | - .color(net.md_5.bungee.api.ChatColor.GREEN) |
127 | | - .event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(searchedTypes))) |
128 | | - .append("from " + dateStr + ":") |
129 | | - .color(net.md_5.bungee.api.ChatColor.WHITE); |
130 | | - |
131 | | - List<? extends SearchResultEntry<?>> results = result.getSortedEntries(); |
132 | | - if(results.size() > 0) { |
133 | | - for(int line = start; line < start + 10 && line < results.size(); line++) { |
134 | | - SearchResultEntry<?> entry = results.get(line); |
135 | | - |
136 | | - builder.append("\n") |
137 | | - .retain(ComponentBuilder.FormatRetention.NONE) |
138 | | - .append(" " + (line + 1) + ": ") |
139 | | - .color(net.md_5.bungee.api.ChatColor.WHITE) |
140 | | - .event( |
141 | | - new HoverEvent( |
142 | | - HoverEvent.Action.SHOW_TEXT, |
143 | | - new ComponentBuilder("Click to teleport to " + (line + 1)) |
144 | | - .color(net.md_5.bungee.api.ChatColor.BLUE) |
145 | | - .create() |
146 | | - ) |
147 | | - ) |
148 | | - .event(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/detect tp " + (line + 1))) |
149 | | - .append(entry.getLocation() + " ") |
150 | | - .color(net.md_5.bungee.api.ChatColor.YELLOW) |
151 | | - .append(entry.getSize() + " ") |
152 | | - .color(net.md_5.bungee.api.ChatColor.RED); |
153 | | - |
154 | | - int entitiesListed = 0; |
155 | | - for(Entry<String, Integer> entityEntry : entry.getEntryCount()) { |
156 | | - builder.append(Utils.enumToHumanName(entityEntry.getKey()) + "[") |
157 | | - .color(net.md_5.bungee.api.ChatColor.GREEN) |
158 | | - .append(entityEntry.getValue().toString()) |
159 | | - .color(net.md_5.bungee.api.ChatColor.WHITE) |
160 | | - .append("] ") |
161 | | - .color(net.md_5.bungee.api.ChatColor.GREEN); |
162 | | - |
163 | | - entitiesListed++; |
164 | | - if(entitiesListed >= 3) |
165 | | - break; |
166 | | - } |
| 121 | + Component message = getMessage(sender, "result.head", "type", Utils.enumToHumanName(result.getType()), "timestamp", dateStr); |
| 122 | + message = Replacer.replaceIn(message, "searchedtypes", searchedTypes); |
| 123 | + |
| 124 | + List<? extends SearchResultEntry<?>> results = result.getSortedEntries(); |
| 125 | + if (results.size() > 0) { |
| 126 | + for (int line = start; line < start + 10 && line < results.size(); line++) { |
| 127 | + SearchResultEntry<?> entry = results.get(line); |
| 128 | + |
| 129 | + Component resultLine = getMessage(sender, "result.entry", |
| 130 | + "line", String.valueOf(line + 1), |
| 131 | + "location", String.valueOf(entry.getLocation()), |
| 132 | + "size", String.valueOf(entry.getSize()) |
| 133 | + ); |
| 134 | + |
| 135 | + Component entityCounts = Component.empty(); |
| 136 | + int entitiesListed = 0; |
| 137 | + for(Entry<String, Integer> entityEntry : entry.getEntryCount()) { |
| 138 | + entityCounts = entityCounts.append(getMessage(sender, "result.entity-count", |
| 139 | + "type", Utils.enumToHumanName(entityEntry.getKey()), |
| 140 | + "count", String.valueOf(entityEntry.getValue()) |
| 141 | + )); |
| 142 | + |
| 143 | + entitiesListed++; |
| 144 | + if(entitiesListed >= 3) |
| 145 | + break; |
167 | 146 | } |
168 | | - } else { |
169 | | - builder.append("\n No entities of that type found!") |
170 | | - .color(net.md_5.bungee.api.ChatColor.RED); |
171 | | - } |
172 | | - |
173 | | - ((Player) sender).spigot().sendMessage(builder.create()); |
174 | | - } else { |
175 | | - List<String> msg = new ArrayList<String>(); |
176 | | - msg.add(ChatColor.GREEN + Utils.enumToHumanName(result.getType()) + " search " + ChatColor.WHITE + "from " + dateStr + ":"); |
177 | | - |
178 | | - List<? extends SearchResultEntry<?>> chunkEntries = result.getSortedEntries(); |
179 | | - if(chunkEntries.size() > 0) { |
180 | | - for(int line = start; line < start + 10 && line < chunkEntries.size(); line++) { |
181 | | - SearchResultEntry<?> chunkEntry = chunkEntries.get(line); |
182 | 147 |
|
183 | | - String lineText = ChatColor.WHITE + " " + (line + 1) + ": " + ChatColor.YELLOW + chunkEntry.getLocation() + " " + ChatColor.RED + chunkEntry.getSize() + " "; |
| 148 | + resultLine = Replacer.replaceIn(resultLine, "entitycounts", entityCounts); |
184 | 149 |
|
185 | | - int entitiesListed = 0; |
186 | | - for(Entry<String, Integer> entityEntry : chunkEntry.getEntryCount()) { |
187 | | - lineText += ChatColor.GREEN + Utils.enumToHumanName(entityEntry.getKey()) + "[" + ChatColor.WHITE + entityEntry.getValue().toString() + ChatColor.GREEN + "] "; |
188 | | - entitiesListed++; |
189 | | - if(entitiesListed >= 3) |
190 | | - break; |
191 | | - } |
192 | | - |
193 | | - msg.add(lineText); |
194 | | - } |
195 | | - } else { |
196 | | - msg.add(ChatColor.RED + " Nothing of that type found!"); |
| 150 | + message = message.append(Component.newline()).append(resultLine); |
197 | 151 | } |
198 | | - sender.sendMessage(msg.toArray(new String[msg.size()])); |
| 152 | + } else { |
| 153 | + message = message.append(Component.newline()).append(getMessage(sender, "result.no-entries")); |
199 | 154 | } |
| 155 | + |
| 156 | + sender.sendMessage(message); |
200 | 157 | } |
201 | 158 |
|
202 | 159 | public SearchResult<?> getResult(CommandSender sender) { |
|
0 commit comments