-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4727b23
commit 5bdca4a
Showing
13 changed files
with
222 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/com/froobworld/farmcontrol/hook/entitygetter/BukkitEntityGetterHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.froobworld.farmcontrol.hook.entitygetter; | ||
|
||
import com.froobworld.farmcontrol.controller.entity.SnapshotEntity; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class BukkitEntityGetterHook implements EntityGetterHook { | ||
@Override | ||
public CompletableFuture<Collection<Entity>> getEntities(World world, Class<?>... classes) { | ||
return CompletableFuture.completedFuture(world.getEntitiesByClasses(classes)); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Collection<Entity>> getEntities(World world) { | ||
return CompletableFuture.completedFuture(world.getEntities()); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<SnapshotEntity>> getSnapshotEntities(World world, Class<?>... classes) { | ||
List<SnapshotEntity> snapshotEntities = new ArrayList<>(); | ||
for (Entity entity : world.getEntitiesByClasses(classes)) { | ||
snapshotEntities.add(new SnapshotEntity(entity)); | ||
} | ||
return CompletableFuture.completedFuture(snapshotEntities); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<SnapshotEntity>> getSnapshotEntities(World world) { | ||
List<SnapshotEntity> snapshotEntities = new ArrayList<>(); | ||
for (Entity entity : world.getEntities()) { | ||
snapshotEntities.add(new SnapshotEntity(entity)); | ||
} | ||
return CompletableFuture.completedFuture(snapshotEntities); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/froobworld/farmcontrol/hook/entitygetter/EntityGetterHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.froobworld.farmcontrol.hook.entitygetter; | ||
|
||
import com.froobworld.farmcontrol.controller.entity.SnapshotEntity; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface EntityGetterHook { | ||
|
||
CompletableFuture<Collection<Entity>> getEntities(World world, Class<?>... classes); | ||
|
||
CompletableFuture<Collection<Entity>> getEntities(World world); | ||
|
||
CompletableFuture<List<SnapshotEntity>> getSnapshotEntities(World world, Class<?>... classes); | ||
|
||
CompletableFuture<List<SnapshotEntity>> getSnapshotEntities(World world); | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/froobworld/farmcontrol/hook/entitygetter/RegionisedEntityGetterHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.froobworld.farmcontrol.hook.entitygetter; | ||
|
||
import com.froobworld.farmcontrol.FarmControl; | ||
import com.froobworld.farmcontrol.controller.entity.SnapshotEntity; | ||
import com.froobworld.farmcontrol.hook.scheduler.RegionisedSchedulerHook; | ||
import com.google.common.collect.Sets; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Chunk; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import java.util.function.Function; | ||
|
||
public class RegionisedEntityGetterHook implements EntityGetterHook { | ||
private final FarmControl farmControl; | ||
|
||
public RegionisedEntityGetterHook(FarmControl farmControl) { | ||
this.farmControl = farmControl; | ||
} | ||
|
||
private <T> CompletableFuture<Collection<T>> getMappedEntities(World world, Function<Entity, T> mapper, Class<?>... entityClasses) { | ||
MappedEntityCollector<T> entityCollector = new MappedEntityCollector<>(); | ||
CompletableFuture<Void> future = CompletableFuture.completedFuture(null); | ||
for (Chunk chunk : world.getLoadedChunks()) { | ||
CompletableFuture<Void> chunkFuture = new CompletableFuture<>(); | ||
Bukkit.getRegionScheduler().execute(farmControl, world, chunk.getX(), chunk.getZ(), () -> { | ||
for (Entity entity : chunk.getEntities()) { | ||
boolean eligible = entityClasses.length == 0; | ||
for (Class<?> entityClass : entityClasses) { | ||
if (entityClass.isAssignableFrom(entity.getClass())) { | ||
eligible = true; | ||
break; | ||
} | ||
} | ||
if (eligible) { | ||
entityCollector.addEntity(entity, mapper.apply(entity)); | ||
} | ||
} | ||
chunkFuture.complete(null); | ||
}); | ||
future = future.thenCompose(v -> chunkFuture); | ||
} | ||
return future.thenApply(v -> entityCollector.getEntities()); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Collection<Entity>> getEntities(World world, Class<?>... entityClasses) { | ||
return getMappedEntities(world, Function.identity(), entityClasses); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Collection<Entity>> getEntities(World world) { | ||
return getMappedEntities(world, Function.identity()); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<SnapshotEntity>> getSnapshotEntities(World world, Class<?>... classes) { | ||
return getMappedEntities(world, SnapshotEntity::new, classes).thenApply(ArrayList::new); | ||
} | ||
|
||
@Override | ||
public CompletableFuture<List<SnapshotEntity>> getSnapshotEntities(World world) { | ||
return getMappedEntities(world, SnapshotEntity::new).thenApply(ArrayList::new); | ||
} | ||
|
||
public static boolean isCompatible() { | ||
return RegionisedSchedulerHook.isCompatible(); | ||
} | ||
|
||
private static class MappedEntityCollector<T> { | ||
private final Set<UUID> seenEntities = Sets.newConcurrentHashSet(); | ||
private final Collection<T> mappedEntities = new ConcurrentLinkedQueue<>(); | ||
|
||
public void addEntity(Entity entity, T mappedEntity) { | ||
if (seenEntities.add(entity.getUniqueId())) { | ||
mappedEntities.add(mappedEntity); | ||
} | ||
} | ||
|
||
public Collection<T> getEntities() { | ||
return mappedEntities; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.