|
| 1 | +package com.robotgryphon.compactcrafting.recipes.data.util; |
| 2 | + |
| 3 | +import com.mojang.serialization.DataResult; |
| 4 | +import com.robotgryphon.compactcrafting.CompactCrafting; |
| 5 | +import com.robotgryphon.compactcrafting.recipes.MiniaturizationRecipe; |
| 6 | +import net.minecraft.block.BlockState; |
| 7 | +import net.minecraft.item.ItemStack; |
| 8 | +import net.minecraft.nbt.CompoundNBT; |
| 9 | +import net.minecraft.nbt.INBT; |
| 10 | +import net.minecraft.nbt.ListNBT; |
| 11 | +import net.minecraft.nbt.NBTDynamicOps; |
| 12 | +import net.minecraft.network.PacketBuffer; |
| 13 | +import net.minecraft.util.ResourceLocation; |
| 14 | +import net.minecraftforge.common.util.Constants; |
| 15 | + |
| 16 | +import javax.annotation.Nonnull; |
| 17 | + |
| 18 | +public abstract class RecipeBufferDataUtil { |
| 19 | + |
| 20 | + public static final ResourceLocation TYPE_CATALYSTS = new ResourceLocation(CompactCrafting.MOD_ID, "catalysts"); |
| 21 | + |
| 22 | + /** |
| 23 | + * Reads component information from a packet buffer, adding it to a recipe. |
| 24 | + * |
| 25 | + * @param recipe Recipe to add component information to. |
| 26 | + * @param buffer Packet to read component information from. |
| 27 | + */ |
| 28 | + public static void readComponentInfo(MiniaturizationRecipe recipe, PacketBuffer buffer) { |
| 29 | + CompoundNBT components = buffer.readCompoundTag(); |
| 30 | + int numComponents = components.getInt("count"); |
| 31 | + if(numComponents > 0) { |
| 32 | + ListNBT compList = components.getList("components", Constants.NBT.TAG_COMPOUND); |
| 33 | + compList.forEach(comp -> { |
| 34 | + CompoundNBT compC = (CompoundNBT) comp; |
| 35 | + String key = compC.getString("key"); |
| 36 | + CompoundNBT stateTag = compC.getCompound("state"); |
| 37 | + |
| 38 | + BlockState.CODEC.decode(NBTDynamicOps.INSTANCE, stateTag) |
| 39 | + .resultOrPartial(CompactCrafting.LOGGER::error) |
| 40 | + .ifPresent(state -> { |
| 41 | + BlockState compState = state.getFirst(); |
| 42 | + recipe.addComponent(key, compState); |
| 43 | + |
| 44 | + CompactCrafting.LOGGER.debug("Got component: {} ({})", key, compState.toString()); |
| 45 | + }); |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Writes component information for a recipe to a compound NBT tag. |
| 52 | + * @param recipe |
| 53 | + * @return |
| 54 | + */ |
| 55 | + @Nonnull |
| 56 | + public static void writeComponentInfo(MiniaturizationRecipe recipe, PacketBuffer buffer) { |
| 57 | + CompoundNBT componentData = new CompoundNBT(); |
| 58 | + int numComponents = recipe.getComponentKeys().size(); |
| 59 | + componentData.putInt("count", numComponents); |
| 60 | + |
| 61 | + if (numComponents > 0) { |
| 62 | + ListNBT compList = new ListNBT(); |
| 63 | + recipe.getComponents().forEach((key, state) -> { |
| 64 | + DataResult<INBT> encode = BlockState.CODEC.encode(state, NBTDynamicOps.INSTANCE, null); |
| 65 | + encode |
| 66 | + .resultOrPartial(CompactCrafting.LOGGER::error) |
| 67 | + .ifPresent(stateNbt -> { |
| 68 | + CompoundNBT componentTag = new CompoundNBT(); |
| 69 | + componentTag.put("state", stateNbt); |
| 70 | + componentTag.putString("key", key); |
| 71 | + |
| 72 | + compList.add(componentTag); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + componentData.put("components", compList); |
| 77 | + } |
| 78 | + |
| 79 | + buffer.writeCompoundTag(componentData); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Reads recipe output information from a packet buffer. |
| 84 | + * |
| 85 | + * @param recipe Recipe to load output information into. |
| 86 | + * @param buffer Buffer to pull data from. |
| 87 | + */ |
| 88 | + public static void readRecipeOutputs(MiniaturizationRecipe recipe, PacketBuffer buffer) throws Exception { |
| 89 | + CompoundNBT outputMeta = buffer.readCompoundTag(); |
| 90 | + if(outputMeta == null || outputMeta.isEmpty() || !outputMeta.contains("outputs")) |
| 91 | + throw new Exception("Output information is not readable: no output count or compound not readable."); |
| 92 | + |
| 93 | + if (outputMeta.getInt("outputs") > 0) { |
| 94 | + int numOutputs = outputMeta.getInt("outputs"); |
| 95 | + for (int out = 0; out < numOutputs; out++) { |
| 96 | + ItemStack output = buffer.readItemStack(); |
| 97 | + recipe.addOutput(output); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Writes recipe output information to a packet buffer. |
| 104 | + * |
| 105 | + * @param recipe Recipe to write output information for. |
| 106 | + * @param buffer Buffer to write data to. |
| 107 | + */ |
| 108 | + public static void writeRecipeOutputs(MiniaturizationRecipe recipe, PacketBuffer buffer) { |
| 109 | + ItemStack[] outputs = recipe.getOutputs(); |
| 110 | + |
| 111 | + CompoundNBT outputMeta = new CompoundNBT(); |
| 112 | + outputMeta.putInt("outputs", outputs.length); |
| 113 | + buffer.writeCompoundTag(outputMeta); |
| 114 | + |
| 115 | + if (outputs.length > 0) { |
| 116 | + for (ItemStack out : outputs) buffer.writeItemStack(out); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Reads catalyst information from a packet buffer. |
| 122 | + * |
| 123 | + * @param recipe The recipe to add catalyst information to. |
| 124 | + * @param buffer The buffer to read information from. |
| 125 | + * @throws Exception |
| 126 | + */ |
| 127 | + public static void readRecipeCatalysts(MiniaturizationRecipe recipe, PacketBuffer buffer) throws Exception { |
| 128 | + CompoundNBT tag = buffer.readCompoundTag(); |
| 129 | + if(tag == null || tag.isEmpty() || !tag.contains("type")) |
| 130 | + throw new Exception("Tag information is not readable: no type tag or compound not readable."); |
| 131 | + |
| 132 | + if(!tag.getString("type").equals(TYPE_CATALYSTS.toString())) |
| 133 | + throw new Exception("Tried to read a non-catalyst tag."); |
| 134 | + |
| 135 | + ItemStack output = buffer.readItemStack(); |
| 136 | + recipe.setCatalyst(output); |
| 137 | + } |
| 138 | + |
| 139 | + public static void writeRecipeCatalysts(MiniaturizationRecipe recipe, PacketBuffer buffer) { |
| 140 | + ItemStack catalyst = recipe.getCatalyst(); |
| 141 | + |
| 142 | + CompoundNBT outputMeta = new CompoundNBT(); |
| 143 | + outputMeta.putString("type", TYPE_CATALYSTS.toString()); |
| 144 | + buffer.writeCompoundTag(outputMeta); |
| 145 | + buffer.writeItemStack(catalyst); |
| 146 | + } |
| 147 | +} |
0 commit comments