diff --git a/api/src/main/java/net/momirealms/customfishing/api/event/FishingLootSpawnEvent.java b/api/src/main/java/net/momirealms/customfishing/api/event/FishingLootSpawnEvent.java new file mode 100644 index 00000000..04d5269b --- /dev/null +++ b/api/src/main/java/net/momirealms/customfishing/api/event/FishingLootSpawnEvent.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) <2022> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.momirealms.customfishing.api.event; + +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +public class FishingLootSpawnEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList handlerList = new HandlerList(); + private final Location location; + private final ItemStack itemStack; + private boolean isCancelled; + + public FishingLootSpawnEvent(@NotNull Player who, Location location, ItemStack itemStack) { + super(who); + this.itemStack = itemStack; + this.location = location; + this.isCancelled = false; + } + + @Override + public boolean isCancelled() { + return isCancelled; + } + + @Override + public void setCancelled(boolean cancel) { + isCancelled = cancel; + } + + public Location getLocation() { + return location; + } + + public ItemStack getItemStack() { + return itemStack; + } + + @Override + public @NotNull HandlerList getHandlers() { + return handlerList; + } + + public static HandlerList getHandlerList() { + return handlerList; + } +} diff --git a/build.gradle.kts b/build.gradle.kts index 0cf6edef..2bb63934 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { allprojects { - version = "2.0.6" + version = "2.0.7" apply() apply(plugin = "java") diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java index 1edef21f..22fa3db6 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/fishing/FishingManagerImpl.java @@ -657,12 +657,8 @@ public class FishingManagerImpl implements Listener, FishingManager { } return; } - case ENTITY -> { - plugin.getEntityManager().summonEntity(hook.getLocation(), player.getLocation(), loot); - } - case BLOCK -> { - plugin.getBlockManager().summonBlock(player, hook.getLocation(), player.getLocation(), loot); - } + case ENTITY -> plugin.getEntityManager().summonEntity(hook.getLocation(), player.getLocation(), loot); + case BLOCK -> plugin.getBlockManager().summonBlock(player, hook.getLocation(), player.getLocation(), loot); } doSuccessActions(loot, effect, fishingPreparation, player); } diff --git a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java index 00195058..7bcc4a1b 100644 --- a/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java +++ b/plugin/src/main/java/net/momirealms/customfishing/mechanic/item/ItemManagerImpl.java @@ -23,6 +23,7 @@ import net.momirealms.customfishing.api.CustomFishingPlugin; import net.momirealms.customfishing.api.common.Key; import net.momirealms.customfishing.api.common.Pair; import net.momirealms.customfishing.api.common.Tuple; +import net.momirealms.customfishing.api.event.FishingLootSpawnEvent; import net.momirealms.customfishing.api.manager.ItemManager; import net.momirealms.customfishing.api.manager.RequirementManager; import net.momirealms.customfishing.api.mechanic.GlobalSettings; @@ -424,6 +425,7 @@ public class ItemManagerImpl implements ItemManager, Listener { /** * Drops an item based on the provided loot, applying velocity from a hook location to a player location. + * This method would also trigger FishingLootSpawnEvent * * @param player The player for whom the item is intended. * @param hookLocation The location where the item will initially drop. @@ -441,6 +443,13 @@ public class ItemManagerImpl implements ItemManager, Listener { if (item.getType() == Material.AIR) { return; } + + FishingLootSpawnEvent spawnEvent = new FishingLootSpawnEvent(player, hookLocation, item); + Bukkit.getPluginManager().callEvent(spawnEvent); + if (spawnEvent.isCancelled()) { + return; + } + Entity itemEntity = hookLocation.getWorld().dropItem(hookLocation, item); Vector vector = playerLocation.subtract(hookLocation).toVector().multiply(0.105); vector = vector.setY((vector.getY() + 0.22) * 1.18);