Files
src
build.gradle
manifest.kod
settings.gradle
Code
7 370
src/main/java/com/kodari/lifestealworld/LifestealWorldPlugin.java
package com.kodari.lifestealworld;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import com.cryptomorin.xseries.XPotion;
public final class LifestealWorldPlugin extends JavaPlugin {
private NamespacedKey heartItemKey;
@Override
public void onEnable() {
saveDefaultConfig();
heartItemKey = new NamespacedKey(this, "heart_item");
getServer().getPluginManager().registerEvents(new LifestealMechanicsListener(this), this);
registerMaceRecipe();
registerHeartRecipe();
registerGoldenAppleRecipe();
registerStrengthPotionRecipe();
}
private void registerMaceRecipe() {
ItemStack mace = item("MACE");
Material heavyCore = material("HEAVY_CORE");
Material breezeRod = material("BREEZE_ROD");
if (mace == null || heavyCore == null || breezeRod == null) {
getLogger().warning("The mace recipe could not be registered because this server does not provide mace materials.");
return;
}
NamespacedKey key = new NamespacedKey(this, "lifesteal_mace");
getServer().removeRecipe(key);
ShapedRecipe recipe = new ShapedRecipe(key, mace);
if (getConfig().getBoolean("require-dragon-egg-for-mace", true)) {
Material dragonEgg = material("DRAGON_EGG");
if (dragonEgg == null) {
getLogger().warning("The mace recipe requires a dragon egg, but the material is unavailable.");
return;
}
recipe.shape("E", "H", "B");
recipe.setIngredient('E', dragonEgg);
} else {
recipe.shape("H", "B");
}
recipe.setIngredient('H', heavyCore);
recipe.setIngredient('B', breezeRod);
getServer().addRecipe(recipe);
}
private void registerHeartRecipe() {
ItemStack heart = createHeartItem();
Material goldBlock = material("GOLD_BLOCK");
Material trialKey = material("TRIAL_KEY");
Material ghastTear = material("GHAST_TEAR");
if (heart == null || goldBlock == null || trialKey == null || ghastTear == null) {
getLogger().warning("The heart recipe could not be registered because a required material is unavailable.");
return;
}
NamespacedKey key = new NamespacedKey(this, "lifesteal_heart");
getServer().removeRecipe(key);
ShapedRecipe recipe = new ShapedRecipe(key, heart);
recipe.shape("GKG", "KHK", "GKG");
recipe.setIngredient('G', goldBlock);
recipe.setIngredient('K', trialKey);
recipe.setIngredient('H', ghastTear);
getServer().addRecipe(recipe);
}
private void registerGoldenAppleRecipe() {
getServer().removeRecipe(new NamespacedKey(this, "lifesteal_enchanted_golden_apple"));
ItemStack goldenApple = item("GOLDEN_APPLE");
Material gold = material("GOLD_INGOT");
Material apple = material("APPLE");
if (goldenApple == null || gold == null || apple == null) {
getLogger().warning("The golden apple recipe could not be registered because a required material is unavailable.");
return;
}
NamespacedKey key = new NamespacedKey(this, "lifesteal_golden_apple");
getServer().removeRecipe(key);
ShapedRecipe recipe = new ShapedRecipe(key, goldenApple);
recipe.shape("G G", " A ", "G G");
recipe.setIngredient('G', gold);
recipe.setIngredient('A', apple);
getServer().addRecipe(recipe);
}
private void registerStrengthPotionRecipe() {
ItemStack potion = createStrengthPotion();
Material blazePowder = material("BLAZE_POWDER");
Material redstone = material("REDSTONE");
Material basePotion = material("POTION");
if (potion == null || blazePowder == null || redstone == null || basePotion == null) {
getLogger().warning("The Strength potion recipe could not be registered because a required material is unavailable.");
return;
}
NamespacedKey key = new NamespacedKey(this, "lifesteal_strength_two");
getServer().removeRecipe(key);
ShapedRecipe recipe = new ShapedRecipe(key, potion);
recipe.shape("BRB", "RPR", "BRB");
recipe.setIngredient('B', blazePowder);
recipe.setIngredient('R', redstone);
recipe.setIngredient('P', basePotion);
getServer().addRecipe(recipe);
}
ItemStack createHeartItem() {
ItemStack heart = item("NETHER_STAR");
if (heart == null) {
return null;
}
ItemMeta meta = heart.getItemMeta();
if (meta == null) {
return heart;
}
meta.setDisplayName("§cHeart");
meta.getPersistentDataContainer().set(heartItemKey, PersistentDataType.BYTE, (byte) 1);
heart.setItemMeta(meta);
return heart;
}
ItemStack createStrengthPotion() {
ItemStack potion = item("POTION");
if (potion == null || !(potion.getItemMeta() instanceof PotionMeta meta)) {
return null;
}
int duration = Math.max(1, getConfig().getInt("strength-potion.duration-seconds", 480)) * 20;
int amplifier = Math.max(0, getConfig().getInt("strength-potion.amplifier", 1));
XPotion.matchXPotion("STRENGTH")
.map(effect -> effect.buildPotionEffect(duration, amplifier))
.ifPresent(effect -> meta.addCustomEffect(effect, true));
meta.setDisplayName("§cStrength II (8:00)");
potion.setItemMeta(meta);
return potion;
}
boolean isHeartItem(ItemStack item) {
if (item == null || !item.hasItemMeta() || heartItemKey == null) {
return false;
}
Byte marker = item.getItemMeta().getPersistentDataContainer().get(heartItemKey, PersistentDataType.BYTE);
return marker != null && marker == (byte) 1;
}
boolean changeMaxHealth(org.bukkit.entity.Player player, double hearts) {
return com.cryptomorin.xseries.XAttribute.of("max_health").map(attribute -> {
org.bukkit.attribute.AttributeInstance instance = player.getAttribute(attribute.get());
if (instance == null) {
return false;
}
double minimum = Math.max(1, getConfig().getInt("lifesteal.min-hearts", 1)) * 2.0;
double maximum = Math.max(minimum, getConfig().getInt("lifesteal.max-hearts", 20) * 2.0);
double next = Math.max(minimum, Math.min(maximum, instance.getBaseValue() + hearts * 2.0));
if (Double.compare(instance.getBaseValue(), next) == 0) {
return false;
}
instance.setBaseValue(next);
if (player.getHealth() > next) {
player.setHealth(next);
}
return true;
}).orElse(false);
}
private ItemStack item(String name) {
return XMaterial.matchXMaterial(name).map(XMaterial::parseItem).orElse(null);
}
private Material material(String name) {
return XMaterial.matchXMaterial(name).map(XMaterial::parseMaterial).orElse(null);
}
}