Java Code Examples for cn.nukkit.item.Item#clone()

The following examples show how to use cn.nukkit.item.Item#clone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean canAddItem(Item item) {
    item = item.clone();
    boolean checkDamage = item.hasMeta();
    boolean checkTag = item.getCompoundTag() != null;
    for (int i = 0; i < this.getSize(); ++i) {
        Item slot = this.getItem(i);
        if (item.equals(slot, checkDamage, checkTag)) {
            int diff;
            if ((diff = slot.getMaxStackSize() - slot.getCount()) > 0) {
                item.setCount(item.getCount() - diff);
            }
        } else if (slot.getId() == Item.AIR) {
            item.setCount(item.getCount() - this.getMaxStackSize());
        }

        if (item.getCount() <= 0) {
            return true;
        }
    }

    return false;
}
 
Example 2
Source File: CraftingManager.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private Item[][] cloneItemMap(Item[][] map) {
    Item[][] newMap = new Item[map.length][];
    for (int i = 0; i < newMap.length; i++) {
        Item[] old = map[i];
        Item[] n = new Item[old.length];

        System.arraycopy(old, 0, n, 0, n.length);
        newMap[i] = n;
    }

    for (int y = 0; y < newMap.length; y++) {
        Item[] row = newMap[y];
        for (int x = 0; x < row.length; x++) {
            Item item = newMap[y][x];
            newMap[y][x] = item.clone();
        }
    }
    return newMap;
}
 
Example 3
Source File: BlockItemFrame.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    BlockEntityItemFrame itemFrame = (BlockEntityItemFrame) blockEntity;
    if (itemFrame.getItem().getId() == Item.AIR) {
        // We can't use Item.get(item.getId(), item.getDamage(), 1) because
        // we need to keep the item's NBT tags
        Item itemOnFrame = item.clone(); // So we clone the item
        itemOnFrame.setCount(1); // Change it to only one item (if we keep +1, visual glitches will happen)
        itemFrame.setItem(itemOnFrame); // And then we set it on the item frame
        // The item will be removed from the player's hand a few lines ahead
        this.getLevel().addSound(new ItemFrameItemAddedSound(this));
        if (player != null && player.isSurvival()) {
            int count = item.getCount();
            if (count-- <= 0) {
                player.getInventory().setItemInHand(new ItemBlock(new BlockAir(), 0, 0));
                return true;
            }
            item.setCount(count);
            player.getInventory().setItemInHand(item);
        }
    } else {
        int itemRot = itemFrame.getItemRotation();
        if (itemRot >= 7) {
            itemRot = 0;
        } else {
            itemRot++;
        }
        itemFrame.setItemRotation(itemRot);
        this.getLevel().addSound(new ItemFrameItemRotated(this));
    }
    return true;
}
 
Example 4
Source File: CraftingTransaction.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void setPrimaryOutput(Item item) {
    if (primaryOutput == null) {
        primaryOutput = item.clone();
    } else if (!primaryOutput.equals(item)) {
        throw new RuntimeException("Primary result item has already been set and does not match the current item (expected " + primaryOutput + ", got " + item + ")");
    }
}
 
Example 5
Source File: ShapedRecipe.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a ShapedRecipe instance.
 *
 * @param primaryResult
 * @param shape<br>        Array of 1, 2, or 3 strings representing the rows of the recipe.
 *                         This accepts an array of 1, 2 or 3 strings. Each string should be of the same length and must be at most 3
 *                         characters long. Each character represents a unique type of ingredient. Spaces are interpreted as air.
 * @param ingredients<br>  Char => Item map of items to be set into the shape.
 *                         This accepts an array of Items, indexed by character. Every unique character (except space) in the shape
 *                         array MUST have a corresponding item in this list. Space character is automatically treated as air.
 * @param extraResults<br> List of additional result items to leave in the crafting grid afterwards. Used for things like cake recipe
 *                         empty buckets.
 *                         <p>
 *                         Note: Recipes **do not** need to be square. Do NOT add padding for empty rows/columns.
 */
public ShapedRecipe(Item primaryResult, String[] shape, Map<Character, Item> ingredients, List<Item> extraResults) {
    int rowCount = shape.length;
    if (rowCount > 3 || rowCount <= 0) {
        throw new RuntimeException("Shaped recipes may only have 1, 2 or 3 rows, not " + rowCount);
    }

    int columnCount = shape[0].length();
    if (columnCount > 3 || rowCount <= 0) {
        throw new RuntimeException("Shaped recipes may only have 1, 2 or 3 columns, not " + columnCount);
    }


    //for($shape as $y => $row) {
    for (int y = 0; y < rowCount; y++) {
        String row = shape[y];

        if (row.length() != columnCount) {
            throw new RuntimeException("Shaped recipe rows must all have the same length (expected " + columnCount + ", got " + row.length() + ")");
        }

        for (int x = 0; x < columnCount; ++x) {
            char c = row.charAt(x);

            if (c != ' ' && !ingredients.containsKey(c)) {
                throw new RuntimeException("No item specified for symbol '" + c + "'");
            }
        }
    }

    this.primaryResult = primaryResult.clone();
    this.extraResults.addAll(extraResults);

    this.shape = shape;

    for (Map.Entry<Character, Item> entry : ingredients.entrySet()) {
        this.setIngredient(entry.getKey(), entry.getValue());
    }
}
 
Example 6
Source File: BlockItemFrame.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onActivate(Item item, Player player) {
    BlockEntity blockEntity = this.getLevel().getBlockEntity(this);
    BlockEntityItemFrame itemFrame = (BlockEntityItemFrame) blockEntity;
    if (itemFrame.getItem().getId() == Item.AIR) {
        // We can't use Item.get(item.getId(), item.getDamage(), 1) because
        // we need to keep the item's NBT tags
        Item itemOnFrame = item.clone(); // So we clone the item
        itemOnFrame.setCount(1); // Change it to only one item (if we keep +1, visual glitches will happen)
        itemFrame.setItem(itemOnFrame); // And then we set it on the item frame
        // The item will be removed from the player's hand a few lines ahead
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ADD_ITEM);
        if (player != null && player.isSurvival()) {
            int count = item.getCount();
            if (count-- <= 0) {
                player.getInventory().setItemInHand(new ItemBlock(new BlockAir(), 0, 0));
                return true;
            }
            item.setCount(count);
            player.getInventory().setItemInHand(item);
        }
    } else {
        int itemRot = itemFrame.getItemRotation();
        if (itemRot >= 7) {
            itemRot = 0;
        } else {
            itemRot++;
        }
        itemFrame.setItemRotation(itemRot);
        this.getLevel().addSound(this, Sound.BLOCK_ITEMFRAME_ROTATE_ITEM);
    }
    return true;
}
 
Example 7
Source File: BaseInventory.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean setItem(int index, Item item, boolean send) {
    item = item.clone();
    if (index < 0 || index >= this.size) {
        return false;
    } else if (item.getId() == 0 || item.getCount() <= 0) {
        return this.clear(index, send);
    }

    InventoryHolder holder = this.getHolder();
    if (holder instanceof Entity) {
        EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, this, this.getItem(index), item, index);
        Server.getInstance().getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            this.sendSlot(index, this.getViewers());
            return false;
        }

        item = ev.getNewItem();
    }

    Item old = this.getItem(index);
    this.slots.put(index, item.clone());
    this.onSlotChange(index, old, send);

    return true;
}
 
Example 8
Source File: CraftingTransaction.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void setExtraOutput(int index, Item item) {
    int y = (index / this.gridSize);
    int x = index % gridSize;

    if (secondaryOutputs[y][x].isNull()) {
        secondaryOutputs[y][x] = item.clone();
    } else if (!secondaryOutputs[y][x].equals(item)) {
        throw new RuntimeException("Output " + index + " has already been set and does not match the current item (expected " + secondaryOutputs[y][x] + ", got " + item + ")");
    }
}
 
Example 9
Source File: FurnaceSmeltEvent.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public FurnaceSmeltEvent(BlockEntityFurnace furnace, Item source, Item result) {
    super(furnace.getBlock());
    this.source = source.clone();
    this.source.setCount(1);
    this.result = result;
    this.furnace = furnace;
}
 
Example 10
Source File: BaseInventory.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean setItem(int index, Item item, boolean send) {
    item = item.clone();
    if (index < 0 || index >= this.size) {
        return false;
    } else if (item.getId() == 0 || item.getCount() <= 0) {
        return this.clear(index);
    }

    InventoryHolder holder = this.getHolder();
    if (holder instanceof Entity) {
        EntityInventoryChangeEvent ev = new EntityInventoryChangeEvent((Entity) holder, this.getItem(index), item, index);
        Server.getInstance().getPluginManager().callEvent(ev);
        if (ev.isCancelled()) {
            this.sendSlot(index, this.getViewers());
            return false;
        }

        item = ev.getNewItem();
    }

    Item old = this.getItem(index);
    this.slots.put(index, item.clone());
    this.onSlotChange(index, old, send);

    return true;
}
 
Example 11
Source File: ShapedRecipe.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public Item getIngredient(int x, int y) {
    Item item = this.ingredients.get(this.shape[y].charAt(x));

    return item != null ? item.clone() : Item.get(Item.AIR);
}
 
Example 12
Source File: PlayerGlassBottleFillEvent.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public PlayerGlassBottleFillEvent(Player player, Block target, Item item) {
    this.player = player;
    this.target = target;
    this.item = item.clone();
}
 
Example 13
Source File: FurnaceRecipe.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void setInput(Item item) {
    this.ingredient = item.clone();
}
 
Example 14
Source File: FurnaceRecipe.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void setInput(Item item) {
    this.ingredient = item.clone();
}
 
Example 15
Source File: PlayerGlassBottleFillEvent.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public PlayerGlassBottleFillEvent(Player player, Block target, Item item) {
    this.player = player;
    this.target = target;
    this.item = item.clone();
}
 
Example 16
Source File: ShapelessRecipe.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public ShapelessRecipe(Item result) {
    this.output = result.clone();
}
 
Example 17
Source File: BrewingRecipe.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public void setInput(Item item) {
    ingredient = item.clone();
}
 
Example 18
Source File: ShapedRecipe.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public Item getIngredient(int x, int y) {
    Item item = this.ingredients.get(this.shape[y].charAt(x));

    return item != null ? item.clone() : Item.get(Item.AIR);
}
 
Example 19
Source File: FurnaceRecipe.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public FurnaceRecipe(Item result, Item ingredient) {
    this.output = result.clone();
    this.ingredient = ingredient.clone();
}
 
Example 20
Source File: CraftingTransaction.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public void setPrimaryOutput(Item item) {
	if (this.primaryOutput == null) {
		this.primaryOutput = item.clone();
	}
}