@@ -53,7 +53,7 @@ public void clear(int count) {
53
53
* Count all entities in the specified chunk in the World corresponding to
54
54
* this WorldCount instance.
55
55
*
56
- * @param chunk
56
+ * @param chunk the counted chunk.
57
57
*/
58
58
public void countChunk (Chunk chunk ) {
59
59
_chunkCounts .add (new ChunkCount (chunk ));
@@ -88,6 +88,7 @@ public Total(EntityType type, int count) {
88
88
* @param sender the command sender to be sent messages.
89
89
*/
90
90
public void summarise (CommandSender sender ) {
91
+ // Add number of entities in all boxes of all chunks to accumulator.
91
92
EntityCounts accumulator = new EntityCounts ();
92
93
for (ChunkCount chunkCount : _chunkCounts ) {
93
94
for (int i = 0 ; i <= 15 ; ++i ) {
@@ -148,16 +149,16 @@ public void list(CommandSender sender, int page) {
148
149
sender .sendMessage (ChatColor .RED + "Valid page numbers are 1 to " + pageCount + "." );
149
150
}
150
151
} 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 ));
153
154
sender .sendMessage (header );
154
155
for (int i = (page - 1 ) * PAGE_SIZE ; i < Math .min (page * PAGE_SIZE , _sortedGroups .length ); ++i ) {
155
156
Location loc = _sortedGroups [i ].getLocation ();
156
157
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 ());
161
162
sender .sendMessage (line );
162
163
}
163
164
sender .sendMessage (header );
@@ -188,10 +189,10 @@ public void tp(CommandSender sender, int id) {
188
189
Location loc = group .getLocation ();
189
190
player .teleport (loc );
190
191
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 ()));
195
196
}
196
197
}
197
198
} // tp
@@ -206,8 +207,11 @@ protected void sortGroups(CommandSender sender) {
206
207
if (_sortedGroups == null ) {
207
208
long start = System .currentTimeMillis ();
208
209
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 ());
211
215
for (ChunkCount chunkCount : _chunkCounts ) {
212
216
for (int i = 0 ; i <= 15 ; ++i ) {
213
217
if (chunkCount .hasBox (i )) {
@@ -216,19 +220,15 @@ protected void sortGroups(CommandSender sender) {
216
220
}
217
221
}
218
222
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 ]);
228
228
229
229
long elapsedMillis = System .currentTimeMillis () - start ;
230
230
sender .sendMessage (ChatColor .GOLD + String .format ("Sorted %d entity groups in %d milliseconds." ,
231
- _sortedGroups .length , elapsedMillis ));
231
+ _sortedGroups .length , elapsedMillis ));
232
232
}
233
233
} // sortGroups
234
234
0 commit comments