diff options
| author | 2024-01-27 03:22:37 +0200 | |
|---|---|---|
| committer | 2024-01-27 03:22:37 +0200 | |
| commit | 99ba3f0be8835dac87b506a37f2a62ba89af0ca0 (patch) | |
| tree | 8506e3afc3cd84a0394982ea3a90e450f8c2262f /src/main/java | |
| parent | Fix a dedicated server crash (diff) | |
| download | mc-eris-alchemy-99ba3f0be8835dac87b506a37f2a62ba89af0ca0.tar.gz mc-eris-alchemy-99ba3f0be8835dac87b506a37f2a62ba89af0ca0.tar.xz mc-eris-alchemy-99ba3f0be8835dac87b506a37f2a62ba89af0ca0.zip | |
Add support for a bunch of mods
Diffstat (limited to 'src/main/java')
3 files changed, 55 insertions, 22 deletions
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/Emc.java b/src/main/java/lv/enes/mc/eris_alchemy/Emc.java index 731ecee..576fdc2 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/Emc.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/Emc.java | |||
| @@ -87,9 +87,7 @@ public final class Emc { | |||
| 87 | public static void initClient(Minecraft ignoredClient) { | 87 | public static void initClient(Minecraft ignoredClient) { |
| 88 | ClientPlayNetworking.registerGlobalReceiver( | 88 | ClientPlayNetworking.registerGlobalReceiver( |
| 89 | NetworkingConstants.UPDATE_EMCS, | 89 | NetworkingConstants.UPDATE_EMCS, |
| 90 | (client, handler, buf, responseSender) -> { | 90 | (client, handler, buf, responseSender) -> syncFrom(buf) |
| 91 | syncFrom(buf); | ||
| 92 | } | ||
| 93 | ); | 91 | ); |
| 94 | } | 92 | } |
| 95 | 93 | ||
| @@ -98,9 +96,7 @@ public final class Emc { | |||
| 98 | reinit(); | 96 | reinit(); |
| 99 | warnOfMissingValues(); | 97 | warnOfMissingValues(); |
| 100 | 98 | ||
| 101 | ServerPlayConnectionEvents.JOIN.register((handler, sender, server1) -> { | 99 | ServerPlayConnectionEvents.JOIN.register((handler, sender, server1) -> syncTo(handler.getPlayer())); |
| 102 | syncTo(handler.getPlayer()); | ||
| 103 | }); | ||
| 104 | } | 100 | } |
| 105 | 101 | ||
| 106 | public static void reloadData( | 102 | public static void reloadData( |
| @@ -192,7 +188,8 @@ public final class Emc { | |||
| 192 | world.getRecipeManager() | 188 | world.getRecipeManager() |
| 193 | .getRecipes() | 189 | .getRecipes() |
| 194 | .stream() | 190 | .stream() |
| 195 | .map(recipe -> new SimplifiedRecipe(recipe, world.registryAccess())) | 191 | .map(recipe -> SimplifiedRecipe.of(recipe, world.registryAccess())) |
| 192 | .flatMap(List::stream) | ||
| 196 | ); | 193 | ); |
| 197 | } | 194 | } |
| 198 | 195 | ||
| @@ -226,7 +223,7 @@ public final class Emc { | |||
| 226 | var sortedItems = sorted(recipes); | 223 | var sortedItems = sorted(recipes); |
| 227 | sortedItems.stream() | 224 | sortedItems.stream() |
| 228 | .filter(id -> !VALUES.containsKey(id)) | 225 | .filter(id -> !VALUES.containsKey(id)) |
| 229 | .forEach(id -> VALUES.put(id, calcEmc(id, recipes))); | 226 | .forEach(id -> calcEmc(id, recipes).ifPresent(v -> VALUES.put(id, OptionalDouble.of(v)))); |
| 230 | ErisAlchemy.LOGGER.info("Done calculating EMC values..."); | 227 | ErisAlchemy.LOGGER.info("Done calculating EMC values..."); |
| 231 | 228 | ||
| 232 | sync(); | 229 | sync(); |
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java b/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java index 469ed52..6bb1274 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/recipe/SimplifiedRecipe.java | |||
| @@ -4,13 +4,17 @@ import com.google.gson.JsonDeserializationContext; | |||
| 4 | import com.google.gson.JsonDeserializer; | 4 | import com.google.gson.JsonDeserializer; |
| 5 | import com.google.gson.JsonElement; | 5 | import com.google.gson.JsonElement; |
| 6 | import com.google.gson.JsonParseException; | 6 | import com.google.gson.JsonParseException; |
| 7 | import earth.terrarium.chipped.common.recipe.ChippedRecipe; | ||
| 7 | import jakarta.annotation.Nonnull; | 8 | import jakarta.annotation.Nonnull; |
| 9 | import lv.enes.mc.eris_alchemy.utils.ForeignUtils; | ||
| 8 | import lv.enes.mc.eris_alchemy.utils.IngredientProvider; | 10 | import lv.enes.mc.eris_alchemy.utils.IngredientProvider; |
| 9 | import lv.enes.mc.eris_alchemy.utils.ItemUtils; | 11 | import lv.enes.mc.eris_alchemy.utils.ItemUtils; |
| 10 | import lv.enes.mc.eris_alchemy.utils.RecipeUtils; | 12 | import lv.enes.mc.eris_alchemy.utils.RecipeUtils; |
| 13 | import net.minecraft.core.Holder; | ||
| 11 | import net.minecraft.core.RegistryAccess; | 14 | import net.minecraft.core.RegistryAccess; |
| 12 | import net.minecraft.core.registries.BuiltInRegistries; | 15 | import net.minecraft.core.registries.BuiltInRegistries; |
| 13 | import net.minecraft.resources.ResourceLocation; | 16 | import net.minecraft.resources.ResourceLocation; |
| 17 | import net.minecraft.world.item.Item; | ||
| 14 | import net.minecraft.world.item.ItemStack; | 18 | import net.minecraft.world.item.ItemStack; |
| 15 | import net.minecraft.world.item.crafting.Ingredient; | 19 | import net.minecraft.world.item.crafting.Ingredient; |
| 16 | import net.minecraft.world.item.crafting.Recipe; | 20 | import net.minecraft.world.item.crafting.Recipe; |
| @@ -21,20 +25,45 @@ import java.util.Arrays; | |||
| 21 | import java.util.Collection; | 25 | import java.util.Collection; |
| 22 | import java.util.List; | 26 | import java.util.List; |
| 23 | import java.util.Objects; | 27 | import java.util.Objects; |
| 28 | import java.util.function.Predicate; | ||
| 24 | import java.util.function.Supplier; | 29 | import java.util.function.Supplier; |
| 25 | import java.util.stream.Stream; | 30 | import java.util.stream.Stream; |
| 26 | 31 | ||
| 27 | public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List<Supplier<Ingredient>> input) { | 32 | public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List<Supplier<Ingredient>> input) { |
| 28 | public SimplifiedRecipe(Recipe<?> recipe, RegistryAccess registryAccess) { | 33 | public static List<SimplifiedRecipe> of(Recipe<?> recipe, RegistryAccess registryAccess) { |
| 29 | this( | 34 | if (ForeignUtils.IS_CHIPPED_AVAILABLE && recipe instanceof ChippedRecipe chippedRecipe) { |
| 35 | var remainder = List.<ItemStack>of(); | ||
| 36 | return chippedRecipe.tags() | ||
| 37 | .stream() | ||
| 38 | .flatMap(tag -> { | ||
| 39 | var items = tag.stream().map(Holder::value).toList(); | ||
| 40 | Predicate<Item> isOutput = item -> ItemUtils.getId(item).getNamespace().equals("chipped"); | ||
| 41 | var inputs = items.stream() | ||
| 42 | .filter(item -> !isOutput.test(item)) | ||
| 43 | .map(Ingredient::of) | ||
| 44 | .map(x -> (Supplier<Ingredient>) () -> x) | ||
| 45 | .map(List::of) | ||
| 46 | .toList(); | ||
| 47 | var outputs = items.stream().filter(isOutput).map(Item::getDefaultInstance).toList(); | ||
| 48 | return outputs.stream() | ||
| 49 | .flatMap( | ||
| 50 | output -> | ||
| 51 | inputs.stream() | ||
| 52 | .map(input -> new SimplifiedRecipe(output, remainder, input)) | ||
| 53 | ); | ||
| 54 | }) | ||
| 55 | .toList(); | ||
| 56 | } | ||
| 57 | |||
| 58 | return List.of(new SimplifiedRecipe( | ||
| 30 | RecipeUtils.getOutput(recipe, registryAccess), | 59 | RecipeUtils.getOutput(recipe, registryAccess), |
| 31 | List.of(), // TODO: | 60 | List.of(), // TODO |
| 32 | RecipeUtils.getIngredients(recipe) | 61 | RecipeUtils.getIngredients(recipe) |
| 33 | .stream() | 62 | .stream() |
| 34 | .filter(ingredient -> !ingredient.isEmpty()) | 63 | .filter(ingredient -> !ingredient.isEmpty()) |
| 35 | .map(x -> (Supplier<Ingredient>) () -> x) | 64 | .map(x -> (Supplier<Ingredient>)() -> x) |
| 36 | .toList() | 65 | .toList() |
| 37 | ); | 66 | )); |
| 38 | } | 67 | } |
| 39 | 68 | ||
| 40 | public Stream<ResourceLocation> dependencies() { | 69 | public Stream<ResourceLocation> dependencies() { |
| @@ -45,15 +74,8 @@ public record SimplifiedRecipe(ItemStack output, List<ItemStack> remainder, List | |||
| 45 | } | 74 | } |
| 46 | 75 | ||
| 47 | public boolean hasDuplication() { | 76 | public boolean hasDuplication() { |
| 48 | var outputId = ItemUtils.getId(output); | 77 | return remainder.stream().anyMatch(stack -> ItemStack.isSameItem(stack, output)) |
| 49 | var outputsInRemainder = remainder.stream() | 78 | || input.stream().anyMatch(ingredient -> ingredient.get().test(output)); |
| 50 | .map(ItemUtils::getId) | ||
| 51 | .anyMatch(id -> Objects.equals(id, outputId)); | ||
| 52 | if (outputsInRemainder) { | ||
| 53 | return true; | ||
| 54 | } | ||
| 55 | |||
| 56 | return input.stream().anyMatch(ingredient -> ingredient.get().test(output)); | ||
| 57 | } | 79 | } |
| 58 | 80 | ||
| 59 | public boolean isAllowed(BannedRecipe ban) { | 81 | public boolean isAllowed(BannedRecipe ban) { |
diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/ForeignUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/ForeignUtils.java new file mode 100644 index 0000000..b686a9e --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/utils/ForeignUtils.java | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | package lv.enes.mc.eris_alchemy.utils; | ||
| 2 | |||
| 3 | public class ForeignUtils { | ||
| 4 | public static boolean IS_CHIPPED_AVAILABLE = check("earth.terrarium.chipped.Chipped"); | ||
| 5 | |||
| 6 | private static boolean check(String className) { | ||
| 7 | try { | ||
| 8 | Class.forName(className); | ||
| 9 | return true; | ||
| 10 | } catch (ClassNotFoundException e) { | ||
| 11 | return false; | ||
| 12 | } | ||
| 13 | } | ||
| 14 | } | ||