Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package RandomPersson.SimpleLifeLeech;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import com.mewin.WGRegionEvents.events.RegionEnterEvent;
import com.mewin.WGRegionEvents.events.RegionLeaveEvent;
import com.sk89q.worldguard.bukkit.WGBukkit;
import net.minecraft.server.v1_8_R3.EntityPlayer;
import net.minecraft.server.v1_8_R3.ItemStack;
import net.minecraft.server.v1_8_R3.NBTTagCompound;
public class SimpleLifeLeech extends JavaPlugin implements Listener, CommandExecutor {
public static final String _21 = "§";
protected String leechName = "LifeLeecher", pvpRegionKeyword = "pvp", noPVPregionKeyword = "spawn";
protected float additionalHealth = 20, lifeLeechAmount = 1;
protected boolean useTags = false, onlyPlayers = true, usePVPregion = false, useNoPVPregion = true, configLoaded = false, configReset = false;
///////////
// STUFF //
///////////
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
if (getConfig().getDouble("configVersion", 0.0d) < 1.3d) {
getConfig().set("configVersion", 1.3d);
getConfig().set("leechName", leechName);
getConfig().set("additionalHealth", additionalHealth);
getConfig().set("lifeLeechAmount", lifeLeechAmount);
getConfig().set("useTags", useTags);
getConfig().set("onlyPlayers", onlyPlayers);
getConfig().set("pvpRegionKeyword", pvpRegionKeyword);
getConfig().set("noPVPregionKeyword", noPVPregionKeyword);
getConfig().set("usePVPregion", usePVPregion);
getConfig().set("useNoPVPregion", useNoPVPregion);
saveConfig();
cout("&bSimpleLifeLeech found old config version. Config has been reset.");
configReset = configLoaded = true;
} else {
getConfig().options().copyDefaults(true);
saveConfig();
reloadVars();
}
}
protected boolean reloadVars() {
try {
leechName = colorize(getConfig().getString("leechName"));
additionalHealth = getConfig().getInt("additionalHealth");
lifeLeechAmount = getConfig().getInt("lifeLeechAmount");
useTags = getConfig().getBoolean("useTags");
onlyPlayers = getConfig().getBoolean("onlyPlayers");
pvpRegionKeyword = getConfig().getString("pvpRegionKeyword");
noPVPregionKeyword = getConfig().getString("noPVPregionKeyword");
usePVPregion = getConfig().getBoolean("usePVPregion");
useNoPVPregion = getConfig().getBoolean("useNoPVPregion");
configLoaded = true;
configReset = false;
return true;
} catch (Exception exc) {
exc.printStackTrace();
cout("&4SimpleLifeLeech could not load config! Using default values.");
}
return false;
}
public void showHelp(CommandSender sender) {
sender.sendMessage(colorize("&aCommand list: reload, apply."));
}
/////////////
// COMMAND //
/////////////
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!sender.hasPermission("sll.admin")) {
sender.sendMessage(colorize("&cYou don't have permission to use this command."));
return true;
}
if (args.length > 0) {
switch(args[0].toLowerCase()) {
case "reload":
reloadConfig();
if(reloadVars()) {
sender.sendMessage(colorize("&aConfig reloaded."));
} else {
sender.sendMessage(colorize("&4Could not load config! Using default values."));
}
break;
case "apply":
if (sender instanceof Player) {
Player p = (Player)sender;
if (p.getItemInHand() == null || p.getItemInHand().getType().equals(Material.AIR)) {
sender.sendMessage(colorize("&cYou don't have any items in your hand!"));
return true;
}
if (useTags) {
ItemStack nmsItem = CraftItemStack.asNMSCopy(p.getItemInHand());
if (!nmsItem.hasTag()) {
nmsItem.setTag(new NBTTagCompound());
}
nmsItem.getTag().setBoolean("lifeLeech", true);
p.setItemInHand(CraftItemStack.asBukkitCopy(nmsItem));
}
org.bukkit.inventory.ItemStack item = p.getItemInHand();
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(leechName);
item.setItemMeta(meta);
p.setItemInHand(item);
sender.sendMessage(colorize("&aLife leech effect has been applied to your item."));
} else {
sender.sendMessage(colorize("&cThis command can be used only from in-game."));
}
break;
default: {
showHelp(sender);
}
}
} else {
showHelp(sender);
}
return true;
}
////////////
// EVENTS //
////////////
@EventHandler
public void onPlayerAttacc(EntityDamageByEntityEvent e) {
if (e.getDamager() instanceof Player && (!onlyPlayers || e.getEntity() instanceof Player)) {
CraftPlayer damager = (CraftPlayer)e.getDamager();
if (usePVPregion && WGBukkit.getRegionManager(damager.getLocation().getWorld()).getApplicableRegions(damager.getLocation()).getRegions().stream().filter(r -> r.getId().toLowerCase().contains(pvpRegionKeyword)).count() == 0) {
return;
}
if (useNoPVPregion && WGBukkit.getRegionManager(damager.getLocation().getWorld()).getApplicableRegions(damager.getLocation()).getRegions().stream().filter(r -> r.getId().toLowerCase().contains(noPVPregionKeyword)).count() != 0) {
return;
}
if (damager.getItemInHand() != null && damager.getItemInHand().getItemMeta() != null && leechName.equals(damager.getItemInHand().getItemMeta().getDisplayName())) {
ItemStack nmsItem = CraftItemStack.asNMSCopy(damager.getItemInHand());
if (useTags && !nmsItem.getTag().getBoolean("lifeLeech")) {
return;
}
EntityPlayer ep = (damager).getHandle();
float amountHealed = ep.getHealth()+lifeLeechAmount > ep.getMaxHealth() ? ep.getMaxHealth()-ep.getHealth() : lifeLeechAmount;
ep.setHealth(ep.getHealth()+amountHealed);
float currentAdditionalHealth = ep.getAbsorptionHearts()+lifeLeechAmount-amountHealed;
if (currentAdditionalHealth < additionalHealth) {
ep.setAbsorptionHearts((float)Math.floor(currentAdditionalHealth));
//Math.floor because sometimes player gets damaged for non-integer value and absorption hearts don't show up properly then
} else {
ep.setAbsorptionHearts(Math.max(ep.getAbsorptionHearts(), additionalHealth));
}
}
}
}
@EventHandler
public void onLeaveRegion(RegionLeaveEvent e) {
if (usePVPregion && e.getRegion().getId().toLowerCase().contains(pvpRegionKeyword)) {
((CraftPlayer)e.getPlayer()).getHandle().setAbsorptionHearts(0.0f);
}
}
@EventHandler
public void onEnterRegion(RegionEnterEvent e) {
if (useNoPVPregion && e.getRegion().getId().toLowerCase().contains(noPVPregionKeyword)) {
((CraftPlayer)e.getPlayer()).getHandle().setAbsorptionHearts(0.0f);
}
}
@EventHandler(priority=EventPriority.LOWEST)
public void onPlayerJoin(PlayerJoinEvent e) {
if (!e.getPlayer().hasPermission("sll.admin")) {
return;
}
if (!configLoaded) {
e.getPlayer().sendMessage(colorize("&4SimpleLifeLeech could not load config! Using default values."));
}
if (configReset) {
e.getPlayer().sendMessage(colorize("&bSimpleLifeLeech found old config version. Config has been reset."));
}
}
///////////
// UTILS //
///////////
public static String colorize(String s) {
return s==null?"":s.replaceAll("&", _21);
}
public void cout(String s) {
getServer().getConsoleSender().sendMessage(colorize(s));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment