Cleanup RollingMachineRecipe syncing, should aid with #1851

This commit is contained in:
modmuss50 2019-10-11 16:18:10 +01:00
parent 264224a6f9
commit 643436d129

View file

@ -24,21 +24,17 @@
package techreborn.api.recipe.recipes; package techreborn.api.recipe.recipes;
import com.google.common.collect.Iterables;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import net.minecraft.inventory.CraftingInventory; import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient; import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.RecipeSerializer; import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.ShapedRecipe; import net.minecraft.recipe.ShapedRecipe;
import net.minecraft.tag.ItemTags;
import net.minecraft.tag.Tag;
import net.minecraft.util.DefaultedList; import net.minecraft.util.DefaultedList;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper; import net.minecraft.util.JsonHelper;
import net.minecraft.util.registry.Registry; import net.minecraft.util.PacketByteBuf;
import net.minecraft.world.World; import net.minecraft.world.World;
import reborncore.common.crafting.RebornRecipe; import reborncore.common.crafting.RebornRecipe;
import reborncore.common.crafting.RebornRecipeType; import reborncore.common.crafting.RebornRecipeType;
@ -50,7 +46,6 @@ import java.util.List;
public class RollingMachineRecipe extends RebornRecipe { public class RollingMachineRecipe extends RebornRecipe {
private ShapedRecipe shapedRecipe; private ShapedRecipe shapedRecipe;
private JsonObject shaped;
public RollingMachineRecipe(RebornRecipeType<?> type, Identifier name) { public RollingMachineRecipe(RebornRecipeType<?> type, Identifier name) {
super(type, name); super(type, name);
@ -58,35 +53,26 @@ public class RollingMachineRecipe extends RebornRecipe {
@Override @Override
public void deserialize(JsonObject jsonObject) { public void deserialize(JsonObject jsonObject) {
if(jsonObject.has("shaped")) {
JsonObject json = JsonHelper.getObject(jsonObject, "shaped"); JsonObject json = JsonHelper.getObject(jsonObject, "shaped");
shapedRecipe = RecipeSerializer.SHAPED.read(getId(), json); shapedRecipe = RecipeSerializer.SHAPED.read(getId(), json);
shaped = withoutTags(json); } else {
//This will be handled by the PacketByteBuf deserialize
}
} }
@Override @Override
public void serialize(JsonObject jsonObject) { public void serialize(PacketByteBuf byteBuf) {
jsonObject.add("shaped", shaped); String s = shapedRecipe.getId().toString();
byteBuf.writeInt(s.length());
byteBuf.writeString(s);
RecipeSerializer.SHAPED.write(byteBuf, shapedRecipe);
} }
//This needs to be done to remove the item tags, and replace them with just items for when syncing to the server @Override
private JsonObject withoutTags(JsonObject jsonObject) { public void deserialize(PacketByteBuf byteBuf) {
JsonObject key = jsonObject.get("key").getAsJsonObject(); Identifier identifier = new Identifier(byteBuf.readString(byteBuf.readInt()));
key.entrySet().forEach(entry -> { shapedRecipe = RecipeSerializer.SHAPED.read(identifier, byteBuf);
if(entry.getValue().isJsonObject()){
JsonObject value = entry.getValue().getAsJsonObject();
if(value.has("tag")){
Identifier tag = new Identifier(value.get("tag").getAsString());
Tag<Item> itemTag = ItemTags.getContainer().get(tag);
Item item = Iterables.get(itemTag.values(), 0);
//Remove the tag, and replace it with a basic s
value.remove("tag");
value.addProperty("item", Registry.ITEM.getId(item).toString());
}
}
});
return jsonObject;
} }
@Override @Override