From 9772081d7b060f1b8880a737f3ac8897fce9b319 Mon Sep 17 00:00:00 2001 From: _OfTeN_ Date: Sun, 7 Nov 2021 14:06:20 +0300 Subject: [PATCH] Implemented spawn events for all types of boss spawns (egg, totem, timer) --- .../bosses/listeners/AutoSpawnTimer.java | 10 +- .../bosses/listeners/SpawnListeners.java | 24 ++- .../events/EcoBossSpawnEggEvent.java | 61 ++++++++ .../ecobosses/events/EcoBossSpawnEvent.java | 143 ++++++++++++++++++ .../events/EcoBossSpawnTimerEvent.java | 46 ++++++ .../events/EcoBossSpawnTotemEvent.java | 61 ++++++++ 6 files changed, 339 insertions(+), 6 deletions(-) create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEggEvent.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEvent.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTimerEvent.java create mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTotemEvent.java diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AutoSpawnTimer.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AutoSpawnTimer.java index 2b95312..8565c5e 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AutoSpawnTimer.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/AutoSpawnTimer.java @@ -3,6 +3,8 @@ package com.willfp.ecobosses.bosses.listeners; import com.willfp.eco.util.NumberUtils; import com.willfp.ecobosses.bosses.EcoBoss; import com.willfp.ecobosses.bosses.EcoBosses; +import com.willfp.ecobosses.events.EcoBossSpawnTimerEvent; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Entity; @@ -49,8 +51,12 @@ public class AutoSpawnTimer implements Runnable { if (tick % boss.getAutoSpawnInterval() == 0) { Location location = locations.get(NumberUtils.randInt(0, locations.size() - 1)); - boss.spawn(location); - boss.setTimeUntilSpawn(boss.getAutoSpawnInterval()); + EcoBossSpawnTimerEvent event = new EcoBossSpawnTimerEvent(boss, location); + Bukkit.getServer().getPluginManager().callEvent(event); + if (!event.isCancelled()) { + boss.spawn(location); + boss.setTimeUntilSpawn(boss.getAutoSpawnInterval()); + } } } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/SpawnListeners.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/SpawnListeners.java index e5915f9..e8507a0 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/SpawnListeners.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/bosses/listeners/SpawnListeners.java @@ -5,6 +5,8 @@ import com.willfp.eco.core.PluginDependent; import com.willfp.ecobosses.bosses.EcoBoss; import com.willfp.ecobosses.bosses.EcoBosses; import com.willfp.ecobosses.bosses.util.obj.SpawnTotem; +import com.willfp.ecobosses.events.EcoBossSpawnEggEvent; +import com.willfp.ecobosses.events.EcoBossSpawnTotemEvent; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.event.Event; @@ -62,16 +64,22 @@ public class SpawnListeners extends PluginDependent implements Listen if (boss.isSpawnTotemEnabled()) { if (!boss.getSpawnTotemDisabledWorldNames().contains(event.getBlock().getWorld().getName().toLowerCase())) { if (boss.getSpawnTotem().equals(placedTotem)) { + if (!boss.areRequirementsMet(event.getPlayer())) { event.getPlayer().sendMessage(this.getPlugin().getLangYml().getMessage("requirements-not-met")); return; } - block1.setType(Material.AIR); - block2.setType(Material.AIR); - block3.setType(Material.AIR); + EcoBossSpawnTotemEvent eggEvent = new EcoBossSpawnTotemEvent(boss, event.getPlayer(), event.getBlock().getLocation(), placedTotem); - boss.spawn(event.getBlock().getLocation()); + this.getPlugin().getServer().getPluginManager().callEvent(eggEvent); + + if (!eggEvent.isCancelled()) { + block1.setType(Material.AIR); + block2.setType(Material.AIR); + block3.setType(Material.AIR); + boss.spawn(event.getBlock().getLocation()); + } } } } @@ -118,6 +126,14 @@ public class SpawnListeners extends PluginDependent implements Listen return; } + EcoBossSpawnEggEvent eggEvent = new EcoBossSpawnEggEvent(boss, event.getPlayer(), event.getClickedBlock().getLocation(), item); + + this.getPlugin().getServer().getPluginManager().callEvent(eggEvent); + + if (eggEvent.isCancelled()) { + return; + } + item.setAmount(item.getAmount() - 1); if (event.getHand() == EquipmentSlot.HAND) { diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEggEvent.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEggEvent.java new file mode 100644 index 0000000..1dbc3cb --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEggEvent.java @@ -0,0 +1,61 @@ +package com.willfp.ecobosses.events; + +import com.willfp.ecobosses.bosses.EcoBoss; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class EcoBossSpawnEggEvent extends EcoBossSpawnEvent { + + /** + * The egg item. + */ + @NotNull + private final ItemStack egg; + + /** + * Bukkit parity. + */ + private static final HandlerList HANDLERS = new HandlerList(); + + /** + * Default constructor + * + * @param boss - The boss to be spawned. + * @param player - The player that spawned this boss (can be null) + * @param location - The location that boss will spawn at. + * @param egg - ItemStack that represents the egg. + */ + public EcoBossSpawnEggEvent(@NotNull EcoBoss boss, @Nullable Player player, @NotNull Location location, @NotNull ItemStack egg) { + super(boss, player, location); + this.egg = egg; + } + + @NotNull + public final ItemStack getEgg() { + return this.egg; + } + + /** + * Bukkit parity. + * + * @return The handlers. + */ + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + + /** + * Bukkit parity. + * + * @return The handler list. + */ + public static HandlerList getHandlerList() { + return HANDLERS; + } + +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEvent.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEvent.java new file mode 100644 index 0000000..58c713c --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnEvent.java @@ -0,0 +1,143 @@ +package com.willfp.ecobosses.events; + +import com.willfp.ecobosses.bosses.EcoBoss; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * This class represents all boss spawn events fired by EcoBosses. + */ +public class EcoBossSpawnEvent extends Event implements Cancellable { + + /** + * Event cancellation state. + */ + private boolean cancelled; + + /** + * The boss to be spawned. + */ + private final EcoBoss boss; + + /** + * Location that boss will be spawned at. + */ + private final Location location; + + /** + * Player that spawned this boss. + * (Will be null if EcoBossSpawnTimerEvent is fired) + */ + @Nullable + private final Player player; + + /** + * Bukkit parity. + */ + private static final HandlerList HANDLERS = new HandlerList(); + + /** + * + * Default constructor + * + * @param boss - The boss to be spawned. + * @param player - The player that spawned this boss (can be null) + * @param location - The location that boss will spawn at. + */ + public EcoBossSpawnEvent(@NotNull final EcoBoss boss, @Nullable final Player player, @NotNull final Location location) { + this.cancelled = false; + this.boss = boss; + this.location = location; + this.player = player; + } + + /** + * + * Get the EcoBoss. + * + * @return - EcoBoss to be spawned. + */ + @NotNull + public EcoBoss getBoss() { + return this.boss; + } + + /** + * + * Get the location. + * + * @return - Location. + */ + @NotNull + public Location getLocation() { + return this.location; + } + + /** + * + * Get the player if present. + * + * @return - The player. + */ + @Nullable + public Player getPlayer() { + return this.player; + } + + /** + * + * Check if this event has any player presented. + * + * @return - If any player is presented in this event. + */ + public boolean hasPlayer() { + return this.player != null; + } + + /** + * Bukkit parity. + * + * @return The handlers. + */ + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + + /** + * + * Get if event is cancelled. + * + * @return Event cancellation state. + */ + @Override + public boolean isCancelled() { + return this.cancelled; + } + + /** + * + * Set event cancellation state. + * + * @param b - The state to set. + */ + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } + + /** + * Bukkit parity. + * + * @return The handler list. + */ + public static HandlerList getHandlerList() { + return HANDLERS; + } + +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTimerEvent.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTimerEvent.java new file mode 100644 index 0000000..4ef7693 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTimerEvent.java @@ -0,0 +1,46 @@ +package com.willfp.ecobosses.events; + +import com.willfp.ecobosses.bosses.EcoBoss; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class EcoBossSpawnTimerEvent extends EcoBossSpawnEvent { + + /** + * Bukkit parity. + */ + private static final HandlerList HANDLERS = new HandlerList(); + + /** + * Default constructor + * + * @param boss - The boss to be spawned. + * @param location - The location that boss will spawn at. + */ + public EcoBossSpawnTimerEvent(@NotNull EcoBoss boss, @NotNull Location location) { + super(boss, null, location); + } + + /** + * Bukkit parity. + * + * @return The handlers. + */ + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + + /** + * Bukkit parity. + * + * @return The handler list. + */ + public static HandlerList getHandlerList() { + return HANDLERS; + } + +} diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTotemEvent.java b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTotemEvent.java new file mode 100644 index 0000000..2587fa8 --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecobosses/events/EcoBossSpawnTotemEvent.java @@ -0,0 +1,61 @@ +package com.willfp.ecobosses.events; + +import com.willfp.ecobosses.bosses.EcoBoss; +import com.willfp.ecobosses.bosses.util.obj.SpawnTotem; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class EcoBossSpawnTotemEvent extends EcoBossSpawnEvent { + + /** + * The totem. + */ + @NotNull + private final SpawnTotem totem; + + /** + * Bukkit parity. + */ + private static final HandlerList HANDLERS = new HandlerList(); + + /** + * Default constructor + * + * @param boss - The boss to be spawned. + * @param player - The player that spawned this boss (can be null) + * @param location - The location that boss will spawn at. + * @param totem - The totem. + */ + public EcoBossSpawnTotemEvent(@NotNull final EcoBoss boss, @Nullable final Player player, @NotNull final Location location, @NotNull final SpawnTotem totem) { + super(boss, player, location); + this.totem = totem; + } + + @NotNull + public SpawnTotem getTotem() { + return this.totem; + } + + /** + * Bukkit parity. + * + * @return The handlers. + */ + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + + /** + * Bukkit parity. + * + * @return The handler list. + */ + public static HandlerList getHandlerList() { + return HANDLERS; + } + +}