mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Add enchantment item meta mock & Anvil fuse bypass permission test
Also fixed durability being set for item that should be at -1
This commit is contained in:
parent
309dedc383
commit
304abd89d7
7 changed files with 231 additions and 9 deletions
102
src/test/java/io/delilaheve/util/EnchantmentUtilTests.java
Normal file
102
src/test/java/io/delilaheve/util/EnchantmentUtilTests.java
Normal file
|
|
@ -0,0 +1,102 @@
|
||||||
|
package io.delilaheve.util;
|
||||||
|
|
||||||
|
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||||
|
import be.seeseemelk.mockbukkit.inventory.ItemStackMock;
|
||||||
|
import io.delilaheve.CustomAnvil;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.event.inventory.InventoryType;
|
||||||
|
import org.bukkit.inventory.AnvilInventory;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.permissions.PermissionAttachment;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||||
|
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
|
||||||
|
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
|
||||||
|
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class EnchantmentUtilTests extends DefaultCustomAnvilTest {
|
||||||
|
|
||||||
|
private AnvilInventory anvil;
|
||||||
|
private PlayerMock player;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
super.setUp();
|
||||||
|
// Mock used player & open anvil
|
||||||
|
player = server.addPlayer();
|
||||||
|
|
||||||
|
Inventory anvil = server.createInventory(player, InventoryType.ANVIL);
|
||||||
|
|
||||||
|
this.anvil = (AnvilInventory) anvil;
|
||||||
|
player.openInventory(anvil);
|
||||||
|
|
||||||
|
ConfigHolder.DEFAULT_CONFIG.getConfig().set("debug_log", true);
|
||||||
|
ConfigHolder.DEFAULT_CONFIG.getConfig().set("debug_log_verbose", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBypassFuse(){
|
||||||
|
// Test permission did not changed (if it do then server owner should be warned)
|
||||||
|
String permission = CustomAnvil.bypassFusePermission;
|
||||||
|
Assertions.assertEquals("ca.bypass.fuse", permission, "bypass fuse permission changed. " +
|
||||||
|
"Caution with that as it will break some server CustomAnvil setup.");
|
||||||
|
|
||||||
|
// Create item
|
||||||
|
ItemStack normalStick = new ItemStackMock(Material.STICK);
|
||||||
|
ItemStack sharpnessBook = AnvilFuseTestUtil.prepareItem(
|
||||||
|
Material.ENCHANTED_BOOK,
|
||||||
|
List.of("sharpness"), 1);
|
||||||
|
|
||||||
|
ItemStack sharpnessStick = AnvilFuseTestUtil.prepareItem(
|
||||||
|
Material.STICK,
|
||||||
|
List.of("sharpness"), 1);
|
||||||
|
|
||||||
|
ItemStack sharpnessResultStick = AnvilFuseTestUtil.prepareItem(
|
||||||
|
Material.STICK, 1,
|
||||||
|
List.of("sharpness"), 1);
|
||||||
|
ItemStack sharpness2ResultStick = AnvilFuseTestUtil.prepareItem(
|
||||||
|
Material.STICK, 1,
|
||||||
|
List.of("sharpness"), 2);
|
||||||
|
|
||||||
|
// Create anvil fuse data
|
||||||
|
AnvilFuseTestData nullResultData = new AnvilFuseTestData(
|
||||||
|
normalStick, sharpnessBook,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
AnvilFuseTestData nullResultData2 = new AnvilFuseTestData(
|
||||||
|
sharpnessStick, sharpnessStick,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
|
AnvilFuseTestData legalResultData = new AnvilFuseTestData(
|
||||||
|
normalStick, sharpnessBook,
|
||||||
|
sharpnessResultStick
|
||||||
|
// TODO add expected price
|
||||||
|
);
|
||||||
|
AnvilFuseTestData legalResultData2 = new AnvilFuseTestData(
|
||||||
|
sharpnessStick, sharpnessStick,
|
||||||
|
sharpness2ResultStick
|
||||||
|
// TODO add expected price
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test with no permission
|
||||||
|
AnvilFuseTestUtil.executeAnvilTest(anvil, player, nullResultData);
|
||||||
|
AnvilFuseTestUtil.executeAnvilTest(anvil, player, nullResultData2);
|
||||||
|
|
||||||
|
// Add permission
|
||||||
|
PermissionAttachment attachment = player.addAttachment(plugin);
|
||||||
|
attachment.setPermission(permission, true);
|
||||||
|
|
||||||
|
// Test with new permission
|
||||||
|
AnvilFuseTestUtil.executeAnvilTest(anvil, player, legalResultData);
|
||||||
|
AnvilFuseTestUtil.executeAnvilTest(anvil, player, legalResultData2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -7,8 +7,8 @@ import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
|
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||||
|
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
|
||||||
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
|
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
|
||||||
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;
|
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;
|
||||||
import xyz.alexcrea.cuanvil.util.CommonItemUtil;
|
import xyz.alexcrea.cuanvil.util.CommonItemUtil;
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,19 @@
|
||||||
package xyz.alexcrea.cuanvil.api;
|
package xyz.alexcrea.cuanvil.api;
|
||||||
|
|
||||||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||||
|
import org.bukkit.Material;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.event.inventory.InventoryType;
|
import org.bukkit.event.inventory.InventoryType;
|
||||||
import org.bukkit.inventory.AnvilInventory;
|
import org.bukkit.inventory.AnvilInventory;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import xyz.alexcrea.cuanvil.tests.ConfigResetCustomAnvilTest;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||||
|
import xyz.alexcrea.cuanvil.tests.ConfigResetCustomAnvilTest;
|
||||||
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
|
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
|
||||||
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;
|
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;
|
||||||
import xyz.alexcrea.cuanvil.util.CommonItemUtil;
|
import xyz.alexcrea.cuanvil.util.CommonItemUtil;
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,10 @@ import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.Arguments;
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
|
|
||||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
|
||||||
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
|
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
|
||||||
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;
|
||||||
|
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
|
||||||
|
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,64 @@
|
||||||
|
package xyz.alexcrea.cuanvil.mock;
|
||||||
|
|
||||||
|
import be.seeseemelk.mockbukkit.inventory.meta.ItemMetaMock;
|
||||||
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class EnchantedItemMetaMock extends ItemMetaMock implements EnchantmentStorageMeta {
|
||||||
|
|
||||||
|
|
||||||
|
public EnchantedItemMetaMock() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public EnchantedItemMetaMock(@NotNull ItemMeta meta) {
|
||||||
|
super(meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasStoredEnchants() {
|
||||||
|
return super.hasEnchants();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasStoredEnchant(@NotNull Enchantment ench) {
|
||||||
|
return super.hasEnchant(ench);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getStoredEnchantLevel(@NotNull Enchantment ench) {
|
||||||
|
return super.getEnchantLevel(ench);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NotNull Map<Enchantment, Integer> getStoredEnchants() {
|
||||||
|
return super.getEnchants();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addStoredEnchant(@NotNull Enchantment ench, int level, boolean ignoreLevelRestriction) {
|
||||||
|
return super.addEnchant(ench, level, ignoreLevelRestriction);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeStoredEnchant(@NotNull Enchantment ench) throws IllegalArgumentException {
|
||||||
|
return super.removeEnchant(ench);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasConflictingStoredEnchant(@NotNull Enchantment ench) {
|
||||||
|
return super.hasConflictingEnchant(ench);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EnchantedItemMetaMock clone() {
|
||||||
|
// Not ideal but we do with what we have
|
||||||
|
return new EnchantedItemMetaMock(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,28 +1,32 @@
|
||||||
package xyz.alexcrea.cuanvil.mock;
|
package xyz.alexcrea.cuanvil.mock;
|
||||||
|
|
||||||
import be.seeseemelk.mockbukkit.inventory.ItemStackMock;
|
import be.seeseemelk.mockbukkit.inventory.ItemStackMock;
|
||||||
|
import be.seeseemelk.mockbukkit.inventory.meta.ItemMetaMock;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class EnchantedItemStackMock extends ItemStackMock {
|
public class EnchantedItemStackMock extends ItemStackMock {
|
||||||
|
|
||||||
EnchantedItemStackMock(){}
|
|
||||||
|
|
||||||
public EnchantedItemStackMock(@NotNull Material type, int amount) {
|
public EnchantedItemStackMock(@NotNull Material type, int amount) {
|
||||||
super(type, amount);
|
super(type, amount);
|
||||||
|
updateItemMeta();
|
||||||
}
|
}
|
||||||
|
|
||||||
public EnchantedItemStackMock(@NotNull Material type) {
|
public EnchantedItemStackMock(@NotNull Material type) {
|
||||||
this(type, 1);
|
this(type, 1);
|
||||||
|
updateItemMeta();
|
||||||
}
|
}
|
||||||
|
|
||||||
public EnchantedItemStackMock(@NotNull ItemStack stack) throws IllegalArgumentException {
|
public EnchantedItemStackMock(@NotNull ItemStack stack) throws IllegalArgumentException {
|
||||||
super(stack);
|
super(stack);
|
||||||
|
updateItemMeta();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -46,11 +50,44 @@ public class EnchantedItemStackMock extends ItemStackMock {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull ItemStack clone() {
|
public @NotNull ItemStack clone() {
|
||||||
ItemStackMock clone = new EnchantedItemStackMock(this.getType());
|
EnchantedItemStackMock clone = new EnchantedItemStackMock(this.getType());
|
||||||
|
|
||||||
clone.setAmount(this.getAmount());
|
clone.setAmount(this.getAmount());
|
||||||
clone.setDurability(this.getDurability());
|
clone.setDurability(this.getDurability());
|
||||||
clone.setItemMeta(this.hasItemMeta() ? this.getItemMeta().clone() : null);
|
clone.setItemMeta(this.hasItemMeta() ? this.getItemMeta().clone() : null);
|
||||||
|
clone.updateItemMeta();
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setType(@NotNull Material type) {
|
||||||
|
super.setType(type);
|
||||||
|
updateItemMeta();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setItemMeta(@Nullable ItemMeta itemMeta) {
|
||||||
|
boolean success = super.setItemMeta(itemMeta);
|
||||||
|
updateItemMeta();
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDurability(short durability) {
|
||||||
|
if(getType().getMaxDurability() == 0) return;
|
||||||
|
super.setDurability(durability);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateItemMeta() {
|
||||||
|
super.setItemMeta(updateItemMeta(getType(), getItemMeta()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static @Nullable ItemMeta updateItemMeta(Material material, ItemMeta oldMeta) {
|
||||||
|
if(oldMeta == null) return null;
|
||||||
|
if(material != Material.ENCHANTED_BOOK) return oldMeta;
|
||||||
|
if(oldMeta instanceof ItemMetaMock) return new EnchantedItemMetaMock(oldMeta);
|
||||||
|
|
||||||
|
return oldMeta;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.event.inventory.PrepareAnvilEvent;
|
import org.bukkit.event.inventory.PrepareAnvilEvent;
|
||||||
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.Repairable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
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;
|
||||||
|
|
@ -23,6 +25,13 @@ public class AnvilFuseTestUtil {
|
||||||
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){
|
||||||
|
return prepareItem(material, 0, enchantments, level);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack prepareItem(@NotNull Material material,
|
||||||
|
int repairCost,
|
||||||
|
@NotNull List<CAEnchantment> enchantments,
|
||||||
|
@NotNull List<Integer> level){
|
||||||
Assertions.assertEquals(enchantments.size(), level.size());
|
Assertions.assertEquals(enchantments.size(), level.size());
|
||||||
|
|
||||||
HashMap<CAEnchantment, Integer> enchantmentMap = new HashMap<>();
|
HashMap<CAEnchantment, Integer> enchantmentMap = new HashMap<>();
|
||||||
|
|
@ -33,6 +42,10 @@ public class AnvilFuseTestUtil {
|
||||||
ItemStack item = new EnchantedItemStackMock(material);
|
ItemStack item = new EnchantedItemStackMock(material);
|
||||||
ItemUtil.INSTANCE.setEnchantmentsUnsafe(item, enchantmentMap);
|
ItemUtil.INSTANCE.setEnchantmentsUnsafe(item, enchantmentMap);
|
||||||
|
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
((Repairable) meta).setRepairCost(repairCost);
|
||||||
|
item.setItemMeta(meta);
|
||||||
|
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,6 +53,12 @@ public class AnvilFuseTestUtil {
|
||||||
public static ItemStack prepareItem(@NotNull Material material,
|
public static ItemStack prepareItem(@NotNull Material material,
|
||||||
@NotNull List<String> enchantmentNames,
|
@NotNull List<String> enchantmentNames,
|
||||||
Integer... levels){
|
Integer... levels){
|
||||||
|
return prepareItem(material, 0, enchantmentNames, levels);
|
||||||
|
}
|
||||||
|
public static ItemStack prepareItem(@NotNull Material material,
|
||||||
|
int repairCost,
|
||||||
|
@NotNull List<String> enchantmentNames,
|
||||||
|
Integer... levels){
|
||||||
List<CAEnchantment> enchantments = new ArrayList<>();
|
List<CAEnchantment> enchantments = new ArrayList<>();
|
||||||
|
|
||||||
for (String enchantmentName : enchantmentNames) {
|
for (String enchantmentName : enchantmentNames) {
|
||||||
|
|
@ -50,7 +69,7 @@ public class AnvilFuseTestUtil {
|
||||||
enchantments.addAll(enchantmentList);
|
enchantments.addAll(enchantmentList);
|
||||||
}
|
}
|
||||||
|
|
||||||
return prepareItem(material, enchantments, List.of(levels));
|
return prepareItem(material, repairCost, enchantments, List.of(levels));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue