Fix StackedItemSpawnEvent not called in some cases

This commit is contained in:
ceze88 2023-11-14 23:40:16 +01:00
parent 85a7995420
commit d6c73e669e
3 changed files with 64 additions and 10 deletions

View File

@ -1,11 +1,14 @@
package com.craftaro.ultimatestacker.api.events.entity;
import com.craftaro.ultimatestacker.api.stack.entity.EntityStack;
import org.bukkit.entity.Item;
import org.bukkit.entity.LivingEntity;
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 org.jetbrains.annotations.Nullable;
/**
* Called before we spawn a stacked items
@ -13,22 +16,59 @@ import org.bukkit.inventory.ItemStack;
public class StackedItemSpawnEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final ItemStack item;
private final long amount;
private Item item;
private ItemStack itemStack;
private long amount;
private boolean cancelled = false;
public StackedItemSpawnEvent(ItemStack item, long amount) {
/**
* Constructor
*
* @param item Item that will be modified. Can be null if we spawn a new one
* @param itemStack ItemStack that will be spawned
* @param amount amount
*/
public StackedItemSpawnEvent(@Nullable Item item, @NotNull ItemStack itemStack, long amount) {
this.item = item;
this.itemStack = itemStack;
this.amount = amount;
}
/**
* Get the item that will be spawned
* Get the item that will be modified
*
* @return Item
*/
public @Nullable Item getItem() {
return item;
}
/**
* Set the item that will be spawned
*
* @param item Item
*/
public void setItem(Item item) {
this.item = item;
}
/**
* Get the ItemStack that will be spawned
*
* @return ItemStack
*/
public ItemStack getItem() {
return item;
public @NotNull ItemStack getItemStack() {
return itemStack;
}
/**
* Set the item that will be spawned
*
* @param itemStack ItemStack
*/
public void setItemStack(ItemStack itemStack) {
if (itemStack == null) throw new IllegalArgumentException("ItemStack cannot be null");
this.itemStack = itemStack;
}
/**
@ -40,6 +80,15 @@ public class StackedItemSpawnEvent extends Event implements Cancellable {
return amount;
}
/**
* Set the amount of items that will be spawned
*
* @param amount amount
*/
public void setAmount(long amount) {
this.amount = amount;
}
@Override
public HandlerList getHandlers() {
return handlers;

View File

@ -24,9 +24,11 @@ public interface StackedItemManager {
* Create a new StackedItem for the given item
* @param item The item to create the stack for
* @param amount The amount of items in the stack
* @return The StackedItem for the given Item
* @return The StackedItem for the given Item or
* null if it could not be created or a
* plugin cancelled the event
*/
@NotNull StackedItem createStack(Item item, int amount);
@Nullable StackedItem createStack(Item item, int amount);
/**
* Create a new StackedItem for the given ItemStack

View File

@ -34,7 +34,7 @@ public class StackedItemManagerImpl implements StackedItemManager {
if (item.getType() == Material.AIR) return null;
World world = location.getWorld();
if (world == null) return null;
StackedItemSpawnEvent event = new StackedItemSpawnEvent(item, amount);
StackedItemSpawnEvent event = new StackedItemSpawnEvent(null, item, amount);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return null;
@ -44,7 +44,10 @@ public class StackedItemManagerImpl implements StackedItemManager {
}
@Override
public @NotNull StackedItem createStack(Item item, int amount) {
public StackedItem createStack(Item item, int amount) {
StackedItemSpawnEvent event = new StackedItemSpawnEvent(item, item.getItemStack(), amount);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) return null;
return new StackedItemImpl(item, amount);
}