Code Monkey home page Code Monkey logo

jsonmerger's Introduction

Json Merger

A small server-only (works on clients) mod that enables loading a combined version of the jsons, for json files that are normally "last man wins". This is done through a patching mechanism, where jsons can manually specify they should act in "merge" mode instead of the default "overwrite" mode. Fine-tuning can be achieved by specifying merge/overwrite/delete in the individual children.

See the How to Use section below, for more details.

What this mod it allows

This mod enables merging for:

  • Loot tables
  • Advancements
  • Recipes
  • Biomes*
  • Dimensions*
  • Structures*

* This mod CANNOT modify the default dimension/biome/structure definitions, only allows merging between json files. This means if you want to patch the vanilla defaults, you will need to first export the vanilla defaults to json (or use an existing vanilla-defaults datapack), and then provide the patch files in your datapack.

How it works

Json Merger has a small coremod that patches two method calls:

  1. The call to JSONUtils.fromJson in JsonReloadListener#prepare, which handles loading datapack jsons such as loot tables.
  2. The call to JsonParser#prepare in the func_241879_a method from the anonymous impl of WorldSettingsImport.IResourceAccess contained in func_244345_a, which handles loading the json files for worldgen settings.

How to use it

This feature is available on specific supported json files (eg, loot tables).

Json objects can contain a "_jm_combine" key, which should itself be a json object.

By default, all json files start in "overwrite" mode, which returns the file as-is, maintaining the default vanilla behaviour.

Certain alternative modes are available depending on the element type.

Objects:

  • Combine: The two objects are combined, processing the child elements recursively.

Arrays:

  • Combine: The two arrays are combined, processing the child elements recursively. When in combine mode, array elements can have special modes to choose what to replace, or where to insert.
  • Zip: The two arrays are combined pairwise, combining each pair of elements into a new element.

Child elements within arrays:

  • Append (default): The element is added to the end of the list.
  • Overwrite: The element with either the specified index, or the specified search pattern, gets replaced.
  • Combine: Same as replace, but combines the existing value instead of overwriting.
  • Insert: The element is inserted at the position with the specified index.

How it's processed

The process begins at the root object, which defaults to "overwrite". If this object is set to "combine" instead, the merger will iterate through the key sets of both the original and the new, combining values for keys that exist on both objects. Children of combined objects inherit the combine mode, but they can choose to overwrite instead, by specifying this explicitly.

For arrays, the combine operation adds elements to the array, without combining them. However those child elements can specify their own mode, in which case the element will either be inserted at a chosen location, will replace (overwrite) and existing value, or will be combined with an existing value. This behaviour is guided either by index, or by search pattern.

For all other data types (primitives, null), overwrite and combine act the same: the element is returned as-is.

Controlling the insertion behaviour for non-object elements

Since only json objects can contain the "_jm_combine" key, another feature is available: any object which has the "_jm_combine" key, and inside has a "value" key, the contents of this value key are used, instead of the object. This allows inserting/replacing arrays, strings, or other primitives, contained within other arrays, or controlling the mode of values in individual keys within an object.

That is, the following object:

{
  "_jm_combine": { "value": 1 }
}

is equivalent to the primitive 1.

jsonmerger's People

Contributors

gigaherz avatar

Stargazers

 avatar

Watchers

 avatar  avatar

jsonmerger's Issues

Can't delete items from arrays

Using the delete mode fails in arrays. Below is an example loot table that replaces the flint drop from gravel with a diamond. Switching that object to delete mode doesn't remove the flint drop. Also in find I had to add extra objects in the arrays for them to match as it seems they are matched in exact mode.

{
	"_jm_combine": {
		"mode": "combine"
	},
	"pools": [
		{
			"_jm_combine": {
				"mode": "combine",
				"find": {
					"entries": [
						{
							"type": "minecraft:alternatives",
							"children": [
								{},
								{
									"type": "minecraft:alternatives",
									"children": [
										{
											"type": "minecraft:item",
											"name": "minecraft:flint"
										},
										{}
									]
								}
							]
						}
					]
				}
			},
			"entries": [
				{
					"_jm_combine": {
						"mode": "combine",
						"find": {
							"type": "minecraft:alternatives",
							"children": [
								{
									"type": "minecraft:alternatives",
									"children": [
										{
											"type": "minecraft:item",
											"name": "minecraft:flint"
										},
										{}
									]
								}
							]
						}
					},
					"children": [
						{
							"_jm_combine": {
								"mode": "combine",
								"find": {
									"type": "minecraft:alternatives",
									"children": [
										{
											"type": "minecraft:item",
											"name": "minecraft:flint"
										}
									]
								}
							},
							"children": [
								{
									"_jm_combine": {
										"mode": "combine",
										"find": {
											"type": "minecraft:item",
											"name": "minecraft:flint"
										}
									},
									"name": "minecraft:diamond"
								}
							]
						}
					]
				}
			]
		}
	]
}

Model JSON merging?

I'm wondering if you have considered adding some client side merging (in my case, merging model JSONs would be really nice).

I've been working on adding armor trim materials for Cinderscapes, but it requires overwriting the vanilla item models, which really sucks. I just need to add a few overrides to them.

It would be great if I could recommend using a mod like yours to resolve problems when folks want to use Cinderscapes with another mod that also adds armor trims. (It would be even greater if Mojang will add this functionality themselves, but they do not seem to care...)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.