Files
src
build.gradle
Code
4 245
src/main/java/pl/kodari/amulets/AmuletPlugin.java
package pl.kodari.amulets;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
public final class AmuletPlugin extends JavaPlugin implements Listener {
private NamespacedKey amuletKey;
@Override
public void onEnable() {
saveDefaultConfig();
amuletKey = new NamespacedKey(this, "amulet");
getServer().getPluginManager().registerEvents(this, this);
getServer().getScheduler().runTaskTimer(this, this::applyEffects, 20L, 40L);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length > 0) {
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
if (!sender.hasPermission("amulety.reload")) {
sender.sendMessage(color("&cNie masz uprawnień."));
return true;
}
reloadConfig();
sender.sendMessage(color("&aPrzeładowano konfigurację amuletów."));
return true;
}
return false;
}
if (!(sender instanceof Player)) {
sender.sendMessage("Tylko gracz może otworzyć sklep.");
return true;
}
openShop((Player) sender);
return true;
}
private void openShop(Player player) {
ShopHolder holder = new ShopHolder();
Inventory inventory = Bukkit.createInventory(holder, 27, color(getConfig().getString("sklep.tytul", "&8Sklep z amuletami")));
holder.inventory = inventory;
ConfigurationSection amulets = getConfig().getConfigurationSection("amulety");
if (amulets != null) {
for (String id : amulets.getKeys(false)) {
ConfigurationSection section = amulets.getConfigurationSection(id);
if (section == null) continue;
int slot = section.getInt("slot", -1);
if (slot < 0 || slot >= inventory.getSize()) {
getLogger().warning("Nieprawidłowy slot amuletu: " + id);
continue;
}
ItemStack item = createAmulet(id, section, true);
if (item != null) inventory.setItem(slot, item);
}
}
player.openInventory(inventory);
}
private ItemStack createAmulet(String id, ConfigurationSection section, boolean shopIcon) {
ItemStack item = XMaterial.matchXMaterial(section.getString("material", "FEATHER"))
.map(XMaterial::parseItem).orElse(null);
if (item == null || item.getType().isAir()) {
getLogger().warning("Nieprawidłowy materiał amuletu: " + id);
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) return null;
meta.setDisplayName(color(section.getString("nazwa", id)));
List<String> lore = new ArrayList<>();
for (String line : section.getStringList("opis")) lore.add(color(line));
if (shopIcon) {
lore.add(color("&eCena: " + Math.max(0, section.getInt("cena")) + " " + getConfig().getString("sklep.nazwa-waluty", "szmaragdów")));
lore.add(color("&aKliknij, aby kupić!"));
}
meta.setLore(lore);
meta.getPersistentDataContainer().set(amuletKey, PersistentDataType.STRING, id);
item.setItemMeta(meta);
return item;
}
@EventHandler
public void onClick(InventoryClickEvent event) {
if (!(event.getView().getTopInventory().getHolder() instanceof ShopHolder)) return;
event.setCancelled(true);
if (!(event.getWhoClicked() instanceof Player) || event.getRawSlot() < 0
|| event.getRawSlot() >= event.getView().getTopInventory().getSize()) return;
ItemStack clicked = event.getCurrentItem();
if (clicked == null || !clicked.hasItemMeta()) return;
String id = clicked.getItemMeta().getPersistentDataContainer().get(amuletKey, PersistentDataType.STRING);
if (id == null) return;
ConfigurationSection section = getConfig().getConfigurationSection("amulety." + id);
if (section == null || section.getInt("slot", -1) != event.getRawSlot()) return;
Player player = (Player) event.getWhoClicked();
Material currency = XMaterial.matchXMaterial(getConfig().getString("sklep.waluta", "EMERALD"))
.map(XMaterial::parseMaterial).orElse(null);
ItemStack amulet = createAmulet(id, section, false);
if (currency == null || currency.isAir() || amulet == null) {
player.sendMessage(color("&cSklep jest niepoprawnie skonfigurowany."));
return;
}
int price = Math.max(0, section.getInt("cena"));
int available = 0;
for (ItemStack item : player.getInventory().getStorageContents()) {
if (item != null && item.getType() == currency && !item.hasItemMeta()) available += item.getAmount();
}
if (available < price) {
player.sendMessage(color("&cNie masz wystarczającej ilości waluty."));
return;
}
if (player.getInventory().firstEmpty() == -1) {
player.sendMessage(color("&cPotrzebujesz wolnego miejsca w ekwipunku."));
return;
}
int remaining = price;
ItemStack[] contents = player.getInventory().getStorageContents();
for (int i = 0; i < contents.length && remaining > 0; i++) {
ItemStack item = contents[i];
if (item == null || item.getType() != currency || item.hasItemMeta()) continue;
int taken = Math.min(remaining, item.getAmount());
remaining -= taken;
if (taken == item.getAmount()) contents[i] = null;
else item.setAmount(item.getAmount() - taken);
}
player.getInventory().setStorageContents(contents);
player.getInventory().addItem(amulet);
player.sendMessage(color("&aKupiono amulet! Trzymaj go w ekwipunku, aby działał."));
}
@EventHandler
public void onDrag(InventoryDragEvent event) {
if (event.getView().getTopInventory().getHolder() instanceof ShopHolder) event.setCancelled(true);
}
private void applyEffects() {
for (Player player : Bukkit.getOnlinePlayers()) {
for (ItemStack item : player.getInventory().getContents()) {
if (item == null || !item.hasItemMeta()) continue;
String id = item.getItemMeta().getPersistentDataContainer().get(amuletKey, PersistentDataType.STRING);
if (id == null) continue;
ConfigurationSection section = getConfig().getConfigurationSection("amulety." + id);
if (section == null) continue;
int amplifier = Math.max(0, Math.min(254, section.getInt("poziom", 1) - 1));
XPotion.matchXPotion(section.getString("efekt", "SPEED"))
.map(potion -> potion.buildPotionEffect(60, amplifier))
.ifPresent(player::addPotionEffect);
}
}
}
private String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
private static final class ShopHolder implements InventoryHolder {
private Inventory inventory;
@Override
public Inventory getInventory() {
return inventory;
}
}
}