generated from Anvil-Dev/ArchitecturyExampleMod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
common/src/main/java/dev/anvilcraft/lib/util/JsonDeserializable.java
This file contains 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,30 @@ | ||
package dev.anvilcraft.lib.util; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.mojang.serialization.JsonOps; | ||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface JsonDeserializable { | ||
static @NotNull ItemStack toItemStack(@NotNull JsonElement element) { | ||
if (element.isJsonPrimitive()) { | ||
return new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation(element.getAsString()))); | ||
} | ||
JsonObject obj = element.getAsJsonObject(); | ||
Item item = BuiltInRegistries.ITEM.get(new ResourceLocation(obj.get("id").getAsString())); | ||
ItemStack stack = new ItemStack(item); | ||
if (obj.has("count")) stack.setCount(obj.get("count").getAsInt()); | ||
if (obj.has("data")) { | ||
CompoundTag.CODEC | ||
.parse(JsonOps.INSTANCE, obj.get("data")) | ||
.result() | ||
.ifPresent(nbt -> stack.setTag(stack.getOrCreateTag().merge(nbt))); | ||
} | ||
return stack; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
common/src/main/java/dev/anvilcraft/lib/util/JsonSerializable.java
This file contains 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,50 @@ | ||
package dev.anvilcraft.lib.util; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonPrimitive; | ||
import net.minecraft.core.registries.BuiltInRegistries; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Objects; | ||
|
||
public interface JsonSerializable { | ||
JsonElement toJson(); | ||
|
||
static @NotNull JsonArray fromList(@NotNull Iterable<? extends JsonSerializable> iterable) { | ||
JsonArray array = new JsonArray(); | ||
iterable.forEach(element -> array.add(element.toJson())); | ||
return array; | ||
} | ||
|
||
static @NotNull JsonElement fromItemStack(@NotNull ItemStack stack) { | ||
Item item = stack.getItem(); | ||
ItemStack instance = item.getDefaultInstance(); | ||
CompoundTag nbt = stack.getTag(); | ||
int count = stack.getCount(); | ||
String id = BuiltInRegistries.ITEM.getKey(item).toString(); | ||
if (count == 1 && (nbt == null || ItemStack.isSameItemSameTags(instance, stack))) { | ||
return new JsonPrimitive(id); | ||
} | ||
JsonObject obj = new JsonObject(); | ||
obj.addProperty("id", id); | ||
obj.addProperty("count", count); | ||
if (nbt != null) { | ||
CompoundTag instanceNbt = instance.getTag(); | ||
if (instanceNbt == null) { | ||
obj.addProperty("data", nbt.toString()); | ||
} else { | ||
CompoundTag diff = nbt.copy(); | ||
for (String key : instanceNbt.getAllKeys()) { | ||
if (Objects.equals(diff.get(key), instanceNbt.get(key))) diff.remove(key); | ||
} | ||
if (!diff.isEmpty()) obj.addProperty("data", diff.toString()); | ||
} | ||
} | ||
return obj; | ||
} | ||
} |
This file contains 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
This file contains 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