Help Making Dueling Plugin

Discussion in 'Plugin Development' started by OhYes, Mar 17, 2016.

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

    OhYes

    Okay, so, SO far, I have what seems to be pretty reasonable and creating/deleting arenas works fine along with setting warps. I need some help now, with the actual Dueling aspect of it. Now, below I will have the code and I'm wondering a few things:

    * Is the code I have optimal for what I want?
    * Could the code be better?
    * How would I make my duelHandler() Method choose a RANDOM arena each time, out of the config?
    * I assume making a kit menu would be easy. Just as, as soon as the player does the /kd accept, it brings up the GUI with a kit selector, and once both players choose a kit, they're teleported?
    * and, how would I go about making a Countdown feature to the duel, e.g. They cant move until the timer hits 0, and it broadcasts in chat like 5, 4, 3, 2, 1, FIGHT.

    Code:
    package me.OhYes.kitsduel;
    
    import java.util.HashMap;
    import java.util.Random;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class Main extends JavaPlugin {
    
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new DuelListener(), this);
            this.getConfig().options().copyDefaults();
            saveDefaultConfig();
        }
    
        @Override
        public void onDisable() {
    
        }
    
        Random arenaselector = new Random(this.getConfig().getKeys(false).size());
    
        public int duel = 0;
    
        public String playername;
    
        public HashMap<String, Player> duels = new HashMap<String, Player>();
    
        public String prefix = ChatColor.DARK_GREEN + "[" + ChatColor.YELLOW + "KitsDuel" + ChatColor.DARK_GREEN + "]"
                + ChatColor.RESET;
    
        @SuppressWarnings("static-access")
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("You cannot use KitsDuel commands as the Console.");
            } else {
                Player p = (Player) sender;
                // Basic command
                if (commandLabel.equalsIgnoreCase("kd")) {
                    if (args.length == 0) {
                        sendHelpMsg(p);
                        return false;
                    } else if (args.length == 1) {
                        // Help command
                        if (args[0].equalsIgnoreCase("help")) {
                            if (p.hasPermission("kd.help")) {
                                sendHelpMsg(p);
                            } else {
                                sendNoPermMsg(p);
                                return false;
                            }
                            // Duel Accept command
                        } else if (args[0].equalsIgnoreCase("accept")) {
                            if (duel == 0) {
                                p.sendMessage(prefix + ChatColor.RED + "You have no pending duel requests!");
                                return false;
                            } else if (duel == 1) {
                                duel = 2;
                                p.sendMessage(prefix + ChatColor.GREEN + "Challenge Accepted!");
                                duelHandler(p);
                            } else {
                                sendErrorMsg(p);
                                return false;
                            }
                        }
                        // Duel Deny command
                        else if (args[0].equalsIgnoreCase("deny")) {
                            duel = 0;
                            duels.remove(playername);
                            p.sendMessage(prefix + ChatColor.RED + "Challenge Denied.");
                            Player requester = Bukkit.getPlayer(playername);
                            requester.sendMessage(prefix + ChatColor.RED + "The Duel Was Denied.");
                            return false;
                        }
                        // Create new arena command
                    } else if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("newarena")) {
                            if (p.hasPermission("kd.newarena")) {
                                if (args[1] == null) {
                                    sendHelpMsg(p);
                                    return false;
                                }
                                try {
                                    String arenaname = args[1];
                                    getConfig().createSection(arenaname);
                                    saveConfig();
                                    p.sendMessage(prefix + ChatColor.GREEN + "Arena " + ChatColor.GOLD + arenaname
                                            + ChatColor.GREEN + " successfully created.");
                                    p.sendMessage(prefix + ChatColor.GREEN
                                            + "Now you should add two warps with /kd setarenawarp1 <arena name> and /kd setarenawarp2 <arena name>");
                                } catch (Exception e) {
                                    sendErrorMsg(p);
                                    return false;
                                }
                            } else {
                                sendNoPermMsg(p);
                                return false;
                            }
                            // Delete existing arena command
                        } else if (args[0].equalsIgnoreCase("delarena")) {
                            if (p.hasPermission("kd.delarena")) {
                                try {
                                    String arenaname = args[1];
                                    getConfig().set(arenaname, null);
                                    saveConfig();
                                    p.sendMessage(prefix + ChatColor.GREEN + "Successfully deleted.");
                                } catch (Exception e) {
                                    sendErrorMsg(p);
                                    return false;
                                }
                            } else {
                                sendNoPermMsg(p);
                                return false;
                            }
                            // setting warp1 command
                        } else if (args[0].equalsIgnoreCase("setarenawarp1")) {
                            if (p.hasPermission("kd.setarenawarp")) {
                                try {
                                    ConfigurationSection sec = getConfig().getConfigurationSection(args[1]);
                                    Location location = p.getLocation();
                                    getConfig().createPath(sec, "warp1");
                                    getConfig().createPath(sec, "warp1.x");
                                    getConfig().createPath(sec, "warp1.y");
                                    getConfig().createPath(sec, "warp1.z");
                                    getConfig().set(args[1] + ".warp1.x", location.getX());
                                    getConfig().set(args[1] + ".warp1.y", location.getY());
                                    getConfig().set(args[1] + ".warp1.z", location.getZ());
    
                                    saveConfig();
    
                                    p.sendMessage(prefix + ChatColor.GREEN + "Warp 1 Location set.");
                                } catch (Exception e) {
                                    sendErrorMsg(p);
                                    return false;
                                }
                            } else {
                                sendNoPermMsg(p);
                                return false;
                            }
                            // setting warp2 command
                        } else if (args[0].equalsIgnoreCase("setarenawarp2")) {
                            if (p.hasPermission("kd.setarenawarp")) {
                                try {
                                    ConfigurationSection sec = getConfig().getConfigurationSection(args[1]);
                                    Location location = p.getLocation();
                                    getConfig().createPath(sec, "warp2");
                                    getConfig().createPath(sec, "warp2.x");
                                    getConfig().createPath(sec, "warp2.y");
                                    getConfig().createPath(sec, "warp2.z");
                                    getConfig().set(args[1] + ".warp2.x", location.getX());
                                    getConfig().set(args[1] + ".warp2.y", location.getY());
                                    getConfig().set(args[1] + ".warp2.z", location.getZ());
    
                                    saveConfig();
    
                                    p.sendMessage(prefix + ChatColor.GREEN + "Warp 2 Location set.");
                                } catch (Exception e) {
                                    sendErrorMsg(p);
                                    return false;
                                }
                            } else {
                                sendNoPermMsg(p);
                                return false;
                            }
                        } else if (args[0].equalsIgnoreCase("duel")) {
                            try {
                                Player target = Bukkit.getPlayer(args[1]);
                                if(target.getName().equals(p.getName())){
                                    p.sendMessage(prefix + ChatColor.RED + "You cannot request to duel yourself!");
                                }else{
                                p.sendMessage(prefix + ChatColor.GREEN + "Sent " + ChatColor.GOLD + target.getName()
                                        + ChatColor.GREEN + " a Duel Invitation...");
                                target.sendMessage(prefix + ChatColor.GOLD + p.getName() + ChatColor.GREEN
                                        + " has sent you a Duel Invitation. (/kd accept or /kd deny)");
                                duels.put(p.getName(), p);
                                playername = p.getName();
                                if (duel == 2) {
                                    p.sendMessage(prefix + ChatColor.RED + "You are already in a duel!");
                                } else {
                                    duel = 1;
                                }
                            }
                            }catch (Exception e) {
                                sendErrorMsg(p);
                                return false;
                            }
                       
                       
                       
                   
                        // arena teleport command
                    } else if (args.length == 3) {
                        if (args[0].equalsIgnoreCase("teleport")) {
                            if (p.hasPermission("kd.teleport")) {
                                try {
                                    String arenaname = args[1];
                                    int warpnumber = Integer.parseInt(args[2]);
    
                                    if (warpnumber == 1) {
                                        p.teleport(new Location(p.getWorld(), getConfig().getInt(arenaname + ".warp1.x"),
                                                getConfig().getInt(arenaname + ".warp1.y"),
                                                getConfig().getInt(arenaname + ".warp1.z")));
                                    } else if (warpnumber == 2) {
                                        p.teleport(new Location(p.getWorld(), getConfig().getInt(arenaname + ".warp2.x"),
                                                getConfig().getInt(arenaname + ".warp2.y"),
                                                getConfig().getInt(arenaname + ".warp2.z")));
                                    } else {
                                        p.sendMessage(prefix + ChatColor.RED + "Each arena only has 2 warps!");
                                    }
                                } catch (Exception e) {
                                    sendErrorMsg(p);
                                    return false;
                                }
                            } else {
                                sendNoPermMsg(p);
                                return false;
                            }
                        }
                    }
                }
            }
            }
            return false;
        }
    
        private void duelHandler(Player p) {
            Player requester = Bukkit.getPlayer(playername);
    
            p.teleport(new Location(p.getWorld(), getConfig().getInt("test9.warp1.x"),
                    getConfig().getInt("test9.warp1.y"), getConfig().getInt("test9.warp1.z")));
           
            requester.teleport(new Location(p.getWorld(), getConfig().getInt("test9.warp2.x"),
                    getConfig().getInt("test9.warp2.y"), getConfig().getInt("test9.warp2.z")));
    
        }
    
        private void sendErrorMsg(Player p) {
            p.sendMessage(prefix + ChatColor.RED + "Something went wrong. Please contact an Administrator.");
    
        }
    
        private void sendNoPermMsg(Player p) {
            p.sendMessage(prefix + ChatColor.RED + "You do not have the Permission for that command.");
        }
    
        private void sendHelpMsg(Player p) {
            p.sendMessage(prefix + ChatColor.GREEN + "Listing commands:");
            p.sendMessage(ChatColor.AQUA + "/kd help (Brings up this Text Screen.)");
            p.sendMessage(ChatColor.AQUA + "/kd newarena <arena name> (Creates a new arena.)");
            p.sendMessage(ChatColor.AQUA + "/kd delarena <arena name> (Deletes an existing arena.)");
            p.sendMessage(ChatColor.AQUA + "/kd setarenawarp1 <arena name> (Sets the first warp for an arena.)");
            p.sendMessage(ChatColor.AQUA + "/kd setarenawarp2 <arena name> (Sets the second warp for an arena.)");
            p.sendMessage(
                    ChatColor.AQUA + "/kd teleport <arena name> <warp 1/2> (Teleports to a specific warp in the arena.)");
            p.sendMessage(ChatColor.AQUA + "/kd duel <player name> (Sends a duel invite to a player.)");
            p.sendMessage(ChatColor.AQUA + "/kd accept (Accepts the duel invitation.)");
            p.sendMessage(ChatColor.AQUA + "/kd deny (Denies the duel invitation.)");
    
        }
    
    }
    
     
  2. Offline

    mcdorli

    1.: You accidentally imported the bungeecoord chatcolor, use the craftbukkit.jar instead of the spigot.jar, to avoid this in the future.
    2.: Although it's acceptable in this case, but don't name your main class Main, while using bukkit.
    3.: You don't need to have the onDisable, if you don't use it.
    4.: Use the private modifier, if you only going to use a field in one class
    5.: You can make your code take up less space by:

    - Use one line if's if possible
    Code:
    //instead of
    if (that.isTrue()) {
        return;
    }
    
    //do
    if (that.isTrue)
        return;
    
    This can be done with loops thugh, ithowever makes the code unreadable
    - If the sender is not a player, then return instead of using an else statement

    Code:
    //instead of
    if (!(sender instanceof Player)) {
        //doStuff
    } else {
        //do Other stuff
    }
    
    //do
    if (!(sender instanceof Player)) {
        //doStuff
        return;
    }
    
    //do Other stuff
    
    same applies to args length
    6.: Use a switch statement
    7.: You should only have max 200 lines of code in a class, while you use bukkit, break it up into multiple classes, for example have a Duel class, wich manages Duels, or even better, have a Duel class and a DuelFactory, to manage Duels.
     
  3. Offline

    Irantwomiles

    Just going to save you some time. You should make a Duel Class instead of using HashMaps. I made the same plugin using hashmaps and I came across many problems later on.
     
  4. Offline

    mcdorli

    I'm tempted to make a proper tutorial about how to make a team plugin sometimes.
     
  5. Offline

    Irantwomiles

    you should tbh. I still have trouble with it. Makes sense in my head but when I try to code one it just doesn't make sense.
     
  6. Offline

    Gonmarte

    I do not advice to pick a random arena. You should do instead a for loop, lets say you have 10 arenas starting in 0 so 0,1,2,3,4,5,6,7,8,9 so you loop for all of them and in each loop you check if that arena is already being used if so it will keep looping till he find ones clear, if he finds you break the loop, otherwise just send a msg saying that all arenas are being used.
    If i was not clear tag me plz
     
  7. Offline

    MajorSkillage

    You should have a class for arena allowing multiple instances of it to be made with a primary key then map that in the main class to allow you to easily reference any arena. Secondly you can make a value in that class for who's dueling in it. As for the random arena you may as well just make an array of arenas then get a random value between 0 and the size of the array - 1 and reference that arena from the random number (you then have a random arena selected, congrats!).
     
  8. Offline

    mythbusterma

    @mcdorli

    I would recommend against ever using code blocks without brackets. It makes it harder to read and easier to introduce mistakes into.
     
Thread Status:
Not open for further replies.

Share This Page