How can I have it so data wont be gone from the plugin when i restart the server

Discussion in 'Plugin Development' started by zoren3105, Jan 3, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    zoren3105

    Hello, I am currently developing a land claim and teams plugin for a server and I figured out the land claim part to the plugin and it does work the only problem I found was that if there are any chunks claimed on the server and the server restarts the claims will disappear and the would-be counted as unclaimed is there a way I can have it so that the chunks and UUIDs of the players save on to some sort of document that the server access on the server starts up then that communicates if a chunk is claimed or not?

    this is the code for the main file
    Code:
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.HashMap;
    import java.util.Objects;
    import java.util.UUID;
    
    public class ZeradentSMP extends JavaPlugin {
    
    
        private HashMap<String, UUID> chunks;
    
    
    
    
        @Override
        public void onEnable() {
            this.chunks = new HashMap<>();
            Objects.requireNonNull(getCommand("claim")).setExecutor(new ClaimCommand(this));
            getServer().getPluginManager().registerEvents(new PreventionListener(this), this);
            Objects.requireNonNull(getCommand("unclaim")).setExecutor(new UnclaimCommand(this));
    
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }
    
        public void addChunk(String chunk, UUID owner){
            chunks.put(chunk, owner);
        }
    
        public boolean isChunk(String chunk){
            return chunks.containsKey(chunk);
        }
    
        public UUID getOwner(String chunk){
            return chunks.get(chunk);
        }
    
        public void removeChunk(String chunk, UUID owner) {chunks.remove(chunk, owner);}
        }
    
    This is the code for the claim command

    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.Chunk;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import java.util.Arrays;
    
    public class ClaimCommand implements CommandExecutor {
    
        private final ZeradentSMP plugin;
    
        public ClaimCommand(ZeradentSMP plugin) {
            this.plugin = plugin;
        }
    
        String[] leaders = {"IrathTEG", "Zeradentt", "kashiikiwii", "CryptenGaming_Tv", "aridonnie", "TheMoBrosGaming", "zoren3105"};
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args){
            if(sender instanceof Player){
    
                Player player = (Player) sender;
    
                Chunk chunk = player.getLocation().getChunk();
    
                String name = player.getName();
    
                String chunkID = chunk.getX() + "." + chunk.getZ();
                if (Arrays.asList(leaders).contains(name)) {
                    if (plugin.isChunk(chunkID)) {
                        sender.sendMessage(ChatColor.RED + "Chunk Is Already Claimed");
                    } else {
                        plugin.addChunk(chunkID, player.getUniqueId());
                        sender.sendMessage(ChatColor.GREEN + "Claimed Succesfuly");
                    }
                }
                else {
                    sender.sendMessage(ChatColor.RED + "You Are Not A Team Leader");
                }
            }
            return true;
        }
    
    
    }
    This is the code for the prevention listener

    Code:
    import org.bukkit.Chunk;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class PreventionListener implements Listener {
    
        private final ZeradentSMP plugin;
    
        public PreventionListener(ZeradentSMP plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void onInteract(PlayerInteractEvent event){
            if(event.getClickedBlock() != null){
                Chunk chunk = event.getClickedBlock().getChunk();
                String chunkID = chunk.getX() + "." + chunk.getZ();
    
                if(plugin.isChunk(chunkID)){
                    Player player = event.getPlayer();
    
                    if(!plugin.getOwner(chunkID).equals(player.getUniqueId())){
                        if(!player.isOp()){
                            event.setCancelled(true);
                            player.sendMessage("you are not the owner");
                        }
                    }
                }
            }
        }
    }
     
  2. Offline

    Strahan

    Just write the map contents to the config at onDisable, and repopulate at onEnable. You can loop it by getting its entryset. e.g.
    Code:
    for (Map.Entry<String, UUID> data : chunks.entrySet()) {
      getConfig().set("claimedchunks." + data.getKey(), data.getValue().toString());
    }
    saveConfig();
    You would need to change the chunk ID delimiter from period to like underscore though, as period would screw with the YAML. Then reading, you just write back into the map:
    Code:
    ConfigurationSection cfg = getConfig().getConfigurationSection("claimedchunks");
    if (cfg == null) return;
    
    chunks.clear();
    for (String chunkID : cfg.getKeys(false)) {
      chunks.put(chunkID, UUID.fromString(cfg.getString(chunkID)));
    }
    Also in your declaration, you may as well init it at time of instantiation, and you should be using Map not HashMap per SOLID design:
    Code:
    private Map<String, UUID> chunks = new HashMap<>();
    Lastly, if you still want to init the map in onEnable, there is no need for the this keyword. This is for scope conflicts, and there are none.
     
  3. Offline

    zoren3105

    @Strahan I did try multiple things in the meantime but this one did seem to work until I got into the server console as the first load up was fine where whereas any time that cofig.yml had data it would say UUID string too large in the console do you know why or how to fix this?
     
    Last edited: Jan 5, 2022
Thread Status:
Not open for further replies.

Share This Page