Skip to content

Commit

Permalink
CSL 3.0.7
Browse files Browse the repository at this point in the history
* Added configuration to not count entities with a certain metadata (the default causes entities added by the Shopkeepers plugin to be ignored)
* Improved efficiency of CreatureSpawnEvent cancelling slightly
* Players are no longer counted towards chunk totals instead of being skipped during entity removal
* Entities with custom names not resulting from a player using a nametag are no longer counted by preserve-named-entities
  • Loading branch information
Jikoo committed Jan 16, 2016
1 parent 283cc10 commit f4f2864
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import org.bukkit.ChatColor;
Expand All @@ -15,6 +16,7 @@
import org.bukkit.entity.Animals;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.NPC;
import org.bukkit.entity.Player;
Expand All @@ -28,6 +30,8 @@

public class ChunkSpawnerLimiterPlugin extends JavaPlugin {

private List<String> ignoreMetadata, excludedWorlds;

@Override
public void onEnable() {

Expand Down Expand Up @@ -63,6 +67,9 @@ public void onEnable() {
} catch (IOException e) {}
}

ignoreMetadata = getConfig().getStringList("properties.ignore-metadata");
excludedWorlds = getConfig().getStringList("excluded-worlds");

}

@Override
Expand Down Expand Up @@ -94,30 +101,56 @@ private void checkForMissingProperties() {

public boolean checkChunk(Chunk chunk, Entity entity) {
// Stop processing quickly if this world is excluded from limits.
if (getConfig().getStringList("excluded-worlds").contains(chunk.getWorld().getName())) {
if (excludedWorlds.contains(chunk.getWorld().getName())) {
return false;
}

if (entity != null) {
// Quick return conditions for cancelling new spawns
if (entity instanceof HumanEntity) {
return false;
}

for (String metadata : ignoreMetadata) {
if (entity.hasMetadata(metadata)) {
return false;
}
}
}

Entity[] entities = chunk.getEntities();
HashMap<String, ArrayList<Entity>> types = new HashMap<String, ArrayList<Entity>>();

for (int i = entities.length - 1; i >= 0; i--) {
nextChunkEntity: for (int i = entities.length - 1; i >= 0; i--) {
Entity chunkEntity = entities[i];
// Don't include HumanEntities in our summed list at all.
// They're either Players or plugin-added and we probably shouldn't touch them.
if (chunkEntity instanceof HumanEntity) {
continue;
}

String eType = entities[i].getType().name();
String eGroup = getMobGroup(entities[i]);
// Ignore any Entity with listed metadata.
for (String metadata : ignoreMetadata) {
if (chunkEntity.hasMetadata(metadata)) {
continue nextChunkEntity;
}
}

String eType = chunkEntity.getType().name();
String eGroup = getMobGroup(chunkEntity);

if (getConfig().contains("entities." + eType)) {
if (!types.containsKey(eType)) {
types.put(eType, new ArrayList<Entity>());
}
types.get(eType).add(entities[i]);
types.get(eType).add(chunkEntity);
}

if (getConfig().contains("entities." + eGroup)) {
if (!types.containsKey(eGroup)) {
types.put(eGroup, new ArrayList<Entity>());
}
types.get(eGroup).add(entities[i]);
types.get(eGroup).add(chunkEntity);
}
}

Expand Down Expand Up @@ -169,8 +202,7 @@ public boolean checkChunk(Chunk chunk, Entity entity) {
entry.getValue().size() - limit, eType);
for (int i = entities.length - 1; i >= 0; i--) {
if (entities[i] instanceof Player) {
Player p = (Player) entities[i];
p.sendMessage(notification);
((Player) entities[i]).sendMessage(notification);
}
}
}
Expand All @@ -180,7 +212,9 @@ public boolean checkChunk(Chunk chunk, Entity entity) {
int index = entry.getValue().size() - 1;
while (toRemove > 0 && index >= 0) {
Entity toCheck = entry.getValue().get(index);
if (!skipNamed || toCheck.getCustomName() == null) {
if (!skipNamed || toCheck.getCustomName() == null
|| toCheck instanceof LivingEntity
&& ((LivingEntity) toCheck).getRemoveWhenFarAway()) {
toCheck.remove();
--toRemove;
}
Expand All @@ -191,10 +225,6 @@ public boolean checkChunk(Chunk chunk, Entity entity) {
}
index = entry.getValue().size() - toRemove - 1;
for (; index < entry.getValue().size(); index++) {
// don't remove players
if (entry.getValue().get(index) instanceof HumanEntity) {
continue;
}
entry.getValue().get(index).remove();
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ properties:
# Prioritize entities without names over older entities.
preserve-named-entities: true

# Ignore entities with any of the following metadata.
ignore-metadata:
- shopkeeper

# Spawn reasons to cull on.
spawn-reasons:
NATURAL: true
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ChunkSpawnerLimiter
main: com.cyprias.chunkSpawnerLimiter.ChunkSpawnerLimiterPlugin
main: com.cyprias.chunkspawnerlimiter.ChunkSpawnerLimiterPlugin
author: Cyprias
description: Limit entities in a chunk.
version: 3.0.6
version: 3.0.7

0 comments on commit f4f2864

Please sign in to comment.