diff --git a/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e index d19e3ba7e..ac8806830 100644 --- a/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e +++ b/src/generated/resources/.cache/9fb1092f32d4fcbf9e061ffd718d4ec689c6c95e @@ -1,3 +1,4 @@ -// 1.20.1 2025-03-11T20:08:33.5240751 Recipes +// 1.20.1 2025-03-11T20:54:56.3941101 Recipes 9775e550332c69f1ee2ac4a09834a35db6f6a2ed data/minecraft/recipes/ammo_box_add_ammo.json +b23d5c199168cf868d89722c62042bfa8852b768 data/minecraft/recipes/ammo_box_extract_ammo.json 2e1d1bbf32801f3d355c0d3f78ebbb1122cebd4c data/minecraft/recipes/potion_mortar_shell.json diff --git a/src/generated/resources/data/minecraft/recipes/ammo_box_extract_ammo.json b/src/generated/resources/data/minecraft/recipes/ammo_box_extract_ammo.json new file mode 100644 index 000000000..6cda2aa93 --- /dev/null +++ b/src/generated/resources/data/minecraft/recipes/ammo_box_extract_ammo.json @@ -0,0 +1,4 @@ +{ + "type": "superbwarfare:ammo_box_extract_ammo", + "category": "misc" +} \ No newline at end of file diff --git a/src/main/java/com/atsuishio/superbwarfare/datagen/ModRecipeProvider.java b/src/main/java/com/atsuishio/superbwarfare/datagen/ModRecipeProvider.java index a5d4fec21..a76cb0312 100644 --- a/src/main/java/com/atsuishio/superbwarfare/datagen/ModRecipeProvider.java +++ b/src/main/java/com/atsuishio/superbwarfare/datagen/ModRecipeProvider.java @@ -20,5 +20,6 @@ public class ModRecipeProvider extends RecipeProvider implements IConditionBuild protected void buildRecipes(@NotNull Consumer pWriter) { SpecialRecipeBuilder.special(ModRecipes.POTION_MORTAR_SHELL_SERIALIZER.get()).save(pWriter, "potion_mortar_shell"); SpecialRecipeBuilder.special(ModRecipes.AMMO_BOX_ADD_AMMO_SERIALIZER.get()).save(pWriter, "ammo_box_add_ammo"); + SpecialRecipeBuilder.special(ModRecipes.AMMO_BOX_EXTRACT_AMMO_SERIALIZER.get()).save(pWriter, "ammo_box_extract_ammo"); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModRecipes.java b/src/main/java/com/atsuishio/superbwarfare/init/ModRecipes.java index 39102c59c..ad8b7ebcb 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModRecipes.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModRecipes.java @@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.init; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.recipe.AmmoBoxAddAmmoRecipe; +import com.atsuishio.superbwarfare.recipe.AmmoBoxExtractAmmoRecipe; import com.atsuishio.superbwarfare.recipe.PotionMortarShellRecipe; import net.minecraft.world.item.crafting.RecipeSerializer; import net.minecraft.world.item.crafting.SimpleCraftingRecipeSerializer; @@ -18,4 +19,7 @@ public class ModRecipes { public static final RegistryObject> AMMO_BOX_ADD_AMMO_SERIALIZER = RECIPE_SERIALIZERS.register("ammo_box_add_ammo", () -> new SimpleCraftingRecipeSerializer<>(AmmoBoxAddAmmoRecipe::new)); + public static final RegistryObject> AMMO_BOX_EXTRACT_AMMO_SERIALIZER = + RECIPE_SERIALIZERS.register("ammo_box_extract_ammo", () -> new SimpleCraftingRecipeSerializer<>(AmmoBoxExtractAmmoRecipe::new)); + } diff --git a/src/main/java/com/atsuishio/superbwarfare/recipe/AmmoBoxExtractAmmoRecipe.java b/src/main/java/com/atsuishio/superbwarfare/recipe/AmmoBoxExtractAmmoRecipe.java new file mode 100644 index 000000000..a56775c65 --- /dev/null +++ b/src/main/java/com/atsuishio/superbwarfare/recipe/AmmoBoxExtractAmmoRecipe.java @@ -0,0 +1,104 @@ +package com.atsuishio.superbwarfare.recipe; + +import com.atsuishio.superbwarfare.init.ModItems; +import com.atsuishio.superbwarfare.init.ModRecipes; +import com.atsuishio.superbwarfare.item.common.ammo.AmmoBox; +import com.atsuishio.superbwarfare.tools.AmmoType; +import net.minecraft.core.NonNullList; +import net.minecraft.core.RegistryAccess; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.inventory.CraftingContainer; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.crafting.CraftingBookCategory; +import net.minecraft.world.item.crafting.CustomRecipe; +import net.minecraft.world.item.crafting.RecipeSerializer; +import net.minecraft.world.level.Level; +import org.jetbrains.annotations.NotNull; + +public class AmmoBoxExtractAmmoRecipe extends CustomRecipe { + + public AmmoBoxExtractAmmoRecipe(ResourceLocation pId, CraftingBookCategory pCategory) { + super(pId, pCategory); + } + + @Override + public boolean matches(CraftingContainer pContainer, @NotNull Level pLevel) { + var hasAmmoBox = false; + var ammoBoxItem = ItemStack.EMPTY; + + for (var item : pContainer.getItems()) { + if (item.getItem() instanceof AmmoBox) { + if (hasAmmoBox) return false; + hasAmmoBox = true; + ammoBoxItem = item; + } else if (!item.isEmpty()) { + return false; + } + } + + var tag = ammoBoxItem.getTag(); + if (tag == null) return false; + + var typeString = tag.getString("Type"); + var type = AmmoType.getType(typeString); + if (type == null) return false; + + return type.get(ammoBoxItem) > 0; + } + + + @Override + public @NotNull ItemStack assemble(CraftingContainer pContainer, @NotNull RegistryAccess pRegistryAccess) { + AmmoType type = null; + + for (var item : pContainer.getItems()) { + if (item.getItem() instanceof AmmoBox) { + type = AmmoType.getType(item.getOrCreateTag().getString("Type")); + break; + } + } + + assert type != null; + + // 也许这边有更好的方案? + return switch (type) { + case HANDGUN -> new ItemStack(ModItems.HANDGUN_AMMO.get()); + case RIFLE -> new ItemStack(ModItems.RIFLE_AMMO.get()); + case SHOTGUN -> new ItemStack(ModItems.SHOTGUN_AMMO.get()); + case SNIPER -> new ItemStack(ModItems.SNIPER_AMMO.get()); + case HEAVY -> new ItemStack(ModItems.HEAVY_AMMO.get()); + default -> throw new IllegalStateException("Unexpected value: " + type); + }; + } + + @Override + public @NotNull NonNullList getRemainingItems(@NotNull CraftingContainer pContainer) { + var remaining = super.getRemainingItems(pContainer); + + for (int i = 0; i < pContainer.getContainerSize(); i++) { + var item = pContainer.getItem(i); + if (item.getItem() instanceof AmmoBox) { + var ammoBox = item.copy(); + AmmoType type = AmmoType.getType(item.getOrCreateTag().getString("Type")); + + assert type != null; + type.add(ammoBox, -1); + remaining.set(i, ammoBox); + + break; + } + } + + return remaining; + } + + @Override + public boolean canCraftInDimensions(int pWidth, int pHeight) { + return true; + } + + @Override + public @NotNull RecipeSerializer getSerializer() { + return ModRecipes.AMMO_BOX_ADD_AMMO_SERIALIZER.get(); + } +}