1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Reduce repeated code

This commit is contained in:
Silent 2023-10-16 14:24:10 -07:00
parent 9cc955d75e
commit 5527729109
6 changed files with 32 additions and 30 deletions

View File

@ -35,15 +35,6 @@ public class SlimefunItemSpawnEvent extends Event implements Cancellable {
private final ItemSpawnReason itemSpawnReason; private final ItemSpawnReason itemSpawnReason;
private final Player player; 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;
}
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public SlimefunItemSpawnEvent(@Nullable Player player, Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) { public SlimefunItemSpawnEvent(@Nullable Player player, Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) {
this.location = location; this.location = location;
@ -53,6 +44,11 @@ public class SlimefunItemSpawnEvent extends Event implements Cancellable {
this.player = player; 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. * Optionally returns the {@link Player} responsible for this spawn reason.
* *

View File

@ -167,7 +167,7 @@ public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> im
ItemUtils.consumeItem(hand, false); ItemUtils.consumeItem(hand, false);
} }
Item entity = SlimefunUtils.spawnItem(p, b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM, false); Item entity = SlimefunUtils.spawnItem(b.getLocation().add(0.5, 1.2, 0.5), displayItem, ItemSpawnReason.ANCIENT_PEDESTAL_PLACE_ITEM, false, p);
if (entity != null) { if (entity != null) {
ArmorStand armorStand = getArmorStand(b, true); ArmorStand armorStand = getArmorStand(b, true);

View File

@ -55,7 +55,7 @@ public class ChristmasPresent extends SimpleSlimefunItem<ItemUseHandler> impleme
Block b = block.getRelative(e.getClickedFace()); Block b = block.getRelative(e.getClickedFace());
ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone(); ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone();
SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true); SlimefunUtils.spawnItem(b.getLocation(), gift, ItemSpawnReason.CHRISTMAS_PRESENT_OPENED, true, e.getPlayer());
}); });
}; };
} }

View File

@ -154,7 +154,7 @@ public class GoldPan extends SimpleSlimefunItem<ItemUseHandler> implements Recip
// Make sure that the randomly selected item is not air // Make sure that the randomly selected item is not air
if (output.getType() != Material.AIR) { if (output.getType() != Material.AIR) {
SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true); SlimefunUtils.spawnItem(b.getLocation(), output.clone(), ItemSpawnReason.GOLD_PAN_USE, true, e.getPlayer());
} }
} }
} }

View File

@ -50,7 +50,7 @@ public class PickaxeOfContainment extends SimpleSlimefunItem<ToolUseHandler> {
if (b.getType() == Material.SPAWNER) { if (b.getType() == Material.SPAWNER) {
ItemStack spawner = breakSpawner(b); ItemStack spawner = breakSpawner(b);
SlimefunUtils.spawnItem(e.getPlayer(), b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true); SlimefunUtils.spawnItem(b.getLocation(), spawner, ItemSpawnReason.BROKEN_SPAWNER_DROP, true, e.getPlayer());
e.setExpToDrop(0); e.setExpToDrop(0);
e.setDropItems(false); e.setDropItems(false);

View File

@ -589,12 +589,14 @@ public final class SlimefunUtils {
* The {@link ItemSpawnReason} why the item is being dropped * The {@link ItemSpawnReason} why the item is being dropped
* @param addRandomOffset * @param addRandomOffset
* Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)}) * Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)})
* @param player
* The player that caused this {@link org.bukkit.event.entity.ItemSpawnEvent}
* *
* @return The dropped {@link Item} (or null if the {@link SlimefunItemSpawnEvent} was cancelled) * @return The dropped {@link Item} (or null if the {@link SlimefunItemSpawnEvent} was cancelled)
*/ */
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset) { public static @Nullable Item spawnItem(Location loc, ItemStack item, ItemSpawnReason reason, boolean addRandomOffset, @Nullable Player player) {
SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(loc, item, reason); SlimefunItemSpawnEvent event = new SlimefunItemSpawnEvent(player, loc, item, reason);
Slimefun.instance().getServer().getPluginManager().callEvent(event); Slimefun.instance().getServer().getPluginManager().callEvent(event);
if (!event.isCancelled()) { if (!event.isCancelled()) {
@ -610,21 +612,25 @@ 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); * Helper method to spawn an {@link ItemStack}.
Slimefun.instance().getServer().getPluginManager().callEvent(event); * This method automatically calls a {@link SlimefunItemSpawnEvent} to allow
* other plugins to catch the item being dropped.
if (!event.isCancelled()) { *
World world = event.getLocation().getWorld(); * @param loc
* The {@link Location} where to drop the item
if (addRandomOffset) { * @param item
return world.dropItemNaturally(event.getLocation(), event.getItemStack()); * The {@link ItemStack} to drop
} else { * @param reason
return world.dropItem(event.getLocation(), event.getItemStack()); * The {@link ItemSpawnReason} why the item is being dropped
} * @param addRandomOffset
} else { * Whether a random offset should be added (see {@link World#dropItemNaturally(Location, ItemStack)})
return null; *
} * @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);
} }
/** /**