Help with a warn plugin

Discussion in 'Plugin Development' started by ElGamesHD, Aug 19, 2017.

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

    ElGamesHD

    Hi, im trying to make a warn plugin and its works, but i want to do that every warn level durate x time for example 7 days, code:

    Code:
    package com.gmail.elgameshdpay;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    public class Principal extends JavaPlugin {
          
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
                    if (cmd.getName().equalsIgnoreCase("warn")) {
                        if(sender.hasPermission("comucore.warn")) {
                            if (args.length == 0) {
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&E&l>> &fEjemplo: &e/warn "+ sender.getName() + " spam"));
                                    return true;
                            }
                          
                            if (args.length == 1) {
                               
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l>> &c¡No has especificado el jugador o la razon!"));
                               
                                return true;
                            }
                            final Player target = Bukkit.getServer().getPlayer(args[0]);
                          
                            if (target == null) {
                                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l>> &c¡El jugador &c&n" + args[0]+ "&c no esta conectado!"));
                                    return true;
                            }
                          
                            String msg = "";
                            for (int i = 1; i < args.length; i++) {
                                    msg += args[i] + "";
                            }
                          
                            Object level = this.getConfig().get(target.getName());
                          
                            if (level == null) {
                                Player player=Bukkit.getPlayer(args[0]);
                                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l>> &c¡Has recibido una advertencia por: "+msg+" ! (1/4)"));
                                    Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&c# &e["+args[0]+"&e] &c[Warn] &a["+ sender.getName() +"&a] &d[1/4] &b["+msg+"&b]"), "comucore.warn");
                                    this.getConfig().set(target.getName(), 1);
                                    this.saveConfig();
                                    return true;
                            }
                          
                            int l = Integer.parseInt(level.toString());
                          
                            if (l == 1) {
                                    Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&c# &e["+args[0]+"&e] &c[Warn] &a["+ sender.getName() +"&a] &d[2/3] &b["+msg+"&b]"), "comucore.warn");
                                Player player=Bukkit.getPlayer(args[0]);
                                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l>> &c¡Has recibido una advertencia por: "+msg+" ! (2/4)"));
                                Player p = (Player)sender;
                                 p.performCommand("kick "+args[0]+" &CHas recibido tu segunda advertencia y has sido kickeado. Razon: "+args[1] + "(2/4)");
                                    this.getConfig().set(target.getName(), 2);
                                    this.saveConfig();
                                    return true;
                            }
                          
                            if (l == 2) {
                                    Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&c# &e["+args[0]+"&e] &c[Warn] &a["+ sender.getName() +"&a] &d[3/4] &b["+msg+"&b]"), "comucore.warn");
                                Player player=Bukkit.getPlayer(args[0]);
                                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l>> &c¡Has recibido una advertencia por: "+msg+" ! (3/4)"));
                                   Player p = (Player)sender;
                                   p.performCommand("tempban "+args[0]+" 1d &CHas recibido tu tercera advertencia y has sido baneado temporalmente por 1 dia. Razon: "+args[1] + "(3/3)");
                                this.getConfig().set(target.getName(), 3);
                                this.saveConfig();
                                return true;
                                    }
                            if (l == 3) {
                                    Bukkit.broadcast(ChatColor.translateAlternateColorCodes('&', "&c# &e["+args[0]+"&e] &c[Warn] &a["+ sender.getName() +"&a] &d[4/4] &b["+msg+"&b]"), "comucore.warn");
                                Player player=Bukkit.getPlayer(args[0]);
                                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&4&l>> &c¡Has recibido una advertencia por: "+msg+" ! (4/4)"));
                                   Player p = (Player)sender;
                                   p.performCommand("tempban "+args[0]+" 1d &CHas recibido tu cuarta advertencia y has sido baneado permanentemente. Razon: "+args[1] + "(4/4)");
                                this.getConfig().set(target.getName(), 0);
                                this.saveConfig();
                                return true;
                                    }
                            }
                    }
                    return true;
                    }
    }
    
     
  2. Offline

    Horsey

    Some servers restart every like 6 hours, so a 7 day scheduler will not work.

    @ElGamesHD You should save the time at which the player receive the warn, and then, in the onEnable, schedule a repeating task to check every like 30 minutes or so (it's up for you to decide, probably use a longer time period since this would probably involve lots of looping) whether the warn has expired by comparing it to the current time, and remove the warn.
     
  3. @Horsey

    Either will work, as long as, as you say, the server doesn't reboot.
     
  4. Offline

    Horsey

    A BukkitRunnable runs after a certain amount of ticks. Usually there are 20 ticks in a second. Usually. However, most servers only get like 19 ticks a second. So, for every 20 seconds, the BukkitRunnable lasts 1 second extra. So, in 7 days, the BukkitRunnable runs nearly 8 hours and 30 minutes later.
     
  5. Offline

    Zombie_Striker

    @NullPointerExptn
    Not only can the precision of tasks be off, but as you say, the server can't reboot. However, Bukkit and some plugins may not handle data that well, potentially holding onto more memory than needed. Because of this, it is commonly recommended that a server should not go over 24 hours without reloading. Because of this, your suggestion will not work.

    @ElGamesHD @Horsey
    Considering you would be just doing a simple time check every time it loops, you can easily bring the delay down to checking every 5 minutes without any noticeable lag.
     
    Horsey likes this.
  6. Offline

    ElGamesHD

    but i dont know the code xD

    The thing that i want to do is if, i have a warn, and if a have other i get kick and other get ban, okay but now i want that if i get one warn and dont get any more warn in 7 days, it restart to level 0

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 19, 2017
  7. Offline

    Zombie_Striker

    @ElGamesHD
    1. Create a Hashmap. The keys will be player's UUIDs and the values will be a Long. This represents when the warning will be over.
    2. In the onCommand, if the player has been warned, get the current time in milliseconds. Add 7*24*60*60*60*1000 (or, seven days in milliseconds) to the time. This is when the warning will be over. Add this time to the hashmap and to the config.
    3. In the onEnable, get all the config values for all players that have been warned and add them to the hashmap.
    4. After that, create a new BukkitRunnable. Set the delay between repeats to be 5 seconds (or 5*60*20 ticks). In it, loop through all the values in the hashmap and check if any of the warnings are over. If they are, remove the time from the config and the hashmap.
     
  8. Offline

    ElGamesHD

    :/ idk how to do all these things, im starting in the world of java
     
  9. Offline

    Horsey

    @ElGamesHD In that case you should learn Java first. There are tons of online resources that can help you.
     
  10. Offline

    ElGamesHD

    And cant somebody do it for me please?
     
  11. Offline

    Zombie_Striker

    @ElGamesHD
    If you want someone to write the plugin for you, post in the Plugin Requests forum. The plugin development forum is here to help you learn and to guide users how to fix their problems. Just giving code here would teach users nothing but to copy and paste code.
     
  12. Offline

    ElGamesHD

    the things i have learned are with code examples
     
  13. Offline

    timtower Administrator Administrator Moderator

    They do that in other places but it is very frowned upon here.
     
Thread Status:
Not open for further replies.

Share This Page