Last active
December 21, 2015 23:59
-
-
Save SupaHam/6385703 to your computer and use it in GitHub Desktop.
Listen for firework launch
This file contains 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 com.supaham.fireworkevent; | |
import org.bukkit.entity.Entity; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.Cancellable; | |
import org.bukkit.event.Event; | |
import org.bukkit.event.HandlerList; | |
/** | |
* Called when a firework is launched. | |
* | |
* @author SupaHam | |
* | |
*/ | |
public class FireworkLaunchEvent extends Event implements Cancellable { | |
private Entity entity; | |
private Player player; | |
private boolean cancelled = false; | |
private static final HandlerList handlers = new HandlerList(); | |
public FireworkLaunchEvent(Entity entity, Player player) { | |
this.setEntity(entity); | |
this.player = player; | |
} | |
/** | |
* Gets the entity | |
* | |
* @return the entity | |
*/ | |
public Entity getEntity() { | |
return entity; | |
} | |
/** | |
* Sets the entity. | |
* | |
* @param entity the entity to set | |
*/ | |
public void setEntity(Entity entity) { | |
this.entity = entity; | |
} | |
/** | |
* Gets the player involved in this event. | |
* | |
* @return player that launched the firework. | |
*/ | |
public Player getPlayer() { | |
return player; | |
} | |
@Override | |
public HandlerList getHandlers() { | |
return handlers; | |
} | |
public static HandlerList getHandlerList() { | |
return handlers; | |
} | |
@Override | |
public boolean isCancelled() { | |
return this.cancelled; | |
} | |
@Override | |
public void setCancelled(boolean cancel) { | |
this.cancelled = cancel; | |
} |
This file contains 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 com.supaham.fireworkevent; | |
import org.bukkit.*; | |
public class FireworkListener extends JavaPlugin implements Listener{ | |
@Override | |
public void onEnable(){ | |
getServer().getPluginManager().registerEvents(this, this); | |
} | |
/** | |
* Listens for fireworks | |
* | |
* @param evt | |
*/ | |
@EventHandler | |
public void onFireworkLaunch(FireworkLaunchEvent evt) { | |
} | |
/** | |
* Handles whether a player launched a firework. | |
* | |
* @param evt | |
*/ | |
@EventHandler | |
public void onPlayerInteract(PlayerInteractEvent evt) { | |
if (evt.getPlayer() != null) { | |
// Whether the player right clicked and the item in their hand was a firework | |
if (evt.getAction() == Action.RIGHT_CLICK_BLOCK) { | |
if (evt.getItem().getType() == Material.FIREWORK) { | |
final Player player = evt.getPlayer(); | |
final Block clickedBlock = evt.getClickedBlock(); | |
final Location clickedLoc = evt.getClickedBlock().getLocation(); | |
getScheduler.scheduleSyncDelayedTask(this, new Runnable() { | |
public void run() { | |
List<Entity> entities = new ArrayList<>(clickedLoc | |
.getWorld().getEntitiesByClasses(Firework.class)); | |
for (Entity entity : entities) { | |
// Check whether the entities are very close to the block | |
// (Possibly where the player clicked) | |
if (isClose(entity, clickedBlock, 2)) { | |
getServer().getPluginManager().callEvent( | |
new FireworkLaunchEvent(entity, player)); | |
} | |
} | |
} | |
}, 0L); | |
} | |
} | |
} | |
} | |
/** | |
* Whether an entity is within {@code radius} from a block. | |
* | |
* @param entity entity to check | |
* @param clickedBlock location to check | |
* @param radius radius to check | |
* @return true if the entity is within the radius of the block, otherwise false. | |
*/ | |
public boolean isClose(Entity entity, Block clickedBlock, double radius) { | |
if (clickedBlock.getLocation().distanceSquared(entity.getLocation()) <= radius) { | |
return true; | |
} else if (clickedBlock.getRelative(BlockFace.DOWN).getLocation() | |
.distanceSquared(entity.getLocation()) <= radius) { | |
return true; | |
} else if (clickedBlock.getRelative(BlockFace.UP).getLocation() | |
.distanceSquared(entity.getLocation()) <= radius) { | |
return true; | |
} else if (clickedBlock.getRelative(BlockFace.EAST).getLocation() | |
.distanceSquared(entity.getLocation()) <= radius) { | |
return true; | |
} else if (clickedBlock.getRelative(BlockFace.WEST).getLocation() | |
.distanceSquared(entity.getLocation()) <= radius) { | |
return true; | |
} else if (clickedBlock.getRelative(BlockFace.NORTH).getLocation() | |
.distanceSquared(entity.getLocation()) <= radius) { | |
return true; | |
} else if (clickedBlock.getRelative(BlockFace.SOUTH).getLocation() | |
.distanceSquared(entity.getLocation()) <= radius) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment