diff --git a/api/src/main/java/net/momirealms/customcrops/api/action/ActionManager.java b/api/src/main/java/net/momirealms/customcrops/api/action/ActionManager.java index 73be9838..4d970dc0 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/action/ActionManager.java +++ b/api/src/main/java/net/momirealms/customcrops/api/action/ActionManager.java @@ -23,7 +23,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; /** * The ActionManager interface manages custom action types and provides methods for handling actions. diff --git a/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionDropItem.java b/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionDropItem.java index 3172d43e..3cb7a96c 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionDropItem.java +++ b/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionDropItem.java @@ -28,10 +28,13 @@ import net.momirealms.customcrops.api.core.world.CustomCropsChunk; import net.momirealms.customcrops.api.core.world.CustomCropsWorld; import net.momirealms.customcrops.api.core.world.Pos3; +import net.momirealms.customcrops.api.event.DropItemActionEvent; import net.momirealms.customcrops.api.misc.value.MathValue; +import net.momirealms.customcrops.api.util.EventUtils; import net.momirealms.customcrops.api.util.PlayerUtils; import net.momirealms.customcrops.common.util.RandomUtils; import org.bukkit.Location; +import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; @@ -65,19 +68,28 @@ public ActionDropItem( @Override protected void triggerAction(Context context) { Location location = requireNonNull(context.arg(ContextKeys.LOCATION)); - Player player = null; + Player player; if (context.holder() instanceof Player p) { player = p; + } else { + player = null; } int random = RandomUtils.generateRandomInt((int) min.evaluate(context), (int) max.evaluate(context)); ItemStack itemStack = generateItem(location, player, random); - if (itemStack != null) { - if (toInv && player != null) { - PlayerUtils.giveItem(player, itemStack, random); - } else { - location.getWorld().dropItemNaturally(location, itemStack); + plugin.getScheduler().sync().run(() -> { + DropItemActionEvent actionEvent = new DropItemActionEvent(context, location, item, itemStack); + if (EventUtils.fireAndCheckCancel(actionEvent)) { + return; } - } + ItemStack itemToDrop = actionEvent.item(); + if (itemToDrop != null && itemToDrop.getType() != Material.AIR && itemToDrop.getAmount() > 0) { + if (toInv && player != null) { + PlayerUtils.giveItem(player, itemToDrop, itemToDrop.getAmount()); + } else { + location.getWorld().dropItemNaturally(location, itemToDrop); + } + } + }, location); } @Nullable diff --git a/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionQualityCrops.java b/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionQualityCrops.java index 2298a4b6..aad39c8c 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionQualityCrops.java +++ b/api/src/main/java/net/momirealms/customcrops/api/action/builtin/ActionQualityCrops.java @@ -29,7 +29,9 @@ import net.momirealms.customcrops.api.core.world.CustomCropsChunk; import net.momirealms.customcrops.api.core.world.CustomCropsWorld; import net.momirealms.customcrops.api.core.world.Pos3; +import net.momirealms.customcrops.api.event.QualityCropActionEvent; import net.momirealms.customcrops.api.misc.value.MathValue; +import net.momirealms.customcrops.api.util.EventUtils; import net.momirealms.customcrops.api.util.PlayerUtils; import net.momirealms.customcrops.common.util.RandomUtils; import org.bukkit.Location; @@ -74,17 +76,27 @@ public ActionQualityCrops( protected void triggerAction(Context context) { Location location = requireNonNull(context.arg(ContextKeys.LOCATION)); int random = RandomUtils.generateRandomInt((int) min.evaluate(context), (int) max.evaluate(context)); - Player player = null; + Player player; if (context.holder() instanceof Player p) { player = p; + } else { + player = null; } - for (ItemStack itemStack : generateItem(location, player, random)) { - if (toInv && player != null) { - PlayerUtils.giveItem(player, itemStack, itemStack.getAmount()); - } else { - location.getWorld().dropItemNaturally(location, itemStack); + plugin.getScheduler().sync().run(() -> { + List itemToDrop = generateItem(location, player, random); + QualityCropActionEvent actionEvent = new QualityCropActionEvent(context, location, qualityLoots.clone(), itemToDrop); + if (EventUtils.fireAndCheckCancel(actionEvent)) { + return; } - } + for (ItemStack itemStack : itemToDrop) { + if (itemStack == null || itemStack.getAmount() == 0 || itemStack.getType() == Material.AIR) continue; + if (toInv && player != null) { + PlayerUtils.giveItem(player, itemStack, itemStack.getAmount()); + } else { + location.getWorld().dropItemNaturally(location, itemStack); + } + } + }, location); } public List generateItem(Location location, @Nullable Player player, int randomAmount) { diff --git a/api/src/main/java/net/momirealms/customcrops/api/core/block/DeadCrop.java b/api/src/main/java/net/momirealms/customcrops/api/core/block/DeadCrop.java index 99bda0ff..870da1ce 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/core/block/DeadCrop.java +++ b/api/src/main/java/net/momirealms/customcrops/api/core/block/DeadCrop.java @@ -28,8 +28,6 @@ import net.momirealms.customcrops.api.core.world.Pos3; import net.momirealms.customcrops.api.core.wrapper.WrappedBreakEvent; import net.momirealms.customcrops.api.core.wrapper.WrappedInteractEvent; -import net.momirealms.customcrops.api.event.ScarecrowBreakEvent; -import net.momirealms.customcrops.api.util.EventUtils; import net.momirealms.customcrops.api.util.LocationUtils; import org.bukkit.Location; import org.bukkit.entity.Player; diff --git a/api/src/main/java/net/momirealms/customcrops/api/event/DropItemActionEvent.java b/api/src/main/java/net/momirealms/customcrops/api/event/DropItemActionEvent.java new file mode 100644 index 00000000..a6851c3b --- /dev/null +++ b/api/src/main/java/net/momirealms/customcrops/api/event/DropItemActionEvent.java @@ -0,0 +1,133 @@ +/* + * Copyright (C) <2024> + * + * 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.customcrops.api.event; + +import net.momirealms.customcrops.api.context.Context; +import org.bukkit.Location; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +/** + * An event that is triggered when "drop-item" action is executed + */ +public class DropItemActionEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private final Context context; + private final Location location; + private final String droppedItemID; + private ItemStack item; + + public DropItemActionEvent(Context context, Location location, String droppedItemID, ItemStack itemStack) { + this.cancelled = false; + this.context = context; + this.location = location; + this.item = itemStack; + this.droppedItemID = droppedItemID; + } + + /** + * Gets the list of handlers for this event. + * + * @return the static handler list. + */ + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Gets the list of handlers for this event instance. + * + * @return the handler list. + */ + @NotNull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + + /** + * Returns whether the event is cancelled. + * + * @return true if the event is cancelled, false otherwise. + */ + @Override + public boolean isCancelled() { + return cancelled; + } + + /** + * Sets the cancelled state of the event. + * + * @param cancel true to cancel the event, false otherwise. + */ + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + /** + * Gets the context related to this event + * + * @return context + */ + public Context context() { + return context; + } + + /** + * Get the location of the dropped items + * + * @return location + */ + public Location location() { + return location; + } + + /** + * Gets the drop + * + * @return the drop + */ + public ItemStack item() { + return item; + } + + /** + * Sets the drop + * + * @param item the drop + */ + public void item(ItemStack item) { + this.item = item; + } + + /** + * Gets the dropped item's ID + * + * @return the dropped item's ID + */ + public String droppedItemID() { + return droppedItemID; + } +} diff --git a/api/src/main/java/net/momirealms/customcrops/api/event/QualityCropActionEvent.java b/api/src/main/java/net/momirealms/customcrops/api/event/QualityCropActionEvent.java new file mode 100644 index 00000000..a33ce896 --- /dev/null +++ b/api/src/main/java/net/momirealms/customcrops/api/event/QualityCropActionEvent.java @@ -0,0 +1,126 @@ +/* + * Copyright (C) <2024> + * + * 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.customcrops.api.event; + +import net.momirealms.customcrops.api.context.Context; +import org.bukkit.Location; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * An event that is triggered when "quality-crops" action is executed + */ +public class QualityCropActionEvent extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private final Context context; + private final Location location; + private final List items; + private final String[] qualityCrops; + + public QualityCropActionEvent(Context context, Location location, String[] qualityCrops, List itemStacks) { + this.cancelled = false; + this.context = context; + this.location = location; + this.items = itemStacks; + this.qualityCrops = qualityCrops; + } + + /** + * Gets the list of handlers for this event. + * + * @return the static handler list. + */ + @NotNull + public static HandlerList getHandlerList() { + return handlers; + } + + /** + * Gets the list of handlers for this event instance. + * + * @return the handler list. + */ + @NotNull + @Override + public HandlerList getHandlers() { + return getHandlerList(); + } + + /** + * Returns whether the event is cancelled. + * + * @return true if the event is cancelled, false otherwise. + */ + @Override + public boolean isCancelled() { + return cancelled; + } + + /** + * Sets the cancelled state of the event. + * + * @param cancel true to cancel the event, false otherwise. + */ + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + /** + * Gets the context related to this event + * + * @return context + */ + public Context context() { + return context; + } + + /** + * Get the location of the dropped items + * + * @return location + */ + public Location location() { + return location; + } + + /** + * Gets the drops + * + * @return the drops + */ + public List items() { + return items; + } + + /** + * Gets the quality crops' ids + * + * @return the quality crops' ids + */ + public String[] qualityCrops() { + return qualityCrops; + } +} diff --git a/gradle.properties b/gradle.properties index 2d27a7e7..494b1545 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Project settings # Rule: [major update].[feature update].[bug fix] -project_version=3.6.10 +project_version=3.6.11 config_version=41 project_group=net.momirealms @@ -28,7 +28,7 @@ mojang_brigadier_version=1.0.18 bstats_version=3.0.2 geantyref_version=1.3.15 caffeine_version=3.1.8 -rtag_version=1.5.6 +rtag_version=1.5.7 exp4j_version=0.4.8 placeholder_api_version=2.11.6 anti_grief_version=0.13 diff --git a/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugDataCommand.java b/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugDataCommand.java index 23f55f86..16d15391 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugDataCommand.java +++ b/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugDataCommand.java @@ -25,17 +25,14 @@ import net.momirealms.customcrops.api.core.world.Pos3; import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature; import net.momirealms.customcrops.common.command.CustomCropsCommandManager; -import net.momirealms.customcrops.common.helper.AdventureHelper; import net.momirealms.customcrops.common.locale.MessageConstants; import net.momirealms.customcrops.common.locale.TranslationManager; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.checkerframework.checker.units.qual.C; import org.incendo.cloud.Command; import org.incendo.cloud.CommandManager; -import org.incendo.cloud.context.CommandContext; import java.util.Optional; diff --git a/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugInsightCommand.java b/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugInsightCommand.java index 712e7720..270cc7c7 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugInsightCommand.java +++ b/plugin/src/main/java/net/momirealms/customcrops/bukkit/command/feature/DebugInsightCommand.java @@ -22,7 +22,6 @@ import net.momirealms.customcrops.api.util.LocationUtils; import net.momirealms.customcrops.bukkit.command.BukkitCommandFeature; import net.momirealms.customcrops.common.command.CustomCropsCommandManager; -import net.momirealms.customcrops.common.helper.AdventureHelper; import net.momirealms.customcrops.common.locale.MessageConstants; import net.momirealms.customcrops.common.plugin.scheduler.SchedulerTask; import net.momirealms.sparrow.heart.SparrowHeart; diff --git a/plugin/src/main/java/net/momirealms/customcrops/bukkit/config/BukkitConfigManager.java b/plugin/src/main/java/net/momirealms/customcrops/bukkit/config/BukkitConfigManager.java index 3a971b9e..f5ef288b 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/bukkit/config/BukkitConfigManager.java +++ b/plugin/src/main/java/net/momirealms/customcrops/bukkit/config/BukkitConfigManager.java @@ -44,7 +44,6 @@ import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Registry; -import org.bukkit.inventory.ItemStack; import java.io.File; import java.io.FileInputStream; diff --git a/plugin/src/main/java/net/momirealms/customcrops/bukkit/item/BukkitItemManager.java b/plugin/src/main/java/net/momirealms/customcrops/bukkit/item/BukkitItemManager.java index e6698c26..f0b2617f 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/bukkit/item/BukkitItemManager.java +++ b/plugin/src/main/java/net/momirealms/customcrops/bukkit/item/BukkitItemManager.java @@ -38,8 +38,8 @@ import net.momirealms.customcrops.api.util.LocationUtils; import net.momirealms.customcrops.api.util.PluginUtils; import net.momirealms.customcrops.common.item.Item; -import org.bukkit.*; import org.bukkit.Registry; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.entity.Entity;