Skip to content

Commit

Permalink
Removed debug
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 26, 2020
1 parent 64b25b0 commit 088f853
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package world.bentobox.limits.listeners;

import java.text.NumberFormat;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand All @@ -23,6 +22,7 @@
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.hanging.HangingPlaceEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent;
import org.bukkit.inventory.ItemStack;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
Expand Down Expand Up @@ -96,7 +96,6 @@ public void onCreatureSpawn(final CreatureSpawnEvent e) {
if (!addon.getPlugin().getIWM().inWorld(e.getLocation())) {
return;
}
long timer = System.nanoTime();
boolean bypass = false;
// Check why it was spawned
switch (e.getSpawnReason()) {
Expand All @@ -117,11 +116,6 @@ public void onCreatureSpawn(final CreatureSpawnEvent e) {
// BentoBox.getInstance().logDebug("Bypass check took " + getTime(timer));
// Tag the entity with the island spawn location
checkLimit(e, bypass);
// BentoBox.getInstance().logDebug("Creature spawn event total = " + getTime(timer));
}

private String getTime(long timer) {
return NumberFormat.getNumberInstance(Locale.US).format(((double)(System.nanoTime() - timer) / 1000000)) + "ms";
}

private boolean checkByPass(Location l) {
Expand Down Expand Up @@ -174,8 +168,9 @@ private void checkLimit(CreatureSpawnEvent e, boolean bypass) {
//// BentoBox.getInstance().logDebug("Entity limit hit");
// Not allowed
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> {
//e.setCancelled(true);
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
tellPlayers(e, res);
});
Expand All @@ -186,13 +181,45 @@ private void checkLimit(CreatureSpawnEvent e, boolean bypass) {

}

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();
if (w == null) return;
long localTime = System.nanoTime();
for (Entity ent : w.getNearbyEntities(e.getLocation(), 5, 5, 5)) {
if (ent instanceof Player) {
if (res.getTypelimit() != null) {
Expand All @@ -206,7 +233,6 @@ private void tellPlayers(CreatureSpawnEvent e, AtLimitResult res) {
}
}
}
// BentoBox.getInstance().logDebug("Notification to players took " + getTime(localTime));
}

}
Expand All @@ -218,7 +244,6 @@ private void tellPlayers(CreatureSpawnEvent e, AtLimitResult res) {
* @return true if at the limit, false if not
*/
private AtLimitResult atLimit(Island island, Entity ent) {
long localTime = System.nanoTime();
// Check island settings first
int limitAmount = -1;
Map<Settings.EntityGroup, Integer> groupsLimits = new HashMap<>();
Expand All @@ -231,8 +256,6 @@ private AtLimitResult atLimit(Island island, Entity ent) {
groupsLimits.put(def, limit);
});
}
// BentoBox.getInstance().logDebug("Island limits check took " + getTime(localTime));
localTime = System.nanoTime();
// If no island settings then try global settings
if (limitAmount < 0 && addon.getSettings().getLimits().containsKey(ent.getType())) {
limitAmount = addon.getSettings().getLimits().get(ent.getType());
Expand All @@ -242,19 +265,14 @@ private AtLimitResult atLimit(Island island, Entity ent) {
.filter(group -> !groupsLimits.containsKey(group) || groupsLimits.get(group) > group.getLimit())
.forEach(group -> groupsLimits.put(group, group.getLimit()));
}
// BentoBox.getInstance().logDebug("Global limits check took " + getTime(localTime));
localTime = System.nanoTime();
if (limitAmount < 0 && groupsLimits.isEmpty()) return new AtLimitResult();
// We have to count the entities
if (limitAmount >= 0)
{
int count = (int) ent.getWorld().getEntitiesByClasses(ent.getClass()).stream()
//.filter(e -> e.getType().equals(ent.getType()))
.filter(e -> island.inIslandSpace(e.getLocation()))
.count();
// BentoBox.getInstance().logDebug("Island entity count took " + getTime(localTime));
localTime = System.nanoTime();
if (count >= limitAmount)
if (count > limitAmount)
return new AtLimitResult(ent.getType(), limitAmount);
}

Expand All @@ -265,12 +283,9 @@ private AtLimitResult atLimit(Island island, Entity ent) {
int count = (int) ent.getWorld().getEntities().stream()
.filter(e -> group.getKey().contains(e.getType()))
.filter(e -> island.inIslandSpace(e.getLocation())).count();
// BentoBox.getInstance().logDebug("Island group limits check took " + getTime(localTime));
localTime = System.nanoTime();
if (count >= group.getValue())
if (count > group.getValue())
return new AtLimitResult(group.getKey(), group.getValue());
}
// BentoBox.getInstance().logDebug("Island limits to find no limits took " + getTime(localTime));
return new AtLimitResult();
}

Expand Down
24 changes: 3 additions & 21 deletions src/main/resources/addon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,12 @@ authors: tastybento
softdepend: AcidIsland, BSkyBlock, CaveBlock

permissions:
acidisland.limits.player.limits:
'[gamemode].limits.player.limits':
description: Player can use limits command
default: true
acidisland.limits.player.recount:
'[gamemode].limits.player.recount':
description: Player can use recount command
default: true
acidisland.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:
'[gamemode].limits.admin.limits':
description: Player can use admin limits command
default: op

0 comments on commit 088f853

Please sign in to comment.