Respawn with X amount of hunger.

Discussion in 'Archived: Plugin Requests' started by jordan12345, Sep 13, 2012.

  1. Offline

    puyttre

    Still get a big error with console.
    Added this to the Listener:
    Code:java
    1. public RespawnListener(HealthOnDeath hod){
    2. this.hod = hod;
    3. }


    Added this to main class:
    Code:java
    1. public RespawnListener respawn = new RespawnListener(this);


    I completely understand what that's supposed to do, yet it doesn't work. :(
     
  2. Offline

    -_Husky_-

    Remove the listener line

    In onEnable()

    Have this

    Code:Java
    1. getServer().getPluginManager().registerEvents(new RespawnListener(this), this);
     
  3. Offline

    Vandrake

  4. Offline

    ConanEdogawa

    whats the error?
     
  5. Offline

    Vandrake

    Dont forget to check the @EventHandler before the events and dont forget to add the little instance at the beggining of your listener class :3 Peace
     
  6. Offline

    Musicguy

    Look at this source code (https://github.com/Musicguy/Beer) it shows you how to create a config and use it.
     
  7. Offline

    puyttre

    Fixed the error with
    -_Husky_- 's and
    Vandrake 's suggestions.
    Yet it doesn't respawn you with the given health and hunger.

    EDIT: It must be something to do with player.setHealth() and player.setHunger() because I set them to 1 instead of getHunger() and they still didnt do anything on the respawn.
     
  8. Offline

    Woobie

    I know how to create and load configs, also i know how to use getBoolean, but not the other stuff =/
    I created a thread about this config stuff on Plugin Development
     
  9. Offline

    puyttre

    Well... here:
    1. Create a new file "config.yml" in the same place that "plugin.yml" goes. Put all the default text in it.
    2. Use "this.saveDefaultConfig();" in onEnable() to first check if config.yml exists, then if it doesn't it uses the one in the .jar and saves it to plugins/<plugin>/config.yml
    3. Then you can get variable from keywords like: "int hunger = getConfig().getInt("hunger");"
    So in your config.yml, it looks for "hunger" and gets the integer after the colon.

    Meh. Letting someone else take it from here.
    Code:java
    1. import org.bukkit.plugin.PluginDescriptionFile;
    2. import org.bukkit.plugin.PluginManager;
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class HealthOnDeath extends JavaPlugin {
    6.  
    7.  
    8. @Override
    9. public void onEnable(){
    10. // Save the config inside the .jar
    11. this.saveDefaultConfig();
    12.  
    13. // Saving PluginManager and PDF to variables.
    14. PluginManager pm = getServer().getPluginManager();
    15. PluginDescriptionFile pdffile = this.getDescription();
    16.  
    17. // Calls log() method below.
    18. log("HealthOnDeath successfully enabled.");
    19. // Resgistering events.
    20. getServer().getPluginManager().registerEvents(new RespawnListener(this), this);
    21. }
    22.  
    23.  
    24.  
    25. public void log(String x){
    26. getServer().getLogger().info(x);
    27. }
    28.  
    29.  
    30.  
    31. public int getHealth(){
    32. int health = getConfig().getInt("health");
    33. return health;
    34. }
    35. public int getHunger(){
    36. int hunger = getConfig().getInt("hunger");
    37. return hunger;
    38. }
    39.  
    40.  
    41. }

    Code:java
    1. import org.bukkit.entity.Player;
    2. import org.bukkit.event.EventHandler;
    3. import org.bukkit.event.Listener;
    4. import org.bukkit.event.player.PlayerRespawnEvent;
    5.  
    6. public class RespawnListener implements Listener {
    7.  
    8. // Saves our HealthOnDeath.java to a variable that we can use to call methods.
    9. HealthOnDeath hod = new HealthOnDeath();
    10. public RespawnListener(HealthOnDeath hod){
    11. this.hod = hod;
    12. }
    13.  
    14. @EventHandler
    15. public void onRespawn(PlayerRespawnEvent event){
    16. // Gets the player that triggered the the PlayerRespawnEvent
    17. Player player = event.getPlayer();
    18. //If Player is an op or has permission to ignore this event, do nothing.
    19. if(player.isOp() || player.hasPermission("healthonrespawn.bypass")){
    20. // Don't do anything! Minecraft will take care of it :)
    21. }else{ // otherwise set the hunger and health to whatever!
    22. player.setFoodLevel(hod.getHunger());
    23. player.setHealth(hod.getHealth());
    24. }
    25. }
    26.  
    27. }
    28.  


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

    JjPwN1

    jordan12345 - I have made HungerRespawn for you!

    It does simply what you wanted. If a player dies, set their hunger to 5. The amount of hunger to set it to is configurable. I have explained what to do to actually give them 5 hunger bars (in config), because just putting 5 as the config option won't give them 5 hunger bars.

    Download:
    <Edit by Moderator: Redacted mediafire url>

    Permissions:
    hungerrespawn.bypass - Bypass the plugin so you respawn with full hunger.

    Config:
    Code:
    # JjPwN1's HungerRespawn v0.1
    # 10 hunger actually equals 5, because the max hunger is 20.
    # So, if you want them to respawn with, let's say 3 hunger,
    # the fraction for that is 3/10. Multiply the numerator and
    # the denominator by 2 to get what you should put as the
    # hunger_on_respawn. So, to respawn with 3 hunger, you'd put
    # 6 as the hunger_on_respawn.
    hunger_on_respawn: 10
    display_message: true
    Note: The player receives the hunger change 5 seconds after dying.
     
    Last edited by a moderator: Nov 9, 2016
  11. Offline

    -_Husky_-

    puyttre

    Line 9: should be
    Code:java
    1. HealthOnDeath hod = null;


    Therefore, when it's declared (called from onEnable(), using the argument 'this' which is 'HeathOnDeath' it will call the constructor, setting hod to the class which called it, You don't need all that mess if you don't want to use methods from that class).
     
  12. Offline

    np98765

    Why are you guys putting so much energy into this? :confused:

    I guess no one mentioned that this does exactly what he wants? :p
     
  13. Yea i think he wanted a plugin like that
     
  14. Offline

    Vandrake

    If you need help creating customized yml like (players.yml or woobie.yml) or the regular config.yml contact me :3 I'll do my best to enlighten you in the right direction
     
  15. Offline

    PogoStick29

    I'd like to know how to make more than one config :)
     
  16. Offline

    JjPwN1

    Same har
     
  17. Offline

    Vandrake

Share This Page