Skip to content

Commit e6f3bd1

Browse files
committed
Filter ITEM_FRAME out of /ec list.
I've been meaning to do filter commands for 3 years and it still hasn't happened. Let's just make things better.
1 parent dd64534 commit e6f3bd1

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/io/totemo/ec/WorldCount.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void clear(int count) {
5353
* Count all entities in the specified chunk in the World corresponding to
5454
* this WorldCount instance.
5555
*
56-
* @param chunk
56+
* @param chunk the counted chunk.
5757
*/
5858
public void countChunk(Chunk chunk) {
5959
_chunkCounts.add(new ChunkCount(chunk));
@@ -88,6 +88,7 @@ public Total(EntityType type, int count) {
8888
* @param sender the command sender to be sent messages.
8989
*/
9090
public void summarise(CommandSender sender) {
91+
// Add number of entities in all boxes of all chunks to accumulator.
9192
EntityCounts accumulator = new EntityCounts();
9293
for (ChunkCount chunkCount : _chunkCounts) {
9394
for (int i = 0; i <= 15; ++i) {
@@ -148,16 +149,16 @@ public void list(CommandSender sender, int page) {
148149
sender.sendMessage(ChatColor.RED + "Valid page numbers are 1 to " + pageCount + ".");
149150
}
150151
} else {
151-
String header = ChatColor.translateAlternateColorCodes('&',
152-
String.format("&f---------- &6Page &e%d &6of &e%d &f----------", page, pageCount));
152+
String header = ChatColor.translateAlternateColorCodes('&', String.format("&f---------- &6Page &e%d &6of &e%d &f----------",
153+
page, pageCount));
153154
sender.sendMessage(header);
154155
for (int i = (page - 1) * PAGE_SIZE; i < Math.min(page * PAGE_SIZE, _sortedGroups.length); ++i) {
155156
Location loc = _sortedGroups[i].getLocation();
156157
String line = String.format("%s(% 3d) %s% 3d %s%-18s %s(%d, %d, %d)",
157-
ChatColor.GOLD.toString(), i + 1,
158-
ChatColor.GREEN.toString(), _sortedGroups[i].getCount(),
159-
ChatColor.YELLOW.toString(), _sortedGroups[i].getEntityType().name(),
160-
ChatColor.GOLD.toString(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
158+
ChatColor.GOLD.toString(), i + 1,
159+
ChatColor.GREEN.toString(), _sortedGroups[i].getCount(),
160+
ChatColor.YELLOW.toString(), _sortedGroups[i].getEntityType().name(),
161+
ChatColor.GOLD.toString(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
161162
sender.sendMessage(line);
162163
}
163164
sender.sendMessage(header);
@@ -188,10 +189,10 @@ public void tp(CommandSender sender, int id) {
188189
Location loc = group.getLocation();
189190
player.teleport(loc);
190191
sender.sendMessage(String.format("%sTeleporting you to %s%d %s%s %sat (%d, %d, %d).",
191-
ChatColor.GOLD.toString(),
192-
ChatColor.GREEN.toString(), group.getCount(),
193-
ChatColor.YELLOW.toString(), group.getEntityType().name(),
194-
ChatColor.GOLD.toString(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
192+
ChatColor.GOLD.toString(),
193+
ChatColor.GREEN.toString(), group.getCount(),
194+
ChatColor.YELLOW.toString(), group.getEntityType().name(),
195+
ChatColor.GOLD.toString(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
195196
}
196197
}
197198
} // tp
@@ -206,8 +207,11 @@ protected void sortGroups(CommandSender sender) {
206207
if (_sortedGroups == null) {
207208
long start = System.currentTimeMillis();
208209

209-
// Sort EntityGroups by entity count and cache the result.
210-
ArrayList<EntityGroup> groups = new ArrayList<EntityGroup>(_chunkCounts.size());
210+
// For all chunks, and for all 16 boxes in a chunk, for each
211+
// EntityType that has a non-zero count append an EntityGroup. Note
212+
// that groups is initialised with a certain capacity, but starts
213+
// with 0 size.
214+
ArrayList<EntityGroup> groups = new ArrayList<>(_chunkCounts.size());
211215
for (ChunkCount chunkCount : _chunkCounts) {
212216
for (int i = 0; i <= 15; ++i) {
213217
if (chunkCount.hasBox(i)) {
@@ -216,19 +220,15 @@ protected void sortGroups(CommandSender sender) {
216220
}
217221
}
218222

219-
// Would you believe ArrayList<>.sort() didn't exist until Java 8?
220-
_sortedGroups = new EntityGroup[groups.size()];
221-
groups.toArray(_sortedGroups);
222-
Arrays.sort(_sortedGroups, new Comparator<EntityGroup>() {
223-
@Override
224-
public int compare(EntityGroup left, EntityGroup right) {
225-
return right.getCount() - left.getCount();
226-
}
227-
});
223+
// Exclude groups of ITEM_FRAME and sort descending by count.
224+
_sortedGroups = groups.stream()
225+
.filter(g -> g.getEntityType() != EntityType.ITEM_FRAME)
226+
.sorted((l, r) -> r.getCount() - l.getCount())
227+
.toArray(size -> new EntityGroup[size]);
228228

229229
long elapsedMillis = System.currentTimeMillis() - start;
230230
sender.sendMessage(ChatColor.GOLD + String.format("Sorted %d entity groups in %d milliseconds.",
231-
_sortedGroups.length, elapsedMillis));
231+
_sortedGroups.length, elapsedMillis));
232232
}
233233
} // sortGroups
234234

0 commit comments

Comments
 (0)