Skip to content

Commit e07fe3c

Browse files
committed
配方添加旋律需求
1 parent 824e5a2 commit e07fe3c

13 files changed

Lines changed: 165 additions & 20 deletions

File tree

src/generated/resources/data/anvilcraft_reverberation/recipe/sound_reactor/acoustic_component.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
"minEnergy": 10,
88
"priority": 0,
9+
"requiredMelody": "anvilcraft_reverberation:equal",
910
"requiredTimbres": [
1011
{
1112
"block": "minecraft:iron_block"

src/main/java/dev/anvilcraft/reverberation/AnvilCraftReverberation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import dev.anvilcraft.reverberation.init.AddonItemGroups;
99
import dev.anvilcraft.reverberation.init.AddonItems;
1010
import dev.anvilcraft.lib.config.ConfigManager;
11+
import dev.anvilcraft.reverberation.init.AddonMelodies;
1112
import dev.anvilcraft.reverberation.init.AddonRecipeType;
1213
import net.minecraft.resources.ResourceLocation;
1314
import net.neoforged.bus.api.IEventBus;
@@ -29,6 +30,7 @@ public AnvilCraftReverberation(IEventBus modEventBus, ModContainer modContainer)
2930
AddonBlocks.register();
3031
AddonBlockEntities.register();
3132
AddonRecipeType.register(modEventBus);
33+
AddonMelodies.register();
3234
AddonDatagen.init();
3335
}
3436

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dev.anvilcraft.reverberation.api;
2+
3+
import com.google.common.base.Supplier;
4+
import com.mojang.serialization.Codec;
5+
import net.minecraft.network.RegistryFriendlyByteBuf;
6+
import net.minecraft.network.codec.StreamCodec;
7+
import net.minecraft.resources.ResourceLocation;
8+
9+
import java.util.Map;
10+
import java.util.concurrent.ConcurrentHashMap;
11+
12+
public abstract class Melody {
13+
private static final Map<ResourceLocation, Melody> MELODY_REGISTRY = new ConcurrentHashMap<>();
14+
15+
public static final Codec<Melody> CODEC = ResourceLocation.CODEC.xmap(
16+
Melody::getOrThrow,
17+
Melody::getId
18+
);
19+
20+
public static final StreamCodec<RegistryFriendlyByteBuf, Melody> STREAM_CODEC = StreamCodec.of(
21+
(buf, melody) -> ResourceLocation.STREAM_CODEC.encode(buf, melody.getId()),
22+
(buf) -> Melody.getOrThrow(ResourceLocation.STREAM_CODEC.decode(buf))
23+
);
24+
25+
public static Melody register(Supplier<Melody> f) {
26+
Melody melody = f.get();
27+
MELODY_REGISTRY.put(melody.getId(), melody);
28+
return melody;
29+
}
30+
31+
public static Melody getOrThrow(ResourceLocation id) {
32+
Melody melody = MELODY_REGISTRY.get(id);
33+
if (melody == null) {
34+
throw new IllegalArgumentException("Unknown Melody: " + id);
35+
}
36+
return melody;
37+
}
38+
39+
public abstract boolean satisfy(MergeSoundStore mergeSoundStore);
40+
41+
public abstract ResourceLocation getId();
42+
}

src/main/java/dev/anvilcraft/reverberation/api/MergeSoundStore.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public boolean isValid(SoundReactorRecipe recipe) {
5959
if (!currentTimbres.contains(requiredTimbre)) return false;
6060
}
6161

62+
// 检查旋律条件是否满足
63+
if (recipe.getRequiredMelody() != null && !recipe.getRequiredMelody().satisfy(this)) {
64+
return false;
65+
}
66+
6267
return true;
6368
}
6469
}

src/main/java/dev/anvilcraft/reverberation/api/melody/Melody.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/dev/anvilcraft/reverberation/data/recipe/SoundReactorRecipeLoader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
import com.tterrag.registrate.providers.RegistrateRecipeProvider;
44
import dev.anvilcraft.reverberation.init.AddonItems;
5+
import dev.anvilcraft.reverberation.init.AddonMelodies;
56
import dev.anvilcraft.reverberation.recipe.SoundReactorRecipe;
67
import dev.dubhe.anvilcraft.init.block.ModBlocks;
78
import dev.dubhe.anvilcraft.init.item.ModItemTags;
89
import net.minecraft.world.level.block.Blocks;
910

1011
public class SoundReactorRecipeLoader {
1112
public static void init(RegistrateRecipeProvider provider) {
13+
// 基础配方 - 不需要旋律
1214
SoundReactorRecipe.builder()
1315
.requires(ModItemTags.BRONZE_PLATES, 2)
1416
.result(AddonItems.ACOUSTIC_COMPONENT.get())
1517
.requiresTimbre(Blocks.IRON_BLOCK)
1618
.requiresTimbre(ModBlocks.BRONZE_BLOCK.get())
19+
.requiresMelody(AddonMelodies.EQUAL_MELODY)
1720
.minEnergy(10)
1821
.save(provider);
1922
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package dev.anvilcraft.reverberation.init;
2+
3+
import dev.anvilcraft.reverberation.melody.AblationMelody;
4+
import dev.anvilcraft.reverberation.melody.EqualMelody;
5+
import dev.anvilcraft.reverberation.melody.HigherMelody;
6+
import dev.anvilcraft.reverberation.melody.LowerMelody;
7+
import dev.anvilcraft.reverberation.api.Melody;
8+
9+
@SuppressWarnings("unused")
10+
public class AddonMelodies {
11+
public static final Melody LOWER_MELODY = Melody.register(LowerMelody::new);
12+
public static final Melody HIGHER_MELODY = Melody.register(HigherMelody::new);
13+
public static final Melody ABLATION_MELODY = Melody.register(AblationMelody::new);
14+
public static final Melody EQUAL_MELODY = Melody.register(EqualMelody::new);
15+
16+
public static void register() {
17+
}
18+
}

src/main/java/dev/anvilcraft/reverberation/api/melody/AblationMelody.java renamed to src/main/java/dev/anvilcraft/reverberation/melody/AblationMelody.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
package dev.anvilcraft.reverberation.api.melody;
1+
package dev.anvilcraft.reverberation.melody;
22

3+
import dev.anvilcraft.reverberation.AnvilCraftReverberation;
4+
import dev.anvilcraft.reverberation.api.Melody;
35
import dev.anvilcraft.reverberation.api.MergeSound;
46
import dev.anvilcraft.reverberation.api.MergeSoundStore;
57
import dev.anvilcraft.reverberation.api.SoundWave;
8+
import dev.dubhe.anvilcraft.init.block.ModBlocks;
9+
import net.minecraft.resources.ResourceLocation;
610
import net.minecraft.world.level.block.Block;
711

812
import java.util.List;
@@ -19,13 +23,17 @@ public boolean satisfy(MergeSoundStore mergeSoundStore) {
1923
int emberEnergy = 0;
2024
for (SoundWave soundWave : soundWaves) {
2125
Block block = soundWave.timbre().block();
22-
// TODO: 根据需要定义霜冻和余烬对应的方块
23-
// if (block == ModBlocks.FROST_ANVIL) {
24-
// frostEnergy += soundWave.loudness();
25-
// } else if (block == ModBlocks.EMBER_ANVIL) {
26-
// emberEnergy += soundWave.loudness();
27-
// }
26+
if (block.equals(ModBlocks.FROST_ANVIL.get())) {
27+
frostEnergy += soundWave.loudness();
28+
} else if (block.equals(ModBlocks.EMBER_ANVIL.get())) {
29+
emberEnergy += soundWave.loudness();
30+
}
2831
}
2932
return frostEnergy == emberEnergy;
3033
}
34+
35+
@Override
36+
public ResourceLocation getId() {
37+
return AnvilCraftReverberation.of("ablation");
38+
}
3139
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dev.anvilcraft.reverberation.melody;
2+
3+
import dev.anvilcraft.reverberation.AnvilCraftReverberation;
4+
import dev.anvilcraft.reverberation.api.Melody;
5+
import dev.anvilcraft.reverberation.api.MergeSound;
6+
import dev.anvilcraft.reverberation.api.MergeSoundStore;
7+
import net.minecraft.resources.ResourceLocation;
8+
9+
import java.util.List;
10+
11+
public class EqualMelody extends Melody {
12+
13+
@Override
14+
public boolean satisfy(MergeSoundStore mergeSoundStore) {
15+
List<MergeSound> soundHistory = mergeSoundStore.getSoundHistory();
16+
if (soundHistory.size() < 2) return false;
17+
18+
MergeSound sound1 = soundHistory.getLast();
19+
MergeSound sound2 = soundHistory.get(soundHistory.size() - 2);
20+
return sound1.getEnergy() == sound2.getEnergy();
21+
}
22+
23+
@Override
24+
public ResourceLocation getId() {
25+
return AnvilCraftReverberation.of("equal");
26+
}
27+
}

src/main/java/dev/anvilcraft/reverberation/api/melody/HigherMelody.java renamed to src/main/java/dev/anvilcraft/reverberation/melody/HigherMelody.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
package dev.anvilcraft.reverberation.api.melody;
1+
package dev.anvilcraft.reverberation.melody;
22

3+
import dev.anvilcraft.reverberation.AnvilCraftReverberation;
4+
import dev.anvilcraft.reverberation.api.Melody;
35
import dev.anvilcraft.reverberation.api.MergeSound;
46
import dev.anvilcraft.reverberation.api.MergeSoundStore;
7+
import net.minecraft.resources.ResourceLocation;
58

69
import java.util.List;
710

@@ -15,4 +18,9 @@ public boolean satisfy(MergeSoundStore mergeSoundStore) {
1518
MergeSound sound2 = soundHistory.get(soundHistory.size() - 2);
1619
return sound1.getEnergy() >= sound2.getEnergy() * 2;
1720
}
21+
22+
@Override
23+
public ResourceLocation getId() {
24+
return AnvilCraftReverberation.of("higher");
25+
}
1826
}

0 commit comments

Comments
 (0)