Thread: Whats a better way to write this?

Results 1 to 5 of 5
  1. #1 Whats a better way to write this? 
    Extreme Donator

    Joy.'s Avatar
    Join Date
    Jul 2011
    Posts
    852
    Thanks given
    153
    Thanks received
    115
    Rep Power
    124
    I'm making it so ironman accounts can buy specific items from the grand exchange and this is how I currently have it set up.
    The code looks ugly, and I know there's a better way to write this. Any ideas?

    Code:
    if (player.isIronman() && buy) {
    			if (itemId == 385 || itemId == 1349) { //TESTING IDS
    				
    			} else {
    				player.getPackets().sendGameMessage("You cannot buy that item in Ironman mode.", false);
    				return;
    			}
    		}
    Reply With Quote  
     

  2. #2  
    Registered Member
    Grant_'s Avatar
    Join Date
    Aug 2014
    Posts
    479
    Thanks given
    92
    Thanks received
    101
    Rep Power
    91
    Quote Originally Posted by Joy. View Post
    I'm making it so ironman accounts can buy specific items from the grand exchange and this is how I currently have it set up.
    The code looks ugly, and I know there's a better way to write this. Any ideas?

    Code:
    if (player.isIronman() && buy) {
    			if (itemId == 385 || itemId == 1349) { //TESTING IDS
    				
    			} else {
    				player.getPackets().sendGameMessage("You cannot buy that item in Ironman mode.", false);
    				return;
    			}
    		}
    A few options that came to mind.
    * You can store all ID's in some sort of List object. Then when the player goes to buy an item, if they are an ironman, check if that item is contained in the list and then block them from buying.

    * Second option would be to maybe update the GameItem container you use to have a canIronmanPurchase flag or something, this way you aren't looping at all but just doing a conditional check on the item itself. This however would most likely require you to flag every single item in your configs. If you could find a way to default set the flag to false for every item config then this would probably be the best as you could just go set flag = true for all the ones you want blocked.
    Reply With Quote  
     

  3. #3  
    Extreme Donator

    Joy.'s Avatar
    Join Date
    Jul 2011
    Posts
    852
    Thanks given
    153
    Thanks received
    115
    Rep Power
    124
    Quote Originally Posted by Grant_ View Post
    A few options that came to mind.
    * You can store all ID's in some sort of List object. Then when the player goes to buy an item, if they are an ironman, check if that item is contained in the list and then block them from buying.

    * Second option would be to maybe update the GameItem container you use to have a canIronmanPurchase flag or something, this way you aren't looping at all but just doing a conditional check on the item itself. This however would most likely require you to flag every single item in your configs. If you could find a way to default set the flag to false for every item config then this would probably be the best as you could just go set flag = true for all the ones you want blocked.
    I made this in a couple seconds on my phone so I don't know if it would work, is this what you're talking about?

    Code:
    if (player.isIronman() && buy) {
    			for (int i = 0; i < IRONMAN_ITEMS.length; i++) {
    				if (itemId != IRONMAN_ITEMS.length) {
    					player.getPackets().sendGameMessage("You cannot buy that item in Ironman mode.", false);
    					return;
    				}	
    			}
    I know I need to add IRONMAN_ITEMS { 1, 12, 9138 blah blah }
    Reply With Quote  
     

  4. #4  




    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    25
    Posts
    16,053
    Thanks given
    7,047
    Thanks received
    11,828
    Rep Power
    5000
    Quote Originally Posted by Joy. View Post
    Code:
    for (int i = 0; i < IRONMAN_ITEMS.length; i++) {
    	if (itemId != IRONMAN_ITEMS.length) {
    		player.getPackets().sendGameMessage("You cannot buy that item in Ironman mode.", false);
    		return;
    	}	
    }
    This is wrong. You're comparing the itemId to the length of the IRONMAN_ITEMS array, rather than the item at this index within the array.

    It should be

    Code:
    for (int i = 0; i < IRONMAN_ITEMS.length; i++) {
    	if (itemId != IRONMAN_ITEMS[i]) {
    		player.getPackets().sendGameMessage("You cannot buy that item in Ironman mode.", false);
    		return;
    	}	
    }
    Last edited by Scu11; 11-04-2019 at 10:24 PM.

    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  5. Thankful users:


  6. #5  
    Extreme Donator

    Joy.'s Avatar
    Join Date
    Jul 2011
    Posts
    852
    Thanks given
    153
    Thanks received
    115
    Rep Power
    124
    Quote Originally Posted by Scu11 View Post
    This is wrong. You're comparing the itemId to the length of the IRONMAN_ITEMS array, rather than the item at this index within the array.

    It should be

    Code:
    for (int i = 0; i < IRONMAN_ITEMS.length; i++) {
    	if (itemId != IRONMAN_ITEMS[i]) {
    		player.getPackets().sendGameMessage("You cannot buy that item in Ironman mode.", false);
    		return;
    	}	
    }
    I had a feeling I wrote that wrong lol. Thanks
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. a better way to write this code
    By NeilG in forum Help
    Replies: 2
    Last Post: 12-06-2017, 12:20 PM
  2. better way to write this
    By Warlord889 in forum Help
    Replies: 2
    Last Post: 01-15-2016, 03:07 AM
  3. Better way to write this?
    By Capsize in forum Help
    Replies: 3
    Last Post: 08-18-2014, 09:24 PM
  4. Better way to do this?
    By TheeeRash in forum Help
    Replies: 6
    Last Post: 12-24-2011, 05:17 PM
  5. Better ways of writing this?
    By jameskmonger in forum Help
    Replies: 15
    Last Post: 05-05-2010, 05:23 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •