Bukkit 1.20 箱子锁定插件:自动记录放置者ID并锁定机制
以下是用Java编写的示例代码,实现了在Bukkit 1.20中放置的箱子自动记录放置者ID到配置文件,并且在箱子锁定者与打开者ID不符时,将其变成大型箱子DoubleChest类型的功能。
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class ChestLockPlugin extends JavaPlugin implements Listener {
private Map<UUID, UUID> lockMap;
private FileConfiguration config;
@Override
public void onEnable() {
lockMap = new HashMap<>();
config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), 'locks.yml'));
if (!config.contains('locks')) {
config.createSection('locks');
}
Bukkit.getPluginManager().registerEvents(this, this);
}
@Override
public void onDisable() {
saveConfigFile();
}
private void saveConfigFile() {
try {
config.save(new File(getDataFolder(), 'locks.yml'));
} catch (IOException e) {
e.printStackTrace();
}
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
Block block = event.getBlock();
BlockState blockState = block.getState();
if (blockState instanceof Chest) {
Chest chest = (Chest) blockState;
UUID placerId = event.getPlayer().getUniqueId();
String chestId = block.getLocation().toString();
lockMap.put(placerId, placerId);
config.set('locks.' + chestId, placerId.toString());
saveConfigFile();
}
}
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
InventoryHolder holder = event.getInventory().getHolder();
if (holder instanceof Chest) {
Chest chest = (Chest) holder;
String chestId = chest.getLocation().toString();
String placerIdStr = config.getString('locks.' + chestId);
if (placerIdStr != null) {
UUID placerId = UUID.fromString(placerIdStr);
Player player = (Player) event.getPlayer();
if (!player.getUniqueId().equals(placerId)) {
Block adjacentChest = chest.getRelative(chest.getBlock().getFace(chest.getBlock().getRelative(0, 1, 0)));
BlockState adjacentChestState = adjacentChest.getState();
if (adjacentChestState instanceof Chest) {
Chest adjacentChest2 = (Chest) adjacentChestState;
if (adjacentChest2.getInventory().getType() == chest.getInventory().getType()) {
DoubleChest doubleChest = new DoubleChest(chest, adjacentChest2);
chest = doubleChest;
}
}
}
}
}
}
}
请注意,这只是一个示例代码,可能需要根据实际需求进行修改和适配。另外,该代码仅实现了将放置者ID记录到配置文件和在锁定者与打开者ID不符时变成DoubleChest类型的功能,其他细节(例如解锁、移除锁定等)需要根据具体需求进行进一步的开发。
原文地址: https://www.cveoy.top/t/topic/pYqk 著作权归作者所有。请勿转载和采集!