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