Skip to content

Commit

Permalink
Update to updated CommonCapabilities API
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Aug 9, 2024
1 parent bde657c commit 2139f56
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 36 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ minecraft_version=1.21.1
neoforge_version=21.1.2
parchment_version=2024.07.07
parchment_minecraft_version=1.21
cyclopscore_version=1.19.10-538
integrateddynamics_version=1.23.1-828
cyclopscore_version=1.19.10-539
integrateddynamics_version=1.23.1-830
release_type=release
fingerprint=bd0353b3e8a2810d60dd584e256e364bc3bedd44

commoncapabilities_version=2.9.2-143
commoncapabilities_version=2.9.2-145

# Workaround for Spotless bug
# https://github.com/diffplug/spotless/issues/834
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.Maps;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntArrayTag;
import net.minecraft.nbt.Tag;
Expand Down Expand Up @@ -142,16 +143,16 @@ public boolean isIgnoreDependencyCheck() {
return ignoreDependencyCheck;
}

public static CompoundTag serialize(CraftingJob craftingJob) {
public static CompoundTag serialize(HolderLookup.Provider lookupProvider, CraftingJob craftingJob) {
CompoundTag tag = new CompoundTag();
tag.putInt("id", craftingJob.id);
tag.putInt("channel", craftingJob.channel);
tag.put("recipe", IRecipeDefinition.serialize(craftingJob.recipe));
tag.put("recipe", IRecipeDefinition.serialize(lookupProvider, craftingJob.recipe));
tag.put("dependencies", new IntArrayTag(craftingJob.getDependencyCraftingJobs()));
tag.put("dependents", new IntArrayTag(craftingJob.getDependentCraftingJobs()));
tag.putInt("amount", craftingJob.amount);
tag.put("ingredientsStorage", IMixedIngredients.serialize(craftingJob.ingredientsStorage));
tag.put("lastMissingIngredients", MissingIngredients.serialize(craftingJob.lastMissingIngredients));
tag.put("ingredientsStorage", IMixedIngredients.serialize(lookupProvider, craftingJob.ingredientsStorage));
tag.put("lastMissingIngredients", MissingIngredients.serialize(lookupProvider, craftingJob.lastMissingIngredients));
tag.putLong("startTick", craftingJob.startTick);
tag.putBoolean("invalidInputs", craftingJob.invalidInputs);
if (craftingJob.initiatorUuid != null) {
Expand All @@ -161,7 +162,7 @@ public static CompoundTag serialize(CraftingJob craftingJob) {
return tag;
}

public static CraftingJob deserialize(CompoundTag tag) {
public static CraftingJob deserialize(HolderLookup.Provider lookupProvider, CompoundTag tag) {
if (!tag.contains("id", Tag.TAG_INT)) {
throw new IllegalArgumentException("Could not find an id entry in the given tag");
}
Expand Down Expand Up @@ -194,9 +195,9 @@ public static CraftingJob deserialize(CompoundTag tag) {
}
int id = tag.getInt("id");
int channel = tag.getInt("channel");
IRecipeDefinition recipe = IRecipeDefinition.deserialize(tag.getCompound("recipe"));
IRecipeDefinition recipe = IRecipeDefinition.deserialize(lookupProvider, tag.getCompound("recipe"));
int amount = tag.getInt("amount");
IMixedIngredients ingredientsStorage = IMixedIngredients.deserialize(tag.getCompound("ingredientsStorage"));
IMixedIngredients ingredientsStorage = IMixedIngredients.deserialize(lookupProvider, tag.getCompound("ingredientsStorage"));
CraftingJob craftingJob = new CraftingJob(id, channel, recipe, amount, ingredientsStorage);
for (int dependency : tag.getIntArray("dependencies")) {
craftingJob.dependencyCraftingJobs.add(dependency);
Expand All @@ -205,7 +206,7 @@ public static CraftingJob deserialize(CompoundTag tag) {
craftingJob.dependentCraftingJobs.add(dependent);
}
Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> lastMissingIngredients = MissingIngredients
.deserialize(tag.getCompound("lastMissingIngredients"));
.deserialize(lookupProvider, tag.getCompound("lastMissingIngredients"));
craftingJob.setLastMissingIngredients(lastMissingIngredients);
craftingJob.setStartTick(tag.getLong("startTick"));
craftingJob.setInvalidInputs(tag.getBoolean("invalidInputs"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntCollection;
import it.unimi.dsi.fastutil.ints.IntIterator;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.IntArrayTag;
import net.minecraft.nbt.ListTag;
Expand Down Expand Up @@ -239,12 +240,12 @@ public void mergeCraftingJobs(CraftingJob target, CraftingJob mergee, boolean ma
}
}

public static CompoundTag serialize(CraftingJobDependencyGraph graph) {
public static CompoundTag serialize(HolderLookup.Provider lookupProvider, CraftingJobDependencyGraph graph) {
CompoundTag tag = new CompoundTag();

ListTag craftingJobs = new ListTag();
for (CraftingJob craftingJob : graph.getCraftingJobs()) {
craftingJobs.add(CraftingJob.serialize(craftingJob));
craftingJobs.add(CraftingJob.serialize(lookupProvider, craftingJob));
}
tag.put("craftingJobs", craftingJobs);

Expand All @@ -269,7 +270,7 @@ public static CompoundTag serialize(CraftingJobDependencyGraph graph) {
return tag;
}

public static CraftingJobDependencyGraph deserialize(CompoundTag tag) {
public static CraftingJobDependencyGraph deserialize(HolderLookup.Provider lookupProvider, CompoundTag tag) {
if (!tag.contains("craftingJobs", Tag.TAG_LIST)) {
throw new IllegalArgumentException("Could not find a craftingJobs entry in the given tag");
}
Expand All @@ -283,7 +284,7 @@ public static CraftingJobDependencyGraph deserialize(CompoundTag tag) {
Int2ObjectMap<CraftingJob> craftingJobs = new Int2ObjectOpenHashMap<>();
ListTag craftingJobsTag = tag.getList("craftingJobs", Tag.TAG_COMPOUND);
for (int i = 0; i < craftingJobsTag.size(); i++) {
CraftingJob craftingJob = CraftingJob.deserialize(craftingJobsTag.getCompound(i));
CraftingJob craftingJob = CraftingJob.deserialize(lookupProvider, craftingJobsTag.getCompound(i));
craftingJobs.put(craftingJob.getId(), craftingJob);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.core.Direction;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
Expand Down Expand Up @@ -92,13 +93,13 @@ public CraftingJobHandler(int maxProcessingJobs, boolean blockingJobsMode,
this.nonBlockingJobsRunningAmount = new Int2IntOpenHashMap();
}

public void writeToNBT(CompoundTag tag) {
public void writeToNBT(HolderLookup.Provider lookupProvider, CompoundTag tag) {
tag.putBoolean("blockingJobsMode", this.blockingJobsMode);

ListTag processingCraftingJobs = new ListTag();
for (CraftingJob processingCraftingJob : this.processingCraftingJobs.values()) {
CompoundTag entriesTag = new CompoundTag();
entriesTag.put("craftingJob", CraftingJob.serialize(processingCraftingJob));
entriesTag.put("craftingJob", CraftingJob.serialize(lookupProvider, processingCraftingJob));

List<Map<IngredientComponent<?, ?>, List<IPrototypedIngredient<?, ?>>>> ingredientsEntries = this.processingCraftingJobsPendingIngredients.get(processingCraftingJob.getId());
ListTag pendingEntries = new ListTag();
Expand All @@ -114,7 +115,7 @@ public void writeToNBT(CompoundTag tag) {
IIngredientSerializer serializer = ingredientComponent.getSerializer();
for (IPrototypedIngredient<?, ?> prototypedIngredient : ingredientComponentListEntry.getValue()) {
CompoundTag instance = new CompoundTag();
instance.put("prototype", serializer.serializeInstance(prototypedIngredient.getPrototype()));
instance.put("prototype", serializer.serializeInstance(lookupProvider, prototypedIngredient.getPrototype()));
instance.put("condition", serializer.serializeCondition(prototypedIngredient.getCondition()));
instances.add(instance);
}
Expand All @@ -131,7 +132,7 @@ public void writeToNBT(CompoundTag tag) {

ListTag pendingCraftingJobs = new ListTag();
for (CraftingJob craftingJob : this.pendingCraftingJobs.values()) {
pendingCraftingJobs.add(CraftingJob.serialize(craftingJob));
pendingCraftingJobs.add(CraftingJob.serialize(lookupProvider, craftingJob));
}
tag.put("pendingCraftingJobs", pendingCraftingJobs);

Expand All @@ -148,7 +149,7 @@ public void writeToNBT(CompoundTag tag) {
tag.put("nonBlockingJobsRunningAmount", nonBlockingJobsRunningAmount);
}

public void readFromNBT(CompoundTag tag) {
public void readFromNBT(HolderLookup.Provider lookupProvider, CompoundTag tag) {
if (tag.contains("blockingJobsMode")) {
this.blockingJobsMode = tag.getBoolean("blockingJobsMode");
}
Expand All @@ -174,7 +175,7 @@ public void readFromNBT(CompoundTag tag) {
List<IPrototypedIngredient<?, ?>> pendingIngredients = Lists.newArrayList();
for (Tag instanceTagUnsafe : pendingIngredientTag.getList("instances", Tag.TAG_COMPOUND)) {
CompoundTag instanceTag = (CompoundTag) instanceTagUnsafe;
Object instance = serializer.deserializeInstance(instanceTag.get("prototype"));
Object instance = serializer.deserializeInstance(lookupProvider, instanceTag.get("prototype"));
Object condition = serializer.deserializeCondition(instanceTag.get("condition"));
pendingIngredients.add(new PrototypedIngredient(ingredientComponent, instance, condition));
}
Expand All @@ -200,7 +201,7 @@ public void readFromNBT(CompoundTag tag) {
List<IPrototypedIngredient<?, ?>> pendingIngredients = Lists.newArrayList();
for (Tag instanceTagUnsafe : pendingIngredientTag.getList("instances", Tag.TAG_COMPOUND)) {
CompoundTag instanceTag = (CompoundTag) instanceTagUnsafe;
Object instance = serializer.deserializeInstance(instanceTag.get("prototype"));
Object instance = serializer.deserializeInstance(lookupProvider, instanceTag.get("prototype"));
Object condition = serializer.deserializeCondition(instanceTag.get("condition"));
pendingIngredients.add(new PrototypedIngredient(ingredientComponent, instance, condition));
}
Expand All @@ -212,7 +213,7 @@ public void readFromNBT(CompoundTag tag) {
}
}

CraftingJob craftingJob = CraftingJob.deserialize(entryTag.getCompound("craftingJob"));
CraftingJob craftingJob = CraftingJob.deserialize(lookupProvider, entryTag.getCompound("craftingJob"));

this.processingCraftingJobs.put(craftingJob.getId(), craftingJob);
this.allCraftingJobs.put(craftingJob.getId(), craftingJob);
Expand All @@ -224,7 +225,7 @@ public void readFromNBT(CompoundTag tag) {

ListTag pendingCraftingJobs = tag.getList("pendingCraftingJobs", Tag.TAG_COMPOUND);
for (Tag craftingJob : pendingCraftingJobs) {
CraftingJob craftingJobInstance = CraftingJob.deserialize((CompoundTag) craftingJob);
CraftingJob craftingJobInstance = CraftingJob.deserialize(lookupProvider, (CompoundTag) craftingJob);
this.pendingCraftingJobs.put(craftingJobInstance.getId(), craftingJobInstance);
this.allCraftingJobs.put(craftingJobInstance.getId(), craftingJobInstance);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
Expand Down Expand Up @@ -45,15 +46,15 @@ public String toString() {
* @param ingredients Ingredients.
* @return An NBT representation of the given ingredients.
*/
public static CompoundTag serialize(Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> ingredients) {
public static CompoundTag serialize(HolderLookup.Provider lookupProvider, Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> ingredients) {
CompoundTag tag = new CompoundTag();
for (Map.Entry<IngredientComponent<?, ?>, MissingIngredients<?, ?>> entry : ingredients.entrySet()) {
ListTag missingIngredientsTag = new ListTag();
for (Element<?, ?> element : entry.getValue().getElements()) {
ListTag elementsTag = new ListTag();
for (PrototypedWithRequested<?, ?> alternative : element.getAlternatives()) {
CompoundTag alternativeTag = new CompoundTag();
alternativeTag.put("requestedPrototype", IPrototypedIngredient.serialize(alternative.getRequestedPrototype()));
alternativeTag.put("requestedPrototype", IPrototypedIngredient.serialize(lookupProvider, alternative.getRequestedPrototype()));
alternativeTag.putLong("quantityMissing", alternative.getQuantityMissing());
alternativeTag.putBoolean("inputReusable", element.isInputReusable()); // Hack, should actually be one level higher, but this is for backwards-compat
elementsTag.add(alternativeTag);
Expand All @@ -71,7 +72,7 @@ public static CompoundTag serialize(Map<IngredientComponent<?, ?>, MissingIngred
* @return A new mixed ingredients instance.
* @throws IllegalArgumentException If the given tag is invalid or does not contain data on the given ingredients.
*/
public static Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> deserialize(CompoundTag tag)
public static Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> deserialize(HolderLookup.Provider lookupProvider, CompoundTag tag)
throws IllegalArgumentException {
Map<IngredientComponent<?, ?>, MissingIngredients<?, ?>> map = Maps.newIdentityHashMap();
for (String componentName : tag.getAllKeys()) {
Expand All @@ -89,7 +90,7 @@ public static CompoundTag serialize(Map<IngredientComponent<?, ?>, MissingIngred
boolean inputReusable = false;
for (int j = 0; j < elementsTag.size(); j++) {
CompoundTag alternativeTag = elementsTag.getCompound(j);
IPrototypedIngredient<?, ?> requestedPrototype = IPrototypedIngredient.deserialize(alternativeTag.getCompound("requestedPrototype"));
IPrototypedIngredient<?, ?> requestedPrototype = IPrototypedIngredient.deserialize(lookupProvider, alternativeTag.getCompound("requestedPrototype"));
long quantityMissing = alternativeTag.getLong("quantityMissing");
inputReusable = alternativeTag.getBoolean("inputReusable");
alternatives.add(new PrototypedWithRequested<>(requestedPrototype, quantityMissing));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,12 @@ public void writeToNBT(ValueDeseralizationContext valueDeseralizationContext, Co
for (IngredientInstanceWrapper instanceWrapper : inventoryOutputBuffer) {
CompoundTag instanceTag = new CompoundTag();
instanceTag.putString("component", IngredientComponent.REGISTRY.getKey(instanceWrapper.getComponent()).toString());
instanceTag.put("instance", instanceWrapper.getComponent().getSerializer().serializeInstance(instanceWrapper.getInstance()));
instanceTag.put("instance", instanceWrapper.getComponent().getSerializer().serializeInstance(valueDeseralizationContext.holderLookupProvider(), instanceWrapper.getInstance()));
instanceTags.add(instanceTag);
}
tag.put("inventoryOutputBuffer", instanceTags);

this.craftingJobHandler.writeToNBT(tag);
this.craftingJobHandler.writeToNBT(valueDeseralizationContext.holderLookupProvider(), tag);
tag.putInt("channelCrafting", channelCrafting);

CompoundTag recipeSlotErrorsTag = new CompoundTag();
Expand Down Expand Up @@ -403,10 +403,10 @@ public void readFromNBT(ValueDeseralizationContext valueDeseralizationContext, C
String componentName = instanceTag.getString("component");
IngredientComponent<?, ?> component = IngredientComponent.REGISTRY.get(ResourceLocation.parse(componentName));
this.inventoryOutputBuffer.add(new IngredientInstanceWrapper(component,
component.getSerializer().deserializeInstance(instanceTag.get("instance"))));
component.getSerializer().deserializeInstance(valueDeseralizationContext.holderLookupProvider(), instanceTag.get("instance"))));
}

this.craftingJobHandler.readFromNBT(tag);
this.craftingJobHandler.readFromNBT(valueDeseralizationContext.holderLookupProvider(), tag);
this.channelCrafting = tag.getInt("channelCrafting");

this.recipeSlotMessages.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cyclops.integratedcrafting.ingredient;

import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.Tag;
Expand All @@ -8,12 +9,12 @@
public class IngredientSerializerLong implements IIngredientSerializer<Long, Boolean> {

@Override
public Tag serializeInstance(Long instance) {
public Tag serializeInstance(HolderLookup.Provider lookupProvider, Long instance) {
return LongTag.valueOf(instance);
}

@Override
public Long deserializeInstance(Tag tag) throws IllegalArgumentException {
public Long deserializeInstance(HolderLookup.Provider lookupProvider, Tag tag) throws IllegalArgumentException {
if (!(tag instanceof LongTag)) {
throw new IllegalArgumentException("This deserializer only accepts NBTTagInt");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package org.cyclops.integratedcrafting.ingredient;

import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.Tag;
import org.cyclops.commoncapabilities.api.ingredient.IIngredientSerializer;

public class IngredientSerializerStub<T, M> implements IIngredientSerializer<T, M> {
@Override
public Tag serializeInstance(T instance) {
public Tag serializeInstance(HolderLookup.Provider lookupProvider, T instance) {
return null;
}

@Override
public T deserializeInstance(Tag tag) throws IllegalArgumentException {
public T deserializeInstance(HolderLookup.Provider lookupProvider, Tag tag) throws IllegalArgumentException {
return null;
}

Expand Down

0 comments on commit 2139f56

Please sign in to comment.