package los.numeros.EPHS; import java.util.Random; import java.util.UUID; import java.util.Collection; import java.util.Date; import java.util.function.BooleanSupplier; import los.numeros.GameEngine.Engine; import los.numeros.GameEngine.Module; import net.fabricmc.fabric.api.networking.v1.PlayerLookup; import net.minecraft.network.MessageType; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import los.numeros.EPHS.modules.*; public class Main implements Engine { BooleanSupplier running = () -> { return true; }; Module modules[] = new Module[10]; public Main(ServerCommandSource source, int numSeekers) { int i; MinecraftServer server = source.getMinecraftServer(); ServerPlayerEntity[] players, seekers, hiders; Random r = new Random(new Date().getTime()); /* get all the players on the server */ Collection playerCollection = PlayerLookup.all(server); players = playerCollection.toArray(new ServerPlayerEntity[playerCollection.size()]); /* create the hiders and seekers array of appropriate sizes */ seekers = new ServerPlayerEntity[numSeekers]; hiders = new ServerPlayerEntity[players.length - numSeekers]; /* too few players to start the game */ if (players.length <= 2) { /* send a message letting everyone know there aren't enough players to start the game */ server.getPlayerManager().broadcastChatMessage( new LiteralText("Not enough players to start the game"), MessageType.SYSTEM, new UUID(0, 0) ); return; } /* creates an array of seekers */ for (i = 0; numSeekers > 0; numSeekers--, i++) { /* TODO: make sure the same person isn't picked to be all of the seekers */ seekers[i] = players[r.nextInt(players.length)]; } /* creates an array of hiders */ i = 0; for (ServerPlayerEntity p : players) { /* add all players who arent seekers to the hiders array */ if (!hasPlayer(seekers, p)) { hiders[i++] = p; } } /* populate the modules */ modules[0] = new MainModule(seekers, hiders, players, source); modules[1] = new GameClockModule(seekers, hiders, players, server); /* start the game */ Engine.StartEngine(running, modules); } private boolean hasPlayer(ServerPlayerEntity[] array, ServerPlayerEntity player) { for (ServerPlayerEntity p : array) { if (p == player) return true; } return false; } }