Solved Delete lines from Group Manager user file

Discussion in 'Plugin Development' started by Franovic, Mar 2, 2015.

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

    Franovic

    I'm pretty new to coding minecraft plugins and I would like to make a plugin that deletes all the lines from the users.yml for a specific user. It will be a command with /ppr and will delete the user's permissions and /ppr <player> will delete the player's permissions

    Code:
    package me.Franhell;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class HypePermRemover extends JavaPlugin{
        public static HypePermRemover plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public FileConfiguration customConfig = null;
     
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled.");
        }
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + "Version: " + pdfFile.getVersion() + " Has Been Enabled.");
        }
     
        public boolean onRequest(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("ppr")){
                if(player.isOp()){
                    if(args.length == 0){
                        File plugins = plugin.getDataFolder().getParentFile();
                        File folder = new File(plugins.getPath() + File.separator + "GroupManager" + File.separator + "worlds" + File.separator + "world" + File.pathSeparator + "users.yml");
                        customConfig = YamlConfiguration.loadConfiguration(folder);
                        customConfig.set(player.getUniqueId() + ".permissions", null);
                        try {
                            customConfig.save(folder);
                        } catch (IOException ex) {
                            getLogger().log(Level.SEVERE, "Could not save config to " + folder, ex);;
                        }
                        player.sendMessage(ChatColor.BLUE + "Player has had their permissions removed.");
                    }else if(args.length == 1){
                        Player targetPlayer = getServer().getPlayer(args[0]);
                        File plugins = plugin.getDataFolder().getParentFile();
                        File folder = new File(plugins.getPath() + File.separator + "GroupManager" + File.separator + "worlds" + File.separator + "world" + File.pathSeparator + "users.yml");
                        customConfig = YamlConfiguration.loadConfiguration(folder);
                        customConfig.set(targetPlayer.getUniqueId() + ".permissions", null);
                        try {
                            customConfig.save(folder);
                        } catch (IOException ex) {
                            getLogger().log(Level.SEVERE, "Could not save config to " + folder, ex);;
                        }
                        player.sendMessage(ChatColor.BLUE + "Player has had their permissions removed.");
                    }
                }else
                    player.sendMessage(ChatColor.RED + "You Need To Be OP For This.");
            }
            return false; 
        }
     
        public boolean onAccept(CommandSender sender, Command cmd , String commandLabel){
            return false;
         
        }
    }
     
    Last edited: Mar 2, 2015
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Franovic

    I don't really know what the problem is I enter the command and nothing happens, I'm adding messages in at each stage now but it's probably something to do with the file opening

    Edit: the first problem is in the
    File plugins = plugin.getDataFolder().getParentFile();
     
    Last edited: Mar 2, 2015
  4. Offline

    Lolmewn

    Call your method onCommand, not onRequest.
     
  5. Offline

    Franovic

    done that although I still get "An internal error occurred while attempting to perform this command" in the File plugins = plugin.getDataFolder().getParentFile();

    also I thought that didn't matter when for a method only what's in the brackets
     
  6. Show us the errorlog
     
  7. Offline

    Franovic

    org.bukkit.command.CommandException: Unhandled exception executing command 'ppr' in plugin HypePermRemover v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    Caused by: java.lang.NullPointerException
    at me.Franhell.HypePermRemover.onCommand(HypePermRemover.java:44) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Spigot-1.7.9-R0.2-207-g03373bb]
    ... 13 more
    [22:31:50] [Server thread/INFO]: CONSOLE: Stopping the server..[m
     
  8. customConfig = null (not found)
    Try replacing the File things just with
    "plugins/GroupManager/worlds/world/users.yml"
    and add
    if(customConfig == null) {
    //debug message
    }
    BEFORE accessing its mehods
    And catch Exception instead of IOException (catches all exceptions)
     
  9. Offline

    Franovic

    Thanks a lot it works after it get's reloaded so I can do that by myself.
     
  10. You could reload GroupManager after it :D
    Code:
    PluginManager pm = Bukkit.getPluginManager();
    Plugin pl = pm.loadPlugin("GroupManager);
    if(pl != null) {
    pm.disablePlugin(pl);
    pm.enablePlugin(pl);
    }
     
    Last edited: Mar 2, 2015
Thread Status:
Not open for further replies.

Share This Page