11package de .themoep .entitydetection .searcher ;
22
3+ import com .tcoded .folialib .impl .PlatformScheduler ;
4+ import com .tcoded .folialib .wrapper .task .WrappedTask ;
35import de .themoep .entitydetection .EntityDetection ;
46import net .md_5 .bungee .api .ChatColor ;
57import org .bukkit .Chunk ;
911import org .bukkit .command .CommandSender ;
1012import org .bukkit .entity .Entity ;
1113import org .bukkit .entity .EntityType ;
12- import org .bukkit .scheduler .BukkitRunnable ;
13- import org .bukkit .scheduler .BukkitTask ;
1414
1515import java .util .ArrayList ;
1616import java .util .Arrays ;
1717import java .util .Collections ;
1818import java .util .HashSet ;
1919import java .util .List ;
2020import java .util .Set ;
21+ import java .util .concurrent .atomic .AtomicInteger ;
2122
2223/**
2324 * Copyright 2016 Max Lee (https://github.com/Phoenix616/)
3435 * You should have received a copy of the Mozilla Public License v2.0
3536 * along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
3637 */
37- public class EntitySearch extends BukkitRunnable {
38+ public class EntitySearch implements Runnable {
3839 private final EntityDetection plugin ;
40+ private final PlatformScheduler scheduler ;
3941 private final CommandSender owner ;
4042 private SearchType type = SearchType .CUSTOM ;
41- private Set <EntityType > searchedEntities = new HashSet <EntityType >();
42- private Set <Class <?>> searchedBlockStates = new HashSet <Class <?> >();
43- private Set <Material > searchedMaterial = new HashSet <Material >();
43+ private Set <EntityType > searchedEntities = new HashSet <>();
44+ private Set <Class <?>> searchedBlockStates = new HashSet <>();
45+ private Set <Material > searchedMaterial = new HashSet <>();
4446 private long startTime ;
4547 private boolean running = true ;
46- private List <Entity > entities = new ArrayList <Entity >();
47- private List <BlockState > blockStates = new ArrayList <BlockState >();
48+ private List <Entity > entities = new ArrayList <>();
49+ private List <BlockState > blockStates = new ArrayList <>();
4850
4951 private boolean isWorldGuardRegion = false ;
52+ private final AtomicInteger pending = new AtomicInteger (0 );
5053
5154 public EntitySearch (EntityDetection plugin , CommandSender sender ) {
5255 this .plugin = plugin ;
56+ this .scheduler = plugin .getScheduler ();
5357 owner = sender ;
5458 }
5559
60+ public PlatformScheduler getScheduler () {
61+ return scheduler ;
62+ }
63+
5664 public SearchType getType () {
5765 return type ;
5866 }
@@ -117,20 +125,44 @@ public long getDuration() {
117125 return (System .currentTimeMillis () - getStartTime ()) / 1000 ;
118126 }
119127
120- public BukkitTask start () {
128+ public WrappedTask start () {
129+ int scheduled = 0 ;
121130 if (searchedEntities .size () > 0 ) {
122131 for (World world : plugin .getServer ().getWorlds ()) {
123- entities .addAll (world .getEntities ());
132+ pending .incrementAndGet ();
133+ scheduled ++;
134+ scheduler .runAtLocation (world .getSpawnLocation (), task -> {
135+ try {
136+ entities .addAll (world .getEntities ());
137+ } finally {
138+ if (pending .decrementAndGet () == 0 ) {
139+ scheduler .runLaterAsync (this , 1L );
140+ }
141+ }
142+ });
124143 }
125144 }
126145 if (searchedBlockStates .size () > 0 || searchedMaterial .size () > 0 ) {
127146 for (World world : plugin .getServer ().getWorlds ()) {
128147 for (Chunk chunk : world .getLoadedChunks ()) {
129- blockStates .addAll (Arrays .asList (chunk .getTileEntities ()));
148+ pending .incrementAndGet ();
149+ scheduled ++;
150+ scheduler .runAtLocation (chunk .getBlock (0 , 0 , 0 ).getLocation (), task -> {
151+ try {
152+ blockStates .addAll (Arrays .asList (chunk .getTileEntities ()));
153+ } finally {
154+ if (pending .decrementAndGet () == 0 ) {
155+ scheduler .runLaterAsync (this , 1L );
156+ }
157+ }
158+ });
130159 }
131160 }
132161 }
133- return runTaskAsynchronously (plugin );
162+ if (scheduled == 0 ) {
163+ return scheduler .runLaterAsync (this , 1L );
164+ }
165+ return null ;
134166 }
135167
136168 public boolean isRunning () {
@@ -139,7 +171,6 @@ public boolean isRunning() {
139171
140172 public void stop (String name ) {
141173 running = false ;
142- cancel ();
143174 if (!owner .getName ().equals (name )) {
144175 owner .sendMessage (ChatColor .YELLOW + name + ChatColor .RED + " stopped your " + getType () + " search after " + getDuration () + "s!" );
145176 }
0 commit comments