Alternative to a loop?

Discussion in 'Plugin Development' started by DarkNightmare, May 27, 2014.

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


    Hello everyone, Im very new to plugin development but i gave some background in coding, Ive never touched Java before so please be friendly :D.

    The problem im having at the moment is with my PvP plugin.


    package me.DarkNightmare;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginDescriptionFile;
    import java.util.logging.Logger;
    public class Gamble extends JavaPlugin{
        private static final boolean True = false;
        private static final boolean False = false;
        public final Logger logger = Logger.getLogger("Minecraft");
        public static Gamble plugin;
        public void onDisable(){
            PluginDescriptionFile pdfFile = this.getDescription();
   + " Has Been Disabled");
        public void onEnable(){
            PluginDescriptionFile pdfFile = this.getDescription();
   + " Has Been Enabled");
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        boolean pvparena = True;
            Player player = (Player) sender;
            World factionsserver = Bukkit.getWorld("factionsserver");
            Location spawn = player.getWorld().getSpawnLocation();
            Location Pvp = new Location(factionsserver, -963, 51, 1019);
                pvparena = True;
                player.sendMessage(ChatColor.AQUA + "Welcome to the PVP arena!");
                player.getInventory().setChestplate(new ItemStack(Material.DIAMOND_CHESTPLATE, 1));
                player.getInventory().setLeggings(new ItemStack(Material.DIAMOND_LEGGINGS, 1));
                player.getInventory().setHelmet(new ItemStack(Material.DIAMOND_HELMET, 1));
                player.getInventory().setBoots(new ItemStack(Material.DIAMOND_BOOTS, 1));
                player.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
                pvparena = false;
            else if(commandLabel.equalsIgnoreCase("pvp") && pvparena == False){
                player.sendMessage(ChatColor.AQUA + "You are already in the PvP arena!");
            player.sendMessage(ChatColor.AQUA + "You have left the PvP arena!");
            pvparena = True;
            if(commandLabel.equalsIgnoreCase("leavepvp") && pvparena == False){
            player.sendMessage(ChatColor.AQUA + "You must be in the PvP Arena to use this command!");
            return false;
    Now 1st of all i would like to explain that yes, I know the problem is the if loops, But im unaware of how to check in Java.

    Second of all my current method is to check the chat command and then to check a boolean called "pvparena" and if pvparena is True or False it will execute some command, However the problem i have is it will execute certain parts twice because of the conditions.

    So i was wandering what alternatives theyre are too a far loop and if i could have some example code and possibly a breakdown?

    Thank you very much,

  2. Offline



    1. public class Gamble extends JavaPlugin{
    2. private static final boolean True = false;
    4. private static final boolean False = false;

    Did you mean to define True as false ?

    I don't see any for-loops.

    Also, your if-statement statement isn't structured the correct way because you have a local variable pvparena that is a boolean flag to determine if someone is inside an arena already.

    I don't really have time-right now to give examples, but I can later.

    Sometimes when writing code, you could just write pseudo-code in plain English first (or your native language).

    Then translate the pseudo-code into Java.

    If command equals X {

    If sub-command equals Y and player is NOT inside an arena already then teleport him inside.

    else if command equals Y and player is inside an arena already then send helpful messages about already being inside an arena and information about how to leave the arena.

    If sub-command equals Z then do something.

    if sub-command equals W then do something else.
  3. Offline


    You'll find that structuring your code is a lot easier if you indent it correctly. I can see multiple occasions where you've not ended if-statements, causing stuff to execute weirdly.

    If you're not using an IDE such as Eclipse (I don't know why you wouldn't be), then I suggest you do so. It will help you indent your code (as long as you don't try to change it yourself) and you'll have a much easier time trying to figure out why your code isn't working.

    To help you out a little, once you've opened up some curly braces in your code, you should indent four spaces (or a tab). For example:

    1. if ( ! player.isOp() || ! player.hasPermission("blockenchant.enchant")) {
    2. for (int i = 0; i < event.getExpLevelCostsOffered().length; i++) {
    3. int offered = event.getExpLevelCostsOffered()[i];
    5. if (offered > BlockEnchant.maxLevel()) {
    6. event.getExpLevelCostsOffered()[i] = BlockEnchant.maxLevel();
    7. }
    8. }
    9. }[/i][/i]

    You see how I indent everything when I go inside the curly braces? That's only there to help you read your code.

    I'm just going to critique your code a bit here so you get a better understanding of what I mean. Take this bit for example:

    1. else if(commandLabel.equalsIgnoreCase("pvp") && pvparena == False){
    3. player.sendMessage(ChatColor.AQUA + "You are already in the PvP arena!");
    5. if(commandLabel.equalsIgnoreCase("leavepvp")){
    7. player.setGameMode(GameMode.SURVIVAL);
    8. player.teleport(spawn);
    9. player.getInventory().clear();
    10. player.getInventory().setHelmet(null);
    11. player.getInventory().setChestplate(null);
    12. player.getInventory().setLeggings(null);
    13. player.getInventory().setBoots(null);
    14. player.sendMessage(ChatColor.AQUA + "You have left the PvP arena!");
    15. pvparena = True;
    16. }

    For starters, you're not ending the curly brace on the 'else if' statement at the top of this code block. That means, in this case, you'd need to send both the commands 'pvp' and 'leavepvp' at the same time for it to work. (that's not possible, btw)

    You need to ensure you've actually structured your code logically before you finish writing a block, otherwise stuff like that's going to happen.

    Finally, you've not indented the code in your 'leavepvp' command. By not doing this, it's hard to see what code goes where, hence making it really hard to debug after you've found nothing works.
  4. Offline


    I've heard you shouldn't use public final Logger logger = Logger.getLogger("Minecraft");
    Try; public final Logger logger = Bukkit.getLogger();
    And I would recommend an ArrayList of player names to keep track of who is playing the game then use one boolean to check wether the game is running or not. Use a repeating task to remove a second from a defined integer as gametime every second as a countdown and there you are, basics.
  5. Offline


    There's your problem.
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page