MaterialAPI - Create new items and blocks!

Discussion in 'Resources' started by Cybermaxke, Jan 19, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Cybermaxke

    What does it do?
    This api makes it possible to create custom items with each a unique id and saved on closing the server. You can also use a anvil without using it. :)

    How to get it?
    Download it: here
    Source: here

    Plugins using the api:
    MaterialManager v2.0:
    - BukkitDev: here
    - Source: here

    How to use?
    Install it like a normal plugin and add it to your libary path to begin.

    1. Extend the CustomMaterial class
    Code:
    public class PhoenixSword extends CustomMaterial {
     
        public PhoenixSword(String id, Material material) {
            super(id, material);
        }
     
        //Called when damaging a entity.
        public void onHit(EntityDamageByEntityEvent event) { }
     
        //Called when you interact right click, left click, ect.
        public void onInteract(PlayerInteractEvent event) { }
     
        //Called when right clicking a entity.
        public void onInteractEntity(PlayerInteractEntityEvent event) { }
     
        //Called when placing the block.
        public void onBlockPlaced(BlockPlaceEvent event) { }
     
        //Called when breaking the block.
        public void onBlockBreak(BlockBreakEvent event) { }
     
        //Called when damaging the block.
        public void onBlockDamage(BlockDamageEvent event) { }
     
        //Called when right clicking on the block.
        public void onBlockInteract(PlayerInteractEvent event) { }
     
        //Called when switching to the item.
        public void onHold(PlayerItemHeldEvent event) { }
    }
    2. You can also add extra features to the item.
    Code:
    //Setting the data value of your item.
    this.setData(0);
     
    //Changing the default name.
    this.setName("Phoenix Sword");
     
    //Setting the lore.
    this.setLore("Mysterious weapon!", "Fire damage!");
     
    //Knockback enchantment lvl 2 and hiding it.
    this.addEnchantment(Enchantment.KNOCKBACK, 2, false);
     
    //Setting a custom map to the material to render.
    this.setMap(customMap);
     
    //Setting the materials color if it's dyeable, this is a example for a hex color code.
    this.setColor(Color.decode("hexColor"));
     
    //Setting the skull owner, if the material is a 'SKULL_ITEM' with data 3.
    this.setSkullOwner("Name");
    3. Now we can add some unique effects to our sword.
    Code:
    //Called when damaging a entity.
    @Override
    public void onHit(Player player, LivingEntity entity) {
        entity.setFireTicks(200);
        entity.getWorld().playEffect(entity.getLocation(), Effect.MOBSPAWNER_FLAMES, 2);
    }
    4. Out weapon class is finished so we can register it and use it in a recipe or adding to inventory.
    Code:
    public static CustomMaterial PHOENIX_SWORD;
     
    @Override
    public void onEnable() {
        //Creating the item.
        PHOENIX_SWORD = new PhoenixSword("phoenixsword", Material.GOLD_SWORD);
     
        //Creating a item stack.
        CustomItemStack is = new CustomItemStack(PHOENIX_SWORD);
     
        //If you are using a custom item in the recipe; its also checked on the custom id.
        //Creating a shaped recipe: you don't need 3 arrays and you can add 3 items in each,
        //sometimes you need to use 'null'
        CustomRecipeShaped r = new CustomRecipeShaped(is);
        r.setShape(
            new CustomItemStack[] { new CustomItemStack(Material.BLAZE_ROD) },
            new CustomItemStack[] { new CustomItemStack(Material.BLAZE_ROD) },
            new CustomItemStack[] { new CustomItemStack(Material.STICK) });
     
        //Or if you like a shapeless recipe.
        CustomRecipeShapeless r2 = new CustomRecipeShapeless(is);
        r.addIngedients(new CustomItemStack(Material.BLAZE_ROD), new CustomItemStack(Material.GOLD_SWORD));
     
        //Registering the recipes.
        RecipeData.registerRecipe(r);
        RecipeData.registerRecipe(r2);
    }
     
    //Or just giving the item to a player.
    public static void givePhoenixSword(Player player) {
        CustomItemStack is = new CustomItemStack(PHOENIX_SWORD);
        player.getInventory().addItem(is.getItem());
    }
    
    5. Don't forget to add this as depency to your plugin in the config file.
    Code:
    depend: [MaterialAPI]
    Since my last update, you can also render images to a map and setting them to a material on a easy way.
    Code:
    CustomMap PICTURE = new CustomMap("pictureId", new File(Plugin.getDataFolder(), "Picture.png"));
    CustomMaterial MAP_PICTURE = new CustomMaterial("materialId", Material.MAP);
    MAP_PICTURE.setName("Picture");
    MAP_PICTURE.setMap(PICTURE);
    If you have questions or if there are bugs, please post below.

    Reserved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    -_Husky_-

    Looks nice mate, well done.
     
    Cybermaxke likes this.
  3. Offline

    t7seven7t

    The configurable crafting recipes are awesome! Any plans for adding something similar for blocks placed in the world? For example, a player might place a custom block in the world which then might heal nearby players, perhaps constantly explode or some other cool scripted effect. If a player were to mine this block it would then also drop the custom block.
     
  4. Offline

    Cybermaxke

    Blocks don't have something to store data, that is the problem. :(
    Only tile entities, but I don't know that they can be saved on a safe way.
     
  5. You could give it a FixedMetadataValue wouldn't that work?
    Also the link is 404 for me...
     
  6. Offline

    Cybermaxke

    I will look into it and let me see to the link. :)

    The link is now working.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  7. Also, do you happen to know how to keep track of an item ( if its a custom one ) without checking instances ( Like your api probably does, but it's a little too heavy for what I want to do ) ? I don't think there is something like the MetadataValue for items... ( No it's not the ItemMeta! )
    Edit: Yup link works now.
     
  8. Offline

    Cybermaxke

    I found the code to store data to blocks.

    And do you mean something like this based on my example code?
    Code:
    ItemStack is;
    CustomItemStack ci = new CustomItemStack(is);
     
    if (ci.isCustomItem() && ci.getMaterial().equals(Plugin.PHOENIX_SWORD)) {
        //Do stuff
    }
     
  9. I wanted to do it without adding new ItemStack Classes and checking with instances or stuff like that if that is possible...
     
  10. Offline

    Cybermaxke

    There is in 'MaterialData' a method to get the custom id and you get also one from your material.
    You can use those to compare.
     
  11. Offline

    t7seven7t

    I just thought of something that might be interesting. If I'm correct you currently have to create your own Bukkit plugin to implement these new items right?

    To simplify adding items, what if you created a class loader to load custom items as though they were plugins to this library? It would load either single class files or jars from its own plugin folder inside MaterialAPI data folder or something, and then automagically load them into the server.
     
  12. Offline

    cMan_

    Looks promising!
     
  13. Offline

    Cybermaxke

    Update #1
    - Added the ability to render images on maps and applying them to a custom material.
    - Test for custom blocks.
    - More features added.
    - Minor changes and fixes.

    I have tested the FixedMetadata but its not saved when the server closes. :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  14. Offline

    CreeperShift

    Hey! I have a question:
    Currently I have a custom weapon that you can craft out of regular swords. Would it be somehow possible to copy over the enchantments to the custom weapon? Right now when people take a iron sword with any enchantments and craft it into the custom sword they lose all their enchantments.
     
  15. You could listen for the CraftItemEvent and handle it there I think..
     
  16. Offline

    CreeperShift

    You mean check the enchantments during the crafting event and then reapplying them later? Sounds like a plan! I'll try it :) Thanks!
     
  17. Offline

    Cybermaxke

    So basicly a anvil in a crafting table? :p
     
  18. Offline

    CreeperShift

    A little bit, yes :p.

    I'd normally not do it, but I'm 110% sure that people will complain "OMG I used my enchanted sword and now they are gone QQQQQQ" :/

    Cybermaxke

    By the way, whenever I add Protocolib I get this error:

    Code:
    2013-01-21 08:09:16 [SEVERE] Could not load 'plugins\Weapons.jar' in folder 'plu
    gins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: me/cyb
    ermaxke/materialapi/material/CustomMaterial
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:184)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugins(CraftServer.jav
    a:239)
            at org.bukkit.craftbukkit.v1_4_6.CraftServer.<init>(CraftServer.java:217
    )
            at net.minecraft.server.v1_4_6.PlayerList.<init>(PlayerList.java:52)
            at net.minecraft.server.v1_4_6.DedicatedPlayerList.<init>(SourceFile:11)
     
            at net.minecraft.server.v1_4_6.DedicatedServer.init(DedicatedServer.java
    :104)
            at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:
    399)
            at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:84
    9)
    Caused by: java.lang.NoClassDefFoundError: me/cybermaxke/materialapi/material/Cu
    stomMaterial
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:173)
            ... 9 more
    Caused by: java.lang.ClassNotFoundException: me.cybermaxke.materialapi.material.
    CustomMaterial
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader
    .java:80)
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.
    java:53)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 12 more
    Tried with both the 1.9 and 2.0 version. The plugin loads fine without Protocolib added. :/


    Also, I have a config file that gets loaded during onEnable. Normally to get the config value in another class I add a constructor to that class and can load the value just fine, however I'm unsure how to do it with your custom class, whenever I extend CustomMaterial. :S If I try to get the value just like that then I will just get a NullpointException.

    Code:
       
    public class DiamondI extends CustomMaterial{
    
          public Weapons plugin;
         
    public DiamondI(String id, Material material) {
            super(id, material);
         
            this.setDamage(this.plugin.dia1); //dia1 is the int config value
            this.setName("Diamond Sword I");
            this.setLore("Mysterious weapon!", "Fire damage!");
            this.addEnchantment(Enchantment.ARROW_INFINITE, 2, false);
         
        }
     
        //Called when damaging a entity.
        @Override
        public void onHit(Player player, LivingEntity entity) {
     
        }
     
    }
     
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  19. Offline

    Cybermaxke

    CreeperShift
    Maybe you should add 'depend: [MaterialAPI]' to you plugin.yml
    And if that doesn't work use 'depend: [MaterialAPI,ProtocolLib]'

    And in your code, you should add the plugin to the constuctor or it will throw some null pointers, I think.
    Code:
    public class DiamondI extends CustomMaterial {
     
        public DiamondI(Weapons plugin, String id, Material material) {
            super(id, material);
       
            this.setDamage(plugin.dia1);
            this.setName("Diamond Sword I");
            this.setLore("Mysterious weapon!", "Fire damage!");
            this.addEnchantment(Enchantment.ARROW_INFINITE, 2, false);
        }
    }
     
  20. Offline

    CreeperShift

    Hmm, sadly it's still throwing the same null pointer :(. Any other ideas? :)
     
  21. Offline

    Cybermaxke

    Are you sure you added my lib to plugins?
     
  22. Offline

    CreeperShift

    Yeah :p It works just fine without the config value, just with it it's throwing the error.
     
  23. Offline

    Cybermaxke

    Oh ok. :)
     
  24. Offline

    CreeperShift

    So no other ideas on why it's throwing that error? :( Because I'm out of ideas myself.
     
  25. Offline

    Cybermaxke

    I don't know your code for the config part but you can take a look at my saving method for saving id data: here
     
  26. Offline

    Taurhuzz

    Will onHit call if the item is a bow and the arrow hits an entity?
     
  27. Offline

    Cybermaxke

    I didn't add that yet.
     
  28. Offline

    Cjreek

    Hi,

    I added a hidden enchantment to my "Silver"-Material:

    Code:java
    1. public class Silver extends CustomMaterial
    2. {
    3. public Silver()
    4. {
    5. this("silver", Material.IRON_INGOT);
    6. }
    7.  
    8. private Silver(String id, Material material)
    9. {
    10. super(id, material);
    11.  
    12. this.setName("Silver");
    13. this.setType(Material.IRON_INGOT);
    14. this.addEnchantment(Enchantment.FIRE_ASPECT, 2, false);
    15. }
    16. }


    But if it's hidden it doesn't work... It's like it doesn't have this enchantment. :(
     
  29. Offline

    Cybermaxke

    Now I got the same problem, it was working for a while but it seems to be broken.
    What version of bukkit do you use? Maybe its a bug in the ProtocolLib.
     
  30. Offline

    Cjreek

    I'm using Bukkit-1.4.6-R0.3 RB #2586 and ProtocolLib v. 2.0.0

    Now I tried:

    Code:java
    1.  
    2. this.addEnchantment(Enchantment.FIRE_ASPECT, 1, false);
    3. this.addEnchantment(Enchantment.KNOCKBACK, 20, false);
    4.  


    I've been on my test server with another player and on some hits it didn't work and then it worked again.. and so on.
    When i removed all my silver from my inventory and got myself some new silver, than it worked again for some time.

    EDIT1: I made the mistake and created a new instance of "Silver" everytime I create a new CustomItemStack of this type. I think that's the source of my problems.

    EDIT2: It's better but the error still appears sometimes.. And for other players the enchantment glow is still visible if I hold the item in my hand.

    EDIT3: New info: If you edit a stack (split, take items, put some items to the stack) the special properties of the custom item gets (temporary) lost

    Sorry for the kinda messy text :-D
     
Thread Status:
Not open for further replies.

Share This Page