Advertisement
Guest User

RespawnHealth.java

a guest
Jul 31st, 2011
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. package com.wwsean08.RespawnHealth;
  2.  
  3. import java.util.logging.Logger;
  4.  
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.Server;
  7. import org.bukkit.event.Event;
  8. import org.bukkit.plugin.PluginManager;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10. import org.bukkit.util.config.Configuration;
  11.  
  12. public class RespawnHealth extends JavaPlugin{
  13.     //all of the instance variables, some are important, others not quite so much
  14.     private Configuration config;   //this is the variable that allows us to access the config file
  15.     private Server server;          //this is a hook into the server which is useful
  16.     private PluginManager pm;       //the plugin manager that allows us to register our player listener
  17.     private RespawnHealthPlayerListener playerListener;     //our player listener
  18.     int respawnHealth;              //the health we will leave the player with upon respawn
  19.     final String PREFIX = "[RespawnHealth] ";   //the prefix that showes up in the console
  20.     Logger log;                     //the logger that displays the text in the console
  21.     final String VERSION = "1.0.0"; //the version that is displayed in the console
  22.  
  23.     //the method that is called when disabling the plugin (like on server shutdown)
  24.     @Override
  25.     public void onDisable() {
  26.         log.info(PREFIX + "disabled");
  27.     }
  28.  
  29.     //the method that is called when enabling the plugin on server startup
  30.     @Override
  31.     public void onEnable() {
  32.         server = Bukkit.getServer();    //this instantiates the server variable so we can use it
  33.         log = server.getLogger();       //this gets the logger using the server (my prefered way of doing it)
  34.         pm = server.getPluginManager(); //we also instantiate the plugin manager using the server
  35.         playerListener = new RespawnHealthPlayerListener(this); //instantiate the playerListener we created
  36.         config = this.getConfiguration();   //instantiate the configuration (note that the getConfiguration method does not exist in the plugin
  37.                                             //this takes advantage of extending JavaPlugin.  I can explain more if you want.
  38.         checkForConfigFile();               //check if the config file exists already, if not, make it
  39.         respawnHealth = config.getInt("respawnHealth", 2);  //get the respawn health value, it's default is 2, if i was smart i wouuld put some error checking in
  40.         log.info(PREFIX + "Health upon respawn set to " + respawnHealth);   //print out the respawn health value to the server console
  41.         pm.registerEvent(Event.Type.PLAYER_RESPAWN, playerListener, Event.Priority.Low, this);  //register our event, if you would like to understand this better...well i'm not
  42.                                                                 //going to be able to help you with that unfortunately lol.  I just know how to use it.
  43.         log.info(PREFIX + "V. " + VERSION + " enabled");
  44.     }
  45.    
  46.     private void checkForConfigFile(){
  47.         Object test = config.getProperty("respawnHealth");  //we get the property of respawnHealth which is in the config file, if it comes back null we know 1 of 2 things
  48.         /*
  49.          * 1. The file does not exist, which means either the plugin has never been run or that the file got deleted for some reason.
  50.          * 2. A user messed with the file and so the value should be reset anyways.
  51.          */
  52.         if(test == null){
  53.             config.setProperty("respawnHealth", 2);     //we write the value to the config file
  54.             config.save();                              //save it
  55.             log.info(PREFIX + "config.yml created");    //give user output
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement