Dh Dhdhd

Привет сделай плагин RageBaff.jar paper 1.21.5 надо сделать так чтобы если у игрока 2 одинаковых зелья или больше то он их мог кинуть под себя и когда он подобрал то они стали как одно зелье название поменялось (фиолетовым) Многоразовое зелье а в описании (розовым) кол-во зелек тоесть Осталось 2 Зелья или больше а когда человек выпил все зельч то оно пропало и если было 3 зелья игрок выпил 1 то стало 2 в описании (главное чттбы описание прошлое удалялось и новое появлялось с новым кол-во)

Kodari AI
KodariNov 7 at 11:45 pm 1
RageBaff
RageBaff is a Paper 1.21.5 Minecraft plugin that allows players to merge identical potions into a single stackable item. When a player holds two or more of the same potion and right-clicks, the potions combine into one item named "Многоразовое зелье" in purple, with lore showing the remaining count in pink (e.g., "Осталось 2 Зелья"). When a player drinks the merged potion, the count decreases by one, the old lore is removed and replaced with the new count, and when the count reaches zero the item disappears. The plugin handles all potion types and ensures lore updates correctly after each use.

Создам RageBaff для Paper 1.21.5: одинаковые зелья объединятся при подборе в многоразовое, а после каждого выпивания обновится остаток в описании.

Готово: одинаковые зелья объединяются при подборе в одно «Многоразовое зелье». После каждого выпивания количество обновляется; последнее зелье можно выпить, после чего предмет исчезнет. Сборка настроена на имя RageBaff.jar.

Чтобы скачать файл: нажмите Compile (на телефоне: ⋮ → Compile Project), затем Compile в открывшемся окне и Download после успешной сборки.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 181
src/main/java/com/ragebaff/RageBaffPlugin.java
package com.ragebaff;

import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
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 RageBaffPlugin extends JavaPlugin implements Listener {
    private NamespacedKey chargesKey;

    @Override
    public void onEnable() {
        chargesKey = new NamespacedKey(this, "potion_charges");
        getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onPotionPickup(EntityPickupItemEvent event) {
        if (!(event.getEntity() instanceof Player player)) {
            return;
        }

        Item pickedItem = event.getItem();
        ItemStack pickedStack = pickedItem.getItemStack();
        if (!isPotion(pickedStack)) {
            return;
        }

        List<Item> matchingItems = new ArrayList<>();
        int totalCharges = 0;
        for (Entity nearby : pickedItem.getWorld().getNearbyEntities(pickedItem.getLocation(), 1.25, 1.25, 1.25)) {
            if (!(nearby instanceof Item nearbyItem) || !nearbyItem.isValid()) {
                continue;
            }

            ItemStack nearbyStack = nearbyItem.getItemStack();
            if (!samePotion(pickedStack, nearbyStack)) {
                continue;
            }

            matchingItems.add(nearbyItem);
            totalCharges += getCharges(nearbyStack) * nearbyStack.getAmount();
        }

        if (totalCharges < 2) {
            return;
        }

        event.setCancelled(true);
        ItemStack reusablePotion = pickedStack.clone();
        reusablePotion.setAmount(1);
        setCharges(reusablePotion, totalCharges);
        matchingItems.forEach(Item::remove);

        for (ItemStack leftover : player.getInventory().addItem(reusablePotion).values()) {
            player.getWorld().dropItemNaturally(player.getLocation(), leftover);
        }
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onPotionConsume(PlayerItemConsumeEvent event) {
        ItemStack consumed = event.getItem();
        int charges = getCharges(consumed);
        if (charges <= 1) {
            return;
        }

        ItemStack remainingPotion = consumed.clone();
        remainingPotion.setAmount(1);
        setCharges(remainingPotion, charges - 1);

        Player player = event.getPlayer();
        getServer().getScheduler().runTask(this, () -> {
            for (ItemStack leftover : player.getInventory().addItem(remainingPotion).values()) {
                player.getWorld().dropItemNaturally(player.getLocation(), leftover);
            }
        });
    }

    private boolean isPotion(ItemStack item) {
        return item != null && item.getType().name().equals("POTION");
    }

    private boolean samePotion(ItemStack first, ItemStack second) {
        if (!isPotion(first) || !isPotion(second)) {
            return false;
        }

        ItemStack firstComparable = comparablePotion(first);
        ItemStack secondComparable = comparablePotion(second);
        return firstComparable.isSimilar(secondComparable);
    }

    private ItemStack comparablePotion(ItemStack potion) {
        ItemStack comparable = potion.clone();
        comparable.setAmount(1);
        ItemMeta meta = comparable.getItemMeta();
        meta.setDisplayName(null);
        meta.setLore(null);
        meta.getPersistentDataContainer().remove(chargesKey);
        comparable.setItemMeta(meta);
        return comparable;
    }

    private int getCharges(ItemStack potion) {
        if (potion == null || !potion.hasItemMeta()) {
            return 1;
        }

        Integer charges = potion.getItemMeta().getPersistentDataContainer().get(chargesKey, PersistentDataType.INTEGER);
        return charges == null ? 1 : Math.max(1, charges);
    }

    private void setCharges(ItemStack potion, int charges) {
        ItemMeta meta = potion.getItemMeta();
        meta.setDisplayName(ChatColor.DARK_PURPLE + "Многоразовое зелье");
        meta.setLore(List.of(ChatColor.LIGHT_PURPLE + "Осталось " + charges + " " + potionWord(charges)));
        meta.getPersistentDataContainer().set(chargesKey, PersistentDataType.INTEGER, charges);
        potion.setItemMeta(meta);
    }

    private String potionWord(int count) {
        int lastTwoDigits = count % 100;
        if (lastTwoDigits >= 11 && lastTwoDigits <= 14) {
            return "зелий";
        }

        return switch (count % 10) {
            case 1 -> "зелье";
            case 2, 3, 4 -> "зелья";
            default -> "зелий";
        };
    }
}