Skip to content

Commit

Permalink
New TypeAdapter for ItemStack (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkrafter5 authored Jan 22, 2025
1 parent ac50cfc commit bd584c6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -203,13 +202,17 @@ private void clampNumericalValues() {
}

/**
* 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.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public MeteorComponent read(JsonReader reader) throws IOException {
}

@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.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Binary file modified src/main/resources/assets/alchemicalwizardry/meteors/meteors.zip
Binary file not shown.
Binary file not shown.

0 comments on commit bd584c6

Please sign in to comment.