mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-19 19:25:48 +00:00
Added CoolerFeedPlayerEvent
This commit is contained in:
parent
1d36ca82ba
commit
05e983917a
@ -29,6 +29,7 @@
|
|||||||
* The Smelters Pick now also works on Ancient Debris
|
* The Smelters Pick now also works on Ancient Debris
|
||||||
* (API) Added PlayerPreResearchEvent
|
* (API) Added PlayerPreResearchEvent
|
||||||
* Added a config option to disable network visualizations
|
* Added a config option to disable network visualizations
|
||||||
|
* (API) Added CoolerFeedPlayerEvent
|
||||||
|
|
||||||
#### Changes
|
#### Changes
|
||||||
* Removed 1.13 support
|
* Removed 1.13 support
|
||||||
|
@ -0,0 +1,113 @@
|
|||||||
|
package io.github.thebusybiscuit.slimefun4.api.events;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.event.player.PlayerEvent;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.PotionMeta;
|
||||||
|
import org.bukkit.potion.PotionEffect;
|
||||||
|
|
||||||
|
import io.github.thebusybiscuit.slimefun4.implementation.items.backpacks.Cooler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This {@link Event} is called whenever a {@link Player} is
|
||||||
|
* fed through a {@link Cooler}.
|
||||||
|
*
|
||||||
|
* @author TheBusyBiscuit
|
||||||
|
*
|
||||||
|
* @see Cooler
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CoolerFeedPlayerEvent extends PlayerEvent implements Cancellable {
|
||||||
|
|
||||||
|
private static final HandlerList handlers = new HandlerList();
|
||||||
|
|
||||||
|
private final Cooler cooler;
|
||||||
|
private final ItemStack coolerItem;
|
||||||
|
|
||||||
|
private ItemStack consumedItem;
|
||||||
|
private boolean cancelled;
|
||||||
|
|
||||||
|
@ParametersAreNonnullByDefault
|
||||||
|
public CoolerFeedPlayerEvent(Player player, Cooler cooler, ItemStack coolerItem, ItemStack consumedItem) {
|
||||||
|
super(player);
|
||||||
|
|
||||||
|
this.cooler = cooler;
|
||||||
|
this.coolerItem = coolerItem;
|
||||||
|
this.consumedItem = consumedItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This returns the {@link Cooler} that was used.
|
||||||
|
*
|
||||||
|
* @return The {@link Cooler} that was used
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public Cooler getCooler() {
|
||||||
|
return cooler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This returns the {@link Cooler} that was used (as an {@link ItemStack})
|
||||||
|
*
|
||||||
|
* @return The {@link Cooler} that was used
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public ItemStack getCoolerItem() {
|
||||||
|
return coolerItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This returns the {@link ItemStack} that was consumed.
|
||||||
|
* The returned {@link ItemStack} is immutable.
|
||||||
|
*
|
||||||
|
* @return The {@link ItemStack} that was consumed
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public ItemStack getConsumedItem() {
|
||||||
|
return consumedItem.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This sets the {@link ItemStack} that should be "consumed".
|
||||||
|
* The {@link ItemStack} must be a potion.
|
||||||
|
* The {@link Player} will receive the {@link PotionEffect PotionEffects} of the
|
||||||
|
* provided potion upon consumption.
|
||||||
|
*
|
||||||
|
* @param item
|
||||||
|
* The new {@link ItemStack}
|
||||||
|
*/
|
||||||
|
public void setConsumedItem(@Nonnull ItemStack item) {
|
||||||
|
Validate.notNull(item, "The consumed Item cannot be null!");
|
||||||
|
Validate.isTrue(item.getItemMeta() instanceof PotionMeta, "The item must be a potion!");
|
||||||
|
|
||||||
|
this.consumedItem = item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCancelled() {
|
||||||
|
return cancelled;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCancelled(boolean cancel) {
|
||||||
|
this.cancelled = cancel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
public static HandlerList getHandlerList() {
|
||||||
|
return handlers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public HandlerList getHandlers() {
|
||||||
|
return getHandlerList();
|
||||||
|
}
|
||||||
|
}
|
@ -15,6 +15,7 @@ import org.bukkit.inventory.ItemStack;
|
|||||||
import org.bukkit.inventory.meta.PotionMeta;
|
import org.bukkit.inventory.meta.PotionMeta;
|
||||||
import org.bukkit.potion.PotionEffect;
|
import org.bukkit.potion.PotionEffect;
|
||||||
|
|
||||||
|
import io.github.thebusybiscuit.slimefun4.api.events.CoolerFeedPlayerEvent;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack;
|
import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
|
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
||||||
@ -37,11 +38,13 @@ import me.mrCookieSlime.Slimefun.api.Slimefun;
|
|||||||
*/
|
*/
|
||||||
public class CoolerListener implements Listener {
|
public class CoolerListener implements Listener {
|
||||||
|
|
||||||
|
private final SlimefunPlugin plugin;
|
||||||
private final Cooler cooler;
|
private final Cooler cooler;
|
||||||
|
|
||||||
public CoolerListener(@Nonnull SlimefunPlugin plugin, @Nonnull Cooler cooler) {
|
public CoolerListener(@Nonnull SlimefunPlugin plugin, @Nonnull Cooler cooler) {
|
||||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||||
|
|
||||||
|
this.plugin = plugin;
|
||||||
this.cooler = cooler;
|
this.cooler = cooler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,12 +96,12 @@ public class CoolerListener implements Listener {
|
|||||||
private void takeJuiceFromCooler(@Nonnull Player p, @Nonnull ItemStack cooler) {
|
private void takeJuiceFromCooler(@Nonnull Player p, @Nonnull ItemStack cooler) {
|
||||||
PlayerProfile.getBackpack(cooler, backpack -> {
|
PlayerProfile.getBackpack(cooler, backpack -> {
|
||||||
if (backpack != null) {
|
if (backpack != null) {
|
||||||
SlimefunPlugin.runSync(() -> consumeJuice(p, backpack));
|
SlimefunPlugin.runSync(() -> consumeJuice(p, cooler, backpack));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean consumeJuice(@Nonnull Player p, @Nonnull PlayerBackpack backpack) {
|
private boolean consumeJuice(@Nonnull Player p, @Nonnull ItemStack coolerItem, @Nonnull PlayerBackpack backpack) {
|
||||||
Inventory inv = backpack.getInventory();
|
Inventory inv = backpack.getInventory();
|
||||||
int slot = -1;
|
int slot = -1;
|
||||||
|
|
||||||
@ -112,17 +115,26 @@ public class CoolerListener implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (slot >= 0) {
|
if (slot >= 0) {
|
||||||
PotionMeta im = (PotionMeta) inv.getItem(slot).getItemMeta();
|
ItemStack item = inv.getItem(slot);
|
||||||
|
CoolerFeedPlayerEvent event = new CoolerFeedPlayerEvent(p, cooler, coolerItem, item);
|
||||||
|
plugin.getServer().getPluginManager().callEvent(event);
|
||||||
|
|
||||||
for (PotionEffect effect : im.getCustomEffects()) {
|
if (!event.isCancelled()) {
|
||||||
p.addPotionEffect(effect);
|
PotionMeta im = (PotionMeta) event.getConsumedItem().getItemMeta();
|
||||||
|
|
||||||
|
for (PotionEffect effect : im.getCustomEffects()) {
|
||||||
|
p.addPotionEffect(effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
p.setSaturation(6F);
|
||||||
|
p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_DRINK, 1F, 1F);
|
||||||
|
inv.setItem(slot, null);
|
||||||
|
backpack.markDirty();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.setSaturation(6F);
|
|
||||||
p.playSound(p.getLocation(), Sound.ENTITY_GENERIC_DRINK, 1F, 1F);
|
|
||||||
inv.setItem(slot, null);
|
|
||||||
backpack.markDirty();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import be.seeseemelk.mockbukkit.MockBukkit;
|
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||||
import be.seeseemelk.mockbukkit.ServerMock;
|
import be.seeseemelk.mockbukkit.ServerMock;
|
||||||
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
|
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
|
||||||
|
import io.github.thebusybiscuit.slimefun4.api.events.CoolerFeedPlayerEvent;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack;
|
import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack;
|
||||||
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
|
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
|
||||||
@ -81,6 +82,8 @@ class TestCoolerListener {
|
|||||||
|
|
||||||
FoodLevelChangeEvent event = new FoodLevelChangeEvent(player, 16);
|
FoodLevelChangeEvent event = new FoodLevelChangeEvent(player, 16);
|
||||||
listener.onHungerLoss(event);
|
listener.onHungerLoss(event);
|
||||||
|
|
||||||
Assertions.assertTrue(player.hasPotionEffect(PotionEffectType.HEALTH_BOOST));
|
Assertions.assertTrue(player.hasPotionEffect(PotionEffectType.HEALTH_BOOST));
|
||||||
|
server.getPluginManager().assertEventFired(CoolerFeedPlayerEvent.class, e -> e.getPlayer() == player && e.getCooler() == cooler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user