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

Add TalismanActivateEvent (Updated version of #3920) #4045

Merged
merged 25 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bdbb9ad
Update Talisman.java
Sniperkaos Jul 25, 2023
0bb8cb9
Adds TalismanActivatedEvent to Slimefun.
Jul 25, 2023
f7b7566
implements cancellable
Jul 25, 2023
c449de2
Update TalismanActivatedEvent.java
Sniperkaos Jul 25, 2023
3520e21
added cancelling logic & changed variable names to camel case
Jul 26, 2023
9612538
forgot to remove some TODO stubs
Jul 26, 2023
4144075
Update src/main/java/io/github/thebusybiscuit/slimefun4/api/events/Ta…
Sniperkaos Jul 26, 2023
de78695
Update src/main/java/io/github/thebusybiscuit/slimefun4/api/events/Ta…
Sniperkaos Jul 26, 2023
1b2c9e0
Merge branch 'master' of https://github.com/Sniperkaos/TalismanEvent.git
Jul 26, 2023
2d248e5
Update src/main/java/io/github/thebusybiscuit/slimefun4/implementatio…
Sniperkaos Jul 26, 2023
ffb8899
Update src/main/java/io/github/thebusybiscuit/slimefun4/implementatio…
Sniperkaos Jul 26, 2023
3c6803e
Update src/main/java/io/github/thebusybiscuit/slimefun4/implementatio…
Sniperkaos Jul 26, 2023
a294e54
Add original PR
JustAHuman-xD Dec 7, 2023
903144e
Suggestions, Formatting, and cleanup
JustAHuman-xD Dec 7, 2023
cd520d0
remove empty line
JustAHuman-xD Dec 7, 2023
0a77680
Add test *and change some logic*
JustAHuman-xD Dec 8, 2023
c28244c
Added negation tests
Sfiguz7 Dec 12, 2023
657f380
Update src/test/java/io/github/thebusybiscuit/slimefun4/api/events/Te…
Sfiguz7 Dec 12, 2023
2b41243
Update src/test/java/io/github/thebusybiscuit/slimefun4/api/events/Te…
Sfiguz7 Dec 12, 2023
5c09027
Update src/test/java/io/github/thebusybiscuit/slimefun4/api/events/Te…
Sfiguz7 Dec 12, 2023
012da24
Update src/test/java/io/github/thebusybiscuit/slimefun4/api/events/Te…
Sfiguz7 Dec 12, 2023
e7d2500
Update src/test/java/io/github/thebusybiscuit/slimefun4/api/events/Te…
Sfiguz7 Dec 12, 2023
8509070
Update src/test/java/io/github/thebusybiscuit/slimefun4/api/events/Te…
Sfiguz7 Dec 12, 2023
2e16d2b
Update src/test/java/io/github/thebusybiscuit/slimefun4/api/events/Te…
Sfiguz7 Dec 12, 2023
74e6f25
Apply suggestions from code review
Sfiguz7 Dec 12, 2023
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
@@ -0,0 +1,96 @@
package io.github.thebusybiscuit.slimefun4.api.events;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.implementation.items.magical.talismans.Talisman;

/**
* This {@link PlayerEvent} is called when a {@link Player} activates a {@link Talisman}
*
* @author cworldstar
*/
public class TalismanActivateEvent extends PlayerEvent implements Cancellable {

private static final HandlerList handlers = new HandlerList();
private final Talisman talisman;
private final ItemStack talismanItemStack;
private boolean preventConsumption = false;
private boolean cancelled = false;

/**
* @param player
* The {@link Player} who activated the talisman.
*
* @param talisman
* The {@link Talisman} that was activated.
*
* @param talismanItem
* The {@link ItemStack} corresponding to the Talisman.
*/
@ParametersAreNonnullByDefault
public TalismanActivateEvent(Player player, Talisman talisman, ItemStack talismanItem) {
super(player);
this.talisman = talisman;
this.talismanItemStack = talismanItem;
}

/**
* @return The {@link Talisman} used.
*/
public @Nonnull Talisman getTalisman() {
return this.talisman;
}

/**
* @return The {@link ItemStack} of the used {@link Talisman}.
*/
public @Nonnull ItemStack getTalismanItem() {
return this.talismanItemStack;
}

/**
* Only applies if {@link Talisman#isConsumable()} is true.
* Defaults to false.
*
* @return Whether the {@link ItemStack} should not be consumed.
*/
public boolean preventsConsumption() {
return this.preventConsumption;
}

/**
* Only applies if {@link Talisman#isConsumable()} is true.
*
* @param preventConsumption
* Whether the {@link ItemStack} should not be consumed.
*/
public void setPreventConsumption(boolean preventConsumption) {
this.preventConsumption = preventConsumption;
}

@Override
public boolean isCancelled() {
return this.cancelled;
}

@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}

@Override
public @Nonnull HandlerList getHandlers() {
return getHandlerList();
}

public static @Nonnull HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.annotation.ParametersAreNonnullByDefault;

import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
Expand All @@ -27,6 +28,7 @@

import io.github.bakedlibs.dough.items.CustomItemStack;
import io.github.bakedlibs.dough.items.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.events.TalismanActivateEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
Expand Down Expand Up @@ -186,11 +188,15 @@ public static boolean trigger(Event e, SlimefunItem item, boolean sendMessage) {
return false;
}
} else {
ItemStack enderTalisman = talisman.getEnderVariant();
SlimefunItemStack enderTalismanItem = talisman.getEnderVariant();
if (enderTalismanItem == null) {
return false;
}

if (SlimefunUtils.containsSimilarItem(p.getEnderChest(), enderTalisman, true)) {
EnderTalisman enderTalisman = enderTalismanItem.getItem(EnderTalisman.class);
if (enderTalisman != null && SlimefunUtils.containsSimilarItem(p.getEnderChest(), enderTalismanItem, true)) {
if (talisman.canUse(p, true)) {
activateTalisman(e, p, p.getEnderChest(), talisman, enderTalisman, sendMessage);
activateTalisman(e, p, p.getEnderChest(), enderTalisman, enderTalismanItem, sendMessage);
return true;
} else {
return false;
Expand All @@ -203,12 +209,19 @@ public static boolean trigger(Event e, SlimefunItem item, boolean sendMessage) {

@ParametersAreNonnullByDefault
private static void activateTalisman(Event e, Player p, Inventory inv, Talisman talisman, ItemStack talismanItem, boolean sendMessage) {
consumeItem(inv, talisman, talismanItem);
applyTalismanEffects(p, talisman);
cancelEvent(e, talisman);
TalismanActivateEvent talismanEvent = new TalismanActivateEvent(p, talisman, talismanItem);
Bukkit.getPluginManager().callEvent(talismanEvent);
if (!talismanEvent.isCancelled()) {
if (!talismanEvent.preventsConsumption()) {
consumeItem(inv, talisman, talismanItem);
}

if (sendMessage) {
talisman.sendMessage(p);
applyTalismanEffects(p, talisman);
cancelEvent(e, talisman);

if (sendMessage) {
talisman.sendMessage(p);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package io.github.thebusybiscuit.slimefun4.api.events;

import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.items.magical.talismans.Talisman;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.TalismanListener;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerItemBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class TestTalismanActivateEvent {

private static ServerMock server;
private static Slimefun plugin;
private static Player player;
private static SlimefunItem talisman;
private static SlimefunItem enderTalisman;

@BeforeAll
public static void load() {
server = MockBukkit.mock();
plugin = MockBukkit.load(Slimefun.class);

new TalismanListener(plugin);

talisman = new Talisman(SlimefunItems.TALISMAN_ANVIL, new ItemStack[] {}, true, false, "anvil");
talisman.register(plugin);

enderTalisman = SlimefunItem.getById("ENDER_" + talisman.getId());

player = server.addPlayer();
}

@AfterAll
public static void unload() {
MockBukkit.unmock();
}

void activateAnvilTalisman(boolean ender) {
player.getInventory().clear();
player.getEnderChest().clear();

ItemStack talismanItem = ender ? enderTalisman.getItem() : talisman.getItem();
ItemStack breakableItem = new ItemStack(Material.IRON_PICKAXE);

if (ender) {
player.getEnderChest().addItem(talismanItem);
} else {
player.getInventory().addItem(talismanItem);
}

player.getInventory().setItemInMainHand(breakableItem);

PlayerItemBreakEvent event = new PlayerItemBreakEvent(player, breakableItem);
server.getPluginManager().callEvent(event);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@DisplayName("Test that TalismanActivateEvent is fired when an anvil talisman activates")
void testEventIsFired() {
// Assert the talisman activates in the inventory
activateAnvilTalisman(false);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, ignored -> true);
server.getPluginManager().clearEvents();

// Assert the talisman activates in the ender chest
activateAnvilTalisman(true);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, ignored -> true);
server.getPluginManager().clearEvents();
}
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved

@Test
@DisplayName("Test that the TalismanActivateEvent has the correct fields")
void testEventFields() {
// Assert the talisman activates in the inventory
activateAnvilTalisman(false);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, event -> {
Assertions.assertEquals(talisman, event.getTalisman());
Assertions.assertEquals(talisman.getItem(), event.getTalismanItem());
Assertions.assertEquals(player, event.getPlayer());
return true;
});
server.getPluginManager().clearEvents();

// Assert the talisman activates in the ender chest
activateAnvilTalisman(true);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, event -> {
Assertions.assertEquals(enderTalisman, event.getTalisman());
Assertions.assertEquals(enderTalisman.getItem(), event.getTalismanItem());
Assertions.assertEquals(player, event.getPlayer());
return true;
});
server.getPluginManager().clearEvents();
}

@Test
@DisplayName("Test that the TalismanActivateEvent can be cancelled")
void testEventCanBeCancelled() {
server.getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onTalismanActivate(TalismanActivateEvent event) {
event.setCancelled(true);
}
}, plugin);

// Assert the talisman activates in the inventory
activateAnvilTalisman(false);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, event -> {
Assertions.assertTrue(event.isCancelled());
return true;
});
server.getPluginManager().clearEvents();

// Assert the talisman activates in the ender chest
activateAnvilTalisman(true);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, event -> {
Assertions.assertTrue(event.isCancelled());
return true;
});
server.getPluginManager().clearEvents();
}

@Test
@DisplayName("Test that the TalismanActivateEvent can prevent consumption")
void testEventCanPreventConsumption() {
server.getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onTalismanActivate(TalismanActivateEvent event) {
event.setPreventConsumption(true);
}
}, plugin);

// Assert the talisman activates in the inventory
activateAnvilTalisman(false);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, event -> {
Assertions.assertTrue(event.preventsConsumption());
return true;
});
server.getPluginManager().clearEvents();

// Assert the talisman activates in the ender chest
activateAnvilTalisman(true);
Sfiguz7 marked this conversation as resolved.
Show resolved Hide resolved
server.getPluginManager().assertEventFired(TalismanActivateEvent.class, event -> {
Assertions.assertTrue(event.preventsConsumption());
return true;
});
server.getPluginManager().clearEvents();
}
}