Solved Registering Offences

Discussion in 'Plugin Development' started by ZeusReksYou_, May 14, 2016.

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

    ZeusReksYou_

    Hello guys,

    I just coded a little plugin to check if I still know a bit how to ban / kick people. I checked it out and everything seems to work properly, and now I want to improve it a bit, but I don't really know how.

    I would like to make, that if a player gets kicked, staffmembers or players with the permission can see the ban/kick/warn history of the player with a command.

    Does anybody know how to do this?

    Here is my source code, I hope you can help me !

    Code:
    package mcplugin.ZeusReksYou_.EzBans;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    
    public class EzBans extends JavaPlugin implements Listener{
         
        public void onEnable()
        {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if (cmd.getName().equalsIgnoreCase("ezkick")){
                if (!(sender.hasPermission("ezbans.kick"))){
                    sender.sendMessage(ChatColor.RED + "You do not have permission to do this !");
                    return true;
                }
                if (args.length < 2) {
                    sender.sendMessage(ChatColor.RED + "Usage: /ezkick <playername> <reason>");
                    return true;
                }
                Player target = Bukkit.getServer().getPlayer(args[0]);
                if (target == null){
                    sender.sendMessage(ChatColor.RED + "Usage: /ezkick <player> <reason>");
                    return true;
                }
                String txt = "";
                  for (int i = 1; i < args.length; i++) txt = txt + (i > 1 ? " " : "") + args[i];
                  target.kickPlayer(ChatColor.RED + "You have been kicked !\n" + ChatColor.GREEN + "Reason: " + txt);
                  Bukkit.getServer().getPluginManager().callEvent(new Enforcer(target, Type.KICK));
                    return true;
        }
    
    return true;
    }
    }
    
    Sincerely,

    ZeusReksYou_
     
    Last edited: May 28, 2016
  2. Offline

    WolfMage1

    Store it in a config?

    Look at Bukkit's configuration API

    http://wiki.bukkit.org/Configuration_API_Reference

    or alternatively store it in a mysql database, but the former is probably a better option for starters.
     
    cococow123 likes this.
  3. Offline

    OhYes

    As wolfmage said, store it in a config. Make custom commands for kick/ban with the same command names. (kick and ban) They will do the same thing as kick and ban, and also add it to a config.
    Example:
    Code:
    if(cmd.getName().equalsIgnoreCase("kick"){
    //Do the same for ban too.
    if(args.length == 0){
    player.sendMessage(ChatColor.RED + "Specify a player.");
    }else if(args.length == 1){
    try{
    Player target = Bukkit.getPlayer(args[0]);
    target.kickPlayer(ChatColor.RED + "You have been kicked.");
    //You could add additional arguments for reason //why they are kicked too.
    
    //PUT THIS OUTSIDE OF onCommand
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = new Date();
    //PUT THIS OUTSIDE OF onCommand
    
    this.getConfig().set("History." + target.getName() + ".lastKicked", dateFormat.format(date))
    
    //This will set the path History.name.lastKicked with //the date and time they are kicked. You can do the //same for bans too.
    
    }catch(Exception e){
    player.sendMessage(ChatColor.RED + "That player is not online, or does not exist");
    return false;
    }
    }
    }
    As for the command, just create a new one that gets the config, and gets the path of History.theirname.lastKicked and assign it to a String. Then, you can use that String in messages.
     
  4. Offline

    ZeusReksYou_

    I don't exactly understand it. I put the DateFormat part out of the onCommand, and to understand it, I used your example. Now the kicking part works, I get kicked if I execute the command, and get a message if I fill in too few args (This is how far I came too)

    I edited the onEnable a little bit:

    Code:
    public void onEnable()
    {
       Bukkit.getServer().getPluginManager().registerEvents(this, this);
       this.getConfig().options().copyDefaults(true);
       this.saveConfig();
    }
    I don't know what to create in the config.yml. If I get kicked nothing happens because (this is what I think) there is no string in the config.yml yet. I don't know what to edit so it will make that store it, and it is able to see with a command

    Could you help?
     
  5. Offline

    XxTimexX

    @ZeusReksYou_
    In your onEnable() you should put this.saveDefaultConfig();
    It will create new config.yml if one doesn't exist. If it does, it will do nothing.
     
  6. Offline

    ZeusReksYou_

    I will edit this ! Thanks for your fast reply ! Lets hope that it works !
     
Thread Status:
Not open for further replies.

Share This Page