Solved Making custom materials

Discussion in 'Plugin Development' started by heroz, Jun 23, 2014.

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

    heroz

    I'm new to java and bukkit.

    I'm making a series of new materials using pre-existing textures to add new recipes and items to my server. For example: I've got an item called "Hardened Wood" that uses the texture for oak wood planks. The problem I'm having is that my new materials can be used in any recipe that their texture can be used in. I can craft Hardened Wood with my custom recipe but i can use the hardened wood to make regular sticks or a crafting table.

    How would i be able to separate the recipes so that the custom items would not work in regular recipes?

    Code:
    public enum Items {
            Hardened Wood;
        }
     
    public ItemStack getCustomItem(Items item) {
            ItemStack is = null;
            ItemMeta in = null;
            ArrayList<String> lore;
            switch (item) {
            case Hardened Wood:
                is = new ItemStack(Material.WOOD, 1);
                in = is.getItemMeta();
                in.setDisplayName("Hardish_Wood");
                lore = new ArrayList<String>();
                lore.add(ChatColor.RED + "Used to make better tools");
                in.setLore(lore);
                is.setItemMeta(in);
                break;
            }
     
  2. Offline

    AoH_Ruthless

    heroz
    I don't think you can*

    Also, you should follow Java code conventions with your enumeration. (All Uppercase, replace whitespace with underscores)

    * Someone correct me if I am wrong.
     
  3. Offline

    xTigerRebornx

  4. Offline

    Jogy34

    This can be done ( AoH_Ruthless ) but it can get really annoying. You basically have to check everywhere that a player can use the original item and check if it's your custom one. So crafting, placing, burning, smelting, etc... If you find your custom item is being used you have to cancel the event. Crafting and such doesn't take the item meta int account so you also have to check everywhere that a player can use your custom item and make sure that it is actually your custom item and not the original one.

    This can get really tedious and end up being a real nuisance if you don't know the right shortcuts to take (which I'll leave you to figure out on your own).
     
  5. Offline

    heroz

    I appreciate being told its possible. That at least reassures me. Anyone actually know how to do it?
     
  6. Offline

    heroz

    I don't mind the work. I'd write as much code is necessary, but I don't know where to begin. I can make the custom items and make recipes, but that its about it. If anyone could give me some pointers, example code, or would be willing to talk to me on skype or something I'd appreciate it.
     
  7. Offline

    Jogy34

    I told you the process that you have to go through but I'll reiterate the main things:
    - You have to check everywhere that a player can use the original item and check if it's your custom one.
    - Crafting and such doesn't take item meta into account so you also have to check everywhere that a player can use your custom item and make sure that it is actually your custom item and not the original one.
     
  8. Offline

    fireblast709

    AoH_Ruthless Jogy34 heroz You forget that ItemStacks carry a durability value, which is also used in the setIngredient/addIngredient methods
     
  9. Offline

    MCForger

    fireblast709 Jogy34 heroz xTigerRebornx
    I would use something like this: (Tested and it works)
    Code:java
    1. package com.mcforger.example;
    2.  
    3. import java.util.Arrays;
    4. import java.util.List;
    5.  
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Event.Result;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.CraftItemEvent;
    13. import org.bukkit.event.player.PlayerJoinEvent;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.Recipe;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public class CustomItemsPlugin extends JavaPlugin implements Listener
    20. {
    21. public enum CustomItem
    22. {
    23. HARDENED_WOOD(Material.WOOD, ChatColor.RED + "Hardened Wood", Arrays
    24. .asList(ChatColor.GREEN + "Used to make better tools!"), Arrays
    25. .asList(Material.WOOD_AXE, Material.WOOD_BUTTON,
    26. Material.WOOD_DOOR, Material.STICK,
    27. Material.WOOD_DOUBLE_STEP, Material.WOOD_HOE,
    28. Material.WOOD_PICKAXE, Material.WOOD_PLATE,
    29. Material.WOOD_SPADE, Material.WOOD_SWORD,
    30. Material.CHEST, Material.SIGN));
    31.  
    32. private Material material;
    33. private String displayName;
    34. private List<String> lore;
    35. private List<Material> usedToMake;
    36.  
    37. private CustomItem(Material material, String displayName,
    38. List<String> lore, List<Material> usedToMake)
    39. {
    40. this.material = material;
    41. this.displayName = displayName;
    42. this.lore = lore;
    43. this.usedToMake = usedToMake;
    44. }
    45.  
    46. public ItemStack getItemStack()
    47. {
    48. ItemStack itemstack = new ItemStack(material, 1);
    49. ItemMeta itemMeta = itemstack.getItemMeta();
    50. itemMeta.setDisplayName(displayName);
    51. itemMeta.setLore(lore);
    52. itemstack.setItemMeta(itemMeta);
    53. return itemstack;
    54. }
    55.  
    56. public boolean isUsedToMakeNormally(Material material)
    57. {
    58. return usedToMake.contains(material);
    59. }
    60. }
    61.  
    62. @Override
    63. public void onEnable()
    64. {
    65. getServer().getPluginManager().registerEvents(this, this);
    66. }
    67.  
    68. @EventHandler
    69. public void onPlayerJoin(PlayerJoinEvent event)
    70. {
    71. // Just for testing purposes
    72. Player player = event.getPlayer();
    73. ItemStack hardenedWood = CustomItem.HARDENED_WOOD.getItemStack();
    74. hardenedWood.setAmount(64);
    75. player.getInventory().addItem(hardenedWood);
    76. player.getInventory().addItem(new ItemStack(Material.WOOD, 64));
    77. }
    78.  
    79. @EventHandler
    80. public void onCraftItem(CraftItemEvent event)
    81. {
    82. // Recipe
    83. Recipe recipe = event.getRecipe();
    84. // Item we are looking out for
    85. CustomItem customItem = CustomItem.HARDENED_WOOD;
    86. for (ItemStack itemstack : event.getInventory().getContents())
    87. {
    88. if (itemstack.isSimilar(customItem.getItemStack()))
    89. {
    90. if (customItem.isUsedToMakeNormally(recipe.getResult()
    91. .getType()))
    92. {
    93. event.setResult(Result.DENY);
    94. getLogger()
    95. .info("Player attempted to use custom item's normal material to make a default bukkit item out of the material!");
    96. }
    97. }
    98. }
    99. }
    100. }

    The only issue is you need to add the materials your special item material can make in normal minecraft to the usedMaterials list.
     
    heroz likes this.
  10. Offline

    heroz

    omg, thank you so much. This is exactly what i needed to get started. Thanks again. Hopefully I can figure out how to work with this :3
     
  11. Offline

    MCForger

    heroz
    Remember to make it solved.
     
  12. Offline

    fireblast709

    heroz Untested, but should work (the rest of the people tagged above should visit the thread on their own accord)
    Code:java
    1. ItemStack recipeOne = new ItemStack(Material.WOOD, 1, (short)1);
    2. // Register recipeOne
    3. ItemStack recipeTwo = new ItemStack(Material.STICK, 1, (short)1);
    4. ShapedRecipe sr = new ShapedRecipe(recipeTwo)
    5. .shape("x ", "x ", " ")
    6. .setIngredient('x', Material.WOOD, 1);
    7. // Register recipeTwo with sr
    Do note, incomplete :3. In case of blocks you would need to listen to the appropiate events for placement and such, but you can always manually set the data for those and recover that data when it is broken.
     
  13. Offline

    heroz

    I have VERY little experience with java, so trying to understand this code is going to be the main struggle. It looks mostly straightforward, but would it be ok if I messaged you about anything i don't understand? @MCForger
     
  14. Offline

    MCForger

    heroz
    Yeah thats fine. I was planning on make a tutorial under resources for this kind of example.
     
  15. Offline

    heroz

    After testing out the code, maybe I'm doing something wrong, but i can use normal wood to make anything the custom item can make as well... Such as 5 hardened wood makes a diamond (for example) so does regular wood...
    Am I doing something wrong in my recipes?
     
  16. Offline

    MCForger

    heroz
    I forgot to mention that I did not add all the Materials that Material.WOOD can create in my usedToMake list. You would have to add more materials to that list to cover the full amount of materials wood creates. The idea of the code was to show you can stop normal bukkit materials from being crafted with custom items.
     
  17. Offline

    heroz

    Also, @Jogy34 i appreciate the help, I just don't know how to do what you describe. Luckily MCForger has given me code to work with.

    MCForger
    yeah, i understand that. The code does almost everything I wanted and I can expand on it. My question was, when I make a new recipe how will i stop normal wood from crafting it and only allow custom wood? (I may just be missing something, I have absolutely no coding knowledge, just a bit of luck and lots of guessing)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  18. Offline

    AoH_Ruthless

    heroz
    Check in the CraftItemEvent if the recipe contains a regular wooden plank, and if so, set the result to deny.

    I am going to be the mean one that you will say "Wow, what a dick", but here it goes. You have to stop everything you are doing and go learn Java. Honestly, you are making this a lot harder on yourself because you have nearly no clue what you are doing. If MCForger didn't graciously give you the code you would still have nothing. The Bukkit API is 20x harder than it needs to be simply because you lack experience with OOP. So, I am suggesting that you stop and go buy an actual book or pay for a class to learn Java (it will take you months to fully grasp everything), but as soon as you learn the basics (maybe a week, or two), practice what you learn with the Bukkit API. I am telling you to pay because almost nothing of value is free (besides the JavaDocs, in my opinion). Maybe you learn better visually so thenewboston is your best bet that way, even though still not my preferred choice.

    You are making your/our lives harder than they need to be. Please, take these words to heart. Understand that I am not telling you to give up and I'm not telling you that you suck or anything. You said it yourself, you have no coding knowledge so you need to build a foundation. Do you learn to run before you learn to walk? No. So don't learn Bukkit before you learn Java.
     
  19. Offline

    heroz

    @AoH_Ruthless
    I understand your frustration. I'm well aware that this forum probably has a lot of questions from truly incompetent people. However, I actually have programming experience, I just meant in regards to java or Bukkit. I may not know java or Bukkit, but i have no intention of learning Bukkit. I was tasked with a project to make a plugin and thought I had figured it all out on my own until i ran into the crafting issues. I appreciate your efforts to pointing me in the right direction and I probably should have put in the title question itself that I would probably need example code since I have no knowledge.

    I am incredibly grateful to MCForger, he has given me exactly what I asked for. I hit a dead end on my project and decided to seek help. I just want to finish my plugin and move onto something else. I do plan to learn java eventually and thenewboston is great. I just don't have the time right now. (Generic excuse, I know). I really hope I can continue receiving assistance from everyone. You've all helped.

    In response to my previous post I just want to clarify: I'm not trying to say I'm better than anyone or more deserving of help. It was probably not worded the best. I'm pretty good when it comes to troubleshooting and I have other programming experience outside of Java. I had figured out how to make the items and craft them and such, I just hit a problem I couldn't find a reasonable solution to. After several hours of googling and trying to figure out how to accomplish what I needed I decided to ask for help. I did put forth a lot of effort before asking. Just wanted to clear that up. Not that it makes much difference. I do really appreciate all the help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page