Search in sources :

Example 1 with CraftMetaItem

use of org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem in project Mohist by MohistMC.

the class CraftItemStack method setItemMeta.

public static boolean setItemMeta(net.minecraft.world.item.ItemStack item, ItemMeta itemMeta) {
    if (item == null) {
        return false;
    }
    if (CraftItemFactory.instance().equals(itemMeta, null)) {
        item.setTag(null);
        return true;
    }
    if (!CraftItemFactory.instance().isApplicable(itemMeta, getType(item))) {
        return false;
    }
    itemMeta = CraftItemFactory.instance().asMetaFor(itemMeta, getType(item));
    if (itemMeta == null)
        return true;
    Item oldItem = item.getItem();
    Item newItem = CraftMagicNumbers.getItem(CraftItemFactory.instance().updateMaterial(itemMeta, CraftMagicNumbers.getMaterial(oldItem)));
    if (oldItem != newItem) {
        item.setItem(newItem);
    }
    CompoundTag tag = new CompoundTag();
    item.setTag(tag);
    ((CraftMetaItem) itemMeta).applyToItem(tag);
    item.convertStack(((CraftMetaItem) itemMeta).getVersion());
    // SpigotCraft#463 this is required now by the Vanilla client, so mimic ItemStack constructor in ensuring it
    if (item.getItem() != null && item.getItem().canBeDepleted()) {
        item.setDamageValue(item.getDamageValue());
    }
    return true;
}
Also used : Item(net.minecraft.world.item.Item) CraftMetaItem(org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem) CraftMetaItem(org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with CraftMetaItem

use of org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem in project Magma by magmafoundation.

the class ItemMetaImplementationOverrideTest method data.

@Parameters(name = "[{index}]:{1}")
public static List<Object[]> data() {
    final List<Object[]> testData = new ArrayList<Object[]>();
    List<Class<? extends CraftMetaItem>> classes = new ArrayList<Class<? extends CraftMetaItem>>();
    for (Material material : ItemStackTest.COMPOUND_MATERIALS) {
        Class<? extends CraftMetaItem> clazz = CraftItemFactory.instance().getItemMeta(material).getClass().asSubclass(parent);
        if (clazz != parent) {
            classes.add(clazz);
        }
    }
    List<Method> list = new ArrayList<Method>();
    for (Method method : parent.getDeclaredMethods()) {
        if (method.isAnnotationPresent(Overridden.class)) {
            list.add(method);
        }
    }
    for (final Class<?> clazz : classes) {
        for (final Method method : list) {
            testData.add(new Object[] { new Callable<Method>() {

                public Method call() throws Exception {
                    return clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
                }
            }, clazz.getSimpleName() + " contains " + method.getName() });
        }
        testData.add(new Object[] { new Callable<DelegateDeserialization>() {

            public DelegateDeserialization call() throws Exception {
                return clazz.getAnnotation(DelegateDeserialization.class);
            }
        }, clazz.getSimpleName() + " contains annotation " + DelegateDeserialization.class });
    }
    return testData;
}
Also used : ArrayList(java.util.ArrayList) Material(org.bukkit.Material) Method(java.lang.reflect.Method) CraftMetaItem(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem) DelegateDeserialization(org.bukkit.configuration.serialization.DelegateDeserialization) Parameters(org.junit.runners.Parameterized.Parameters)

Example 3 with CraftMetaItem

use of org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem in project Magma by magmafoundation.

the class FactoryItemMaterialTest method generalCase.

@Test
public void generalCase() {
    CraftMetaItem meta = (CraftMetaItem) factory.getItemMeta(material);
    if (meta == null) {
        assertThat(material, is(Material.AIR));
    } else {
        assertTrue(factory.isApplicable(meta, material));
        assertTrue(meta.applicableTo(material));
        meta = meta.clone();
        assertTrue(factory.isApplicable(meta, material));
        assertTrue(meta.applicableTo(material));
    }
}
Also used : CraftMetaItem(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem) Test(org.junit.Test)

Example 4 with CraftMetaItem

use of org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem in project Magma by magmafoundation.

the class FactoryItemMaterialTest method asMetaFor.

@Ignore
@Test
public void asMetaFor() {
    final CraftMetaItem baseMeta = (CraftMetaItem) factory.getItemMeta(material);
    if (baseMeta == null) {
        assertThat(material, is(Material.AIR));
        return;
    }
    for (Material other : materials) {
        final ItemStack bukkitStack = new ItemStack(other);
        final CraftItemStack craftStack = CraftItemStack.asCraftCopy(bukkitStack);
        final CraftMetaItem otherMeta = (CraftMetaItem) factory.asMetaFor(baseMeta, other);
        final String testName = name(material, other);
        if (otherMeta == null) {
            assertThat(testName, other, is(Material.AIR));
            continue;
        }
        assertTrue(testName, factory.isApplicable(otherMeta, craftStack));
        assertTrue(testName, factory.isApplicable(otherMeta, bukkitStack));
        assertTrue(testName, factory.isApplicable(otherMeta, other));
        assertTrue(testName, otherMeta.applicableTo(other));
    }
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) CraftMetaItem(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem) Material(org.bukkit.Material) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with CraftMetaItem

use of org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem in project Magma by magmafoundation.

the class FactoryItemMaterialTest method blankEqualities.

@Test
public void blankEqualities() {
    if (material == Material.AIR) {
        return;
    }
    final CraftMetaItem baseMeta = (CraftMetaItem) factory.getItemMeta(material);
    final CraftMetaItem baseMetaClone = baseMeta.clone();
    final ItemStack baseMetaStack = new ItemStack(material);
    baseMetaStack.setItemMeta(baseMeta);
    assertThat(baseMeta, is(not(sameInstance(baseMetaStack.getItemMeta()))));
    assertTrue(factory.equals(baseMeta, null));
    assertTrue(factory.equals(null, baseMeta));
    assertTrue(factory.equals(baseMeta, baseMetaClone));
    assertTrue(factory.equals(baseMetaClone, baseMeta));
    assertThat(baseMeta, is(not(sameInstance(baseMetaClone))));
    assertThat(baseMeta, is(baseMetaClone));
    assertThat(baseMetaClone, is(baseMeta));
    for (Material other : materials) {
        final String testName = name(material, other);
        final CraftMetaItem otherMeta = (CraftMetaItem) factory.asMetaFor(baseMetaClone, other);
        if (otherMeta == null) {
            assertThat(testName, other, is(Material.AIR));
            continue;
        }
        assertTrue(testName, factory.equals(baseMeta, otherMeta));
        assertTrue(testName, factory.equals(otherMeta, baseMeta));
        assertThat(testName, baseMeta, is(otherMeta));
        assertThat(testName, otherMeta, is(baseMeta));
        assertThat(testName, baseMeta.hashCode(), is(otherMeta.hashCode()));
    }
}
Also used : CraftMetaItem(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem) Material(org.bukkit.Material) CraftItemStack(org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack) ItemStack(org.bukkit.inventory.ItemStack) Test(org.junit.Test)

Aggregations

CraftMetaItem (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftMetaItem)4 Material (org.bukkit.Material)3 Test (org.junit.Test)3 CraftItemStack (org.bukkit.craftbukkit.v1_12_R1.inventory.CraftItemStack)2 ItemStack (org.bukkit.inventory.ItemStack)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 Item (net.minecraft.world.item.Item)1 DelegateDeserialization (org.bukkit.configuration.serialization.DelegateDeserialization)1 Overridden (org.bukkit.craftbukkit.v1_18_R2.Overridden)1 CraftMetaItem (org.bukkit.craftbukkit.v1_18_R2.inventory.CraftMetaItem)1 CraftPersistentDataContainer (org.bukkit.craftbukkit.v1_18_R2.persistence.CraftPersistentDataContainer)1 Enchantment (org.bukkit.enchantments.Enchantment)1 Ignore (org.junit.Ignore)1 Parameters (org.junit.runners.Parameterized.Parameters)1