Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce size of recipes in PacketBuffer's #4387

Open
wants to merge 5 commits into
base: 1.15
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"ici",
"ggg",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"geg",
"g g",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
" c ",
"gpg",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
" S ",
"S S",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
" i ",
"pep",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"GeG",
"GgG",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
" I ",
"PGP",
Expand All @@ -13,7 +13,7 @@
"tag": "forge:dusts/glowstone"
},
"P": {
"item": "minecraft:glass_pane"
"tag": "forge:glass_panes"
}
},
"result": {
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"sss",
"sds",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"pi ",
"c i",
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"ipi",
"pep"
Expand Down
@@ -1,5 +1,5 @@
{
"type": "minecraft:crafting_shaped",
"type": "immersiveengineering:compressed_shaped",
"pattern": [
"www",
"wbw",
Expand Down
Expand Up @@ -9,45 +9,59 @@

package blusunrize.immersiveengineering.api.crafting;

import blusunrize.immersiveengineering.api.utils.IEPacketBuffer;
import blusunrize.immersiveengineering.api.utils.ItemUtils;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.datafixers.util.Either;
import net.minecraft.fluid.Fluid;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tags.FluidTags;
import net.minecraft.tags.Tag;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.util.Lazy;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class FluidTagInput implements Predicate<FluidStack>
{
protected final Lazy<Tag<Fluid>> fluidTag;
protected final Either<Tag<Fluid>, List<Fluid>> content;
protected final int amount;
protected final CompoundNBT nbtTag;

public FluidTagInput(Lazy<Tag<Fluid>> fluidTag, int amount, CompoundNBT nbtTag)
public FluidTagInput(Either<Tag<Fluid>, List<Fluid>> content, int amount, CompoundNBT nbtTag)
{
this.fluidTag = fluidTag;
this.content = content;
this.amount = amount;
this.nbtTag = nbtTag;
}

public FluidTagInput(List<Fluid> fluids, int amount, CompoundNBT nbtTag)
{
this(Either.right(fluids), amount, nbtTag);
}

public FluidTagInput(Tag<Fluid> tag, int amount, CompoundNBT nbtTag)
{
this(Either.left(tag), amount, nbtTag);
}

public FluidTagInput(ResourceLocation resourceLocation, int amount, CompoundNBT nbtTag)
{
this(Lazy.of(() -> new FluidTags.Wrapper(resourceLocation)), amount, nbtTag);
this(new FluidTags.Wrapper(resourceLocation), amount, nbtTag);
}

public FluidTagInput(ResourceLocation resourceLocation, int amount)
Expand All @@ -72,17 +86,9 @@ public static FluidTagInput deserialize(JsonElement input)
}
}

public static FluidTagInput read(PacketBuffer input)
{
ResourceLocation resourceLocation = input.readResourceLocation();
int amount = input.readInt();
CompoundNBT nbt = input.readBoolean()?input.readCompoundTag(): null;
return new FluidTagInput(resourceLocation, amount, nbt);
}

public FluidTagInput withAmount(int amount)
{
return new FluidTagInput(this.fluidTag, amount, this.nbtTag);
return new FluidTagInput(this.content, amount, this.nbtTag);
}

@Override
Expand All @@ -95,7 +101,10 @@ public boolean testIgnoringAmount(@Nullable FluidStack fluidStack)
{
if(fluidStack==null)
return false;
if(!fluidTag.get().contains(fluidStack.getFluid()))
if(!content.map(
tag -> tag.contains(fluidStack.getFluid()),
list -> list.contains(fluidStack.getFluid())
))
return false;
if(this.nbtTag!=null)
return fluidStack.hasTag()&&fluidStack.getTag().equals(this.nbtTag);
Expand All @@ -105,19 +114,28 @@ public boolean testIgnoringAmount(@Nullable FluidStack fluidStack)
@Nonnull
public List<FluidStack> getMatchingFluidStacks()
{
Tag<Fluid> tag = this.fluidTag.get();
if(tag==null)
return Collections.emptyList();
return tag.getAllElements().stream()
return getMatchingFluids()
.stream()
.map(fluid -> new FluidStack(fluid, FluidTagInput.this.amount, FluidTagInput.this.nbtTag))
.collect(Collectors.toList());
}

private Collection<Fluid> getMatchingFluids() {
return content.map(
tag -> {
if(tag==null)
return ImmutableList.of();
return tag.getAllElements();
},
Function.identity()
);
}

@Nonnull
public JsonElement serialize()
{
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("tag", this.fluidTag.get().getId().toString());
jsonObject.addProperty("tag", this.content.orThrow().getId().toString());
jsonObject.addProperty("amount", this.amount);
if(this.nbtTag!=null)
jsonObject.addProperty("nbt", this.nbtTag.toString());
Expand All @@ -137,10 +155,22 @@ public FluidStack getRandomizedExampleStack(int rand)

public void write(PacketBuffer out)
{
out.writeResourceLocation(this.fluidTag.get().getId());
out.writeInt(this.amount);
IEPacketBuffer.wrap(out).writeList(
getMatchingFluids(), (f, buffer) -> buffer.writeRegistryIdUnsafe(ForgeRegistries.FLUIDS, f)
);
out.writeVarInt(this.amount);
out.writeBoolean(this.nbtTag!=null);
if(this.nbtTag!=null)
out.writeCompoundTag(this.nbtTag);
}

public static FluidTagInput read(PacketBuffer input)
{
List<Fluid> fluids = IEPacketBuffer.wrap(input).readList(
buffer -> buffer.readRegistryIdUnsafe(ForgeRegistries.FLUIDS)
);
int amount = input.readVarInt();
CompoundNBT nbt = input.readBoolean()?input.readCompoundTag(): null;
return new FluidTagInput(fluids, amount, nbt);
}
}
Expand Up @@ -19,12 +19,15 @@
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.registries.ForgeRegistryEntry;

public abstract class IERecipeSerializer<R extends IRecipe<?>> extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<R>
import javax.annotation.Nonnull;

//TODO new interface technically breaks binary compat?
public abstract class IERecipeSerializer<R extends IRecipe<?>> extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IIEBufferRecipeSerializer<R>
{
public abstract ItemStack getIcon();

@Override
public final R read(ResourceLocation recipeId, JsonObject json)
public final R read(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json)
{
if(CraftingHelper.processConditions(json, "conditions"))
return readFromJson(recipeId, json);
Expand Down