Skip to content

Commit

Permalink
Merge pull request #3998 from TheSilentPro/master
Browse files Browse the repository at this point in the history
Optional player for SlimefunItemSpawnEvent
  • Loading branch information
Sfiguz7 authored Dec 6, 2023
2 parents f8dfd75 + cb736d3 commit ddbe8ae
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.github.thebusybiscuit.slimefun4.api.events;

import java.util.Optional;

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

import org.apache.commons.lang.Validate;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -29,13 +33,29 @@ public class SlimefunItemSpawnEvent extends Event implements Cancellable {
private ItemStack itemStack;
private boolean cancelled;
private final ItemSpawnReason itemSpawnReason;
private final Player player;

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

@ParametersAreNonnullByDefault
public SlimefunItemSpawnEvent(Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) {
this(null, location, itemStack, itemSpawnReason);
}

/**
* Optionally returns the {@link Player} responsible for this spawn reason.
*
* @return The player responsible if applicable.
*/
public @Nonnull Optional<Player> getPlayer() {
return Optional.ofNullable(player);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void placeItem(@Nonnull Player p, @Nonnull Block b) {
ItemUtils.consumeItem(hand, false);
}

Item entity = SlimefunUtils.spawnItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM);
Item entity = SlimefunUtils.spawnItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM, false, p);

if (entity != null) {
ArmorStand armorStand = getArmorStand(b, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ChristmasPresent(ItemGroup itemGroup, SlimefunItemStack item, RecipeType

Block b = block.getRelative(e.getClickedFace());
ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone();
SlimefunUtils.spawnItem(b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true);
SlimefunUtils.spawnItem(b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true, e.getPlayer());
});
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void updateRandomizer() {

// Make sure that the randomly selected item is not air
if (output.getType() != Material.AIR) {
SlimefunUtils.spawnItem(b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true);
SlimefunUtils.spawnItem(b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true, e.getPlayer());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public PickaxeOfContainment(ItemGroup itemGroup, SlimefunItemStack item, RecipeT

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

e.setExpToDrop(0);
e.setDropItems(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,14 @@ public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack it
* The {@link ItemSpawnReason} why the item is being dropped
* @param addRandomOffset
* Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)})
* @param player
* The player that caused this {@link SlimefunItemSpawnEvent}
*
* @return The dropped {@link Item} (or null if the {@link SlimefunItemSpawnEvent} was cancelled)
*/
@ParametersAreNonnullByDefault
public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) {
SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(loc, item, reason);
public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset, @Nullable Player player) {
SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(player, loc, item, reason);
Slimefun.instance().getServer().getPluginManager().callEvent(event);

if (!event.isCancelled()) {
Expand All @@ -610,6 +612,27 @@ public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack it
}
}

/**
* Helper method to spawn an {@link ItemStack}.
* This method automatically calls a {@link SlimefunItemSpawnEvent} to allow
* other plugins to catch the item being dropped.
*
* @param loc
* The {@link Location} where to drop the item
* @param item
* The {@link ItemStack} to drop
* @param reason
* The {@link ItemSpawnReason} why the item is being dropped
* @param addRandomOffset
* Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)})
*
* @return The dropped {@link Item} (or null if the {@link SlimefunItemSpawnEvent} was cancelled)
*/
@ParametersAreNonnullByDefault
public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) {
return spawnItem(loc, item, reason, addRandomOffset, null);
}

/**
* Helper method to spawn an {@link ItemStack}.
* This method automatically calls a {@link SlimefunItemSpawnEvent} to allow
Expand Down

0 comments on commit ddbe8ae

Please sign in to comment.