Skip to content

Commit 517e699

Browse files
committed
Extract buffer implementations to dedicated class, add catalyst info
1 parent c146659 commit 517e699

File tree

2 files changed

+158
-70
lines changed

2 files changed

+158
-70
lines changed
Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
package com.robotgryphon.compactcrafting.recipes.data;
22

33
import com.google.gson.JsonObject;
4-
import com.mojang.serialization.DataResult;
54
import com.robotgryphon.compactcrafting.CompactCrafting;
65
import com.robotgryphon.compactcrafting.recipes.MiniaturizationRecipe;
76
import com.robotgryphon.compactcrafting.recipes.data.json.MiniaturizationRecipeJsonSerializer;
8-
import net.minecraft.block.BlockState;
9-
import net.minecraft.item.ItemStack;
7+
import com.robotgryphon.compactcrafting.recipes.data.util.RecipeBufferDataUtil;
108
import net.minecraft.item.crafting.IRecipeSerializer;
11-
import net.minecraft.nbt.CompoundNBT;
12-
import net.minecraft.nbt.INBT;
13-
import net.minecraft.nbt.ListNBT;
14-
import net.minecraft.nbt.NBTDynamicOps;
159
import net.minecraft.network.PacketBuffer;
1610
import net.minecraft.util.ResourceLocation;
17-
import net.minecraftforge.common.util.Constants;
1811
import net.minecraftforge.registries.ForgeRegistryEntry;
1912

2013
import javax.annotation.Nullable;
@@ -34,74 +27,22 @@ public MiniaturizationRecipe read(ResourceLocation recipeId, JsonObject json) {
3427
public MiniaturizationRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
3528
MiniaturizationRecipe recipe = new MiniaturizationRecipe(recipeId);
3629

37-
CompoundNBT outputMeta = buffer.readCompoundTag();
38-
if (outputMeta.getInt("outputs") > 0) {
39-
int numOutputs = outputMeta.getInt("outputs");
40-
for (int out = 0; out < numOutputs; out++) {
41-
ItemStack output = buffer.readItemStack();
42-
recipe.addOutput(output);
43-
}
30+
try {
31+
RecipeBufferDataUtil.readRecipeCatalysts(recipe, buffer);
32+
RecipeBufferDataUtil.readRecipeOutputs(recipe, buffer);
33+
RecipeBufferDataUtil.readComponentInfo(recipe, buffer);
34+
} catch (Exception e) {
35+
CompactCrafting.LOGGER.error(e);
4436
}
4537

46-
CompoundNBT components = buffer.readCompoundTag();
47-
int numComponents = components.getInt("count");
48-
if(numComponents > 0) {
49-
ListNBT compList = components.getList("components", Constants.NBT.TAG_COMPOUND);
50-
compList.forEach(comp -> {
51-
CompoundNBT compC = (CompoundNBT) comp;
52-
String key = compC.getString("key");
53-
CompoundNBT stateTag = compC.getCompound("state");
54-
55-
BlockState.CODEC.decode(NBTDynamicOps.INSTANCE, stateTag)
56-
.resultOrPartial(CompactCrafting.LOGGER::error)
57-
.ifPresent(state -> {
58-
BlockState compState = state.getFirst();
59-
recipe.addComponent(key, compState);
60-
61-
CompactCrafting.LOGGER.debug("Got component: {} ({})", key, compState.toString());
62-
});
63-
});
64-
}
6538
return recipe;
6639
}
6740

6841
@Override
6942
public void write(PacketBuffer buffer, MiniaturizationRecipe recipe) {
70-
ItemStack[] outputs = recipe.getOutputs();
71-
72-
CompoundNBT outputMeta = new CompoundNBT();
73-
outputMeta.putInt("outputs", outputs.length);
74-
buffer.writeCompoundTag(outputMeta);
75-
76-
if (outputs.length > 0) {
77-
for (ItemStack out : outputs) buffer.writeItemStack(out);
78-
}
79-
80-
try {
81-
CompoundNBT componentMeta = new CompoundNBT();
82-
int numComponents = recipe.getComponentKeys().size();
83-
componentMeta.putInt("count", numComponents);
84-
85-
if (numComponents > 0) {
86-
ListNBT compList = new ListNBT();
87-
recipe.getComponents().forEach((key, state) -> {
88-
DataResult<INBT> encode = BlockState.CODEC.encode(state, NBTDynamicOps.INSTANCE, null);
89-
encode
90-
.resultOrPartial(CompactCrafting.LOGGER::error)
91-
.ifPresent(stateNbt -> {
92-
CompoundNBT componentTag = new CompoundNBT();
93-
componentTag.put("state", stateNbt);
94-
componentTag.putString("key", key);
95-
96-
compList.add(componentTag);
97-
});
98-
});
99-
100-
componentMeta.put("components", compList);
101-
}
102-
103-
buffer.writeCompoundTag(componentMeta);
104-
} catch (Exception ex) {
105-
}
43+
RecipeBufferDataUtil.writeRecipeCatalysts(recipe, buffer);
44+
RecipeBufferDataUtil.writeRecipeOutputs(recipe, buffer);
45+
RecipeBufferDataUtil.writeComponentInfo(recipe, buffer);
10646
}
47+
10748
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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

Comments
 (0)