Plugin won't ban me

Discussion in 'Spigot Plugin Development' started by Enderaura, Apr 18, 2017.

  1. When I type `/test` it calls my method to ban a player with testing values, but in console when trying to ban, it says:

    Code (Text):

    [10:45:50 INFO]: Could not ban player c4ef7b5f-1502-44c5-bb51-39542176d1f5

    ºcºlReason:
     
    Code (Java):
    package com.xenderaura.moderation;

    //import java.util.UUID;
    import java.util.logging.Logger;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.xenderaura.moderation.cmnds.Mod;

    public class Moderation extends JavaPlugin implements Listener{
       
       
       
        public void onEnable(){
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
           
            getCommand("mod").setExecutor(new Mod());
           
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(this, this);
           
            logger.info(pdfFile.getName() + " has been enabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(label.equalsIgnoreCase("test")){
                System.out.println("test 4");
               
                Player p = (Player) sender;
               
                this.createPunishment(p, p, Type.BAN, "Test Ban");
            }
           
            return false;
        }
       
        public void onDisable(){
            PluginDescriptionFile pdfFile = getDescription();
            Logger logger = Logger.getLogger("Minecraft");
           
            logger.info(pdfFile.getName() + " has been disabled!");
        }
       

        public void createPunishment(Player staff, Player target, Type t, String reason){
            //UUID u = target.getUniqueId();
           
            if(t==Type.BAN){
                System.out.println("test 1");
                this.getServer().dispatchCommand(this.getServer().getConsoleSender(), "ban " + target.getName() + "\n§cReason: §f" + reason + "\n§cStaff: §f" + staff.getName() + "\n§cExpires: §fNEVER\n§cAppeal: §fForums");
               
            }else{
                System.out.println("test 2");
            }
        }
       
       
        public void createPunishment(Player staff, Player target, Type t, String reason, int time){
           
        }

    }
     
  2. Why do it that way? Why not just use player#setBanned()?
     
  3. latiku

    Supporter

    Pretty sure then the player would have to relog, but that can be easily fixed by kicking the player
     
  4. md_5

    Administrator Developer

    Yeah, use the API.
    Otherwise you're relying on what implements /ban and how its handled.

    In this case however you error is you have no space between .getName() and the "\n<message>", so its trying to do:
    /ban player\ninsertbanmessagehere
     
  5. I dont see an option to set a ban reason for Player#setBanned it's just true or false.
     
  6. md_5

    Administrator Developer

  7. So,

    Code (Java):
    public void createPunishment(Player staff, Player target, Type t, String reason){
           //UUID u = target.getUniqueId();
           String a = target.getName();
           String b = "\n§cReason: §f" + reason + "\n§cStaff: §f" + staff.getName() + "\n§cExpires: §fNEVER\n§cAppeal: §fForums";
           String c = staff.getName();
           if(t==Type.BAN){
               System.out.println("test 1");
               BanList.addBan(a, b, null, c)
           }
     
    ?
     
  8. you'd have to call Server::getBanList(BanList.Type) first (with the right BanList.Type), to get the right BanList.

    Moreover:
    • Don't use the Minecraft Logger, use your plugin's Logger (JavaPlugin:: getLogger())
    • Don't compare command labels, but compare command names (Command::getName(), which automatically supports aliases)
    • Don't blindly cast to Player, CommandSender can be f.e. ConsoleCommandSender (which will result in a ClassCastException)
    • It's advised to annotate overridden method with @Override, so the compiler warns you for user errors (like typos in method names)
     
  9. Code (Java):
    BanList banList = BanList.getBanList(BanList.BAN);
    String a = target.
    String b = "\n§cReason: §f" + reason + "\n§cStaff: §f" + staff.getName() + "\n§cExpires: §fNEVER\n§cAppeal: §fForums";
    String c = staff.getName();
    if(t==Type.BAN){
           System.out.println("test 1");
           banList.addBan(a, b, null, c);
           }
    ?

    I don't know what valid types there are for the BanList type.