Java Code Examples for org.bukkit.World#spawn()

The following examples show how to use org.bukkit.World#spawn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TNTMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void handleInstantActivation(BlockPlaceEvent event) {
  if (this.properties.instantIgnite && event.getBlock().getType() == Material.TNT) {
    World world = event.getBlock().getWorld();
    TNTPrimed tnt =
        world.spawn(
            event.getBlock().getLocation().clone().add(new Location(world, 0.5, 0.5, 0.5)),
            TNTPrimed.class);

    if (this.properties.fuse != null) {
      tnt.setFuseTicks(this.getFuseTicks());
    }

    if (this.properties.power != null) {
      tnt.setYield(this.properties.power); // Note: not related to EntityExplodeEvent.yield
    }

    if (callPrimeEvent(tnt, event.getPlayer())) {
      event.setCancelled(true); // Allow the block to be placed if priming is cancelled
      world.playSound(tnt.getLocation(), Sound.FUSE, 1, 1);

      ItemStack inHand = event.getPlayer().getItemInHand();
      if (inHand.getAmount() == 1) {
        event.getPlayer().setItemInHand(null);
      } else {
        inHand.setAmount(inHand.getAmount() - 1);
      }
    }
  }
}
 
Example 2
Source File: TNTMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void handleInstantActivation(BlockPlaceEvent event) {
    if(this.properties.instantIgnite && event.getBlock().getType() == Material.TNT) {
        World world = event.getBlock().getWorld();
        TNTPrimed tnt = world.spawn(BlockUtils.base(event.getBlock()), TNTPrimed.class);

        if(this.properties.fuse != null) {
            tnt.setFuseTicks(this.getFuseTicks());
        }

        if(this.properties.power != null) {
            tnt.setYield(this.properties.power); // Note: not related to EntityExplodeEvent.yield
        }

        if(callPrimeEvent(tnt, event.getPlayer(), true)) {
            // Only cancel the block placement if the prime event is NOT cancelled.
            // If priming is cancelled, the block is allowed to stay (unless some
            // other handler has already cancelled the place event).
            event.setCancelled(true);
            world.playSound(tnt.getLocation(), Sound.ENTITY_TNT_PRIMED, 1, 1);

            ItemStack inHand = event.getItemInHand();
            if(inHand.getAmount() == 1) {
                inHand = null;
            } else {
                inHand.setAmount(inHand.getAmount() - 1);
            }
            event.getPlayer().getInventory().setItem(event.getHand(), inHand);
        }
    }
}
 
Example 3
Source File: CommonCustomMob.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public void dropItems() {
	try {
		if (entity == null) {
			return;
		}
		
		LinkedList<MobDrop> dropped = getRandomDrops();
		World world = entity.getBukkitEntity().getWorld();
		Location loc = getLocation(entity);
		
		for (MobDrop d : dropped) {
			ItemStack stack;
			if (d.isVanillaDrop) {
				stack = ItemManager.createItemStack(d.vanillaType, 1, d.vanillaData);
			} else {
				LoreCraftableMaterial craftMat = LoreCraftableMaterial.getCraftMaterialFromId(d.craftMatId);
				stack = LoreCraftableMaterial.spawn(craftMat);
			}
			
			world.dropItem(loc, stack);
		}
		
		if (this.coinMax != 0 && this.coinMin != 0) {
			ExperienceOrb orb = (ExperienceOrb)world.spawn(loc, ExperienceOrb.class);
			Random random = new Random();
			int coins = random.nextInt(this.coinMax - this.coinMin) + this.coinMin;
			orb.setExperience(coins);

		}
	} catch(Exception e) {
		e.printStackTrace();
	}
}
 
Example 4
Source File: ModifyBowProjectileMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void changeBowProjectile(EntityShootBowEvent event) {
  Plugin plugin = PGM.get();
  Entity newProjectile;

  if (this.cls == Arrow.class && event.getProjectile() instanceof Arrow) {
    // Don't change the projectile if it's an Arrow and the custom entity type is also Arrow
    newProjectile = event.getProjectile();
  } else {
    // Replace the projectile
    Projectile oldEntity = (Projectile) event.getProjectile();
    if (Projectile.class.isAssignableFrom(this.cls)) {
      newProjectile = event.getEntity().launchProjectile((Class<? extends Projectile>) this.cls);
    } else {
      World world = event.getEntity().getWorld();
      newProjectile = world.spawn(oldEntity.getLocation(), this.cls);
    }
    event.setProjectile(newProjectile);

    // Copy some things from the old projectile
    newProjectile.setVelocity(oldEntity.getVelocity());
    newProjectile.setFallDistance(oldEntity.getFallDistance());
    newProjectile.setFireTicks(oldEntity.getFireTicks());

    if (newProjectile instanceof Projectile) {
      ((Projectile) newProjectile).setShooter(oldEntity.getShooter());
      ((Projectile) newProjectile).setBounce(oldEntity.doesBounce());
    }

    // Save some special properties of Arrows
    if (oldEntity instanceof Arrow) {
      Arrow arrow = (Arrow) oldEntity;
      newProjectile.setMetadata("critical", new FixedMetadataValue(plugin, arrow.isCritical()));
      newProjectile.setMetadata(
          "knockback", new FixedMetadataValue(plugin, arrow.getKnockbackStrength()));
      newProjectile.setMetadata(
          "damage", new FixedMetadataValue(plugin, arrow.spigot().getDamage()));
    }
  }

  // Tag the projectile as custom
  newProjectile.setMetadata("customProjectile", new FixedMetadataValue(plugin, true));

  match.callEvent(new EntityLaunchEvent(newProjectile, event.getEntity()));
}
 
Example 5
Source File: ModifyBowProjectileMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void changeBowProjectile(EntityShootBowEvent event) {
    Plugin plugin = this.getMatch().getPlugin();
    Entity newProjectile;

    if(this.projectile == Arrow.class && event.getProjectile() instanceof Arrow) {
        // Don't change the projectile if it's an Arrow and the custom entity type is also Arrow
        newProjectile = event.getProjectile();
    } else {
        // Replace the projectile
        World world = event.getEntity().getWorld();
        Projectile oldEntity = (Projectile) event.getProjectile();

        if(Projectile.class.isAssignableFrom(projectile)) {
            // Ender Pearls need to be soawned this way, otherwise they will hit the shooter sometimes
            newProjectile = event.getEntity().launchProjectile(projectile.asSubclass(Projectile.class), oldEntity.getVelocity());
        } else {
            newProjectile = world.spawn(oldEntity.getLocation(), projectile);
            newProjectile.setVelocity(oldEntity.getVelocity());
        }

        event.setProjectile(newProjectile);

        // Copy some things from the old projectile
        newProjectile.setFallDistance(oldEntity.getFallDistance());
        newProjectile.setFireTicks(oldEntity.getFireTicks());

        if(newProjectile instanceof Projectile) {
            ((Projectile) newProjectile).setShooter(oldEntity.getShooter());
            ((Projectile) newProjectile).setBounce(oldEntity.doesBounce());
        }

        // Save some special properties of Arrows
        if(oldEntity instanceof Arrow) {
            Arrow arrow = (Arrow) oldEntity;
            newProjectile.setMetadata("critical", new FixedMetadataValue(plugin, arrow.isCritical()));
            newProjectile.setMetadata("knockback", new FixedMetadataValue(plugin, arrow.getKnockbackStrength()));
            newProjectile.setMetadata("damage", new FixedMetadataValue(plugin, arrow.getDamage()));
        }
    }

    // Tag the projectile as custom
    newProjectile.setMetadata("customProjectile", new FixedMetadataValue(plugin, true));

    getMatch().callEvent(new EntityLaunchEvent(newProjectile, event.getEntity()));
}
 
Example 6
Source File: ExpCapListener.java    From NyaaUtils with MIT License 4 votes vote down vote up
@EventHandler
public void onExpCapHit(ExpBottleEvent event) {
    if (event.getEntity().hasMetadata("nu_expcap_exp")) {
        List<MetadataValue> nu_expcap_exp = event.getEntity().getMetadata("nu_expcap_exp");
        MetadataValue metadataValue = nu_expcap_exp.get(0);
        if (metadataValue == null) {
            return;
        }
        int exp = metadataValue.asInt();
        if (exp <= 0)return;
        //生成的经验球数量大于1,小于总经验数
        int maxOrbAmount = Math.min(exp,cfg.expcap_max_orb_amount);
        int minOrbAmount = Math.max(1, ((NyaaUtils) plugin).cfg.expcap_min_orb_amount);
        int orbAmount = Math.min(Math.max(exp, minOrbAmount), maxOrbAmount);
        Location location = event.getEntity().getLocation();
        int expPerOrb = exp / orbAmount;
        int delay = 0;
        int step = Math.max(cfg.expcap_orb_ticksBetweenSpawn,0);
        //整形除法可能导致实际生成经验量偏少
        int spawnedExp = orbAmount * expPerOrb;
        int remain = exp - spawnedExp;
        final World world = location.getWorld();
        if (world == null) return;
        world.spawn(location, ExperienceOrb.class, experienceOrb -> {
            experienceOrb.setExperience(remain);
        });

        for (int i = 0; i < orbAmount; i++) {
            Bukkit.getScheduler().runTaskLater(plugin, ()->{
                Location add = null;
                for (int j = 0; j < 5; j++) {
                    Location clone = location.clone();
                    double dx = random.nextDouble() * 6;
                    double dy = random.nextDouble() * 3;
                    double dz = random.nextDouble() * 6;
                    dx -= 3;
                    dz -= 3;
                    add = clone.add(new Vector(dx, dy, dz));
                    if (!world.getBlockAt(location).getType().isSolid())break;
                }
                if (!world.getBlockAt(location).getType().isSolid()) add = location;
                world.spawn(add, ExperienceOrb.class, experienceOrb -> {
                    experienceOrb.setExperience(expPerOrb);
                });
            },delay+=step);
        }
    }
}
 
Example 7
Source File: FireworkEffectPlayer.java    From civcraft with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Play a pretty firework at the location with the FireworkEffect when called
 * @param world
 * @param loc
 * @param fe
 * @throws Exception
 */
public void playFirework(World world, Location loc, FireworkEffect fe) throws Exception {
    // Bukkity load (CraftFirework)
    Firework fw = (Firework) world.spawn(loc, Firework.class);
    // the net.minecraft.server.World
    Object nms_world = null;
    Object nms_firework = null;
    /*
     * The reflection part, this gives us access to funky ways of messing around with things
     */
    if(world_getHandle == null) {
        // get the methods of the craftbukkit objects
        world_getHandle = getMethod(world.getClass(), "getHandle");
        firework_getHandle = getMethod(fw.getClass(), "getHandle");
    }
    // invoke with no arguments
    nms_world = world_getHandle.invoke(world, (Object[]) null);
    nms_firework = firework_getHandle.invoke(fw, (Object[]) null);
    // null checks are fast, so having this seperate is ok
    if(nms_world_broadcastEntityEffect == null) {
        // get the method of the nms_world
        nms_world_broadcastEntityEffect = getMethod(nms_world.getClass(), "broadcastEntityEffect");
    }
    /*
     * Now we mess with the metadata, allowing nice clean spawning of a pretty firework (look, pretty lights!)
     */
    // metadata load
    FireworkMeta data = (FireworkMeta) fw.getFireworkMeta();
    // clear existing
    data.clearEffects();
    // power of one
    data.setPower(1);
    // add the effect
    data.addEffect(fe);
    // set the meta
    fw.setFireworkMeta(data);
    /*
     * Finally, we broadcast the entity effect then kill our fireworks object
     */
    // invoke with arguments
    nms_world_broadcastEntityEffect.invoke(nms_world, new Object[] {nms_firework, (byte) 17});
    // remove from the game
   // fw.
    fw.remove();
}