Added an event fired on when a entity spawns.

This commit is contained in:
Brianna O'Keefe 2018-09-22 19:50:25 -04:00
parent f616c4337a
commit f288a88155
2 changed files with 67 additions and 6 deletions

View File

@ -0,0 +1,51 @@
package com.songoda.epicspawners.api.events;
import com.songoda.epicspawners.api.EpicSpawnersAPI;
import com.songoda.epicspawners.api.spawner.Spawner;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityEvent;
/**
* Called when a spawner spawns an entity.
*/
public class SpawnerSpawnEvent extends EntityEvent implements Cancellable {
private static final HandlerList HANDLERS = new HandlerList();
private boolean canceled = false;
private Spawner spawner;
public SpawnerSpawnEvent(Entity entity, Spawner spawner) {
super(entity);
this.spawner = spawner;
}
public Spawner getSpawner() {
return spawner;
}
public HandlerList getHandlers() {
return HANDLERS;
}
public static HandlerList getHandlerList() {
return HANDLERS;
}
@Override
public boolean isCancelled() {
return canceled;
}
@Override
public void setCancelled(boolean canceled) {
this.canceled = canceled;
}
}

View File

@ -2,6 +2,8 @@ package com.songoda.epicspawners.spawners.spawner.option;
import com.songoda.arconix.plugin.Arconix;
import com.songoda.epicspawners.EpicSpawnersPlugin;
import com.songoda.epicspawners.api.events.SpawnerChangeEvent;
import com.songoda.epicspawners.api.events.SpawnerSpawnEvent;
import com.songoda.epicspawners.api.spawner.Spawner;
import com.songoda.epicspawners.api.spawner.SpawnerData;
import com.songoda.epicspawners.api.spawner.SpawnerStack;
@ -9,6 +11,7 @@ import com.songoda.epicspawners.api.spawner.condition.SpawnCondition;
import com.songoda.epicspawners.spawners.condition.SpawnConditionNearbyEntities;
import com.songoda.epicspawners.utils.Debugger;
import com.songoda.epicspawners.utils.Methods;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -105,13 +108,13 @@ public class SpawnOptionEntity implements SpawnOption {
while (spawnCount-- > 0) {
EntityType type = types[ThreadLocalRandom.current().nextInt(types.length)];
spawnEntity(location, type, data);
spawnEntity(location, type, spawner, data);
spawner.setSpawnCount(spawner.getSpawnCount() + 1);
// TODO: Talk to the author of StackMob to get his ass in gear. lolk (I dropped support, try and add it in later)
}
}
public void spawnEntity(Location location, EntityType type, SpawnerData data) {
private void spawnEntity(Location location, EntityType type, Spawner spawner, SpawnerData data) {
try {
Location spot = null;
@ -153,14 +156,14 @@ public class SpawnOptionEntity implements SpawnOption {
spot.add(spawnX, .5, spawnZ);
spawnFinal(spot, data, type);
spawnFinal(spot, data, spawner, type);
}
} catch (Exception ex) {
Debugger.runReport(ex);
}
}
public boolean canSpawn(SpawnerData data, Location location) {
private boolean canSpawn(SpawnerData data, Location location) {
try {
List<Material> spawnBlocks = Arrays.asList(data.getSpawnBlocks());
@ -182,7 +185,7 @@ public class SpawnOptionEntity implements SpawnOption {
return false;
}
public boolean isWater(Material type) {
private boolean isWater(Material type) {
try {
if (type == Material.WATER) {
return true;
@ -194,12 +197,19 @@ public class SpawnOptionEntity implements SpawnOption {
return false;
}
private boolean spawnFinal(Location location, SpawnerData data, EntityType type) {
private boolean spawnFinal(Location location, SpawnerData data, Spawner spawner, EntityType type) {
World world = location.getWorld();
try {
Entity entity = (Entity) nmsSpawnMethod.invoke(world, location, type.getEntityClass(), null, CreatureSpawnEvent.SpawnReason.SPAWNER); //ToDo: account for all mobs in the spawner.
SpawnerSpawnEvent event = new SpawnerSpawnEvent(entity, spawner);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
entity.remove();
return true;
}
if (data.isSpawnOnFire())
entity.setFireTicks(160);