Hello Sponge Community! I can’t stand Zombie Pigmen spawning in the Nether at all light levels. They get on your tracks, in your fortresses, everywhere! I made this plugin to learn the API and share it with those looking to get a start on plugin development. It could likely use some optimization and efficiency, but it gets the job done!
This plugin is designed to crash zombie pigmen parties and to tell them to stop frolicking. The plugin has three functions presently.
The plugin makes pigmen only spawn at light levels of 7 or lower by removing all pigmen spawns above a light level of 7.
The plugin makes it so that Endermen that pickup blocks do not destroy the blocks they pickup by cancelling the event.
It disables fires started by lightning bolts by clearing the transactions of the event.
I’d maybe replace the e.getCause().contains(EntityTypes.PIG_ZOMBIE) with a check to the entity being spawned instead.
Also checking to see if the cause contains a GroundLuminanceProperty.greaterThan(7) I’m not sure if that’s actually testing the property against the one found in the cause, or checking for equality…
This is just first thoughts after looking at the code, I’d need to run it through a debugger to be sure.
This won’t work. The implementation of Cause#contains(Object) will check if any of its object in a cause chain equal each other. In this case, because greaterThan(7) has a value of 7, it will only match other properties with 7, even if they are greater (due to how the equals function is implemented on DoubleProperty.
@gabizou perhaps the equals function could be implemented on these operator types so that it looks at compareTo rather than the values?
This seems odd - why would you ignore the event if any of the entities spawned are a player? Wouldn’t it be better to wrap the entire thing in a for loop, so that each entity is individually checked and removed if they meet the criteria?
@ZephireNZ@ryantheleach Thanks guys for the critique; I appreciate it! Here is a modification based off what you have said. Does this seem more in line with what you have both suggested?
Checking for whether there are entities.
Then cycling through the event’s entities and checking for WORLD_SPAWNER pigmen.
Needing to acquire the light level.
Cancelling if all conditions met.
@Listener
public void onSpawn (SpawnEntityEvent e) {
if (e.getEntities().isEmpty()) return;
for (Entity entity : e.getEntities()) {
if (entity.getType() == EntityTypes.PIG_ZOMBIE) {
if (e.getCause().contains(SpawnTypes.WORLD_SPANWER)) {
if (/*Light Level*/) {
e.setCancelled(true);
}
}
}
}
}
A little redundant, for loop will just not continue if there are no entities.
I’m not sure if this would be in the cause, best way would be to use SpawnEntityEvent.Spawner instead.
If this event spawned 5 zombie pigmen and a skeleton, then it would cancel the event for all of them, including the skeleton.
In fact, I think best way is to use AffectEntityEvent#filterEntities(Predicate<Entity>) instead of a for loop - that will iterate for each entity, and allow you to return true or false if that entity should spawn.