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 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
public SlimefunItemSpawnEvent(@Nullable Player player, Location location, ItemStack itemStack, ItemSpawnReason itemSpawnReason) {
this.location = location;
@ -53,6 +44,11 @@ public class SlimefunItemSpawnEvent extends Event implements Cancellable {
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.
*

View File

@ -167,7 +167,7 @@ public class AncientPedestal extends SimpleSlimefunItem<BlockDispenseHandler> im
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) {
ArmorStand armorStand = getArmorStand(b, true);

View File

@ -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(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
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) {
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.setDropItems(false);

View File

@ -589,28 +589,13 @@ public final class SlimefunUtils {
* 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 org.bukkit.event.entity.ItemSpawnEvent}
*
* @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);
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;
}
}
public static @Nullable Item spawnItem(Player player, 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(player, loc, item, reason);
Slimefun.instance().getServer().getPluginManager().callEvent(event);
@ -627,6 +612,27 @@ public final class SlimefunUtils {
}
}
/**
* 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