Skip to content

Commit

Permalink
Added SlimefunItemSpawnEvent and ItemSpawnReason
Browse files Browse the repository at this point in the history
This allows other plugins to provide compatibility in some places
  • Loading branch information
TheBusyBiscuit committed Jul 8, 2021
1 parent e19f291 commit 5a1442f
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* Deepslate ores can now be doubled using an Ore Crusher
* Tridents can now be crafted
* The Industrial Miner can now mine up to the minimum world limit (previously only until y=0)
* (API) Added SlimefunItemSpawnEvent and ItemSpawnReason

#### Changes

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package io.github.thebusybiscuit.slimefun4.api.events;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
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 io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;

/**
* This {@link Event} is fired whenever slimefun drops an {@link ItemStack}.
* Creating a custom {@link Event} for this allows other plugins to provide
* compatibility with auto-pickup options or similar.
*
* @author TheBusyBiscuit
*
* @see ItemSpawnReason
*/
public class SlimefunItemSpawnEvent extends Event implements Cancellable {

private static final HandlerList handlers = new HandlerList();

private Location location;
private ItemStack itemStack;
private boolean cancelled;
private final ItemSpawnReason itemSpawnReason;

@ParametersAreNonnullByDefault
public SlimefunItemSpawnEvent(Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) {
this.location = location;
this.itemStack = itemStack;
this.itemSpawnReason = itemSpawnReason;
this.cancelled = false;
}

/**
* This returns the {@link ItemSpawnReason} why we dropped an {@link ItemStack}.
*
* @return the {@link ItemSpawnReason}.
*/
public @Nonnull ItemSpawnReason getItemSpawnReason() {
return itemSpawnReason;
}

/**
* This returns the {@link Location} where we will drop the item.
*
* @return The {@link Location} where the item will be dropped
*/
public @Nonnull Location getLocation() {
return location;
}

/**
* This sets the {@link Location} on where to drop this item.
*
* @param location
* The {@link Location} where to drop the {@link ItemStack}
*/
public void setLocation(@Nonnull Location location) {
Validate.notNull(location, "The Location cannot be null!");

this.location = location;
}

/**
* This returns the {@link ItemStack} that will be dropped.
*
* @return The {@link ItemStack} that will be dropped
*/
public @Nonnull ItemStack getItemStack() {
return itemStack;
}

/**
* This method sets the {@link ItemStack} that should be dropped.
*
* @param itemStack
* The {@link ItemStack} to drop
*/
public void setItemStack(@Nonnull ItemStack itemStack) {
Validate.notNull(itemStack, "Cannot drop null.");
Validate.isTrue(!itemStack.getType().isAir(), "Cannot drop air.");

this.itemStack = itemStack;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}

public static @Nonnull HandlerList getHandlerList() {
return handlers;
}

@Override
public @Nonnull HandlerList getHandlers() {
return getHandlerList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.github.thebusybiscuit.slimefun4.api.items;

import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.api.events.SlimefunItemSpawnEvent;
import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet;
import io.github.thebusybiscuit.slimefun4.implementation.items.altar.AncientPedestal;
import io.github.thebusybiscuit.slimefun4.implementation.items.seasonal.ChristmasPresent;
import io.github.thebusybiscuit.slimefun4.implementation.items.seasonal.EasterEgg;
import io.github.thebusybiscuit.slimefun4.implementation.items.tools.GoldPan;
import io.github.thebusybiscuit.slimefun4.implementation.items.tools.PickaxeOfContainment;

/**
* This enum holds the different reasons as to why we may need to spawn an item.
*
* @author TheBusyBiscuit
*
* @see SlimefunItemSpawnEvent
*
*/
public enum ItemSpawnReason {

/**
* The item is spawned on top of an {@link AncientPedestal}.
*/
ANCIENT_PEDESTAL_PLACE_ITEM,

/**
* This {@link ItemStack} is dropped as a result of the {@link PickaxeOfContainment}
* breaking a monster spawner.
*/
BROKEN_SPAWNER_DROP,

/**
* The {@link ItemStack} is dropped as the result of a {@link CargoNet}
* overflowing.
*/
CARGO_OVERFLOW,

/**
* THe {@link ItemStack} is dropped as the result of an opened {@link ChristmasPresent}.
*/
CHRISTMAS_PRESENT_OPENED,

/**
* THe {@link ItemStack} is dropped as the result of an opened {@link EasterEgg}.
*/
EASTER_EGG_OPENED,

/**
* The {@link ItemStack} is dropped as the result of a {@link GoldPan} being used
* on a {@link Block} which yields drops.
*/
GOLD_PAN_USE,

/**
* Other reasons we did not account for.
*/
MISC;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.cscorelib2.blocks.BlockPosition;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.core.networks.NetworkManager;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;

import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
Expand Down Expand Up @@ -140,7 +143,7 @@ private void insertItem(Block inputTarget, int previousSlot, ItemStack item) {

if (rest != null && !manager.isItemDeletionEnabled()) {
// If the item still couldn't be inserted, simply drop it on the ground
inputTarget.getWorld().dropItem(inputTarget.getLocation().add(0, 1, 0), rest);
SlimefunUtils.spawnItem(inputTarget.getLocation().add(0, 1, 0), rest, ItemSpawnReason.CARGO_OVERFLOW);
}
}
} else {
Expand All @@ -150,7 +153,7 @@ private void insertItem(Block inputTarget, int previousSlot, ItemStack item) {
if (menu.getItemInSlot(previousSlot) == null) {
menu.replaceExistingItem(previousSlot, item);
} else if (!manager.isItemDeletionEnabled()) {
inputTarget.getWorld().dropItem(inputTarget.getLocation().add(0, 1, 0), item);
SlimefunUtils.spawnItem(inputTarget.getLocation().add(0, 1, 0), item, ItemSpawnReason.CARGO_OVERFLOW);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockDispenseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
Expand Down Expand Up @@ -57,8 +58,7 @@ public AncientPedestal(Category category, SlimefunItemStack item, RecipeType rec
addItemHandler(onBreak());
}

@Nonnull
private BlockBreakHandler onBreak() {
private @Nonnull BlockBreakHandler onBreak() {
return new SimpleBlockBreakHandler() {

@Override
Expand All @@ -79,12 +79,11 @@ public void onBlockBreak(@Nonnull Block b) {
}

@Override
public BlockDispenseHandler getItemHandler() {
public @Nonnull BlockDispenseHandler getItemHandler() {
return (e, d, block, machine) -> e.setCancelled(true);
}

@Nonnull
public Optional<Item> getPlacedItem(@Nonnull Block pedestal) {
public @Nonnull Optional<Item> getPlacedItem(@Nonnull Block pedestal) {
Location l = pedestal.getLocation().add(0.5, 1.2, 0.5);

for (Entity n : l.getWorld().getNearbyEntities(l, 0.5, 0.5, 0.5, this::testItem)) {
Expand All @@ -107,8 +106,7 @@ private boolean testItem(@Nullable Entity n) {
}
}

@Nonnull
public ItemStack getOriginalItemStack(@Nonnull Item item) {
public @Nonnull ItemStack getOriginalItemStack(@Nonnull Item item) {
ItemStack stack = item.getItemStack().clone();
String customName = item.getCustomName();

Expand Down Expand Up @@ -142,12 +140,14 @@ public void placeItem(@Nonnull Player p, @Nonnull Block b) {
ItemUtils.consumeItem(hand, false);
}

Item entity = b.getWorld().dropItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem);
entity.setVelocity(new Vector(0, 0.1, 0));
entity.setCustomNameVisible(true);
entity.setCustomName(nametag);
SlimefunUtils.markAsNoPickup(entity, "altar_item");
p.playSound(b.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.3F, 0.3F);
}
Item entity = SlimefunUtils.spawnItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM);

if (entity != null) {
entity.setVelocity(new Vector(0, 0.1, 0));
entity.setCustomNameVisible(true);
entity.setCustomName(nametag);
SlimefunUtils.markAsNoPickup(entity, "altar_item");
p.playSound(b.getLocation(), Sound.ENTITY_ITEM_PICKUP, 0.3F, 0.3F);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable;
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.FireworkUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;

import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
Expand Down Expand Up @@ -54,7 +56,7 @@ public ChristmasPresent(Category category, SlimefunItemStack item, RecipeType re

Block b = block.getRelative(e.getClickedFace());
ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone();
b.getWorld().dropItemNaturally(b.getLocation(), gift);
SlimefunUtils.spawnItem(b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true);
});
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.FireworkUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;

import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
Expand Down Expand Up @@ -51,7 +54,7 @@ public EasterEgg(Category category, SlimefunItemStack item, RecipeType recipeTyp
}

FireworkUtils.launchRandom(p, 2);
p.getWorld().dropItemNaturally(p.getLocation(), gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone());
SlimefunUtils.spawnItem(p.getLocation(), gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone(), ItemSpawnReason.EASTER_EGG_OPENED, true);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.github.thebusybiscuit.cscorelib2.collections.RandomizedSet;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem;
import io.github.thebusybiscuit.slimefun4.core.handlers.EntityInteractHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
Expand All @@ -26,6 +27,8 @@
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.ElectricGoldPan;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.AutomatedPanningMachine;
import io.github.thebusybiscuit.slimefun4.implementation.settings.GoldPanDrop;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;

import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
Expand Down Expand Up @@ -133,7 +136,7 @@ public void updateRandomizer() {

// Make sure that the randomly selected item is not air
if (output.getType() != Material.AIR) {
b.getWorld().dropItemNaturally(b.getLocation(), output.clone());
SlimefunUtils.spawnItem(b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.api.items.ItemSpawnReason;
import io.github.thebusybiscuit.slimefun4.core.handlers.ToolUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.AbstractMonsterSpawner;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.BrokenSpawner;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.RepairedSpawner;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.papermc.lib.PaperLib;

import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
Expand All @@ -41,22 +44,21 @@ public PickaxeOfContainment(Category category, SlimefunItemStack item, RecipeTyp
}

@Override
public ToolUseHandler getItemHandler() {
public @Nonnull ToolUseHandler getItemHandler() {
return (e, tool, fortune, drops) -> {
Block b = e.getBlock();

if (b.getType() == Material.SPAWNER) {
ItemStack spawner = breakSpawner(b);
b.getLocation().getWorld().dropItemNaturally(b.getLocation(), spawner);
SlimefunUtils.spawnItem(b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true);

e.setExpToDrop(0);
e.setDropItems(false);
}
};
}

@Nonnull
private ItemStack breakSpawner(@Nonnull Block b) {
private @Nonnull ItemStack breakSpawner(@Nonnull Block b) {
AbstractMonsterSpawner spawner;

/**
Expand Down
Loading

0 comments on commit 5a1442f

Please sign in to comment.