Repository navigation
fix: stop vehicle containers losing custom items on reload #61
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/java/net/tfminecraft/vehicleframework/database/ContainerPayloadRoundTripTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package net.tfminecraft.vehicleframework.database; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.util.List; | ||
| import java.util.TreeMap; | ||
|
|
||
| import org.json.simple.JSONObject; | ||
| import org.json.simple.JSONValue; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonObject; | ||
|
|
||
| class ContainerPayloadRoundTripTest { | ||
|
|
||
| private static final String SNBT = "{components:{\"minecraft:custom_name\":{extra:[{bold:0b,text:\"<#c4b99f>Arcane 'Fuel'\"}]," | ||
| + "text:\"\"},\"minecraft:attribute_modifiers\":[{amount:0.0d}]},count:52,id:\"minecraft:iron_nugget\"}"; | ||
|
|
||
| @Test | ||
| void snbtStringEntry_survivesSaveAndLoadUnchanged() { | ||
| JsonArray entry = new JsonArray(); | ||
| entry.add(3); | ||
| entry.add(SNBT); | ||
| JsonArray items = new JsonArray(); | ||
| items.add(entry); | ||
| JsonObject container = new JsonObject(); | ||
| container.addProperty("id", "cart"); | ||
| container.add("items", items); | ||
|
|
||
| // Same path as ActiveVehicleSnapshotFactory.saveContainers and the payload write. | ||
| JSONObject vehicle = new JSONObject(); | ||
| JSONObject section = new JSONObject(); | ||
| section.put("cart", JSONValue.parse(container.toString())); | ||
| vehicle.put("containers", section); | ||
| TreeMap<String, Object> sorted = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); | ||
| sorted.putAll(vehicle); | ||
| String payload = new GsonBuilder().setPrettyPrinting().create().toJson(sorted); | ||
|
|
||
| List<JsonObject> loaded = VehiclePayloadCodec.loadContainers((JSONObject) JSONValue.parse(payload)); | ||
|
|
||
| assertEquals(1, loaded.size()); | ||
| JsonArray loadedEntry = loaded.get(0).getAsJsonArray("items").get(0).getAsJsonArray(); | ||
| assertEquals(3, loadedEntry.get(0).getAsInt()); | ||
| assertEquals(SNBT, loadedEntry.get(1).getAsString()); | ||
| } | ||
| } |
112 changes: 112 additions & 0 deletions
112
.../java/net/tfminecraft/vehicleframework/vehicles/handlers/container/ContainerSnbtTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| package net.tfminecraft.vehicleframework.vehicles.handlers.container; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonNull; | ||
| import com.google.gson.JsonParser; | ||
| import com.google.gson.JsonPrimitive; | ||
|
|
||
| class ContainerSnbtTest { | ||
|
|
||
| // Trimmed from the Arcane Fuel stack that Main failed to load on 2026-10-05. | ||
| private static final String SNBT = "{DataVersion:4554,components:{\"minecraft:attribute_modifiers\":" | ||
| + "[{amount:0.0d,id:\"mmoitems:decoy\",operation:\"add_value\",type:\"minecraft:attack_speed\"}]," | ||
| + "\"minecraft:custom_data\":{HSTRY_NAME:'{\"Stat\":\"NAME\"}',MMOITEMS_CUSTOM_MODEL_DATA:4210," | ||
| + "MMOITEMS_DISABLE_CRAFTING:1b},\"minecraft:custom_model_data\":{floats:[4210.0f]}," | ||
| + "\"minecraft:custom_name\":{extra:[{bold:0b,color:\"#C4B99F\",text:\"Arcane Fuel\"}],text:\"\"}," | ||
| + "\"minecraft:profile\":{id:[I;1,-2,3,4]}},count:52,id:\"minecraft:iron_nugget\"}"; | ||
|
|
||
| @Test | ||
| void stringEntry_isUsedAsSnbtAsIs() { | ||
| assertEquals(List.of(SNBT), Container.snbtCandidates(new JsonPrimitive(SNBT))); | ||
| } | ||
|
|
||
| @Test | ||
| void legacyEntry_triesRestoredTypesThenSavedForm() { | ||
| JsonElement legacy = JsonParser.parseString(SNBT); | ||
|
|
||
| List<String> candidates = Container.snbtCandidates(legacy); | ||
|
|
||
| assertEquals(2, candidates.size()); | ||
| assertEquals(Container.legacySnbt(legacy), candidates.get(0)); | ||
| assertEquals(legacy.toString(), candidates.get(1)); | ||
| } | ||
|
|
||
| @Test | ||
| void nullEntry_hasNoCandidates() { | ||
| assertEquals(List.of(), Container.snbtCandidates(JsonNull.INSTANCE)); | ||
| assertEquals(List.of(), Container.snbtCandidates(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void legacyParse_hadTurnedTypedNumbersIntoStrings() { | ||
| String saved = JsonParser.parseString(SNBT).toString(); | ||
|
|
||
| assertTrue(saved.contains("\"amount\":\"0.0d\"")); | ||
| assertTrue(saved.contains("\"bold\":\"0b\"")); | ||
| assertTrue(saved.contains("[\"I\",1,-2,3,4]")); | ||
| } | ||
|
|
||
| @Test | ||
| void legacySnbt_restoresTypedNumbersAndArrays() { | ||
| String repaired = Container.legacySnbt(JsonParser.parseString(SNBT)); | ||
|
|
||
| assertTrue(repaired.contains("\"amount\":0.0d")); | ||
| assertTrue(repaired.contains("\"bold\":0b")); | ||
| assertTrue(repaired.contains("\"MMOITEMS_DISABLE_CRAFTING\":1b")); | ||
| assertTrue(repaired.contains("\"floats\":[4210.0f]")); | ||
| assertTrue(repaired.contains("\"id\":[I;1,-2,3,4]")); | ||
| assertTrue(repaired.contains("\"count\":52")); | ||
| assertTrue(repaired.contains("\"MMOITEMS_CUSTOM_MODEL_DATA\":4210")); | ||
| } | ||
|
|
||
| @Test | ||
| void legacySnbt_keepsTextQuotedAndEscaped() { | ||
| String repaired = Container.legacySnbt(JsonParser.parseString(SNBT)); | ||
|
|
||
| assertTrue(repaired.contains("\"text\":\"Arcane Fuel\"")); | ||
| assertTrue(repaired.contains("\"HSTRY_NAME\":\"{\\\"Stat\\\":\\\"NAME\\\"}\"")); | ||
| assertTrue(repaired.contains("\"id\":\"minecraft:iron_nugget\"")); | ||
| assertTrue(repaired.contains("\"text\":\"\"")); | ||
| } | ||
|
|
||
| @Test | ||
| void legacySnbt_typedArraysOfTypedValues() { | ||
| String repaired = Container.legacySnbt(JsonParser.parseString("{b:[B;1b,0b],l:[L;5L,-6L]}")); | ||
|
|
||
| assertEquals("{\"b\":[B;1b,0b],\"l\":[L;5L,-6L]}", repaired); | ||
| } | ||
|
|
||
| @Test | ||
| void legacySnbt_plainListsStayLists() { | ||
| assertEquals("[\"I\"]", Container.legacySnbt(JsonParser.parseString("[\"I\"]"))); | ||
| assertEquals("[\"I\",\"me\"]", Container.legacySnbt(JsonParser.parseString("[\"I\",\"me\"]"))); | ||
| assertEquals("[\"X\",1]", Container.legacySnbt(JsonParser.parseString("[\"X\",1]"))); | ||
| assertEquals("[[1],2]", Container.legacySnbt(JsonParser.parseString("[[1],2]"))); | ||
| assertEquals("[\"I\",[1]]", Container.legacySnbt(JsonParser.parseString("[\"I\",[1]]"))); | ||
| } | ||
|
|
||
| @Test | ||
| void legacySnbt_skipsNullsAndKeepsBooleans() { | ||
| assertEquals("{\"a\":[1,2],\"c\":true}", Container.legacySnbt(JsonParser.parseString("{a:[1,null,2],b:null,c:true}"))); | ||
| } | ||
|
|
||
| @Test | ||
| void legacySnbt_numberLikeTextWithoutSuffixStaysQuoted() { | ||
| assertEquals("{\"t\":\"12\",\"u\":\"1.5x\"}", Container.legacySnbt(JsonParser.parseString("{t:\"12\",u:\"1.5x\"}"))); | ||
| } | ||
|
|
||
| @Test | ||
| void legacySnbt_noLongerMatchesSavedForm() { | ||
| JsonElement legacy = JsonParser.parseString(SNBT); | ||
|
|
||
| assertFalse(Container.legacySnbt(legacy).contains("\"0.0d\"")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.