Get online player inventory on Website

Discussion in 'Plugin Requests' started by Rilekt, Aug 16, 2015.

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

    Rilekt

    How to get player's inventory and show it on my Website? Any examples?
     
  2. Online

    timtower Administrator Administrator Moderator

    @Rilekt You need a plugin to copy the inventory to MySQL, and you need a webpage capable of reading that.
     
  3. Offline

    Rilekt

    Can i use JSONAPI to do this?
     
  4. Online

    timtower Administrator Administrator Moderator

    Maybe, but a plugin that writes to the database on regular intervals would keep it more up to date.
     
  5. Offline

    Rilekt

    Also i want to get only online players inventory.. I think MySQL is not good idea
     
  6. Online

    timtower Administrator Administrator Moderator

    @Rilekt Then JSONAPI should do the trick with the right php/javascript stuff. No need for a plugin then.
     
    Rilekt likes this.
  7. Offline

    Rilekt

    So what i want to do? Any documentations..?
     
  8. Online

    timtower Administrator Administrator Moderator

    @Rilekt PHP, get the online players, get their inventory, find a way to display the inventory.
    JSONAPI should have his own documentation somewhere.
     
  9. Offline

    567legodude

    @Rilekt @timtower I feel that this would require using sockets. I've never actually used sockets, because I can't open up more ports to test.
    I would have a plugin on the server and a php file on the site, and when someone logs into the website, the php file connects to the server, and the plugin gives it all the info it needs.
     
  10. Online

    timtower Administrator Administrator Moderator

    @567legodude JSONAPI is pure sockets to be honest. You can also let the plugin create a full html page and show that on the website.
     
  11. Offline

    567legodude

    @timtower That's true, but then you would either have to make it so that your website redirects to the server, or make a php file that loads the content of the server-generated page.

    EDIT: Actually the php part would be the easiest:
    PHP:
    <?php
    include 'server.ip.here';
     
  12. Online

    timtower Administrator Administrator Moderator

    @567legodude
    Code:
    <iframe>
    Or echo(file_get_content(<remote url>))
     
  13. Offline

    567legodude

    @timtower Both of our methods work. I think I might actually try that sometime just to see if I can get it to work.
     
  14. Offline

    Rilekt

    Soo... How to get this inventory using JSONAPI? :mad:
     
  15. Online

    timtower Administrator Administrator Moderator

  16. Offline

    567legodude

    @timtower I want to say that I did get it to work. I made a socket on the plugin and when connecting to the socket from the URL it showed me the number of players online.

    The only thing I couldn't figure out was how to connect and show the page with php; The methods we mentioned didn't work when trying to load the contents that the socket returned. I tried doing socket stuff on php but I couldn't figure that out.
     
  17. Online

    timtower Administrator Administrator Moderator

  18. Offline

    au2001

    @Rilekt Configure JSONAPI on your server, and then add a invsee.json file in the methods folder :
    Code:
    {
        "name" : "Inventory View",
        "namespace" : "invsee",
        "depends" : [
            "JSONAPI"
        ],
        "methods" : [
            {
                "name": "getInventorySlot",
                "desc": "Gets the item type the player has the slot specified.",
                "returns": ["String", "Type of the item in the specified slot"],
                "call": "this.getPlayerExact(0).getInventory().getItem(1).getType().getName()",
                "methods": [
                    ["String", "The name of the player"],
                    ["int", "The slot id, starting from 0"]
                ]
            },
            {
                "name": "getInventoryContents",
                "desc": "Gets each itemstack in the specified player's inventory.",
                "returns": ["ItemStack[]", "Types of the items in the player's inventory"],
                "call": "this.getPlayerExact(0).getInventory().getContents()",
                "methods": [
                    ["String", "The name of the player"]
                ]
            },
            {
                "name": "getArmorContents",
                "desc": "Gets each itemtack in the specified player's armor.",
                "returns": ["ItemStack[]", "The itemstacks in the player's armor"],
                "call": "this.getPlayerExact(0).getInventory().getArmorContents()",
                "methods": [
                    ["String", "The name of the player"]
                ]
            }
        ]
    }
    This will allow you to retrieve the contents of a player's inventory, remotely through JSONAPI.
    If you want to change his inventory, there are default commands in the methods.json file.
    And if you want to get the durability of an item, create another method to do that.
    I think there's also a default method to get the item's enchantments, not sure though.

    Now you will have to download JSONAPI's PHP SDK, which you can get on github here :
    https://github.com/alecgorge/jsonapi/blob/master/sdk/php/JSONAPI.php

    So once that's done, create a .php file (invsee.php for example) right next to JSONAPI's SDK :
    PHP:
    <?php

    // Include JSONAPI's PHP SDK
    require_once("JSONAPI.php");

    $host "mc.example.com"// The IP of your server
    $port 20059// The port JSONAPI is running on
    $user "admin"// The admin username to login
    $pass "changeme"// The password of the user
    // $salt = "optional"; // The salt to connect to your server, optional

    try {
        
    // Create the $json object by connecting to the server with JSONAPI
        
    $json = new JSONAPI($host$port$user$pass /* $salt */);

        
    $result $json->call("getPlayerNames"); // Get the online players

        
    if ($array["result"] == "success" && array_key_exists("success"$result) && is_array($result["success"])) { // If the call worked
            
    foreach ($result["success"] as $name) { // For each player name
                
    echo "<br/><br/>--------------------------------------------------------------------------<br/><br/>";
                echo 
    "Showing contents for player " $name ":<br/><br/>";
                
    $inventory $json->call("getInventoryContents"); // Call the method we created before
                
    if ($inventory["result"] == "success" && array_key_exists("success"$inventory) && is_array($inventory["success"])) { // If the call worked
                    
    foreach ($inventory["success"] as $item) { // For each itemstack in his inventory
                        
    echo "<br/>Item in the slot " $i++ . " for player " $name ":<br/>";
                        
    var_dump($item); // Display the item on the page.
                    
    }
                } else die(
    "Error while fetching player's inventory: " $array["error"]); // Display error and exit script
            
    }
        } else die(
    "Error while fetching player list: " $array["error"]); // Display error and exit script
    } catch (Exception $e) { // Check for an error while connecting to the server
        
    var_dump($e);
    }

    ?>
    I added comments so that you understand what I wanted to do on each line, it will be easier to debug ;)

    PS: I have never worked with JSONAPI before, and don't have anything to test it on so it is really unlikely it works just like that, you will probably have to debug it and work a bit more on it to have it work...
    Hope I helped you though :)
     
    Last edited: Aug 26, 2015
    Rilekt likes this.
  19. Offline

    Rilekt

    @au2001
    Owwww great!
    I have an error:
    Error while fetching player's inventory: Caught exception: java.lang.Exception: Incorrect number of args: gave 0 ([]), expected 3 for method getInventoryContents at com.alecgorge.minecraft.jsonapi.dynamic.Caller.call(Caller.java:64) at com.alecgorge.minecraft.jsonapi.api.v2.JSONResponse.serveAPICall(JSONResponse.java:131) at com.alecgorge.minecraft.jsonapi.api.v2.JSONResponse.getJSONObject(JSONResponse.java:76) at com.alecgorge.minecraft.jsonapi.api.v2.APIv2Handler.call(APIv2Handler.java:84) at com.alecgorge.minecraft.jsonapi.api.v2.APIv2Handler.serve(APIv2Handler.java:52) at com.alecgorge.minecraft.jsonapi.JSONServer.serve(JSONServer.java:248) at com.alecgorge.minecraft.jsonapi.NanoHTTPD$HTTPSession.run(NanoHTTPD.java:544) at java.lang.Thread.run(Thread.java:745)
     
  20. Offline

    au2001

    @Rilekt Oh, yes sorry. I forgot to pass the arguments to JSONAPI, you have to use this:
    PHP:
    $json->call("getInventoryContents", array($name));
     
    Rilekt likes this.
  21. Offline

    Rilekt

    @au2001
    Emm, and the next error:
    Error while fetching player's inventory: Caught exception: java.lang.ArrayIndexOutOfBoundsException: 0 at com.alecgorge.minecraft.jsonapi.dynamic.Call.sigForIndices(Call.java:189) at com.alecgorge.minecraft.jsonapi.dynamic.Call.call(Call.java:98) at com.alecgorge.minecraft.jsonapi.dynamic.Caller$1.call(Caller.java:91) at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftFuture.run(CraftFuture.java:89) at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:722) at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:653) at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:556) at java.lang.Thread.run(Thread.java:745)
     
  22. Offline

    au2001

    @Rilekt Is this error happening after you changed the code I told you?
     
    Rilekt likes this.
  23. Offline

    Rilekt

    @au2001 Yes after i dont have this error.
     
  24. Offline

    au2001

    @timtower This has been moved to Plugin Requests?
    He doesn't really want someone to make the plugin for him...
    I think it was more appropriate in Plugin Development :|
     
    Rilekt likes this.
  25. Online

    timtower Administrator Administrator Moderator

    Nope, this wasn't moved.
     
  26. Offline

    Rilekt

    @au2001
    I added this methods in methods.json, i can't add this in method folder beacuse my server doesn't see the invsee.json file :confused:
     
  27. Offline

    Rilekt

    Any ideas to repair it?
     
    Last edited: Aug 27, 2015
  28. Offline

    au2001

    @Rilekt Try moving the methods to the methods.json file, should work but isn't recommended I guess.
    I don't know how JSONAPI works so I don't really have an idea on how to make custom .json method files...

    PS: Please tag me/the person you're talking to if you want him to see your post ;)
     
    Rilekt likes this.
  29. Offline

    Rilekt

    //Refresh :(
     
  30. Offline

    Rilekt

    //Refresh
     
Thread Status:
Not open for further replies.

Share This Page