Repository navigation
Credit ingredients added to BreweryX cauldrons #82
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/net/tfminecraft/activitytf/listeners/BreweryListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package net.tfminecraft.activitytf.listeners; | ||
|
|
||
| import com.dre.brewery.api.events.IngedientAddEvent; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.EventPriority; | ||
| import org.bukkit.event.Listener; | ||
| import net.tfminecraft.activitytf.managers.ActivityManager; | ||
|
|
||
| public class BreweryListener implements Listener { | ||
|
|
||
| private final ActivityManager manager; | ||
|
|
||
| public BreweryListener(ActivityManager manager) { | ||
| this.manager = manager; | ||
| } | ||
|
|
||
| // BreweryX only fires this for an item a cauldron recipe accepts, once per item added. | ||
| @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
| public void onIngredientAdd(IngedientAddEvent event) { | ||
| if (event.getPlayer() == null) { | ||
| return; | ||
| } | ||
| manager.recordAction(event.getPlayer().getUniqueId(), "brew_ingredient", 1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/java/net/tfminecraft/activitytf/listeners/BreweryListenerHandlersTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package net.tfminecraft.activitytf.listeners; | ||
|
|
||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.EventPriority; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.lang.reflect.Method; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class BreweryListenerHandlersTest { | ||
|
|
||
| // BreweryListenerTest calls the handlers directly, which skips Bukkit's ignoreCancelled filter. | ||
| @Test | ||
| void everyHandlerSkipsCancelledEventsAtMonitor() { | ||
| int handlers = 0; | ||
| for (Method method : BreweryListener.class.getDeclaredMethods()) { | ||
| EventHandler handler = method.getAnnotation(EventHandler.class); | ||
| if (handler == null) { | ||
| continue; | ||
| } | ||
| handlers++; | ||
| assertTrue(handler.ignoreCancelled(), method.getName() + " must ignore cancelled events"); | ||
| assertEquals(EventPriority.MONITOR, handler.priority(), method.getName()); | ||
| } | ||
| assertTrue(handlers > 0); | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
src/test/java/net/tfminecraft/activitytf/listeners/BreweryListenerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package net.tfminecraft.activitytf.listeners; | ||
|
|
||
| import com.dre.brewery.api.events.IngedientAddEvent; | ||
| import net.tfminecraft.activitytf.managers.ActivityManager; | ||
| import net.tfminecraft.activitytf.managers.TestManagers; | ||
| import net.tfminecraft.activitytf.models.ActivityDef; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.player.PlayerEvent; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.objenesis.ObjenesisStd; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationHandler; | ||
| import java.lang.reflect.Proxy; | ||
| import java.util.UUID; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class BreweryListenerTest { | ||
|
|
||
| private static Player stubPlayer(UUID uuid) { | ||
| InvocationHandler handler = (proxy, method, args) -> switch (method.getName()) { | ||
| case "getUniqueId" -> uuid; | ||
| case "toString" -> "stub-player"; | ||
| case "hashCode" -> uuid.hashCode(); | ||
| case "equals" -> proxy == args[0]; | ||
| default -> throw new UnsupportedOperationException( | ||
| "unexpected call to Player#" + method.getName() + " - this test double only answers getUniqueId()"); | ||
| }; | ||
| return (Player) Proxy.newProxyInstance( | ||
| BreweryListenerTest.class.getClassLoader(), new Class<?>[]{Player.class}, handler); | ||
| } | ||
|
|
||
| static void set(Class<?> owner, Object target, String name, Object value) { | ||
| try { | ||
| Field field = owner.getDeclaredField(name); | ||
| field.setAccessible(true); | ||
| field.set(target, value); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private static IngedientAddEvent ingredient(Player player) { | ||
| IngedientAddEvent event = new ObjenesisStd().newInstance(IngedientAddEvent.class); | ||
| set(PlayerEvent.class, event, "player", player); | ||
| return event; | ||
| } | ||
|
|
||
| /** A manager with every given activity revealed for the returned player. */ | ||
| static UUID revealed(ActivityManager manager) { | ||
| TestManagers.bukkit(); | ||
| TestManagers.storeLoaded(manager); | ||
| UUID uuid = UUID.randomUUID(); | ||
| for (int slot = 0; slot < manager.tasks(uuid).tasks().size(); slot++) { | ||
| manager.reveal(uuid, slot); | ||
| } | ||
| return uuid; | ||
| } | ||
|
|
||
| static ActivityManager managerWith(String... ids) { | ||
| ActivityDef[] defs = new ActivityDef[ids.length]; | ||
| for (int i = 0; i < ids.length; i++) { | ||
| defs[i] = new ActivityDef(ids[i], ids[i], Material.POTION, null, 1, 1, 0); | ||
| } | ||
| ActivityManager manager = TestManagers.manager(defs); | ||
| TestManagers.guarantee(manager, ids); | ||
| return manager; | ||
| } | ||
|
|
||
| @Test | ||
| void anIngredientWithoutAPlayerIsIgnored() { | ||
| BreweryListener listener = new BreweryListener(null); | ||
| assertDoesNotThrow(() -> listener.onIngredientAdd(ingredient(null))); | ||
| } | ||
|
|
||
| @Test | ||
| void eachIngredientAddedCreditsOne() { | ||
| ActivityManager manager = managerWith("brew_ingredient"); | ||
| UUID uuid = revealed(manager); | ||
| BreweryListener listener = new BreweryListener(manager); | ||
|
|
||
| listener.onIngredientAdd(ingredient(stubPlayer(uuid))); | ||
| listener.onIngredientAdd(ingredient(stubPlayer(uuid))); | ||
|
|
||
| assertEquals(2, manager.tasks(uuid).count("brew_ingredient")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.