Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 130 lines 5.1 kB view raw
1package net.lerariemann.infinity.block.custom; 2 3import com.mojang.serialization.MapCodec; 4import net.lerariemann.infinity.block.entity.ChromaticBlockEntity; 5import net.lerariemann.infinity.registry.core.ModBlocks; 6import net.lerariemann.infinity.registry.core.ModItems; 7import net.minecraft.core.BlockPos; 8import net.minecraft.core.Direction; 9import net.minecraft.sounds.SoundEvents; 10import net.minecraft.sounds.SoundSource; 11import net.minecraft.util.RandomSource; 12import net.minecraft.world.InteractionHand; 13import net.minecraft.world.InteractionResult; 14import net.minecraft.world.entity.player.Player; 15import net.minecraft.world.level.*; 16import net.minecraft.world.level.block.Block; 17import net.minecraft.world.level.block.Blocks; 18import net.minecraft.world.level.block.state.BlockState; 19import net.minecraft.world.level.block.state.StateDefinition; 20import net.minecraft.world.level.block.state.properties.IntegerProperty; 21import net.minecraft.world.phys.BlockHitResult; 22import net.minecraft.world.phys.shapes.CollisionContext; 23import net.minecraft.world.phys.shapes.VoxelShape; 24import org.jetbrains.annotations.Nullable; 25 26public class IridescentBlock extends Block { 27 public static int num_models = 24; 28 public static final IntegerProperty COLOR_OFFSET = IntegerProperty.create("color", 0, num_models - 1); 29 //? if >1.21 30 public static final MapCodec<IridescentBlock> CODEC = simpleCodec(IridescentBlock::new); 31 32 public IridescentBlock(Properties settings) { 33 super(settings); 34 this.registerDefaultState(defaultBlockState().setValue(COLOR_OFFSET, 0)); 35 } 36 37 //? if >1.21 { 38 @Override 39 public MapCodec<? extends IridescentBlock> codec() { 40 return CODEC; 41 } 42 //?} 43 44 @Override 45 protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { 46 super.createBlockStateDefinition(builder); 47 builder.add(COLOR_OFFSET); 48 } 49 50 @Nullable 51 public BlockState toStatic(BlockState state) { 52 return ModBlocks.CHROMATIC_WOOL.get().defaultBlockState(); 53 } 54 55 @Override 56 //? if <1.21 { 57 /*public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { 58 *///?} else { 59 protected InteractionResult useWithoutItem(BlockState state, Level world, BlockPos pos, Player player, BlockHitResult hit) { 60 //?} 61 if (player.getItemInHand(InteractionHand.MAIN_HAND).is(ModItems.STAR_OF_LANG.get())) { 62 BlockState state1 = toStatic(state); 63 if (state1 != null) { 64 world.setBlockAndUpdate(pos, state1); 65 if (world.getBlockEntity(pos) instanceof ChromaticBlockEntity cbe) { 66 cbe.setColor(state.getValue(COLOR_OFFSET)*(360 / num_models), 255, 255, null); 67 } 68 world.playSound(null, pos, SoundEvents.AMETHYST_BLOCK_BREAK, SoundSource.BLOCKS, 1f, 1f); 69 return InteractionResult.SUCCESS; 70 } 71 } 72 //? if <1.21 { 73 /*return super.use(state, world, pos, player, hand, hit); 74 *///?} else { 75 return super.useWithoutItem(state, world, pos, player, hit); 76 //?} 77 } 78 79 public static class Carpet extends IridescentBlock { 80 protected static final VoxelShape SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 1.0, 16.0); 81 82 public Carpet(Properties settings) { 83 super(settings); 84 } 85 86 @Override 87 //? if >1.21 { 88 protected 89 //?} else { 90 /*public 91 *///?} 92 VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { 93 return SHAPE; 94 } 95 96 @Override 97 //? if >1.21 { 98 protected 99 //?} else { 100 /*public 101 *///?} 102 //? if >1.21.2 { 103 BlockState updateShape(BlockState state, LevelReader world, ScheduledTickAccess scheduledTickAccess, BlockPos pos, Direction direction, BlockPos neighborPos, BlockState neighborState, RandomSource random) { 104 //?} else { 105 /*BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor world, BlockPos pos, BlockPos neighborPos) { 106 *///?} 107 if (!state.canSurvive(world, pos)) return Blocks.AIR.defaultBlockState(); 108 //? if >1.21.2 { 109 return super.updateShape(state, world, scheduledTickAccess, pos, direction, neighborPos, neighborState, random); 110 //?} else { 111 /*return super.updateShape(state, direction, neighborState, world, pos, neighborPos); 112 *///?} 113 } 114 115 @Override 116 //? if >1.21 { 117 protected 118 //?} else { 119 /*public 120 *///?} 121 boolean canSurvive(BlockState state, LevelReader world, BlockPos pos) { 122 return !world.isEmptyBlock(pos.below()); 123 } 124 125 @Override 126 public BlockState toStatic(BlockState state) { 127 return ModBlocks.CHROMATIC_CARPET.get().defaultBlockState(); 128 } 129 } 130}