1515import work .crash .fallingalchemy .item .ConsumedItem ;
1616
1717import java .lang .ref .WeakReference ;
18- import java .util .ArrayList ;
19- import java .util .Collections ;
20- import java .util .Iterator ;
21- import java .util .List ;
18+ import java .util .*;
2219import java .util .stream .Collectors ;
2320
2421import static work .crash .fallingalchemy .Tags .MOD_ID ;
22+ import static work .crash .fallingalchemy .utils .Math .*;
2523
2624@ Mod .EventBusSubscriber (modid = MOD_ID )
2725public class FallingBlockHandler {
2826
2927 private static final List <WeakReference <EntityFallingBlock >> trackedBlocks = new ArrayList <>();
28+ private final Dictionary <EntityFallingBlock , BlockPos > blockPosition = new Hashtable <>();
3029
3130 @ SubscribeEvent
32- public static void onEntityJoinWorld (EntityJoinWorldEvent event ) {
31+ public void onEntityJoinWorld (EntityJoinWorldEvent event ) {
3332 if (event .getEntity () instanceof EntityFallingBlock block ) {
3433 Block blockType = block .getBlock ().getBlock ();
3534 if (FallingAlchemyTweaker .RULES .containsKey (blockType )) {
3635 trackedBlocks .add (new WeakReference <>(block ));
36+ blockPosition .put (block , block .getPosition ());
3737 }
3838 }
3939 }
4040
4141 @ SubscribeEvent
42- public static void onWorldTick (TickEvent .WorldTickEvent event ) {
42+ public void onWorldTick (TickEvent .WorldTickEvent event ) {
4343 if (event .phase == TickEvent .Phase .END && !event .world .isRemote ) {
4444 Iterator <WeakReference <EntityFallingBlock >> iterator = trackedBlocks .iterator ();
4545
@@ -48,15 +48,15 @@ public static void onWorldTick(TickEvent.WorldTickEvent event) {
4848 if (block == null || block .isDead ) {
4949 BlockPos pos = new BlockPos (block .posX , block .posY , block .posZ );
5050 if (event .world .getBlockState (pos ).getBlock () == block .getBlock ().getBlock ()) {
51- processConversion (event .world , pos , block .getBlock ().getBlock ());
51+ processConversion (event .world , blockPosition . get ( block ), pos , block .getBlock ().getBlock ());
5252 }
5353 iterator .remove ();
5454 }
5555 }
5656 }
5757 }
5858
59- private static void processConversion (World world , BlockPos pos , Block triggerBlock ) {
59+ private void processConversion (World world , BlockPos oriPos , BlockPos pos , Block triggerBlock ) {
6060 List <FallingAlchemyTweaker .ConversionRule > rules = FallingAlchemyTweaker .RULES .getOrDefault (triggerBlock , Collections .emptyList ());
6161
6262 List <FallingAlchemyTweaker .ConversionRule > sortedRules = rules .stream ().sorted (FallingAlchemyTweaker .ConversionRule ::compareTo ).collect (Collectors .toList ());
@@ -84,6 +84,15 @@ private static void processConversion(World world, BlockPos pos, Block triggerBl
8484 }
8585
8686 if (multiplier <= 0 ) continue ;
87+ // 避免无消耗物时multiplier异常
88+ if (rule .consumedItems .isEmpty ()) {
89+ multiplier = 1 ;
90+ }
91+
92+ // 计算位移是否满足要求
93+ if (positionDistance (oriPos , pos ) < rule .displacement ) {
94+ continue ;
95+ }
8796
8897 // 扣除每个消耗品
8998 for (ConsumedItem consumed : rule .consumedItems ) {
@@ -116,14 +125,19 @@ private static void processConversion(World world, BlockPos pos, Block triggerBl
116125 int finalMultiplier = multiplier ;
117126 rule .outputs .forEach (output -> {
118127 ItemStack spawnedStack = output .copy ();
119- spawnedStack .setCount (spawnedStack .getCount () * finalMultiplier );
120-
128+ //
129+ if (rule .displacement > 0 && positionDistance (oriPos , pos ) > rule .displacement && rule .additionalProducts ) {
130+ spawnedStack .setCount ((int ) ((positionDistance (oriPos , pos ) - rule .displacement ) + (spawnedStack .getCount () * finalMultiplier )));
131+ } else {
132+ spawnedStack .setCount (spawnedStack .getCount () * finalMultiplier );
133+ }
121134 EntityItem newItem = new EntityItem (world , pos .getX () + 0.5 , pos .getY () + 0.2 , pos .getZ () + 0.5 , spawnedStack );
122135 newItem .setDefaultPickupDelay ();
123136 newItem .motionY = 0.1 ;
124137 world .spawnEntity (newItem );
125138 });
126139 rule .playSuccessSound (world , pos );
140+ // 是否消耗方块
127141 if (world .rand .nextFloat () >= rule .keepBlockChance ) {
128142 world .setBlockToAir (pos );
129143 }
0 commit comments