From bca6a74e9a17e04de419743850f66af96a6473cc Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Thu, 11 Jan 2024 23:11:27 +0100 Subject: Move default emc values to JSON files in datapack --- src/main/java/lv/enes/mc/eris_alchemy/EMC.java | 600 --------------------- src/main/java/lv/enes/mc/eris_alchemy/Emc.java | 214 ++++++++ .../java/lv/enes/mc/eris_alchemy/EmcLoader.java | 99 ++++ .../java/lv/enes/mc/eris_alchemy/ErisAlchemy.java | 11 + .../lv/enes/mc/eris_alchemy/SimplifiedRecipe.java | 89 +++ .../eris_alchemy/block/EnergyCondenserBlock.java | 4 +- .../block/entity/EnergyCondenserEntity.java | 6 +- .../eris_alchemy/client/EnergyCondenserScreen.java | 4 +- .../mc/eris_alchemy/client/ErisAlchemyClient.java | 11 +- .../mc/eris_alchemy/mixin/CoralBlockMixin.java | 20 - .../mc/eris_alchemy/mixin/CoralFanBlockMixin.java | 20 - .../eris_alchemy/mixin/CoralPlantBlockMixin.java | 20 - .../eris_alchemy/mixin/CoralWallFanBlockMixin.java | 20 - .../lv/enes/mc/eris_alchemy/utils/AxeUtils.java | 18 - .../lv/enes/mc/eris_alchemy/utils/BlockUtils.java | 15 - .../lv/enes/mc/eris_alchemy/utils/CoralUtils.java | 44 -- .../lv/enes/mc/eris_alchemy/utils/DyeUtils.java | 62 --- .../lv/enes/mc/eris_alchemy/utils/ItemUtils.java | 20 +- .../lv/enes/mc/eris_alchemy/utils/TagUtils.java | 16 - 19 files changed, 441 insertions(+), 852 deletions(-) delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/EMC.java create mode 100644 src/main/java/lv/enes/mc/eris_alchemy/Emc.java create mode 100644 src/main/java/lv/enes/mc/eris_alchemy/EmcLoader.java create mode 100644 src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralBlockMixin.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralFanBlockMixin.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralPlantBlockMixin.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralWallFanBlockMixin.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/utils/AxeUtils.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/utils/BlockUtils.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/utils/CoralUtils.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/utils/DyeUtils.java delete mode 100644 src/main/java/lv/enes/mc/eris_alchemy/utils/TagUtils.java (limited to 'src/main/java') diff --git a/src/main/java/lv/enes/mc/eris_alchemy/EMC.java b/src/main/java/lv/enes/mc/eris_alchemy/EMC.java deleted file mode 100644 index 92c0624..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/EMC.java +++ /dev/null @@ -1,600 +0,0 @@ -package lv.enes.mc.eris_alchemy; - -import jakarta.annotation.Nullable; -import lv.enes.mc.eris_alchemy.utils.*; -import lv.enes.mc.eris_alchemy.utils.ItemUtils; -import net.minecraft.core.RegistryAccess; -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.tags.BlockTags; -import net.minecraft.tags.ItemTags; -import net.minecraft.tags.TagKey; -import net.minecraft.world.item.*; -import net.minecraft.world.item.crafting.Ingredient; -import net.minecraft.world.item.crafting.Recipe; -import net.minecraft.world.level.ItemLike; -import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.Block; -import vazkii.patchouli.common.item.PatchouliItems; - -import java.text.DecimalFormat; -import java.util.*; -import java.util.stream.Stream; - -public class EMC { - private record SimplifiedRecipe(ItemStack output, List remainder, List inputs) { - public SimplifiedRecipe(ItemLike output, Ingredient... inputs) { - this(new ItemStack(output), List.of(), List.of(inputs)); - } - - public SimplifiedRecipe(ItemLike output, ItemLike... inputs) { - this(new ItemStack(output), List.of(), Arrays.stream(inputs).map(Ingredient::of).toList()); - } - - public SimplifiedRecipe(ItemLike output, ItemStack... inputs) { - this(new ItemStack(output), List.of(), Arrays.stream(inputs).map(Ingredient::of).toList()); - } - - public SimplifiedRecipe(ItemLike output, List remainder, ItemLike... inputs) { - this(new ItemStack(output), remainder, Arrays.stream(inputs).map(Ingredient::of).toList()); - } - - public SimplifiedRecipe(ItemStack output, ItemLike... inputs) { - this(output, List.of(), Arrays.stream(inputs).map(Ingredient::of).toList()); - } - - public SimplifiedRecipe(ItemStack output, ItemStack... inputs) { - this(output, List.of(), Arrays.stream(inputs).map(Ingredient::of).toList()); - } - - public SimplifiedRecipe(Recipe recipe, RegistryAccess registryAccess) { - this( - RecipeUtils.getOutput(recipe, registryAccess), - List.of(), // TODO: - RecipeUtils.getIngredients(recipe).stream().filter(ingredient -> !ingredient.isEmpty()).toList() - ); - } - } - - private static final Map ITEM_COMMON_MAP = new HashMap<>(); - private static final Map, Double> ITEM_TAG_COMMON_MAP = new HashMap<>(); - private static final Map, Double> BLOCK_TAG_COMMON_MAP = new HashMap<>(); - private static final List FAKE_RECIPES = new ArrayList<>(); - - private static final EMC defaultInstance = new EMC(null); - private static final Map instances = Collections.synchronizedMap(new WeakHashMap<>()); - - private static final DecimalFormat formatter = new DecimalFormat("0"); - static { - formatter.setMaximumFractionDigits(1); - } - - public static EMC getInstance(Level world) { - if (world == null) { - return defaultInstance; - } - - if (instances.containsKey(world)) { - return instances.get(world); - } - - var instance = new EMC(world); - instances.put(world, instance); - return instance; - } - - public static String formatEmc(double value) { - return formatter.format(value); - } - - private final Map data; - - private EMC(@Nullable Level world) { - data = Collections.synchronizedMap(new HashMap<>(ITEM_COMMON_MAP)); - ITEM_TAG_COMMON_MAP.forEach( - (tag, emcValue) -> BuiltInRegistries.ITEM - .getTagOrEmpty(tag) - .forEach(holder -> data.putIfAbsent(holder.value(), emcValue)) - ); - BLOCK_TAG_COMMON_MAP.forEach( - (tag, emcValue) -> BuiltInRegistries.BLOCK - .getTagOrEmpty(tag) - .forEach(holder -> data.putIfAbsent(holder.value().asItem(), emcValue)) - ); - - ErisAlchemy.LOGGER.info("Calculating EMC values..."); - var recipes = getRecipes(world); - var configured = new HashSet<>(data.keySet()); - BuiltInRegistries.ITEM.forEach(item -> { - configEmc(recipes, configured, item); - if (world != null && !data.containsKey(item)) { - ErisAlchemy.LOGGER.warn("No EMC value for '{}' known", item); - } - }); - } - - public OptionalDouble get(ItemStack stack) { - if (stack.isEmpty()) { - return OptionalDouble.empty(); - } - - var item = stack.getItem(); - var value = data.get(item); - if (value == null || value <= 0) { - return OptionalDouble.empty(); - } - - EmcStorage storage = null; - if (item instanceof EmcStorage emcStorage) { - storage = emcStorage; - } else if (item instanceof BlockItem blockItem) { - if (blockItem.getBlock() instanceof EmcStorage emcStorage) { - storage = emcStorage; - } - } - - if (storage != null) { - value += storage.getStoredEmc(stack); - } - - return OptionalDouble.of(value); - } - - private List getRecipes(@Nullable Level world) { - Stream recipes = Stream.concat( - FAKE_RECIPES.stream(), - AxeUtils.getStrippables() - .entrySet() - .stream() - .map(entry -> new SimplifiedRecipe(entry.getValue(), Ingredient.of(entry.getKey()))) - ); - - if (world != null) { - recipes = Stream.concat( - recipes, - world.getRecipeManager() - .getRecipes() - .stream() - .map(recipe -> new SimplifiedRecipe(recipe, world.registryAccess())) - ); - } - - return recipes.toList(); - } - - private OptionalDouble configEmc(List recipes, Set configured, Item item) { - var res = get(item.getDefaultInstance()); - if (res.isPresent() || configured.contains(item)) { - return res; - } - - configured.add(item); - - res = recipes.stream() - .filter(recipe -> recipe.output.is(item)) - .map(recipe -> calculateEmcForRecipe(recipes, configured, recipe)) - .filter(OptionalDouble::isPresent) - .mapToDouble(OptionalDouble::getAsDouble) - .average(); - res.ifPresent(emc -> data.put(item, emc)); - if (res.isPresent() && res.getAsDouble() <= 0) { - res = OptionalDouble.empty(); - } - return res; - } - - private OptionalDouble calculateEmcForRecipe( - List recipes, - Set configured, - SimplifiedRecipe recipe - ) { - try { - if (recipe.inputs.isEmpty()) { - return OptionalDouble.empty(); - } - - var inputEmc = recipe.inputs - .stream() - .map(ingredient -> calculateEmcForIngredient(recipes, configured, ingredient)) - .mapToDouble(OptionalDouble::orElseThrow) - .sum(); - - if (inputEmc <= 0) { - return OptionalDouble.empty(); - } - - var remainderEmc = recipe.remainder - .stream() - .map(remainder -> configEmc(recipes, configured, remainder.getItem())) - .mapToDouble(OptionalDouble::orElseThrow) - .sum(); - - if (remainderEmc > inputEmc) { - ErisAlchemy.LOGGER.warn("Recipe generating {} creates too much EMC out of thin air!", recipe.output); - return OptionalDouble.empty(); - } else if (remainderEmc < 0) { - return OptionalDouble.empty(); - } - - var outputDivisor = (double) recipe.output.getCount(); - - return OptionalDouble.of((inputEmc - remainderEmc) / outputDivisor); - } catch (NoSuchElementException e) { - return OptionalDouble.empty(); - } - } - - private OptionalDouble calculateEmcForIngredient( - List recipes, - Set configured, - Ingredient ingredient - ) { - return Arrays.stream(ingredient.getItems()) - .map(stack -> configEmc(recipes, configured, stack.getItem()).stream() - .map(x -> x * stack.getCount()) - .findFirst()) - .filter(OptionalDouble::isPresent) - .mapToDouble(OptionalDouble::getAsDouble) - .filter(x -> x > 0) - .average(); - } - - static { - ITEM_COMMON_MAP.putAll(Map.ofEntries( - Map.entry(Items.AIR, -1.0), - Map.entry(Items.AMETHYST_CLUSTER, -1.0), - Map.entry(Items.AMETHYST_SHARD, 1024.0), - Map.entry(Items.ANCIENT_DEBRIS, -1.0), - Map.entry(Items.APPLE, 128.0), - Map.entry(Items.BAMBOO, 2.0), - Map.entry(Items.BARRIER, -1.0), - Map.entry(Items.BEDROCK, -1.0), - Map.entry(Items.BEEF, 64.0), - Map.entry(Items.BEETROOT, 24.0), - Map.entry(Items.BEETROOT_SEEDS, 16.0), - Map.entry(Items.BIG_DRIPLEAF, 1.0), - Map.entry(Items.BLAZE_ROD, 1536.0), - Map.entry(Items.BONE, 96.0), - Map.entry(Items.BROWN_MUSHROOM, 32.0), - Map.entry(Items.BROWN_MUSHROOM_BLOCK, -1.0), - Map.entry(Items.BUDDING_AMETHYST, -1.0), - Map.entry(Items.BUNDLE, -1.0), - Map.entry(Items.CACTUS, 8.0), - Map.entry(Items.CARROT, 24.0), - Map.entry(Items.CALCITE, 1.0), - Map.entry(Items.CHAIN_COMMAND_BLOCK, -1.0), - Map.entry(Items.CHICKEN, 64.0), - Map.entry(Items.CHORUS_FLOWER, 32.0), - Map.entry(Items.CHORUS_FRUIT, 32.0), - Map.entry(Items.CHORUS_PLANT, 24.0), - Map.entry(Items.CLAY_BALL, 64.0), - Map.entry(Items.COAL, 128.0), - Map.entry(Items.COBWEB, 12.0), - Map.entry(Items.COBBLESTONE, 1.0), - Map.entry(Items.COBBLED_DEEPSLATE, 1.0), - Map.entry(Items.COCOA_BEANS, 8.0), - Map.entry(Items.COMMAND_BLOCK, -1.0), - Map.entry(Items.COMMAND_BLOCK_MINECART, -1.0), - Map.entry(Items.COPPER_INGOT, 85.0), - Map.entry(Items.CREEPER_HEAD, 34816.0), - Map.entry(Items.CRIMSON_FUNGUS, 24.0), - Map.entry(Items.CRIMSON_ROOTS, 1.0), - Map.entry(Items.CRYING_OBSIDIAN, 64.0), - Map.entry(Items.DEAD_BUSH, 1.0), - Map.entry(Items.DEBUG_STICK, -1.0), - Map.entry(Items.DIAMOND, 8192.0), - Map.entry(Items.DIRT_PATH, 1.0), - Map.entry(Items.DRAGON_BREATH, 34816.0), - Map.entry(Items.DRAGON_EGG, 139264.0), - Map.entry(Items.DRAGON_HEAD, 34816.0), - Map.entry(Items.ECHO_SHARD, 128.0), - Map.entry(Items.EGG, 32.0), - Map.entry(Items.ELYTRA, 8196.0), - Map.entry(Items.EMERALD, 1024.0), - Map.entry(Items.ENCHANTED_BOOK, -1.0), - Map.entry(Items.END_PORTAL_FRAME, -1.0), - Map.entry(Items.END_STONE, 1.0), - Map.entry(Items.ENDER_PEARL, 1024.0), - Map.entry(Items.EXPERIENCE_BOTTLE, -1.0), - Map.entry(Items.FARMLAND, -1.0), - Map.entry(Items.FEATHER, 48.0), - Map.entry(Items.FERN, 1.0), - Map.entry(Items.FIREWORK_STAR, -1.0), - Map.entry(Items.FLINT, 4.0), - Map.entry(Items.FROGSPAWN, -1.0), - Map.entry(Items.GHAST_TEAR, 4096.0), - Map.entry(Items.GLOW_BERRIES, 8.0), - Map.entry(Items.GLOW_LICHEN, 8.0), - Map.entry(Items.GLOWSTONE_DUST, 384.0), - Map.entry(Items.GOAT_HORN, 32.0), - Map.entry(Items.GOLD_INGOT, 2048.0), - Map.entry(Items.GOLD_NUGGET, 2048.0/9), - Map.entry(Items.GRASS, 1.0), - Map.entry(Items.GRAVEL, 4.0), - Map.entry(Items.GUNPOWDER, 192.0), - Map.entry(Items.HANGING_ROOTS, 1.0), - Map.entry(Items.HEART_OF_THE_SEA, 4096.0), - Map.entry(Items.HONEYCOMB, 24.0), - Map.entry(Items.ICE, 1.0), - Map.entry(Items.INFESTED_CHISELED_STONE_BRICKS, -1.0), - Map.entry(Items.INFESTED_COBBLESTONE, -1.0), - Map.entry(Items.INFESTED_CRACKED_STONE_BRICKS, -1.0), - Map.entry(Items.INFESTED_DEEPSLATE, -1.0), - Map.entry(Items.INFESTED_MOSSY_STONE_BRICKS, -1.0), - Map.entry(Items.INFESTED_STONE, -1.0), - Map.entry(Items.INFESTED_STONE_BRICKS, -1.0), - Map.entry(Items.IRON_INGOT, 256.0), - Map.entry(Items.IRON_NUGGET, 256.0/9), - Map.entry(Items.JIGSAW, -1.0), - Map.entry(Items.KELP, 32.0), - Map.entry(Items.KNOWLEDGE_BOOK, -1.0), - Map.entry(Items.LAPIS_LAZULI, 864.0), - Map.entry(Items.LARGE_AMETHYST_BUD, -1.0), - Map.entry(Items.LARGE_FERN, 1.0), - Map.entry(Items.LEATHER, 64.0), - Map.entry(Items.LIGHT, -1.0), - Map.entry(Items.LILY_PAD, 16.0), - Map.entry(Items.LINGERING_POTION, -1.0), - Map.entry(Items.MANGROVE_ROOTS, 1.0), - Map.entry(Items.MEDIUM_AMETHYST_BUD, -1.0), - Map.entry(Items.MELON_SLICE, 144.0), - Map.entry(Items.MUD, 1.0), - Map.entry(Items.MUSHROOM_STEM, -1.0), - Map.entry(Items.MUTTON, 64.0), - Map.entry(Items.NAME_TAG, -1.0), - Map.entry(Items.NAUTILUS_SHELL, 64.0), - Map.entry(Items.NETHER_QUARTZ_ORE, -1.0), - Map.entry(Items.NETHER_SPROUTS, 1.0), - Map.entry(Items.NETHER_STAR, 139264.0), - Map.entry(Items.NETHER_WART, 24.0), - Map.entry(Items.NETHERITE_SCRAP, 16384.0), - Map.entry(Items.OBSIDIAN, 64.0), - Map.entry(Items.PHANTOM_MEMBRANE, 96.0), - Map.entry(Items.PIGLIN_HEAD, 34816.0), - Map.entry(Items.PITCHER_POD, 8.0), - Map.entry(Items.PLAYER_HEAD, 34816.0), - Map.entry(Items.POINTED_DRIPSTONE, 0.25), - Map.entry(Items.PORKCHOP, 64.0), - Map.entry(Items.POTATO, 24.0), - Map.entry(Items.POTION, -1.0), - Map.entry(Items.PRISMARINE_CRYSTALS, 384.0), - Map.entry(Items.PRISMARINE_SHARD, 0.25), - Map.entry(Items.PUMPKIN, 144.0), - Map.entry(Items.QUARTZ, 64.0), - Map.entry(Items.RABBIT, 64.0), - Map.entry(Items.RABBIT_HIDE, 64.0), - Map.entry(Items.RAW_COPPER, -1.0), - Map.entry(Items.RAW_COPPER_BLOCK, -1.0), - Map.entry(Items.RAW_GOLD, -1.0), - Map.entry(Items.RAW_GOLD_BLOCK, -1.0), - Map.entry(Items.RAW_IRON, -1.0), - Map.entry(Items.RAW_IRON_BLOCK, -1.0), - Map.entry(Items.RED_MUSHROOM, 32.0), - Map.entry(Items.RED_MUSHROOM_BLOCK, -1.0), - Map.entry(Items.REDSTONE, 64.0), - Map.entry(Items.REINFORCED_DEEPSLATE, -1.0), - Map.entry(Items.REPEATING_COMMAND_BLOCK, -1.0), - Map.entry(Items.ROTTEN_FLESH, 24.0), - Map.entry(Items.SADDLE, 192.0), - Map.entry(Items.SCULK, 1.0), - Map.entry(Items.SCULK_CATALYST, 16.0), - Map.entry(Items.SCULK_SENSOR, 32.0), - Map.entry(Items.SCULK_SHRIEKER, 16.0), - Map.entry(Items.SCULK_VEIN, 1.0), - Map.entry(Items.SCUTE, 32.0), - Map.entry(Items.SEA_PICKLE, 1.0), - Map.entry(Items.SEAGRASS, 1.0), - Map.entry(Items.SHULKER_SHELL, 256.0), - Map.entry(Items.SKELETON_SKULL, 34816.0), - Map.entry(Items.SLIME_BALL, 24.0), - Map.entry(Items.SMALL_AMETHYST_BUD, -1.0), - Map.entry(Items.SMALL_DRIPLEAF, 1.0), - Map.entry(Items.SNIFFER_EGG, 32.0), - Map.entry(Items.SNOWBALL, 1.0), - Map.entry(Items.SOUL_SAND, 49.0), - Map.entry(Items.SOUL_SOIL, 49.0), - Map.entry(Items.SPAWNER, -1.0), - Map.entry(Items.SPIDER_EYE, 128.0), - Map.entry(Items.SPLASH_POTION, -1.0), - Map.entry(Items.SPONGE, 48.0), - Map.entry(Items.SPORE_BLOSSOM, 1.0), - Map.entry(Items.STICK, 4.0), - Map.entry(Items.STRING, 12.0), - Map.entry(Items.STRUCTURE_BLOCK, -1.0), - Map.entry(Items.STRUCTURE_VOID, -1.0), - Map.entry(Items.SUGAR_CANE, 32.0), - Map.entry(Items.SUSPICIOUS_GRAVEL, 4.0), - Map.entry(Items.SWEET_BERRIES, 8.0), - Map.entry(Items.TALL_GRASS, 1.0), - Map.entry(Items.TIPPED_ARROW, -1.0), - Map.entry(Items.TORCHFLOWER_SEEDS, 8.0), - Map.entry(Items.TOTEM_OF_UNDYING, 4096.0), - Map.entry(Items.TURTLE_EGG, 32.0), - Map.entry(Items.TWISTING_VINES, 8.0), - Map.entry(Items.VINE, 8.0), - Map.entry(Items.WARPED_FUNGUS, 24.0), - Map.entry(Items.WARPED_ROOTS, 1.0), - Map.entry(Items.WARPED_WART_BLOCK, 1.0), - Map.entry(Items.WEEPING_VINES, 8.0), - Map.entry(Items.WET_SPONGE, 48.0), - Map.entry(Items.WHEAT, 24.0), - Map.entry(Items.WHEAT_SEEDS, 16.0), - Map.entry(Items.WITHER_SKELETON_SKULL, 34816.0), - Map.entry(Items.WRITTEN_BOOK, -1.0), - Map.entry(Items.ZOMBIE_HEAD, 34816.0), - - Map.entry(PatchouliItems.BOOK, -1.0) - )); - - SpawnEggItem.eggs().forEach(spawnEgg -> ITEM_COMMON_MAP.put(spawnEgg, -1.0)); - - CoralUtils.streamAllCoralBlocks().forEach(block -> ITEM_COMMON_MAP.put(block.asItem(), 1.0)); - CoralUtils.streamAllDeadCoralBlocks().forEach(block -> ITEM_COMMON_MAP.put(block.asItem(), 1.0)); - CoralUtils.streamAllCorals().forEach(block -> ITEM_COMMON_MAP.put(block.asItem(), 1.0)); - CoralUtils.streamAllDeadCorals().forEach(block -> ITEM_COMMON_MAP.put(block.asItem(), 1.0)); - - BLOCK_TAG_COMMON_MAP.putAll(Map.ofEntries( - Map.entry(BlockTags.BASE_STONE_NETHER, 1.0), - Map.entry(BlockTags.BASE_STONE_OVERWORLD, 1.0), - Map.entry(BlockTags.NYLIUM, 1.0) - )); - - ITEM_TAG_COMMON_MAP.putAll(Map.ofEntries( - Map.entry(ItemTags.COAL_ORES, -1.0), - Map.entry(ItemTags.COPPER_ORES, -1.0), - Map.entry(ItemTags.DIAMOND_ORES, -1.0), - Map.entry(ItemTags.DIRT, 1.0), - Map.entry(ItemTags.MUSIC_DISCS, 4096.0), - Map.entry(ItemTags.EMERALD_ORES, -1.0), - Map.entry(ItemTags.FISHES, 64.0), - Map.entry(ItemTags.FLOWERS, 16.0), - Map.entry(ItemTags.GOLD_ORES, -1.0), - Map.entry(ItemTags.IRON_ORES, -1.0), - Map.entry(ItemTags.LAPIS_ORES, -1.0), - Map.entry(ItemTags.LEAVES, 1.0), - Map.entry(ItemTags.LOGS, 32.0), - Map.entry(ItemTags.REDSTONE_ORES, -1.0), - Map.entry(ItemTags.SAND, 1.0), - Map.entry(ItemTags.SAPLINGS, 32.0), - Map.entry(ItemTags.WOOL, 48.0) - )); - - FAKE_RECIPES.addAll(List.of( - // more oxidised coppers are worth less - new SimplifiedRecipe(new ItemStack(Items.EXPOSED_COPPER, 4), new ItemStack(Items.COPPER_BLOCK, 3)), - new SimplifiedRecipe(new ItemStack(Items.WEATHERED_COPPER, 4), new ItemStack(Items.COPPER_BLOCK, 2)), - new SimplifiedRecipe(new ItemStack(Items.OXIDIZED_COPPER, 4), new ItemStack(Items.COPPER_BLOCK, 1)), - - new SimplifiedRecipe(new ItemStack(Items.WAXED_EXPOSED_COPPER, 4), new ItemStack(Items.WAXED_COPPER_BLOCK, 3)), - new SimplifiedRecipe(new ItemStack(Items.WAXED_WEATHERED_COPPER, 4), new ItemStack(Items.WAXED_COPPER_BLOCK, 2)), - new SimplifiedRecipe(new ItemStack(Items.WAXED_OXIDIZED_COPPER, 4), new ItemStack(Items.WAXED_COPPER_BLOCK, 1)), - - // more damaged anvils are worth less - new SimplifiedRecipe(new ItemStack(Items.CHIPPED_ANVIL, 3), new ItemStack(Items.ANVIL, 2)), - new SimplifiedRecipe(new ItemStack(Items.DAMAGED_ANVIL, 3), new ItemStack(Items.ANVIL, 1)), - - // old oak slabs are the same value as modern ones - new SimplifiedRecipe(Items.PETRIFIED_OAK_SLAB, Items.OAK_SLAB), - - // chainmail armour is worth the same as normal iron armor - new SimplifiedRecipe(Items.CHAINMAIL_HELMET, Items.IRON_HELMET), - new SimplifiedRecipe(Items.CHAINMAIL_CHESTPLATE, Items.IRON_CHESTPLATE), - new SimplifiedRecipe(Items.CHAINMAIL_LEGGINGS, Items.IRON_LEGGINGS), - new SimplifiedRecipe(Items.CHAINMAIL_BOOTS, Items.IRON_BOOTS), - - // enchanted golden apple is worth uhh 16 times the non-enchanted one :3 - new SimplifiedRecipe(Items.ENCHANTED_GOLDEN_APPLE, new ItemStack(Items.GOLDEN_APPLE, 16)), - - // carving a pumpkin with shears - new SimplifiedRecipe(Items.CARVED_PUMPKIN, List.of(new ItemStack(Items.PUMPKIN_SEEDS, 4)), Items.PUMPKIN), - - // gathering honey - new SimplifiedRecipe(Items.HONEY_BOTTLE, Items.HONEYCOMB, Items.GLASS_BOTTLE), - - // smithing template duplicating recipes but -1 templates on both sides - new SimplifiedRecipe(Items.COAST_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.COBBLESTONE)), - new SimplifiedRecipe(Items.DUNE_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.SANDSTONE)), - new SimplifiedRecipe(Items.EYE_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.END_STONE)), - new SimplifiedRecipe(Items.HOST_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.TERRACOTTA)), - new SimplifiedRecipe(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.NETHERRACK)), - new SimplifiedRecipe(Items.RAISER_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.TERRACOTTA)), - new SimplifiedRecipe(Items.RIB_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.NETHERRACK)), - new SimplifiedRecipe(Items.SENTRY_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.COBBLESTONE)), - new SimplifiedRecipe(Items.SHAPER_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.TERRACOTTA)), - new SimplifiedRecipe(Items.SILENCE_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.COBBLED_DEEPSLATE)), - new SimplifiedRecipe(Items.SNOUT_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.BLACKSTONE)), - new SimplifiedRecipe(Items.SPIRE_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.PURPUR_BLOCK)), - new SimplifiedRecipe(Items.TIDE_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.PRISMARINE)), - new SimplifiedRecipe(Items.VEX_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.COBBLESTONE)), - new SimplifiedRecipe(Items.WARD_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.COBBLED_DEEPSLATE)), - new SimplifiedRecipe(Items.WAYFINDER_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.TERRACOTTA)), - new SimplifiedRecipe(Items.WILD_ARMOR_TRIM_SMITHING_TEMPLATE, new ItemStack(Items.DIAMOND, 7), new ItemStack(Items.MOSSY_COBBLESTONE)), - - // water bucket = bucket + water, water assumed to be same value as cobblestone - new SimplifiedRecipe(Items.WATER_BUCKET, Items.BUCKET, Items.COBBLESTONE), - - // lava bucket = bucket + lava, lava assumed to be same value as obsidian - new SimplifiedRecipe(Items.LAVA_BUCKET, Items.BUCKET, Items.OBSIDIAN), - - // powder snow bucket = bucket + snow - new SimplifiedRecipe(Items.POWDER_SNOW_BUCKET, Items.BUCKET, Items.SNOW_BLOCK), - - // milk bucket = bucket + milk, milk assumed to be same value as wheat - new SimplifiedRecipe(Items.MILK_BUCKET, Items.BUCKET, Items.WHEAT), - - // [fish] bucket = bucket + [fish] - new SimplifiedRecipe(Items.COD_BUCKET, Items.BUCKET, Items.COD), - new SimplifiedRecipe(Items.PUFFERFISH_BUCKET, Items.BUCKET, Items.PUFFERFISH), - new SimplifiedRecipe(Items.SALMON_BUCKET, Items.BUCKET, Items.SALMON), - new SimplifiedRecipe(Items.TROPICAL_FISH_BUCKET, Items.BUCKET, Items.TROPICAL_FISH), - - // yep - new SimplifiedRecipe(Items.AXOLOTL_BUCKET, Items.TROPICAL_FISH_BUCKET), - new SimplifiedRecipe(Items.TADPOLE_BUCKET, Items.AXOLOTL_BUCKET), - - // dye value is the canon - new SimplifiedRecipe(Items.INK_SAC, Items.BLACK_DYE), - - // disc value is the canon - new SimplifiedRecipe(new ItemStack(Items.DISC_FRAGMENT_5, 9), Items.MUSIC_DISC_5), - - // glowing ink sac is more expensive than an ink sac - new SimplifiedRecipe(Items.GLOW_INK_SAC, new ItemStack(Items.INK_SAC, 4)), - - // activated maps cost the same as unactivated ones - new SimplifiedRecipe(Items.FILLED_MAP, Items.MAP), - - // poisonous potato costs less than a normal one - new SimplifiedRecipe(new ItemStack(Items.POISONOUS_POTATO, 2), Items.POTATO), - - // rabbits foot costs more than rabbit meat - new SimplifiedRecipe(Items.RABBIT_FOOT, new ItemStack(Items.RABBIT, 8)), - - // [X] horse armor == [X] leggings - // wtf, why? see how much leather is used by leather horse armor - new SimplifiedRecipe(Items.IRON_HORSE_ARMOR, Items.IRON_LEGGINGS), - new SimplifiedRecipe(Items.GOLDEN_HORSE_ARMOR, Items.GOLDEN_LEGGINGS), - new SimplifiedRecipe(Items.DIAMOND_HORSE_ARMOR, Items.DIAMOND_LEGGINGS), - - // Trident = 1.5 * iron sword - new SimplifiedRecipe(new ItemStack(Items.TRIDENT, 2), new ItemStack(Items.IRON_SWORD, 3)), - - // an actual recipe but it's special, minecraft:crafting_special_suspiciousstew - new SimplifiedRecipe(Items.SUSPICIOUS_STEW, Items.BOWL, Items.BROWN_MUSHROOM, Items.RED_MUSHROOM, Items.POPPY), - - // obvious - new SimplifiedRecipe(Items.GLOBE_BANNER_PATTERN, Items.CREEPER_BANNER_PATTERN), - new SimplifiedRecipe(Items.PIGLIN_BANNER_PATTERN, Items.CREEPER_BANNER_PATTERN), - - // the vibes fit - new SimplifiedRecipe(Items.BELL, Items.SPYGLASS), - - // light sources, shroomlight has no crafting uses but is brighter - new SimplifiedRecipe(Items.SHROOMLIGHT, Items.GLOWSTONE), - - // bee nest = honeycomb block - new SimplifiedRecipe(Items.BEE_NEST, new ItemStack(Items.HONEYCOMB, 9)), - - // gilded blackstone = blackstone with gold nuggets - new SimplifiedRecipe(Items.GILDED_BLACKSTONE, new ItemStack(Items.BLACKSTONE), new ItemStack(Items.GOLD_NUGGET, 4)), - - // froglight = eaten magma cube - new SimplifiedRecipe(Items.OCHRE_FROGLIGHT, Items.MAGMA_CREAM), - new SimplifiedRecipe(Items.PEARLESCENT_FROGLIGHT, Items.MAGMA_CREAM), - new SimplifiedRecipe(Items.VERDANT_FROGLIGHT, Items.MAGMA_CREAM) - )); - - // pot sherds can be replaced with bricks when making decorated pots - ItemUtils.streamTag(ItemTags.DECORATED_POT_SHERDS).forEach(sherd -> FAKE_RECIPES.add(new SimplifiedRecipe(sherd, Items.BRICK))); - - for (var color : DyeColor.values()) { - ITEM_COMMON_MAP.put(DyeUtils.getDye(color), 8.0); - FAKE_RECIPES.addAll(List.of( - // putting concrete powder in water - new SimplifiedRecipe(DyeUtils.getConcrete(color), DyeUtils.getConcretePowder(color)), - - // an actual recipe but it's special, minecraft:crafting_special_shulkerboxcoloring - new SimplifiedRecipe(DyeUtils.getShulkerBox(color), Items.SHULKER_BOX, DyeUtils.getDye(color)) - )); - } - } -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/Emc.java b/src/main/java/lv/enes/mc/eris_alchemy/Emc.java new file mode 100644 index 0000000..fdc7c0a --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/Emc.java @@ -0,0 +1,214 @@ +package lv.enes.mc.eris_alchemy; + +import jakarta.annotation.Nonnull; +import jakarta.annotation.Nullable; +import lv.enes.mc.eris_alchemy.utils.ItemUtils; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; + +import java.text.DecimalFormat; +import java.util.*; +import java.util.stream.Stream; + +public class Emc { + private static final Map ITEM_VALUES = new HashMap<>(); + private static final Map, OptionalDouble> ITEM_TAG_VALUES = new HashMap<>(); + private static final Map, OptionalDouble> BLOCK_TAG_VALUES = new HashMap<>(); + private static final List FAKE_RECIPES = new ArrayList<>(); + + private static final Map instances = Collections.synchronizedMap(new WeakHashMap<>()); + + private static final DecimalFormat formatter = new DecimalFormat("0"); + static { + formatter.setMaximumFractionDigits(1); + } + + public static Emc getInstance(@Nonnull Level world) { + if (instances.containsKey(world)) { + return instances.get(world); + } + + var instance = new Emc(world); + instances.put(world, instance); + return instance; + } + + public static String formatEmc(double value) { + return formatter.format(value); + } + + public static void reloadData( + Map itemValues, + Map itemTagValues, + Map blockTagValues, + List fakeRecipes + ) { + ITEM_VALUES.clear(); + ITEM_VALUES.putAll(itemValues); + + ITEM_TAG_VALUES.clear(); + itemTagValues.forEach((id, value) -> ITEM_TAG_VALUES.put(TagKey.create(Registries.ITEM, id), value)); + + BLOCK_TAG_VALUES.clear(); + blockTagValues.forEach((id, value) -> BLOCK_TAG_VALUES.put(TagKey.create(Registries.BLOCK, id), value)); + + FAKE_RECIPES.clear(); + FAKE_RECIPES.addAll(fakeRecipes); + + instances.clear(); + } + + private final Map data; + + private Emc(@Nonnull Level world) { + data = Collections.synchronizedMap(new HashMap<>(ITEM_VALUES)); + ITEM_TAG_VALUES.forEach( + (tag, emcValue) -> BuiltInRegistries.ITEM + .getTagOrEmpty(tag) + .forEach(holder -> data.putIfAbsent(ItemUtils.getId(holder), emcValue)) + ); + BLOCK_TAG_VALUES.forEach( + (tag, emcValue) -> BuiltInRegistries.BLOCK + .getTagOrEmpty(tag) + .forEach(holder -> data.putIfAbsent(ItemUtils.getId(holder), emcValue)) + ); + + ErisAlchemy.LOGGER.info("Calculating EMC values..."); + var recipes = getRecipes(world); + var configured = new HashSet<>(data.keySet()); + BuiltInRegistries.ITEM.keySet().forEach(item -> { + configEmc(recipes, configured, item); + if (!data.containsKey(item)) { + ErisAlchemy.LOGGER.warn("No EMC value for '{}' known", item); + } + }); + } + + public OptionalDouble get(ItemStack stack) { + if (stack.isEmpty()) { + return OptionalDouble.empty(); + } + + var item = stack.getItem(); + var itemId = ItemUtils.getId(item); + return get(itemId) + .stream() + .map(value -> { + EmcStorage storage = null; + if (item instanceof EmcStorage emcStorage) { + storage = emcStorage; + } else if (item instanceof BlockItem blockItem) { + if (blockItem.getBlock() instanceof EmcStorage emcStorage) { + storage = emcStorage; + } + } + if (storage != null) { + return value + storage.getStoredEmc(stack); + } + return value; + }) + .findFirst(); + } + + public OptionalDouble get(ResourceLocation itemId) { + return data.getOrDefault(itemId, OptionalDouble.empty()); + } + + private List getRecipes(@Nullable Level world) { + Stream recipes = FAKE_RECIPES.stream(); + + if (world != null) { + recipes = Stream.concat( + recipes, + world.getRecipeManager() + .getRecipes() + .stream() + .map(recipe -> new SimplifiedRecipe(recipe, world.registryAccess())) + ); + } + + return recipes.toList(); + } + + private OptionalDouble configEmc( + List recipes, + Set configured, + ResourceLocation itemId + ) { + var res = get(itemId); + if (res.isPresent() || configured.contains(itemId)) { + return res; + } + + configured.add(itemId); + var item = BuiltInRegistries.ITEM.get(itemId); + res = recipes.stream() + .filter(recipe -> recipe.output().is(item)) + .map(recipe -> calculateEmcForRecipe(recipes, configured, recipe)) + .flatMapToDouble(OptionalDouble::stream) + .average(); + res.ifPresentOrElse( + emc -> data.put(itemId, OptionalDouble.of(emc)), + () -> configured.remove(itemId) + ); + return res; + } + + private OptionalDouble calculateEmcForRecipe( + List recipes, + Set configured, + SimplifiedRecipe recipe + ) { + try { + if (recipe.input().isEmpty()) { + return OptionalDouble.empty(); + } + + var inputEmc = recipe.input() + .stream() + .map(ingredient -> calculateEmcForIngredient(recipes, configured, ingredient)) + .mapToDouble(OptionalDouble::orElseThrow) + .sum(); + + var remainderEmc = recipe.remainder() + .stream() + .map(remainder -> configEmc(recipes, configured, ItemUtils.getId(remainder))) + .mapToDouble(OptionalDouble::orElseThrow) + .sum(); + + if (remainderEmc > inputEmc) { + ErisAlchemy.LOGGER.warn("Recipe generating {} creates too much EMC out of thin air!", recipe.output()); + return OptionalDouble.empty(); + } + + var outputDivisor = (double) recipe.output().getCount(); + + return OptionalDouble.of((inputEmc - remainderEmc) / outputDivisor); + } catch (NoSuchElementException e) { + return OptionalDouble.empty(); + } + } + + private OptionalDouble calculateEmcForIngredient( + List recipes, + Set configured, + Ingredient ingredient + ) { + return Arrays.stream(ingredient.getItems()) + .map(stack -> configEmc(recipes, configured, ItemUtils.getId(stack.getItem())).stream() + .map(x -> x * stack.getCount()) + .findFirst()) + .filter(OptionalDouble::isPresent) + .mapToDouble(OptionalDouble::getAsDouble) + .filter(x -> x > 0) + .average(); + } +} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/EmcLoader.java b/src/main/java/lv/enes/mc/eris_alchemy/EmcLoader.java new file mode 100644 index 0000000..dd4613f --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/EmcLoader.java @@ -0,0 +1,99 @@ +package lv.enes.mc.eris_alchemy; + +import com.google.gson.reflect.TypeToken; +import jakarta.annotation.Nonnull; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.ResourceManager; +import org.quiltmc.qsl.resource.loader.api.reloader.SimpleSynchronousResourceReloader; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.*; + +import static lv.enes.mc.eris_alchemy.ErisAlchemy.GSON; + +public class EmcLoader implements SimpleSynchronousResourceReloader { + public static final EmcLoader INSTANCE = new EmcLoader(); + + @Nonnull + @Override + public ResourceLocation getQuiltId() { + return new ResourceLocation(ErisAlchemy.ID, "emc_loader"); + } + + @Override + public void onResourceManagerReload(ResourceManager manager) { + var itemValues = loadAllFiles(manager, "item_emcs", new HashMap<>(), EmcLoader::loadEmcValues); + var itemTagValues = loadAllFiles(manager, "item_tag_emcs", new HashMap<>(), EmcLoader::loadEmcValues); + var blockTagValues = loadAllFiles(manager, "block_tag_emcs", new HashMap<>(), EmcLoader::loadEmcValues); + + var fakeRecipes = loadAllFiles(manager, "fake_recipes", new ArrayList<>(), EmcLoader::loadFakeRecipes); + + Emc.reloadData(itemValues, itemTagValues, blockTagValues, fakeRecipes); + } + + private static T loadAllFiles( + ResourceManager manager, + String path, + T arg, + Loader loader + ) { + manager.listResources( + ErisAlchemy.ID + "/" + path, + loc -> loc.getPath().endsWith(".json") || loc.getPath().endsWith(".json5") + ).forEach((id, res) -> { + ErisAlchemy.LOGGER.info("Loading {}:{}...", id.getNamespace(), id.getPath()); + try (var is = res.open()) { + loader.loadFile(arg, id, is); + } catch (IOException e) { + ErisAlchemy.LOGGER.error( + "Error occured while reading {}:{}", + id.getNamespace(), + id.getPath(), + e + ); + } + }); + return arg; + } + + private static void loadEmcValues(Map map, ResourceLocation id, InputStream is) + throws IOException + { + try (var reader = new InputStreamReader(is)) { + var json = GSON.fromJson(reader, new TypeToken>(){}); + json.forEach((item, newEmcRaw) -> { + var newEmc = newEmcRaw == null ? OptionalDouble.empty() : OptionalDouble.of(newEmcRaw); + var oldEmc = map.get(item); + if (oldEmc != null && !newEmc.equals(oldEmc)) { + var oldStr = oldEmc.isEmpty() ? "NONE" : Double.toString(oldEmc.getAsDouble()); + var newStr = newEmc.isEmpty() ? "NONE" : Double.toString(newEmc.getAsDouble()); + ErisAlchemy.LOGGER.warn( + "Redefining the EMC value for {} from {} to {}", + item, + oldStr, + newStr + ); + } + map.put(item, newEmc); + }); + } + } + + private static void loadFakeRecipes(List recipes, ResourceLocation id, InputStream is) + throws IOException + { + try (var reader = new InputStreamReader(is)) { + var json = GSON.fromJson(reader, new TypeToken>(){}); + recipes.addAll(json); + } + } + + @FunctionalInterface + private interface Loader { + void loadFile(T arg, ResourceLocation id, InputStream is) throws IOException; + } + + private EmcLoader() {} +} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/ErisAlchemy.java b/src/main/java/lv/enes/mc/eris_alchemy/ErisAlchemy.java index bb804b3..bb84e18 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/ErisAlchemy.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/ErisAlchemy.java @@ -1,18 +1,27 @@ package lv.enes.mc.eris_alchemy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.PackType; import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.ItemStack; import org.quiltmc.loader.api.ModContainer; import org.quiltmc.qsl.base.api.entrypoint.ModInitializer; +import org.quiltmc.qsl.resource.loader.api.ResourceLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ErisAlchemy implements ModInitializer { + public static final Gson GSON = new GsonBuilder() + .registerTypeAdapter(ResourceLocation.class, new ResourceLocation.Serializer()) + .registerTypeAdapter(SimplifiedRecipe.class, new SimplifiedRecipe.Deserializer()) + .create(); + public static final String ID = "eris_alchemy"; public static final Logger LOGGER = LoggerFactory.getLogger(ID); @@ -31,6 +40,8 @@ public class ErisAlchemy implements ModInitializer { throw new RuntimeException("Hardcoded mod ID doesn't match the configured one!"); } + ResourceLoader.get(PackType.SERVER_DATA).registerReloader(EmcLoader.INSTANCE); + Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, new ResourceLocation(ID, "item_group"), ITEM_GROUP); ErisAlchemyRegistry.BlockEntities.consume( diff --git a/src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java b/src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java new file mode 100644 index 0000000..ef69b84 --- /dev/null +++ b/src/main/java/lv/enes/mc/eris_alchemy/SimplifiedRecipe.java @@ -0,0 +1,89 @@ +package lv.enes.mc.eris_alchemy; + +import com.google.gson.*; +import jakarta.annotation.Nonnull; +import lv.enes.mc.eris_alchemy.utils.RecipeUtils; +import net.minecraft.core.RegistryAccess; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.item.crafting.Recipe; +import net.minecraft.world.item.crafting.ShapedRecipe; + +import java.lang.reflect.Type; +import java.util.List; + +public record SimplifiedRecipe(ItemStack output, List remainder, List input) { + public SimplifiedRecipe(Recipe recipe, RegistryAccess registryAccess) { + this( + RecipeUtils.getOutput(recipe, registryAccess), + List.of(), // TODO: + RecipeUtils.getIngredients(recipe).stream().filter(ingredient -> !ingredient.isEmpty()).toList() + ); + } + + static class Deserializer implements JsonDeserializer { + @Nonnull + @Override + public SimplifiedRecipe deserialize( + JsonElement jsonElement, + Type type, + JsonDeserializationContext jsonDeserializationContext + ) throws JsonParseException { + if (!jsonElement.isJsonObject()) { + throw new JsonParseException("Recipe must be an object"); + } + var obj = jsonElement.getAsJsonObject(); + + if (obj.get("output") == null || obj.get("input") == null) { + throw new JsonParseException("Recipe must have 'output' and 'input' fields"); + } + + var output = parseOutputOrRemainder(obj.get("output")); + var remainder = parseRemainders(obj.get("remainder")); + var input = parseInputs(obj.get("input")); + return new SimplifiedRecipe(output, remainder, input); + } + + private List parseInputs(JsonElement el) { + if (el.isJsonArray()) { + return el.getAsJsonArray().asList().stream().map(this::parseInput).map(Ingredient::of).toList(); + } + + return List.of(Ingredient.of(parseInput(el))); + } + + private ItemStack parseInput(JsonElement el) { + if (el.isJsonObject()) { + return ShapedRecipe.itemStackFromJson(el.getAsJsonObject()); + } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { + var id = new ResourceLocation(el.getAsString()); + return BuiltInRegistries.ITEM.get(id).getDefaultInstance(); + } else { + throw new JsonParseException("Every recipe input should be an object or a string"); + } + } + + private ItemStack parseOutputOrRemainder(JsonElement el) { + if (el.isJsonObject()) { + return ShapedRecipe.itemStackFromJson(el.getAsJsonObject()); + } else if (el.isJsonPrimitive() && el.getAsJsonPrimitive().isString()) { + var id = new ResourceLocation(el.getAsString()); + return BuiltInRegistries.ITEM.get(id).getDefaultInstance(); + } else { + throw new JsonParseException("Recipe's output or remainder must be an object or a string"); + } + } + + private List parseRemainders(JsonElement el) { + if (el == null) { + return List.of(); + } else if (el.isJsonArray()) { + return el.getAsJsonArray().asList().stream().map(this::parseOutputOrRemainder).toList(); + } else { + return List.of(parseOutputOrRemainder(el)); + } + } + } +} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/block/EnergyCondenserBlock.java b/src/main/java/lv/enes/mc/eris_alchemy/block/EnergyCondenserBlock.java index 86ac061..af58fc6 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/block/EnergyCondenserBlock.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/block/EnergyCondenserBlock.java @@ -2,7 +2,7 @@ package lv.enes.mc.eris_alchemy.block; import jakarta.annotation.Nonnull; import jakarta.annotation.Nullable; -import lv.enes.mc.eris_alchemy.EMC; +import lv.enes.mc.eris_alchemy.Emc; import lv.enes.mc.eris_alchemy.EmcStorage; import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry; import lv.enes.mc.eris_alchemy.block.entity.EnergyCondenserEntity; @@ -33,7 +33,7 @@ public class EnergyCondenserBlock extends ChestLikeBlock TooltipFlag options ) { if (getStoredEmc(stack) >= 0.1) { - tooltip.add(Component.literal("Stored EMC: %s".formatted(EMC.formatEmc(getStoredEmc(stack))))); + tooltip.add(Component.literal("Stored EMC: %s".formatted(Emc.formatEmc(getStoredEmc(stack))))); } } diff --git a/src/main/java/lv/enes/mc/eris_alchemy/block/entity/EnergyCondenserEntity.java b/src/main/java/lv/enes/mc/eris_alchemy/block/entity/EnergyCondenserEntity.java index 0f39ccb..a264722 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/block/entity/EnergyCondenserEntity.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/block/entity/EnergyCondenserEntity.java @@ -1,7 +1,7 @@ package lv.enes.mc.eris_alchemy.block.entity; import jakarta.annotation.Nonnull; -import lv.enes.mc.eris_alchemy.EMC; +import lv.enes.mc.eris_alchemy.Emc; import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry.BlockEntities; import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry.Blocks; import lv.enes.mc.eris_alchemy.ErisAlchemyRegistry.Materials; @@ -81,7 +81,7 @@ public class EnergyCondenserEntity extends ChestLikeEntity implements ExtendedSc @Override public void tick(Level world, BlockPos pos, BlockState state) { super.tick(world, pos, state); - EMC.getInstance(world).get(items.get(0)).ifPresent(cost -> { + Emc.getInstance(world).get(items.get(0)).ifPresent(cost -> { tryConsumeEmc(world, cost); tryCloneTemplate(cost); }); @@ -128,7 +128,7 @@ public class EnergyCondenserEntity extends ChestLikeEntity implements ExtendedSc return; } - var emc = EMC.getInstance(world); + var emc = Emc.getInstance(world); var template = items.get(0); var sacrifice = items.stream() .skip(1) // skip the template diff --git a/src/main/java/lv/enes/mc/eris_alchemy/client/EnergyCondenserScreen.java b/src/main/java/lv/enes/mc/eris_alchemy/client/EnergyCondenserScreen.java index b6762c8..7b9871b 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/client/EnergyCondenserScreen.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/client/EnergyCondenserScreen.java @@ -1,6 +1,6 @@ package lv.enes.mc.eris_alchemy.client; -import lv.enes.mc.eris_alchemy.EMC; +import lv.enes.mc.eris_alchemy.Emc; import lv.enes.mc.eris_alchemy.ErisAlchemy; import lv.enes.mc.eris_alchemy.menu.EnergyCondenserMenu; import net.minecraft.client.gui.GuiGraphics; @@ -36,7 +36,7 @@ public class EnergyCondenserScreen extends ChestLikeScreen @Override protected void renderLabels(GuiGraphics graphics, int mouseX, int mouseY) { - graphics.drawString(font, EMC.formatEmc(menu.getStoredEmc()), 28, 6, 0xFFFFFF); + graphics.drawString(font, Emc.formatEmc(menu.getStoredEmc()), 28, 6, 0xFFFFFF); } @Override diff --git a/src/main/java/lv/enes/mc/eris_alchemy/client/ErisAlchemyClient.java b/src/main/java/lv/enes/mc/eris_alchemy/client/ErisAlchemyClient.java index a69d2e1..f4de045 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/client/ErisAlchemyClient.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/client/ErisAlchemyClient.java @@ -1,6 +1,6 @@ package lv.enes.mc.eris_alchemy.client; -import lv.enes.mc.eris_alchemy.EMC; +import lv.enes.mc.eris_alchemy.Emc; import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry; import net.minecraft.client.gui.screens.MenuScreens; import net.minecraft.client.renderer.blockentity.BlockEntityRenderers; @@ -18,9 +18,12 @@ public class ErisAlchemyClient implements ClientModInitializer { ErisAlchemyClientRegistry.MenuScreens.consume(MenuScreens::register); ItemTooltipCallback.EVENT.register((stack, player, context, tooltip) -> { - var world = player == null ? null : player.level(); - var emc = EMC.getInstance(world).get(stack); - emc.ifPresent(value -> tooltip.add(Component.literal("EMC %s".formatted(EMC.formatEmc(value))))); + if (player == null) { + return; + } + + var emc = Emc.getInstance(player.level()).get(stack); + emc.ifPresent(value -> tooltip.add(Component.literal("EMC %s".formatted(Emc.formatEmc(value))))); }); } } diff --git a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralBlockMixin.java b/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralBlockMixin.java deleted file mode 100644 index d8024c9..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralBlockMixin.java +++ /dev/null @@ -1,20 +0,0 @@ -package lv.enes.mc.eris_alchemy.mixin; - -import lv.enes.mc.eris_alchemy.utils.CoralUtils; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.CoralBlock; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Mixin(CoralBlock.class) -public abstract class CoralBlockMixin implements CoralUtils.CoralSuper { - @Final - @Shadow - private Block deadBlock; - - @Override - public Block lv_enes_mc$getDead() { - return deadBlock; - } -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralFanBlockMixin.java b/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralFanBlockMixin.java deleted file mode 100644 index 86ab8f6..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralFanBlockMixin.java +++ /dev/null @@ -1,20 +0,0 @@ -package lv.enes.mc.eris_alchemy.mixin; - -import lv.enes.mc.eris_alchemy.utils.CoralUtils; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.CoralFanBlock; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Mixin(CoralFanBlock.class) -public abstract class CoralFanBlockMixin implements CoralUtils.CoralSuper { - @Final - @Shadow - private Block deadBlock; - - @Override - public Block lv_enes_mc$getDead() { - return deadBlock; - } -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralPlantBlockMixin.java b/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralPlantBlockMixin.java deleted file mode 100644 index a5614f9..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralPlantBlockMixin.java +++ /dev/null @@ -1,20 +0,0 @@ -package lv.enes.mc.eris_alchemy.mixin; - -import lv.enes.mc.eris_alchemy.utils.CoralUtils; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.CoralPlantBlock; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Mixin(CoralPlantBlock.class) -public abstract class CoralPlantBlockMixin implements CoralUtils.CoralSuper { - @Final - @Shadow - private Block deadBlock; - - @Override - public Block lv_enes_mc$getDead() { - return deadBlock; - } -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralWallFanBlockMixin.java b/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralWallFanBlockMixin.java deleted file mode 100644 index 8ffa07e..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/mixin/CoralWallFanBlockMixin.java +++ /dev/null @@ -1,20 +0,0 @@ -package lv.enes.mc.eris_alchemy.mixin; - -import lv.enes.mc.eris_alchemy.utils.CoralUtils; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.CoralWallFanBlock; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; - -@Mixin(CoralWallFanBlock.class) -public abstract class CoralWallFanBlockMixin implements CoralUtils.CoralSuper { - @Final - @Shadow - private Block deadBlock; - - @Override - public Block lv_enes_mc$getDead() { - return deadBlock; - } -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/AxeUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/AxeUtils.java deleted file mode 100644 index 22c0bad..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/AxeUtils.java +++ /dev/null @@ -1,18 +0,0 @@ -package lv.enes.mc.eris_alchemy.utils; - -import net.minecraft.world.item.AxeItem; -import net.minecraft.world.item.Tier; -import net.minecraft.world.level.block.Block; - -import java.util.Map; - -/** This extends AxeItem only to read the STRIPPABLES variable :3 */ -public final class AxeUtils extends AxeItem { - public static Map getStrippables() { - return STRIPPABLES; - } - - private AxeUtils(Tier material, float attackDamage, float attackSpeed, Properties settings) { - super(material, attackDamage, attackSpeed, settings); - } -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/BlockUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/BlockUtils.java deleted file mode 100644 index 88b6231..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/BlockUtils.java +++ /dev/null @@ -1,15 +0,0 @@ -package lv.enes.mc.eris_alchemy.utils; - -import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.tags.TagKey; -import net.minecraft.world.level.block.Block; - -import java.util.stream.Stream; - -public final class BlockUtils { - public static Stream streamTag(TagKey tag) { - return TagUtils.stream(BuiltInRegistries.BLOCK, tag); - } - - private BlockUtils() {} -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/CoralUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/CoralUtils.java deleted file mode 100644 index 1136c9e..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/CoralUtils.java +++ /dev/null @@ -1,44 +0,0 @@ -package lv.enes.mc.eris_alchemy.utils; - -import net.minecraft.tags.BlockTags; -import net.minecraft.world.level.block.*; - -import java.util.stream.Stream; - -public final class CoralUtils { - /** - * @see lv.enes.mc.eris_alchemy.mixin.CoralBlockMixin - * @see lv.enes.mc.eris_alchemy.mixin.CoralFanBlockMixin - * @see lv.enes.mc.eris_alchemy.mixin.CoralPlantBlockMixin - * @see lv.enes.mc.eris_alchemy.mixin.CoralWallFanBlockMixin - */ - public interface CoralSuper { - Block lv_enes_mc$getDead(); - } - - public static Stream streamAllCoralBlocks() { - return BlockUtils.streamTag(BlockTags.CORAL_BLOCKS).map(b -> (CoralBlock)b); - } - - public static Stream streamAllCorals() { - return BlockUtils.streamTag(BlockTags.CORALS).map(b -> (BaseCoralPlantTypeBlock)b); - } - - public static Stream streamAllDeadCoralBlocks() { - return streamAllCoralBlocks().map(CoralUtils::getDeadCoralBlock); - } - - public static Stream streamAllDeadCorals() { - return streamAllCorals().map(CoralUtils::getDeadCoral); - } - - public static Block getDeadCoral(BaseCoralPlantTypeBlock live ) { - return ((CoralSuper)live).lv_enes_mc$getDead(); - } - - public static Block getDeadCoralBlock(CoralBlock live) { - return ((CoralSuper)live).lv_enes_mc$getDead(); - } - - private CoralUtils() {} -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/DyeUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/DyeUtils.java deleted file mode 100644 index 3ceb965..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/DyeUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -package lv.enes.mc.eris_alchemy.utils; - -import net.minecraft.world.item.DyeColor; -import net.minecraft.world.item.DyeItem; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.ConcretePowderBlock; -import net.minecraft.world.level.block.ShulkerBoxBlock; - -public final class DyeUtils { - public static Block getConcrete(DyeColor color) { - return switch (color) { - case BLACK -> Blocks.BLACK_CONCRETE; - case BLUE -> Blocks.BLUE_CONCRETE; - case BROWN -> Blocks.BROWN_CONCRETE; - case CYAN -> Blocks.CYAN_CONCRETE; - case GRAY -> Blocks.GRAY_CONCRETE; - case GREEN -> Blocks.GREEN_CONCRETE; - case LIGHT_BLUE -> Blocks.LIGHT_BLUE_CONCRETE; - case LIGHT_GRAY -> Blocks.LIGHT_GRAY_CONCRETE; - case LIME -> Blocks.LIME_CONCRETE; - case MAGENTA -> Blocks.MAGENTA_CONCRETE; - case ORANGE -> Blocks.ORANGE_CONCRETE; - case PINK -> Blocks.PINK_CONCRETE; - case PURPLE -> Blocks.PURPLE_CONCRETE; - case RED -> Blocks.RED_CONCRETE; - case WHITE -> Blocks.WHITE_CONCRETE; - case YELLOW -> Blocks.YELLOW_CONCRETE; - }; - } - - public static ConcretePowderBlock getConcretePowder(DyeColor color) { - return (ConcretePowderBlock)switch (color) { - case BLACK -> Blocks.BLACK_CONCRETE_POWDER; - case BLUE -> Blocks.BLUE_CONCRETE_POWDER; - case BROWN -> Blocks.BROWN_CONCRETE_POWDER; - case CYAN -> Blocks.CYAN_CONCRETE_POWDER; - case GRAY -> Blocks.GRAY_CONCRETE_POWDER; - case GREEN -> Blocks.GREEN_CONCRETE_POWDER; - case LIGHT_BLUE -> Blocks.LIGHT_BLUE_CONCRETE_POWDER; - case LIGHT_GRAY -> Blocks.LIGHT_GRAY_CONCRETE_POWDER; - case LIME -> Blocks.LIME_CONCRETE_POWDER; - case MAGENTA -> Blocks.MAGENTA_CONCRETE_POWDER; - case ORANGE -> Blocks.ORANGE_CONCRETE_POWDER; - case PINK -> Blocks.PINK_CONCRETE_POWDER; - case PURPLE -> Blocks.PURPLE_CONCRETE_POWDER; - case RED -> Blocks.RED_CONCRETE_POWDER; - case WHITE -> Blocks.WHITE_CONCRETE_POWDER; - case YELLOW -> Blocks.YELLOW_CONCRETE_POWDER; - }; - } - - public static DyeItem getDye(DyeColor color) { - return DyeItem.byColor(color); - } - - public static ShulkerBoxBlock getShulkerBox(DyeColor color) { - return (ShulkerBoxBlock)ShulkerBoxBlock.getBlockByColor(color); - } - - private DyeUtils() {} -} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java index 26d4405..4414d06 100644 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java +++ b/src/main/java/lv/enes/mc/eris_alchemy/utils/ItemUtils.java @@ -1,14 +1,22 @@ package lv.enes.mc.eris_alchemy.utils; +import net.minecraft.core.Holder; import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.tags.TagKey; -import net.minecraft.world.item.Item; - -import java.util.stream.Stream; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.ItemLike; public final class ItemUtils { - public static Stream streamTag(TagKey tag) { - return TagUtils.stream(BuiltInRegistries.ITEM, tag); + public static ResourceLocation getId(Holder holder) { + return getId(holder.value()); + } + + public static ResourceLocation getId(ItemLike item) { + return BuiltInRegistries.ITEM.getKey(item.asItem()); + } + + public static ResourceLocation getId(ItemStack stack) { + return getId(stack.getItem()); } private ItemUtils() {} diff --git a/src/main/java/lv/enes/mc/eris_alchemy/utils/TagUtils.java b/src/main/java/lv/enes/mc/eris_alchemy/utils/TagUtils.java deleted file mode 100644 index 93e577f..0000000 --- a/src/main/java/lv/enes/mc/eris_alchemy/utils/TagUtils.java +++ /dev/null @@ -1,16 +0,0 @@ -package lv.enes.mc.eris_alchemy.utils; - -import net.minecraft.core.Holder; -import net.minecraft.core.Registry; -import net.minecraft.tags.TagKey; - -import java.util.stream.Stream; -import java.util.stream.StreamSupport; - -public final class TagUtils { - public static Stream stream(Registry registry, TagKey tag) { - return StreamSupport.stream(registry.getTagOrEmpty(tag).spliterator(), false).map(Holder::value); - } - - private TagUtils() {} -} -- cgit v1.2.3