mirror of
https://github.com/songoda/UltimateStacker.git
synced 2024-09-27 14:12:59 +02:00
Fix StackedItemSpawnEvent not called in some cases
This commit is contained in:
parent
85a7995420
commit
d6c73e669e
@ -1,11 +1,14 @@
|
|||||||
package com.craftaro.ultimatestacker.api.events.entity;
|
package com.craftaro.ultimatestacker.api.events.entity;
|
||||||
|
|
||||||
import com.craftaro.ultimatestacker.api.stack.entity.EntityStack;
|
import com.craftaro.ultimatestacker.api.stack.entity.EntityStack;
|
||||||
|
import org.bukkit.entity.Item;
|
||||||
import org.bukkit.entity.LivingEntity;
|
import org.bukkit.entity.LivingEntity;
|
||||||
import org.bukkit.event.Cancellable;
|
import org.bukkit.event.Cancellable;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called before we spawn a stacked items
|
* Called before we spawn a stacked items
|
||||||
@ -13,22 +16,59 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
public class StackedItemSpawnEvent extends Event implements Cancellable {
|
public class StackedItemSpawnEvent extends Event implements Cancellable {
|
||||||
|
|
||||||
private static final HandlerList handlers = new HandlerList();
|
private static final HandlerList handlers = new HandlerList();
|
||||||
private final ItemStack item;
|
private Item item;
|
||||||
private final long amount;
|
private ItemStack itemStack;
|
||||||
|
private long amount;
|
||||||
private boolean cancelled = false;
|
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.item = item;
|
||||||
|
this.itemStack = itemStack;
|
||||||
this.amount = amount;
|
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
|
* @return ItemStack
|
||||||
*/
|
*/
|
||||||
public ItemStack getItem() {
|
public @NotNull ItemStack getItemStack() {
|
||||||
return item;
|
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;
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the amount of items that will be spawned
|
||||||
|
*
|
||||||
|
* @param amount amount
|
||||||
|
*/
|
||||||
|
public void setAmount(long amount) {
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HandlerList getHandlers() {
|
public HandlerList getHandlers() {
|
||||||
return handlers;
|
return handlers;
|
||||||
|
@ -24,9 +24,11 @@ public interface StackedItemManager {
|
|||||||
* Create a new StackedItem for the given item
|
* Create a new StackedItem for the given item
|
||||||
* @param item The item to create the stack for
|
* @param item The item to create the stack for
|
||||||
* @param amount The amount of items in the stack
|
* @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
|
* Create a new StackedItem for the given ItemStack
|
||||||
|
@ -34,7 +34,7 @@ public class StackedItemManagerImpl implements StackedItemManager {
|
|||||||
if (item.getType() == Material.AIR) return null;
|
if (item.getType() == Material.AIR) return null;
|
||||||
World world = location.getWorld();
|
World world = location.getWorld();
|
||||||
if (world == null) return null;
|
if (world == null) return null;
|
||||||
StackedItemSpawnEvent event = new StackedItemSpawnEvent(item, amount);
|
StackedItemSpawnEvent event = new StackedItemSpawnEvent(null, item, amount);
|
||||||
Bukkit.getPluginManager().callEvent(event);
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
if (event.isCancelled()) return null;
|
if (event.isCancelled()) return null;
|
||||||
|
|
||||||
@ -44,7 +44,10 @@ public class StackedItemManagerImpl implements StackedItemManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
return new StackedItemImpl(item, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user