-
-
Notifications
You must be signed in to change notification settings - Fork 754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix-count-entities #4356
Open
ch4ika
wants to merge
5
commits into
IntellectualSites:main
Choose a base branch
from
ch4ika:fix-count-entities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix-count-entities #4356
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a5de34d
fix-count-entities
ch4ika 9c6078c
Simplify array assignment
ch4ika f8c7124
Fix entity filtering in chunk to match plot boundaries
ch4ika a120e49
Fix asynchronous loading issue causing premature count return
ch4ika 9720a91
Implement asynchronous entity check
ch4ika File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,19 +41,18 @@ | |
import com.sk89q.worldedit.regions.CuboidRegion; | ||
import com.sk89q.worldedit.world.block.BaseBlock; | ||
import com.sk89q.worldedit.world.block.BlockTypes; | ||
import io.papermc.lib.PaperLib; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Chunk; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.Item; | ||
import org.bukkit.entity.Player; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL; | ||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY; | ||
|
@@ -88,79 +87,45 @@ public boolean handleClear( | |
|
||
@Override | ||
public int[] countEntities(@NonNull Plot plot) { | ||
int[] count = new int[6]; | ||
int[] existing = (int[]) plot.getMeta("EntityCount"); | ||
if (existing != null && (System.currentTimeMillis() - (long) plot.getMeta("EntityCountTime") < 1000)) { | ||
return existing; | ||
} | ||
PlotArea area = plot.getArea(); | ||
World world = BukkitUtil.getWorld(area.getWorldName()); | ||
Location bot = plot.getBottomAbs(); | ||
Location top = plot.getTopAbs(); | ||
int bx = bot.getX() >> 4; | ||
int bz = bot.getZ() >> 4; | ||
|
||
int tx = top.getX() >> 4; | ||
int tz = top.getZ() >> 4; | ||
|
||
int size = tx - bx << 4; | ||
|
||
Set<Chunk> chunks = new HashSet<>(); | ||
for (int X = bx; X <= tx; X++) { | ||
for (int Z = bz; Z <= tz; Z++) { | ||
if (world.isChunkLoaded(X, Z)) { | ||
chunks.add(world.getChunkAt(X, Z)); | ||
} | ||
} | ||
if (world == null) { | ||
return count; | ||
} | ||
|
||
boolean doWhole = false; | ||
List<Entity> entities = null; | ||
if (size > 200 && chunks.size() > 200) { | ||
entities = world.getEntities(); | ||
if (entities.size() < 16 + size / 8) { | ||
doWhole = true; | ||
} | ||
} | ||
|
||
int[] count = new int[6]; | ||
if (doWhole) { | ||
for (Entity entity : entities) { | ||
org.bukkit.Location location = entity.getLocation(); | ||
PaperLib.getChunkAtAsync(location).thenAccept(chunk -> { | ||
if (chunks.contains(chunk)) { | ||
int X = chunk.getX(); | ||
int Z = chunk.getZ(); | ||
if (X > bx && X < tx && Z > bz && Z < tz) { | ||
count(count, entity); | ||
} else { | ||
Plot other = area.getPlot(BukkitUtil.adapt(location)); | ||
if (plot.equals(other)) { | ||
count(count, entity); | ||
} | ||
for (final CuboidRegion region : plot.getRegions()) { | ||
for (int x = region.getMinimumPoint().getX() >> 4; x <= region.getMaximumPoint().getX() >> 4; x++) { | ||
for (int z = region.getMinimumPoint().getZ() >> 4; z <= region.getMaximumPoint().getZ() >> 4; z++) { | ||
Chunk chunk = world.getChunkAt(x, z); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this method actually be called async? |
||
final Entity[] entities = chunk.getEntities(); | ||
for (Entity entity : entities) { | ||
if (entity instanceof Player || entity instanceof Item) { | ||
continue; | ||
} | ||
} | ||
}); | ||
} | ||
} else { | ||
for (Chunk chunk : chunks) { | ||
int X = chunk.getX(); | ||
int Z = chunk.getZ(); | ||
Entity[] entities1 = chunk.getEntities(); | ||
for (Entity entity : entities1) { | ||
if (X == bx || X == tx || Z == bz || Z == tz) { | ||
Plot other = area.getPlot(BukkitUtil.adapt(entity.getLocation())); | ||
|
||
org.bukkit.Location location = entity.getLocation(); | ||
Plot other = area.getPlot(BukkitUtil.adapt(location)); | ||
if (plot.equals(other)) { | ||
count(count, entity); | ||
} | ||
} else { | ||
count(count, entity); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<int[]> countEntitiesAsync(final Plot plot) { | ||
return CompletableFuture.supplyAsync(() -> countEntities(plot)); | ||
} | ||
|
||
@Override | ||
public boolean regenerateRegion( | ||
final @NonNull Location pos1, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,13 +100,6 @@ | |
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Consumer; | ||
|
||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL; | ||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY; | ||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_MISC; | ||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_MOB; | ||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_MONSTER; | ||
import static com.plotsquared.core.util.entity.EntityCategories.CAP_VEHICLE; | ||
|
||
/** | ||
* The plot class<br> | ||
* [IMPORTANT] | ||
|
@@ -1219,17 +1212,11 @@ public boolean removeFlag(final @NonNull PlotFlag<?, ?> flag) { | |
* @see RegionManager#countEntities(Plot) | ||
*/ | ||
public int[] countEntities() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably deprecate the "old" sync methods if moving to async |
||
int[] count = new int[6]; | ||
for (Plot current : this.getConnectedPlots()) { | ||
int[] result = this.regionManager.countEntities(current); | ||
count[CAP_ENTITY] += result[CAP_ENTITY]; | ||
count[CAP_ANIMAL] += result[CAP_ANIMAL]; | ||
count[CAP_MONSTER] += result[CAP_MONSTER]; | ||
count[CAP_MOB] += result[CAP_MOB]; | ||
count[CAP_VEHICLE] += result[CAP_VEHICLE]; | ||
count[CAP_MISC] += result[CAP_MISC]; | ||
} | ||
return count; | ||
return this.regionManager.countEntities(this); | ||
} | ||
|
||
public CompletableFuture<int[]> countEntitiesAsync() { | ||
return this.regionManager.countEntitiesAsync(this); | ||
} | ||
|
||
/** | ||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code style