Skip to content

Commit

Permalink
Move all REI code into REI package for safety
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiDragon committed Jul 12, 2023
1 parent 2e9efe3 commit 734c022
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 52 deletions.
3 changes: 2 additions & 1 deletion changelog/1.0.0-beta.2+1.20.1.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Fix recipe serialization not working on dedicated servers
* Make barrel recipe abort if connections change
* Make barrel recipe abort if connections change
* Make rei compat code slightly more safe
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package wraith.fabricaeexnihilo.compatibility.rei;

import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.util.EntryIngredients;
import me.shedaniel.rei.api.common.util.EntryStacks;
import wraith.fabricaeexnihilo.recipe.util.BlockIngredient;
import wraith.fabricaeexnihilo.recipe.util.FluidIngredient;

public class ReiIngredientUtil {
private ReiIngredientUtil() {}

public static EntryIngredient of(FluidIngredient fluid) {
return fluid.getValue().map(EntryIngredients::of, EntryIngredients::ofFluidTag);
}

public static EntryIngredient of(BlockIngredient fluid) {
return fluid.getValue().map(EntryIngredients::of, tag -> EntryIngredients.ofTag(tag, entry -> EntryStacks.of(entry.value())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.jetbrains.annotations.Nullable;
import wraith.fabricaeexnihilo.FabricaeExNihilo;
import wraith.fabricaeexnihilo.compatibility.rei.PluginEntry;
import wraith.fabricaeexnihilo.compatibility.rei.ReiIngredientUtil;
import wraith.fabricaeexnihilo.recipe.barrel.BarrelRecipe;
import wraith.fabricaeexnihilo.recipe.barrel.BarrelRecipeAction;
import wraith.fabricaeexnihilo.recipe.barrel.BarrelRecipeCondition;
Expand Down Expand Up @@ -56,13 +57,13 @@ public BarrelDisplay(BarrelRecipe recipe) {

private void processCondition(BarrelRecipeCondition condition) {
if (condition instanceof BarrelRecipeCondition.BlockAbove blockAbove) {
above = blockAbove.block().asReiIngredient();
above = ReiIngredientUtil.of(blockAbove.block());
} else if (condition instanceof BarrelRecipeCondition.FluidAbove fluidAbove) {
above = fluidAbove.fluid().asReiIngredient();
above = ReiIngredientUtil.of(fluidAbove.fluid());
} else if (condition instanceof BarrelRecipeCondition.BlockBelow blockBelow) {
below = blockBelow.block().asReiIngredient();
below = ReiIngredientUtil.of(blockBelow.block());
} else if (condition instanceof BarrelRecipeCondition.FluidIn fluidIn) {
inputFluid = fluidIn.fluid().asReiIngredient();
inputFluid = ReiIngredientUtil.of(fluidIn.fluid());
} else {
FabricaeExNihilo.LOGGER.warn("Unsupported barrel recipe condition in REI code: " + condition);
}
Expand All @@ -79,9 +80,9 @@ private void processAction(BarrelRecipeAction action) {
} else if (action instanceof BarrelRecipeAction.StoreFluid storeFluid) {
outputs.add(EntryIngredients.of(FluidStackHooksFabric.fromFabric(storeFluid.fluid(), storeFluid.amount())));
} else if (action instanceof BarrelRecipeAction.ConsumeFluid consumeFluid) {
inputFluid = consumeFluid.fluid().asReiIngredient().map(entryStack -> ClientEntryStacks.setFluidRenderRatio(EntryStacks.of(entryStack.<FluidStack>cast().getValue().copyWithAmount(consumeFluid.amount())), (float) consumeFluid.amount() / FluidConstants.BUCKET));
inputFluid = ReiIngredientUtil.of(consumeFluid.fluid()).map(entryStack -> ClientEntryStacks.setFluidRenderRatio(EntryStacks.of(entryStack.<FluidStack>cast().getValue().copyWithAmount(consumeFluid.amount())), (float) consumeFluid.amount() / FluidConstants.BUCKET));
} else if (action instanceof BarrelRecipeAction.ConvertBlock convertBlock) {
nearby = convertBlock.filter().asReiIngredient();
nearby = ReiIngredientUtil.of(convertBlock.filter());
conversionOutput = EntryIngredients.of(convertBlock.result().getBlock());
} else if (action instanceof BarrelRecipeAction.DropItem dropItem) {
outputs.add(EntryIngredients.of(dropItem.stack()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import net.minecraft.util.Identifier;
import wraith.fabricaeexnihilo.compatibility.rei.PluginEntry;
import wraith.fabricaeexnihilo.compatibility.rei.ReiIngredientUtil;
import wraith.fabricaeexnihilo.recipe.crucible.CrucibleHeatRecipe;
import wraith.fabricaeexnihilo.recipe.util.BlockIngredient;

import java.util.List;
import java.util.Optional;
Expand All @@ -16,7 +18,8 @@ public class CrucibleHeatDisplay implements Display {
private final Identifier id;

public CrucibleHeatDisplay(CrucibleHeatRecipe recipe) {
this.source = recipe.getBlock().asReiIngredient();
BlockIngredient blockIngredient = recipe.getBlock();
this.source = ReiIngredientUtil.of(blockIngredient);
this.heat = recipe.getHeat();
this.id = recipe.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.util.EntryIngredients;
import net.minecraft.util.Identifier;
import wraith.fabricaeexnihilo.compatibility.rei.ReiIngredientUtil;
import wraith.fabricaeexnihilo.recipe.ToolRecipe;
import wraith.fabricaeexnihilo.recipe.util.BlockIngredient;

import java.util.List;
import java.util.Optional;
Expand All @@ -19,7 +21,8 @@ public class ToolDisplay implements Display {

public ToolDisplay(ToolRecipe recipe, CategoryIdentifier<ToolDisplay> category) {
this.category = category;
this.block = recipe.getBlock().asReiIngredient();
BlockIngredient blockIngredient = recipe.getBlock();
this.block = ReiIngredientUtil.of(blockIngredient);
this.result = EntryIngredients.of(recipe.getResult().stack());
this.id = recipe.getId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import me.shedaniel.rei.api.common.util.EntryIngredients;
import net.minecraft.util.Identifier;
import wraith.fabricaeexnihilo.compatibility.rei.PluginEntry;
import wraith.fabricaeexnihilo.compatibility.rei.ReiIngredientUtil;
import wraith.fabricaeexnihilo.recipe.util.FluidIngredient;
import wraith.fabricaeexnihilo.recipe.witchwater.WitchWaterWorldRecipe;

import java.util.List;
Expand All @@ -17,7 +19,8 @@ public class WitchWaterWorldDisplay implements Display {
private final Identifier id;

public WitchWaterWorldDisplay(WitchWaterWorldRecipe recipe) {
this.input = recipe.getTarget().asReiIngredient();
FluidIngredient fluidIngredient = recipe.getTarget();
this.input = ReiIngredientUtil.of(fluidIngredient);
this.outputs = recipe.getResult().flatten(EntryIngredients::of);
this.id = recipe.getId();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package wraith.fabricaeexnihilo.recipe.crucible;

import com.google.gson.JsonObject;
import me.shedaniel.rei.api.common.entry.EntryStack;
import net.minecraft.block.BlockState;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.registry.Registries;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.world.World;
Expand All @@ -17,6 +19,7 @@
import wraith.fabricaeexnihilo.recipe.util.BlockIngredient;

import java.util.Optional;
import java.util.function.Predicate;

public class CrucibleHeatRecipe extends BaseRecipe<CrucibleHeatRecipe.Context> {
private final BlockIngredient block;
Expand Down Expand Up @@ -52,11 +55,15 @@ public RecipeType<?> getType() {

@Override
public ItemStack getDisplayStack() {
return block.asReiIngredient().stream()
.map(EntryStack::cheatsAs)
.map(EntryStack::getValue)
return block.getValue().map(ItemStack::new, tag -> Registries.BLOCK
.getEntryList(tag)
.stream()
.flatMap(RegistryEntryList::stream)
.map(RegistryEntry::value)
.map(ItemStack::new)
.filter(Predicate.not(ItemStack::isEmpty))
.findFirst()
.orElse(ItemStack.EMPTY);
.orElse(ItemStack.EMPTY));
}

public BlockIngredient getBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.util.EntryIngredients;
import me.shedaniel.rei.api.common.util.EntryStacks;
import com.mojang.datafixers.util.Either;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.network.PacketByteBuf;
Expand Down Expand Up @@ -82,7 +80,7 @@ public static BlockIngredient tag(TagKey<Block> tag) {

public abstract JsonElement toJson();

public abstract EntryIngredient asReiIngredient();
public abstract Either<Block, TagKey<Block>> getValue();

private static final class Single extends BlockIngredient {
private final Block block;
Expand Down Expand Up @@ -116,8 +114,8 @@ public JsonElement toJson() {
}

@Override
public EntryIngredient asReiIngredient() {
return EntryIngredients.of(block.asItem());
public Either<Block, TagKey<Block>> getValue() {
return Either.left(block);
}
}

Expand Down Expand Up @@ -153,8 +151,8 @@ public JsonElement toJson() {
}

@Override
public EntryIngredient asReiIngredient() {
return EntryIngredients.ofTag(tag, block -> EntryStacks.of(block.value().asItem(), 1));
public Either<Block, TagKey<Block>> getValue() {
return Either.right(tag);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.util.EntryIngredients;
import com.mojang.datafixers.util.Either;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.Fluids;
import net.minecraft.network.PacketByteBuf;
Expand Down Expand Up @@ -42,12 +41,12 @@ public static FluidIngredient tag(TagKey<Fluid> tag) {
return new Tag(tag);
}

public abstract Either<Fluid, TagKey<Fluid>> getValue();

public abstract void toPacket(PacketByteBuf buf);

public abstract JsonElement toJson();

public abstract EntryIngredient asReiIngredient();

public abstract boolean isEmpty();

private static final class Single extends FluidIngredient {
Expand All @@ -62,6 +61,11 @@ public boolean test(Fluid fluid) {
return fluid.matchesType(this.fluid);
}

@Override
public Either<Fluid, TagKey<Fluid>> getValue() {
return Either.left(fluid);
}

@Override
public void toPacket(PacketByteBuf buf) {
buf.writeByte(0);
Expand All @@ -73,11 +77,6 @@ public JsonElement toJson() {
return new JsonPrimitive(Registries.FLUID.getId(fluid).toString());
}

@Override
public EntryIngredient asReiIngredient() {
return EntryIngredients.of(fluid);
}

@Override
public boolean isEmpty() {
return fluid == Fluids.EMPTY;
Expand All @@ -96,6 +95,11 @@ public boolean test(Fluid fluid) {
return fluid.isIn(tag);
}

@Override
public Either<Fluid, TagKey<Fluid>> getValue() {
return Either.right(tag);
}

@Override
public void toPacket(PacketByteBuf buf) {
buf.writeByte(1);
Expand All @@ -107,11 +111,6 @@ public JsonElement toJson() {
return new JsonPrimitive("#" + tag.id().toString());
}

@Override
public EntryIngredient asReiIngredient() {
return EntryIngredients.ofFluidTag(tag);
}

@Override
public boolean isEmpty() {
return false;
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/wraith/fabricaeexnihilo/util/RegistryEntryLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,12 @@
import com.google.gson.JsonElement;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.JsonOps;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.entry.EntryStack;
import me.shedaniel.rei.api.common.util.EntryStacks;
import net.minecraft.item.ItemConvertible;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.*;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.entry.RegistryEntryList;

import java.util.function.Function;

public class RegistryEntryLists {
private static final DynamicRegistryManager STATIC_DRM = DynamicRegistryManager.of(Registries.REGISTRIES);

Expand All @@ -42,14 +35,6 @@ public static <T> void toPacket(RegistryKey<? extends Registry<T>> registry, Reg
buf.writeNbt(wrapper);
}

public static <T> EntryIngredient asReiIngredient(RegistryEntryList<T> list, Function<T, EntryStack<?>> transformer) {
return EntryIngredient.of(list.stream().map(RegistryEntry::value).map(transformer).toList());
}

public static EntryIngredient asReiIngredient(RegistryEntryList<? extends ItemConvertible> list) {
return EntryIngredient.of(list.stream().map(RegistryEntry::value).map(EntryStacks::of).toList());
}

private static <O> RegistryOps<O> getRegistryOps(DynamicOps<O> ops) {
return RegistryOps.of(ops, STATIC_DRM);
}
Expand Down

0 comments on commit 734c022

Please sign in to comment.