Thread: Safety Checks for Exchanging Items Between Containers

Results 1 to 5 of 5
  1. #1 Safety Checks for Exchanging Items Between Containers 
    Registered Member
    Dexter Morgan's Avatar
    Join Date
    Nov 2008
    Age
    14
    Posts
    4,331
    Thanks given
    917
    Thanks received
    649
    Rep Power
    2893
    Not really a feature. Not something so great. Most probably for those who know what they are doing.

    Recently I been writing content such as Trading Post, Auction system, and a Gambling system which include exchanging items between containers and I find that there are very necessary checks to be included so I created a method that does it all.

    If you know other checks that are necessary to be included then please comment. If you know a better way of dealing with the exchange then please let me know

    It returns an inspected amount that is valid and can be exchanged

    Code:
    	/**
    	 * Verifying item to exchange in containers
    	 * 
    	 * @param to the container to exchange to
    	 * @param item the item
    	 * @param slot the slot
    	 * @return the parsed amount
    	 */
    	public int verifyContainerExchange(ItemContainer to, Item item, int slot) {
    		ItemDefinition def = item.getDefinition();
    		if (def == null || isEmpty()) {
    			return 0;
    		}
    		if (!contains(item.getId())) {
    			return 0;
    		}
    		Item slotItem = get(slot);
    		if (slotItem == null) {
    			return 0;
    		}
    		if (slotItem.getId() != item.getId()) {
    			return 0;
    		}
    		if (slotItem.getAmount() < 1 || item.getAmount() < 1) {
    			return 0;
    		}
    		int amount = item.getAmount();
    		int maxAmount = item.getDefinition().isStackable() ? slotItem.getAmount() : getAmount(item.getId());
    		if (amount > maxAmount) {
    			amount = maxAmount;
    		}
    		long existing = to.getAmount(item.getId());
    		if (existing + item.getAmount() > Integer.MAX_VALUE) {
    			amount = (int) (Integer.MAX_VALUE - existing);
    		}
    		if (amount < 1) {
    			return 0;
    		}
    		if (!def.isStackable() && to.getFreeSlots() < amount) {
    			return to.getFreeSlots();
    		}
    		if (def.isStackable() && to.getFreeSlots() == 0 && !to.contains(item.getId())) {
    			return 0;
    		}
    		return amount;
    	}
    [Only registered and activated users can see links. ]
    Quote Originally Posted by UberNation View Post
    Did you update the quantum network sockets to match the polarity of the wilderness counter levels in the Boolean that divides the wilderness level by zero?
    Reply With Quote  
     

  2. #2  
    nbness2#5894

    nbness2's Avatar
    Join Date
    Aug 2011
    Posts
    599
    Thanks given
    224
    Thanks received
    96
    Rep Power
    254
    Some documentation is good, but do you really need to comment everything imagineable? It actually makes the code less appealing to look at. It's a lot of unnecessary clutter especially when, like in this case, the logic, variables and functions are self explanatory.

    Like... come on man!
    Code:
    /*
     * The amount
     */
    int amount = item.getAmount();
    The logic in the code is fine, but you need to chill on the other stuff. Over documentation (good code is self documenting, and I could understand exactly what was going on after I removed all the comments) and over formatting (like the seemingly random newline indent at maxAmount) makes code no fun to look at.
    Discord: nbness2#5894
    Python Framework (???): [Only registered and activated users can see links. ]!
    NBX: [Only registered and activated users can see links. ]!
    DropTable DSL: [Only registered and activated users can see links. ]!
    Make your Inventory easier and safer to work with. Introducing Container: [Only registered and activated users can see links. ]
    Reply With Quote  
     

  3. Thankful users:


  4. #3  
    Registered Member
    Join Date
    Oct 2012
    Posts
    92
    Thanks given
    15
    Thanks received
    31
    Rep Power
    21
    Quote Originally Posted by nbness2 View Post
    Some documentation is good, but do you really need to comment everything imagineable? It actually makes the code less appealing to look at. It's a lot of unnecessary clutter especially when, like in this case, the logic, variables and functions are self explanatory.

    Like... come on man!
    Code:
    /*
     * The amount
     */
    int amount = item.getAmount();
    The logic in the code is fine, but you need to chill on the other stuff. Over documentation (good code is self documenting, and I could understand exactly what was going on after I removed all the comments) and over formatting (like the seemingly random newline indent at maxAmount) makes code no fun to look at.
    This for sure.

    Also documentation like this should just be

    //The amount

    Instead of

    /*
    * The amount
    */

    A lot of the documentation is useless except for if statements like this

    Code:
    if (def.isStackable() && to.getFreeSlots() == 0 && !to.contains(item.getId())) {
    Because it would be faster to interpret what this if statement does by reading a documentation instead of reading the code itself
    NandoStudios
    Reply With Quote  
     

  5. Thankful users:


  6. #4  
    Registered Member
    Dexter Morgan's Avatar
    Join Date
    Nov 2008
    Age
    14
    Posts
    4,331
    Thanks given
    917
    Thanks received
    649
    Rep Power
    2893
    Quote Originally Posted by Valiii View Post
    This for sure.

    Also documentation like this should just be

    //The amount

    Instead of

    /*
    * The amount
    */

    A lot of the documentation is useless except for if statements like this

    Code:
    if (def.isStackable() && to.getFreeSlots() == 0 && !to.contains(item.getId())) {
    Because it would be faster to interpret what this if statement does by reading a documentation instead of reading the code itself
    Cheers. Appreciated. Thanks for helping me learn.
    [Only registered and activated users can see links. ]
    Quote Originally Posted by UberNation View Post
    Did you update the quantum network sockets to match the polarity of the wilderness counter levels in the Boolean that divides the wilderness level by zero?
    Reply With Quote  
     

  7. Thankful user:


  8. #5  
    HYBRID JEFE
    Brett the Don's Avatar
    Join Date
    Dec 2009
    Posts
    1,801
    Thanks given
    254
    Thanks received
    297
    Rep Power
    575
    None the less good job and might use, thank you.

    All of humanity's problems stem from man's
    inability to sit quietly in a room by himself
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

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

Similar Threads

  1. Replies: 26
    Last Post: 10-30-2016, 04:13 PM
  2. [562] Check for items help
    By snwspeckle in forum Help
    Replies: 0
    Last Post: 07-25-2010, 12:30 AM
  3. Easyest method to check for runes / other items
    By blackaddiction in forum Snippets
    Replies: 15
    Last Post: 01-19-2010, 04:32 PM
  4. Replies: 3
    Last Post: 05-09-2009, 07:44 AM
  5. Easier way to check for preimum items
    By Cascade in forum Configuration
    Replies: 4
    Last Post: 12-10-2008, 03:42 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •