Skip to content

Commit

Permalink
让矿物涌泉使用配方系统
Browse files Browse the repository at this point in the history
  • Loading branch information
DancingSnow0517 committed Sep 27, 2024
1 parent 91e591c commit ab165d5
Show file tree
Hide file tree
Showing 12 changed files with 532 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parent": "minecraft:recipes/root",
"criteria": {
"has_the_recipe": {
"conditions": {
"recipe": "anvilcraft:mineral_fountain/deepslate_iron_ore"
},
"trigger": "minecraft:recipe_unlocked"
}
},
"requirements": [
[
"has_the_recipe"
]
],
"rewards": {
"recipes": [
"anvilcraft:mineral_fountain/deepslate_iron_ore"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parent": "minecraft:recipes/root",
"criteria": {
"has_the_recipe": {
"conditions": {
"recipe": "anvilcraft:mineral_fountain_chance/earth_core_shard_ore"
},
"trigger": "minecraft:recipe_unlocked"
}
},
"requirements": [
[
"has_the_recipe"
]
],
"rewards": {
"recipes": [
"anvilcraft:mineral_fountain_chance/earth_core_shard_ore"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"parent": "minecraft:recipes/root",
"criteria": {
"has_the_recipe": {
"conditions": {
"recipe": "anvilcraft:mineral_fountain_chance/void_stone"
},
"trigger": "minecraft:recipe_unlocked"
}
},
"requirements": [
[
"has_the_recipe"
]
],
"rewards": {
"recipes": [
"anvilcraft:mineral_fountain_chance/void_stone"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "anvilcraft:mineral_fountain",
"from_block": "minecraft:deepslate",
"need_block": "minecraft:deepslate_iron_ore",
"to_block": "minecraft:deepslate_iron_ore"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "anvilcraft:mineral_fountain_chance",
"chance": 0.1,
"dimension": "minecraft:overworld",
"from_block": "minecraft:deepslate",
"to_block": "anvilcraft:earth_core_shard_ore"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "anvilcraft:mineral_fountain_chance",
"chance": 0.1,
"dimension": "minecraft:overworld",
"from_block": "minecraft:deepslate",
"to_block": "anvilcraft:void_stone"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@

import dev.dubhe.anvilcraft.api.heatable.HeatableBlockManager;
import dev.dubhe.anvilcraft.init.ModBlockEntities;
import dev.dubhe.anvilcraft.init.ModBlockTags;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.init.ModRecipeTypes;
import dev.dubhe.anvilcraft.recipe.mineral.MineralFountainRecipe;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MineralFountainBlockEntity extends BlockEntity {
private static final HashMap<ResourceLocation, HashMap<Block, Float>> CHANGE_MAP = new HashMap<>() {
Expand Down Expand Up @@ -66,9 +68,9 @@ public void tick() {
tickCount++;
if (tickCount < 20) return;
tickCount = 0;
BlockState aroundBlock = getAroundBlock();
BlockState aroundState = getAroundBlock();
// 冷却检查
if (aroundBlock.is(Blocks.BLUE_ICE)
if (aroundState.is(Blocks.BLUE_ICE)
|| aroundHas(Blocks.BEDROCK)
|| aroundHas(ModBlocks.MINERAL_FOUNTAIN.get())) {
level.destroyBlock(getBlockPos(), false);
Expand All @@ -84,49 +86,74 @@ public void tick() {
checkPos = checkPos.below();
if (!level.getBlockState(checkPos).is(Blocks.BEDROCK)) return;
}
BlockState aboveBlock = level.getBlockState(getBlockPos().above());
BlockState aboveState = level.getBlockState(getBlockPos().above());
// 岩浆处理
if (aroundBlock.is(Blocks.LAVA)) {
if (aboveBlock.is(Blocks.AIR)) {
if (aroundState.is(Blocks.LAVA)) {
if (aboveState.is(Blocks.AIR)) {
level.setBlockAndUpdate(getBlockPos().above(), Blocks.LAVA.defaultBlockState());
return;
}
Block hotBlock = HeatableBlockManager.getHotBlock(aboveBlock.getBlock());
Block hotBlock = HeatableBlockManager.getHotBlock(aboveState.getBlock());
if (hotBlock == null) return;
level.setBlockAndUpdate(getBlockPos().above(), hotBlock.defaultBlockState());
} else if (aroundBlock.is(ModBlockTags.DEEPSLATE_METAL) && aboveBlock.is(Blocks.DEEPSLATE)) {
HashMap<Block, Float> changeMap =
CHANGE_MAP.containsKey(level.dimension().location())
? CHANGE_MAP.get(level.dimension().location())
: CHANGE_MAP.get(Level.OVERWORLD.location());
for (Block block : changeMap.keySet()) {
if (level.getRandom().nextDouble() <= changeMap.get(block)) {
level.setBlockAndUpdate(getBlockPos().above(), block.defaultBlockState());
return;
}
}
level.setBlockAndUpdate(getBlockPos().above(), aroundBlock);
} else {
MineralFountainRecipe.Input input =
new MineralFountainRecipe.Input(aroundState.getBlock(), aboveState.getBlock());
level.getRecipeManager()
.getRecipeFor(ModRecipeTypes.MINERAL_FOUNTAIN.get(), input, level)
.ifPresent(recipe -> {
var chanceList = level
.getRecipeManager()
.getAllRecipesFor(ModRecipeTypes.MINERAL_FOUNTAIN_CHANCE.get())
.stream()
.filter(r -> r.value()
.getDimension()
.equals(level.dimension().location()))
.filter(r -> r.value().getFromBlock() == aboveState.getBlock())
.toList();
for (var changeRecipe : chanceList) {
if (level.getRandom().nextDouble()
<= changeRecipe.value().getChance()) {
level.setBlockAndUpdate(
getBlockPos().above(),
changeRecipe.value().getToBlock().defaultBlockState());
return;
}
}
level.setBlockAndUpdate(
getBlockPos().above(),
recipe.value().getToBlock().defaultBlockState());
});
}
}

private static final Direction[] HORIZONTAL_DIRECTION = {
Direction.NORTH, Direction.WEST, Direction.EAST, Direction.SOUTH
};

private BlockState getAroundBlock() {
if (level == null) return Blocks.AIR.defaultBlockState();
BlockState blockState = level.getBlockState(getBlockPos().south());
if (blockState.is(Blocks.LAVA) && blockState.getValue(LiquidBlock.LEVEL) > 0)
if (level == null) {
return Blocks.AIR.defaultBlockState();
for (Direction direction : new Direction[] {Direction.NORTH, Direction.WEST, Direction.EAST}) {
BlockState checkBlockState = level.getBlockState(getBlockPos().relative(direction));
if (!checkBlockState.is(blockState.getBlock())) return Blocks.AIR.defaultBlockState();
if (checkBlockState.is(Blocks.LAVA) && checkBlockState.getValue(LiquidBlock.LEVEL) > 0)
return Blocks.AIR.defaultBlockState();
}
return blockState;
List<BlockState> blockStates = Arrays.stream(HORIZONTAL_DIRECTION)
.map(direction -> level.getBlockState(getBlockPos().relative(direction)))
.toList();
BlockState firstState = blockStates.getFirst();
long count = blockStates.stream()
.filter(s -> s.is(firstState.getBlock())
&& (s.getFluidState().isEmpty() || s.getFluidState().isSource()))
.count();
return count == 4 ? firstState : Blocks.AIR.defaultBlockState();
}

private boolean aroundHas(Block block) {
if (level == null) return false;
for (Direction direction : new Direction[] {Direction.SOUTH, Direction.NORTH, Direction.WEST, Direction.EAST}) {
if (level.getBlockState(getBlockPos().relative(direction)).is(block)) return true;
if (level == null) {
return false;
}
for (Direction direction : HORIZONTAL_DIRECTION) {
if (level.getBlockState(getBlockPos().relative(direction)).is(block)) {
return true;
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.dubhe.anvilcraft.data.recipe;

import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.recipe.mineral.MineralFountainChanceRecipe;
import dev.dubhe.anvilcraft.recipe.mineral.MineralFountainRecipe;

import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;

import com.tterrag.registrate.providers.RegistrateRecipeProvider;

public class MineralFountainRecipeLoader {
public static void init(RegistrateRecipeProvider provider) {
MineralFountainRecipe.builder()
.needBlock(Blocks.DEEPSLATE_IRON_ORE)
.fromBlock(Blocks.DEEPSLATE)
.toBlock(Blocks.DEEPSLATE_IRON_ORE)
.save(provider);

MineralFountainChanceRecipe.builder()
.dimension(Level.OVERWORLD.location())
.fromBlock(Blocks.DEEPSLATE)
.toBlock(ModBlocks.VOID_STONE.get())
.chance(0.1)
.save(provider);

MineralFountainChanceRecipe.builder()
.dimension(Level.OVERWORLD.location())
.fromBlock(Blocks.DEEPSLATE)
.toBlock(ModBlocks.EARTH_CORE_SHARD_ORE.get())
.chance(0.1)
.save(provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public static void init(RegistrateRecipeProvider provider) {
MultiBlockRecipeLoader.init(provider);
MobTransformRecipeLoader.init(provider);
ConcreteRecipeLoader.init(provider);

MineralFountainRecipeLoader.init(provider);
}
}
14 changes: 14 additions & 0 deletions src/main/java/dev/dubhe/anvilcraft/init/ModRecipeTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import dev.dubhe.anvilcraft.recipe.anvil.StampingRecipe;
import dev.dubhe.anvilcraft.recipe.anvil.SuperHeatingRecipe;
import dev.dubhe.anvilcraft.recipe.anvil.TimeWarpRecipe;
import dev.dubhe.anvilcraft.recipe.mineral.MineralFountainChanceRecipe;
import dev.dubhe.anvilcraft.recipe.mineral.MineralFountainRecipe;
import dev.dubhe.anvilcraft.recipe.multiblock.MultiblockRecipe;
import dev.dubhe.anvilcraft.recipe.transform.MobTransformRecipe;

Expand Down Expand Up @@ -109,6 +111,18 @@ public class ModRecipeTypes {
public static final DeferredHolder<RecipeSerializer<?>, RecipeSerializer<MultiblockRecipe>> MULITBLOCK_SERIALIZER =
RECIPE_SERIALIZERS.register("multiblock", MultiblockRecipe.Serializer::new);

public static final DeferredHolder<RecipeType<?>, RecipeType<MineralFountainRecipe>> MINERAL_FOUNTAIN =
registerType("mineral_fountain");
public static final DeferredHolder<RecipeSerializer<?>, RecipeSerializer<MineralFountainRecipe>>
MINERAL_FOUNTAIN_SERIALIZER =
RECIPE_SERIALIZERS.register("mineral_fountain", MineralFountainRecipe.Serializer::new);

public static final DeferredHolder<RecipeType<?>, RecipeType<MineralFountainChanceRecipe>> MINERAL_FOUNTAIN_CHANCE =
registerType("mineral_fountain_chance");
public static final DeferredHolder<RecipeSerializer<?>, RecipeSerializer<MineralFountainChanceRecipe>>
MINERAL_FOUNTAIN_CHANCE_SERIALIZER =
RECIPE_SERIALIZERS.register("mineral_fountain_chance", MineralFountainChanceRecipe.Serializer::new);

private static <T extends Recipe<?>> DeferredHolder<RecipeType<?>, RecipeType<T>> registerType(String name) {
return RECIPE_TYPES.register(name, () -> new RecipeType<>() {
@Override
Expand Down
Loading

0 comments on commit ab165d5

Please sign in to comment.