[Please Help!]Quick question about events

Discussion in 'Plugin Development' started by The_Coder, Aug 11, 2012.

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

    The_Coder

    Hi, I am make a drugs plugin and I need to set food ticks down unless there is an item in there inventory. I know how to do this via a command but I need it to be a listener. Would it be a onPlayerMoveEvent?
    Thanks
     
  2. Offline

    travja

    Just find the event you want to trigger it and input the code from the command.
     
  3. Offline

    The_Coder

    travja can you fix this:

    Main:
    Code:
    package me.The_Coder.Project;
     
    import java.util.logging.Logger;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class TheProjectMain extends JavaPlugin{
     
        public static TheProjectMain plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public final ProjectListener blockListener = new ProjectListener(this);
     
     
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is now disabled.");
            saveConfig();
        }
     
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.blockListener, this);
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " is now enabled!");
            getConfig().options().copyDefaults(true);
         
        }
     
    }
    Listener:
    Code:
    package me.The_Coder.Project;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class ProjectListener implements Listener {
        public static TheProjectMain plugin;
     
        public ProjectListener(TheProjectMain theProjectMain) {
            plugin = theProjectMain;
        }
     
        public void onPlayerMove(PlayerMoveEvent pme) {
        Player p = pme.getPlayer();
         
        if(p.getInventory().contains(Material.SUGAR)) {
         
            p.setFoodLevel(20);
            ItemStack itemstack = new ItemStack(Material.SUGAR, 1);
            p.getInventory().remove(itemstack);
        }else {
         
            p.setFoodLevel(2);
            if(p.getFoodLevel() > 4) {
                p.sendMessage(ChatColor.DARK_RED + "You need drugs!");
            }
         
        }
     
     
     
     
        }
    }
    nothing happens when my hungry bar should go down
     
  4. Offline

    travja

    Any time you move, Even when you move your mouse, this event is triggered.... So, it will remove all sugar in your inventory VERY quickly..... So I suggest.... You check if their food is lower than say 19. Then do it. Also... your food isn't going down if you don't have sugar... Is it giving you the message?
     
  5. Offline

    The_Coder

    travja how many hunger ticks are there in the bar
     
  6. Offline

    travja

    What do you mean? How long does it take before it goes down 1 or what?
     
  7. Offline

    The_Coder

    I mean like heath, each player has 20 ticks of heath
     
  8. Offline

    travja

    ok, so what do you want done?
     
  9. Offline

    The_Coder

    i need to know the most efficient and easy way possible to decrees the hunger bar
     
  10. Offline

    travja

    It's either player.setFood() or player.setFoodLevel() either way, you would do player.setFoodLevel(player.getFoodLevel()-2); That would remove a drumstick from their food bar. But... The Player Move event isn't the best one to make that in unless you make a scheduler that you only allow 1 instance of therefore only running it when it's needed. Otherwise their food would go kerplat...
     
  11. Offline

    The_Coder

    well how do you stop the food bar from going down at a certain point than the player wouldn't die

    maybe like this:
    Code:
    p.setFoodLevel(2);
            if(p.getFoodLevel() > 2) {
                p.setFoodLevel(2);
                p.sendMessage(ChatColor.DARK_RED + "You need drugs!");
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  12. Offline

    travja

    You could check the food level and if it's below say 6 (3 drumsticks) they won't lose food, however, you would need a scheduler to keep their food from plummeting when they only move their mouse.

    That would work too

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

    The_Coder

    let me test that

    travja
    it doesn't seem to work

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

    travja

    The_Coder
    Can you give me your class again.
     
  15. Offline

    The_Coder

    Here is the Listener:

    Code:
    package me.The_Coder.Project;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class ProjectListener implements Listener {
        public static TheProjectMain plugin;
       
        public ProjectListener(TheProjectMain theProjectMain) {
            plugin = theProjectMain;
        }
       
        public void onPlayerMove(PlayerMoveEvent pme) {
        Player p = pme.getPlayer();
           
        if(p.getInventory().contains(Material.SUGAR)) {
           
            p.setFoodLevel(p.getFoodLevel()-2);
            ItemStack itemstack = new ItemStack(Material.SUGAR, 1);
            p.getInventory().remove(itemstack);
        }else {
           
            p.setFoodLevel(2);
            if(p.getFoodLevel() > 2) {
                p.setFoodLevel(2);
                p.sendMessage(ChatColor.DARK_RED + "You need drugs!");
            }
           
        }
       
       
       
       
        }
    }
     
  16. Offline

    travja

    oh, you are taking food off when they have food, you could run an if and see if their health is below 19 and if it is add the food and remove the sugar, and if they don't, don't take them down to 2 right off the bat, do it after your run the if(p.getFoodLevel() > 2) because if you lower to 2 before you run the if it won't trigger the if statement.
     
  17. Offline

    The_Coder

    travja what do you mean by that
     
  18. Offline

    travja

    if(p.getInventory().contains(Material.SUGAR)) {

    p.setFoodLevel(p.getFoodLevel()-2); ItemStack itemstack = new ItemStack(Material.SUGAR, 1); p.getInventory().remove(itemstack);
    }else {

    p.setFoodLevel(2);
    if(p.getFoodLevel() > 2) {
    p.setFoodLevel(2);
    p.sendMessage(ChatColor.DARK_RED + "You need drugs!");
    }
    The bold stuff is where you are taking food off when they have sugar, the red code is the code you need to remove
     
  19. Offline

    The_Coder

    so sorry about I think the problem was I was trying to make it so food would good down faster
     
  20. Offline

    travja

    So, run with that code and see if it's how you like it... then get back to me.
     
  21. Offline

    The_Coder

    well my hunger goes down and the sugar doesn't go down and I don't get food bars
     
  22. Offline

    Phil2812

    Just throwing that in: I guess it would be better to listen for a FoodLevelChangeEvent here. This is an entity event so remember to check if the entity is a player.
     
  23. Offline

    The_Coder

    thanks Phil I will check if they are a player or not

    Phil2812
    but I did this line of code:
    Code:
    Player p = pme.getPlayer();    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  24. Offline

    Phil2812

    You only have to check if the entity is a player when you use the FoodLevelChangeEvent and not a PlayerMoveEvent. But I just read again what you are trying to do and a FoodLevelChangeEvent would not help you with that. Sorry I will try to read more carefully the next time ;)
     
  25. Offline

    The_Coder

    do you have any idea how to fix this code
     
  26. Offline

    Phil2812

    The_Coder
    Post what you have now. I assume you have tried and implemented the code travja suggested? If not please do so first.
     
  27. Offline

    The_Coder

    ok here is my Listener:
    Code:
    package me.The_Coder.Project;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
     
    public class ProjectListener implements Listener {
        public static TheProjectMain plugin;
       
        public ProjectListener(TheProjectMain theProjectMain) {
            plugin = theProjectMain;
        }
       
        public void onPlayerMove(PlayerMoveEvent pme) {
       
       
           
        Player p = pme.getPlayer();   
        if(p.getInventory().contains(Material.SUGAR)) {
           
            p.setFoodLevel(p.getFoodLevel()-2);
            ItemStack itemstack = new ItemStack(Material.SUGAR, 1);
            p.getInventory().remove(itemstack);
        }else {
            if(p.getFoodLevel() > 2) {
                p.setFoodLevel(2);
                p.sendMessage(ChatColor.DARK_RED + "You need drugs!");
            }
           
        }
       
       
       
       
        }
    }
     
  28. Offline

    Phil2812

    And what happens? And what do you want to happen? How fast does you food bar decrease now?
     
  29. Offline

    The_Coder

    my food bar decreases faster than normal, what I want to happen is when a player moves the hunger bar will go down and if a player has a material like sugar it will decrease in number of sugar and give you food bars

    Bump

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

    ThijsD

    p.getFoodLevel()-2 You are decreasing the food level, when you actually want to increase it?
     
Thread Status:
Not open for further replies.

Share This Page