Hey, I am trying to develope a chestlock command. But I don't know how to check the random chestid. So I can't cancel if another player who didn't locked the chest trys to open it. Thank you for response! Sincerly, Rillekk ChestLock Command Code: package de.rillekk.server.commands; import java.io.File; import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import io.netty.util.internal.ThreadLocalRandom; public class ChestLockCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { Player player = (Player) sender; File file = new File("plugins/projekt/chestlocks", "chestlocks.yml"); FileConfiguration cfg = YamlConfiguration.loadConfiguration(file); String world = player.getWorld().getName(); Block block = player.getTargetBlock(null, 10); int zahl = ThreadLocalRandom.current().nextInt(1, 10000000 + 1); if(command.getName().equalsIgnoreCase("cl")) { if(block.getType().equals(Material.CHEST)) { double x = block.getLocation().getX(); double y = block.getLocation().getY(); double z = block.getLocation().getZ(); cfg.set("chestlocks." + player.getName() + ".chestid." + zahl + ".world", world); cfg.set("chestlocks." + player.getName() + ".chestid." + zahl + ".x", x); cfg.set("chestlocks." + player.getName() + ".chestid." + zahl + ".y", y); cfg.set("chestlocks." + player.getName() + ".chestid." + zahl + ".z", z); try { cfg.save(file); player.sendMessage("§8[§bRille§8] §7Du hast erfolgreich eine Chest gelockt!"); } catch (Exception e) { e.printStackTrace(); } } else { player.sendMessage("§8[§bRille§8] §7Du kannst nur Chests locken!"); } } return false; } } Chestlock Listener Code: package de.rillekk.server.listener; import java.io.File; import org.bukkit.Material; import org.bukkit.block.Block; 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.Action; import org.bukkit.event.player.PlayerInteractEvent; public class ChestLockListener implements Listener { @EventHandler public void onChestLock(PlayerInteractEvent event) { Player player = event.getPlayer(); File file = new File("plugins/projekt/chestlocks", "chestlocks.yml"); FileConfiguration cfg = YamlConfiguration.loadConfiguration(file); Block block1 = event.getClickedBlock(); if(event.getAction() == Action.RIGHT_CLICK_BLOCK) { if(block1.getType() == Material.CHEST) { if(!cfg.contains("chestlocks." + player.getName())) { player.closeInventory(); event.setCancelled(true); player.sendMessage("§8[§bRille§8] §7Diese Chest gehört nicht dir!"); } } } } }
You would loop the configuration section and check if the item's location matches the one being processed. But I wouldn't do that, I'll explain after I cover a few other things. First, you should be checking sender to ensure it is a Player before casting it thus. Second, don't keep loading the file like that. Create a configuration object in a scope accessible to all the classes and load that at startup then work from that, being sure to flush to disk periodically. Though if your server is not much used this isn't that critical. You also should be using UUID instead of player's name, as names can change. Also instead of a random number for chest ID which could conflict, I'd use a geo-reference. Now the reason for that also is to save you from having to loop eeeeevvvverrything. The way you do it now, you need to parse every single player to check if they locked the chest being checked. Using a geo reference reduces that to one simple query. So I would do my config like: Code: chestlocks: world: -109_67_2918: fe5303f8-0716-42cb-ade3-0a6023be87d9 772_49_1293: fe5303f8-0716-42cb-ade3-0a6023be87d9 world_nether: 1233_80_559: fe5303f8-0716-42cb-ade3-0a6023be87d9 So then when you want to see if a chest has been claimed, you just do like Code: String chestOwner = cfg.getString(getBlockPath(block1)); if (chestOwner == null) return; player.sendMessage("This is locked, piss off!"); } private String getBlockPath(Block b) { String returnValue = "chestlocks." + b.getWorld().getName().toLowerCase() + "."; returnValue += b.getLocation().getBlockX() + "_"; returnValue += b.getLocation().getBlockY() + "_"; returnValue += b.getLocation().getBlockZ; return returnValue; }