Merge pull request #88 from BentoBoxWorld/profiler

Async limit checking
This commit is contained in:
tastybento 2020-07-26 14:41:52 -07:00 committed by GitHub
commit 71018c08d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 48 deletions

View File

@ -64,7 +64,7 @@
<!-- Do not change unless you want different name for local builds. --> <!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number> <build.number>-LOCAL</build.number>
<!-- This allows to change between versions. --> <!-- This allows to change between versions. -->
<build.version>1.14.0</build.version> <build.version>1.15.0</build.version>
</properties> </properties>
<!-- Profiles will allow to automatically change build version. --> <!-- Profiles will allow to automatically change build version. -->

View File

@ -8,7 +8,9 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
@ -20,7 +22,9 @@ import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent; import org.bukkit.event.vehicle.VehicleCreateEvent;
import org.bukkit.inventory.ItemStack;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
@ -111,7 +115,6 @@ public class EntityLimitListener implements Listener {
} }
// Tag the entity with the island spawn location // Tag the entity with the island spawn location
checkLimit(e, bypass); checkLimit(e, bypass);
} }
private boolean checkByPass(Location l) { private boolean checkByPass(Location l) {
@ -156,14 +159,61 @@ public class EntityLimitListener implements Listener {
} }
private void checkLimit(CreatureSpawnEvent e, boolean bypass) { private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
Bukkit.getScheduler().runTaskAsynchronously(BentoBox.getInstance(), () ->
addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> { addon.getIslands().getIslandAt(e.getLocation()).ifPresent(island -> {
// Check if creature is allowed to spawn or not // Check if creature is allowed to spawn or not
AtLimitResult res; AtLimitResult res;
if (!bypass && !island.isSpawn() && (res = atLimit(island, e.getEntity())).hit()) { if (!bypass && !island.isSpawn() && (res = atLimit(island, e.getEntity())).hit()) {
// Not allowed // Not allowed
e.setCancelled(true); Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> {
e.getEntity().remove();
// If the entity was build, drop the building materials
Bukkit.getScheduler().runTask(addon.getPlugin(), () -> replaceEntity(e));
// If the reason is anything but because of a spawner then tell players within range // If the reason is anything but because of a spawner then tell players within range
if (!e.getSpawnReason().equals(SpawnReason.SPAWNER) && !e.getSpawnReason().equals(SpawnReason.NATURAL) && !e.getSpawnReason().equals(SpawnReason.INFECTION) && !e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL) && !e.getSpawnReason().equals(SpawnReason.REINFORCEMENTS) && !e.getSpawnReason().equals(SpawnReason.SLIME_SPLIT)) { tellPlayers(e, res);
});
}
}));
}
private void replaceEntity(CreatureSpawnEvent e) {
World world = e.getEntity().getWorld();
Location l = e.getLocation();
switch (e.getSpawnReason()) {
case BUILD_IRONGOLEM:
world.dropItem(l, new ItemStack(Material.IRON_BLOCK,3));
world.dropItem(l, new ItemStack(Material.CARVED_PUMPKIN));
break;
case BUILD_SNOWMAN:
world.dropItem(l, new ItemStack(Material.SNOW_BLOCK,2));
world.dropItem(l, new ItemStack(Material.CARVED_PUMPKIN));
l.getBlock().setType(Material.AIR);
break;
case BUILD_WITHER:
world.dropItem(l, new ItemStack(Material.SOUL_SAND,3));
world.dropItem(l, new ItemStack(Material.WITHER_SKELETON_SKULL, 3));
break;
case CURED:
world.spawnEntity(e.getLocation(), EntityType.ZOMBIE_VILLAGER);
break;
case DISPENSE_EGG:
break;
case EGG:
world.dropItem(l, new ItemStack(Material.EGG));
break;
case SPAWNER_EGG:
break;
default:
break;
}
}
private void tellPlayers(CreatureSpawnEvent e, AtLimitResult res) {
if (!e.getSpawnReason().equals(SpawnReason.SPAWNER) && !e.getSpawnReason().equals(SpawnReason.NATURAL)
&& !e.getSpawnReason().equals(SpawnReason.INFECTION) && !e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)
&& !e.getSpawnReason().equals(SpawnReason.REINFORCEMENTS) && !e.getSpawnReason().equals(SpawnReason.SLIME_SPLIT)) {
World w = e.getLocation().getWorld(); World w = e.getLocation().getWorld();
if (w == null) return; if (w == null) return;
for (Entity ent : w.getNearbyEntities(e.getLocation(), 5, 5, 5)) { for (Entity ent : w.getNearbyEntities(e.getLocation(), 5, 5, 5)) {
@ -182,9 +232,6 @@ public class EntityLimitListener implements Listener {
} }
} }
});
}
/** /**
* Checks if new entities can be added to island * Checks if new entities can be added to island
@ -215,14 +262,13 @@ public class EntityLimitListener implements Listener {
.forEach(group -> groupsLimits.put(group, group.getLimit())); .forEach(group -> groupsLimits.put(group, group.getLimit()));
} }
if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult(); if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult();
// We have to count the entities // We have to count the entities
if (limitAmount >= 0) if (limitAmount >= 0)
{ {
int count = (int) ent.getWorld().getEntities().stream() int count = (int) ent.getWorld().getEntitiesByClasses(ent.getClass()).stream()
.filter(e -> e.getType().equals(ent.getType())) .filter(e -> island.inIslandSpace(e.getLocation()))
.filter(e -> island.inIslandSpace(e.getLocation())).count(); .count();
if (count >= limitAmount) if (count > limitAmount)
return new AtLimitResult(ent.getType(), limitAmount); return new AtLimitResult(ent.getType(), limitAmount);
} }
@ -233,7 +279,7 @@ public class EntityLimitListener implements Listener {
int count = (int) ent.getWorld().getEntities().stream() int count = (int) ent.getWorld().getEntities().stream()
.filter(e -> group.getKey().contains(e.getType())) .filter(e -> group.getKey().contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count(); .filter(e -> island.inIslandSpace(e.getLocation())).count();
if (count >= group.getValue()) if (count > group.getValue())
return new AtLimitResult(group.getKey(), group.getValue()); return new AtLimitResult(group.getKey(), group.getValue());
} }
return new AtLimitResult(); return new AtLimitResult();

View File

@ -8,30 +8,12 @@ authors: tastybento
softdepend: AcidIsland, BSkyBlock, CaveBlock softdepend: AcidIsland, BSkyBlock, CaveBlock
permissions: permissions:
acidisland.limits.player.limits: '[gamemode].limits.player.limits':
description: Player can use limits command description: Player can use limits command
default: true default: true
acidisland.limits.player.recount: '[gamemode].limits.player.recount':
description: Player can use recount command description: Player can use recount command
default: true default: true
acidisland.limits.admin.limits: '[gamemode].limits.admin.limits':
description: Player can use admin limits command
default: op
bskyblock.limits.player.limits:
description: Player can use limits command
default: true
bskyblock.limits.player.recount:
description: Player can use recount command
default: true
bskyblock.limits.admin.limits:
description: Player can use admin limits command
default: op
caveblock.limits.player.limits:
description: Player can use limits command
default: true
caveblock.limits.player.recount:
description: Player can use recount command
default: true
caveblock.limits.admin.limits:
description: Player can use admin limits command description: Player can use admin limits command
default: op default: op