Skip to content

Commit

Permalink
Remove deprecated item stack serde methods
Browse files Browse the repository at this point in the history
This includes serialising and deserialising item stacks with the default
Jankson instance from JanksonFactory.

These were deprecated in 8.0.0 as they didn't include item components.
Components can't be included in JanksonFactory.builder() since they need
registry access, so devs should use the vanilla item stack codecs instead.
  • Loading branch information
Juuxel committed Oct 23, 2024
1 parent 683f82b commit 750c774
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,11 @@
import blue.endless.jankson.api.Marshaller;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.state.property.Property;
import net.minecraft.util.Identifier;

public class BlockAndItemSerializers {
/**
* @deprecated This method doesn't support item stack components.
* You can use {@link ItemStack#CODEC} with a {@link net.minecraft.registry.RegistryOps}
* wrapping a {@link JanksonOps}.
*/
@Deprecated(forRemoval = true)
public static ItemStack getItemStack(JsonObject json, Marshaller m) {
String itemIdString = json.get(String.class, "item");
Item item = Registries.ITEM.getOptionalValue(Identifier.of(itemIdString)).orElse(Items.AIR);
ItemStack stack = new ItemStack(item);
if (json.containsKey("count")) {
Integer count = json.get(Integer.class, "count");
if (count!=null) {
stack.setCount(count);
}
}
return stack;
}

/**
* @deprecated This method doesn't support item stack components.
* You can use {@link ItemStack#CODEC} with a {@link net.minecraft.registry.RegistryOps}
* wrapping a {@link JanksonOps}. For supporting the inline/primitive format, you can use
* {@link com.mojang.serialization.Codec#withAlternative(com.mojang.serialization.Codec, com.mojang.serialization.Codec, java.util.function.Function)
* Codec.withAlternative}
* where the secondary codec is an {@link Item} codec.
*/
@Deprecated(forRemoval = true)
public static ItemStack getItemStackPrimitive(String s, Marshaller m) {
Item item = Registries.ITEM.getOptionalValue(Identifier.of(s)).orElse(Items.AIR);
ItemStack stack = new ItemStack(item);
return stack;
}

/**
* @deprecated This method doesn't support item stack components.
* You can use {@link ItemStack#CODEC} with a {@link net.minecraft.registry.RegistryOps}
* wrapping a {@link JanksonOps}.
*/
@Deprecated(forRemoval = true)
public static JsonElement saveItemStack(ItemStack stack, Marshaller m) {
JsonPrimitive id = new JsonPrimitive(Registries.ITEM.getId(stack.getItem()).toString());
if (stack.getCount()==1) return id;

JsonObject result = new JsonObject();
result.put("item", new JsonPrimitive(Registries.ITEM.getId(stack.getItem()).toString()));
result.put("count", new JsonPrimitive(stack.getCount()));
return result;
}

/**
* @deprecated The {@link blue.endless.jankson.Jankson} instance created
* by {@link JanksonFactory} already supports {@link Block} with this format.
*/
@Deprecated(forRemoval = true)
public static Block getBlockPrimitive(String blockIdString, Marshaller m) {
Optional<Block> blockOpt = Registries.BLOCK.getOptionalValue(Identifier.of(blockIdString));
return blockOpt.orElse(null);
}

/**
* @deprecated The {@link blue.endless.jankson.Jankson} instance created
* by {@link JanksonFactory} already supports {@link Block} with this format.
*/
@Deprecated(forRemoval = true)
public static JsonElement saveBlock(Block block, Marshaller m) {
return new JsonPrimitive(Registries.BLOCK.getId(block).toString());
}


public static BlockState getBlockStatePrimitive(String blockIdString, Marshaller m) {
Optional<Block> blockOpt = Registries.BLOCK.getOptionalValue(Identifier.of(blockIdString));
if (blockOpt.isPresent()) {
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/io/github/cottonmc/jankson/JanksonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.minecraft.fluid.Fluid;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.consume.ConsumeEffect;
import net.minecraft.item.map.MapDecorationType;
import net.minecraft.loot.condition.LootConditionType;
Expand Down Expand Up @@ -78,11 +77,6 @@
public class JanksonFactory {
public static Jankson.Builder builder() {
Jankson.Builder builder = Jankson.builder();

builder
.registerDeserializer(String.class, ItemStack.class, BlockAndItemSerializers::getItemStackPrimitive)
.registerDeserializer(JsonObject.class, ItemStack.class, BlockAndItemSerializers::getItemStack)
.registerSerializer(ItemStack.class, BlockAndItemSerializers::saveItemStack);

builder
.registerDeserializer(String.class, BlockState.class, BlockAndItemSerializers::getBlockStatePrimitive)
Expand Down

0 comments on commit 750c774

Please sign in to comment.