Files
Advenskalender/src/main/java/de/mviper/adventskalender/christmas/CookieManager.java
T
2026-11-13 17:46:26 +00:00

82 lines
3.6 KiB
Java

package de.mviper.adventskalender.christmas;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/** Plaetzchen und Lebkuchen: Essbare Items mit Traenke-Effekten. */
public final class CookieManager {
private final ChristmasManager mgr;
private final NamespacedKey key;
CookieManager(ChristmasManager mgr) {
this.mgr = mgr;
this.key = new NamespacedKey(mgr.plugin(), "cookie_id");
}
public Set<String> ids() {
ConfigurationSection s = mgr.cfg().getConfigurationSection("cookies");
return s == null ? new LinkedHashSet<>() : s.getKeys(false);
}
public ItemStack create(String id, int amount) {
ConfigurationSection s = mgr.cfg().getConfigurationSection("cookies." + id);
if (s == null) return null;
Material m = Material.matchMaterial(s.getString("material", "COOKIE"));
if (m == null || !m.isItem() || !m.isEdible()) m = Material.COOKIE;
ItemStack it = new ItemStack(m, Math.max(1, amount));
ItemMeta meta = it.getItemMeta();
if (meta == null) return it;
meta.setDisplayName(ChristmasManager.color(s.getString("name", "&6" + id)));
List<String> lore = new ArrayList<>();
for (String l : s.getStringList("lore")) lore.add(ChristmasManager.color(l));
meta.setLore(lore);
meta.setEnchantmentGlintOverride(true);
meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, id);
it.setItemMeta(meta);
return it;
}
public String idOf(ItemStack it) {
if (it == null || !it.hasItemMeta()) return null;
return it.getItemMeta().getPersistentDataContainer().get(key, PersistentDataType.STRING);
}
/** Wendet die Effekte eines gegessenen Plaetzchens an. */
public void eat(Player p, String id) {
if (mgr.cfg().getBoolean("cookies." + id + ".steam", false)) {
var at = p.getEyeLocation().add(p.getLocation().getDirection().multiply(0.4));
p.getWorld().spawnParticle(org.bukkit.Particle.CLOUD, at, 14, 0.25, 0.2, 0.25, 0.02);
p.getWorld().spawnParticle(org.bukkit.Particle.HEART, at.clone().add(0, 0.4, 0), 3, 0.3, 0.2, 0.3, 0);
p.getWorld().playSound(p.getLocation(), org.bukkit.Sound.BLOCK_BREWING_STAND_BREW, 0.6f, 1.4f);
}
for (String spec : mgr.cfg().getStringList("cookies." + id + ".effects")) {
String[] parts = spec.split(":");
PotionEffectType type = effect(parts[0].trim());
if (type == null) continue;
int seconds = parts.length > 1 ? ChristmasManager.toInt(parts[1], 30) : 30;
int amp = parts.length > 2 ? ChristmasManager.toInt(parts[2], 0) : 0;
p.addPotionEffect(new PotionEffect(type, seconds * 20, Math.max(0, amp)));
}
}
@SuppressWarnings("deprecation")
private static PotionEffectType effect(String name) {
String n = name.toLowerCase();
PotionEffectType t = Registry.EFFECT.get(NamespacedKey.minecraft(n));
return t != null ? t : PotionEffectType.getByName(name.toUpperCase());
}
}