Solved Command doing nothing

Discussion in 'Plugin Development' started by ZiemniakZPieca, Jul 2, 2022.

  1. Offline

    ZiemniakZPieca

    Hello. I have writed command that must modify (or create if theres no one) two .yml files, but when i activate command it doesn't work.
    Here is main class:
    Code:
    package me.kartofel374;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    public final class PlotOwners extends JavaPlugin {
    
        @Override
        public void onEnable() {
            // Plugin startup logic
            getLogger().info("Starting PlotOwners Plugin");
            this.getCommand("CreatePlot").setExecutor(new CreatePlotCommandExecutor(this));
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
            getLogger().info("Stopping PlotOwners Plugin");
        }
    
    
    }
    
    Here is CreatePlotCommandExecutor.java:
    Code:
    package me.kartofel374;
    
    import com.sun.javafx.binding.Logging;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    import java.io.File;
    import java.util.logging.Logger;
    
    
    public class CreatePlotCommandExecutor implements CommandExecutor {
        private final PlotOwners plugin;
        public CreatePlotCommandExecutor(PlotOwners plugin) {
            this.plugin = plugin;
        }
        public Logger log = Bukkit.getLogger();
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            int x1, z1, x2, z2;
            if (args.length > 5 || args.length < 0) {
                sender.sendMessage("Too many arguments! To use command type: /CreatePlot <x1 coordinate> <z1 coordinate> <x2 coordinate> <z2coordinate>");
                log.info(sender.getName() + ": Too many arguments! To use command type: /CreatePlot <x1 coordinate> <z1 coordinate> <x2 coordinate> <z2coordinate>");
            }
            switch (args.length) {
                case 0:
                    sender.sendMessage("Wrong command usage! To use command type: /CreatePlot <x1 coordinate> <z1 coordinate> <x2 coordinate> <z2coordinate>");
                    log.info(sender.getName() + ": Wrong command usage! To use command type: /CreatePlot <x1 coordinate> <z1 coordinate> <x2 coordinate> <z2coordinate>");
                    break;
                case 1:
                    try {
                        x1 = Integer.parseInt(args[0]);
                    } catch (NumberFormatException ex1) {
                        sender.sendMessage("Invalid x1 coordinate!");
                        log.info(sender.getName() + ": Invalid x1 coordinate!");
                        ex1.printStackTrace();
                        break;
                    }
                    sender.sendMessage("Wrong command usage! To use command type: /CreatePlot " + x1 + " <z1 coordinate> <x2 coordinate> <z2 coordinate>");
                    log.info(sender.getName() + ": Wrong command usage! To use command type: /CreatePlot " + x1 + " <z1 coordinate> <x2 coordinate> <z2 coordinate>");
                    break;
                case 2:
                    try {
                        x1 = Integer.parseInt(args[0]);
                    } catch (NumberFormatException ex2) {
                        sender.sendMessage("Invalid x1 coordinate!");
                        log.info(sender.getName() + ": Invalid x1 coordinate!");
                        ex2.printStackTrace();
                        break;
                    }
                    try {
                        z1 = Integer.parseInt(args[1]);
                    } catch (NumberFormatException ex3) {
                        sender.sendMessage("Invalid z1 coordinate!");
                        log.info(sender.getName() + ": Invalid z1 coordinate!");
                        ex3.printStackTrace();
                        break;
                    }
                    sender.sendMessage("Wrong command usage! To use command type: /Create plot " + x1 + " " + z1 + " <x2coordinate> <z2 coordinate>");
                    log.info(sender.getName() + ": Wrong command usage! To use command type: /Create plot " + x1 + " " + z1 + " <x2coordinate> <z2 coordinate>");
                    break;
                case 3:
                    try {
                        x1 = Integer.parseInt(args[0]);
                    } catch (NumberFormatException ex4) {
                        sender.sendMessage("Invalid x1 coordinate!");
                        log.info(sender.getName() + ": Invalid x1 coordinate!");
                        ex4.printStackTrace();
                        break;
                    }
                    try {
                        z1 = Integer.parseInt(args[1]);
                    } catch (NumberFormatException ex5) {
                        sender.sendMessage("Invalid z1 coordinate!");
                        log.info(sender.getName() + ": Invalid z1 coordinate!");
                        ex5.printStackTrace();
                        break;
                    }
                    try {
                        x2 = Integer.parseInt(args[2]);
                    } catch (NumberFormatException ex6) {
                        sender.sendMessage("Invalid x2 coordinate!");
                        log.info(sender.getName() + ": Invalid x2 coordinate!");
                        ex6.printStackTrace();
                        break;
                    }
                    sender.sendMessage("Wrong command usage! To use command type: /Create plot " + x1 + " " + z1 + " " + x2 + " <z2 coordinate>");
                    log.info(sender.getName() + ": Wrong command usage! To use command type: /Create plot " + x1 + " " + z1 + " " + x2 + " <z2 coordinate>");
                    break;
                case 4:
                case 5:
    
                    //casting all arguments to int
                    try {
                        x1 = Integer.parseInt(args[0]);
                    } catch (NumberFormatException ex4) {
                        sender.sendMessage("Invalid x1 coordinate!");
                        log.info(sender.getName() + ": Invalid x1 coordinate!");
                        ex4.printStackTrace();
                        break;
                    }
                    try {
                        z1 = Integer.parseInt(args[1]);
                    } catch (NumberFormatException ex5) {
                        sender.sendMessage("Invalid z1 coordinate!");
                        log.info(sender.getName() + ": Invalid z1 coordinate!");
                        ex5.printStackTrace();
                        break;
                    }
                    try {
                        x2 = Integer.parseInt(args[2]);
                    } catch (NumberFormatException ex6) {
                        sender.sendMessage("Invalid x2 coordinate!");
                        log.info(sender.getName() + ": Invalid x2 coordinate!");
                        ex6.printStackTrace();
                        break;
                    }
                    try {
                        z2 = Integer.parseInt(args[3]);
                    } catch (NumberFormatException ex7) {
                        sender.sendMessage("Invalid z2 coordinate!");
                        log.info(sender.getName() + ": Invalid z2 coordinate!");
                        ex7.printStackTrace();
                        break;
                    }
    
                    //checks is sender has permission
                    boolean hasPermission;
                    if(sender instanceof Player) {
                        if (sender.hasPermission("PlotOwners.CreatePlot")) {
                            hasPermission = true;
                        } else {
                            hasPermission = false;
                            sender.sendMessage("You don't have permission to do that!");
                            log.info(sender.getName() + ": You don't have permission to do that!");
                            break;
                        }
                    } else {
                        hasPermission = true;
                    }
                    if (hasPermission) {
    
                        //setting up needed files
                        File Plots = new File("plugins/PlotOwners/Plots.yml");
                        log.config("File \"Plots.yml\" was created!");
                        File PlayerPlots = new File("plugins/PlotOwners/PlayerPlots.yml");
                        log.config("File \"PlayerPlots.yml\" was created!");
                        YamlConfiguration PlotsFile = YamlConfiguration.loadConfiguration(Plots);
                        YamlConfiguration PlayerPlotsFile = YamlConfiguration.loadConfiguration(PlayerPlots);
    
                        //Checks is there 5 arguments (contains plot name?)
                        boolean FiveArguments;
                        if (args.length == 5) {
                            FiveArguments = true;
                        } else {
                            FiveArguments = false;
                        }
    
                        //Checks is sender Player
                        boolean IsSenderPlayer;
                        if(sender instanceof Player) {
                            IsSenderPlayer = true;
                        } else {
                            IsSenderPlayer = false;
                        }
    
                        //Plots counting system
                        if (IsSenderPlayer) {
                            if(PlayerPlotsFile.contains(sender.getName())) {
                                int oldPlayerPlots = PlayerPlotsFile.getInt(sender.getName());
                                PlayerPlotsFile.set(sender.getName(), ++oldPlayerPlots);
                            } else {
                                PlayerPlotsFile.set(sender.getName(), 1);
                            }
                        } else {
                            if(PlayerPlotsFile.contains("Console")) {
                                int oldConsolePlots = PlayerPlotsFile.getInt("Console");
                                PlayerPlotsFile.set("Console", ++oldConsolePlots);
                            } else {
                                PlayerPlotsFile.set("Console", 1);
                            }
                        }
    
                        //registering Plot in PlotFile
                        if (FiveArguments) {
                            if (IsSenderPlayer) {
                                PlotsFile.set((sender.getName()) + "." + args[4] + ".x1", x1);
                                PlotsFile.set((sender.getName()) + "." + args[4] + ".z1", z1);
                                PlotsFile.set((sender.getName()) + "." + args[4] + ".x2", x2);
                                PlotsFile.set((sender.getName()) + "." + args[4] + ".z2", z2);
                            } else {
                                PlotsFile.set("Console." + args[4] + ".x1", x1);
                                PlotsFile.set("Console." + args[4] + ".z1", z1);
                                PlotsFile.set("Console." + args[4] + ".x2", x2);
                                PlotsFile.set("Console." + args[4] + ".z2", z2);
                            }
                        } else {
                            if (IsSenderPlayer) {
                                PlotsFile.set((sender.getName()) + ".plot" + PlayerPlotsFile.getInt(sender.getName()) + ".x1", x1);
                                PlotsFile.set((sender.getName()) + ".plot" + PlayerPlotsFile.getInt(sender.getName()) + ".z1", z1);
                                PlotsFile.set((sender.getName()) + ".plot" + PlayerPlotsFile.getInt(sender.getName()) + ".x2", x2);
                                PlotsFile.set((sender.getName()) + ".plot" + PlayerPlotsFile.getInt(sender.getName()) + ".z2", z2);
                            } else {
                                PlotsFile.set("Console.Plot" + PlayerPlotsFile.getInt("Console") + ".x1", x1);
                                PlotsFile.set("Console.Plot" + PlayerPlotsFile.getInt("Console") + ".z1", z1);
                                PlotsFile.set("Console.Plot" + PlayerPlotsFile.getInt("Console") + ".x2", x2);
                                PlotsFile.set("Console.Plot" + PlayerPlotsFile.getInt("Console") + ".z2", z2);
                            }
                        }
                    }
                    break;
            }
            return true;
        }
    }
    I tried with "CreatePlot" command and "createplot" command. all is fine in plugin.yml.
     
  2. Offline

    DopeBrot

    i think you are not saving the file
     

Share This Page