Thread: If contains?

Results 1 to 6 of 6
  1. #1 If contains? 
    Registered Member
    Join Date
    Dec 2015
    Posts
    68
    Thanks given
    5
    Thanks received
    1
    Rep Power
    61
    Hey hoping someone can help me as it'll probably be quite simple for most people!

    I've got an item which you can buy, when you open it I've made it give you X amount of runes if you have 3 inventory spaces free, but if your inventory already contains some of the runes needed then you don't need 3 spaces free, how would I implement this?

    Example:

    I already have death and blood runes in my inventory, so really I only need 1 space free now?

    Spoiler for Show:
    case 20435:
    if (player.getInventory().getFreeSlots() < 3) {
    player.getPacketSender().sendMessage("You need three spaces free to open a barrage box.");
    return;
    }
    player.getInventory().add(565, 2000);
    player.getInventory().add(555, 6000);
    player.getInventory().add(560, 4000);
    player.getInventory().delete(20435, 1);


    break;
    Quote Originally Posted by OG KingFox View Post
    vouch, so far has been good to me
    Quote Originally Posted by Blaka View Post
    Vouch for my man craig.
    Quote Originally Posted by JustsayGang View Post
    Vouch. I Worked for Craig , everything went smooth.
    Quote Originally Posted by Goody View Post
    Vouch, i've worked with craig before
    Attached image
    Reply With Quote  
     

  2. #2  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Code:
    final Inventory inventory = player.getInventory();
    final int bloods = inventory.getAmountOf(565);
    final int deaths = inventory.getAmountOf(560);
    final int waters = inventory.getAmountOf(555);
    if (bloods + 2000 < 0 || deaths + 4000 < 0 || waters + 6000 < 0) {//prevents overflow.
    player.sendMessage("You're holding too many runes.");
    return;
    }
    int spaceRequired = -1;//because we delete the box first, we can start off at -1. So if the inventory contains none of the items, we delete the box - opening up a space for the first item, and require two spaces to add the two other items.
    if (bloods == 0)
    spaceRequired++;
    if (deaths == 0)
    spaceRequired++;
    if (waters == 0)
    spaceRequired++;
    if (inventory.getFreeSlots() < spaceRequired) {
    player.sendMessage("You need more free space to open this.");
    return;
    }
    player.getInventory().delete(20435, 1);//always delete first - this way you open up a space before adding any furthermore items.
    player.getInventory().add(565, 2000);
    player.getInventory().add(555, 6000);
    player.getInventory().add(560, 4000);
    This is the general idea behind it. However this shit should be automatically checked by your container system. Writing such code every time it is required is just random nonsense. Callbacks are a nice option to prevent overflow/lack of space too.
    E.g. being able to(this line is how it works on my server):
    player.getInventory().addItem(new Item(11802, 5)).onFailure(remainderItem -> World.spawnFloorItem(remainderItems, player, location));
    Attached image
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Dec 2015
    Posts
    68
    Thanks given
    5
    Thanks received
    1
    Rep Power
    61
    Quote Originally Posted by Kris View Post
    Code:
    final Inventory inventory = player.getInventory();
    final int bloods = inventory.getAmountOf(565);
    final int deaths = inventory.getAmountOf(560);
    final int waters = inventory.getAmountOf(555);
    if (bloods + 2000 < 0 || deaths + 4000 < 0 || waters + 6000 < 0) {//prevents overflow.
    player.sendMessage("You're holding too many runes.");
    return;
    }
    final int spaceRequired = -1;//because we delete the box first, we can start off at -1. So if the inventory contains none of the items, we delete the box - opening up a space for the first item, and require two spaces to add the two other items.
    if (bloods == 0)
    spaceRequired++;
    if (deaths == 0)
    spaceRequired++;
    if (waters == 0)
    spaceRequired++;
    if (inventory.getFreeSlots() < spaceRequired) {
    player.sendMessage("You need more free space to open this.");
    return;
    }
    player.getInventory().delete(20435, 1);//always delete first - this way you open up a space before adding any furthermore items.
    player.getInventory().add(565, 2000);
    player.getInventory().add(555, 6000);
    player.getInventory().add(560, 4000);
    This is the general idea behind it. However this shit should be automatically checked by your container system. Writing such code every time it is required is just random nonsense. Callbacks are a nice option to prevent overflow/lack of space too.
    E.g. being able to(this line is how it works on my server):
    player.getInventory().addItem(new Item(11802, 5)).onFailure(remainderItem -> World.spawnFloorItem(remainderItems, player, location));
    Thank you mate really appreciate it.

    Do you do any paid work?
    Quote Originally Posted by OG KingFox View Post
    vouch, so far has been good to me
    Quote Originally Posted by Blaka View Post
    Vouch for my man craig.
    Quote Originally Posted by JustsayGang View Post
    Vouch. I Worked for Craig , everything went smooth.
    Quote Originally Posted by Goody View Post
    Vouch, i've worked with craig before
    Attached image
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Sep 2014
    Posts
    42
    Thanks given
    6
    Thanks received
    19
    Rep Power
    37
    Quote Originally Posted by Kris View Post
    E.g. being able to(this line is how it works on my server):
    player.getInventory().addItem(new Item(11802, 5)).onFailure(remainderItem -> World.spawnFloorItem(remainderItems, player, location));
    What does your Inventory method addItem(Item i) return?
    Some derived functional interface type?

    You could perhaps consider organizing your callback functions internally in your
    Inventory class in order to slightly increase readability in external calls, i.e.

    Code:
    // Somewhere external to Inventory.java ...
    player.getInventory().addItem(new Item(995, 1), CallbackPolicy.AT_FEET);
    
    // In Inventory.jjava
    public void addItem(Item i, CallbackPolicy policy) {
      if(policy == CallbackPolicy.AT_FEET)
        addItemDropRemainder(i);
      else doSomeOtherAddItem(i);
    }
    
    private void addItemDropRemainder(Item i) {
      // your onFailure logic here.
    }
    You can of course have many different CallbackPolicy types, but I can't imagine
    needing more than 2-3.

    In any event I like your functional approach.

    Edit:

    Extending upon this, if you wanted to really get fancy for custom callbacks...

    Code:
    //externally
    player.getInventory().addItem(new Item(995, 345), remainder -> {
      Logging.info("There was some left over... " + remainder.getAmount());
    })
    
    // internally
    public void addItem(Item i, Consumer<Item> callback) {
      // do logic to get remainder that couldn't fit...
      Item remainder = ...
      callback.accept(remainder);
    }
    Reply With Quote  
     

  5. Thankful users:


  6. #5  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    Quote Originally Posted by dlo3 View Post
    What does your Inventory method addItem(Item i) return?
    Some derived functional interface type?

    You could perhaps consider organizing your callback functions internally in your
    Inventory class in order to slightly increase readability in external calls, i.e.

    Code:
    // Somewhere external to Inventory.java ...
    player.getInventory().addItem(new Item(995, 1), CallbackPolicy.AT_FEET);
    
    // In Inventory.jjava
    public void addItem(Item i, CallbackPolicy policy) {
      if(policy == CallbackPolicy.AT_FEET)
        addItemDropRemainder(i);
      else doSomeOtherAddItem(i);
    }
    
    private void addItemDropRemainder(Item i) {
      // your onFailure logic here.
    }
    You can of course have many different CallbackPolicy types, but I can't imagine
    needing more than 2-3.

    In any event I like your functional approach.
    Returns an object containing information about the "transaction" - the item used, the transaction type(add, remove w/e), amount requested/succeeded, callback option for the unsuccessful amount.
    Your ideas aren't bad here either, I might implement that actually, seeing as most often I throw the item into either bank or under feet, or both in a chain-reaction if applicable.
    Attached image
    Reply With Quote  
     

  7. #6  
    Registered Member
    Join Date
    Sep 2014
    Posts
    42
    Thanks given
    6
    Thanks received
    19
    Rep Power
    37
    Quote Originally Posted by Kris View Post
    Returns an object containing information about the "transaction" - the item used, the transaction type(add, remove w/e), amount requested/succeeded, callback option for the unsuccessful amount.
    Your ideas aren't bad here either, I might implement that actually, seeing as most often I throw the item into either bank or under feet, or both in a chain-reaction if applicable.
    See edit - otherwise very nice implementation.
    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. Replies: 4
    Last Post: 07-22-2013, 02:03 AM
  2. Checking if file contains
    By rexz0rd in forum Application Development
    Replies: 6
    Last Post: 11-20-2011, 09:06 PM
  3. Replies: 5
    Last Post: 06-16-2011, 10:43 AM
  4. Replies: 14
    Last Post: 01-08-2011, 02:52 AM
  5. Replies: 5
    Last Post: 03-30-2009, 03:17 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
  •