Solved ItemStack(Material.MELON) is undefined

Discussion in 'Plugin Development' started by Zenya4, May 11, 2017.

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

    Zenya4

    Hi all,

    I'm making my first Minecraft plugin but I'm experiencing some difficulty. Here is my code.

    Code:
    
    package com.Zenya4.ExplodingMelons;
    
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.EventPriority;
    
    import org.bukkit.event.block.Action;
    
    import org.bukkit.event.player.PlayerInteractEvent;
    
    import org.bukkit.inventory.ItemStack;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.Bukkit;
    
    import org.bukkit.Material;
    
    import org.bukkit.block.*;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.event.Event;
    
    import org.bukkit.event.player.PlayerEvent;
    
    
    
    public class Main extends JavaPlugin implements Listener {
    
    @Override
    
    public void onEnable() {
    
    
    }
    
    
    @Override
    
    public void onDisable() {
    
    
    }
    
    
    @EventHandler(priority = EventPriority.HIGHEST)
    
    public void onPlayerItemConsumeEvent(Player player, ItemStack item) {
    
    if (item = ItemStack(Material.MELON)) {
    
    //stuff happens here
    
    }
    
    }
    
    }
    
    I'm trying to set the "item" in
    Code:
    
    onPlayerItemConsumeEvent(Player player, ItemStack item)
    
    to a melon slice, so when the player eats a melon, something can happen. Hope someone can help me. Thanks!

    EDIT: It is solved. Nevermind
     
    Last edited: May 11, 2017
  2. For people that have the same issues, you need to put the code under 'public void onEnable() {' instead of 'public void onDisable() {'

    Considering it's solved. please mark your thread as solved.

    Go to the top of your thread, click Thread Tools then click on Edit Title, (here you could have a prefix already if you don't it will say the word prefix. If you do it is the same steps) then click Prefix and change it to Solved or Filled.
     
  3. Offline

    Zenya4

    Ok nevermind it doesnt actually work. The server does register my plugin but it does not do what my plugin wants it to do. I want to make it such that a primedtnt is spawned when a player eats a melon slice. Here is the code (again)

    Code:
    
    package com.Zenya4.ExplodingMelons;
    
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.EventPriority;
    
    import org.bukkit.inventory.ItemStack;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.Location;
    
    import org.bukkit.Material;
    
    import org.bukkit.World;
    
    import org.bukkit.entity.Entity;
    
    import org.bukkit.entity.EntityType;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.entity.TNTPrimed;
    
    
    
    public class Main extends JavaPlugin implements Listener {
    
    @Override
    
    public void onEnable() {
    
    this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "Exploding Melons plugin has been enabled.");
    
    }
    
    @EventHandler(priority = EventPriority.HIGHEST)
    
    public void onPlayerItemConsumeEvent(Player player, ItemStack item) {
    
        ItemStack melon = new ItemStack(Material.MELON);
    
        if(item == melon) {
    
        Player p = player;
    
        Location l = p.getLocation();
    
        World w = p.getWorld();
    
        w.spawnEntity(l, EntityType.PRIMED_TNT);
    
        Entity e = w.spawnEntity(l, EntityType.PRIMED_TNT);
    
        ((TNTPrimed)e).setFuseTicks(0);
    
        }
    
        return;
    
    }
    
    @Override
    
    public void onDisable() {
    
    this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Exploding Melons plugin has been disabled.");
    
    }
    
    }
    Can you help me again? Thanks
    @aGuy_ !
     
  4. Offline

    Caderape2

  5. Offline

    Zenya4

    @Caderape2 I registered the event but it still doesnt work. Am I doing it wrong? 'Cause I dont really understand what
    Code:
    this
    in
    Code:
    .registerEvents(new EatListener(), this);
    is for. Here is my edited code.
    Code:
    
    package com.Zenya4.ExplodingMelons;
    
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.EventPriority;
    
    import org.bukkit.inventory.ItemStack;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.Bukkit;
    
    import org.bukkit.Location;
    
    import org.bukkit.Material;
    
    import org.bukkit.World;
    
    import org.bukkit.entity.Entity;
    
    import org.bukkit.entity.EntityType;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.entity.TNTPrimed;
    
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    
    
    
    public class Main extends JavaPlugin implements Listener {
    
    @Override
    
    public void onEnable() {
    
    this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "Exploding Melons plugin has been enabled.");
    
    Bukkit.getServer().getPluginManager().registerEvents(new EatListener(), this);
    
    }
    
    public final class EatListener implements Listener {
    
    @EventHandler(priority = EventPriority.HIGHEST)
    
    public void eatMelon(PlayerItemConsumeEvent event) {
    
    event.getItem();
    
    event.getPlayer();
    
        ItemStack melon = new ItemStack(Material.MELON);
    
        Player player = event.getPlayer();
    
        if(event.getItem() == melon) {
    
        Location location = player.getLocation();
    
        World world = player.getWorld();
    
        Entity entity = world.spawnEntity(location, EntityType.PRIMED_TNT);
    
        ((TNTPrimed)entity).setFuseTicks(0);
    
        world.spawnEntity(location, EntityType.PRIMED_TNT);
    
        }
    
        }
    
    }
    
    @Override
    
    public void onDisable() {
    
    this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Exploding Melons plugin has been disabled.");
    
    }
    
    }
    
    Thank you!

    EDIT: I also think I'm doing the setfuseticks wrong. I'm not really sure how to summon the PrimedTNT with 0 fuse ticks cause I think it will summon a normal PrimedTNT. But that's just a guess cause it doesn't even summon any TNT yet lol. I would appreciate if anyone could tell me which line/part of the code I'm doing wrong. Thanks!
     
  6. Offline

    Zombie_Striker

    @Zenya4
    1. The "this" is there to tell bukkit which plugin is registering the class.
    2. Remove the event.getItem and event.getPlayer lines. They do nothing.
    3. The item will never equal "melon", as you have only just created melon in that method. Instead, check if the item's type is equal to a melon.
    4. You are spawning two entities. Remove the second spawnEntity line.
     
  7. Offline

    Zenya4

    @Zombie_Striker

    1. Thanks for the info!

    2. Ok. I was just testing what it does actually lol

    3. Sorry, I don't really get what you mean. I changed it to
    Code:
    if(player.getInventory().getItemInMainHand() == melon) {
    }
    
    but I want the event to trigger when the player eats the melon, not just hold it in his hand. Is there a possible way to do that?

    4. Isn't the
    Code:
    Entity entity = world.spawnEntity(location, EntityType.PRIMED_TNT);
    just for declaring "entity" since I put
    Code:
    Entity entity = ...
    Hope you can answer my questions and thanks for your help again!
     
  8. Offline

    ipodtouch0218

    Q3. You should use .getItem() from the event instead of .getItemInMainHand(), as a Player can eat food from their off-hand.

    Don't use "==" for comparing two itemstacks, use ".isSimilar(ItemStack)" or ".equals(ItemStack)".
    You could also check if the Material of the ItemStack (.getType()) is equal to "Material.MELON" instead of checking through ItemStacks.

    Q4. Using World.spawnEntity() returns the Entity that is spawned AND spawns the entity in the world.
     
  9. Offline

    NeinSpass

    You need to add a new itemstack:
    ItemStack melon = new ItemStack(Material.MELON);
     
  10. Offline

    Zombie_Striker

    @NeinSpass
    No, he does not need to.

    @Zenya4
    As Ipod posted, check if the Type is equal to Melon.
     
  11. Offline

    Zenya4

    Thank you all for your help. I finally finished the plugin! :D

    It would not have been possible without all your help and I really appreciate it. As I've said before, this my first plugin and with such a friendly community here, I would be encouraged to make more plugins. Thank you all for your help! I will mark this as [SOLVED]

    For reference: (Completed Code)
    Code:
    
    package com.Zenya4.ExplodingMelons;
    
    
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.event.EventHandler;
    
    import org.bukkit.event.EventPriority;
    
    import org.bukkit.inventory.ItemStack;
    
    import org.bukkit.event.Listener;
    
    import org.bukkit.Bukkit;
    
    import org.bukkit.Location;
    
    import org.bukkit.Material;
    
    import org.bukkit.World;
    
    import org.bukkit.entity.Entity;
    
    import org.bukkit.entity.EntityType;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.entity.TNTPrimed;
    
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    
    
    
    public class Main extends JavaPlugin implements Listener {
    
    @Override
    
    public void onEnable() {
    
    this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "Exploding Melons plugin has been enabled.");
    
    Bukkit.getServer().getPluginManager().registerEvents(new EatListener(), this);
    
    }
    
    public final class EatListener implements Listener {
    
    @EventHandler(priority = EventPriority.HIGHEST)
    
    public void eatMelon(PlayerItemConsumeEvent event) {
    
        Player player = event.getPlayer();
    
        ItemStack melon = player.getInventory().getItemInMainHand();
    
        if (melon.getType() == Material.MELON) {
    
        if (event.getItem().isSimilar(melon)) {
    
        Location location = player.getLocation();
    
        World world = player.getWorld();
    
        Entity entity = world.spawnEntity(location, EntityType.PRIMED_TNT);
    
        ((TNTPrimed)entity).setFuseTicks(0);
    
        }
    
        }
    
        }
    
        }
    
    @Override
    
    public void onDisable() {
    
    this.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Exploding Melons plugin has been disabled.");
    
    }
    
    }
    
     
Thread Status:
Not open for further replies.

Share This Page