Inspired by 2020's April Fools' 20w14infinite Snapshot, this mod brings endless randomly generated dimensions into Minecraft.
at master 202 lines 7.7 kB view raw
1package net.lerariemann.infinity.options; 2 3import net.lerariemann.infinity.InfinityMod; 4import net.lerariemann.infinity.access.InfinityOptionsAccess; 5import net.lerariemann.infinity.util.VersionMethods; 6import net.lerariemann.infinity.util.core.CommonIO; 7import net.lerariemann.infinity.util.core.NbtUtils; 8import net.minecraft.client.Minecraft; 9import net.minecraft.nbt.CompoundTag; 10import net.minecraft.nbt.Tag; 11import net.minecraft.resources.ResourceLocation; 12import net.minecraft.server.MinecraftServer; 13import net.minecraft.util.RandomSource; 14import net.minecraft.world.level.Level; 15import net.minecraft.world.level.storage.LevelResource; 16import org.joml.Vector3f; 17 18import java.io.File; 19import java.util.function.Function; 20 21public class InfinityOptions { 22 public CompoundTag data; 23 public PitchShifter shifter; 24 public EffectGiver effect; 25 26 private final double mavity; 27 private final double timeScale; 28 private final boolean haunted; 29 30 public InfinityOptions(CompoundTag data) { 31 this.data = data; 32 this.shifter = PitchShifter.decode(NbtUtils.getCompound(data, "pitch_shift", new CompoundTag())); 33 this.effect = EffectGiver.of(NbtUtils.getCompound(data, "effect", new CompoundTag())); 34 this.mavity = NbtUtils.getDouble(data, "mavity", 1.0); 35 this.timeScale = NbtUtils.getDouble(data, "time_scale", 1.0); 36 this.haunted = NbtUtils.getBoolean(data, "haunted", false); 37 } 38 39 public CompoundTag data() { 40 return data; 41 } 42 43 public static InfinityOptions empty() { 44 return new InfinityOptions(new CompoundTag()); 45 } 46 47 public static CompoundTag readData(MinecraftServer server, ResourceLocation worldId) { 48 if (worldId.getNamespace().equals(InfinityMod.MOD_ID)) { 49 String name = worldId.getPath(); 50 File f = server.getWorldPath(LevelResource.DATAPACK_DIR).resolve(name + "/data/infinity/options.json").toFile(); 51 if (f.exists()) { 52 return CommonIO.read(f); 53 } 54 } 55 return new CompoundTag(); 56 } 57 public static InfinityOptions generate(MinecraftServer server, ResourceLocation worldId) { 58 return new InfinityOptions(readData(server, worldId)); 59 } 60 61 public static InfinityOptions access(Level world) { 62 return ((InfinityOptionsAccess)world).infinity$getOptions(); 63 } 64 65 public static InfinityOptions ofClient() { 66 return ofClient(Minecraft.getInstance()); 67 } 68 69 public static InfinityOptions ofClient(Minecraft client) { 70 return ((InfinityOptionsAccess)client).infinity$getOptions(); 71 } 72 73 public static InfinityOptions nullSafe(InfinityOptions options) { 74 return (options != null) ? options : InfinityOptions.empty(); 75 } 76 77 public boolean isEmpty() { 78 return data.isEmpty(); 79 } 80 81 public CompoundTag getShader() { 82 return NbtUtils.getCompound(data, "shader", new CompoundTag()); 83 } 84 85 public double getTimeScale() { 86 return timeScale; 87 } 88 public double getMavity() { 89 return mavity; 90 } 91 public Function<Float, Float> getSoundPitch() { 92 return shifter.applier(); 93 } 94 public boolean isHaunted() { 95 return haunted; 96 } 97 public int getHauntingTicks(RandomSource random) { 98 if (!isHaunted()) return -2; 99 if (data.contains("haunting_ticks")) return NbtUtils.getInt(data, "haunting_ticks"); 100 return random.nextIntBetweenInclusive(NbtUtils.getInt(data, "min_haunting_ticks", 20), NbtUtils.getInt(data, "max_haunting_ticks", 200)); 101 } 102 103 //sky - common 104 public String getSkyType() { 105 return NbtUtils.getString(data, "sky_type", "empty"); 106 } 107 public float getHorizonShadingRatio() { 108 return NbtUtils.getFloat(data, "horizon_shading_ratio", 1.0f); 109 } 110 public boolean endSkyLike() { 111 return NbtUtils.getBoolean(data, "end_sky_like", false); 112 } 113 public boolean hasDawn() { 114 return NbtUtils.getBoolean(data, "dawn", !getSkyType().equals("rainbow")); 115 } 116 117 //sun 118 public float getSolarSize() { 119 return NbtUtils.getFloat(data, "solar_size", 30.0f); 120 } 121 public float getSolarTilt() { 122 return NbtUtils.getFloat(data, "solar_tilt", -90.0f); 123 } 124 public Vector3f getSolarTint() { 125 int color = NbtUtils.getInt(data, "solar_tint",16777215); 126 return new Vector3f((float)(color >> 16 & 0xFF) / 255.0f, (float)(color >> 8 & 0xFF) / 255.0f, (float)(color & 0xFF) / 255.0f); 127 } 128 public ResourceLocation getSolarTexture() { 129 return VersionMethods.id(NbtUtils.getString(data, "solar_texture", "textures/environment/sun.png")); 130 } 131 132 //stars 133 public int getNumStars() { 134 return NbtUtils.getInt(data, "num_stars", 1500); 135 } 136 public float getStarSizeBase() { 137 return NbtUtils.getFloat(data, "star_size_base", 0.15f); 138 } 139 public float getStarSizeModifier() { 140 return NbtUtils.getFloat(data, "star_size_modifier", 0.1f); 141 } 142 public float getStellarTiltY() { 143 return NbtUtils.getFloat(data, "stellar_tilt_y", -90.0f); 144 } 145 public float getStellarTiltZ() { 146 return NbtUtils.getFloat(data, "stellar_tilt_z", 0.0f); 147 } 148 public float getStellarVelocity() { 149 return NbtUtils.getFloat(data, "stellar_velocity", 1.0f); 150 } 151 public float getDayStarBrightness() { 152 return NbtUtils.getFloat(data, "star_brightness_day", 0.0f); 153 } 154 public float getNightStarBrightness() { 155 return NbtUtils.getFloat(data, "star_brightness_night", 0.5f); 156 } 157 public Vector3f getStellarColor() { 158 int color = NbtUtils.getInt(data, "stellar_color",16777215); 159 return new Vector3f((float)(color >> 16 & 0xFF) / 255.0f, (float)(color >> 8 & 0xFF) / 255.0f, (float)(color & 0xFF) / 255.0f); 160 } 161 162 //moons 163 public int getNumMoons() { 164 return data.contains("moons") ? NbtUtils.getList(data,"moons", Tag.TAG_COMPOUND).size() : 1; 165 } 166 public boolean lunarTest(String key, int i) { 167 return data.contains("moons") && ((CompoundTag)(NbtUtils.getList(data, "moons", Tag.TAG_COMPOUND).get(i))).contains(key); 168 } 169 public float fullLunarTest(String key, int i, float def) { 170 return lunarTest(key, i) ? ((CompoundTag)(NbtUtils.getList(data, "moons", Tag.TAG_COMPOUND).get(i))).getFloat(key) 171 //? if >1.21.5 172 .orElse(def) 173 : def; 174 } 175 public float getLunarSize(int i) { 176 return fullLunarTest("lunar_size", i, 20.0f); 177 } 178 public float getLunarTiltY(int i) { 179 return fullLunarTest("lunar_tilt_y", i, -90.0f); 180 } 181 public float getLunarTiltZ(int i) { 182 return fullLunarTest("lunar_tilt_z", i, 0.0f); 183 } 184 public Vector3f getLunarTint(int i) { 185 int color = lunarTest("lunar_tint", i) ? ((CompoundTag)(NbtUtils.getList(data, "moons", Tag.TAG_COMPOUND).get(i))).getInt("lunar_tint") 186 //? if >1.21.5 187 .orElse(16777215) 188 : 16777215; 189 return new Vector3f((float)(color >> 16 & 0xFF) / 255.0f, (float)(color >> 8 & 0xFF) / 255.0f, (float)(color & 0xFF) / 255.0f); 190 } 191 public ResourceLocation getLunarTexture(int i) { 192 if (lunarTest("lunar_texture", i)) 193 return VersionMethods.id(NbtUtils.getString(((CompoundTag) (NbtUtils.getList(data, "moons", Tag.TAG_COMPOUND).get(i))), "lunar_texture")); 194 return VersionMethods.id("textures/environment/moon_phases.png"); 195 } 196 public float getLunarVelocity(int i) { 197 return fullLunarTest("lunar_velocity", i, 1.0f); 198 } 199 public float getLunarOffset(int i) { 200 return fullLunarTest("lunar_offset", i, 0.0f); 201 } 202}