Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional player for SlimefunItemSpawnEvent #3998

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@ 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) {
this.location = location;
this.itemStack = itemStack;
this.itemSpawnReason = itemSpawnReason;
this.cancelled = false;
this.player = null;
TheSilentPro marked this conversation as resolved.
Show resolved Hide resolved
TheSilentPro marked this conversation as resolved.
Show resolved Hide resolved
}

@ParametersAreNonnullByDefault
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;
}

/**
* Optionally returns the {@link Player} responsible for this spawn reason.
*
* @return The player responsible if applicable.
*/
public Optional<Player> getPlayer() {
TheSilentPro marked this conversation as resolved.
Show resolved Hide resolved
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(p, b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM, false);

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(e.getPlayer(), b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true);
});
};
}
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(e.getPlayer(), b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true);
}
}
}
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(e.getPlayer(), b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true);

e.setExpToDrop(0);
e.setDropItems(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,23 @@ public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack it
}
}

public static @Nullable Item spawnItem(Player player, Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) {
SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(player, loc, item, reason);
Slimefun.instance().getServer().getPluginManager().callEvent(event);

if (!event.isCancelled()) {
World world = event.getLocation().getWorld();

if (addRandomOffset) {
return world.dropItemNaturally(event.getLocation(), event.getItemStack());
} else {
return world.dropItem(event.getLocation(), event.getItemStack());
}
} else {
return null;
}
}
TheSilentPro marked this conversation as resolved.
Show resolved Hide resolved

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