2
2
3
3
import dev .dubhe .anvilcraft .api .heatable .HeatableBlockManager ;
4
4
import dev .dubhe .anvilcraft .init .ModBlockEntities ;
5
- import dev .dubhe .anvilcraft .init .ModBlockTags ;
6
5
import dev .dubhe .anvilcraft .init .ModBlocks ;
6
+ import dev .dubhe .anvilcraft .init .ModRecipeTypes ;
7
+ import dev .dubhe .anvilcraft .recipe .mineral .MineralFountainRecipe ;
7
8
8
9
import net .minecraft .core .BlockPos ;
9
10
import net .minecraft .core .Direction ;
10
11
import net .minecraft .resources .ResourceLocation ;
11
12
import net .minecraft .world .level .Level ;
12
13
import net .minecraft .world .level .block .Block ;
13
14
import net .minecraft .world .level .block .Blocks ;
14
- import net .minecraft .world .level .block .LiquidBlock ;
15
15
import net .minecraft .world .level .block .entity .BlockEntity ;
16
16
import net .minecraft .world .level .block .entity .BlockEntityType ;
17
17
import net .minecraft .world .level .block .state .BlockState ;
18
18
19
19
import org .jetbrains .annotations .NotNull ;
20
20
21
+ import java .util .Arrays ;
21
22
import java .util .HashMap ;
23
+ import java .util .List ;
22
24
23
25
public class MineralFountainBlockEntity extends BlockEntity {
24
26
private static final HashMap <ResourceLocation , HashMap <Block , Float >> CHANGE_MAP = new HashMap <>() {
@@ -66,9 +68,9 @@ public void tick() {
66
68
tickCount ++;
67
69
if (tickCount < 20 ) return ;
68
70
tickCount = 0 ;
69
- BlockState aroundBlock = getAroundBlock ();
71
+ BlockState aroundState = getAroundBlock ();
70
72
// 冷却检查
71
- if (aroundBlock .is (Blocks .BLUE_ICE )
73
+ if (aroundState .is (Blocks .BLUE_ICE )
72
74
|| aroundHas (Blocks .BEDROCK )
73
75
|| aroundHas (ModBlocks .MINERAL_FOUNTAIN .get ())) {
74
76
level .destroyBlock (getBlockPos (), false );
@@ -84,49 +86,74 @@ public void tick() {
84
86
checkPos = checkPos .below ();
85
87
if (!level .getBlockState (checkPos ).is (Blocks .BEDROCK )) return ;
86
88
}
87
- BlockState aboveBlock = level .getBlockState (getBlockPos ().above ());
89
+ BlockState aboveState = level .getBlockState (getBlockPos ().above ());
88
90
// 岩浆处理
89
- if (aroundBlock .is (Blocks .LAVA )) {
90
- if (aboveBlock .is (Blocks .AIR )) {
91
+ if (aroundState .is (Blocks .LAVA )) {
92
+ if (aboveState .is (Blocks .AIR )) {
91
93
level .setBlockAndUpdate (getBlockPos ().above (), Blocks .LAVA .defaultBlockState ());
92
94
return ;
93
95
}
94
- Block hotBlock = HeatableBlockManager .getHotBlock (aboveBlock .getBlock ());
96
+ Block hotBlock = HeatableBlockManager .getHotBlock (aboveState .getBlock ());
95
97
if (hotBlock == null ) return ;
96
98
level .setBlockAndUpdate (getBlockPos ().above (), hotBlock .defaultBlockState ());
97
- } else if (aroundBlock .is (ModBlockTags .DEEPSLATE_METAL ) && aboveBlock .is (Blocks .DEEPSLATE )) {
98
- HashMap <Block , Float > changeMap =
99
- CHANGE_MAP .containsKey (level .dimension ().location ())
100
- ? CHANGE_MAP .get (level .dimension ().location ())
101
- : CHANGE_MAP .get (Level .OVERWORLD .location ());
102
- for (Block block : changeMap .keySet ()) {
103
- if (level .getRandom ().nextDouble () <= changeMap .get (block )) {
104
- level .setBlockAndUpdate (getBlockPos ().above (), block .defaultBlockState ());
105
- return ;
106
- }
107
- }
108
- level .setBlockAndUpdate (getBlockPos ().above (), aroundBlock );
99
+ } else {
100
+ MineralFountainRecipe .Input input =
101
+ new MineralFountainRecipe .Input (aroundState .getBlock (), aboveState .getBlock ());
102
+ level .getRecipeManager ()
103
+ .getRecipeFor (ModRecipeTypes .MINERAL_FOUNTAIN .get (), input , level )
104
+ .ifPresent (recipe -> {
105
+ var chanceList = level
106
+ .getRecipeManager ()
107
+ .getAllRecipesFor (ModRecipeTypes .MINERAL_FOUNTAIN_CHANCE .get ())
108
+ .stream ()
109
+ .filter (r -> r .value ()
110
+ .getDimension ()
111
+ .equals (level .dimension ().location ()))
112
+ .filter (r -> r .value ().getFromBlock () == aboveState .getBlock ())
113
+ .toList ();
114
+ for (var changeRecipe : chanceList ) {
115
+ if (level .getRandom ().nextDouble ()
116
+ <= changeRecipe .value ().getChance ()) {
117
+ level .setBlockAndUpdate (
118
+ getBlockPos ().above (),
119
+ changeRecipe .value ().getToBlock ().defaultBlockState ());
120
+ return ;
121
+ }
122
+ }
123
+ level .setBlockAndUpdate (
124
+ getBlockPos ().above (),
125
+ recipe .value ().getToBlock ().defaultBlockState ());
126
+ });
109
127
}
110
128
}
111
129
130
+ private static final Direction [] HORIZONTAL_DIRECTION = {
131
+ Direction .NORTH , Direction .WEST , Direction .EAST , Direction .SOUTH
132
+ };
133
+
112
134
private BlockState getAroundBlock () {
113
- if (level == null ) return Blocks .AIR .defaultBlockState ();
114
- BlockState blockState = level .getBlockState (getBlockPos ().south ());
115
- if (blockState .is (Blocks .LAVA ) && blockState .getValue (LiquidBlock .LEVEL ) > 0 )
135
+ if (level == null ) {
116
136
return Blocks .AIR .defaultBlockState ();
117
- for (Direction direction : new Direction [] {Direction .NORTH , Direction .WEST , Direction .EAST }) {
118
- BlockState checkBlockState = level .getBlockState (getBlockPos ().relative (direction ));
119
- if (!checkBlockState .is (blockState .getBlock ())) return Blocks .AIR .defaultBlockState ();
120
- if (checkBlockState .is (Blocks .LAVA ) && checkBlockState .getValue (LiquidBlock .LEVEL ) > 0 )
121
- return Blocks .AIR .defaultBlockState ();
122
137
}
123
- return blockState ;
138
+ List <BlockState > blockStates = Arrays .stream (HORIZONTAL_DIRECTION )
139
+ .map (direction -> level .getBlockState (getBlockPos ().relative (direction )))
140
+ .toList ();
141
+ BlockState firstState = blockStates .getFirst ();
142
+ long count = blockStates .stream ()
143
+ .filter (s -> s .is (firstState .getBlock ())
144
+ && (s .getFluidState ().isEmpty () || s .getFluidState ().isSource ()))
145
+ .count ();
146
+ return count == 4 ? firstState : Blocks .AIR .defaultBlockState ();
124
147
}
125
148
126
149
private boolean aroundHas (Block block ) {
127
- if (level == null ) return false ;
128
- for (Direction direction : new Direction [] {Direction .SOUTH , Direction .NORTH , Direction .WEST , Direction .EAST }) {
129
- if (level .getBlockState (getBlockPos ().relative (direction )).is (block )) return true ;
150
+ if (level == null ) {
151
+ return false ;
152
+ }
153
+ for (Direction direction : HORIZONTAL_DIRECTION ) {
154
+ if (level .getBlockState (getBlockPos ().relative (direction )).is (block )) {
155
+ return true ;
156
+ }
130
157
}
131
158
return false ;
132
159
}
0 commit comments