3232import valandur .webapi .api .cache .plugin .ICachedPluginContainer ;
3333import valandur .webapi .api .cache .tileentity .ICachedTileEntity ;
3434import valandur .webapi .api .cache .world .ICachedWorld ;
35+ import valandur .webapi .api .permission .IPermissionService ;
3536import valandur .webapi .cache .chat .CachedChatMessage ;
3637import valandur .webapi .cache .command .CachedCommand ;
3738import valandur .webapi .cache .command .CachedCommandCall ;
4546import valandur .webapi .cache .world .CachedChunk ;
4647import valandur .webapi .cache .world .CachedWorld ;
4748import valandur .webapi .json .JsonService ;
48- import valandur .webapi .permission .PermissionService ;
4949import valandur .webapi .util .Util ;
5050
5151import java .lang .reflect .Field ;
5454import java .util .*;
5555import java .util .concurrent .ConcurrentHashMap ;
5656import java .util .concurrent .ConcurrentLinkedQueue ;
57+ import java .util .function .Predicate ;
5758
5859public class CacheService implements ICacheService {
5960
@@ -259,6 +260,25 @@ public Collection<ICachedPlayer> getPlayers() {
259260 return new ArrayList <>(players .values ());
260261 }
261262 @ Override
263+ public Optional <ICachedPlayer > getPlayer (String nameOrUuid ) {
264+ if (Util .isValidUUID (nameOrUuid )) {
265+ return getPlayer (UUID .fromString (nameOrUuid ));
266+ }
267+
268+ Optional <CachedPlayer > player = players .values ().stream ().filter (p -> p .getName ().equalsIgnoreCase (nameOrUuid )).findAny ();
269+ if (player .isPresent ())
270+ return player .flatMap (p -> getPlayer (p .getUUID ()));
271+
272+ return WebAPI .runOnMain (() -> {
273+ Optional <UserStorageService > optSrv = Sponge .getServiceManager ().provide (UserStorageService .class );
274+ if (!optSrv .isPresent ())
275+ return null ;
276+
277+ Optional <User > optUser = optSrv .get ().get (nameOrUuid );
278+ return optUser .<ICachedPlayer >map (CachedPlayer ::new ).orElse (null );
279+ });
280+ }
281+ @ Override
262282 public Optional <ICachedPlayer > getPlayer (UUID uuid ) {
263283 if (!players .containsKey (uuid )) {
264284 return WebAPI .runOnMain (() -> {
@@ -267,10 +287,8 @@ public Optional<ICachedPlayer> getPlayer(UUID uuid) {
267287 return null ;
268288
269289 Optional <User > optUser = optSrv .get ().get (uuid );
270- if (!optUser .isPresent ())
271- return null ;
290+ return optUser .<ICachedPlayer >map (CachedPlayer ::new ).orElse (null );
272291
273- return new CachedPlayer (optUser .get ());
274292 });
275293 }
276294
@@ -411,32 +429,47 @@ public ICachedCommand updateCommand(CommandMapping command) {
411429 }
412430
413431 @ Override
414- public Optional <Collection <ICachedTileEntity >> getTileEntities () {
432+ public Optional <Collection <ICachedTileEntity >> getTileEntities (Predicate < TileEntity > predicate , int limit ) {
415433 return WebAPI .runOnMain (() -> {
416434 Collection <ICachedTileEntity > entities = new LinkedList <>();
417435
418- for (World world : Sponge .getServer ().getWorlds ()) {
419- Collection <TileEntity > ents = world .getTileEntities ();
436+ int i = 0 ;
437+ Collection <World > worlds = Sponge .getServer ().getWorlds ();
438+ for (World world : worlds ) {
439+ Collection <TileEntity > ents = world .getTileEntities (predicate );
420440 for (TileEntity te : ents ) {
441+ if (!te .isValid ()) continue ;
421442 entities .add (new CachedTileEntity (te ));
443+
444+ i ++;
445+ if (limit > 0 && i >= limit )
446+ break ;
422447 }
448+
449+ if (limit > 0 && i >= limit )
450+ break ;
423451 }
424452
425453 return entities ;
426454 });
427455 }
428456 @ Override
429- public Optional <Collection <ICachedTileEntity >> getTileEntities (ICachedWorld world ) {
457+ public Optional <Collection <ICachedTileEntity >> getTileEntities (ICachedWorld world , Predicate < TileEntity > predicate , int limit ) {
430458 return WebAPI .runOnMain (() -> {
431459 Optional <?> w = world .getLive ();
432460 if (!w .isPresent ())
433461 return null ;
434462
463+ int i = 0 ;
435464 Collection <ICachedTileEntity > entities = new LinkedList <>();
436- Collection <TileEntity > ents = ((World )w .get ()).getTileEntities ();
465+ Collection <TileEntity > ents = ((World )w .get ()).getTileEntities (predicate );
437466 for (TileEntity te : ents ) {
438467 if (!te .isValid ()) continue ;
439468 entities .add (new CachedTileEntity (te ));
469+
470+ i ++;
471+ if (limit > 0 && i >= limit )
472+ break ;
440473 }
441474
442475 return entities ;
@@ -533,7 +566,7 @@ public Tuple<Map<String, JsonNode>, Map<String, JsonNode>> getExtraData(ICachedO
533566
534567 try {
535568 Object res = field .get ().get (obj );
536- fields .put (fieldName , json .toJson (res , true , PermissionService .permitAllNode ()));
569+ fields .put (fieldName , json .toJson (res , true , IPermissionService .permitAllNode ()));
537570 } catch (IllegalAccessException e ) {
538571 fields .put (fieldName , TextNode .valueOf ("ERROR: " + e .toString ()));
539572 }
@@ -559,7 +592,7 @@ public Tuple<Map<String, JsonNode>, Map<String, JsonNode>> getExtraData(ICachedO
559592
560593 try {
561594 Object res = method .get ().invoke (obj );
562- methods .put (methodName , json .toJson (res , true , PermissionService .permitAllNode ()));
595+ methods .put (methodName , json .toJson (res , true , IPermissionService .permitAllNode ()));
563596 } catch (IllegalAccessException | InvocationTargetException e ) {
564597 methods .put (methodName , TextNode .valueOf ("ERROR: " + e .toString ()));
565598 }
0 commit comments