以下是一个示例代码,用于在Bukkit 1.20.1中实现SHIFT+右键箱子打开GUI,并显示箱子坐标:

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
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.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

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

public class ChestGUIPlugin extends JavaPlugin implements Listener {
    private Map<Location, Inventory> chestInventories;

    @Override
    public void onEnable() {
        chestInventories = new HashMap<>();
        Bukkit.getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (event.getAction() == Action.RIGHT_CLICK_BLOCK && player.isSneaking()) {
            Block clickedBlock = event.getClickedBlock();
            if (clickedBlock != null && clickedBlock.getType() == Material.CHEST) {
                Location chestLocation = clickedBlock.getLocation();
                Inventory chestInventory = getOrCreateChestInventory(chestLocation);
                player.openInventory(chestInventory);

                event.setCancelled(true);
            }
        }
    }

    private Inventory getOrCreateChestInventory(Location chestLocation) {
        if (chestInventories.containsKey(chestLocation)) {
            return chestInventories.get(chestLocation);
        }

        BlockState blockState = chestLocation.getBlock().getState();
        if (blockState instanceof Chest) {
            Chest chest = (Chest) blockState;
            Inventory chestInventory = Bukkit.createInventory(null, chest.getInventory().getSize(), ChatColor.BOLD + "Chest GUI");
            chestInventory.setContents(chest.getInventory().getContents());

            chestInventories.put(chestLocation, chestInventory);
            return chestInventory;
        }

        return null;
    }
}

这个插件会在玩家SHIFT+右键点击箱子时打开一个GUI,显示该箱子的内容。点击GUI内的箱子图标会在聊天栏中显示该箱子的坐标。

请注意,这只是一个简单的示例代码,可能需要根据你的实际需求进行修改和扩展。

用Java写一个Bukkit1201的SHIFT+右键箱子可以打开一个GUI 点击GUI内的箱子图标可以显示该箱子的坐标

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

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