CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2011
    Posts
    2

    Can't seem to figure this out...

    I just created an account to ask the users of this forum if they could figure out my problem.

    So, let's get right into it. I'm coding a client for Minecraft.

    First, I declared an ArrayList like so:

    Code:
    public ArrayList<Bind> binds = new ArrayList<Bind>();
    Then, when the user types into a console "bind <key> <cmd>[;<cmd>]" it adds a Bind to the array list, like so:

    Code:
    if(s.startsWith("bind")) {
    	String[] args = s.split(" ");
    	if(args.length > 2) {
    			try {
    					String key = args[1].toUpperCase();
    					int kCode = Keyboard.getKeyIndex(key);
    					
    					String cmd = "";
    					for(int i=2; i<args.length; i++) {
    						cmd += args[i] + " ";
    					}
    					
    					boolean exists = false;
    					if(kCode != Keyboard.KEY_NONE) {
    						for(int i=0; i<binds.size(); i++) {
    							if(kCode == binds.get(i).keyCode) {
    								binds.set(i, new Bind(kCode, cmd));
    								mc.thePlayer.addChatMessage("Overwrote a bind!");
    								exists = true;
    								break;
    							}
    						}
    				}
    					
    					if(!exists) {
    						binds.add(new Bind(kCode, cmd));
    						mc.thePlayer.addChatMessage(binds.get(binds.size() - 1).bind);
    					}
    				} catch (Exception e) {	}
    	} else {
    		mc.thePlayer.addChatMessage("Syntax: bind <key> <cmd>[;<cmd>]");
    	}
    }
    All works fine, until I add another bind. After I add another it just sets both keys to do what the second one was set to do and I'm not sure. :|

  2. #2
    Join Date
    Aug 2011
    Posts
    2

    Re: Can't seem to figure this out...

    Sorry for the double post as I'm sure that's against the rules, but I figured out how to fix it. For some reason .set doesn't work as I assumed it does so I just removed the bind then added another.

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can't seem to figure this out...

    Sorry for the double post as I'm sure that's against the rules
    Not at all, if you figure out the answer for yourself please do tell us as it saves us wasting time trying to solve something you've already solved.

    For some reason .set doesn't work as I assumed
    If you're not sure what a method does read the API docs. For 'ArrayList.set(..)' the docs say

    "Replaces the element at the specified position in this list with the specified element."

    And I can confirm it's worked this way every time I've used it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured