mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add cursor click test
This commit is contained in:
parent
7bde5bcb04
commit
1b462e8af6
2 changed files with 119 additions and 10 deletions
|
|
@ -0,0 +1,50 @@
|
||||||
|
package xyz.alexcrea.cuanvil.util;
|
||||||
|
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
public record AnvilClickTestData(
|
||||||
|
@Nullable ItemStack leftItem,
|
||||||
|
@Nullable ItemStack rightItem,
|
||||||
|
@Nullable ItemStack resultSlotItem,
|
||||||
|
@Nullable ItemStack expectedCursor,
|
||||||
|
int levelCost,
|
||||||
|
@Nullable Event.Result expectedResult,
|
||||||
|
boolean testNoLevelNoChange,
|
||||||
|
@Nullable Event.Result npChangeResult
|
||||||
|
) {
|
||||||
|
|
||||||
|
public AnvilClickTestData(@Nullable ItemStack leftItem,
|
||||||
|
@Nullable ItemStack rightItem,
|
||||||
|
@Nullable ItemStack resultSlotItem,
|
||||||
|
@Nullable ItemStack expectedCursor,
|
||||||
|
int levelCost,
|
||||||
|
@Nullable Event.Result expectedResult) {
|
||||||
|
this(leftItem, rightItem, resultSlotItem,
|
||||||
|
expectedCursor, levelCost, expectedResult,
|
||||||
|
false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnvilClickTestData(@Nullable ItemStack leftItem,
|
||||||
|
@Nullable ItemStack rightItem,
|
||||||
|
@Nullable ItemStack resultSlotItem,
|
||||||
|
@Nullable ItemStack expectedCursor,
|
||||||
|
int levelCost) {
|
||||||
|
this(leftItem, rightItem, resultSlotItem,
|
||||||
|
expectedCursor, levelCost, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnvilClickTestData(@Nullable ItemStack expectedCursor,
|
||||||
|
int levelCost,
|
||||||
|
@Nullable Event.Result expectedResult) {
|
||||||
|
this(null, null, null,
|
||||||
|
expectedCursor, levelCost, expectedResult,
|
||||||
|
false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnvilClickTestData(@Nullable ItemStack expectedCursor,
|
||||||
|
int levelCost) {
|
||||||
|
this(expectedCursor, levelCost, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,9 @@ package xyz.alexcrea.cuanvil.util;
|
||||||
import io.delilaheve.util.ItemUtil;
|
import io.delilaheve.util.ItemUtil;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.entity.HumanEntity;
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.event.inventory.PrepareAnvilEvent;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.inventory.*;
|
||||||
import org.bukkit.inventory.AnvilInventory;
|
import org.bukkit.inventory.AnvilInventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.ItemMeta;
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
|
@ -12,6 +14,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
|
import xyz.alexcrea.cuanvil.listener.AnvilResultListener;
|
||||||
import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener;
|
import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener;
|
||||||
import xyz.alexcrea.cuanvil.mock.AnvilViewMock;
|
import xyz.alexcrea.cuanvil.mock.AnvilViewMock;
|
||||||
|
|
||||||
|
|
@ -21,6 +24,9 @@ import java.util.List;
|
||||||
|
|
||||||
public class AnvilFuseTestUtil {
|
public class AnvilFuseTestUtil {
|
||||||
|
|
||||||
|
private static PrepareAnvilListener PREPARE_LISTENER = new PrepareAnvilListener();
|
||||||
|
private static AnvilResultListener RESULT_LISTENER = new AnvilResultListener();
|
||||||
|
|
||||||
public static ItemStack prepareItem(@NotNull Material material,
|
public static ItemStack prepareItem(@NotNull Material material,
|
||||||
@NotNull List<CAEnchantment> enchantments,
|
@NotNull List<CAEnchantment> enchantments,
|
||||||
@NotNull List<Integer> level){
|
@NotNull List<Integer> level){
|
||||||
|
|
@ -86,7 +92,7 @@ public class AnvilFuseTestUtil {
|
||||||
PrepareAnvilEvent event = new PrepareAnvilEvent(view, anvil.getItem(2));
|
PrepareAnvilEvent event = new PrepareAnvilEvent(view, anvil.getItem(2));
|
||||||
|
|
||||||
// Not ideal but possible and the easiest so why not
|
// Not ideal but possible and the easiest so why not
|
||||||
new PrepareAnvilListener().anvilCombineCheck(event);
|
PREPARE_LISTENER.anvilCombineCheck(event);
|
||||||
anvil.setResult(event.getResult());
|
anvil.setResult(event.getResult());
|
||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
Assertions.fail(e);
|
Assertions.fail(e);
|
||||||
|
|
@ -117,9 +123,62 @@ public class AnvilFuseTestUtil {
|
||||||
testPlacingItem(anvil, player,
|
testPlacingItem(anvil, player,
|
||||||
0, data.expectedPriceAfterBothPlaced(),
|
0, data.expectedPriceAfterBothPlaced(),
|
||||||
data.leftItem(), data.expectedResult());
|
data.leftItem(), data.expectedResult());
|
||||||
|
}
|
||||||
|
|
||||||
// Sadly, can't currently test player click
|
public static void executeAnvilClick(
|
||||||
|
@NotNull AnvilInventory anvil,
|
||||||
|
@NotNull Player player,
|
||||||
|
@NotNull AnvilClickTestData data
|
||||||
|
) {
|
||||||
|
if(data.testNoLevelNoChange()){
|
||||||
|
ItemStack left = anvil.getFirstItem();
|
||||||
|
ItemStack right = anvil.getSecondItem();
|
||||||
|
ItemStack result = anvil.getResult();
|
||||||
|
|
||||||
|
player.setLevel(0);
|
||||||
|
player.setItemOnCursor(null);
|
||||||
|
|
||||||
|
// Do a test with not enough level
|
||||||
|
simulateClick(anvil, player, data.npChangeResult());
|
||||||
|
|
||||||
|
// Nothing should have changed
|
||||||
|
assertEqual(left, anvil.getFirstItem());
|
||||||
|
assertEqual(right, anvil.getSecondItem());
|
||||||
|
assertEqual(result, anvil.getResult());
|
||||||
|
assertEqual(null, player.getItemOnCursor());
|
||||||
|
}
|
||||||
|
player.setLevel(data.levelCost());
|
||||||
|
player.setItemOnCursor(null);
|
||||||
|
|
||||||
|
simulateClick(anvil, player, data.expectedResult());
|
||||||
|
|
||||||
|
// Nothing should have changed
|
||||||
|
assertEqual(data.leftItem(), anvil.getFirstItem());
|
||||||
|
assertEqual(data.rightItem(), anvil.getSecondItem());
|
||||||
|
assertEqual(data.resultSlotItem(), anvil.getResult());
|
||||||
|
assertEqual(data.expectedCursor(), data.expectedCursor());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void simulateClick(
|
||||||
|
@NotNull AnvilInventory anvil,
|
||||||
|
@NotNull Player player,
|
||||||
|
@Nullable Event.Result expectedResult
|
||||||
|
){
|
||||||
|
AnvilViewMock view = new AnvilViewMock(player, anvil);
|
||||||
|
try {
|
||||||
|
InventoryClickEvent event = new InventoryClickEvent(view,
|
||||||
|
InventoryType.SlotType.RESULT,
|
||||||
|
PrepareAnvilListener.ANVIL_OUTPUT_SLOT,
|
||||||
|
ClickType.LEFT,
|
||||||
|
InventoryAction.PICKUP_ALL);
|
||||||
|
|
||||||
|
RESULT_LISTENER.anvilExtractionCheck(event);
|
||||||
|
if(expectedResult != null){
|
||||||
|
Assertions.assertEquals(expectedResult, event.getResult());
|
||||||
|
}
|
||||||
|
} catch (Exception e){
|
||||||
|
Assertions.fail(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings({"removal"})
|
@SuppressWarnings({"removal"})
|
||||||
|
|
@ -140,15 +199,15 @@ public class AnvilFuseTestUtil {
|
||||||
assertPriceEqual(expectedPrice, anvil.getRepairCost());
|
assertPriceEqual(expectedPrice, anvil.getRepairCost());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertEqual(@Nullable ItemStack item1, @Nullable ItemStack item2) {
|
public static void assertEqual(@Nullable ItemStack expected, @Nullable ItemStack other) {
|
||||||
boolean secondIsAir = isAir(item2);
|
boolean secondIsAir = isAir(other);
|
||||||
if(isAir(item1)) Assertions.assertTrue(secondIsAir,"Item "+item2+" was not AIR but was expected to be air");
|
if(isAir(expected)) Assertions.assertTrue(secondIsAir,"Item "+other+" was not AIR but was expected to be air");
|
||||||
else {
|
else {
|
||||||
Assertions.assertFalse(secondIsAir,"Item "+item2+" was expected not to be air");
|
Assertions.assertFalse(secondIsAir,"Item "+other+" was expected not to be air");
|
||||||
|
|
||||||
item1.setDurability(item1.getDurability());
|
expected.setDurability(expected.getDurability());
|
||||||
item2.setDurability(item2.getDurability());
|
other.setDurability(other.getDurability());
|
||||||
Assertions.assertEquals(item1, item2);
|
Assertions.assertEquals(expected, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue