mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-19 19:25:48 +00:00
Optional player for SlimefunItemSpawnEvent
This commit is contained in:
parent
c977454e94
commit
9cc955d75e
@ -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;
|
||||
@ -29,6 +33,7 @@ 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) {
|
||||
@ -36,6 +41,25 @@ public class SlimefunItemSpawnEvent extends Event implements Cancellable {
|
||||
this.itemStack = itemStack;
|
||||
this.itemSpawnReason = itemSpawnReason;
|
||||
this.cancelled = false;
|
||||
this.player = null;
|
||||
}
|
||||
|
||||
@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() {
|
||||
return Optional.ofNullable(player);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -167,7 +167,7 @@ public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> im
|
||||
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);
|
||||
|
@ -55,7 +55,7 @@ public class ChristmasPresent extends SimpleSlimefunItem<ItemUseHandler> impleme
|
||||
|
||||
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);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
@ -154,7 +154,7 @@ public class GoldPan extends SimpleSlimefunItem<ItemUseHandler> implements Recip
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ public class PickaxeOfContainment extends SimpleSlimefunItem<ToolUseHandler> {
|
||||
|
||||
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);
|
||||
|
@ -610,6 +610,23 @@ public final class SlimefunUtils {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to spawn an {@link ItemStack}.
|
||||
* This method automatically calls a {@link SlimefunItemSpawnEvent} to allow
|
||||
|
Loading…
Reference in New Issue
Block a user