Eating a Specific Item

Discussion in 'Plugin Development' started by Ian0526, Jul 10, 2013.

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

    Ian0526

    I have created a plugin for my server but the playeritemconsume event seems to be broken.
    Code:
    package me.Ian0526.GodFromStone;
     
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.logging.Logger;
     
    import net.minecraft.server.v1_6_R1.Block;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
     
    public class Core extends JavaPlugin implements Listener
    {
      public static Plugin plugin;
      public final Logger logger = Logger.getLogger("Minecraft");
     
      public void onEnable() {
        this.logger.info("[GodFromStone] Plugin Enabled");
        Bukkit.getPluginManager().registerEvents(this, this);
      }
     
      public void onDisable() {
        this.logger.info("[GodFromStone] Plugin Disabled");
        getConfig().options().copyDefaults(true);
        saveConfig();
      }
        ItemStack gapple = new ItemStack(Material.GOLDEN_APPLE);{
            ItemMeta meta = (ItemMeta) gapple.getItemMeta();
            meta.setDisplayName("§a§l§k]§b§l§k[§5§l Ultimate God Apple §a§l§k]§b§l§k[");
            ArrayList<String> lores = new ArrayList<String>();
            lores.add("§cStrength 3 §a- §5§l5:00");
            lores.add("§dRegeneration 10 §a- §5§l1:00");
            lores.add("§8Resistance 2 §a- §5§l5:00");
            lores.add("§6Fire Resistance §a- §5§l20:00");
            lores.add("§bSwiftness 3 §a- §5§l4:00");
            meta.setLore(lores);
            gapple.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 0);
            gapple.setItemMeta(meta);
            gapple.removeEnchantment(Enchantment.ARROW_DAMAGE);
        }
          public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
                Player player = (Player)sender;
                if(commandlabel.equalsIgnoreCase("gapple"));
                    if (player.hasPermission("gapple.give"));{
                        player.getInventory().addItem(gapple);
                    }
                    return false;
          }
        @EventHandler
        public void onBlockBreak(BlockBreakEvent  event) {
            Player player = event.getPlayer();
            Random r = new Random();
            Location loc = player.getLocation();
         
            if(event.getBlock().getType() == Material.STONE && r.nextInt(2000) + 1 ==  r.nextInt(2000) + 1) {
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.playSound(loc, Sound.PORTAL_TRAVEL, 100, 1);
                player.sendMessage("§5§oGod's Whispers fill your head as the apple drops.");
                player.getWorld().dropItemNaturally(player.getLocation(), gapple);
            }
             
            }
     
        @EventHandler
        public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            Location loc = player.getLocation();
                if(event.equals(gapple));
                    player.sendMessage("§d§oGod's Power moves through your body!");
                    player.playSound(loc, Sound.PORTAL_TRAVEL, 1, 100);
                    player.removePotionEffect(PotionEffectType.REGENERATION);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1200, 10));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 6000, 2));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 24000, 1));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4800, 3));
        }
    }
    Eating any item gives you the effects of the item gapple
     
  2. Offline

    adde

    Any errors?
     
  3. Offline

    ZeusAllMighty11

    Events don't equal gapple. That's your issue.
     
  4. Offline

    Ian0526

    And how would I write that
     
  5. Offline

    xTrollxDudex

    Ian0526
    It's getServer() not Bukkit in your plugin registry but I don't think that matters. What's the console out put when your in the plugin?
     
  6. Offline

    ZeusAllMighty11

    xTrollxDudex

    That doesn't matter.


    The problem is that he tried to check if the event was equal to an itemstack, which it never will be.

    Use the getItem() method or whatever. check the suggestions for the methods in the event
     
  7. Offline

    slasheh

    In your if statement in the ItemConsumeEvent you're comparing an Event to an ItemStack. To fix get the player's item in hand and then compare the item in hand to the golden apple, also remember to use brackets with if statements and not a semicolon.
     
    Ian0526 likes this.
  8. Offline

    Ian0526

    So like this?
    Code:
        @EventHandler
        public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            Location loc = player.getLocation();
                if(player.getItemInHand().equals(gapple));
                    player.sendMessage("§d§oGod's Power moves through your body!");
                    player.playSound(loc, Sound.PORTAL_TRAVEL, 1, 100);
                    player.removePotionEffect(PotionEffectType.REGENERATION);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1200, 10));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 6000, 2));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 24000, 1));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4800, 3));
     
  9. Offline

    ZeusAllMighty11

    You should use event.getItem()
     
  10. Offline

    Ian0526

    It Is still broken, any item eaten gives the effects
     
  11. Offline

    ERROR372

    Ian0526

    Uh... did you notice that you have a semicolon after your if statement? That should be throwing massive errors...

    It also would make the compiler not take in the if statement...

    After removing that semicolon... you should surround that whole indented chunk with { }
     
  12. Offline

    Ian0526

    I will try that
     
  13. Offline

    Eats_Rainbows

    Please! Try cleaning your code up before posting here! Indent a bit! It is much easier for everybody to read and you'll get more people helping you!
     
  14. Offline

    Ian0526

    Same outcome.
     
  15. Offline

    ERROR372

    Eats_Rainbows

    I dunno, I found the code pretty readable. Just noticed the semi-colons after almost all the if-statements, and well... that's not right =P
     
  16. Offline

    chasechocolate

    Ian0526 post your current code.
     
  17. Offline

    Ian0526


    Code:
    package me.Ian0526.GodFromStone;
     
    import java.awt.Event;
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.logging.Logger;
     
    import net.minecraft.server.v1_6_R1.Block;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
     
    public class Core extends JavaPlugin implements Listener
    {
      public static Plugin plugin;
      public final Logger logger = Logger.getLogger("Minecraft");
     
      public void onEnable() {
        this.logger.info("[GodFromStone] Plugin Enabled");
        Bukkit.getPluginManager().registerEvents(this, this);
      }
     
      public void onDisable() {
        this.logger.info("[GodFromStone] Plugin Disabled");
        getConfig().options().copyDefaults(true);
        saveConfig();
      }
        ItemStack gapple = new ItemStack(Material.GOLDEN_APPLE);{
            ItemMeta meta = (ItemMeta) gapple.getItemMeta();
            meta.setDisplayName("§a§l§k]§b§l§k[§5§l Ultimate God Apple §a§l§k]§b§l§k[");
            ArrayList<String> lores = new ArrayList<String>();
            lores.add("§cStrength 3 §a- §5§l5:00");
            lores.add("§dRegeneration 10 §a- §5§l1:00");
            lores.add("§8Resistance 2 §a- §5§l5:00");
            lores.add("§6Fire Resistance §a- §5§l20:00");
            lores.add("§bSwiftness 3 §a- §5§l4:00");
            meta.setLore(lores);
            gapple.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 0);
            gapple.setItemMeta(meta);
            gapple.removeEnchantment(Enchantment.ARROW_DAMAGE);
        }
          public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
                Player player = (Player)sender;
                if(commandlabel.equalsIgnoreCase("gapple"));
                    if (player.hasPermission("gapple.give"));{
                        player.getInventory().addItem(gapple);
                    }
                    return false;
          }
        @EventHandler
        public void onBlockBreak(BlockBreakEvent  event) {
            Player player = event.getPlayer();
            Random r = new Random();
            Location loc = player.getLocation();
         
            if(event.getBlock().getType() == Material.STONE && r.nextInt(2000) + 1 ==  r.nextInt(2000) + 1) {
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.playSound(loc, Sound.PORTAL_TRAVEL, 100, 1);
                player.sendMessage("§5§oGod's Whispers fill your head as the apple drops.");
                player.getWorld().dropItemNaturally(player.getLocation(), gapple);
            }
             
            }
     
        @EventHandler
        public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            Location loc = player.getLocation();
            if(event.getItem().equals(gapple));{
                    player.sendMessage("§d§oGod's Power moves through your body!");
                    player.playSound(loc, Sound.PORTAL_TRAVEL, 1, 100);
                    player.removePotionEffect(PotionEffectType.REGENERATION);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1200, 10));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 6000, 2));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 24000, 1));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4800, 3));   
            }
        }
    }
     
  18. Offline

    chasechocolate

    Create a null "gapple" variable (ItemStack gapple = null), and in your onEnable(), add:
    Code:java
    1. gapple = new ItemStack(Material.GOLDEN_APPLE);
    2. ItemMeta meta = (ItemMeta) gapple.getItemMeta();
    3. meta.setDisplayName("§a§l§k]§b§l§k[§5§l Ultimate God Apple §a§l§k]§b§l§k[");
    4. ArrayList<String> lores = new ArrayList<String>();
    5. lores.add("§cStrength 3 §a- §5§l5:00");
    6. lores.add("§dRegeneration 10 §a- §5§l1:00");
    7. lores.add("§8Resistance 2 §a- §5§l5:00");
    8. lores.add("§6Fire Resistance §a- §5§l20:00");
    9. lores.add("§bSwiftness 3 §a- §5§l4:00");
    10. meta.setLore(lores);
    11. gapple.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 0);
    12. gapple.setItemMeta(meta);
    13. gapple.removeEnchantment(Enchantment.ARROW_DAMAGE);

    Also delete your old "gapple" variable and the ItemMeta stuff below it.
     
  19. Offline

    ERROR372

    Ian0526

    if(commandlabel.equalsIgnoreCase("gapple"));
    if (player.hasPermission("gapple.give"));{

    if(event.getItem().equals(gapple));


    You still have semicolons after your if statements... get rid of those!
     
  20. Offline

    Ian0526

    K thanks bros :D
     
  21. Offline

    Eats_Rainbows

    It is some of the cleaner codes, but it is good to keep consistency when you write your code. Making your code spaced out a bit can help it look more professional... There are also alot of newbie coder mistakes in there.

    This
    Code:
    player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
    could be replaced with.
    Code:
    for (int i =0; i < 12; i++) {
        player.sendMessage(" ");
    }
    This is not as known, but this is a way cleaner way to add lores.

    Instead of

    Code:
    lores.add("§cStrength 3 §a- §5§l5:00");
    lores.add("§dRegeneration 10 §a- §5§l1:00");
    lores.add("§8Resistance 2 §a- §5§l5:00");
    lores.add("§6Fire Resistance §a- §5§l20:00");
    lores.add("§bSwiftness 3 §a- §5§l4:00");
    meta.setLore(lores);
    I do
    Code:
    List<String> lores = Arrays.asList("§dRegeneration 10 §a- §5§l1:00", "§8Resistance 2 §a- §5§l5:00", "§6Fire Resistance §a- §5§l20:00", "§bSwiftness 3 §a- §5§l4:00");
    meta.setLore(lores)[/CODE]
     
  22. Offline

    Ian0526

    I saw that earlier, I am just a beginner but thank you for teaching me :)

    chasechocolate Where do I import the ItemStack gapple = null; ?

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

    xTrollxDudex

    Ian0526
    Even if I don't trust you with that avatar, what do you mean "where do I import ItemStack gapple = null"? You just put that just after you begin the class.
    PHP:
    public class YourClass extends JavaPlugin{

    public 
    ItemStack gapple null;

    //the on enable with all the stuff chasecohocolate told you about
    //the rest of the class
    }
     
    blobic123 likes this.
  24. Offline

    Ian0526

    Code:
    package me.Ian0526.GodFromStone;
     
    import java.awt.Event;
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.logging.Logger;
     
    import net.minecraft.server.v1_6_R1.Block;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
     
    public class Core extends JavaPlugin implements Listener
    {
      public static Plugin plugin;
      public final Logger logger = Logger.getLogger("Minecraft");
      public ItemStack gapple = null;
     
      public void onEnable() {
        this.logger.info("[GodFromStone] Plugin Enabled");
        gapple = new ItemStack(Material.GOLDEN_APPLE);
        ItemMeta meta = (ItemMeta) gapple.getItemMeta();
        meta.setDisplayName("§a§l§k]§b§l§k[§5§l Ultimate God Apple §a§l§k]§b§l§k[");
        ArrayList<String> lores = new ArrayList<String>();
        lores.add("§cStrength 3 §a- §5§l5:00");
        lores.add("§dRegeneration 10 §a- §5§l1:00");
        lores.add("§8Resistance 2 §a- §5§l5:00");
        lores.add("§6Fire Resistance §a- §5§l20:00");
        lores.add("§bSwiftness 3 §a- §5§l4:00");
        meta.setLore(lores);
        gapple.addUnsafeEnchantment(Enchantment.ARROW_DAMAGE, 0);
        gapple.setItemMeta(meta);
        gapple.removeEnchantment(Enchantment.ARROW_DAMAGE);
        Bukkit.getPluginManager().registerEvents(this, this);
        }
     
      public void onDisable() {
        this.logger.info("[GodFromStone] Plugin Disabled");
        getConfig().options().copyDefaults(true);
        saveConfig();
      }
          public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
                Player player = (Player)sender;
                if(commandlabel.equalsIgnoreCase("gapple"));
                    if (player.hasPermission("gapple.give"));{
                        player.getInventory().addItem(gapple);
                    }
                    return false;
          }
        @EventHandler
        public void onBlockBreak(BlockBreakEvent  event) {
            Player player = event.getPlayer();
            Random r = new Random();
            Location loc = player.getLocation();
         
            if(event.getBlock().getType() == Material.STONE && r.nextInt(2000) + 1 ==  r.nextInt(2000) + 1) {
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.sendMessage(" ");
                player.playSound(loc, Sound.PORTAL_TRAVEL, 100, 1);
                player.sendMessage("§5§oGod's Whispers fill your head as the apple drops.");
                player.getWorld().dropItemNaturally(player.getLocation(), gapple);
            }
             
            }
     
        @EventHandler
        public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            Location loc = player.getLocation();
            if(event.getItem() == null);{
                    player.sendMessage("§d§oGod's Power moves through your body!");
                    player.playSound(loc, Sound.PORTAL_TRAVEL, 1, 100);
                    player.removePotionEffect(PotionEffectType.REGENERATION);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1200, 10));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 6000, 2));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 24000, 1));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4800, 3));
            }
            return;
        }
    }
    Still getting the same effect :/
     
  25. Offline

    Kazzababe


    Get rid of the semi-colons after your if statements .
    That return statement in your onPlayerItemConsume() method is not needed.
    You should probably surround the code after your if statements using curly braces.
    Code:
    if(something == somethingelse) {
        //Do something here
    }
     
  26. Offline

    Diamondminer77

    Never mind, I checked the code in my last post and it didn't work. I tested this code and it should work:

    Code:
     @EventHandler
        public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
            Player player = event.getPlayer();
            Location loc = player.getLocation();
            if(player.getItemInHand().getType() == Material.GOLDEN_APPLE) {
                    if(player.getItemInHand().getItemMeta().getDisplayName() ==
     
                  ("§a§l§k]§b§l§k[§5§l Ultimate God Apple §a§l§k]§b§l§k[") {
     
                    player.sendMessage("§d§oGod's Power moves through your body!");
                    player.playSound(loc, Sound.PORTAL_TRAVEL, 1, 100);
                    player.removePotionEffect(PotionEffectType.REGENERATION);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 3));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1200, 10));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 6000, 2));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 24000, 1));
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 4800, 3));
     
     
            }
     
            }
            }
     
    

    Hope I helped
     
  27. Offline

    PocketMines

    Diamondminer77
    I tried your code, and it doesn't want to work-
    Code:java
    1. @EventHandler
    2. public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
    3. Player player = event.getPlayer();
    4. if(player.getItemInHand().getType() == Material.BREAD) {
    5. if(player.getItemInHand().getItemMeta().getDisplayName() == "Twisted Rum") {
    6. player.sendMessage("You feel drowsy... but will it be enough?");
    7. player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 1000000, 0));
    8. }
    9. }
    10. }
     
  28. Offline

    ChipDev

    Make sure to read dates <3
     
  29. Offline

    PocketMines

    Worth a shot :oops:
    (¬_¬)
     
    ChipDev likes this.
  30. Offline

    Skionz

Thread Status:
Not open for further replies.

Share This Page