Search in sources :

Example 1 with ItemStackHandle

use of com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle in project BKCommonLib by bergerhealer.

the class NMSItemStack method newInstance.

public static Object newInstance(Material type, int data, int amount) {
    ItemStackHandle instance = ItemStackHandle.newInstance(type);
    instance.setDurability(data);
    instance.setAmountField(amount);
    return instance.getRaw();
}
Also used : ItemStackHandle(com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle)

Example 2 with ItemStackHandle

use of com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle in project BKCommonLib by bergerhealer.

the class IInventoryProxyHook method splitStack.

/* Questionable implementations taken over from EntityMinecartContainer */
@HookMethod("public ItemStack splitStack:???(int i, int j)")
public Object splitStack(int i, int j) {
    if ((i < 0) || (i >= inventory.getSize()) || (j <= 0)) {
        return ItemStackHandle.EMPTY_ITEM.getRaw();
    }
    org.bukkit.inventory.ItemStack item = this.inventory.getItem(i);
    if (ItemUtil.isEmpty(item)) {
        return ItemStackHandle.EMPTY_ITEM.getRaw();
    }
    ItemStackHandle nmsItem = ItemStackHandle.fromBukkit(item);
    if (nmsItem == null) {
        return ItemStackHandle.EMPTY_ITEM.getRaw();
    }
    return ItemStackHandle.getRaw(nmsItem.cloneAndSubtract(j));
}
Also used : ItemStackHandle(com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle)

Example 3 with ItemStackHandle

use of com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle in project BKCommonLib by bergerhealer.

the class ItemUtil method createItem.

/**
 * Creates a new ItemStack that is guaranteed to be a CraftItemStack with a valid NMS ItemStack handle
 *
 * @param type of item
 * @param amount of the item
 * @return ItemStack
 */
public static org.bukkit.inventory.ItemStack createItem(Material type, int amount) {
    ItemStackHandle stack = ItemStackHandle.newInstance(type);
    stack.setAmountField(amount);
    return stack.toBukkit();
}
Also used : CraftItemStackHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftItemStackHandle) ItemStackHandle(com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle)

Example 4 with ItemStackHandle

use of com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle in project BKCommonLib by bergerhealer.

the class InventoryTest method testBaseInventory.

@Test
public void testBaseInventory() {
    InventoryBaseImpl inventory = new InventoryBaseImpl(9 * 3);
    assertEquals(9 * 3, inventory.getSize());
    for (int i = 0; i < inventory.getSize(); i++) {
        assertNull(inventory.getItem(i));
    }
    // Verify that the IInventory handle returns non-null items (uses EMPTY constant)
    IInventoryHandle handle = IInventoryHandle.createHandle(HandleConversion.toIInventoryHandle(inventory));
    assertEquals(9 * 3, handle.getSize());
    for (int i = 0; i < handle.getSize(); i++) {
        assertTrue(ItemStackHandle.EMPTY_ITEM.equals(handle.getItem(i)));
    }
    List<ItemStackHandle> handleContents = handle.getContents();
    assertEquals(9 * 3, handleContents.size());
    for (int i = 0; i < handleContents.size(); i++) {
        assertTrue(ItemStackHandle.EMPTY_ITEM.equals(handle.getItem(i)));
    }
    ItemStack testItem1 = new ItemStack(Material.GRASS, 1);
    inventory.setItem(2, testItem1);
    assertItemEquals(testItem1, inventory.getItem(2));
    ItemStack testItem2 = new ItemStack(Material.STONE, 16);
    int cnt = 0;
    for (int n = 0; n < 4; n++) {
        cnt += 16;
        inventory.addItem(testItem2);
        assertItemEquals(new ItemStack(Material.STONE, cnt), inventory.getItem(0));
    }
    inventory.addItem(testItem2);
    assertItemEquals(new ItemStack(Material.STONE, 64), inventory.getItem(0));
    assertItemEquals(new ItemStack(Material.STONE, 16), inventory.getItem(1));
}
Also used : InventoryBaseImpl(com.bergerkiller.bukkit.common.inventory.InventoryBaseImpl) IInventoryHandle(com.bergerkiller.generated.net.minecraft.world.IInventoryHandle) ItemStackHandle(com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle) ItemStack(org.bukkit.inventory.ItemStack) Test(org.junit.Test)

Example 5 with ItemStackHandle

use of com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle in project BKCommonLib by bergerhealer.

the class HandleConversion method toItemStackHandle.

@ConverterMethod(output = "net.minecraft.world.item.ItemStack", acceptsNull = true)
public static Object toItemStackHandle(org.bukkit.inventory.ItemStack itemStack) {
    Object raw_handle = null;
    if (itemStack != null) {
        if (CraftItemStackHandle.T.isAssignableFrom(itemStack)) {
            raw_handle = CraftItemStackHandle.T.handle.get(itemStack);
        } else {
            if (CommonBootstrap.isTestMode()) {
                // Fallback under test - does not go into production!
                ItemStackHandle handle = ItemStackHandle.newInstance(itemStack.getType());
                handle.setAmountField(itemStack.getAmount());
                handle.setDurability(itemStack.getDurability());
                return handle.getRaw();
            }
            raw_handle = CraftItemStackHandle.asNMSCopy(itemStack);
        }
    }
    if (raw_handle == null && CommonCapabilities.ITEMSTACK_EMPTY_STATE) {
        raw_handle = ItemStackHandle.EMPTY_ITEM.getRaw();
    }
    return raw_handle;
}
Also used : RecipeItemStackHandle(com.bergerkiller.generated.net.minecraft.world.item.crafting.RecipeItemStackHandle) ItemStackHandle(com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle) CraftItemStackHandle(com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftItemStackHandle) ConverterMethod(com.bergerkiller.mountiplex.conversion.annotations.ConverterMethod)

Aggregations

ItemStackHandle (com.bergerkiller.generated.net.minecraft.world.item.ItemStackHandle)5 CraftItemStackHandle (com.bergerkiller.generated.org.bukkit.craftbukkit.inventory.CraftItemStackHandle)2 InventoryBaseImpl (com.bergerkiller.bukkit.common.inventory.InventoryBaseImpl)1 IInventoryHandle (com.bergerkiller.generated.net.minecraft.world.IInventoryHandle)1 RecipeItemStackHandle (com.bergerkiller.generated.net.minecraft.world.item.crafting.RecipeItemStackHandle)1 ConverterMethod (com.bergerkiller.mountiplex.conversion.annotations.ConverterMethod)1 ItemStack (org.bukkit.inventory.ItemStack)1 Test (org.junit.Test)1