91 lines
3.4 KiB
Java
91 lines
3.4 KiB
Java
package net.Chipperfluff.chipi.server;
|
|
|
|
import net.Chipperfluff.chipi.SpawnPlacedState;
|
|
import net.Chipperfluff.chipi.armor.ProtectionAuraHandler;
|
|
import net.Chipperfluff.chipi.block.ChipperPortalBlock;
|
|
import net.Chipperfluff.chipi.world.gen.ChipiDungeonGenerator;
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
|
|
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.registry.RegistryKey;
|
|
import net.minecraft.registry.RegistryKeys;
|
|
import net.minecraft.server.MinecraftServer;
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
|
import net.minecraft.server.world.ServerWorld;
|
|
import net.minecraft.structure.StructurePlacementData;
|
|
import net.minecraft.structure.StructureTemplate;
|
|
import net.minecraft.util.Identifier;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.world.GameRules;
|
|
import net.minecraft.world.World;
|
|
|
|
public final class ChipiServerEvents {
|
|
|
|
public static final RegistryKey<World> CHIPI_DIMENSION_KEY =
|
|
RegistryKey.of(RegistryKeys.WORLD, new Identifier("chipi", "chipi_dimension"));
|
|
|
|
private static final Identifier SPAWN_STRUCTURE =
|
|
new Identifier("chipi", "spawn");
|
|
|
|
private static MinecraftServer SERVER;
|
|
|
|
private ChipiServerEvents() {}
|
|
|
|
public static void register() {
|
|
ServerLifecycleEvents.SERVER_STARTED.register(server -> SERVER = server);
|
|
ServerTickEvents.END_SERVER_TICK.register(ChipiServerEvents::tickPlayers);
|
|
ServerTickEvents.END_WORLD_TICK.register(ChipiServerEvents::handleVoidFailsafe);
|
|
ServerWorldEvents.LOAD.register(ChipiServerEvents::onWorldLoad);
|
|
}
|
|
|
|
private static void tickPlayers(MinecraftServer server) {
|
|
for (ServerPlayerEntity player : server.getPlayerManager().getPlayerList()) {
|
|
ProtectionAuraHandler.tick(player);
|
|
}
|
|
}
|
|
|
|
private static void handleVoidFailsafe(ServerWorld world) {
|
|
if (!world.getRegistryKey().equals(CHIPI_DIMENSION_KEY)) return;
|
|
|
|
for (PlayerEntity player : world.getPlayers()) {
|
|
if (player.getBlockY() < 50) {
|
|
ChipperPortalBlock.teleportToChipiSpawn(world, player);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void onWorldLoad(MinecraftServer server, ServerWorld world) {
|
|
if (!world.getRegistryKey().equals(CHIPI_DIMENSION_KEY)) return;
|
|
|
|
world.setTimeOfDay(18000);
|
|
world.getGameRules().get(GameRules.DO_DAYLIGHT_CYCLE).set(false, server);
|
|
|
|
SpawnPlacedState state = world.getPersistentStateManager().getOrCreate(
|
|
SpawnPlacedState::fromNbt,
|
|
SpawnPlacedState::new,
|
|
"chipi_spawn"
|
|
);
|
|
|
|
if (state.placed) return;
|
|
|
|
StructureTemplate template =
|
|
world.getStructureTemplateManager().getTemplate(SPAWN_STRUCTURE).orElse(null);
|
|
|
|
if (template == null) return;
|
|
|
|
BlockPos spawn = new BlockPos(0, 80, 0);
|
|
|
|
template.place(world, spawn, spawn, new StructurePlacementData(), world.getRandom(), 2);
|
|
world.setSpawnPos(spawn.up(), 0f);
|
|
ChipiDungeonGenerator.generateInitialLayout(world, spawn);
|
|
|
|
state.placed = true;
|
|
state.markDirty();
|
|
}
|
|
|
|
public static ServerWorld getChipiWorld() {
|
|
return SERVER == null ? null : SERVER.getWorld(CHIPI_DIMENSION_KEY);
|
|
}
|
|
}
|