以下是一个使用Java编写的Bukkit 1.20.1的箱子保护功能的示例代码:

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);
    }
}

这个插件的功能是当玩家右键点击一个箱子时,如果该箱子没有被保护,则将其保护起来,只有拥有者可以访问。保护的方式是将箱子的类型更改为熔炉,并在其元数据中存储拥有者的UUID。保护持续10秒后自动取消,箱子恢复为普通的箱子。

用Java写一个Bukkit1201 箱子保护功能

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

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