Search in sources :

Example 1 with RecipeRegistration

use of org.spongepowered.api.item.recipe.RecipeRegistration in project SpongeCommon by SpongePowered.

the class RecipeTest method onRecipeRegistry.

@Listener
@SuppressWarnings("unchecked")
public void onRecipeRegistry(final RegisterDataPackValueEvent<RecipeRegistration> event) {
    if (!this.enabled) {
        return;
    }
    // Standard recipes and ItemStack(with nbt) ingredient and results
    final Ingredient whiteRock = Ingredient.of(ItemTypes.POLISHED_DIORITE.get());
    final Ingredient whiteBed = Ingredient.of(ItemTypes.WHITE_BED.get());
    final ItemStack bedrock = ItemStack.of(ItemTypes.BEDROCK);
    final RecipeRegistration whiteBedrockRecipe = CraftingRecipe.shapedBuilder().rows().row(whiteRock, whiteRock, whiteRock).row(whiteRock, whiteBed, whiteRock).row(whiteRock, whiteRock, whiteRock).result(bedrock.copy()).key(ResourceKey.of(this.plugin, "white_bedrock")).build();
    event.register(whiteBedrockRecipe);
    final Ingredient redRock = Ingredient.of(ItemTypes.POLISHED_GRANITE);
    final Ingredient redBed = Ingredient.of(ItemTypes.RED_BED);
    final ItemStack redBedRock = bedrock.copy();
    redBedRock.offer(Keys.CUSTOM_NAME, Component.text("Bedrock", NamedTextColor.RED));
    final RecipeRegistration redBedrockRecipe = CraftingRecipe.shapedBuilder().rows().aisle("ggg", "gbg", "ggg").where('g', redRock).where('b', redBed).result(redBedRock).key(ResourceKey.of(this.plugin, "red_bedrock")).build();
    event.register(redBedrockRecipe);
    final ItemStack moreBedrock = bedrock.copy();
    moreBedrock.setQuantity(9);
    final RecipeRegistration moreBedrockRecipe = CraftingRecipe.shapedBuilder().rows().aisle("ggg", "gbg", "ggg").where('g', redRock).where('b', Ingredient.of(bedrock.copy())).result(moreBedrock).key(ResourceKey.of(this.plugin, "more_red_bedrock")).build();
    event.register(moreBedrockRecipe);
    final RecipeRegistration cheapGoldenAppleRecipe = CraftingRecipe.shapelessBuilder().addIngredients(ItemTypes.YELLOW_WOOL, ItemTypes.APPLE).result(ItemStack.of(ItemTypes.GOLDEN_APPLE)).key(ResourceKey.of(this.plugin, "cheap_golden_apple")).build();
    event.register(cheapGoldenAppleRecipe);
    final RecipeRegistration expensiveGoldenAppleRecipe = CraftingRecipe.shapelessBuilder().addIngredients(ItemTypes.YELLOW_WOOL, ItemTypes.ENCHANTED_GOLDEN_APPLE).result(ItemStack.of(ItemTypes.GOLDEN_APPLE)).key(ResourceKey.of(this.plugin, "expensive_golden_apple")).build();
    event.register(expensiveGoldenAppleRecipe);
    final Ingredient bedrocks = Ingredient.of(bedrock, redBedRock);
    final RecipeRegistration bedrocksToGranite = CraftingRecipe.shapelessBuilder().addIngredients(bedrocks, bedrocks).result(ItemStack.of(ItemTypes.GRANITE, 13)).key(ResourceKey.of(this.plugin, "bedrocks_to_granite")).build();
    event.register(bedrocksToGranite);
    final RecipeRegistration diamondToCoalRecipe = CookingRecipe.builder().type(RecipeTypes.SMELTING).ingredient(Ingredient.of(ItemTypes.DIAMOND)).result(ItemTypes.COAL).experience(0).key(ResourceKey.of(this.plugin, "diamond_to_coal")).build();
    event.register(diamondToCoalRecipe);
    final RecipeRegistration burnPaperAndSticksRecipe = CookingRecipe.builder().type(RecipeTypes.SMELTING).ingredient(Ingredient.of(ItemTypes.PAPER, ItemTypes.STICK)).result(ItemTypes.GUNPOWDER).experience(1).cookingTime(Ticks.of(1)).key(ResourceKey.of(this.plugin, "burn_paper_and_sticks")).build();
    event.register(burnPaperAndSticksRecipe);
    final RecipeRegistration charcoalToCoalRecipe = CookingRecipe.builder().type(RecipeTypes.BLASTING).ingredient(Ingredient.of(ItemTypes.CHARCOAL)).result(ItemTypes.COAL).key(ResourceKey.of(this.plugin, "charcoal_to_coal")).build();
    event.register(charcoalToCoalRecipe);
    final ItemStack redderBedrock = bedrock.copy();
    redderBedrock.offer(Keys.CUSTOM_NAME, Component.text("Bedrock", NamedTextColor.DARK_RED));
    final RecipeRegistration removeRedOnBedrock = CookingRecipe.builder().type(RecipeTypes.BLASTING).ingredient(Ingredient.of(redBedRock)).result(redderBedrock).cookingTime(Ticks.of(20)).experience(100).key(ResourceKey.of(this.plugin, "redder_bedrock")).build();
    event.register(removeRedOnBedrock);
    final RecipeRegistration overcookedPorkchopRecipe = CookingRecipe.builder().type(RecipeTypes.SMOKING).ingredient(Ingredient.of(ItemTypes.COOKED_PORKCHOP)).result(ItemTypes.COAL).key(ResourceKey.of(this.plugin, "overcooked_porkchop")).build();
    event.register(overcookedPorkchopRecipe);
    final RecipeRegistration sticksToTorches = CookingRecipe.builder().type(RecipeTypes.CAMPFIRE_COOKING).ingredient(Ingredient.of(ItemTypes.STICK)).result(ItemTypes.TORCH).cookingTime(Ticks.of(20)).key(ResourceKey.of(this.plugin, "stick_to_torch")).build();
    event.register(sticksToTorches);
    final RecipeRegistration stonecutter1 = StoneCutterRecipe.builder().ingredient(ItemTypes.BEDROCK).result(ItemStack.of(ItemTypes.BLACK_CONCRETE, 64)).key(ResourceKey.of(this.plugin, "cut_bedrock_to_concrete")).build();
    final RecipeRegistration stonecutter2 = StoneCutterRecipe.builder().ingredient(ItemTypes.BEDROCK).result(ItemStack.of(ItemTypes.BLACK_GLAZED_TERRACOTTA, 64)).key(ResourceKey.of(this.plugin, "cut_bedrock_to_terracotta")).build();
    final RecipeRegistration stonecutter3 = StoneCutterRecipe.builder().ingredient(ItemTypes.BEDROCK).result(ItemStack.of(ItemTypes.BLACK_WOOL, 64)).key(ResourceKey.of(this.plugin, "cut_bedrock_wool")).build();
    event.register(stonecutter1);
    event.register(stonecutter2);
    event.register(stonecutter3);
    // Predicate Ingredients
    // e.g. obsidian
    final Predicate<ItemStack> hardnessPredicate = stack -> stack.type().block().map(b -> b.defaultState().get(Keys.DESTROY_SPEED).orElse(0d) > 20).orElse(false);
    final Ingredient hardBlock = Ingredient.of(ResourceKey.of(this.plugin, "hardblock"), hardnessPredicate, ItemStack.of(ItemTypes.BEDROCK));
    final RecipeRegistration hardblockToWool = ShapelessCraftingRecipe.builder().addIngredients(hardBlock).result(ItemStack.of(ItemTypes.WHITE_WOOL)).key(ResourceKey.of(this.plugin, "hardblock_to_wool")).build();
    event.register(hardblockToWool);
    // Function Results
    final ItemStack villagerEgg = ItemStack.of(ItemTypes.VILLAGER_SPAWN_EGG);
    final RecipeRegistration villagerSpawnEggRecipe = ShapedCraftingRecipe.builder().aisle(" e ", "eve", " e ").where('v', Ingredient.of(ItemTypes.BOOK)).where('e', Ingredient.of(ItemTypes.EMERALD_BLOCK)).result(grid -> {
        final Optional<ServerPlayer> player = Sponge.server().causeStackManager().currentCause().first(ServerPlayer.class);
        final String name = player.map(ServerPlayer::name).orElse("Steve");
        villagerEgg.offer(Keys.CUSTOM_NAME, Component.text(name));
        return villagerEgg.copy();
    }, villagerEgg.copy()).key(ResourceKey.of(this.plugin, "villager_spawn_egg")).build();
    event.register(villagerSpawnEggRecipe);
    final ItemStack writtenBook = ItemStack.of(ItemTypes.WRITTEN_BOOK);
    writtenBook.offer(Keys.CUSTOM_NAME, Component.text("Biome Data"));
    writtenBook.offer(Keys.AUTHOR, Component.text("Herobrine"));
    final RecipeRegistration biomeDetectorRecipe = ShapedCraftingRecipe.builder().aisle("d", "b").where('d', Ingredient.of(ItemTypes.DAYLIGHT_DETECTOR)).where('b', Ingredient.of(ItemTypes.BOOK)).result(grid -> {
        final Optional<ServerPlayer> player = Sponge.server().causeStackManager().currentCause().first(ServerPlayer.class);
        final Optional<Biome> biome = player.map(p -> p.world().biome(p.blockPosition()));
        final String name = biome.map(present -> RegistryTypes.BIOME.keyFor(player.get().world(), present)).map(ResourceKey::toString).orElse("Unknown");
        final Integer biomeTemperature = biome.map(Biome::temperature).map(d -> (int) (d * 10)).orElse(0);
        final Integer biomeHumidity = biome.map(Biome::humidity).map(d -> (int) (d * 10)).orElse(0);
        final TextComponent temperature = Component.text("Temperature: ").append(Component.text(biomeTemperature));
        final TextComponent humidity = Component.text("Humidity: ").append(Component.text(biomeHumidity));
        writtenBook.offer(Keys.CUSTOM_NAME, Component.text("Biome Data: " + name));
        writtenBook.offer(Keys.PAGES, Arrays.asList(temperature, humidity));
        writtenBook.offer(Keys.AUTHOR, Component.text(player.map(ServerPlayer::name).orElse("Herobrine")));
        return writtenBook.copy();
    }, writtenBook.copy()).key(ResourceKey.of(this.plugin, "biome_detector")).build();
    event.register(biomeDetectorRecipe);
    final Ingredient blackOrWhite = Ingredient.of(ItemTypes.BLACK_WOOL, ItemTypes.WHITE_WOOL);
    final RecipeRegistration blackOrWhiteRecipe = ShapelessCraftingRecipe.builder().addIngredients(blackOrWhite, blackOrWhite, blackOrWhite).result(grid -> {
        final int blacks = grid.query(QueryTypes.ITEM_TYPE, ItemTypes.BLACK_WOOL).capacity();
        final int whites = grid.query(QueryTypes.ITEM_TYPE, ItemTypes.WHITE_WOOL).capacity();
        return blacks > whites ? ItemStack.of(ItemTypes.BLACK_WOOL, 3) : ItemStack.of(ItemTypes.WHITE_WOOL, 3);
    }, ItemStack.empty()).key(ResourceKey.of(this.plugin, "black_or_white")).build();
    event.register(blackOrWhiteRecipe);
    // Custom results dont work well in cooking recipes
    // final ItemStack anvil = ItemStack.of(ItemTypes.DAMAGED_ANVIL);
    // final RecipeRegistration<SmeltingRecipe> cookedAnvilRecipe = SmeltingRecipe.builder().type(RecipeTypes.BLASTING)
    // .ingredient(ItemTypes.IRON_BLOCK)
    // .result(inv -> {
    // return anvil.copy();
    // }, anvil.copy())
    // .key(ResourceKey.of(this.plugin, "cooked_anvil"))
    // .build();
    // event.register(cookedAnvilRecipe);
    final RecipeRegistration cutPlanksRecipe = StoneCutterRecipe.builder().ingredient(ItemTypes.OAK_PLANKS).result(input -> {
        if (new Random().nextBoolean()) {
            return ItemStack.of(ItemTypes.OAK_SLAB, 4);
        }
        return ItemStack.of(ItemTypes.OAK_SLAB, 3);
    }, ItemStack.of(ItemTypes.OAK_SLAB, 2)).key(ResourceKey.of(this.plugin, "cut_planks")).build();
    event.register(cutPlanksRecipe);
    final RecipeRegistration stripedBannerRecipe = SpecialCraftingRecipe.builder().matching((inv, world) -> {
        if (inv.capacity() != 9) {
            return false;
        }
        final ItemType stick = inv.asGrid().peek(2, 1).get().type();
        if (!stick.isAnyOf(ItemTypes.STICK)) {
            return false;
        }
        final ItemStack middleItem = inv.peekAt(1).get();
        final ItemType type00 = inv.asGrid().peek(0, 0).get().type();
        final ItemType type10 = inv.asGrid().peek(0, 1).get().type();
        final ItemType type20 = inv.asGrid().peek(0, 2).get().type();
        final ItemType type01 = inv.asGrid().peek(1, 0).get().type();
        final ItemType type11 = inv.asGrid().peek(1, 1).get().type();
        final ItemType type21 = inv.asGrid().peek(1, 2).get().type();
        if (type00 == type01 && type01 == type20 && type20 == type21 && type10 == type11) {
            if (type00.isAnyOf(ItemTypes.WHITE_WOOL)) {
                if (middleItem.get(Keys.DYE_COLOR).isPresent()) {
                    return true;
                }
            }
        }
        return false;
    }).result((inv -> {
        final DyeColor dyeColor = inv.peekAt(1).get().get(Keys.DYE_COLOR).get();
        final ItemStack banner = ItemStack.of(ItemTypes.WHITE_BANNER);
        final BannerPatternLayer pattern = BannerPatternLayer.of(BannerPatternShapes.STRIPE_CENTER, dyeColor);
        banner.offer(Keys.BANNER_PATTERN_LAYERS, Arrays.asList(pattern));
        return banner;
    })).key(ResourceKey.of(this.plugin, "special")).build();
    event.register(stripedBannerRecipe);
    final RecipeRegistration squeezeSpongeRecipe = ShapelessCraftingRecipe.builder().addIngredients(ItemTypes.WET_SPONGE, ItemTypes.BUCKET).remainingItems(inv -> inv.slots().stream().map(Slot::peek).map(item -> (item.type().isAnyOf(ItemTypes.WET_SPONGE) ? ItemTypes.SPONGE : ItemTypes.AIR).get()).map(ItemStack::of).collect(Collectors.toList())).result(ItemStack.of(ItemTypes.WATER_BUCKET)).key(ResourceKey.of(this.plugin, "squeeze_sponge_recipe")).build();
    event.register(squeezeSpongeRecipe);
    // Smithing recipes
    final RecipeRegistration ironToGoldIngot = SmithingRecipe.builder().base(ItemTypes.IRON_INGOT).addition(ItemTypes.NETHERITE_INGOT).result(ItemStack.of(ItemTypes.GOLD_INGOT)).key(ResourceKey.of(this.plugin, "iron_to_gold_ingot")).build();
    event.register(ironToGoldIngot);
    final ItemStack chainMail = ItemStack.of(ItemTypes.CHAINMAIL_CHESTPLATE);
    chainMail.offer(Keys.CUSTOM_NAME, Component.text("Heavy Chainmail", NamedTextColor.DARK_GRAY));
    chainMail.offer(Keys.LORE, Arrays.asList(Component.text("Smithing occured", NamedTextColor.DARK_GRAY)));
    final RecipeRegistration smithChainmail = SmithingRecipe.builder().base(ItemTypes.IRON_CHESTPLATE).addition(ItemTypes.FIRE_CHARGE).result(chainMail).key(ResourceKey.of(this.plugin, "smith_chainmail")).build();
    event.register(smithChainmail);
}
Also used : ShapelessCraftingRecipe(org.spongepowered.api.item.recipe.crafting.ShapelessCraftingRecipe) Arrays(java.util.Arrays) Inject(com.google.inject.Inject) ItemTypes(org.spongepowered.api.item.ItemTypes) Random(java.util.Random) Biome(org.spongepowered.api.world.biome.Biome) ShapedCraftingRecipe(org.spongepowered.api.item.recipe.crafting.ShapedCraftingRecipe) ItemStack(org.spongepowered.api.item.inventory.ItemStack) RegisterDataPackValueEvent(org.spongepowered.api.event.lifecycle.RegisterDataPackValueEvent) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) Component(net.kyori.adventure.text.Component) CookingRecipe(org.spongepowered.api.item.recipe.cooking.CookingRecipe) ResourceKey(org.spongepowered.api.ResourceKey) SmithingRecipe(org.spongepowered.api.item.recipe.smithing.SmithingRecipe) RecipeTypes(org.spongepowered.api.item.recipe.RecipeTypes) SpecialCraftingRecipe(org.spongepowered.api.item.recipe.crafting.SpecialCraftingRecipe) TextComponent(net.kyori.adventure.text.TextComponent) Plugin(org.spongepowered.plugin.builtin.jvm.Plugin) Ticks(org.spongepowered.api.util.Ticks) LoadableModule(org.spongepowered.test.LoadableModule) DyeColor(org.spongepowered.api.data.type.DyeColor) Predicate(java.util.function.Predicate) Sponge(org.spongepowered.api.Sponge) RecipeRegistration(org.spongepowered.api.item.recipe.RecipeRegistration) StoneCutterRecipe(org.spongepowered.api.item.recipe.single.StoneCutterRecipe) Slot(org.spongepowered.api.item.inventory.Slot) QueryTypes(org.spongepowered.api.item.inventory.query.QueryTypes) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) Collectors(java.util.stream.Collectors) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) Keys(org.spongepowered.api.data.Keys) BannerPatternLayer(org.spongepowered.api.data.meta.BannerPatternLayer) PluginContainer(org.spongepowered.plugin.PluginContainer) CraftingRecipe(org.spongepowered.api.item.recipe.crafting.CraftingRecipe) CommandContext(org.spongepowered.api.command.parameter.CommandContext) Optional(java.util.Optional) BannerPatternShapes(org.spongepowered.api.data.type.BannerPatternShapes) Listener(org.spongepowered.api.event.Listener) ItemType(org.spongepowered.api.item.ItemType) CommandException(org.spongepowered.api.command.exception.CommandException) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) TextComponent(net.kyori.adventure.text.TextComponent) BannerPatternLayer(org.spongepowered.api.data.meta.BannerPatternLayer) ItemType(org.spongepowered.api.item.ItemType) RecipeRegistration(org.spongepowered.api.item.recipe.RecipeRegistration) DyeColor(org.spongepowered.api.data.type.DyeColor) Biome(org.spongepowered.api.world.biome.Biome) Random(java.util.Random) Ingredient(org.spongepowered.api.item.recipe.crafting.Ingredient) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Aggregations

Inject (com.google.inject.Inject)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 Random (java.util.Random)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Component (net.kyori.adventure.text.Component)1 TextComponent (net.kyori.adventure.text.TextComponent)1 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)1 ResourceKey (org.spongepowered.api.ResourceKey)1 Sponge (org.spongepowered.api.Sponge)1 CommandException (org.spongepowered.api.command.exception.CommandException)1 CommandContext (org.spongepowered.api.command.parameter.CommandContext)1 Keys (org.spongepowered.api.data.Keys)1 BannerPatternLayer (org.spongepowered.api.data.meta.BannerPatternLayer)1 BannerPatternShapes (org.spongepowered.api.data.type.BannerPatternShapes)1 DyeColor (org.spongepowered.api.data.type.DyeColor)1 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)1 Listener (org.spongepowered.api.event.Listener)1 RegisterDataPackValueEvent (org.spongepowered.api.event.lifecycle.RegisterDataPackValueEvent)1