下面列出了怎么用org.bukkit.entity.Wither的API类实例代码及写法,或者点击链接到github查看源代码。
public static Object armorstandCreation(Location location, Entity entity, ServerMob serverMob) {
try {
Object armorstand = entity.getWorld().spawnEntity(entity.getLocation()
.clone()
.add(0,
((LivingEntity) entity).getEyeHeight() - (entity instanceof Wither ? 0.15 : 0.3),
0), EntityType.valueOf("ARMOR_STAND"));
armorstand.getClass().getMethod("setVisible", boolean.class).invoke(armorstand, false);
armorstand.getClass().getMethod("setCustomNameVisible", boolean.class).invoke(armorstand, true);
armorstand.getClass().getMethod("setGravity", boolean.class).invoke(armorstand, false);
armorstand.getClass().getMethod("setBasePlate", boolean.class).invoke(armorstand, false);
armorstand.getClass().getMethod("setSmall", boolean.class).invoke(armorstand, true);
armorstand.getClass().getMethod("setCanPickupItems", boolean.class).invoke(armorstand, false);
return armorstand;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
public Relation compare(final DamageCause dc, final EntityData e) {
switch (dc) {
case ENTITY_ATTACK:
return Relation.get(e.isSupertypeOf(EntityData.fromClass(Entity.class)));
case PROJECTILE:
return Relation.get(e.isSupertypeOf(EntityData.fromClass(Projectile.class)));
case WITHER:
return Relation.get(e.isSupertypeOf(EntityData.fromClass(Wither.class)));
case FALLING_BLOCK:
return Relation.get(e.isSupertypeOf(EntityData.fromClass(FallingBlock.class)));
//$CASES-OMITTED$
default:
return Relation.NOT_EQUAL;
}
}
@EventHandler(ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event) {
if (event == null || !plugin.getWorldManager().isSkyAssociatedWorld(event.getLocation().getWorld())) {
return; // Bail out, we don't care
}
if (!event.isCancelled() && ADMIN_INITIATED.contains(event.getSpawnReason())) {
return; // Allow it, the above method would have blocked it if it should be blocked.
}
checkLimits(event, event.getEntity().getType(), event.getLocation());
if (event.getEntity() instanceof WaterMob) {
Location loc = event.getLocation();
if (isDeepOceanBiome(loc) && isPrismarineRoof(loc)) {
loc.getWorld().spawnEntity(loc, EntityType.GUARDIAN);
event.setCancelled(true);
}
}
if (event.getSpawnReason() == CreatureSpawnEvent.SpawnReason.BUILD_WITHER && event.getEntity() instanceof Wither) {
IslandInfo islandInfo = plugin.getIslandInfo(event.getLocation());
if (islandInfo != null && islandInfo.getLeader() != null) {
event.getEntity().setCustomName(I18nUtil.tr("{0}''s Wither", islandInfo.getLeader()));
event.getEntity().setMetadata("fromIsland", new FixedMetadataValue(plugin, islandInfo.getName()));
}
}
}
@EventHandler(ignoreCancelled = true)
public void onTargeting(EntityTargetLivingEntityEvent e) {
if (!witherEnabled || !plugin.getWorldManager().isSkyAssociatedWorld(e.getEntity().getWorld())) {
return;
}
if (e.getEntity() instanceof Wither && e.getTarget() != null) {
handleWitherRampage(e, (Wither) e.getEntity(), e.getTarget().getLocation());
}
}
@EventHandler
public void onWitherSkullExplosion(EntityDamageByEntityEvent e) {
if (!witherEnabled
|| !(e.getEntity() instanceof WitherSkull)
|| !plugin.getWorldManager().isSkyAssociatedWorld(e.getEntity().getWorld())) {
return;
}
// Find owner
ProjectileSource shooter = ((WitherSkull) e.getEntity()).getShooter();
if (shooter instanceof Wither) {
handleWitherRampage(e, (Wither) shooter, e.getDamager().getLocation());
}
}
private void handleWitherRampage(Cancellable e, Wither shooter, Location targetLocation) {
String islandName = getOwningIsland(shooter);
String targetIsland = WorldGuardHandler.getIslandNameAt(targetLocation);
if (targetIsland == null || !targetIsland.equals(islandName)) {
e.setCancelled(true);
checkWitherLeash(shooter, islandName);
}
}
private void checkWitherLeash(Wither shooter, String islandName) {
String currentIsland = WorldGuardHandler.getIslandNameAt(shooter.getLocation());
if (currentIsland == null || !currentIsland.equals(islandName)) {
shooter.remove();
IslandInfo islandInfo = plugin.getIslandInfo(islandName);
if (islandInfo != null) {
islandInfo.sendMessageToOnlineMembers(I18nUtil.tr("\u00a7cWither Despawned!\u00a7e It wandered too far from your island."));
}
}
}
private String getOwningIsland(Wither wither) {
if (wither.hasMetadata("fromIsland")) {
return wither.getMetadata("fromIsland").get(0).asString();
}
try {
Object[] parse = new MessageFormat(I18nUtil.marktr("{0}''s Wither")).parse(wither.getCustomName());
if (parse != null && parse.length == 1 && parse[0] instanceof String) {
return (String) parse[0];
}
} catch (ParseException e) {
// Ignore
}
return null;
}
@EventHandler(ignoreCancelled = true)
public void onWitherDestroy(EntityChangeBlockEvent e) {
if (e.getEntity().getType() == EntityType.WITHER) {
String id = BlockStorage.checkID(e.getBlock());
if (id != null) {
WitherProof witherproof = SlimefunPlugin.getRegistry().getWitherProofBlocks().get(id);
if (witherproof != null) {
e.setCancelled(true);
witherproof.onAttack(e.getBlock(), (Wither) e.getEntity());
}
}
}
}
@Override
public void onAttack(Block block, Wither wither) {
// In this implementation we simply do nothing.
}
/**
* Deal with pre-explosions
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void WitherExplode(final ExplosionPrimeEvent e) {
if (DEBUG) {
plugin.getLogger().info(e.getEventName());
}
// Only cover withers in the island world
if (!IslandGuard.inWorld(e.getEntity()) || e.getEntity() == null) {
return;
}
// The wither or wither skulls can both blow up
if (e.getEntityType() == EntityType.WITHER) {
//plugin.getLogger().info("DEBUG: Wither");
// Check the location
if (mobSpawnInfo.containsKey(e.getEntity())) {
// We know about this wither
if (DEBUG) {
plugin.getLogger().info("DEBUG: We know about this wither");
}
if (!mobSpawnInfo.get(e.getEntity()).inIslandSpace(e.getEntity().getLocation())) {
// Cancel the explosion
if (DEBUG) {
plugin.getLogger().info("DEBUG: cancelling wither pre-explosion");
}
e.setCancelled(true);
}
}
// Testing only e.setCancelled(true);
}
if (e.getEntityType() == EntityType.WITHER_SKULL) {
//plugin.getLogger().info("DEBUG: Wither skull");
// Get shooter
Projectile projectile = (Projectile)e.getEntity();
if (projectile.getShooter() instanceof Wither) {
//plugin.getLogger().info("DEBUG: shooter is wither");
Wither wither = (Wither)projectile.getShooter();
// Check the location
if (mobSpawnInfo.containsKey(wither)) {
// We know about this wither
if (DEBUG) {
plugin.getLogger().info("DEBUG: We know about this wither");
}
if (!mobSpawnInfo.get(wither).inIslandSpace(e.getEntity().getLocation())) {
// Cancel the explosion
if (DEBUG) {
plugin.getLogger().info("DEBUG: cancel wither skull explosion");
}
e.setCancelled(true);
}
}
}
}
}
/**
* This method is called when a {@link Wither} tried to attack the given {@link Block}.
* You can use this method to play particles or even damage the {@link Wither}.
*
* @param block
* The {@link Block} which was attacked.
* @param wither
* The {@link Wither} who attacked.
*/
void onAttack(Block block, Wither wither);