Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Player Inventory Issues #115

Open
darkwolfie37 opened this issue Jan 6, 2025 · 0 comments
Open

Player Inventory Issues #115

darkwolfie37 opened this issue Jan 6, 2025 · 0 comments

Comments

@darkwolfie37
Copy link

darkwolfie37 commented Jan 6, 2025

When trying to set the player's inventory to a inventory stored at logout, the inventory cannot be restored. even though this is saved under a file.

When restoring contents of the inventory manually, the enchants do not get reapplied to the items.

Example save file:

{
  "data": {
    "player": {
      "name": "DarkWolfieVT",
      "uuid": "f902b8bc-e495-4cf6-8a1a-74b815cbcd04"
    },
    "save": {
      "stats": {
        "maxMana": 50.0,
        "maxHealth": 20.0
      },
      "class": "Paladin",
      "money": 0.0,
      "lastLocation": {
        "x": -267.8069469601242,
        "y": 72.0,
        "z": -824.1704415054036
      },
      "inventory": {
        "..": "org.bukkit.craftbukkit.inventory.CraftInventoryPlayer",
        "title": "Crafting",
        "type": "PLAYER",
        "holder": "DarkWolfieVT",
        "contents": {
          "Slot 0": null,
          "Slot 1": null,
          "Slot 2": null,
          "Slot 3": null,
          "Slot 4": {
            "==": "org.bukkit.inventory.ItemStack",
            "v": 4189,
            "meta": {
              "==": "ItemMeta",
              "meta-type": "UNSPECIFIC",
              "enchants": {
                "minecraft:sharpness": 5
              }
            },
            "type": "NETHERITE_SWORD"
          },
          "Slot 5": null,
          "Slot 6": null,
          "Slot 7": null,
          "Slot 8": null,
          "Slot 9": null,
          "Slot 10": null,
          "Slot 11": null,
          "Slot 12": null,
          "Slot 13": null,
          "Slot 14": null,
          "Slot 15": null,
          "Slot 16": null,
          "Slot 17": null,
          "Slot 18": null,
          "Slot 19": null,
          "Slot 20": null,
          "Slot 21": null,
          "Slot 22": null,
          "Slot 23": null,
          "Slot 24": null,
          "Slot 25": null,
          "Slot 26": null,
          "Slot 27": null,
          "Slot 28": null,
          "Slot 29": null,
          "Slot 30": null,
          "Slot 31": null,
          "Slot 32": null,
          "Slot 33": null,
          "Slot 34": null,
          "Slot 35": null,
          "Slot 36": null,
          "Slot 37": null,
          "Slot 38": null,
          "Slot 39": null,
          "Slot 40": null
        }
      }
    }
  }
}

The Saving part that runs on quit runs these 2 functions with a 10 tick wait between them to maker sure it saves without breaking the file

    if json file "saves\%{_p}%\%{_slot}%.json" exists:
        set {_json} to json from json file "saves\%{_p}%\%{_slot}%.json"
        set value of json object "data::save::stats::maxMana" in {_json} to {stats::%{_p}%::maxMana}
        set value of json object "data::save::stats::maxHealth" in {_json} to {stats::%{_p}%::maxHealth}
        set value of json object "data::save::class" in {_json} to {classes::%{_p}%::class}
        set value of json object "data::save::money" in {_json} to {currency::%{_p}%::cash}
        write json from {_json} to json file "saves\%{_p}%\%{_slot}%.json"
        delete {stats::%{_p}%::maxMana}
        delete {stats::%{_p}%::maxHealth}
        delete {classes::%{_p}%::class}
        delete {classes::%{_p}%::class::formatted}
        delete {currency::%{_p}%::cash}


function saveLastLocation(p: player, slot: integer, loc: location):
    if json file "saves\%{_p}%\%{_slot}%.json" exists:
        set {_json} to json from json file "saves\%{_p}%\%{_slot}%.json"
        set value of json object "data::save::lastLocation::x" in {_json} to x-coordinate of {_loc}
        set value of json object "data::save::lastLocation::y" in {_json} to y-coordinate of {_loc}
        set value of json object "data::save::lastLocation::z" in {_json} to z-coordinate of {_loc}
        set value of json object "data::save::inventory" in {_json} to inventory of {_p}
        write json from {_json} to json file "saves\%{_p}%\%{_slot}%.json"
        delete {saves::%{_p}%::slot}

To load the save, we do

    if json file "saves\%{_p}%\%{_slot}%.json" exists:
        set {_json} to json from json file "saves\%{_p}%\%{_slot}%.json"
        set {stats::%{_p}%::maxMana} to value "data::save::stats::maxMana" of {_json}
        set {stats::%{_p}%::maxHealth} to value "data::save::stats::maxHealth" of {_json}
        set {classes::%{_p}%::class} to value "data::save::class" of {_json}
        set {currency::%{_p}%::cash} to value "data::save::money" of {_json}
        set {saves::%{_p}%::slot} to {_slot}
        tpToLastLocation({_p}, {_slot})
    
function tpToLastLocation(p: player, slot: integer):
    if json file "saves\%{_p}%\%{_slot}%.json" exists:
        set {_json} to json from json file "saves\%{_p}%\%{_slot}%.json"
        if value "data::save::lastLocation::x" of {_json} is set:
            teleport {_p} to location(value "data::save::lastLocation::x" of {_json}, value "data::save::lastLocation::y" of {_json}, value "data::save::lastLocation::z" of {_json})
            wait 2 ticks
            # this sets the items to the respective slots but doesn't keep enchants.
            loop values "data::save::inventory::contents" of {_json}:
                set {_slot} to "%json-key.replace("Slot ", "")%" parsed as integer
                set slot {_slot} of {_p}'s inventory to json-value 
                
            # Cannnot set inventory with 
            # set {_inv} to value "data::save::inventory" of {_json}
            # set {_p}'s inventory to {_inv}

when trying to set it directly, i got this error after

[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! [Skript] Severe Error:
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! Something went horribly wrong with Skript.
[11:47:10 ERROR]: #!#! This issue is NOT your fault! You probably can't fix it yourself, either.
[11:47:10 ERROR]: #!#! It looks like you are using some plugin(s) that alter how Skript works (addons).
[11:47:10 ERROR]: #!#! Here is full list of them:
[11:47:10 ERROR]: #!#! skript-reflect v2.5.1 (https://github.com/SkriptLang/skript-reflect) SkJson v4.0.1 SkBee v3.6.6 (https://github.com/SkriptHub/SkBee) SkEssentials v1.0.5 
[11:47:10 ERROR]: #!#! We could not identify which of those are specially related, so this might also be Skript issue.
[11:47:10 ERROR]: #!#! You should try disabling those plugins one by one, trying to find which one causes it.
[11:47:10 ERROR]: #!#! If the error doesn't disappear even after disabling all listed plugins, it is probably Skript issue.
[11:47:10 ERROR]: #!#! In that case, you will be given instruction on how should you report it.
[11:47:10 ERROR]: #!#! On the other hand, if the error disappears when disabling some plugin, report it to author of that plugin.
[11:47:10 ERROR]: #!#! Only if the author tells you to do so, report it to Skript's issue tracker.
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! Stack trace:
[11:47:10 ERROR]: #!#! java.lang.ClassCastException: class org.bukkit.craftbukkit.inventory.CraftInventoryCustom cannot be cast to class org.bukkit.inventory.ItemStack (org.bukkit.craftbukkit.inventory.CraftInventoryCustom and org.bukkit.inventory.ItemStack are in unnamed module of loader java.net.URLClassLoader @4dcbadb4)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//org.skriptlang.skript.lang.converter.ChainedConverter.convert(ChainedConverter.java:58)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//org.skriptlang.skript.lang.converter.Converters.convert(Converters.java:412)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.lang.Variable.getConverted(Variable.java:486)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.lang.Variable.getAll(Variable.java:707)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.lang.Variable.getArray(Variable.java:699)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.effects.EffChange.execute(EffChange.java:284)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.lang.Effect.run(Effect.java:49)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.lang.TriggerItem.walk(TriggerItem.java:61)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.lang.TriggerItem.walk(TriggerItem.java:88)
[11:47:10 ERROR]: #!#!     at Skript-2.9.5.jar//ch.njol.skript.effects.Delay.lambda$walk$0(Delay.java:107)
[11:47:10 ERROR]: #!#!     at org.bukkit.craftbukkit.scheduler.CraftTask.run(CraftTask.java:78)
[11:47:10 ERROR]: #!#!     at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:474)
[11:47:10 ERROR]: #!#!     at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:1659)
[11:47:10 ERROR]: #!#!     at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1529)
[11:47:10 ERROR]: #!#!     at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1251)
[11:47:10 ERROR]: #!#!     at net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:310)
[11:47:10 ERROR]: #!#!     at java.base/java.lang.Thread.run(Thread.java:1570)
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! Version Information:
[11:47:10 ERROR]: #!#!   Skript: 2.9.5 (latest)
[11:47:10 ERROR]: #!#!     Flavor: skriptlang-github
[11:47:10 ERROR]: #!#!     Date: 19:09:25.858356200
[11:47:10 ERROR]: #!#!   Bukkit: 1.21.4-R0.1-SNAPSHOT
[11:47:10 ERROR]: #!#!   Minecraft: 1.21.4
[11:47:10 ERROR]: #!#!   Java: 22.0.2 (Java HotSpot(TM) 64-Bit Server VM 22.0.2+9-70)
[11:47:10 ERROR]: #!#!   OS: Windows 10 amd64 10.0
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! Server platform: Paper
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! Current node: null
[11:47:10 ERROR]: #!#! Current item: set inventory of {_p} (as java.lang.Object) to {inv::%{_p} (as java.lang.Object)%::contents::slot::%{_slot} (as java.lang.Object)%} (as java.lang.Iterable)
[11:47:10 ERROR]: #!#! Current trigger: function tpToLastLocation (simple event) (save\savehandler.sk, line 40)
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! Thread: Server thread
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! Language: english
[11:47:10 ERROR]: #!#! Link parse mode: DISABLED
[11:47:10 ERROR]: #!#! 
[11:47:10 ERROR]: #!#! End of Error.
[11:47:10 ERROR]: #!#!

is there any issues with how I am implementing this or is this an issue worth while?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant