Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 101 lines 3.5 kB view raw
1package net.lerariemann.infinity.dimensions; 2 3import net.lerariemann.infinity.InfinityMod; 4import net.lerariemann.infinity.util.config.ConfigManager; 5import net.lerariemann.infinity.util.core.ConfigType; 6import net.lerariemann.infinity.util.core.NbtUtils; 7import net.minecraft.nbt.CompoundTag; 8import org.apache.commons.io.FileUtils; 9 10import java.io.File; 11import java.io.IOException; 12import java.nio.charset.StandardCharsets; 13import java.nio.file.Files; 14import java.nio.file.Path; 15import java.util.*; 16import java.util.stream.Stream; 17 18import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; 19 20public class RandomText extends RandomStructure { 21 public static List<Path> mod_resources; 22 public static void walkPaths() { 23 mod_resources = new ArrayList<>(); 24 try (Stream<Path> files = Files.walk(InfinityMod.commonConfigPathInJar)) { 25 files.filter(p -> p.toString().endsWith(".json")).forEach(p -> mod_resources.add(p)); 26 } catch (IOException e) { 27 throw new RuntimeException(e); 28 } 29 } 30 31 RandomText(int i, RandomBiome b) { 32 super(i, b); 33 } 34 35 @Override 36 void addData() { 37 RandomDimension daddy = parent.parent; 38 type = "infinity:text"; 39 name = "text_" + id; 40 data = new CompoundTag(); 41 data.putString("type", "infinity:text"); 42 data.putString("step", "surface_structures"); 43 data.put("spawn_overrides", new CompoundTag()); 44 data.putString("biomes", parent.fullname); 45 data.put("block", daddy.PROVIDER.randomBlockProvider(random, ConfigType.FULL_BLOCKS_WG)); 46 data.put("y", NbtUtils.randomHeightProvider(random, 47 daddy.sea_level, daddy.min_y+daddy.height, 48 true, true)); 49 data.putString("text", genText(random)); 50 } 51 52 public static String genText(Random random) { 53 try { 54 return genTextFromModResources(random); 55 } 56 catch (Exception e) { 57 return genTextRandomly(random); 58 } 59 } 60 61 static String genTextFromModResources(Random random) throws IOException { 62 Path tempfile = ConfigManager.tempFile; 63 Path p = mod_resources.get(random.nextInt(mod_resources.size())); 64 Files.copy(p, tempfile, REPLACE_EXISTING); 65 return genTextFromFile(random, tempfile.toFile()); 66 } 67 68 static String genTextFromFile(Random random, File f) throws IOException { 69 try { 70 return select(random, FileUtils.readFileToString(f, StandardCharsets.UTF_8)); 71 } 72 catch (Exception e) { 73 throw new IOException(); 74 } 75 } 76 77 static String select(Random random, String str) { 78 String[] lst = str.split("\n"); 79 int i2 = random.nextInt(1, 16); 80 if (lst.length <= i2) return str; 81 int i = random.nextInt(0, lst.length - i2); 82 int j; 83 StringBuilder res = new StringBuilder(); 84 res.append(lst[i]); 85 for (j = 1; j<i2; j++) { 86 res.append("$n").append(lst[i+j]); 87 } 88 return res.toString(); 89 } 90 91 public static String genTextRandomly(Random random) { 92 return genTextRandomly(random, 64); 93 } 94 95 public static String genTextRandomly(Random random, int bound) { 96 String s = "1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"; 97 StringBuilder res = new StringBuilder(); 98 for (int j = 0; j<bound; j++) res.append(s.charAt(random.nextInt(s.length()))); 99 return res.toString(); 100 } 101}