[SOLVED] get player hotbar inventory

Discussion in 'Plugin Development' started by cool_s, Mar 30, 2012.

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

    cool_s

    I'm creating a fork based on the MapMarkers plugin by TJ09 that also returns the player inventory.

    I know how to get the complete inventory, but how can I check if an item is in the hotbar?
    The reason for this is that I want to show the player inventory on a website (see here).

    Any help?

    Otherwise... would it be possible to get the slot location of a ItemStack?
    Here's the code I use to get the inventory:

    Code:
    PlayerInventory inventory = p.getInventory();
    int i = 0;
     
    JSONObject out = new JSONObject();
     
    JSONObject mainInv = new JSONObject();
     
    for (ItemStack stack : inventory.getContents()) {
        if (stack != null) {
            ++i;
            JSONObject inv = new JSONObject();
            inv.put("amount",Integer.valueOf(stack.getAmount()));
            MaterialData stackData = stack.getData();
            inv.put("type", stack.getTypeId() + "-" + stack.getDurability());
            inv.put("name", stackData.toString());
            mainInv.put(Integer.valueOf(i), inv);
        }
    }
    out.put("inventory", mainInv);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  2. As far as I know the hotbar are id 0 to 8 from the inventory of the player.

    Short piece of code:
    Code:
    inv = player.getInventory(); // The player's inventory
    item1 = inv.getItem(0); // First slot of the hotbar
    item8 = inv.getItem(8); // Last slot of the hotbar
    
    Hope this helps :)
     
  3. Offline

    cool_s

    So 0 -35 would be the complete player inventory...
    Let's see if I can get this to work :)

    woot!!!

    fixed it like this:
    Code:
    PlayerInventory inventory = p.getInventory();
                JSONObject mainInv = new JSONObject();
             
                for (int i=35;i>0;i--) {
                    ItemStack stack = inventory.getItem(i);
                    JSONObject inv = new JSONObject();
                    inv.put("id",Integer.valueOf(i));
                 
                    if (stack != null) {
                        inv.put("amount",Integer.valueOf(stack.getAmount()));
                        MaterialData stackData = stack.getData();
                        inv.put("type", stack.getTypeId() + "-" + stack.getDurability());
                        inv.put("name", stackData.toString());
                        mainInv.put(Integer.valueOf(i), inv);
                    }
                    else mainInv.put(Integer.valueOf(i), inv);
                }
    and this is the json file I get:
    Code:
    "[{"timestamp":"20120330 12:25:27","id":4,"inventory":{"1":{"amount":1,"id":1,"name":"SANDSTONE(0)","type":"24-0"},"2":{"amount":1,"id":2,"name":"SANDSTONE STEP(1)","type":"44-1"},"3":{"amount":1,"id":3,"name":"COBBLESTONE(0)","type":"4-0"},"4":{"amount":1,"id":4,"name":"NETHER_BRICK(0)","type":"112-0"},"5":{"amount":1,"id":5,"name":"VINE","type":"106-0"},"6":{"amount":1,"id":6,"name":"BIRCH SAPLING(2)","type":"6-2"},"7":{"amount":1,"id":7,"name":"REDWOOD SAPLING(1)","type":"6-1"},"8":{"amount":1,"id":8,"name":"GENERIC SAPLING(0)","type":"6-0"},"9":{"amount":64,"id":9,"name":"SAND(0)","type":"12-0"},"10":{"amount":64,"id":10,"name":"SAND(0)","type":"12-0"},"11":{"amount":1,"id":11,"name":"SAND(0)","type":"12-0"},"12":{"id":12},"13":{"id":13},"14":{"id":14},"15":{"id":15},"17":{"id":17},"16":{"id":16},"19":{"id":19},"18":{"id":18},"21":{"id":21},"20":{"id":20},"23":{"id":23},"22":{"id":22},"25":{"id":25},"24":{"id":24},"27":{"id":27},"26":{"id":26},"29":{"id":29},"28":{"id":28},"31":{"id":31},"30":{"id":30},"34":{"id":34},"35":{"id":35},"32":{"id":32},"33":{"id":33}},"z":295.91841093588,"msg":"cool_s","y":107,"world":"Ruins of Obsidian","x":-39.854859714293}]"
    and with this PHP script I get a nice inventory overview:
    PHP:
    $cInventory '';
                foreach (
    $aPlayerInventory[$cPlayer] as $aInv) {
                    if (
    $aInv['id'] > 8) {
                        if (empty(
    $aInv['type']) || !isset($aInv['type'])) {
                            
    $cInventory .= '<div class=\'sprites items-1-9-0-0\'></div>';
                        } else {
                            
    $cInventory .= '<div class=\'sprites items-1-9-'.$aInv['type'].'\'><span>'.$aInv['amount'].'</span></div>';
                        }
                    }
                }
                
    $cInventory .= '<div class=\'clear\'></div><hr />';
                foreach (
    $aPlayerInventory[$cPlayer] as $aInv) {
                    if (
    $aInv['id'] <= 8) {
                        if (empty(
    $aInv['type']) || !isset($aInv['type'])) {
                            
    $cInventory .= '<div class=\'sprites items-1-9-0-0\'></div>';
                        } else {
                            
    $cInventory .= '<div class=\'sprites items-1-9-'.$aInv['type'].'\'><span>'.$aInv['amount'].'</span></div>';
                        }
                    }
                }
    And this is the end result (hover over online (green) avatars):
    http://mc.m-o-p-s.nl/Ruins_of_Obsidian/custompage/members

    Now I only have to edit the css part of it all... so that I can show ALL the items (I'm using a list from 1.8 for this test)

    So I was hoping it was done... but no... it could not be :(

    Seems like the mainInv JSONObject doesn't save itseld in the right order.
    Is there a way to sort this object?

    This is my current JSON return:
    Code:
    "[
        {"timestamp":"20120330 14:54:14",
        "id":4,
        "inventory":{
            "0":{"amount":4,"id":0,"type":"13-0"},
            "1":{"amount":1,"id":1,"type":"24-0"},
            "2":{"amount":1,"id":2,"type":"44-1"},
            "3":{"id":3},
            "4":{"id":4},
            "5":{"amount":1,"id":5,"type":"106-0"},
            "6":{"id":6},
            "7":{"amount":1,"id":7,"type":"6-1"},
            "8":{"amount":1,"id":8,"type":"6-0"},
            "9":{"amount":64,"id":9,"type":"12-0"},
            "10":{"id":10},
            "11":{"id":11},
            "12":{"amount":1,"id":12,"type":"1-0"},
            "13":{"id":13},
            "14":{"id":14},
            "15":{"amount":1,"id":15,"type":"4-0"},
            "17":{"amount":64,"id":17,"type":"12-0"},
            "16":{"id":16},
            "19":{"id":19},
            "18":{"id":18},
            "21":{"id":21},
            "20":{"amount":1,"id":20,"type":"6-2"},
            "23":{"amount":1,"id":23,"type":"2-0"},
            "22":{"id":22},
            "25":{"id":25},
            "24":{"id":24},
            "27":{"id":27},
            "26":{"id":26},
            "29":{"amount":1,"id":29,"type":"12-0"},
            "28":{"id":28},
            "31":{"id":31},
            "30":{"id":30},
            "34":{"amount":1,"id":34,"type":"112-0"},
            "35":{"id":35},
            "32":{"id":32},
            "33":{"id":33}
        },
        "z":290.44959605355,
        "msg":"cool_s","y":107,
        "world":"Ruins of Obsidian",
        "x":-57.832346860163}
    ]"
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  4. Offline

    cool_s

    I fixed it, using my PHP skills (more experienced with that).

    since the JSONObject, containing the inventory, will be handled by PHP to show the inventory.
    I added this line before I loop through the array: ksort($aPlayerInventory[$cPlayer]); this will sort the array by keys :)
     
Thread Status:
Not open for further replies.

Share This Page