Command Not Being Executed If Another Isnt First!?

Discussion in 'Plugin Development' started by J4K3ST3R, Mar 12, 2012.

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

    J4K3ST3R

    alright, lets say that i have a plugin calls start, and run...
    if an admin does /start, then a user can do /start run.
    but if the admin hasnt done /start and the user does /run, it will tell him that it hasnt been started?
    i also need to know how to make it so that when /start is executed, it runs for a certain amount of time... so if i do /start, the user has like 30 seconds to do /run? please help
     
  2. Offline

    SgtStud

    Were going to need some more information than that, as no one understands what you are talking about
     
  3. Offline

    J4K3ST3R

    alright, sorry... i was in a hurry.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, Command command, String[] args){
            Player player = (Player) sender;       
            if(command.getName().equalsIgnoreCase("race")){
               
                if(args.length == 0){
                    return false;
                }
                if(args.length > 1){
                                  player.sendMessage(ChatColor.RED + "Error: /Race <command>");
                    return true;
                }
                if(args[0].equalsIgnoreCase("start")){
                                        //do something.
                    return true;
                }
                if(args[0].equalsIgnoreCase("run")){
                    //do something.
                    return true;
                }
            }
            return true;
    okay, i wrote this up really quick to use as an example. you see how there is start, and run. start would be used by an admin to start a race. and run would be used by a user wanting to run in the race. if the admin had started the race, and a user had performed the /run command. it would do something... but if the user did the /run command without the admin starting it, i want it to tell the user that the race hadnt been started.

    so.
    if the admin did /start, it would start the race
    a user could then join with /run
    but if the admin hadnt done /start, the user would get a message saying it hadnt been started.


    also i want the start command to only last a certain amount of time. so like if the admin did /start, the user would only have ;ets say 1 min to enter with /run.
    sorry if this is confusing):
     
  4. Offline

    xzKinGzxBuRnzx

    You could add a variable to your main file.

    public int started;

    Now on your onCommand file just check if plugin.started == 1;

    http://wiki.bukkit.org/Scheduler_Programming This should help with making something of a timer for how long /start is active.
     
  5. Offline

    J4K3ST3R

    hmmmm, thanks... now, could you help me with this... that above i just wrote as an example but in the plugin im making i need to change the weather... how do i do this? im guessing its something like player.getServer().?????

    alright, all i need is... if someone types /start for ex. it will run for lets say 30 seconds. how would this be done? as well as the time it runs being configable in the config.yml file? i dont want to ask you to just feed me the code, i just need some understanding of how this works?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  6. Offline

    xzKinGzxBuRnzx

    world.setThundering(true); To create a lightning storm
    world.setStorm(true); To create a rain storm
    Setting both of those to false should make it sunny.

    http://jd.bukkit.org/doxygen/dd/daa/interfaceorg_1_1bukkit_1_1World.html
     
  7. Offline

    gus

    this is what i would suggest, but make sure you remember to set the "started" variable back to 0 once the race is over.

    if you wanted to have multiple races at a time, this approach will not work; if you want to go really overboard, i'd suggest creating a "Race" class.

    it could contain a Set of all players involved, an int representing its length, and a boolean for whether it is active or not. (much like the "started" variable that king suggested)

    then you'd use a scheduler to make sure that the race was deactivated after the length had elapsed.

    that's just a suggestion, but it would be clean.


    EDIT: nvm, didn't realize the race was simply an example lol
     
  8. Offline

    J4K3ST3R

    yeah, lol but... the real plugin follows the same concept. i want an admin to be able to start a vote. and player use commands such as /vote yes/no. and the votes are stored in variables. now, i want the /start to onlt last a certain amount of time and i want that time to be configable in the config file. and i want the vote variables to be reset after each vote has ended? how do i do this!!! lol
     
  9. Offline

    gus

    same basic concept. make a "VotingSession" class.

    every "VotingSession" object would have a boolean variable to indicate if it is active or not. if it's not active, players who try to send their vote will get denied.

    you could put this piece of code in the constructor:

    Code:
    myPlugin.getServer().getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
     
        public void run() {
            this.endVote;
        }
    }, [TICKS]L);
    where [TICKS] is the number of ticks the vote will last for. in minecraft, 20 ticks = 1 second. so if the config file value for vote length is in seconds, simply use 20*length for the ticks.

    the endVote method essentially disables the object, setting the "active?" boolean to false. it could possibly display the results and change the weather (i'm guessing that's what you're voting on?) depending on the vote.
     
  10. Offline

    J4K3ST3R

    alright, now im having this problem. the variables that store the votes are not resetting after each vote?
     
  11. Offline

    gus

    did you include code to make them reset? can you just post the class?
     
  12. Offline

    J4K3ST3R

    Code:
    package me.J4K3ST3R.BrightVote;
     
     
    import java.io.File;
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class BrightVote extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static BrightVote plugin;
     
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " [Version: " + pdfFile.getVersion() + " ]" + " Has Been Enabled!");
            new File("plugins/BrightVote").mkdir();
            File configFile = new File("plugins/BrightVote/config.yml");
            if(!configFile.exists()){
                try { configFile.createNewFile();
                } catch(Exception e){ System.out.println("Error when creating config file."); }
            }
        }
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args){
            Player player = (Player) sender;
            int yv = 0, nv = 0;
           
            if(commandlabel.equalsIgnoreCase("Bvt")){
               
                if(args.length == 0){
                    player.sendMessage(ChatColor.DARK_AQUA + "[BrightVote]");
                    player.sendMessage(ChatColor.GREEN + "Usage: " + ChatColor.GOLD + "/Bvt <Day/No>");
                    player.sendMessage(ChatColor.GOLD + "Day = Vote For Sunny!");
                    player.sendMessage(ChatColor.GOLD + "No = Vote For Weather To Remain The Same!");
                    return true;
                }
               
               
                if(args.length > 1){
                    player.sendMessage(ChatColor.RED + "Error: /Bvt <Day/No>");
                    return true;
                }
               
               
                if(args[0].equalsIgnoreCase("Start")){
                    if(player.hasPermission("brightvote.start")){
                    player.getServer().broadcastMessage(ChatColor.DARK_AQUA + "[BrightVote]" + ChatColor.GREEN + "A Vote Has Started! Use /Bvt <Day/No> To Vote!");
                    }else{
                        player.sendMessage(ChatColor.RED + "You Do Not Have Permission To Start A Vote!");
                        return true;
                    }
                }
               
               
                if(args[0].equalsIgnoreCase("Day")){
                    if(player.hasPermission("brightvote.vote")){
                        player.sendMessage(ChatColor.GREEN + "Thanks For Voting!");
                        yv++;
                    }else{
                        player.sendMessage(ChatColor.RED + "You Do Not Have Permission To Vote!");
                        return true;
                    }
                }
               
               
                if(args[0].equalsIgnoreCase("No")){
                    if(player.hasPermission("brightvote.vote")){
                        player.sendMessage(ChatColor.GREEN + "Thanks For Voting!");
                        yv++;
                    }else{
                        player.sendMessage(ChatColor.RED + "You Do Not Have Permission To Vote!");
                        return true;
                    }
                }
               
               
                if(yv >= nv){
                    player.getServer().broadcastMessage(ChatColor.GREEN + "The Yes Votes Won! The Weather And Time Was Set!");
                    player.getWorld().setStorm(false);
                    player.getWorld().setThundering(false);
                    player.getWorld().setTime(0);
                }else if(nv > yv){
                    player.getServer().broadcastMessage(ChatColor.GREEN + "The No Votes Won! The Weather And Time Remains The Same!");
                    return true;
                    }       
                }
            return true;
        }
    }
     
    
     
  13. Offline

    gus

    okay, okay, you got the basics down.

    there is a fundamental problem with your coding logic and flow, though. this is just constructive criticism, don't take it personally. BUT i do suggest you take some of this advice; for what it's worth, i'm taking time out of my day that i don't really have to type this up. mainly because i'd rather help you out here than do boring work.

    i warn you, this is not going to directly solve your problem. call me an ass, but i want you to figure this one out on your own, haha. that's how ya learn.

    i'll deconstruct this and try to hit every point.

    1. general organization.

    it's a good rule in Java coding to split things into different classes. first of all, because it's necessary at times. second of all, it makes for clearer organization. and third, Java is an object oriented programming language. so create your own objects! it makes things much easier.

    creating your own classes is challenging at first, but once you get the hang of it, you'll wonder why you didn't do it in the first place.

    2. consideration of "re-usability".

    look at your plugin right now. you have a "yv" variable and a "nv" variable. that's what you need, and it will work the first time you call a vote, but when you run another vote, you'll still have the old "yv" and "nv" values. now, you could simply reset the variables to 0 once the vote is over, but you don't have code that calls for the vote to end! which leads me to my next point...

    3. programming isn't magic.

    things aren't going to happen by themselves. so you want to make a plugin that starts a vote and ends it eventually? well then you need to write the code that will end the vote eventually.

    4. consider the logical flow.

    this may be the easiest one. just read your code. reading through that the first time, i spotted a problem right off the bat. look at your onCommand method. it makes sense for the most part (though it's very messy)...you're filtering through the different commands...but then, right at the end, you have that final if statement which compares the votes.

    just think about that for a second. because it's down there in the bottom of onCommand, that means it's going to be called every time someone votes, every time someone starts a vote, or every time someone enters an incorrect command. every single time you call a command, it will show you the vote totals. that does not make any sense.



    so in conclusion,
    if you made it this far, i'm impressed. i know what it's like to flounder about, so i hope you'll take these tips and use them.

    if you're not reading this, maybe it's because i am actually being unhelpful...but i could have rewritten this plugin in less time than it took me to type this. so i think i had good intentions...

    good luck, lol. if you're really stumped, i can write some example code.
     
  14. Offline

    J4K3ST3R

    PLEASE/: lol
     
  15. Offline

    Sabersamus

    hey just a suggestion, i read through this fast,

    Code:java
    1. Player player = (Player)sender;


    you didnt check to see if the sender is a player first, you might wanna do that
     
  16. Offline

    gus

    alright, gimme some time. finishing up some work quick.

    THIS IS NOT A FUNCTIONING PLUGIN RIGHT NOW. it doesn't include all necessary methods. i just gave you the gist of it.

    be sure to read comments, i do my best to explain what's happening with them.


    EDIT:
    i left out the voting commands :/. don't have time to get to them now.

    here's the main method:
    Code:
    package com.gus.weatherVote;
     
    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 WeatherVote extends JavaPlugin{
     
        //i will explain this later.
        public VotingSession currentSession = null;
       
        @Override
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
       
       
       
            super.onCommand(sender, command, label, args);
       
            Player player = (Player) sender;
       
            //base command
            if(command.getName().equalsIgnoreCase("bvt")){
           
                //you can combine both conditions which should indicate incorrect usage.
                if(  (args.length == 0)  ||  (args.length > 1)  ){
                    //if you return false wherever a command is used incorrectly, bukkit will display the
                    //command usage that you have entered in plugin.yml. no need to make your own error messages.
                    return false;
                }
           
           
                //this is the important stuff
                if(args[0].equalsIgnoreCase("start")){
               
                    if(player.hasPermission("WeatherVote.start")){
                   
                   
                        //creating a new vote; individual voting sessions are now represented by a new class.
                        new VotingSession(this, player);
                   
                    }
               
                }
           
           
           
           
            }
       
       
       
            return false;
        }
       
    }
    

    and here's a brand new class called VotingSession:

    Code:
    package com.gus.weatherVote;
     
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;
     
    public class VotingSession {
     
        //the length of the vote, in seconds, which is defined in the config.
        int voteLength;
     
        //an object representing the plugin itself
        final WeatherVote plugin;
     
        public VotingSession(WeatherVote weatherVote, Player player){
       
            plugin = weatherVote;
       
            //if there isn't a current voting session (indicated by currentSession being null)
            if(plugin.currentSession == null){
           
                //this is where the voting session is actually defined.
           
                voteLength = 1;//but other than 1, it is actually some value attained from the config.
               
                //setting the plugin object's current session to this specific session.
                plugin.currentSession = this;
           
                //informing players they can vote...
                plugin.getServer().broadcastMessage("Voting has begun! You may vote on the weather now.");
           
                //then, begin a new thread, that will shut the vote down after a time delay.
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
               
                    public void run() {
                        plugin.currentSession.endVote();
                    }
                }, (voteLength*20)/*times 20 because there are 20 ticks per second.*/);
           
           
           
            }else player.sendMessage("Voting already in progress!");
       
       
        }
     
     
        public void endVote(){
       
            //set the plugin's current session back to null.
            plugin.currentSession = null;
       
            //this is where you'd calculate the results and change the weather accordingly; you can do that yourself though.
       
        }
     
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
Thread Status:
Not open for further replies.

Share This Page