Skip to content

Instantly share code, notes, and snippets.

@1129hsalS
Created July 23, 2018 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1129hsalS/c2554de4bf8067843d93d86d6f01416b to your computer and use it in GitHub Desktop.
Save 1129hsalS/c2554de4bf8067843d93d86d6f01416b to your computer and use it in GitHub Desktop.
Code of a Bukkit v1.13 plugin called HealthSteal by Slash9211
package me.slash9211;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;
public class HealthStealEvent implements Listener {
/*
* When a player attacks a player it checks to see if the weapon was a sword (Diamond, Iron, Gold, Stone, or Wood)
* Then checks to make sure the sword name is what the configuration says (Default: 'Health Steal')
* Also checks to see if a player was damaged and if their health is less than 20 (Full row of hearts) it'll
* change the MAX health from 40 (Two rows of hearts) down to 20 so there's no extra row of empty hearts, same with going up.
*/
@SuppressWarnings("deprecation")
@EventHandler
public void onPlayerHit(EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) { //Makes sure both are players
Player damager = (Player) event.getDamager();
ItemStack weapon = damager.getInventory().getItemInMainHand();
if (Main.swordID.contains(weapon.getType().getId()) && weapon.getItemMeta().getDisplayName().contains(Main.healthsteal_name)) { //Making sure its a sword and has the right name
if (damager.getHealth() >= 19)
damager.setMaxHealth(40); //Allows the player to have up to two rows of hearts if they have max default health
damager.setHealth(damager.getHealth() + 0.5); //Adds half a heart to the damagers health
}
} else if (event.getEntity() instanceof Player) {
Player damaged = (Player) event.getEntity();
if (damaged.getHealth() <= 20)
damaged.setMaxHealth(20); //Removing the second row of hearts when it's not needed if they ever dip below 20 health
}
}
}
package me.slash9211;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Logger;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public static String healthsteal_name;
private File config_file;
private FileConfiguration config;
public static ArrayList<Integer> swordID = new ArrayList<>(Arrays.asList(267, 268, 272, 276, 283));
public void onEnable() {
/*
* Generates a configuration file to save the name of the 'Health Steal' sword for later use
*/
if (config_file == null)
config_file = new File(getDataFolder(), "healthsteal.yml");
config = YamlConfiguration.loadConfiguration(config_file);
if (!config.contains("settings.healthsteal_name")) {
config.set("settings.healthsteal_name", "Health Steal"); //Defaults to 'Health Steal'
saveConfig();
}
healthsteal_name = config.getString("settings.healthsteal_name");
getServer().getPluginManager().registerEvents(new HealthStealEvent(), this);
/*
* Prints out to the console the plugin has been enabled
*/
System.out.println("[HealthSteal] Enabled!");
}
/*
* Prints out to the console the plugin has been disabled
*/
public void onDisable() {
System.out.println("Disabled!");
}
/*
* Saves the plugins configuration file
*/
public void saveConfig() {
try {
config.save(config_file);
} catch (IOException ex) {
Logger.getLogger("Minecraft").severe("[Health Steal] Unable to Save config");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment