Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 77 lines 3.1 kB view raw
1package net.lerariemann.infinity.block.custom; 2 3import java.util.Random; 4import net.minecraft.core.BlockPos; 5//? if >1.21 { 6import net.minecraft.core.component.DataComponents; 7import net.minecraft.server.network.Filterable; 8import net.minecraft.world.item.component.WrittenBookContent; 9import net.minecraft.network.chat.Component; 10import net.minecraft.world.level.block.state.BlockBehaviour; 11import java.util.ArrayList; 12import java.util.List; 13//?} else { 14/*import net.minecraft.nbt.CompoundTag; 15import net.minecraft.nbt.ListTag; 16import net.minecraft.nbt.StringTag; 17*///?} 18import net.minecraft.world.InteractionHand; 19import net.minecraft.world.InteractionResult; 20import net.minecraft.world.entity.player.Player; 21import net.minecraft.world.item.ItemStack; 22import net.minecraft.world.item.Items; 23import net.minecraft.world.level.Level; 24import net.minecraft.world.level.block.Block; 25import net.minecraft.world.level.block.state.BlockState; 26import net.minecraft.world.phys.BlockHitResult; 27 28public class BookBoxBlock extends Block { 29 public BookBoxBlock(Properties settings) { 30 super(settings); 31 } 32 33 public String title(BlockPos pos) { 34 return pos.getX() + "." + pos.getY() + "." + pos.getZ(); 35 } 36 37 public String text(BlockPos pos) { 38 Random r = new Random(pos.getX() + 30000000L*(pos.getZ() + 30000000L*pos.getY())); 39 String s = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890.^&*№+-_~@$%()!?"; 40 StringBuilder res = new StringBuilder(); 41 for (int j = 0; j<256; j++) { 42 res.append(s.charAt(r.nextInt(s.length()))); 43 } 44 return res.toString(); 45 } 46 47 //? if <1.21 { 48 /*@Override 49 *///?} 50 public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) { 51 return useWithoutItem(blockState, level, blockPos, player, blockHitResult); 52 } 53 54 //? if >1.21 { 55 @Override 56 //?} 57 public InteractionResult useWithoutItem(BlockState state, Level world, BlockPos pos, Player player, BlockHitResult hit) { 58 if (!world.isClientSide()) { 59 ItemStack itemStack1 = Items.WRITTEN_BOOK.getDefaultInstance(); 60 //? if >1.21 { 61 List<Filterable<Component>> pages = new ArrayList<>(); 62 pages.add(Filterable.passThrough(Component.nullToEmpty(text(pos)))); 63 WrittenBookContent component = new WrittenBookContent(Filterable.passThrough(title(pos)), "§kLeraRiemann", 3, pages, false); 64 itemStack1.set(DataComponents.WRITTEN_BOOK_CONTENT, component); 65 //?} else { 66 /*CompoundTag compound = itemStack1.getOrCreateTag(); 67 compound.putString("author", "§kLeraRiemann"); 68 ListTag lst = new ListTag(); 69 lst.add(StringTag.valueOf(text(pos))); 70 compound.put("pages", lst); 71 compound.putString("title", title(pos)); 72 *///?} 73 player.getInventory().add(itemStack1); 74 } 75 return InteractionResult.SUCCESS; 76 } 77}