Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wwsean08.RespawnHealth;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- import org.bukkit.Server;
- import org.bukkit.event.Event;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.util.config.Configuration;
- public class RespawnHealth extends JavaPlugin{
- //all of the instance variables, some are important, others not quite so much
- private Configuration config; //this is the variable that allows us to access the config file
- private Server server; //this is a hook into the server which is useful
- private PluginManager pm; //the plugin manager that allows us to register our player listener
- private RespawnHealthPlayerListener playerListener; //our player listener
- int respawnHealth; //the health we will leave the player with upon respawn
- final String PREFIX = "[RespawnHealth] "; //the prefix that showes up in the console
- Logger log; //the logger that displays the text in the console
- final String VERSION = "1.0.0"; //the version that is displayed in the console
- //the method that is called when disabling the plugin (like on server shutdown)
- @Override
- public void onDisable() {
- log.info(PREFIX + "disabled");
- }
- //the method that is called when enabling the plugin on server startup
- @Override
- public void onEnable() {
- server = Bukkit.getServer(); //this instantiates the server variable so we can use it
- log = server.getLogger(); //this gets the logger using the server (my prefered way of doing it)
- pm = server.getPluginManager(); //we also instantiate the plugin manager using the server
- playerListener = new RespawnHealthPlayerListener(this); //instantiate the playerListener we created
- config = this.getConfiguration(); //instantiate the configuration (note that the getConfiguration method does not exist in the plugin
- //this takes advantage of extending JavaPlugin. I can explain more if you want.
- checkForConfigFile(); //check if the config file exists already, if not, make it
- 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
- log.info(PREFIX + "Health upon respawn set to " + respawnHealth); //print out the respawn health value to the server console
- 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
- //going to be able to help you with that unfortunately lol. I just know how to use it.
- log.info(PREFIX + "V. " + VERSION + " enabled");
- }
- private void checkForConfigFile(){
- 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
- /*
- * 1. The file does not exist, which means either the plugin has never been run or that the file got deleted for some reason.
- * 2. A user messed with the file and so the value should be reset anyways.
- */
- if(test == null){
- config.setProperty("respawnHealth", 2); //we write the value to the config file
- config.save(); //save it
- log.info(PREFIX + "config.yml created"); //give user output
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement