DomsItems

DomsItems - Item Storing like a pro!

What are DomsItems?
The technical name is a DomsItem, and for the Villages plugin, a VillageItem (Though this will likely change to DomsItem in a future Version). The items allow developers to use a tonne of easy Item based features that Bukkit's ItemStack's simply can't.

DomsItem's are easy to use Class Items that can apply to nearly any given situation for items, one of the best of which is a full toString() method, that can then be parsed back into a DomsItem instance. The idea of DomsItem's are to keep things basic, offer full functionality of Items that are human ready and Code Ready, and attempting to not miss a single spot.

The DomsItem's were originally designed for the Villages plugin as a way to store Items in an SQL Database, and went on to help with DomsCommands as a full Item interface in Inventory saving and for making Advanced Kits.

DomsItem's aren't bound by Bukkit restrictions such as using ID's over Material Names, and most importantly contain a bunch of features, which are very useful for Inventory checking.

Basic DomsItem

DomsItem's are designed to be efficient, storing only what's needed in a easy to read format. Here we see a simple application that uses it

package com.domsplace.TestingDomsItems;

import com.domsplace.TestingDomsItems.Objects.DomsItem;
import org.bukkit.Bukkit;
import org.bukkit.Material;

public class Tester {
    public static void printItem() {
        DomsItem item = new DomsItem(Material.STONE);
        Bukkit.getLogger().info(item.toString());
        /*
            Result:
                {id:"STONE"}
        */
    }
}

Which is a very useful way to easily store single items in a string, but say we wanted to go more complex, we can store items with ID's, such as Jungle Wood:

    public static void printItem() {
        DomsItem item = new DomsItem(Material.WOOD, new Short("3"));
        Bukkit.getLogger().info(item.toString());
        /*
            Result:
                {id:"WOOD"},{data:"3"}
        */
    }

This is still pretty basic, however the toString() method will allow any kind of item to be stored, even items with lores names anything. Take this example:

    public static void printItem() {
        DomsItem item = new DomsItem(Material.WOOD, new Short("3"));
        item.setName("Doms Cool Jungle Wood");
        item.addLore("My wood is awesome!");
        item.addLore("Better than your wood!");
        item.addEnchantment(Enchantment.DURABILITY, 3);
        Bukkit.getLogger().info(item.toString());
        /*
            Result:
                {id:"WOOD"},{data:"3"},
                {name:"Doms Cool Jungle Wood"},
                {lore:"My wood is awesome!"},
                {lore:"Better than your wood!"},
                {enchantment:"DURABILITY*3"}
        */
    }

And there are heaps more options, here are them all (some still need to be fully added):

  • id
  • data
  • lore
  • page
  • name
  • author
  • enchantment
  • storedenchant
  • potioneffect
  • head
  • color
  • repaircost
    DomsItem's also have ItemStack conversions, back and fourth, that will let you auto apply the data you want to, and from item stacks, useful for Player Inventory stuff. Example:
        Player player = Bukkit.getPlayer("DOMIN8TRIX25");
        ItemStack itemInHand = player.getItemInHand();
        
        List<DomsItem> heldItems = DomsItem.itemStackToDomsItems(itemInHand);
        player.sendMessage("You are holding " + heldItems.size()
                + " of " + heldItems.get(0).toHumanString());

And convert DomsItems back to ItemStacks:

        DomsItem item = new DomsItem(Material.TNT);
        item.setName("KA-BOOM!");
        //Gives a player 10 TNT named "KA-BOOM!"
        try {
            ItemStack is = item.getItemStack(10);
            Bukkit.getPlayer("DOMIN8TRIX25").getInventory().addItem(is);
        } catch(InvalidItemException e) {
            //Will be thrown if the item is Invalid
        }

fairly straight forward

Humans Creating DomsItem's

The next best thing about DomsItem's is that they can be converted FROM string, accepting many formats, the best however is the direct conversion from the original toString() method.
Examples of Acceptable formats:


Reverse toString():

{id:"WOOD"},{data:"3"},{name:"My Awesome stone!"}


Classic Way:

WOOD:3


Using IDs:

{id:"17"},{data:"3"}


Or:

17:3



Human's Reading DomsItem's

Another great feature that I'm constantly working on for DomsItem's is the conversion to Human Readable string! Take this Example:

        DomsItem item = new DomsItem(Material.WOOD, new Short("3"));
        item.setName("Doms Cool Jungle Wood");
        item.addLore("My wood is awesome!");
        item.addLore("Better than your wood!");
        item.addEnchantment(Enchantment.DURABILITY, 3);
        Bukkit.getLogger().info(item.toString());

Will output something like:

Wood, named Doms Cool Jungle Wood, with the enchantment, Durability at level 3, with the lores, My wood is awesome!, Better than your wood!

Just as an example.

Inventory Checking, Removing etc

DomsItem's also have a few static methods for inventories, first checking if they have items, have room for items and/or give an inventory items Example of Checking for a full inventory:

boolean t = DomsItem.isInventoryFull(Bukkit.getPlayer("DOMIN8TRIX25").getInventory());


Checking if an Inventory contains 10 stone:

DomsItem item = new DomsItem(Material.STONE);
boolean t = DomsItem.hasItem(item, 10, Bukkit.getPlayer("DOMIN8TRIX25").getInventory());


Full List of Methods

Static Methods

contains(List<DomsItem> a, DomsItem b); //Returns true if a contains b
contains(List<DomsItem> a, List<DomsItem> b); //Returns true if a contains b
copy(DomsItem item); //Returns an exact copy of item, throws InvalidItemException
createAllItems(List<String>); //Returns a list of DomsItem's created from the List of String, throws InvalidItemException
createItem(ItemStack is); //Creates a DomsItem from an ItemStack
createItem(String line); //Creates a single item from a String, throws InvalidItemException
createItem(List<DomsItem> items); //Creates an ItemStack from a list of DomsItem's
createItems(String line); //Creates a list of DomsItem's from a String, throws InvalidItemException
getHumanMessages(List<DomsItem> items); //Creates a list of String containing the HumanMessages of each item in the list of items
guessItem(String s); //Tries to create an item using the supplied string, good for making items the "classic" way, throws InvalidItemException
guessMaterial(String l); //Tries to determine a Material from String (useful for getting Materials using the classic ID approach)
hasItem(DomsItem item, int amount, Inventory inv); //Does the Inventory inv contain at least amount lots of item
isInventoryFull(Inventory i); //Is the supplied Inventory full (checks for air etc)
itemStackToDomsItem(ItemStack is); //Creates a List of DomsItem's from an ItemStack
multiply(DomsItem item, int times); //Creates a copy of the supplied item, the supplied amount of times and adds it to a list.
removeItem(DomsItem item, int amount, Inventory inv); //Removes amount number of item from the inv
toItemStackArray(List<DomsItem> items); //Get's a list of ItemStacks from a list of Items, private access by default, can be changed


Constructors I'm always making these, tell me if you want me to add a specific one, they're pretty easy after all..

DomsItem(Material m);
DomsItem(String m); //Note on this, use the Material Name, not the toString String, e.g. "STONE"
DomsItem(String m, short data);
DomsItem(Material m, short data);
DomsItem(String m, short d, List<String> lores);
DomsItem(String m, short d, String name);
DomsItem(String m, short d, String name, List<String> lores);
DomsItem(String material, short data, List<String> pages, String name, List<String> lores);
//Ugh Jeeze there are heaps of these things.. I'll just show the complex one
DomsItem(String material, short data, Map<Enchantment, Integer> enchants, Map<Enchantment, Integer> storedEnchants, List<String> pages, String name);


Methods Ugh soo many methods

addEnchantment(Enchantment e, int lvl);
addLore(String l);
addPage(String l);
compare(DomsItem item);
copy();
getBookAuthor();
getBookPages();
getColor();
getData();
getEnchantments();
**CreditShops Only** getGuessPrice();
getItemID(); //Used for DomsCommands only, but is on all the plugins
getItemMeta(ItemStack apply); //Get's an ItemMeta you can apply to the supplied ItemStack
getItemStack(int amount); //Get's an ItemStack of this DomsItem with the supplied amount, throws InvalidItemException
getItemStack(); //Get's an item stack of this item with the max ItemStack size for this item, throws InvalidItemException
getMaterial();
getMaterialData(); //Deprecated
getMaterialName();
getName();
getPlayerHead();
getPotionEffects();
getRepairCost();
getStoredEnchantments(); //This is different from getEnchantments, this is for Enchanted Books (and any future Enchantment storing items)
getTypeName(); //Humanises the name, needs more work
giveToPlayer(Player player); //Will give to the supplied Player, OR drop the item infront of them if their inventory's full
hasData(); //Returns True if this item has some kind of Data
isAir();
isBook(); //Is a Written Book, or Book and Quill only!
isHead();
isMobNameable(); //Example, spawn eggs, name tags
setAuthor(String author);
setColor(int color); //Hex based color for Leather Armour
setData(short data);
setEnchantments(Map<Enchantment, Integer> enchants);
setLores(List<String> lores);
setMaterialName(String material);
setName(String name);
setPage(int page, String x);
setPages(List<String> pages);
setPlayerHead(OfflinePlayer player);
setPotionEffects(List<PotionEffect> effects);
setRepairCost(int cost);
setStoredEnchantments(Map<Enchantment, Integer> enchants);
toHumanString(); //Humanises the Item, check above for info
toString();



I need a coffee and a nap after all that T_T

Uses

Take some examples, some of the API used may be a tad old on each one..
Villages
DomsCommands
CreditShops


Feel free to take any and change, just a little credit would be nice :P


Comments

Posts Quoted:
Reply
Clear All Quotes