Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 183 lines 7.9 kB view raw
1package net.lerariemann.infinity.block.custom; 2 3import net.lerariemann.infinity.block.entity.BiomeBottleBlockEntity; 4import net.lerariemann.infinity.registry.core.ModBlocks; 5import net.lerariemann.infinity.registry.var.ModCriteria; 6import net.lerariemann.infinity.util.VersionMethods; 7import net.lerariemann.infinity.util.var.BishopBattle; 8import net.minecraft.core.BlockPos; 9//? if >1.21 { 10import net.minecraft.core.component.DataComponentPatch; 11import net.minecraft.core.component.DataComponents; 12import net.minecraft.world.item.component.BlockItemStateProperties; 13import net.minecraft.world.item.component.CustomModelData; 14//?} 15import net.minecraft.nbt.CompoundTag; 16import net.minecraft.server.level.ServerLevel; 17import net.minecraft.server.level.ServerPlayer; 18import net.minecraft.sounds.SoundEvents; 19import net.minecraft.sounds.SoundSource; 20import net.minecraft.world.InteractionHand; 21import net.minecraft.world.InteractionResult; 22import net.minecraft.world.entity.player.Player; 23import net.minecraft.world.item.DyeItem; 24import net.minecraft.world.item.ItemStack; 25import net.minecraft.world.item.Items; 26import net.minecraft.world.level.BlockGetter; 27import net.minecraft.world.level.Level; 28import net.minecraft.world.level.LevelReader; 29import net.minecraft.world.level.block.Block; 30import net.minecraft.world.level.block.state.BlockState; 31import net.minecraft.world.level.block.state.StateDefinition; 32import net.minecraft.world.level.block.state.properties.BooleanProperty; 33import net.minecraft.world.level.block.state.properties.IntegerProperty; 34import net.minecraft.world.phys.BlockHitResult; 35import net.minecraft.world.phys.shapes.CollisionContext; 36import net.minecraft.world.phys.shapes.Shapes; 37import net.minecraft.world.phys.shapes.VoxelShape; 38import java.util.Map; 39 40public class AltarBlock extends Block { 41 public static final int numColors = 13; 42 public static final BooleanProperty FLOWER = BooleanProperty.create("flower"); 43 public static final IntegerProperty COLOR = IntegerProperty.create("color", 0, numColors - 1); 44 public static final VoxelShape BASE_SHAPE = Block.box(1.5, 0, 1.5, 14.5, 14, 14.5); 45 public static final VoxelShape TOP_SHAPE = Block.box(0, 14, 0, 16, 16, 16); 46 public static final VoxelShape LEG1 = Block.box(0, 0, 0, 3, 3, 3); 47 public static final VoxelShape LEG2 = Block.box(0, 0, 13, 3, 3, 16); 48 public static final VoxelShape LEG3 = Block.box(13, 0, 0, 16, 3, 3); 49 public static final VoxelShape LEG4 = Block.box(13, 0, 13, 16, 3, 16); 50 public static final VoxelShape SHAPE = Shapes.or(BASE_SHAPE, TOP_SHAPE, LEG1, LEG2, LEG3, LEG4); 51 52 public AltarBlock(Properties settings) { 53 super(settings); 54 this.registerDefaultState(defaultBlockState().setValue(COLOR, 0).setValue(FLOWER, false)); 55 } 56 57 @Override 58 public VoxelShape getCollisionShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { 59 return SHAPE; 60 } 61 @Override 62 public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) { 63 return SHAPE; 64 } 65 66 @Override 67 protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { 68 builder.add(COLOR); 69 builder.add(FLOWER); 70 } 71 72 public static void setColor(Level world, BlockPos pos, BlockState state, int i) { 73 world.setBlockAndUpdate(pos, state.setValue(COLOR, i)); 74 } 75 76 //? if >1.21 { 77 public static DataComponentPatch toComponentChanges(BlockState state) { 78 int color = state.getValue(AltarBlock.COLOR); 79 boolean flower = state.getValue(AltarBlock.FLOWER); 80 if (color > 0 || flower) return DataComponentPatch.builder().set(DataComponents.BLOCK_STATE, 81 new BlockItemStateProperties(Map.of()) 82 .with(AltarBlock.COLOR, color) 83 .with(AltarBlock.FLOWER, flower)) 84 .set(DataComponents.CUSTOM_MODEL_DATA, VersionMethods.getColoredModel(color + 85 (flower ? AltarBlock.numColors : 0))) 86 .build(); 87 return DataComponentPatch.EMPTY; 88 } 89 //?} else { 90 /*public static CompoundTag toComponentChanges(BlockState state) { 91 CompoundTag compoundTag = new CompoundTag(); 92 int color = state.getValue(AltarBlock.COLOR); 93 boolean flower = state.getValue(AltarBlock.FLOWER); 94 if (color > 0 || flower) { 95 compoundTag.putInt("CustomModelData", color + 96 (flower ? AltarBlock.numColors : 0)); 97 compoundTag.putInt(AltarBlock.COLOR.getName(), color); 98 compoundTag.putBoolean(AltarBlock.FLOWER.getName(), flower); 99 } 100 return compoundTag; 101 } 102 *///?} 103 104 @Override 105 public ItemStack getCloneItemStack( 106 //? if >1.21 { 107 LevelReader 108 //?} else { 109 /*BlockGetter 110 *///?} 111 world, BlockPos pos, BlockState state 112 //? if >1.21.2 113 , boolean bl 114 ) { 115 ItemStack res = super.getCloneItemStack(world, pos, state 116 //? if >1.21.2 117 , bl 118 ); 119 //? if >1.21 { 120 res.applyComponentsAndValidate(toComponentChanges(state)); 121 //?} else { 122 /*res.setTag(toComponentChanges(state)); 123 *///?} 124 return res; 125 } 126 127 public void testRituals(ServerLevel world, BlockPos pos, ServerPlayer player) { 128 //biome spreading 129 if (world.getBlockEntity(pos.above()) instanceof BiomeBottleBlockEntity bbbe) { 130 ModCriteria.BIOME_BOTTLE.get().trigger(player, bbbe); 131 bbbe.startTicking(); 132 } 133 } 134 135 @Override 136 //? if >1.21 { 137 public InteractionResult useWithoutItem(BlockState state, Level world, BlockPos pos, Player player, BlockHitResult hit) { 138 //?} else { 139 /*public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { 140 *///?} 141 ItemStack itemStack = player.getItemInHand(InteractionHand.MAIN_HAND); 142 //rituals 143 if (world instanceof ServerLevel serverWorld) { 144 if (itemStack.isEmpty()) testRituals(serverWorld, pos, (ServerPlayer)player); 145 } 146 //coloration 147 if (itemStack.getItem() instanceof DyeItem) { 148 int i = getColor(itemStack, state); 149 if (i>=0 && state.getValue(COLOR) != i) { 150 setColor(world, pos, state, i); 151 world.playSound(null, pos, SoundEvents.DYE_USE, SoundSource.BLOCKS, 1f, 1f); 152 } 153 return InteractionResult.SUCCESS; 154 } 155 if (itemStack.is(Items.SUNFLOWER)) { 156 world.setBlockAndUpdate(pos, state.setValue(FLOWER, !state.getValue(FLOWER))); 157 world.playSound(null, pos, SoundEvents.AZALEA_LEAVES_PLACE, SoundSource.BLOCKS, 1f, 1f); 158 return InteractionResult.SUCCESS; 159 } 160 //? if >1.21 { 161 return super.useWithoutItem(state, world, pos, player, hit); 162 //?} else { 163 /*return super.use(state, world, pos, player, hand, hit); 164 *///?} 165 } 166 167 public static int getColor(ItemStack itemStack, BlockState oldState) { 168 int i = -1; 169 if (itemStack.is(Items.RED_DYE)) i = 1; 170 if (itemStack.is(Items.ORANGE_DYE)) i = 2; 171 if (itemStack.is(Items.YELLOW_DYE)) i = 3; 172 if (itemStack.is(Items.LIME_DYE)) i = oldState.getValue(COLOR) == 6 ? 5 : 4; 173 if (itemStack.is(Items.GREEN_DYE)) i = oldState.getValue(COLOR) == 4 ? 5 : 6; 174 if (itemStack.is(Items.LIGHT_BLUE_DYE)) i = 7; 175 if (itemStack.is(Items.CYAN_DYE)) i = 8; 176 if (itemStack.is(Items.BLUE_DYE)) i = 9; 177 if (itemStack.is(Items.PURPLE_DYE)) i = 10; 178 if (itemStack.is(Items.MAGENTA_DYE)) i = 11; 179 if (itemStack.is(Items.PINK_DYE)) i = 12; 180 if (itemStack.is(Items.GRAY_DYE)) i = 0; 181 return i; 182 } 183}