Last active
October 17, 2015 23:42
-
-
Save NeatMonster/bd4c33ab2dfc4e268099 to your computer and use it in GitHub Desktop.
@PYRRH4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package be.pyrrh4.pparticles.specialeffects; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Random; | |
import org.bukkit.Bukkit; | |
import org.bukkit.DyeColor; | |
import org.bukkit.Location; | |
import org.bukkit.Sound; | |
import org.bukkit.entity.EntityType; | |
import org.bukkit.entity.Item; | |
import org.bukkit.entity.Pig; | |
import org.bukkit.entity.Player; | |
import org.bukkit.entity.TNTPrimed; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.entity.EntityDamageByEntityEvent; | |
import org.bukkit.event.entity.EntityExplodeEvent; | |
import org.bukkit.material.Dye; | |
import org.bukkit.scheduler.BukkitRunnable; | |
import org.bukkit.util.Vector; | |
import be.pyrrh4.pparticles.Main; | |
public class FountainPig implements Listener { | |
private static final Random random = new Random(); | |
private final Map<Pig, TNTPrimed> couples = new HashMap<>(); | |
public FountainPig(final Player player, final long duration, final long period) { | |
new BukkitRunnable() { | |
private long remaining = duration; | |
@Override | |
public void run() { | |
if (remaining >= 0L) { | |
final Location location = player.getLocation(); | |
location.add(0D, 1.5D, 0D); | |
final int length = 1 + random.nextInt(15); | |
final Vector velocity = new Vector(); | |
velocity.setX((2f * random.nextFloat() - 1f) * length); | |
velocity.setY(random.nextFloat() * length); | |
velocity.setZ((2f * random.nextFloat() - 1f) * length); | |
final Pig pig = (Pig) player.getWorld().spawnEntity(location, EntityType.PIG); | |
pig.setVelocity(velocity); | |
pig.getWorld().playSound(location, Sound.PIG_IDLE, 1f, 1f); | |
final TNTPrimed tnt = (TNTPrimed) player.getWorld().spawnEntity(location, EntityType.PRIMED_TNT); | |
tnt.setVelocity(velocity); | |
couples.put(pig, tnt); | |
remaining -= period; | |
} else | |
cancel(); | |
} | |
}.runTaskTimer(Main.getInstance(), 0L, period); | |
Bukkit.getPluginManager().registerEvents(this, Main.getInstance()); | |
} | |
@EventHandler(ignoreCancelled = true) | |
public void onDamageByTNT(final EntityDamageByEntityEvent event) { | |
if (event.getDamager().getType() == EntityType.PRIMED_TNT) { | |
final TNTPrimed tnt = (TNTPrimed) event.getDamager(); | |
if (couples.containsValue(tnt)) | |
event.setCancelled(true); | |
} | |
} | |
@EventHandler | |
public void onTNTExplode(final EntityExplodeEvent event) { | |
if (event.getEntityType() == EntityType.PRIMED_TNT) { | |
final TNTPrimed tnt = (TNTPrimed) event.getEntity(); | |
if (couples.containsValue(tnt)) { | |
event.setCancelled(true); | |
tnt.getWorld().playSound(tnt.getLocation(), Sound.EXPLODE, 1f, 1f); | |
final List<Item> items = new ArrayList<>(); | |
for (int i = 0; i < 150 + random.nextInt(150); ++i) { | |
final Dye dye = new Dye(); | |
if (random.nextFloat() < 0.33f) | |
dye.setColor(DyeColor.BLACK); | |
else | |
dye.setColor(DyeColor.WHITE); | |
final Item item = tnt.getWorld().dropItemNaturally(tnt.getLocation(), dye.toItemStack()); | |
final Vector velocity = new Vector(); | |
velocity.setX((2f * random.nextFloat() - 1f) * 5f); | |
velocity.setX(0.5f + random.nextFloat()); | |
velocity.setX((2f * random.nextFloat() - 1f) * 5f); | |
item.setVelocity(velocity); | |
item.setPickupDelay(Integer.MAX_VALUE); | |
items.add(item); | |
} | |
new BukkitRunnable() { | |
@Override | |
public void run() { | |
for (final Item item : items) | |
item.remove(); | |
} | |
}.runTaskLater(Main.getInstance(), 100L); | |
for (final Map.Entry<Pig, TNTPrimed> couple : couples.entrySet()) | |
if (couple.getValue().equals(tnt)) | |
couple.getKey().remove(); | |
couples.values().remove(tnt); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment