New TypeAdapter for ItemStack (#67)

This commit is contained in:
koolkrafter5 authored and GitHub committed 2025-01-21 23:59:50 -06:00
1 parent ac50cfc691
commit bd584c6d52
6 files changed
+56 -6

No files matched your search

@@ -0,0 +1,46 @@
package WayofTime.alchemicalWizardry.common.summoning.meteor;
import java.io.IOException;
import net.minecraft.item.ItemStack;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import WayofTime.alchemicalWizardry.AlchemicalWizardry;
import cpw.mods.fml.common.registry.GameRegistry;
class ItemStackAdapter extends TypeAdapter<ItemStack> {
@Override
public ItemStack read(JsonReader reader) throws IOException {
String str = reader.nextString();
String[] input = str.split(":");
if (input.length < 2) {
AlchemicalWizardry.logger
.warn("Unable to read item stack \"{}\". Valid format is \"modid:name(:meta optional)\"", str);
return null;
}
ItemStack itemStack = GameRegistry.findItemStack(input[0], input[1], 1);
if (itemStack == null) {
AlchemicalWizardry.logger.warn("Unable to find item stack \"{}\".", str);
return null;
}
if (input.length < 3) {
return itemStack;
}
try {
itemStack.setItemDamage(Integer.parseInt(input[2]));
} catch (NumberFormatException e) {
AlchemicalWizardry.logger
.warn("Invalid metadata value {} for item stack {}:{}", input[2], input[0], input[1]);
}
return itemStack;
}
@Override
public void write(JsonWriter writer, ItemStack mpc) throws IOException {
// Not implemented because it is not needed.
}
}
@@ -8,7 +8,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.minecraft.init.Items;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
@@ -27,7 +26,7 @@ public class Meteor {
public int radius;
public int cost;
public float fillerChance; // Out of 100.0
public transient ItemStack focusItem;
public ItemStack focusItem;
private String focusModId;
private String focusName;
private int focusMeta;
@@ -203,13 +202,17 @@ public class Meteor {
}
/**
* Construct the focus item for this meteor from its modid, name, and metadata
* Construct the focus item for this meteor from its modid, name, and metadata if using the old config format.
*/
private void buildFocusItem() {
if (this.focusItem != null) return;
this.focusItem = GameRegistry.findItemStack(this.focusModId, this.focusName, 1);
if (this.focusItem == null) return;
Items.feather.setDamage(this.focusItem, this.focusMeta);
this.focusItem.setItemDamage(this.focusMeta);
AlchemicalWizardry.logger.warn(
"Setting focusModId, focusName, and focusMeta individually in meteor"
+ "configs has been deprecated. Set \"focusItem\" with format \"modId:name(:meta optional)\""
+ " instead.");
}
/**
@@ -14,7 +14,7 @@ class MeteorComponentAdapter extends TypeAdapter<MeteorComponent> {
}
@Override
public void write(JsonWriter writer, MeteorComponent mpc) throws IOException {
public void write(JsonWriter writer, MeteorComponent component) throws IOException {
// Not implemented because it is not needed.
}
}
@@ -24,7 +24,8 @@ public class MeteorRegistry {
public static void loadConfig() {
Gson gson = new GsonBuilder().setPrettyPrinting()
.registerTypeAdapter(MeteorComponent.class, new MeteorComponentAdapter()).create();
.registerTypeAdapter(MeteorComponent.class, new MeteorComponentAdapter())
.registerTypeAdapter(ItemStack.class, new ItemStackAdapter()).create();
File file = new File("config/BloodMagic/meteors");
File[] files = file.listFiles();
if (files != null) {