Bukkit 1.20.1 箱子保护插件 (Java)

这篇文章提供了一个简单的Bukkit 1.20.1箱子保护插件的Java代码示例。此插件允许玩家通过右键点击来保护他们的箱子。保护机制是将箱子临时转换为熔炉,并在一段时间后自动解除保护。

代码

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

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

public class ChestProtectionPlugin extends JavaPlugin implements Listener {

    private Map<UUID, Block> protectedChests;

    @Override
    public void onEnable() {
        protectedChests = new HashMap<>();
        Bukkit.getPluginManager().registerEvents(this, this);
        getLogger().info('ChestProtectionPlugin has been enabled!');
    }

    @Override
    public void onDisable() {
        getLogger().info('ChestProtectionPlugin has been disabled!');
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
            Block clickedBlock = event.getClickedBlock();
            if (clickedBlock.getType() == Material.CHEST) {
                if (isProtectedChest(clickedBlock)) {
                    if (!isChestOwner(player, clickedBlock)) {
                        event.setCancelled(true);
                        player.sendMessage('This chest is protected!');
                    }
                } else {
                    protectChest(player, clickedBlock);
                }
            }
        }
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        UUID playerId = player.getUniqueId();
        if (protectedChests.containsKey(playerId)) {
            Block chestBlock = protectedChests.get(playerId);
            chestBlock.setType(Material.CHEST);
            protectedChests.remove(playerId);
        }
    }

    private boolean isProtectedChest(Block chestBlock) {
        return chestBlock.getType() == Material.FURNACE; // 以熔炉作为保护标记
    }

    private boolean isChestOwner(Player player, Block chestBlock) {
        UUID chestOwnerId = chestBlock.getMetadata('owner').get(0).asUUID();
        return chestOwnerId.equals(player.getUniqueId());
    }

    private void protectChest(Player player, Block chestBlock) {
        chestBlock.setType(Material.FURNACE); // 将熔炉作为保护标记
        chestBlock.setMetadata('owner', new FixedMetadataValue(this, player.getUniqueId()));
        protectedChests.put(player.getUniqueId(), chestBlock);
        player.sendMessage('Chest protected!');
        // 10秒后取消保护
        new BukkitRunnable() {
            @Override
            public void run() {
                if (isProtectedChest(chestBlock) && isChestOwner(player, chestBlock)) {
                    chestBlock.setType(Material.CHEST);
                    protectedChests.remove(player.getUniqueId());
                    player.sendMessage('Chest protection expired!');
                }
            }
        }.runTaskLater(this, 200L);
    }
}

解释

这个插件使用以下逻辑来保护箱子:

  1. 监听玩家交互事件: 当玩家右键点击一个箱子时,插件会检查该箱子是否已经被保护。
  2. 使用熔炉作为标记: 如果箱子未被保护,插件会将其类型更改为熔炉,并使用箱子元数据存储箱子拥有者的UUID。
  3. 验证拥有者: 如果箱子已被保护,插件会检查当前玩家是否是箱子的拥有者。
  4. 自动解除保护: 保护状态会在10秒后自动解除,箱子类型会恢复为普通的箱子。

注意

  • 这个插件只是一个简单的示例,你可以根据自己的需要进行修改和扩展。
  • 在实际使用中,建议使用更安全的保护机制,例如使用数据库来存储箱子拥有者信息。
  • 请确保你的插件与你服务器的Bukkit版本兼容。
Bukkit 1.20.1 箱子保护插件 (Java)

原文地址: http://www.cveoy.top/t/topic/fR0e 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录