Bukkit 1.20 CMI 玩家Shift+右键箱子显示HolographicDisplays文字
这是一个使用Java编写的Bukkit插件,可以实现玩家Shift+右键箱子时,在该箱子上方显示HolographicDisplays文字。当两个箱子合并后,文字将只显示一条,同时支持多个箱子。
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.BlockFace;
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.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import com.gmail.filoghost.holographicdisplays.api.Hologram;
import com.gmail.filoghost.holographicdisplays.api.HologramsAPI;
public class ChestHologramPlugin extends JavaPlugin implements Listener {
private Plugin holographicDisplaysPlugin;
@Override
public void onEnable() {
if (!setupHolographicDisplays()) {
getLogger().severe('HolographicDisplays plugin not found!');
getServer().getPluginManager().disablePlugin(this);
return;
}
getServer().getPluginManager().registerEvents(this, this);
}
private boolean setupHolographicDisplays() {
holographicDisplaysPlugin = getServer().getPluginManager().getPlugin('HolographicDisplays');
return holographicDisplaysPlugin != null;
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
Player player = event.getPlayer();
Action action = event.getAction();
if (action == Action.RIGHT_CLICK_BLOCK && player.isSneaking()) {
Block clickedBlock = event.getClickedBlock();
if (clickedBlock != null && clickedBlock.getType() == Material.CHEST) {
Block aboveBlock = clickedBlock.getRelative(BlockFace.UP);
if (aboveBlock.getType() == Material.AIR) {
// Create a new hologram above the chest
Hologram hologram = HologramsAPI.createHologram(this, aboveBlock.getLocation().add(0.5, 1.0, 0.5));
hologram.appendTextLine(ChatColor.GREEN + 'Chest contents');
// Store the hologram in the chest's metadata
clickedBlock.setMetadata('hologram', new FixedMetadataValue(this, hologram));
} else if (aboveBlock.getType() == Material.CHEST) {
// Get the hologram from the first chest
Hologram hologram = null;
if (clickedBlock.hasMetadata('hologram')) {
hologram = (Hologram) clickedBlock.getMetadata('hologram').get(0).value();
}
// Get the hologram from the second chest
if (aboveBlock.hasMetadata('hologram')) {
Hologram aboveHologram = (Hologram) aboveBlock.getMetadata('hologram').get(0).value();
if (hologram != null) {
// Merge the holograms
for (String line : aboveHologram.getLines()) {
hologram.appendTextLine(line);
}
// Remove the hologram from the second chest
aboveHologram.delete();
} else {
// Move the hologram to the second chest
aboveHologram.teleport(aboveBlock.getLocation().add(0.5, 1.0, 0.5));
aboveBlock.removeMetadata('hologram', this);
clickedBlock.setMetadata('hologram', new FixedMetadataValue(this, aboveHologram));
}
}
}
}
}
}
}
请确保已经安装了HolographicDisplays插件,并将该插件放置在Bukkit服务器的插件文件夹中。
原文地址: https://www.cveoy.top/t/topic/fRmf 著作权归作者所有。请勿转载和采集!