下面列出了怎么用org.bukkit.event.entity.CreatureSpawnEvent的API类实例代码及写法,或者点击链接到github查看源代码。
@EventHandler
public void onCreatureSpawn(CreatureSpawnEvent event) {
if (event.isCancelled() || event.getSpawnReason() == SpawnReason.CUSTOM) {
return;
}
for (String gameName : Main.getGameNames()) {
Game game = Main.getGame(gameName);
if (game.getStatus() != GameStatus.DISABLED)
// prevent creature spawn everytime, not just in game
if (/*(game.getStatus() == GameStatus.RUNNING || game.getStatus() == GameStatus.GAME_END_CELEBRATING) &&*/ game.getOriginalOrInheritedPreventSpawningMobs()) {
if (GameCreator.isInArea(event.getLocation(), game.getPos1(), game.getPos2())) {
event.setCancelled(true);
return;
//}
} else /*if (game.getStatus() == GameStatus.WAITING) {*/
if (game.getLobbyWorld() == event.getLocation().getWorld()) {
if (event.getLocation().distanceSquared(game.getLobbySpawn()) <= Math
.pow(Main.getConfigurator().config.getInt("prevent-lobby-spawn-mobs-in-radius"), 2)) {
event.setCancelled(true);
return;
}
}
}
}
}
@Override
public EntityPet spawn(IPet pet, Player owner) {
Location l = owner.getLocation();
PetPreSpawnEvent spawnEvent = new PetPreSpawnEvent(pet, l);
EchoPet.getPlugin().getServer().getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Pet spawn was cancelled externally.");
EchoPet.getManager().removePet(pet, true);
return null;
}
l = spawnEvent.getSpawnLocation();
World mcWorld = ((CraftWorld) l.getWorld()).getHandle();
EntityPet entityPet = (EntityPet) pet.getPetType().getNewEntityPetInstance(mcWorld, pet);
entityPet.setLocation(new Location(mcWorld.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch()));
if (!l.getChunk().isLoaded()) {
l.getChunk().load();
}
if (!mcWorld.addEntity(entityPet, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Failed to spawn pet entity.");
EchoPet.getManager().removePet(pet, true);
} else {
Particle.MAGIC_RUNES.builder().at(l).show();
}
return entityPet;
}
@Override
public EntityPet spawn(IPet pet, Player owner) {
Location l = owner.getLocation();
PetPreSpawnEvent spawnEvent = new PetPreSpawnEvent(pet, l);
EchoPet.getPlugin().getServer().getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Pet spawn was cancelled externally.");
EchoPet.getManager().removePet(pet, true);
return null;
}
l = spawnEvent.getSpawnLocation();
World mcWorld = ((CraftWorld) l.getWorld()).getHandle();
EntityPet entityPet = (EntityPet) pet.getPetType().getNewEntityPetInstance(mcWorld, pet);
entityPet.setLocation(new Location(mcWorld.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch()));
if (!l.getChunk().isLoaded()) {
l.getChunk().load();
}
if (!mcWorld.addEntity(entityPet, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Failed to spawn pet entity.");
EchoPet.getManager().removePet(pet, true);
} else {
Particle.MAGIC_RUNES.builder().at(l).show();
}
return entityPet;
}
@Override
public <T extends ControllableEntityHandle<? extends LivingEntity>> T createEntityHandle(ControllableEntity entity, Location location) {
SafeConstructor<ControllableEntityHandle> entityConstructor = getConstructorFor(entity.getEntityType());
EntityRegistrationEntry oldEntry = GameRegistry.get(IEntityRegistry.class).getDefaultEntryFor(entity.getEntityType());
GameRegistry.get(IEntityRegistry.class).register(new EntityRegistrationEntry(
entity.getEntityType().getName(),
entity.getEntityType().getId(),
entity.getEntityType().getHandleClass()
));
WorldServer worldServer = ((CraftWorld) location.getWorld()).getHandle();
T handle = (T) entityConstructor.getAccessor().invoke(worldServer, entity);
handle.setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity((net.minecraft.server.v1_8_R1.Entity) handle, CreatureSpawnEvent.SpawnReason.CUSTOM);
GameRegistry.get(IEntityRegistry.class).register(oldEntry);
Material beneath = location.getBlock().getRelative(BlockFace.DOWN).getType();
if (beneath.isBlock()) { // What lies beneath
((Entity) handle).onGround = true;
}
return handle;
}
@Override
public boolean makeWaiting(Entity entity, CreatureSpawnEvent.SpawnReason reason){
if(!sm.getCustomConfig().getBoolean("wait-to-stack.enabled")){
return false;
}
if(!sm.getCustomConfig().getStringList("wait-to-stack.entity-types")
.contains(entity.getType().toString())){
return false;
}
if(!sm.getCustomConfig().getStringList("wait-to-stack.spawn-reasons")
.contains(reason.toString())){
return false;
}
int waitingTime = sm.getCustomConfig().getInt("wait-to-stack.wait-time");
StackTools.addWaiting(entity, waitingTime);
return true;
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent e){
LivingEntity entity = e.getEntity();
// IEntityTools before running task
if(!(entity instanceof Mob)){
return;
}
if(sm.getLogic().doSpawnChecks(entity, e.getSpawnReason().toString())){
return;
}
if(sm.getLogic().makeWaiting(entity, e.getSpawnReason())){
return;
}
// BukkitRunnable to delay this, so the needed metadata can be set before attempting to merge.
new SpawnTask(sm, entity).runTask(sm);
}
@Override
public <T extends ControllableEntityHandle<? extends LivingEntity>> T createHandle(ControllableEntity entity, Location location) {
SafeConstructor<ControllableEntityHandle> entityConstructor = new Reflection().reflect(entity.getEntityType().getHandleClass()).getSafeConstructor(World.class, entity.getEntityType().getControllableInterface());
EntityRegistrationEntry oldEntry = GameRegistry.get(IEntityRegistry.class).getDefaultEntryFor(entity.getEntityType());
GameRegistry.get(IEntityRegistry.class).register(new EntityRegistrationEntry(
entity.getEntityType().getName(),
entity.getEntityType().getId(),
entity.getEntityType().getHandleClass()
));
WorldServer worldServer = ((CraftWorld) location.getWorld()).getHandle();
T handle = (T) entityConstructor.getAccessor().invoke(worldServer, entity);
handle.setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity((net.minecraft.server.v1_7_R1.Entity) handle, CreatureSpawnEvent.SpawnReason.CUSTOM);
GameRegistry.get(IEntityRegistry.class).register(oldEntry);
Material beneath = location.getBlock().getRelative(BlockFace.DOWN).getType();
if (beneath.isBlock()) { // What lies beneath
((Entity) handle).onGround = true;
}
return handle;
}
@Override
public EntityPet spawn(IPet pet, Player owner) {
Location l = owner.getLocation();
PetPreSpawnEvent spawnEvent = new PetPreSpawnEvent(pet, l);
EchoPet.getPlugin().getServer().getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Pet spawn was cancelled externally.");
EchoPet.getManager().removePet(pet, true);
return null;
}
l = spawnEvent.getSpawnLocation();
World mcWorld = ((CraftWorld) l.getWorld()).getHandle();
EntityPet entityPet = (EntityPet) pet.getPetType().getNewEntityPetInstance(mcWorld, pet);
entityPet.setLocation(new Location(mcWorld.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch()));
if (!l.getChunk().isLoaded()) {
l.getChunk().load();
}
if (!mcWorld.addEntity(entityPet, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Failed to spawn pet entity.");
EchoPet.getManager().removePet(pet, true);
} else {
Particle.MAGIC_RUNES.builder().at(l).show();
}
return entityPet;
}
@Override
public EntityPet spawn(IPet pet, Player owner) {
Location l = owner.getLocation();
PetPreSpawnEvent spawnEvent = new PetPreSpawnEvent(pet, l);
EchoPet.getPlugin().getServer().getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Pet spawn was cancelled externally.");
EchoPet.getManager().removePet(pet, true);
return null;
}
l = spawnEvent.getSpawnLocation();
World mcWorld = ((CraftWorld) l.getWorld()).getHandle();
EntityPet entityPet = (EntityPet) pet.getPetType().getNewEntityPetInstance(mcWorld, pet);
entityPet.setLocation(new Location(mcWorld.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch()));
if (!l.getChunk().isLoaded()) {
l.getChunk().load();
}
if (!mcWorld.addEntity(entityPet, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Failed to spawn pet entity.");
EchoPet.getManager().removePet(pet, true);
} else {
Particle.MAGIC_RUNES.builder().at(l).show();
}
return entityPet;
}
@Override
public EntityPet spawn(IPet pet, Player owner) {
Location l = owner.getLocation();
PetPreSpawnEvent spawnEvent = new PetPreSpawnEvent(pet, l);
EchoPet.getPlugin().getServer().getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Pet spawn was cancelled externally.");
EchoPet.getManager().removePet(pet, true);
return null;
}
l = spawnEvent.getSpawnLocation();
World mcWorld = ((CraftWorld) l.getWorld()).getHandle();
EntityPet entityPet = (EntityPet) pet.getPetType().getNewEntityPetInstance(mcWorld, pet);
entityPet.setLocation(new Location(mcWorld.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch()));
if (!l.getChunk().isLoaded()) {
l.getChunk().load();
}
if (!mcWorld.addEntity(entityPet, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Failed to spawn pet entity.");
EchoPet.getManager().removePet(pet, true);
} else {
Particle.MAGIC_RUNES.builder().at(l).show();
}
return entityPet;
}
@Override
public EntityPet spawn(IPet pet, Player owner) {
Location l = owner.getLocation();
PetPreSpawnEvent spawnEvent = new PetPreSpawnEvent(pet, l);
EchoPet.getPlugin().getServer().getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Pet spawn was cancelled externally.");
EchoPet.getManager().removePet(pet, true);
return null;
}
l = spawnEvent.getSpawnLocation();
World mcWorld = ((CraftWorld) l.getWorld()).getHandle();
EntityPet entityPet = (EntityPet) pet.getPetType().getNewEntityPetInstance(mcWorld, pet);
entityPet.setLocation(new Location(mcWorld.getWorld(), l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch()));
if (!l.getChunk().isLoaded()) {
l.getChunk().load();
}
if (!mcWorld.addEntity(entityPet, CreatureSpawnEvent.SpawnReason.CUSTOM)) {
owner.sendMessage(EchoPet.getPrefix() + ChatColor.YELLOW + "Failed to spawn pet entity.");
EchoPet.getManager().removePet(pet, true);
} else {
Particle.MAGIC_RUNES.builder().at(l).show();
}
return entityPet;
}
/**
* Track where the mob was created. This will determine its allowable movement zone.
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void mobSpawn(CreatureSpawnEvent e) {
// Only cover withers in the island world
if (!IslandGuard.inWorld(e.getEntity())) {
return;
}
if (!e.getEntityType().equals(EntityType.WITHER) && !e.getEntityType().equals(EntityType.BLAZE) && !e.getEntityType().equals(EntityType.GHAST)) {
return;
}
if (DEBUG) {
plugin.getLogger().info("Flying mobs " + e.getEventName());
}
// Store where this mob originated
Island island = plugin.getGrid().getIslandAt(e.getLocation());
if (island != null) {
if (DEBUG) {
plugin.getLogger().info("DEBUG: Mob spawned on known island - id = " + e.getEntity().getUniqueId());
}
mobSpawnInfo.put(e.getEntity(),island);
} // Else do nothing - maybe an Op spawned it? If so, on their head be it!
}
@Test
public void onPhantomSpawn_notNaturalSpawned() {
when(worldManager.isSkyWorld(any(World.class))).thenReturn(true);
CreatureSpawnEvent event = new CreatureSpawnEvent(getFakePhantom(),
CreatureSpawnEvent.SpawnReason.SPAWNER_EGG);
spawnEvents.setPhantomsInOverworld(false);
spawnEvents.onPhantomSpawn(event);
assertFalse(event.isCancelled());
}
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
net.minecraft.server.v1_13_R1.Chunk nmsChunk = nmsWorld.getChunkAtWorldCoords(nmsEntity.getChunkCoordinates());
if (nmsChunk != null) {
Chunk chunk = nmsChunk.bukkitChunk;
if (!chunk.isLoaded()) {
chunk.load();
plugin.getLogger().info("Loaded chunk (x:" + chunk.getX() + " z:" + chunk.getZ() + ") to spawn a Hologram");
}
}
return nmsWorld.addEntity(nmsEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSlimeSplit(CreatureSpawnEvent event) {
switch (event.getSpawnReason()) {
case SLIME_SPLIT:
Slime parent = splitter.get();
if (parent != null) {
MobInfo info = resolveEntity(parent);
if (info != null) {
entities().trackEntity(event.getEntity(), info);
}
}
break;
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onBedWarsSpawnIsCancelled(CreatureSpawnEvent event) {
if (!event.isCancelled()) {
return;
}
// Fix for uSkyBlock plugin
if (event.getSpawnReason() == SpawnReason.CUSTOM && Main.getInstance().isEntityInGame(event.getEntity())) {
event.setCancelled(false);
}
}
@Override
public <T extends Entity> T spawn(Location location, Class<T> entity, Consumer<T> e) {
if (Version.isAfter(Version.v1_12)) {
return location.getWorld().spawn(location, entity, e::accept);
} else {
Object createEntity = ((CraftWorld) location.getWorld()).createEntity(location, entity);
try {
e.accept((T) ((net.minecraft.server.v1_13_R2.Entity) createEntity).getBukkitEntity());
} catch (Throwable t) {
t.printStackTrace();
}
return ((CraftWorld) location.getWorld()).addEntity((net.minecraft.server.v1_13_R2.Entity) createEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
}
@EventHandler(ignoreCancelled = true)
public void onCreateSpawn(CreatureSpawnEvent e) {
if (placedArmorStandPlayer != null) {
Player player = plugin.getServer().getPlayer(placedArmorStandPlayer);
Entity block = e.getEntity();
placedArmorStandPlayer = null;
if (player != null) {
if (player.getWorld().equals(block.getWorld())
&& player.getLocation().distance(block.getLocation()) <= 5) {
entityCreatedByPlayer(block, player);
}
}
}
}
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event) {
Location location = BukkitUtil.adapt(event.getLocation());
if (manager.isPlotWorld(location)) {
PlotId id = manager.getPlotId(location);
if (id != null) {
event.setCancelled(true);
}
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onSlimeSplit(CreatureSpawnEvent event) {
switch(event.getSpawnReason()) {
case SLIME_SPLIT:
Slime parent = splitter.get();
if(parent != null) {
MobInfo info = resolveEntity(parent);
if(info != null) {
entities().trackEntity(event.getEntity(), info);
}
}
break;
}
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onSkeletonSpawn(final CreatureSpawnEvent e) {
if (DEBUG)
plugin.getLogger().info("DEBUG: " + e.getEventName());
if (!Settings.hackSkeletonSpawners) {
return;
}
if (!hasWitherSkeleton) {
// Only if this type of Entity exists
return;
}
// Check for spawn reason
if (e.getSpawnReason().equals(SpawnReason.SPAWNER) && e.getEntityType().equals(EntityType.SKELETON)) {
if (!Settings.createNether || !Settings.newNether || ASkyBlock.getNetherWorld() == null) {
return;
}
// Check world
if (!e.getLocation().getWorld().equals(ASkyBlock.getNetherWorld())) {
return;
}
if (random.nextDouble() < WITHER_SKELETON_SPAWN_CHANCE) {
if (DEBUG)
plugin.getLogger().info("DEBUG: Wither Skelly spawned");
e.setCancelled(true);
e.getLocation().getWorld().spawnEntity(e.getLocation(), EntityType.WITHER_SKELETON);
} else {
if (DEBUG)
plugin.getLogger().info("DEBUG: Standard Skelly spawned");
}
}
}
@SuppressWarnings("null")
@Override
public boolean check(final Event e) {
if (types == null)
return true;
final Entity en = e instanceof EntityDeathEvent ? ((EntityDeathEvent) e).getEntity() : ((CreatureSpawnEvent) e).getEntity();
for (final EntityData<?> d : types) {
if (d.isInstance(en))
return true;
}
return false;
}
@EventHandler(priority = EventPriority.LOW)
public void registerNaturalEntity(CreatureSpawnEvent event) {
if (event.isCancelled()) return;
if (event.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.NATURAL) ||
event.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.CUSTOM) &&
!ConfigValues.defaultConfig.getBoolean(DefaultConfig.STRICT_SPAWNING_RULES))
registerNaturalEntity(event.getEntity());
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
void onCreatureSpawn(CreatureSpawnEvent event) {
if (nextSpawnLocation == null) return;
if (event.getEntityType() == nextEntityType && event.getLocation().equals(nextSpawnLocation)) {
event.setCancelled(false);
} else {
// this shouldn't normally be reached..
Log.debug("Shopkeeper entity-spawning seems to be out of sync: spawn-force was activated for an entity of type "
+ nextEntityType.name() + " at location " + nextSpawnLocation.toString() + ", but a (different) entity of type "
+ event.getEntityType().name() + " was spawned at location " + event.getLocation().toString() + ".");
}
nextSpawnLocation = null;
nextEntityType = null;
}
@EventHandler(priority = EventPriority.LOWEST)
public void onCreatureSpawn(CreatureSpawnEvent event) {
final LivingEntity livingEntity = event.getEntity();
final int entityId = livingEntity.getEntityId();
EntityIdList.addEntity(livingEntity);
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
public void run() {
if(livingEntity == null || !livingEntity.isValid()) {
EntityIdList.removeEntity(entityId);
}
}
}, 40L);
}
/**
* Comes AFTER the SpawnEvents{@link #onCreatureSpawn(CreatureSpawnEvent)} - so cancelled will have effect
* @param e
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent e) {
if (!spawnEnabled || e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL) {
return;
}
if (!plugin.getWorldManager().isSkyNether(e.getLocation().getWorld())) {
return;
}
if (e.getLocation().getBlockY() > plugin.getWorldManager().getNetherWorld().getMaxHeight()) {
// Block spawning above nether...
e.setCancelled(true);
return;
}
if (e.getEntity() instanceof PigZombie) {
Block block = e.getLocation().getBlock().getRelative(BlockFace.DOWN);
if (isNetherFortressWalkway(block)) {
e.setCancelled(true);
double p = RND.nextDouble();
if (p <= chanceWither && block.getRelative(BlockFace.UP, 3).getType() == Material.AIR) {
WitherSkeleton mob = (WitherSkeleton) e.getLocation().getWorld().spawnEntity(
e.getLocation(), EntityType.WITHER_SKELETON);
mob.getEquipment().setItemInMainHand(new ItemStack(Material.STONE_SWORD, 1));
} else if (p <= chanceWither+chanceBlaze) {
e.getLocation().getWorld().spawnEntity(e.getLocation(), EntityType.BLAZE);
} else if (p <= chanceWither+chanceBlaze+chanceSkeleton) {
e.getLocation().getWorld().spawnEntity(e.getLocation(), EntityType.SKELETON);
} else {
e.setCancelled(false); // Spawn PigZombie
}
}
}
}
public TrashMobEntity(EntityType entityType, Location location, String name) {
super(entityType, location, 1, name, CreatureSpawnEvent.SpawnReason.CUSTOM);
super.setHasCustomHealth(true);
super.getLivingEntity().getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(1);
super.getLivingEntity().setHealth(1);
super.setHasSpecialLoot(false);
}
@Test
public void onPhantomSpawn_notInNetherWorld() {
when(worldManager.isSkyNether(any(World.class))).thenReturn(false);
CreatureSpawnEvent event = new CreatureSpawnEvent(getFakePhantom(),
CreatureSpawnEvent.SpawnReason.NATURAL);
spawnEvents.setPhantomsInNether(false);
spawnEvents.onPhantomSpawn(event);
assertFalse(event.isCancelled());
}
@Override
public boolean spawnEntity(NMSInsentientEntity wrapper, Location l) {
EntityLiving entity = ((NMSEntityInsentientImpl) wrapper).getHandle();
entity.spawnIn(((CraftWorld) l.getWorld()).getHandle());
((LivingEntity) entity.getBukkitEntity()).setCollidable(false);
entity.setLocation(l.getX(), l.getY(), l.getZ(), l.getYaw(), l.getPitch());
if (!l.getChunk().isLoaded()) {
l.getChunk().load();
}
return entity.world.addEntity(entity, CreatureSpawnEvent.SpawnReason.CUSTOM);
}
@Test
public void onPhantomSpawn_notInSkyworld() {
when(worldManager.isSkyWorld(any(World.class))).thenReturn(false);
CreatureSpawnEvent event = new CreatureSpawnEvent(getFakePhantom(),
CreatureSpawnEvent.SpawnReason.NATURAL);
spawnEvents.setPhantomsInOverworld(false);
spawnEvents.onPhantomSpawn(event);
assertFalse(event.isCancelled());
}