Skip to content

Commit

Permalink
Move/rename ImmutableGristSet interface into GristSet.Immutable (#644)
Browse files Browse the repository at this point in the history
Due to its small size and its relation to the base interface, it seemed
fitting to move it inside the GristSet interface.
  • Loading branch information
kirderf1 authored Nov 26, 2024
1 parent 9de2886 commit 4ca6198
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mraof.minestuck.api.alchemy.GristSet;
import com.mraof.minestuck.api.alchemy.GristType;
import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.recipe.GristCostRecipe;
import com.mraof.minestuck.api.alchemy.recipe.JeiGristCost;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratedCostProvider;
Expand Down Expand Up @@ -36,10 +35,10 @@ public final class GristCost implements GristCostRecipe
private final Ingredient ingredient;
@Nullable
private final Integer priority;
private final ImmutableGristSet cost;
private final GristSet.Immutable cost;

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public GristCost(Ingredient ingredient, ImmutableGristSet cost, Optional<Integer> priority)
public GristCost(Ingredient ingredient, GristSet.Immutable cost, Optional<Integer> priority)
{
this.ingredient = ingredient;
this.priority = priority.orElse(null);
Expand Down Expand Up @@ -115,7 +114,7 @@ private static GristCost fromNetwork(RegistryFriendlyByteBuf buffer)
{
Ingredient ingredient = Ingredient.CONTENTS_STREAM_CODEC.decode(buffer);
int priority = buffer.readInt();
ImmutableGristSet cost = GristSet.Codecs.STREAM_CODEC.decode(buffer);
GristSet.Immutable cost = GristSet.Codecs.STREAM_CODEC.decode(buffer);

return new GristCost(ingredient, cost, Optional.of(priority));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mraof.minestuck.api.alchemy.GristSet;
import com.mraof.minestuck.api.alchemy.GristType;
import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.recipe.GristCostRecipe;
import com.mraof.minestuck.api.alchemy.recipe.JeiGristCost;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratedCostProvider;
Expand Down Expand Up @@ -40,21 +39,21 @@ public final class ContainerGristCost implements GristCostRecipe
private static final Logger LOGGER = LogManager.getLogger();

private final Ingredient ingredient;
private final ImmutableGristSet addedCost;
private final GristSet.Immutable addedCost;
@Nullable
private final Integer priority;
private final GeneratedGristCostCache cache = new GeneratedGristCostCache();

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public ContainerGristCost(Ingredient ingredient, ImmutableGristSet addedCost, Optional<Integer> priority)
public ContainerGristCost(Ingredient ingredient, GristSet.Immutable addedCost, Optional<Integer> priority)
{
this.ingredient = ingredient;
this.addedCost = addedCost;
this.priority = priority.orElse(null);
}

@Nullable
private GristSet generateCost(GeneratorCallback callback, ImmutableGristSet addedCost, ResourceLocation recipeId)
private GristSet generateCost(GeneratorCallback callback, GristSet.Immutable addedCost, ResourceLocation recipeId)
{
ItemStack container = ingredient.getItems().length > 0 ? ingredient.getItems()[0].getCraftingRemainingItem() : ItemStack.EMPTY;
if(!container.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mraof.minestuck.alchemy.recipe.generator;

import com.mraof.minestuck.api.alchemy.GristSet;
import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratedCostProvider;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratorCallback;
import com.mraof.minestuck.api.alchemy.recipe.generator.GristCostResult;
Expand All @@ -14,7 +13,7 @@
public final class GeneratedGristCostCache
{
@Nullable
private ImmutableGristSet cachedCost = null;
private GristSet.Immutable cachedCost = null;
private boolean hasGeneratedCost = false;

public void fromNetwork(RegistryFriendlyByteBuf buffer)
Expand All @@ -37,7 +36,7 @@ public void toNetwork(RegistryFriendlyByteBuf buffer)
}

@Nullable
public ImmutableGristSet getCachedCost()
public GristSet.Immutable getCachedCost()
{
return this.cachedCost;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mraof.minestuck.api.alchemy.GristSet;
import com.mraof.minestuck.api.alchemy.GristType;
import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.MutableGristSet;
import com.mraof.minestuck.api.alchemy.recipe.GristCostRecipe;
import com.mraof.minestuck.api.alchemy.recipe.JeiGristCost;
Expand Down Expand Up @@ -42,13 +41,13 @@ public final class SourceGristCost implements GristCostRecipe
private final Ingredient ingredient;
private final List<Source> sources;
private final float multiplier;
private final ImmutableGristSet addedCost;
private final GristSet.Immutable addedCost;
@Nullable
private final Integer priority;
private final GeneratedGristCostCache cache = new GeneratedGristCostCache();

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public SourceGristCost(Ingredient ingredient, List<Source> sources, float multiplier, ImmutableGristSet addedCost, Optional<Integer> priority)
public SourceGristCost(Ingredient ingredient, List<Source> sources, float multiplier, GristSet.Immutable addedCost, Optional<Integer> priority)
{
this.ingredient = ingredient;
this.sources = sources;
Expand All @@ -70,7 +69,7 @@ public int getPriority()
}

@Nullable
private GristSet generateCost(GeneratorCallback callback, List<Source> sources, float multiplier, ImmutableGristSet addedCost)
private GristSet generateCost(GeneratorCallback callback, List<Source> sources, float multiplier, GristSet.Immutable addedCost)
{
MutableGristSet costSum = MutableGristSet.newDefault();
for(Source source : sources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mraof.minestuck.api.alchemy.GristSet;
import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.MutableGristSet;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratorCallback;
import com.mraof.minestuck.api.alchemy.recipe.generator.LookupTracker;
Expand All @@ -13,7 +12,7 @@

import java.util.List;

public record CookingCostInterpreter(ImmutableGristSet fuelCost) implements RecipeInterpreter
public record CookingCostInterpreter(GristSet.Immutable fuelCost) implements RecipeInterpreter
{
public static final MapCodec<CookingCostInterpreter> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
GristSet.Codecs.MAP_CODEC.fieldOf("added_cost").forGetter(CookingCostInterpreter::fuelCost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mraof.minestuck.Minestuck;
import com.mraof.minestuck.api.alchemy.GristSet;
import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.recipe.JeiGristCost;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratedCostProvider;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratorCallback;
Expand Down Expand Up @@ -53,15 +52,15 @@ public class RecipeGeneratedCostHandler extends SimplePreparableReloadListener<L
public static final String PATH = "minestuck/grist_cost_generation_recipes.json";

private final RecipeManager recipeManager;
private Map<Item, ImmutableGristSet> generatedCosts = Collections.emptyMap();
private Map<Item, GristSet.Immutable> generatedCosts = Collections.emptyMap();
private RecipeGeneratedCostProcess process = null;

private RecipeGeneratedCostHandler(RecipeManager recipeManager)
{
this.recipeManager = recipeManager;
}

private RecipeGeneratedCostHandler(Map<Item, ImmutableGristSet> generatedCosts)
private RecipeGeneratedCostHandler(Map<Item, GristSet.Immutable> generatedCosts)
{
recipeManager = null;
this.generatedCosts = generatedCosts;
Expand All @@ -77,15 +76,15 @@ public static void addListener(AddReloadListenerEvent event)
* Returns an immutable map of all grist costs generated from recipes.
* If called before grist cost generation has finished, an empty map will be returned.
*/
public Map<Item, ImmutableGristSet> getMap()
public Map<Item, GristSet.Immutable> getMap()
{
return generatedCosts;
}

void write(RegistryFriendlyByteBuf buffer)
{
buffer.writeInt(generatedCosts.size());
for(Map.Entry<Item, ImmutableGristSet> entry : generatedCosts.entrySet())
for(Map.Entry<Item, GristSet.Immutable> entry : generatedCosts.entrySet())
{
buffer.writeVarInt(Item.getId(entry.getKey()));
GristSet.Codecs.STREAM_CODEC.encode(buffer, entry.getValue());
Expand All @@ -99,11 +98,11 @@ static RecipeGeneratedCostHandler read(RegistryFriendlyByteBuf buffer)
return null;

int size = buffer.readInt();
ImmutableMap.Builder<Item, ImmutableGristSet> builder = new ImmutableMap.Builder<>();
ImmutableMap.Builder<Item, GristSet.Immutable> builder = new ImmutableMap.Builder<>();
for(int i = 0; i < size; i++)
{
Item item = Item.byId(buffer.readVarInt());
ImmutableGristSet cost = GristSet.Codecs.STREAM_CODEC.decode(buffer);
GristSet.Immutable cost = GristSet.Codecs.STREAM_CODEC.decode(buffer);
builder.put(item, cost);
}
return new RecipeGeneratedCostHandler(builder.build());
Expand All @@ -112,7 +111,7 @@ static RecipeGeneratedCostHandler read(RegistryFriendlyByteBuf buffer)
List<JeiGristCost> createJeiCosts()
{
List<JeiGristCost> costs = new ArrayList<>();
for(Map.Entry<Item, ImmutableGristSet> entries : generatedCosts.entrySet())
for(Map.Entry<Item, GristSet.Immutable> entries : generatedCosts.entrySet())
{
if(entries.getValue() != null)
costs.add(new JeiGristCost.Set(Ingredient.of(entries.getKey()), entries.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mraof.minestuck.MinestuckConfig;
import com.mraof.minestuck.alchemy.recipe.generator.GenerationContext;
import com.mraof.minestuck.api.alchemy.GristSet;
import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.recipe.generator.GeneratorCallback;
import com.mraof.minestuck.api.alchemy.recipe.generator.GristCostResult;
import com.mraof.minestuck.api.alchemy.recipe.generator.LookupTracker;
Expand All @@ -21,14 +20,14 @@ class RecipeGeneratedCostProcess
private static final Logger LOGGER = LogManager.getLogger();

private final Map<Item, List<Pair<RecipeHolder<?>, RecipeInterpreter>>> lookupMap;
private final Map<Item, ImmutableGristSet> generatedCosts = new HashMap<>();
private final Map<Item, GristSet.Immutable> generatedCosts = new HashMap<>();

RecipeGeneratedCostProcess(Map<Item, List<Pair<RecipeHolder<?>, RecipeInterpreter>>> lookupMap)
{
this.lookupMap = lookupMap;
}

Map<Item, ImmutableGristSet> buildMap()
Map<Item, GristSet.Immutable> buildMap()
{
//Clean out null grist costs
generatedCosts.entrySet().removeIf(entry -> entry.getValue() == null);
Expand Down Expand Up @@ -120,4 +119,4 @@ private void checkRecipeLogging(Item item, GristSet cost, GenerationContext cont
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* An immutable grist set that can hold any number of grist types.
* Suitable when an immutable grist set is needed, but {@link GristAmount} is insufficient.
*/
public final class DefaultImmutableGristSet implements ImmutableGristSet
public final class DefaultImmutableGristSet implements GristSet.Immutable
{
private final ImmutableMap<GristType, Long> map;

Expand All @@ -28,7 +28,7 @@ public DefaultImmutableGristSet(ImmutableMap.Builder<GristType, Long> builder)
map = builder.build();
}

public static ImmutableGristSet create(List<GristAmount> amounts)
public static GristSet.Immutable create(List<GristAmount> amounts)
{
ImmutableMap.Builder<GristType, Long> builder = ImmutableMap.builder();
for(GristAmount gristAmount : amounts)
Expand All @@ -41,4 +41,4 @@ public Map<GristType, Long> asMap()
{
return this.map;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;
Expand All @@ -18,7 +17,7 @@
/**
* Container for a GristType + integer combination that might be useful when iterating through a GristSet.
*/
public record GristAmount(GristType type, long amount) implements ImmutableGristSet
public record GristAmount(GristType type, long amount) implements GristSet.Immutable
{
public static final Codec<GristAmount> CODEC = RecordCodecBuilder.create(instance ->
instance.group(GristTypes.REGISTRY.byNameCodec().fieldOf("type").forGetter(GristAmount::type),
Expand Down Expand Up @@ -92,4 +91,4 @@ public Component asTextComponent()
GristAmount::amount,
GristAmount::new
);
}
}
30 changes: 22 additions & 8 deletions src/main/java/com/mraof/minestuck/api/alchemy/GristSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* An interface for anything that might contain grist.
* There are several different implementations of this interface with different properties.
* <p>
* If you want to limit yourself to a set that doesn't change after creation, check out {@link ImmutableGristSet}.
* If you want to limit yourself to a set that doesn't change after creation, check out {@link GristSet.Immutable}.
* You can create an immutable grist set using {@link GristSet#of(GristAmount...)},
* or you can create an instance of {@link DefaultImmutableGristSet} directly.
* You can convert any grist set into an immutable grist set using {@link GristSet#asImmutable()}.
Expand Down Expand Up @@ -68,7 +68,7 @@ default double getValue()
*/
Map<GristType, Long> asMap();

ImmutableGristSet asImmutable();
GristSet.Immutable asImmutable();

default MutableGristSet mutableCopy()
{
Expand All @@ -94,9 +94,9 @@ default Component asTextComponent()
else return Component.empty();
}

ImmutableGristSet EMPTY = Collections::emptyMap;
GristSet.Immutable EMPTY = Collections::emptyMap;

static ImmutableGristSet of(GristAmount... amounts)
static GristSet.Immutable of(GristAmount... amounts)
{
if(amounts.length == 0)
return GristSet.EMPTY;
Expand All @@ -109,21 +109,35 @@ static ImmutableGristSet of(GristAmount... amounts)
return new DefaultImmutableGristSet(builder);
}

/**
* A version of {@link GristSet}, but with the extra guarantee that this set cannot be changed.
* Suitable for things like recipes, where the set should not be allowed to change after the recipe was loaded.
* Can be obtained from any grist set by calling {@link GristSet#asImmutable()}.
*/
interface Immutable extends GristSet
{
@Override
default Immutable asImmutable()
{
return this;
}
}

/**
* Container for different codecs for grist sets.
* Codecs are not placed directly in the grist set class to avoid cyclic class loading between grist set and grist amount.
*/
final class Codecs
{
public static final Codec<ImmutableGristSet> NON_NEGATIVE_CODEC = GristAmount.NON_NEGATIVE_LIST_CODEC.xmap(DefaultImmutableGristSet::create, ImmutableGristSet::asAmounts);
public static final Codec<GristSet.Immutable> NON_NEGATIVE_CODEC = GristAmount.NON_NEGATIVE_LIST_CODEC.xmap(DefaultImmutableGristSet::create, GristSet::asAmounts);
/**
* Codec for serializing a grist set in a map format. Currently used for json serialization.
* Perhaps we should prefer this format for nbt-serialization as well? Something worth considering for the future.
*/
public static final Codec<ImmutableGristSet> MAP_CODEC = Codec.unboundedMap(GristTypes.REGISTRY.byNameCodec(), Codec.LONG).xmap(DefaultImmutableGristSet::new, ImmutableGristSet::asMap);
public static final Codec<ImmutableGristSet> LIST_CODEC = GristAmount.LIST_CODEC.xmap(DefaultImmutableGristSet::create, ImmutableGristSet::asAmounts);
public static final Codec<GristSet.Immutable> MAP_CODEC = Codec.unboundedMap(GristTypes.REGISTRY.byNameCodec(), Codec.LONG).xmap(DefaultImmutableGristSet::new, GristSet::asMap);
public static final Codec<GristSet.Immutable> LIST_CODEC = GristAmount.LIST_CODEC.xmap(DefaultImmutableGristSet::create, GristSet::asAmounts);

public static final StreamCodec<RegistryFriendlyByteBuf, ImmutableGristSet> STREAM_CODEC = StreamCodec.composite(
public static final StreamCodec<RegistryFriendlyByteBuf, GristSet.Immutable> STREAM_CODEC = StreamCodec.composite(
GristAmount.STREAM_CODEC.apply(ByteBufCodecs.list()),
GristSet::asAmounts,
DefaultImmutableGristSet::create
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static MutableGristSet newDefault()
}

@Override
default ImmutableGristSet asImmutable()
default GristSet.Immutable asImmutable()
{
return new DefaultImmutableGristSet(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mraof.minestuck.api.alchemy.recipe;

import com.mraof.minestuck.api.alchemy.ImmutableGristSet;
import com.mraof.minestuck.api.alchemy.GristSet;
import net.minecraft.FieldsAreNonnullByDefault;
import net.minecraft.world.item.crafting.Ingredient;

Expand All @@ -13,7 +13,7 @@ public sealed interface JeiGristCost permits JeiGristCost.Set, JeiGristCost.Wild

Ingredient ingredient();

record Set(Ingredient ingredient, ImmutableGristSet gristSet) implements JeiGristCost
record Set(Ingredient ingredient, GristSet.Immutable gristSet) implements JeiGristCost
{
public Set
{
Expand Down
Loading

0 comments on commit 4ca6198

Please sign in to comment.