mirror of
https://github.com/Xiao-MoMi/Custom-Crops.git
synced 2025-12-28 11:29:19 +00:00
new API Events
This commit is contained in:
@@ -23,7 +23,9 @@ import net.momirealms.customcrops.common.plugin.feature.Reloadable;
|
||||
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.
|
||||
|
||||
@@ -28,10 +28,13 @@ import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
|
||||
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 class ActionDropItem<T> extends AbstractBuiltInAction<T> {
|
||||
@Override
|
||||
protected void triggerAction(Context<T> 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
|
||||
|
||||
@@ -29,7 +29,9 @@ import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
|
||||
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 class ActionQualityCrops<T> extends AbstractBuiltInAction<T> {
|
||||
protected void triggerAction(Context<T> 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<ItemStack> 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<ItemStack> generateItem(Location location, @Nullable Player player, int randomAmount) {
|
||||
|
||||
@@ -28,8 +28,6 @@ import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (C) <2024> <XiaoMoMi>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (C) <2024> <XiaoMoMi>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<ItemStack> items;
|
||||
private final String[] qualityCrops;
|
||||
|
||||
public QualityCropActionEvent(Context<?> context, Location location, String[] qualityCrops, List<ItemStack> 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<ItemStack> items() {
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the quality crops' ids
|
||||
*
|
||||
* @return the quality crops' ids
|
||||
*/
|
||||
public String[] qualityCrops() {
|
||||
return qualityCrops;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user