| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.server.level.ServerPlayer;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.tags.EnchantmentTags;
|
| 8 | import net.minecraft.util.RandomSource;
|
| 9 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 10 | import net.minecraft.world.item.ItemStack;
|
| 11 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 12 | import net.minecraft.world.level.Level;
|
| 13 | import net.minecraft.world.level.LightLayer;
|
| 14 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 15 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public class IceBlock extends HalfTransparentBlock {
|
| 21 | public IceBlock(final BlockBehaviour.Properties properties) {
|
| 22 | super(properties);
|
| 23 | }
|
| 24 |
|
| 25 | public static BlockState meltsInto() {
|
| 26 | return Blocks.WATER.defaultBlockState();
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public void playerDestroy(
|
| 31 | final ServerLevel level,
|
| 32 | final ServerPlayer player,
|
| 33 | final BlockPos pos,
|
| 34 | final BlockState state,
|
| 35 | final @Nullable BlockEntity blockEntity,
|
| 36 | final ItemStack destroyedWith
|
| 37 | ) {
|
| 38 | super.playerDestroy(level, player, pos, state, blockEntity, destroyedWith);
|
| 39 | if (!EnchantmentHelper.hasTag(destroyedWith, EnchantmentTags.PREVENTS_ICE_MELTING)) {
|
| 40 | if (level.environmentAttributes().getValue(EnvironmentAttributes.WATER_EVAPORATES, pos)) {
|
| 41 | level.removeBlock(pos, false);
|
| 42 | return;
|
| 43 | }
|
| 44 |
|
| 45 | BlockState belowState = level.getBlockState(pos.below());
|
| 46 | if (belowState.is(BlockTags.ICE_MELTS_WHEN_DESTROYED_ABOVE) || belowState.liquid()) {
|
| 47 | level.setBlockAndUpdate(pos, meltsInto());
|
| 48 | }
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
|
| 54 | if (level.getBrightness(LightLayer.BLOCK, pos) > 11 - state.getLightDampening()) {
|
| 55 | this.melt(state, level, pos);
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | protected void melt(final BlockState state, final Level level, final BlockPos pos) {
|
| 60 | if (level.environmentAttributes().getValue(EnvironmentAttributes.WATER_EVAPORATES, pos)) {
|
| 61 | level.removeBlock(pos, false);
|
| 62 | } else {
|
| 63 | level.setBlockAndUpdate(pos, meltsInto());
|
| 64 | level.neighborChanged(pos, meltsInto().getBlock(), null);
|
| 65 | level.gameEvent(GameEvent.BLOCK_DESTROY, pos, GameEvent.Context.of(state));
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 |
|