Skip to content

Commit

Permalink
feat: use AlmostUnified preferred stack for miner recipes, if mod loaded
Browse files Browse the repository at this point in the history
Note: On 1.19.3. this is not yet possible due to AU not being updated yet
  • Loading branch information
klikli-dev committed Feb 1, 2023
1 parent 1927087 commit 322ed0d
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ jobs:
game_versions: Forge,Minecraft 1.19:${{ steps.get_mc_version.outputs.result }}
display_name: occultism-${{ steps.get_mc_version.outputs.result }}-${{ steps.get_version.outputs.result }}.jar
release_type: ${{ steps.get_release_type.outputs.result }}
relations: modonomicon:requiredDependency,curios:requiredDependency,geckolib:requiredDependency,smartbrainlib:requiredDependency,theurgy:optionalDependency,jei:optionalDependency
relations: modonomicon:requiredDependency,curios:requiredDependency,geckolib:requiredDependency,smartbrainlib:requiredDependency,theurgy:optionalDependency,jei:optionalDependency,almost-unified:optionalDependency
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ dependencies {
//smartbrainlib
implementation fg.deobf("curse.maven:smartbrainlib-661293:${smartbrainlib_file_id}-sources-${smartbrainlib_src_file_id}")

//almostunified
//TODO: enable once updated
// compileOnly fg.deobf("curse.maven:almostunified-633823:${almostunified_file_id}")
// runtimeOnly fg.deobf("curse.maven:almostunified-633823:${almostunified_file_id}")

//modonomicon
implementation fg.deobf("com.klikli_dev:modonomicon:${modonomicon_mc_version}-${modonomicon_version}") //we build against full jar because we NEED IT ALL

Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ geckolib_version=4.0.2
smartbrainlib_file_id=4312180
smartbrainlib_src_file_id=4312182

# Almost Unified v0.3.3
almostunified_file_id=TODO

modonomicon_mc_version=1.19.3
modonomicon_version=1.23.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package com.github.klikli_dev.occultism.common.blockentity;

import com.github.klikli_dev.occultism.common.container.DimensionalMineshaftContainer;
import com.github.klikli_dev.occultism.common.misc.WeightedIngredient;
import com.github.klikli_dev.occultism.common.misc.WeightedOutputIngredient;
import com.github.klikli_dev.occultism.crafting.recipe.MinerRecipe;
import com.github.klikli_dev.occultism.exceptions.ItemHandlerMissingException;
import com.github.klikli_dev.occultism.registry.OccultismRecipes;
Expand Down Expand Up @@ -91,7 +91,7 @@ protected void onContentsChanged(int slot) {
public int maxMiningTime = 0;
public int rollsPerOperation = 0;
protected Item currentInputType;
protected List<WeightedIngredient> possibleResults;
protected List<WeightedOutputIngredient> possibleResults;

//endregion Fields
//region Initialization
Expand Down Expand Up @@ -251,7 +251,7 @@ public void mine() {
return;

for (int i = 0; i < this.rollsPerOperation; i++) {
Optional<WeightedIngredient> result = WeightedRandom.getRandomItem(this.level.random, this.possibleResults);
Optional<WeightedOutputIngredient> result = WeightedRandom.getRandomItem(this.level.random, this.possibleResults);
//Important: copy the result, don't use it raw!
result.ifPresent(r -> {
ItemHandlerHelper.insertItemStacked(outputHandler, r.getStack().copy(), false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.klikli_dev.occultism.common.misc;

import com.github.klikli_dev.occultism.integration.almostunified.AlmostUnifiedIntegration;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;

import java.util.Arrays;
import java.util.stream.Stream;

/**
* A class that makes it easier to use ingredients as output and handles AlmostUnified Integration
*/
public class OutputIngredient {

protected Ingredient ingredient;
protected ItemStack[] itemStacks;

public OutputIngredient(Ingredient ingredient) {
this.ingredient = ingredient;
}

public ItemStack getStack() {
//copied from Ingredient.dissolve, but modified to handle tag ingredient preferred items
if (this.itemStacks == null) {
this.itemStacks = Arrays.stream(this.ingredient.values).flatMap((value) -> {
if (value instanceof Ingredient.TagValue tagValue) {
if (AlmostUnifiedIntegration.isLoaded()) {
return Stream.of(AlmostUnifiedIntegration.getPreferredItemForTag(tagValue.tag));
}
}
return value.getItems().stream();
}).distinct().toArray(ItemStack[]::new);
}
return this.itemStacks[0];
}

public Ingredient getIngredient() {
return this.ingredient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;

public class WeightedIngredient extends WeightedEntry.IntrusiveBase {
protected Ingredient ingredient;
public class WeightedOutputIngredient extends WeightedEntry.IntrusiveBase {
protected OutputIngredient ingredient;

public WeightedIngredient(Ingredient ingredient, int itemWeightIn) {
public WeightedOutputIngredient(Ingredient ingredient, int itemWeightIn) {
super(itemWeightIn);
this.ingredient = ingredient;
this.ingredient = new OutputIngredient(ingredient);
}

public ItemStack getStack() {
return this.ingredient.getItems()[0];
return this.ingredient.getStack();
}

public Ingredient getIngredient() {
return this.ingredient;
return this.ingredient.getIngredient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

package com.github.klikli_dev.occultism.crafting.recipe;

import com.github.klikli_dev.occultism.common.misc.WeightedIngredient;
import com.github.klikli_dev.occultism.common.misc.WeightedOutputIngredient;
import com.github.klikli_dev.occultism.registry.OccultismRecipes;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -39,28 +39,21 @@
import net.minecraftforge.items.wrapper.RecipeWrapper;

public class MinerRecipe implements Recipe<RecipeWrapper> {
//region Fields
public static Serializer SERIALIZER = new Serializer();
protected final ResourceLocation id;
protected final Ingredient input;
protected final WeightedIngredient output;
//endregion Fields
protected final WeightedOutputIngredient output;

//region Initialization
public MinerRecipe(ResourceLocation id, Ingredient input, WeightedIngredient output) {
public MinerRecipe(ResourceLocation id, Ingredient input, WeightedOutputIngredient output) {
this.input = input;
this.output = output;
this.id = id;
}
//endregion Initialization

//region Getter / Setter
public WeightedIngredient getWeightedOutput() {
public WeightedOutputIngredient getWeightedOutput() {
return this.output;
}
//endregion Getter / Setter

//region Overrides
@Override
public boolean matches(RecipeWrapper inv, Level level) {
return this.input.test(inv.getItem(0));
Expand Down Expand Up @@ -102,7 +95,6 @@ public RecipeType<?> getType() {
return OccultismRecipes.MINER_TYPE.get();
}

//endregion Overrides

public static class Serializer implements RecipeSerializer<MinerRecipe> {

Expand All @@ -116,7 +108,7 @@ public MinerRecipe fromJson(ResourceLocation recipeId, JsonObject json) {
Ingredient result = Ingredient.fromJson(resultElement);
int weight = GsonHelper.getAsInt(json, "weight");

return new MinerRecipe(recipeId, ingredient, new WeightedIngredient(result, weight));
return new MinerRecipe(recipeId, ingredient, new WeightedOutputIngredient(result, weight));
}

@Override
Expand All @@ -125,7 +117,7 @@ public MinerRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer
Ingredient result = Ingredient.fromNetwork(buffer);
int weight = buffer.readInt();

return new MinerRecipe(recipeId, ingredient, new WeightedIngredient(result, weight));
return new MinerRecipe(recipeId, ingredient, new WeightedOutputIngredient(result, weight));
}

@Override
Expand All @@ -134,6 +126,5 @@ public void toNetwork(FriendlyByteBuf buffer, MinerRecipe recipe) {
recipe.output.getIngredient().toNetwork(buffer);
buffer.writeInt(recipe.output.getWeight().asInt());
}
//endregion Overrides
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.klikli_dev.occultism.integration.almostunified;

import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.fml.ModList;

public class AlmostUnifiedIntegration {
public static boolean isLoaded() {
return ModList.get().isLoaded("almostunified");
}

public static ItemStack getPreferredItemForTag(TagKey<Item> tag) {
//TODO: enable once almostunified is updated
// if (isLoaded()) {
// return AlmostUnifiedHelper.getPreferredItemForTag(tag);
// }
return ItemStack.EMPTY;
}

public static class AlmostUnifiedHelper {
public static ItemStack getPreferredItemForTag(TagKey<Item> tag) {
// var asUnifyTag = UnifyTag.item(tag.location());
// var item = AlmostUnified
// .getRuntime()
// .getReplacementMap()
// .getPreferredItemForTag(asUnifyTag, $ -> true);
// if(item == null){
// return new Ingredient.TagValue(tag).getItems().stream().findFirst().get();
// }
//
// return new ItemStack(ForgeRegistries.ITEMS.getValue(item));
throw new UnsupportedOperationException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package com.github.klikli_dev.occultism.integration.jei.recipes;

import com.github.klikli_dev.occultism.Occultism;
import com.github.klikli_dev.occultism.common.misc.WeightedIngredient;
import com.github.klikli_dev.occultism.common.misc.WeightedOutputIngredient;
import com.github.klikli_dev.occultism.crafting.recipe.MinerRecipe;
import com.github.klikli_dev.occultism.integration.jei.JeiRecipeTypes;
import com.github.klikli_dev.occultism.registry.OccultismRecipes;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void setRecipe(IRecipeLayoutBuilder builder, MinerRecipe recipe, IFocusGr
List<MinerRecipe> recipes = level.getRecipeManager()
.getRecipesFor(OccultismRecipes.MINER_TYPE.get(),
new RecipeWrapper(simulatedHandler), level);
List<WeightedIngredient> possibleResults = recipes.stream().map(MinerRecipe::getWeightedOutput).collect(Collectors.toList());
List<WeightedOutputIngredient> possibleResults = recipes.stream().map(MinerRecipe::getWeightedOutput).collect(Collectors.toList());

float chance = (float) recipe.getWeightedOutput().getWeight().asInt() / (float) WeightedRandom.getTotalWeight(possibleResults) * 100.0F;
//reduce to two decimals
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public net.minecraft.data.loot.BlockLootSubProvider f_244248_ # HAS_NO_SHEARS_OR
public net.minecraft.data.loot.BlockLootSubProvider f_244441_ # map
public net.minecraft.data.loot.EntityLootSubProvider f_244213_ # map
public-f net.minecraft.world.item.Item f_41378_ # craftingRemainingItem
public net.minecraft.world.inventory.CraftingContainer f_39320_ # items
public net.minecraft.world.inventory.CraftingContainer f_39320_ # items
public net.minecraft.world.item.crafting.Ingredient f_43902_ # values
public net.minecraft.world.item.crafting.Ingredient$TagValue f_43959_ # tag

0 comments on commit 322ed0d

Please sign in to comment.