[IGNORE] How to refer to an item stack from a different method?

Discussion in 'Spigot Plugin Development' started by pondamo, Jun 25, 2015.

  1. Im creating a small little plugin where you can craft a certain item and then once you drop the item it does something. I have pretty much completed the plugin but now I need to check and make sure that the correct item is being dropped. At the beginning of my code I created a method for a custom recipe:
    Code (Text):
    public void recipe1() {
            ItemStack Item1 = new ItemStack (Material.FIREWORK_CHARGE);
            ItemMeta I1Meta = Item1.getItemMeta();
            ShapelessRecipe I1recipe = new ShapelessRecipe(Item1);
            I1recipe.addIngredient(5, Material.COAL_BLOCK);
             I1recipe.addIngredient(3, Material.DIAMOND);
    But now in my EventHandler I need to check if Item1 is being dropped. Right now I have this code to check the item being dropped:
    Code (Text):
    if (!(e.getItemDrop().getItemStack().equals(Item1))) {
                this.getLogger().info("An item was not dropped");
    But I get an error saying that "Item1 cannot be resolved into a variable."
  2. You will need to keep track of all of the recipes you've registered somewhere- a central class (like a "recipe manager") would work well.

    These are some very basic Java/OO concepts that you will probably want to learn about before trying to take on a Bukkit plugin. It's a good idea to at least get the basics of programming down. I know that learning for some goes better when you have a practical application, but it will be hard to get useful help here if you don't have a grasp on the very basics- most people here are more ready to help with API and other specific issues, not so much basic Java coding.
  3. I will admit that im not the best when it comes to coding (Still pretty new at it), but I do know a pretty decent amount of coding. I completly agree with you though I do have alot I need to learn so thats why I like to come to the spigot forums. The community here is a big help and I really appreciate you helping me! I have one more question though, how would I 'call' the class to use it in my listener?
  4. Personally I think the best way is to pass whatever your Listener is going to need to know about into its constructor.

    So in your main plugin class, let's say you do this:

    Code (Text):
    RecipeManager recipes = new RecipeManager();
    // This will load and register all your recipes
    // Then create your listener, giving it the manager
    MyListener listener = new MyListener(recipes);

    // Then in your listener class, you can access methods of the RecipeManager to figure out what you need to
  5. Okay, After looking over my code I realized that was a MUCH easier fix that I cant beleive I didnt think of. All I have to do is check the DisplayName:
    Code (Text):
    if ((e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equals(displayName))) {
    //code goes here
    } else {
    The code works perfectly fine in game, if I drop the correct item then stuff happens and if I dont drop the correct item nothing happens. The only issue though is I get a big error in console if I dont drop the specific item.
  6. You'll want to get the ItemStack, ItemMeta and displayName separately, and check for each one being null.

    I don't think the item should ever be null, but ItemMeta might be.. I think. The getDisplayName definitely will be for most items. But it's generally a good idea to check for null values anywhere you're getting an object and you don't know where it's coming from.
  7. In order for me to get the display name though I have to get the item stack.
  8. Okay so I beleive I fixed it, I tested it out and everything worked just fine. Basically what I did was create 2 variables:
    Code (Text):
    String displayName = getConfig().getString("ItemOne.DisplayName").replaceAll("&", "ยง");
            String getName = e.getItemDrop().getItemStack().getItemMeta().getDisplayName();
    And then I re did the code a little bit:
    Code (Text):
    if ((getName != null) && (getName.equals(displayName))) {
    } else {
    • Like Like x 1