下面列出了怎么用org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason的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;
}
}
}
}
}
@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;
}
}
}
}
}
public Entity spawnEntity(Location loc, EntityType entityType) {
// Cauldron start - handle custom entity spawns from plugins
if (EntityRegistry.entityClassMap.get(entityType.getName()) != null)
{
net.minecraft.entity.Entity entity = null;
entity = getEntity(EntityRegistry.entityClassMap.get(entityType.getName()), world);
if (entity != null)
{
entity.setLocationAndAngles(loc.getX(), loc.getY(), loc.getZ(), 0, 0);
world.addEntity(entity, SpawnReason.CUSTOM);
return entity.getBukkitEntity();
}
}
// Cauldron end
return spawn(loc, entityType.getEntityClass());
}
public static Entity spawn(Location loc, ICustomMob iCustom, String name) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBaseZombie zombie = new MobBaseZombie(mcWorld, iCustom);
if (name != null) {
zombie.setCustomName(name);
zombie.setCustomNameVisible(true);
}
iCustom.setEntity(zombie);
zombie.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(zombie, SpawnReason.CUSTOM);
return zombie;
}
@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);
}
}
@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);
}
}
public Item dropItem(Location loc, ItemStack item) {
Validate.notNull(item, "Cannot drop a Null item.");
Validate.isTrue(item.getTypeId() != 0, "Cannot drop AIR.");
EntityItem entity = new EntityItem(world, loc.getX(), loc.getY(), loc.getZ(), CraftItemStack.asNMSCopy(item));
entity.pickupDelay = 10;
world.spawnEntity(entity, SpawnReason.CUSTOM);
// TODO this is inconsistent with how Entity.getBukkitEntity() works.
// However, this entity is not at the moment backed by a server entity class so it may be left.
return new CraftItem(world.getServer(), entity);
}
public Entity spawnEntity(Location loc, EntityType entityType) {
if (EntityRegistry.entityClassMap.get(entityType.getName()) != null) {
net.minecraft.entity.Entity entity = null;
entity = getEntity(EntityRegistry.entityClassMap.get(entityType.getName()), world);
if (entity != null) {
entity.setLocationAndAngles(loc.getX(), loc.getY(), loc.getZ(), 0, 0);
world.spawnEntity(entity, SpawnReason.CUSTOM);
return entity.getBukkitEntity();
}
}
return spawn(loc, entityType.getEntityClass());
}
public FallingBlock spawnFallingBlock(Location location, org.bukkit.Material material, byte data) throws IllegalArgumentException {
Validate.notNull(location, "Location cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(material.isBlock(), "Material must be a block");
EntityFallingBlock entity = new EntityFallingBlock(world, location.getX(), location.getY(), location.getZ(), CraftMagicNumbers.getBlock(material).getStateFromMeta(data));
entity.ticksExisted = 1;
world.spawnEntity(entity, SpawnReason.CUSTOM);
return (FallingBlock) entity.getBukkitEntity();
}
@SuppressWarnings("unchecked")
public <T extends Entity> T addEntity(net.minecraft.entity.Entity entity, SpawnReason reason, Consumer<T> function) throws IllegalArgumentException {
Preconditions.checkArgument(entity != null, "Cannot spawn null entity");
if (entity instanceof EntityLiving) {
((EntityLiving) entity).onInitialSpawn(getHandle().getDifficultyForLocation(new BlockPos(entity)), null);
}
if (function != null) {
function.accept((T) entity.getBukkitEntity());
}
world.spawnEntity(entity, reason);
return (T) entity.getBukkitEntity();
}
/**
* CreatureSpawnEvent
*/
public static CreatureSpawnEvent callCreatureSpawnEvent(EntityLivingBase entityliving, SpawnReason spawnReason) {
LivingEntity entity = (LivingEntity) entityliving.getBukkitEntity();
CraftServer craftServer = (CraftServer) entity.getServer();
CreatureSpawnEvent event = new CreatureSpawnEvent(entity, spawnReason);
craftServer.getPluginManager().callEvent(event);
return event;
}
@EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
public void onSpawn(CreatureSpawnEvent e) {
LivingEntity entity = e.getEntity();
SpawnReason spawnReason = e.getSpawnReason();
FixedMetadataValue fixedValue = new FixedMetadataValue(this.expansion.getPlugin().getPlugin(), spawnReason);
entity.setMetadata("combatlogx_spawn_reason", fixedValue);
}
private boolean checkMobSpawnReasonDisabled(LivingEntity enemy) {
FileConfiguration config = this.expansion.getConfig("mob-tagger.yml");
List<String> spawnReasonList = config.getStringList("spawn-reason-list");
if(spawnReasonList.contains("*")) return true;
SpawnReason spawnReason = getSpawnReason(enemy);
String spawnReasonName = spawnReason.name();
return spawnReasonList.contains(spawnReasonName);
}
private SpawnReason getSpawnReason(LivingEntity entity) {
if(entity == null) return SpawnReason.DEFAULT;
if(!entity.hasMetadata("combatlogx_spawn_reason")) return SpawnReason.DEFAULT;
List<MetadataValue> spawnReasonValues = entity.getMetadata("combatlogx_spawn_reason");
for(MetadataValue metadataValue : spawnReasonValues) {
Object object = metadataValue.value();
if(!(object instanceof SpawnReason)) continue;
return (SpawnReason) object;
}
return SpawnReason.DEFAULT;
}
public FallingBlock spawnFallingBlock(Location location, org.bukkit.Material material, byte data) throws IllegalArgumentException {
Validate.notNull(location, "Location cannot be null");
Validate.notNull(material, "Material cannot be null");
Validate.isTrue(material.isBlock(), "Material must be a block");
double x = location.getBlockX() + 0.5;
double y = location.getBlockY() + 0.5;
double z = location.getBlockZ() + 0.5;
net.minecraft.entity.item.EntityFallingBlock entity = new net.minecraft.entity.item.EntityFallingBlock(world, x, y, z, net.minecraft.block.Block.getBlockById(material.getId()), data);
entity.field_145812_b = 1; // ticksLived
world.addEntity(entity, SpawnReason.CUSTOM);
return (FallingBlock) entity.getBukkitEntity();
}
public static void logEntitySpawn(World world, Entity entity, SpawnReason spawnReason)
{
if (MinecraftServer.cauldronConfig.entitySpawnLogging.getValue())
{
logInfo("Dim: {0} Spawning ({1}): {2}", world.provider.dimensionId, spawnReason, entity);
logInfo("Dim: {0} Entities Last Tick: {1}", world.provider.dimensionId, world.entitiesTicked);
logInfo("Dim: {0} Tiles Last Tick: {1}", world.provider.dimensionId, world.tilesTicked);
//logInfo("Chunk Is Active: {0}", world.inActiveChunk(entity));
logStack();
}
}
public static Entity spawn(Location loc, String name) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBaseVillager zombie = new MobBaseVillager(mcWorld);
// if (name != null) {
// zombie.setCustomName(name);
// zombie.setCustomNameVisible(true);
// }
zombie.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(zombie, SpawnReason.CUSTOM);
return zombie;
}
public static Entity spawnCustom(Location loc, ICustomMob iCustom) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBasePigZombie pigzombie = new MobBasePigZombie(mcWorld, iCustom);
iCustom.setEntity(pigzombie);
pigzombie.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(pigzombie, SpawnReason.CUSTOM);
return pigzombie;
}
public static Entity spawnCustom(Location loc, ICustomMob iCustom) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBaseZombieGiant zombie = new MobBaseZombieGiant(mcWorld, iCustom);
iCustom.setEntity(zombie);
zombie.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(zombie, SpawnReason.CUSTOM);
return zombie;
}
public static Entity spawnCustom(Location loc, ICustomMob iCustom) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBaseZombie zombie = new MobBaseZombie(mcWorld, iCustom);
iCustom.setEntity(zombie);
zombie.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(zombie, SpawnReason.CUSTOM);
return zombie;
}
public static Entity spawnCustom(Location loc, ICustomMob iCustom) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBaseIronGolem pigzombie = new MobBaseIronGolem(mcWorld, iCustom);
iCustom.setEntity(pigzombie);
pigzombie.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(pigzombie, SpawnReason.CUSTOM);
return pigzombie;
}
public static Entity spawnCustom(Location loc, ICustomMob iCustom) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBaseWitch witch = new MobBaseWitch(mcWorld, iCustom);
iCustom.setEntity(witch);
witch.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(witch, SpawnReason.CUSTOM);
return witch;
}
public static Entity spawnCustom(Location loc, ICustomMob iCustom) {
CraftWorld world = (CraftWorld) loc.getWorld();
World mcWorld = world.getHandle();
MobBaseWither pigzombie = new MobBaseWither(mcWorld, iCustom);
iCustom.setEntity(pigzombie);
pigzombie.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcWorld.addEntity(pigzombie, SpawnReason.CUSTOM);
return pigzombie;
}
@Nullable
@Override
public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state) {
checkNotNull(location);
checkNotNull(state);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
WorldServer worldServer = craftWorld.getHandle();
Entity createdEntity = createEntityFromId(state.getType().getId(), craftWorld.getHandle());
if (createdEntity != null) {
CompoundTag nativeTag = state.getNbtData();
if (nativeTag != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
readTagIntoEntity(tag, createdEntity);
}
createdEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity(createdEntity, SpawnReason.CUSTOM);
return createdEntity.getBukkitEntity();
} else {
return null;
}
}
@Nullable
@Override
public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state) {
checkNotNull(location);
checkNotNull(state);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
WorldServer worldServer = craftWorld.getHandle();
Entity createdEntity = createEntityFromId(state.getType().getId(), craftWorld.getHandle());
if (createdEntity != null) {
CompoundTag nativeTag = state.getNbtData();
if (nativeTag != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
readTagIntoEntity(tag, createdEntity);
}
createdEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity(createdEntity, SpawnReason.CUSTOM);
return createdEntity.getBukkitEntity();
} else {
return null;
}
}
@Nullable
@Override
public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state) {
checkNotNull(location);
checkNotNull(state);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
WorldServer worldServer = craftWorld.getHandle();
Entity createdEntity = createEntityFromId(state.getType().getId(), craftWorld.getHandle());
if (createdEntity != null) {
CompoundTag nativeTag = state.getNbtData();
if (nativeTag != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
readTagIntoEntity(tag, createdEntity);
}
createdEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity(createdEntity, SpawnReason.CUSTOM);
return createdEntity.getBukkitEntity();
} else {
return null;
}
}
@Nullable
@Override
public org.bukkit.entity.Entity createEntity(Location location, BaseEntity state) {
checkNotNull(location);
checkNotNull(state);
CraftWorld craftWorld = ((CraftWorld) location.getWorld());
WorldServer worldServer = craftWorld.getHandle();
Entity createdEntity = createEntityFromId(state.getType().getId(), craftWorld.getHandle());
if (createdEntity != null) {
CompoundTag nativeTag = state.getNbtData();
if (nativeTag != null) {
NBTTagCompound tag = (NBTTagCompound) fromNative(nativeTag);
for (String name : Constants.NO_COPY_ENTITY_NBT_FIELDS) {
tag.remove(name);
}
readTagIntoEntity(tag, createdEntity);
}
createdEntity.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
worldServer.addEntity(createdEntity, SpawnReason.CUSTOM);
return createdEntity.getBukkitEntity();
} else {
return null;
}
}
@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");
}
}
}
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add");
if (validateEntityMethod == null) {
return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM);
}
final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0);
final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0);
if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) {
// This should never happen
nmsEntity.dead = true;
return false;
}
nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity);
nmsWorld.entityList.add(nmsEntity);
try {
validateEntityMethod.invoke(nmsWorld, nmsEntity);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private boolean addEntityToWorld(WorldServer nmsWorld, Entity nmsEntity) {
Validator.isTrue(Bukkit.isPrimaryThread(), "Async entity add");
if (validateEntityMethod == null) {
return nmsWorld.addEntity(nmsEntity, SpawnReason.CUSTOM);
}
final int chunkX = MathHelper.floor(nmsEntity.locX / 16.0);
final int chunkZ = MathHelper.floor(nmsEntity.locZ / 16.0);
if (!nmsWorld.chunkProviderServer.isChunkLoaded(chunkX, chunkZ)) {
// This should never happen
nmsEntity.dead = true;
return false;
}
nmsWorld.getChunkAt(chunkX, chunkZ).a(nmsEntity);
nmsWorld.entityList.add(nmsEntity);
try {
validateEntityMethod.invoke(nmsWorld, nmsEntity);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}