Files
src
build.gradle
gradle.properties
settings.gradle
Code
19 575
src/main/java/org/infiniteuniverse/InfiniteUniverse.java
package org.infiniteuniverse;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.LongArgumentType;
import java.util.Locale;
import java.util.Set;
import java.util.SplittableRandom;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.commands.Commands;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.permissions.Permissions;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
public final class InfiniteUniverse implements ModInitializer {
private static final ResourceKey<Level> SPACE = ResourceKey.create(Registries.DIMENSION,
Identifier.fromNamespaceAndPath("infinite_universe", "space"));
@Override
public void onInitialize() {
SpaceTransition.initialize();
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) ->
dispatcher.register(Commands.literal("universe")
.then(Commands.literal("podroz")
.requires(source -> source.permissions().hasPermission(Permissions.COMMANDS_MODERATOR))
.then(Commands.argument("galaktyka", LongArgumentType.longArg())
.then(Commands.argument("uklad", LongArgumentType.longArg())
.then(Commands.argument("planeta", IntegerArgumentType.integer(1, 12))
.executes(context -> {
var source = context.getSource();
var player = source.getPlayerOrException();
long galaxy = LongArgumentType.getLong(context, "galaktyka");
long systemIndex = LongArgumentType.getLong(context, "uklad");
int planetIndex = IntegerArgumentType.getInteger(context, "planeta") - 1;
var system = UniverseGenerator.generate(source.getServer().overworld().getSeed(), galaxy, systemIndex);
if (planetIndex >= system.planets().size()) {
source.sendFailure(Component.literal("W tym układzie nie ma planety o takim numerze."));
return 0;
}
ServerLevel space = source.getServer().getLevel(SPACE);
if (space == null) {
source.sendFailure(Component.literal("Wymiar kosmosu jest niedostępny. Utwórz nowy świat z modem."));
return 0;
}
int centerX = systemCoordinate(galaxy, systemIndex, true);
int z = systemCoordinate(galaxy, systemIndex, false);
int x = centerX + (planetIndex + 1) * 32;
var planet = system.planets().get(planetIndex);
int starRadius = switch (system.star().type()) {
case "niebieska gwiazda" -> 19;
case "żółta gwiazda" -> 18;
case "czerwony karzeł" -> 16;
default -> 15;
};
DyeColor starColor = switch (system.star().type()) {
case "czerwony karzeł" -> DyeColor.RED;
case "żółta gwiazda" -> DyeColor.YELLOW;
default -> DyeColor.BLUE;
};
createStar(space, centerX, 100, z, starRadius, starColor);
for (int i = 0; i < system.planets().size(); i++) {
var body = system.planets().get(i);
Block surface = switch (body.type()) {
case "lodowa", "oceaniczna" -> Blocks.ICE;
case "pustynna" -> Blocks.SANDSTONE;
case "wulkaniczna" -> Blocks.BASALT;
case "leśna" -> Blocks.GRASS_BLOCK;
case "gazowy olbrzym" -> Blocks.SNOW_BLOCK;
case "martwa" -> Blocks.END_STONE;
default -> Blocks.STONE;
};
createBody(space, centerX + (i + 1) * 32, 100, z, planetRadius(body), surface);
}
var asteroids = new SplittableRandom(source.getServer().overworld().getSeed()
^ Long.rotateLeft(galaxy, 32) ^ systemIndex ^ 0x61737465726f6964L);
for (int i = 0; i < 12; i++) {
double angle = asteroids.nextDouble(Math.PI * 2);
int distance = asteroids.nextInt(48, 220);
int asteroidX = centerX + (int) (Math.cos(angle) * distance);
int asteroidZ = z + (int) (Math.sin(angle) * distance);
createBody(space, asteroidX, asteroids.nextInt(135, 155), asteroidZ,
asteroids.nextInt(1, 7), Blocks.DEEPSLATE);
}
if (!player.teleportTo(space, x + 0.5, 112 + planetRadius(planet), z + 0.5, Set.of(), player.getYRot(), player.getXRot(), false)) {
source.sendFailure(Component.literal("Nie udało się przenieść gracza."));
return 0;
}
SpaceTransition.setActiveSystem(player, galaxy, systemIndex);
source.sendSuccess(() -> Component.literal("Dotarto do miniatury planety " + planet.name() + ". Cały układ jest dostępny do eksploracji; w trybie kreatywnym możesz latać między planetami. Powrót: /universe powrot"), false);
return 1;
})))))
.then(Commands.literal("powrot")
.requires(source -> source.permissions().hasPermission(Permissions.COMMANDS_MODERATOR))
.executes(context -> {
var source = context.getSource();
var player = source.getPlayerOrException();
var overworld = source.getServer().overworld();
var spawn = overworld.getRespawnData();
var pos = spawn.pos();
if (!player.teleportTo(overworld, pos.getX() + 0.5, pos.getY() + 1, pos.getZ() + 0.5,
Set.of(), spawn.yaw(), spawn.pitch(), false)) {
source.sendFailure(Component.literal("Nie udało się wrócić do świata podstawowego."));
return 0;
}
source.sendSuccess(() -> Component.literal("Powrót do punktu odrodzenia świata."), false);
return 1;
}))
.then(Commands.literal("system")
.then(Commands.argument("galaktyka", LongArgumentType.longArg())
.then(Commands.argument("uklad", LongArgumentType.longArg())
.executes(context -> {
var player = context.getSource().getPlayerOrException();
var system = UniverseGenerator.generate(context.getSource().getServer().overworld().getSeed(),
LongArgumentType.getLong(context, "galaktyka"),
LongArgumentType.getLong(context, "uklad"));
var star = system.star();
context.getSource().sendSuccess(() -> Component.literal(String.format(Locale.ROOT,
"Galaktyka %d, układ %d: %s, %d K, promień %.3f Słońca; planet: %d",
system.galaxy(), system.index(), star.type(), star.temperatureK(),
star.radiusSolar(), system.planets().size())), false);
for (int i = 0; i < system.planets().size(); i++) {
var planet = system.planets().get(i);
int number = i + 1;
context.getSource().sendSuccess(() -> Component.literal(String.format(Locale.ROOT,
"%d. %s — %s, orbita %.2f AU, księżyce: %d",
number, planet.name(), planet.type(), planet.orbitAu(), planet.moons())), false);
}
return 1;
})
.then(Commands.argument("planeta", IntegerArgumentType.integer(1, 12))
.executes(context -> {
var player = context.getSource().getPlayerOrException();
var system = UniverseGenerator.generate(context.getSource().getServer().overworld().getSeed(),
LongArgumentType.getLong(context, "galaktyka"),
LongArgumentType.getLong(context, "uklad"));
int index = IntegerArgumentType.getInteger(context, "planeta") - 1;
if (index >= system.planets().size()) {
context.getSource().sendFailure(Component.literal("W tym układzie nie ma planety o takim numerze."));
return 0;
}
var planet = system.planets().get(index);
context.getSource().sendSuccess(() -> Component.literal(String.format(Locale.ROOT,
"%s: %s; promień %.2f Ziemi; grawitacja %.2f g; atmosfera: %s; temperatura %d K; orbita %.2f AU; księżyce: %d",
planet.name(), planet.type(), planet.radiusEarth(), planet.gravityEarth(),
planet.atmosphere() ? "tak" : "nie", planet.temperatureK(),
planet.orbitAu(), planet.moons())), false);
return 1;
})))))));
}
static int planetRadius(UniverseGenerator.Planet planet) {
return Math.min(12, 5 + (int) Math.round(planet.radiusEarth() * 0.6));
}
static int systemCoordinate(long galaxy, long system, boolean xAxis) {
if (galaxy >= 0 && galaxy < 100 && system >= 0 && system < 100) {
return (int) (xAxis ? galaxy : system) * 512;
}
long value = galaxy ^ Long.rotateLeft(system, 29) ^ (xAxis ? 0x6a09e667f3bcc909L : 0xbb67ae8584caa73bL);
value = (value ^ (value >>> 30)) * 0xbf58476d1ce4e5b9L;
value = (value ^ (value >>> 27)) * 0x94d049bb133111ebL;
value ^= value >>> 31;
return (int) Math.floorMod(value, 100_000L) * 512 - 25_600_000;
}
static void createStar(ServerLevel level, int x, int y, int z, int radius, DyeColor color) {
BlockPos center = new BlockPos(x, y, z);
if (!level.getBlockState(center).isAir()) return;
Block glass = Blocks.STAINED_GLASS.pick(color);
for (int dx = -radius; dx <= radius; dx++) {
for (int dy = -radius; dy <= radius; dy++) {
for (int dz = -radius; dz <= radius; dz++) {
int distanceSquared = dx * dx + dy * dy + dz * dz;
if (distanceSquared > radius * radius) continue;
BlockPos pos = new BlockPos(x + dx, y + dy, z + dz);
if (level.getBlockState(pos).isAir()) {
Block material = distanceSquared >= (radius - 2) * (radius - 2) ? glass : Blocks.GLOWSTONE;
level.setBlock(pos, material.defaultBlockState(), Block.UPDATE_CLIENTS, 512);
}
}
}
}
}
static void createBody(ServerLevel level, int x, int y, int z, int radius, Block material) {
BlockPos center = new BlockPos(x, y, z);
boolean newBody = level.getBlockState(center).isAir();
if (!newBody) return;
for (int dx = -radius; dx <= radius; dx++) {
for (int dy = -radius; dy <= radius; dy++) {
for (int dz = -radius; dz <= radius; dz++) {
if (dx * dx + dy * dy + dz * dz > radius * radius) continue;