Java Code Examples for org.bukkit.entity.TNTPrimed#setVelocity()

The following examples show how to use org.bukkit.entity.TNTPrimed#setVelocity() . 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 ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void dispenserNukes(BlockTransformEvent event) {
    BlockState oldState = event.getOldState();
    if(oldState instanceof Dispenser &&
       this.properties.dispenserNukeLimit > 0 &&
       this.properties.dispenserNukeMultiplier > 0 &&
       event.getCause() instanceof EntityExplodeEvent) {

        EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
        Dispenser dispenser = (Dispenser) oldState;
        int tntLimit = Math.round(this.properties.dispenserNukeLimit / this.properties.dispenserNukeMultiplier);
        int tntCount = 0;

        for(ItemStack stack : dispenser.getInventory().contents()) {
            if(stack != null && stack.getType() == Material.TNT) {
                int transfer = Math.min(stack.getAmount(), tntLimit - tntCount);
                if(transfer > 0) {
                    stack.setAmount(stack.getAmount() - transfer);
                    tntCount += transfer;
                }
            }
        }

        tntCount = (int) Math.ceil(tntCount * this.properties.dispenserNukeMultiplier);

        for(int i = 0; i < tntCount; i++) {
            TNTPrimed tnt = this.getMatch().getWorld().spawn(BlockUtils.base(dispenser), TNTPrimed.class);

            tnt.setFuseTicks(10 + this.getMatch().getRandom().nextInt(10)); // between 0.5 and 1.0 seconds, same as vanilla TNT chaining

            Random random = this.getMatch().getRandom();
            Vector velocity = new Vector(random.nextGaussian(), random.nextGaussian(), random.nextGaussian()); // uniform random direction
            velocity.normalize().multiply(0.5 + 0.5 * random.nextDouble());
            tnt.setVelocity(velocity);

            callPrimeEvent(tnt, explodeEvent.getEntity(), false);
        }
    }
}
 
Example 2
Source File: SelfDestruct.java    From ce with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void effect(Event e, ItemStack item, int level) {
	PlayerDeathEvent event = (PlayerDeathEvent) e;
	for(int i = level; i >= 0; i--) {
	TNTPrimed tnt = (TNTPrimed) event.getEntity().getWorld().spawnEntity(event.getEntity().getLocation(), EntityType.PRIMED_TNT);
	tnt.setFuseTicks(delay);
	tnt.setVelocity(new Vector(Tools.random.nextDouble()*1.5 - 1, Tools.random.nextDouble() * 1.5, Tools.random.nextDouble()*1.5 - 1));
	if(!Main.createExplosions)
		tnt.setMetadata("ce.explosive", new FixedMetadataValue(getPlugin(), null));
	}
}
 
Example 3
Source File: Tnt.java    From CardinalPGM with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onEntityExplode(EntityExplodeEvent event) {
    if (GameHandler.getGameHandler().getMatch().isRunning() && event.getEntity() instanceof TNTPrimed) {
        if (!blockDamage) {
            event.blockList().clear();
        } else if (yield != 0.3){
            event.setYield((float)yield);
        }
        UUID player = TntTracker.getWhoPlaced(event.getEntity());
        for (Block block : event.blockList()) {
            if (block.getState() instanceof Dispenser) {
                Inventory inventory = ((Dispenser) block.getState()).getInventory();
                Location location = block.getLocation();
                double tntCount = 0;
                for (ItemStack itemstack : inventory.getContents()) {
                    if (itemstack != null && itemstack.getType() == Material.TNT) tntCount += itemstack.getAmount() * multiplier;
                    if (tntCount >= limit) {
                        tntCount = limit;
                        break;
                    }
                }
                inventory.remove(Material.TNT);
                if (tntCount > 0) {
                    Random random = new Random();
                    for (double i = tntCount; i > 0; i--) {
                        TNTPrimed tnt = event.getWorld().spawn(location, TNTPrimed.class);
                        Vector velocity = new Vector((1.5 * random.nextDouble()) - 0.75, (1.5 * random.nextDouble()) - 0.75, (1.5 * random.nextDouble()) - 0.75);
                        tnt.setVelocity(velocity);
                        tnt.setFuseTicks(random.nextInt(10) + 10);
                        if (player != null) {
                            tnt.setMetadata("source", new FixedMetadataValue(Cardinal.getInstance(), player));
                        }
                    }
                }
            }
        }
    }
}
 
Example 4
Source File: TNTMatchModule.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void dispenserNukes(BlockTransformEvent event) {
  BlockState oldState = event.getOldState();
  if (oldState instanceof Dispenser
      && this.properties.dispenserNukeLimit > 0
      && this.properties.dispenserNukeMultiplier > 0
      && event.getCause() instanceof EntityExplodeEvent) {

    EntityExplodeEvent explodeEvent = (EntityExplodeEvent) event.getCause();
    Dispenser dispenser = (Dispenser) oldState;
    int tntLimit =
        Math.round(this.properties.dispenserNukeLimit / this.properties.dispenserNukeMultiplier);
    int tntCount = 0;

    ItemStack[] inv = dispenser.getInventory().getContents();
    for (int slot = 0; slot < inv.length; slot++) {
      ItemStack stack = inv[slot];
      if (stack != null && stack.getType() == Material.TNT) {
        int transfer = Math.min(stack.getAmount(), tntLimit - tntCount);
        if (transfer > 0) {
          stack.setAmount(stack.getAmount() - transfer);
          tntCount += transfer;
          dispenser.getInventory().setItem(slot, stack);
        }
      }
    }

    tntCount = (int) Math.ceil(tntCount * this.properties.dispenserNukeMultiplier);

    for (int i = 0; i < tntCount; i++) {
      TNTPrimed tnt = match.getWorld().spawn(dispenser.getLocation(), TNTPrimed.class);

      tnt.setFuseTicks(
          10
              + match
                  .getRandom()
                  .nextInt(10)); // between 0.5 and 1.0 seconds, same as vanilla TNT chaining

      Random random = match.getRandom();
      Vector velocity =
          new Vector(
              random.nextGaussian(),
              random.nextGaussian(),
              random.nextGaussian()); // uniform random direction
      velocity.normalize().multiply(0.5 + 0.5 * random.nextDouble());
      tnt.setVelocity(velocity);

      callPrimeEvent(tnt, explodeEvent.getEntity());
    }
  }
}