diff --git a/src/main/java/dev/dubhe/anvilcraft/mixin/EndPortalBlockMixin.java b/src/main/java/dev/dubhe/anvilcraft/mixin/EndPortalBlockMixin.java index 871f62f66..52a6f3190 100644 --- a/src/main/java/dev/dubhe/anvilcraft/mixin/EndPortalBlockMixin.java +++ b/src/main/java/dev/dubhe/anvilcraft/mixin/EndPortalBlockMixin.java @@ -3,12 +3,12 @@ import dev.dubhe.anvilcraft.init.ModBlockTags; import dev.dubhe.anvilcraft.init.ModBlocks; +import dev.dubhe.anvilcraft.util.SpectralAnvilConversionUtil; import net.minecraft.core.BlockPos; import net.minecraft.tags.BlockTags; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.item.FallingBlockEntity; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.EndPortalBlock; import net.minecraft.world.level.block.Portal; import net.minecraft.world.level.block.state.BlockState; @@ -18,6 +18,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + @Mixin(EndPortalBlock.class) abstract class EndPortalBlockMixin { @Inject( @@ -36,24 +37,11 @@ private void fallBlockEntityInside( && !fallingBlockEntity.blockState.is(ModBlockTags.END_PORTAL_UNABLE_CHANGE) ) { BlockState newState = ModBlocks.END_DUST.getDefaultState(); - anvil: if (fallingBlockEntity.blockState.is(BlockTags.ANVIL)) { double rand = pLevel.random.nextDouble(); - if (fallingBlockEntity.blockState.is(Blocks.DAMAGED_ANVIL) && rand >= 0.01) { - break anvil; - } else if (fallingBlockEntity.blockState.is(Blocks.CHIPPED_ANVIL) && rand >= 0.02) { - break anvil; - } else if (fallingBlockEntity.blockState.is(Blocks.ANVIL) && rand >= 0.03) { - break anvil; - } else if (fallingBlockEntity.blockState.is(ModBlocks.ROYAL_ANVIL) && rand >= 0.5) { - break anvil; - } else if (fallingBlockEntity.blockState.is(ModBlocks.EMBER_ANVIL)) { + if (rand < SpectralAnvilConversionUtil.chance(fallingBlockEntity.blockState.getBlock())) { newState = ModBlocks.SPECTRAL_ANVIL.getDefaultState(); - break anvil; - } else if (rand >= 0.03) { - break anvil; } - newState = ModBlocks.SPECTRAL_ANVIL.getDefaultState(); } fallingBlockEntity.blockState = newState; fallingBlockEntity.setAsInsidePortal((Portal) this, pPos); diff --git a/src/main/java/dev/dubhe/anvilcraft/util/SpectralAnvilConversionUtil.java b/src/main/java/dev/dubhe/anvilcraft/util/SpectralAnvilConversionUtil.java new file mode 100644 index 000000000..4e2c126a3 --- /dev/null +++ b/src/main/java/dev/dubhe/anvilcraft/util/SpectralAnvilConversionUtil.java @@ -0,0 +1,23 @@ +package dev.dubhe.anvilcraft.util; + +import dev.dubhe.anvilcraft.init.ModBlocks; +import it.unimi.dsi.fastutil.objects.Object2DoubleMap; +import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; + +public class SpectralAnvilConversionUtil { + private static final Object2DoubleMap SPECTRAL_ANVIL_CONVERSION_CHANCE = new Object2DoubleOpenHashMap<>(); + + static { + SPECTRAL_ANVIL_CONVERSION_CHANCE.put(Blocks.DAMAGED_ANVIL, 0.01); + SPECTRAL_ANVIL_CONVERSION_CHANCE.put(Blocks.CHIPPED_ANVIL, 0.02); + SPECTRAL_ANVIL_CONVERSION_CHANCE.put(Blocks.ANVIL, 0.03); + SPECTRAL_ANVIL_CONVERSION_CHANCE.put(ModBlocks.ROYAL_ANVIL.get(), 0.5); + SPECTRAL_ANVIL_CONVERSION_CHANCE.put(ModBlocks.EMBER_ANVIL.get(), 1.0); + } + + public static double chance(Block block) { + return SPECTRAL_ANVIL_CONVERSION_CHANCE.getOrDefault(block, 0.03); + } +}