ExcellentEnchants-spigot/Core/src/main/java/su/nightexpress/excellentenchants/enchantment/impl/armor/SaturationEnchant.java

84 lines
3.0 KiB
Java
Raw Normal View History

2024-02-05 00:26:03 +01:00
package su.nightexpress.excellentenchants.enchantment.impl.armor;
import org.bukkit.enchantments.EnchantmentTarget;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
2024-03-24 13:11:31 +01:00
import su.nightexpress.excellentenchants.ExcellentEnchantsPlugin;
import su.nightexpress.excellentenchants.api.Modifier;
import su.nightexpress.excellentenchants.api.enchantment.Rarity;
import su.nightexpress.excellentenchants.api.enchantment.data.PeriodicSettings;
2024-02-05 00:26:03 +01:00
import su.nightexpress.excellentenchants.api.enchantment.type.PassiveEnchant;
2024-03-24 13:11:31 +01:00
import su.nightexpress.excellentenchants.enchantment.data.AbstractEnchantmentData;
import su.nightexpress.excellentenchants.enchantment.data.PeriodSettingsImpl;
import su.nightexpress.nightcore.config.FileConfig;
import su.nightexpress.nightcore.util.NumberUtil;
2024-02-05 00:26:03 +01:00
2024-03-24 13:11:31 +01:00
import java.io.File;
2024-02-05 00:26:03 +01:00
2024-03-24 13:11:31 +01:00
import static su.nightexpress.excellentenchants.Placeholders.*;
public class SaturationEnchant extends AbstractEnchantmentData implements PassiveEnchant {
2024-02-05 00:26:03 +01:00
2024-03-24 13:11:31 +01:00
public static final String ID = "saturation";
2024-02-05 00:26:03 +01:00
2024-03-24 13:11:31 +01:00
private Modifier feedAmount;
private Modifier maxFoodLevel;
2024-02-05 00:26:03 +01:00
2024-03-24 13:11:31 +01:00
private PeriodSettingsImpl periodSettings;
2024-02-05 00:26:03 +01:00
2024-03-24 13:11:31 +01:00
public SaturationEnchant(@NotNull ExcellentEnchantsPlugin plugin, @NotNull File file) {
super(plugin, file);
this.setDescription("Restores " + GENERIC_AMOUNT + " food points every few seconds.");
this.setMaxLevel(3);
this.setRarity(Rarity.RARE);
2024-02-05 00:26:03 +01:00
}
@Override
2024-03-24 13:11:31 +01:00
protected void loadAdditional(@NotNull FileConfig config) {
this.periodSettings = PeriodSettingsImpl.create(config);
2024-02-05 00:26:03 +01:00
2024-03-24 13:11:31 +01:00
this.feedAmount = Modifier.read(config, "Settings.Saturation.Amount",
Modifier.add(0, 1, 1, 10),
2024-02-05 00:26:03 +01:00
"Amount of food points to restore.");
2024-03-24 13:11:31 +01:00
this.maxFoodLevel = Modifier.read(config, "Settings.Saturation.Max_Food_Level",
Modifier.add(20, 0, 0),
2024-02-05 00:26:03 +01:00
"Maximal player's food level for the enchantment to stop feeding them.");
2024-03-24 13:11:31 +01:00
this.addPlaceholder(GENERIC_AMOUNT, level -> NumberUtil.format(this.getFeedAmount(level)));
this.addPlaceholder(GENERIC_MAX, level -> NumberUtil.format(this.getMaxFoodLevel(level)));
2024-02-05 00:26:03 +01:00
}
@NotNull
@Override
2024-03-24 13:11:31 +01:00
public PeriodicSettings getPeriodSettings() {
return periodSettings;
2024-02-05 00:26:03 +01:00
}
@Override
@NotNull
public EnchantmentTarget getCategory() {
return EnchantmentTarget.ARMOR_HEAD;
}
2024-03-24 13:11:31 +01:00
public final int getFeedAmount(int level) {
return (int) this.feedAmount.getValue(level);
2024-02-05 00:26:03 +01:00
}
public final int getMaxFoodLevel(int level) {
2024-03-24 13:11:31 +01:00
return (int) this.maxFoodLevel.getValue(level);
2024-02-05 00:26:03 +01:00
}
@Override
public boolean onTrigger(@NotNull LivingEntity entity, @NotNull ItemStack item, int level) {
if (!(entity instanceof Player player)) return false;
if (player.getFoodLevel() >= this.getMaxFoodLevel(level)) return false;
2024-03-24 13:11:31 +01:00
int amount = this.getFeedAmount(level);
2024-02-05 00:26:03 +01:00
player.setFoodLevel(Math.min(20, player.getFoodLevel() + amount));
return true;
}
}