Skip to content

Commit

Permalink
🐛 修复可能存在的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Dec 19, 2024
1 parent c707b6f commit cc56763
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
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 common/src/main/java/dev/anvilcraft/lib/util/JsonSerializable.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AnvilLibForge() {
for (IModInfo mod : ModList.get().getMods()) {
Map<String, Object> modProperties = mod.getModProperties();
for (Map.Entry<String, Object> entry : modProperties.entrySet()) {
if (!entry.getKey().equals("anvilcraft")) continue;
if (!entry.getKey().equals(AnvilLib.MOD_ID)) continue;
if (!(entry.getValue() instanceof UnmodifiableConfig anvilConfig)) return;
for (UnmodifiableConfig.Entry entry1 : anvilConfig.entrySet()) {
if (!entry1.getKey().equals("integrations")) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AnvilLibForge() {
for (IModInfo mod : ModList.get().getMods()) {
Map<String, Object> modProperties = mod.getModProperties();
for (Map.Entry<String, Object> entry : modProperties.entrySet()) {
if (!entry.getKey().equals("anvilcraft")) continue;
if (!entry.getKey().equals(AnvilLib.MOD_ID)) continue;
if (!(entry.getValue() instanceof UnmodifiableConfig anvilConfig)) return;
for (UnmodifiableConfig.Entry entry1 : anvilConfig.entrySet()) {
if (!entry1.getKey().equals("integrations")) continue;
Expand Down

0 comments on commit cc56763

Please sign in to comment.