Преглед изворни кода

World setup + base for the world system

____tbvns____ пре 5 месеци
родитељ
комит
b04922e89b

+ 5 - 0
src/client/java/xyz/tbvns/rogue_block/client/Rogue_blockClient.java

@@ -1,10 +1,15 @@
 package xyz.tbvns.rogue_block.client;
 
 import net.fabricmc.api.ClientModInitializer;
+import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
+import net.minecraft.client.gui.screen.world.CreateWorldScreen;
+import net.minecraft.world.gen.chunk.FlatChunkGenerator;
+import net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig;
 
 public class Rogue_blockClient implements ClientModInitializer {
 
     @Override
     public void onInitializeClient() {
+
     }
 }

+ 23 - 0
src/client/java/xyz/tbvns/rogue_block/client/mixin/MixinTitleScreen.java

@@ -0,0 +1,23 @@
+package xyz.tbvns.rogue_block.client.mixin;
+
+import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
+import net.minecraft.client.font.TextRenderer;
+import net.minecraft.client.gui.DrawContext;
+import net.minecraft.client.gui.LogoDrawer;
+import net.minecraft.client.gui.screen.SplashTextRenderer;
+import net.minecraft.client.gui.screen.TitleScreen;
+import org.jetbrains.annotations.Nullable;
+import org.spongepowered.asm.mixin.Final;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Shadow;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
+
+@Mixin(TitleScreen.class)
+public abstract class MixinTitleScreen {
+    @Inject(method = "render", at = @At(value = "TAIL"))
+    public void removeRealm(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
+
+    }
+}

+ 5 - 2
src/client/resources/rogue_block.client.mixins.json

@@ -1,11 +1,14 @@
 {
   "required": true,
   "minVersion": "0.8",
-  "package": "xyz.tbvns.rogue_block.mixin.client",
+  "package": "xyz.tbvns.rogue_block.client.mixin",
   "compatibilityLevel": "JAVA_21",
   "client": [
   ],
   "injectors": {
     "defaultRequire": 1
-  }
+  },
+  "mixins": [
+    "MixinTitleScreen"
+  ]
 }

+ 17 - 0
src/main/java/xyz/tbvns/rogue_block/Events/PlayerJoinEvents.java

@@ -0,0 +1,17 @@
+package xyz.tbvns.rogue_block.Events;
+
+import net.fabricmc.fabric.api.networking.v1.PacketSender;
+import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.network.ServerPlayNetworkHandler;
+import xyz.tbvns.rogue_block.Managers.WorldManager;
+import xyz.tbvns.rogue_block.Utils;
+
+public class PlayerJoinEvents implements ServerPlayConnectionEvents.Join {
+    @Override
+    public void onPlayReady(ServerPlayNetworkHandler serverPlayNetworkHandler, PacketSender packetSender, MinecraftServer minecraftServer) {
+
+        //Delay the tp to prevent issues
+        Utils.delay(1000, () -> WorldManager.teleportToLobbyWorld(serverPlayNetworkHandler.player));
+    }
+}

+ 24 - 0
src/main/java/xyz/tbvns/rogue_block/Events/PlayerStartButtonEvent.java

@@ -0,0 +1,24 @@
+package xyz.tbvns.rogue_block.Events;
+
+import net.fabricmc.fabric.api.event.player.UseBlockCallback;
+import net.minecraft.entity.player.PlayerEntity;
+import net.minecraft.util.ActionResult;
+import net.minecraft.util.Hand;
+import net.minecraft.util.hit.BlockHitResult;
+import net.minecraft.util.math.BlockPos;
+import net.minecraft.world.World;
+import xyz.tbvns.rogue_block.Managers.WorldManager;
+
+public class PlayerStartButtonEvent implements UseBlockCallback {
+    @Override
+    public ActionResult interact(PlayerEntity playerEntity, World world, Hand hand, BlockHitResult blockHitResult) {
+        if (blockHitResult.getBlockPos().equals(BlockPos.ORIGIN)) {
+            if (!world.isClient) {
+                WorldManager.createNewRunWorld(world.getServer());
+                WorldManager.teleportToRunWorld(world.getServer());
+            }
+            return ActionResult.PASS;
+        }
+        return ActionResult.PASS_TO_DEFAULT_BLOCK_ACTION;
+    }
+}

+ 12 - 0
src/main/java/xyz/tbvns/rogue_block/Events/ServerStartEvent.java

@@ -0,0 +1,12 @@
+package xyz.tbvns.rogue_block.Events;
+
+import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
+import net.minecraft.server.MinecraftServer;
+import xyz.tbvns.rogue_block.Managers.WorldManager;
+
+public class ServerStartEvent implements ServerLifecycleEvents.ServerStarted {
+    @Override
+    public void onServerStarted(MinecraftServer minecraftServer) {
+        WorldManager.createNewLobbyWorld(minecraftServer);
+    }
+}

+ 15 - 0
src/main/java/xyz/tbvns/rogue_block/Events/WorldLoadEvent.java

@@ -0,0 +1,15 @@
+package xyz.tbvns.rogue_block.Events;
+
+import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.world.ServerWorld;
+import xyz.tbvns.rogue_block.Managers.WorldManager;
+
+public class WorldLoadEvent implements ServerWorldEvents.Load{
+    @Override
+    public void onWorldLoad(MinecraftServer minecraftServer, ServerWorld serverWorld) {
+        if (serverWorld == minecraftServer.getOverworld()) {
+            serverWorld.setMobSpawnOptions(false);
+        }
+    }
+}

+ 167 - 0
src/main/java/xyz/tbvns/rogue_block/Managers/WorldManager.java

@@ -0,0 +1,167 @@
+package xyz.tbvns.rogue_block.Managers;
+
+import com.mojang.datafixers.util.Either;
+import net.fabricmc.fabric.api.util.TriState;
+import net.minecraft.block.Blocks;
+import net.minecraft.network.packet.s2c.play.PositionFlag;
+import net.minecraft.registry.RegistryEntryLookup;
+import net.minecraft.registry.RegistryKeys;
+import net.minecraft.registry.entry.RegistryEntry;
+import net.minecraft.registry.entry.RegistryEntryList;
+import net.minecraft.registry.entry.RegistryEntryOwner;
+import net.minecraft.registry.tag.TagKey;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.server.network.ServerPlayerEntity;
+import net.minecraft.server.world.ServerWorld;
+import net.minecraft.structure.StructureSet;
+import net.minecraft.util.math.BlockPos;
+import net.minecraft.world.Difficulty;
+import net.minecraft.world.GameRules;
+import net.minecraft.world.biome.Biome;
+import net.minecraft.world.biome.BiomeKeys;
+import net.minecraft.world.dimension.DimensionTypes;
+import net.minecraft.world.gen.chunk.FlatChunkGenerator;
+import net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig;
+import net.minecraft.world.gen.chunk.FlatChunkGeneratorLayer;
+import net.minecraft.world.gen.feature.PlacedFeature;
+import org.jetbrains.annotations.NotNull;
+import xyz.nucleoid.fantasy.Fantasy;
+import xyz.nucleoid.fantasy.RuntimeWorldConfig;
+import xyz.nucleoid.fantasy.RuntimeWorldHandle;
+
+import java.util.*;
+import java.util.stream.Stream;
+
+public class WorldManager {
+
+    static RuntimeWorldHandle runWorldHandle;
+    static RuntimeWorldHandle lobbyWorldHandle;
+
+    public static void createNewLobbyWorld(MinecraftServer server) {
+        RegistryEntryLookup<Biome> biomeLookup = server.getRegistryManager().getOrThrow(RegistryKeys.BIOME);
+        RegistryEntryLookup<StructureSet> structureSetLookup = server.getRegistryManager().getOrThrow(RegistryKeys.STRUCTURE_SET);
+        RegistryEntryLookup<PlacedFeature> featuresLookup = server.getRegistryManager().getOrThrow(RegistryKeys.PLACED_FEATURE);
+
+        FlatChunkGeneratorConfig config = FlatChunkGeneratorConfig.getDefaultConfig(biomeLookup, structureSetLookup, featuresLookup);
+        List<FlatChunkGeneratorLayer> layers = new ArrayList<>() {{
+            add(new FlatChunkGeneratorLayer(1, Blocks.BEDROCK)); //The order is reversed
+            add(new FlatChunkGeneratorLayer(20, Blocks.STONE));
+            add(new FlatChunkGeneratorLayer(7, Blocks.DIRT));
+            add(new FlatChunkGeneratorLayer(1, Blocks.GRASS_BLOCK));
+        }};
+        config = config.with(layers, Optional.of(new StructureSetRegistryEntryList()), biomeLookup.getOrThrow(BiomeKeys.PLAINS));
+
+        FlatChunkGenerator generator = new FlatChunkGenerator(config);
+
+        Fantasy fantasy = Fantasy.get(server);
+        RuntimeWorldConfig worldConfig = new RuntimeWorldConfig()
+                .setDimensionType(DimensionTypes.OVERWORLD)
+                .setDifficulty(Difficulty.HARD)
+                .setGameRule(GameRules.DO_DAYLIGHT_CYCLE, false)
+                .setGenerator(generator)
+                .setFlat(TriState.TRUE)
+                .setSeed(1234L);
+
+        lobbyWorldHandle = fantasy.openTemporaryWorld(worldConfig);
+
+        ServerWorld world = lobbyWorldHandle.asWorld();
+        world.setMobSpawnOptions(false);
+        world.setBlockState(BlockPos.ORIGIN, Blocks.ACACIA_BUTTON.getDefaultState());
+
+    }
+
+    public static void createNewRunWorld(MinecraftServer server) {
+        if (runWorldHandle != null) {
+            runWorldHandle.delete();
+        }
+
+        Fantasy fantasy = Fantasy.get(server);
+        RuntimeWorldConfig worldConfig = new RuntimeWorldConfig()
+                .setDimensionType(DimensionTypes.OVERWORLD)
+                .setDifficulty(Difficulty.HARD)
+                .setGameRule(GameRules.DO_DAYLIGHT_CYCLE, false)
+                .setGenerator(server.getOverworld().getChunkManager().getChunkGenerator())
+                .setSeed(new Random().nextLong());
+
+        runWorldHandle = fantasy.openTemporaryWorld(worldConfig);
+    }
+
+    public static void teleportToRunWorld(MinecraftServer server) {
+        for (ServerPlayerEntity entity : server.getPlayerManager().getPlayerList()) {
+            Random random = new Random();
+            int x = random.nextInt(-50, 50);
+            int z = random.nextInt(-50, 50);
+            entity.teleport(runWorldHandle.asWorld(), x, 255, z, PositionFlag.VALUES, 0, 0, true);
+        }
+    }
+
+    public static void teleportToLobbyWorld(MinecraftServer server) {
+        for (ServerPlayerEntity entity : server.getPlayerManager().getPlayerList()) {
+            Random random = new Random();
+            int x = random.nextInt(-3, 3);
+            int z = random.nextInt(-3, 3);
+            entity.teleport(lobbyWorldHandle.asWorld(), x, 255, z, PositionFlag.VALUES, 0, 0, true);
+        }
+    }
+
+    public static void teleportToLobbyWorld(ServerPlayerEntity entity) {
+        Random random = new Random();
+        int x = random.nextInt(-3, 3);
+        int z = random.nextInt(-3, 3);
+        entity.teleport(lobbyWorldHandle.asWorld(), x, 255, z, PositionFlag.VALUES, 0, 0, true);
+    }
+
+    //This is required to remove every structure
+    //You should fold this with your code editor
+    private static class StructureSetRegistryEntryList implements RegistryEntryList<StructureSet> {
+        @Override
+        public Stream<RegistryEntry<StructureSet>> stream() {
+            return Stream.empty();
+        }
+
+        @Override
+        public int size() {
+            return 0;
+        }
+
+        @Override
+        public boolean isBound() {
+            return false;
+        }
+
+        @Override
+        public Either<TagKey<StructureSet>, List<RegistryEntry<StructureSet>>> getStorage() {
+            return null;
+        }
+
+        @Override
+        public Optional<RegistryEntry<StructureSet>> getRandom(net.minecraft.util.math.random.Random random) {
+            return Optional.empty();
+        }
+
+        @Override
+        public RegistryEntry<StructureSet> get(int index) {
+            return null;
+        }
+
+        @Override
+        public boolean contains(RegistryEntry<StructureSet> entry) {
+            return false;
+        }
+
+        @Override
+        public boolean ownerEquals(RegistryEntryOwner<StructureSet> owner) {
+            return false;
+        }
+
+        @Override
+        public Optional<TagKey<StructureSet>> getTagKey() {
+            return Optional.empty();
+        }
+
+        @Override
+        public @NotNull Iterator<RegistryEntry<StructureSet>> iterator() {
+            return null;
+        }
+    }
+}

+ 9 - 0
src/main/java/xyz/tbvns/rogue_block/Rogue_block.java

@@ -1,10 +1,19 @@
 package xyz.tbvns.rogue_block;
 
 import net.fabricmc.api.ModInitializer;
+import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
+import net.fabricmc.fabric.api.event.player.UseBlockCallback;
+import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
+import xyz.tbvns.rogue_block.Events.PlayerJoinEvents;
+import xyz.tbvns.rogue_block.Events.PlayerStartButtonEvent;
+import xyz.tbvns.rogue_block.Events.ServerStartEvent;
 
 public class Rogue_block implements ModInitializer {
 
     @Override
     public void onInitialize() {
+        ServerPlayConnectionEvents.JOIN.register(new PlayerJoinEvents());
+        UseBlockCallback.EVENT.register(new PlayerStartButtonEvent());
+        ServerLifecycleEvents.SERVER_STARTED.register(new ServerStartEvent());
     }
 }

+ 14 - 0
src/main/java/xyz/tbvns/rogue_block/Utils.java

@@ -0,0 +1,14 @@
+package xyz.tbvns.rogue_block;
+
+public class Utils {
+    public static void delay(int delay, Runnable runnable) {
+        new Thread(() -> {
+            try {
+                Thread.sleep(delay);
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+            runnable.run();
+        }).start();
+    }
+}