Files
src
build.gradle
manifest.kod
Code
11 590
src/main/java/com/mysteryhead/MysteryHead.java
package com.mysteryhead;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.profiles.builder.XSkull;
import com.cryptomorin.xseries.profiles.objects.Profileable;
import com.mysteryhead.commands.GiveCommand;
import com.mysteryhead.commands.ReloadCommand;
import com.mysteryhead.listeners.HeadUseListener;
import com.mysteryhead.managers.CooldownManager;
import com.mysteryhead.managers.MessageManager;
import com.tcoded.folialib.FoliaLib;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
@Getter
public class MysteryHead extends JavaPlugin {
private FoliaLib foliaLib;
private CooldownManager cooldownManager;
private MessageManager messageManager;
private final Random random = new Random();
private ItemStack mysteryHead;
private List<PotionEffect> possibleEffects;
private long cooldownSeconds;
private FileConfiguration messagesConfig;
@Override
public void onEnable() {
this.foliaLib = new FoliaLib(this);
this.cooldownManager = new CooldownManager();
this.messageManager = new MessageManager(this);
saveDefaultConfig();
File messagesFile = new File(getDataFolder(), "messages.yml");
if (!messagesFile.exists()) {
saveResource("messages.yml", false);
}
loadMessagesConfig();
loadConfiguration();
Bukkit.getPluginManager().registerEvents(new HeadUseListener(this), this);
ReloadCommand reloadCommand = new ReloadCommand(this);
getCommand("mysteryhead").setExecutor(reloadCommand);
GiveCommand giveCommand = new GiveCommand(this);
getCommand("givemysteryhead").setExecutor(giveCommand);
getCommand("givemysteryhead").setTabCompleter(giveCommand);
getLogger().info("MysteryHead has been enabled!");
}
@Override
public void onDisable() {
if (foliaLib != null) {
foliaLib.getScheduler().cancelAllTasks();
}
getLogger().info("MysteryHead has been disabled!");
}
public void loadMessagesConfig() {
File messagesFile = new File(getDataFolder(), "messages.yml");
this.messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
}
public FileConfiguration getMessagesConfig() {
return messagesConfig;
}
public void loadConfiguration() {
reloadConfig();
loadMessagesConfig();
String name = ChatColor.translateAlternateColorCodes('&', getConfig().getString("head.name", "&d&lMystery Head"));
List<String> loreConfig = getConfig().getStringList("head.lore");
List<String> lore = loreConfig.stream()
.map(line -> ChatColor.translateAlternateColorCodes('&', line))
.collect(Collectors.toList());
String texture = getConfig().getString("head.texture", "");
this.mysteryHead = createMysteryHead(name, lore, texture);
this.cooldownSeconds = getConfig().getLong("cooldown", 60);
this.possibleEffects = loadEffects();
getLogger().info("Configuration loaded with " + possibleEffects.size() + " possible effects");
}
private ItemStack createMysteryHead(String name, List<String> lore, String texture) {
ItemStack head;
if (texture != null && !texture.isEmpty()) {
head = XSkull.createItem().profile(Profileable.detect(texture)).apply();
} else {
head = XMaterial.PLAYER_HEAD.parseItem();
}
if (head != null) {
ItemMeta meta = head.getItemMeta();
if (meta != null) {
meta.setDisplayName(name);
meta.setLore(lore);
head.setItemMeta(meta);
}
}
return head;
}
private List<PotionEffect> loadEffects() {
List<PotionEffect> effects = new ArrayList<>();
List<String> effectStrings = getConfig().getStringList("effects");
for (String effectStr : effectStrings) {
String[] parts = effectStr.split(":");
if (parts.length >= 3) {
String effectName = parts[0];
int duration = Integer.parseInt(parts[1]);
int amplifier = Integer.parseInt(parts[2]);
XPotion.matchXPotion(effectName)
.map(xp -> xp.buildPotionEffect(duration, amplifier))
.ifPresent(effects::add);
}
}
return effects;
}
public boolean isMysteryHead(ItemStack item) {
if (item == null || mysteryHead == null) return false;
if (!item.hasItemMeta() || !mysteryHead.hasItemMeta()) return false;
ItemMeta itemMeta = item.getItemMeta();
ItemMeta headMeta = mysteryHead.getItemMeta();
if (!itemMeta.hasDisplayName() || !headMeta.hasDisplayName()) return false;
return itemMeta.getDisplayName().equals(headMeta.getDisplayName());
}
public PotionEffect getRandomEffect() {
if (possibleEffects.isEmpty()) return null;
return possibleEffects.get(random.nextInt(possibleEffects.size()));
}
}