Unban Plugin

Discussion in 'Plugin Development' started by GodzillaFlame42, Sep 11, 2016.

Thread Status:
Not open for further replies.
  1. When i try to unban the player that is banned it doesnt unban them and it says it couldnt find the player. How do i fix this?

    Unban class:
    Code:
    package me.godzilla;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    
    public class UnBan implements Listener, CommandExecutor {
      
            @SuppressWarnings({ "deprecation" })
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                    if (cmd.getName().equalsIgnoreCase("unban")) {
                        Player p = (Player) sender;
                        if(p.hasPermission("UnBan.Allow")) {
                            Date now = new Date();
                            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
                            if (args.length < 2) {
                                    sender.sendMessage(ChatColor.RED + "Punish" + ChatColor.DARK_GRAY + " » " + ChatColor.GRAY + "/unban <player> <reason>");
                                    return true;
                            }
                      
                            final Player target = Bukkit.getServer().getPlayer(args[0]);
                         
                            if (target == null) {
                                    sender.sendMessage(ChatColor.RED + "Could not find player " + args[0]);
                                    return true;
                            }
                         
                            String msg = "";
                            for (int i = 1; i < args.length; i++) {
                                    msg += args[i] + " ";
                                   
                            }
                  
                                Bukkit.broadcastMessage(ChatColor.RED + "Punish" + ChatColor.DARK_GRAY + " » " + ChatColor.YELLOW + target.getName() + ChatColor.GRAY + " has been un-banned by " + ChatColor.YELLOW + p.getName());
                                Bukkit.broadcastMessage(ChatColor.RED + "Punish" + ChatColor.DARK_GRAY + " » " + ChatColor.GRAY + "Reason: " + ChatColor.YELLOW + msg);
                                Bukkit.broadcastMessage(ChatColor.RED + "Punish" + ChatColor.DARK_GRAY + " » " + ChatColor.GRAY + "Date: " + ChatColor.YELLOW + format.format(now));
                                target.setBanned(false);
                            }
                        }
                    return false;
            }
    }
     
  2. Offline

    geomap

    .getPlayer() only gets Online players. If the player is banned they are not going to be on the server so they are Offline.

    So try, changing the target variable to OfflinePlayer instead of Player, and use .getOfflinePlayer() instead of .getPlayer()
     
  3. Offline

    Tecno_Wizard

    @GodzillaFlame42 before you ask, Bukkit#getOfflinePlayer() will work if the player is online.
     
  4. Offline

    I Al Istannen

    @Tecno_Wizard @GodzillaFlame42
    And also if the player doesn't exist.

    And Bukkit#getOfflinePlayer(String name) will most likely involve a blocking web request. Don't do that on the main thread.

    @geomap
    Remember you need to tahg users if you want them to get an alert. Click the "Tahg User" button at the lower right of a post or write "@" and then the name.
     
  5. Offline

    Tecno_Wizard

    As far as I am aware it doesn't. It pulls the data from the usercache.json file, and if it does not have an entry in the cache, it returns a player with a random UUID.
     
  6. Offline

    I Al Istannen

    @Tecno_Wizard
    Okay, I don't know about likely, but I have seen it happen and the JavaDoc agrees with me.
     
  7. Offline

    Tecno_Wizard

    @I Al Istannen, wow. I have a probable bug to go fix in my API now.
     
    I Al Istannen likes this.
  8. Offline

    I Al Istannen

Thread Status:
Not open for further replies.

Share This Page