From 0fc7586df3c5616b929f6eb1e4554cabd3ba549a Mon Sep 17 00:00:00 2001 From: SydMontague Date: Sat, 15 Feb 2020 17:03:56 +0100 Subject: [PATCH] Added ImagePlaceEvent --- .../de/craftlancer/imagemaps/ImageMaps.java | 10 ++- .../imagemaps/ImagePlaceEvent.java | 74 +++++++++++++++++++ .../imagemaps/PlacingCacheEntry.java | 6 +- 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/craftlancer/imagemaps/ImagePlaceEvent.java diff --git a/src/main/java/de/craftlancer/imagemaps/ImageMaps.java b/src/main/java/de/craftlancer/imagemaps/ImageMaps.java index adffe92..cb01e45 100644 --- a/src/main/java/de/craftlancer/imagemaps/ImageMaps.java +++ b/src/main/java/de/craftlancer/imagemaps/ImageMaps.java @@ -15,6 +15,7 @@ import java.util.logging.Level; import javax.imageio.ImageIO; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.block.Block; @@ -79,7 +80,7 @@ public class ImageMaps extends JavaPlugin implements Listener { placing.put(p.getUniqueId(), new PlacingCacheEntry(image, fastsend, scale)); } - public boolean placeImage(Block block, BlockFace face, PlacingCacheEntry cache) { + public boolean placeImage(Player player, Block block, BlockFace face, PlacingCacheEntry cache) { int xMod = 0; int zMod = 0; @@ -113,6 +114,11 @@ public class ImageMaps extends JavaPlugin implements Listener { int width = (int) Math.ceil((double) image.getWidth() / (double) MAP_WIDTH * cache.getScale() - 0.0001); int height = (int) Math.ceil((double) image.getHeight() / (double) MAP_HEIGHT * cache.getScale() - 0.0001); + ImagePlaceEvent event = new ImagePlaceEvent(player, block, face, width, height, cache); + Bukkit.getPluginManager().callEvent(event); + if(event.isCancelled()) + return false; + for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) { if (!block.getRelative(x * xMod, -y, x * zMod).getType().isSolid()) @@ -151,7 +157,7 @@ public class ImageMaps extends JavaPlugin implements Listener { if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; - if (!placeImage(e.getClickedBlock(), e.getBlockFace(), placing.get(e.getPlayer().getUniqueId()))) + if (!placeImage(e.getPlayer(), e.getClickedBlock(), e.getBlockFace(), placing.get(e.getPlayer().getUniqueId()))) e.getPlayer().sendMessage(ChatColor.RED + "Can't place the image here!\nMake sure the area is large enough, unobstructed and without pre-existing hanging entities."); else saveMaps(); diff --git a/src/main/java/de/craftlancer/imagemaps/ImagePlaceEvent.java b/src/main/java/de/craftlancer/imagemaps/ImagePlaceEvent.java new file mode 100644 index 0000000..00cacc2 --- /dev/null +++ b/src/main/java/de/craftlancer/imagemaps/ImagePlaceEvent.java @@ -0,0 +1,74 @@ +package de.craftlancer.imagemaps; + +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ImagePlaceEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private final Player player; + private final Block block; + private final BlockFace face; + private final int width; + private final int height; + private final PlacingCacheEntry cache; + + private boolean cancelled; + + public ImagePlaceEvent(Player player, Block block, BlockFace face, int width, int height, PlacingCacheEntry cache) { + this.player = player; + this.block = block; + this.face = face; + this.width = width; + this.height = height; + this.cache = cache; + } + + public Player getPlayer() { + return player; + } + + public Block getBlock() { + return block; + } + + public BlockFace getFace() { + return face; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public PlacingCacheEntry getCacheEntry() { + return cache; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/de/craftlancer/imagemaps/PlacingCacheEntry.java b/src/main/java/de/craftlancer/imagemaps/PlacingCacheEntry.java index 15b0a45..0b725f4 100644 --- a/src/main/java/de/craftlancer/imagemaps/PlacingCacheEntry.java +++ b/src/main/java/de/craftlancer/imagemaps/PlacingCacheEntry.java @@ -2,9 +2,9 @@ package de.craftlancer.imagemaps; public class PlacingCacheEntry { - private String image; - private boolean fastsend; - private double scale; + private final String image; + private final boolean fastsend; + private final double scale; public PlacingCacheEntry(String image, boolean fastsend, double scale) {