Renata Sousa

Um sapato que faz você correr rápido

Kodari AI
KodariOct 29 at 7:43 pm 1
SpeedShoes
A Minecraft plugin that adds a special pair of shoes granting players a significant speed boost when worn. The plugin introduces a custom item, "Speed Shoes," which can be obtained through crafting or admin commands. When equipped in the boots slot, the player receives a configurable movement speed multiplier, enhancing their running capabilities. The plugin includes configurable speed values, duration-based effects, and visual particle effects to indicate the active speed boost. It also provides compatibility with other armor pieces and ensures the speed effect is removed when the shoes are unequipped. The plugin supports both Bukkit and Spigot servers, with easy configuration through a YAML file.
  1. Criar o plugin com o comando /speedshoe para entregar o sapato.

  2. Aplicar velocidade enquanto o sapato estiver equipado e restaurar o efeito anterior ao removê-lo.

Criado o plugin SpeedShoe. Use /speedshoe para receber o sapato; ao equipá-lo, você ganha velocidade. Ao removê-lo, o efeito anterior é restaurado.

Kodari can make mistakes. Review important code before using.
Files
src
Code
2 178
src/main/java/com/kodari/speedshoe/SpeedShoePlugin.java
package com.kodari.speedshoe;

import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class SpeedShoePlugin extends JavaPlugin implements Listener, CommandExecutor {
    private static final String SHOE_NAME = ChatColor.AQUA + "Sapato Veloz";
    private static final String SHOE_MARKER = ChatColor.GRAY + "Sapato especial";

    private final Set<UUID> activePlayers = new HashSet<>();
    private final Map<UUID, SavedEffect> savedEffects = new HashMap<>();
    private PotionEffect speedEffect;

    @Override
    public void onEnable() {
        speedEffect = XPotion.matchXPotion("SPEED")
                .map(potion -> potion.buildPotionEffect(40, 1))
                .orElse(null);
        if (speedEffect == null) {
            getLogger().severe("Could not load the speed potion effect.");
            getServer().getPluginManager().disablePlugin(this);
            return;
        }

        getCommand("speedshoe").setExecutor(this);
        getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getScheduler().runTaskTimer(this, this::updateShoes, 0L, 10L);
    }

    @Override
    public void onDisable() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            restoreEffect(player);
        }
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player player)) {
            sender.sendMessage("Apenas jogadores podem usar este comando.");
            return true;
        }
        if (!player.hasPermission("speedshoe.give")) {
            player.sendMessage(ChatColor.RED + "Você não tem permissão para usar este comando.");
            return true;
        }

        ItemStack shoe = createShoe();
        if (shoe == null) {
            player.sendMessage(ChatColor.RED + "Não foi possível criar o sapato neste servidor.");
            return true;
        }

        Map<Integer, ItemStack> leftovers = player.getInventory().addItem(shoe);
        leftovers.values().forEach(item -> player.getWorld().dropItemNaturally(player.getLocation(), item));
        player.sendMessage(ChatColor.GREEN + "Você recebeu o Sapato Veloz!");
        return true;
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        restoreEffect(event.getPlayer());
    }

    private void updateShoes() {
        for (Player player : Bukkit.getOnlinePlayers()) {
            UUID playerId = player.getUniqueId();
            if (isSpeedShoe(player.getInventory().getBoots())) {
                if (activePlayers.add(playerId)) {
                    savedEffects.put(playerId, new SavedEffect(
                            player.getPotionEffect(speedEffect.getType()), System.currentTimeMillis()));
                }

                PotionEffect current = player.getPotionEffect(speedEffect.getType());
                if (current == null || current.getAmplifier() < speedEffect.getAmplifier()
                        || current.getDuration() <= 20) {
                    player.addPotionEffect(speedEffect, true);
                }
            } else if (activePlayers.contains(playerId)) {
                restoreEffect(player);
            }
        }
    }

    private void restoreEffect(Player player) {
        UUID playerId = player.getUniqueId();
        activePlayers.remove(playerId);
        SavedEffect saved = savedEffects.remove(playerId);
        if (speedEffect == null) {
            return;
        }

        PotionEffect current = player.getPotionEffect(speedEffect.getType());
        if (current != null && current.getAmplifier() == speedEffect.getAmplifier()
                && current.getDuration() <= speedEffect.getDuration()) {
            player.removePotionEffect(speedEffect.getType());
        }

        if (saved != null && saved.effect != null) {
            int elapsedTicks = (int) ((System.currentTimeMillis() - saved.savedAtMillis) / 50L);
            int remainingTicks = saved.effect.getDuration() - elapsedTicks;
            if (remainingTicks > 0) {
                PotionEffect previous = saved.effect;
                player.addPotionEffect(new PotionEffect(previous.getType(), remainingTicks,
                        previous.getAmplifier(), previous.isAmbient()), true);
            }
        }
    }

    private ItemStack createShoe() {
        ItemStack shoe = XMaterial.matchXMaterial("LEATHER_BOOTS")
                .map(XMaterial::parseItem)
                .orElse(null);
        if (shoe == null) {
            return null;
        }

        ItemMeta meta = shoe.getItemMeta();
        if (meta == null) {
            return null;
        }
        meta.setDisplayName(SHOE_NAME);
        meta.setLore(java.util.Collections.singletonList(SHOE_MARKER));
        shoe.setItemMeta(meta);
        return shoe;
    }

    private boolean isSpeedShoe(ItemStack item) {
        if (item == null || item.getType().isAir() || !item.hasItemMeta()) {
            return false;
        }
        ItemMeta meta = item.getItemMeta();
        return meta != null && meta.hasDisplayName() && SHOE_NAME.equals(meta.getDisplayName())
                && meta.hasLore() && meta.getLore().contains(SHOE_MARKER);
    }

    private static final class SavedEffect {
        private final PotionEffect effect;
        private final long savedAtMillis;

        private SavedEffect(PotionEffect effect, long savedAtMillis) {
            this.effect = effect;
            this.savedAtMillis = savedAtMillis;
        }
    }
}