Skip to content

Commit

Permalink
ini
Browse files Browse the repository at this point in the history
  • Loading branch information
AzaleeX committed Dec 17, 2024
1 parent 3e25ee4 commit 8f9f730
Show file tree
Hide file tree
Showing 30 changed files with 636 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .run/Sculk.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<extension name="coverage">
<pattern>
<option name="PATTERN" value="org.sculk.player.*" />
<option name="ENABLED" value="false" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
Expand Down
96 changes: 38 additions & 58 deletions src/main/java/org/sculk/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.Logger;
import org.cloudburstmc.protocol.bedrock.packet.BedrockPacket;
import org.cloudburstmc.protocol.bedrock.packet.PlayerListPacket;
import org.sculk.api.player.GameMode;
import org.sculk.api.server.Operators;
import org.sculk.command.CommandSender;
import org.sculk.command.SimpleCommandMap;
import org.sculk.config.Config;
Expand All @@ -30,12 +31,11 @@
import org.sculk.network.session.SculkServerSession;
import org.sculk.player.Player;
import org.sculk.player.client.ClientChainData;
import org.sculk.player.skin.Skin;
import org.sculk.plugin.PluginManager;
import org.sculk.resourcepack.ResourcePackManager;
import org.sculk.scheduler.Scheduler;
import org.sculk.server.SculkOperators;
import org.sculk.server.SculkWhitelist;
import org.sculk.utils.SkinUtils;
import org.sculk.utils.TextFormat;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -67,6 +67,8 @@ public class Server {
private final TerminalConsole console;
private final EventManager eventManager;
private final PluginManager pluginManager;
@Getter
private final ResourcePackManager resourcePackManager;
private final Injector injector;

private final Scheduler scheduler;
Expand All @@ -91,10 +93,12 @@ public class Server {
private final Map<UUID, Player> playerList = new HashMap<>();
private final Map<SocketAddress, Player> players = new HashMap<>();

@Getter
private String motd;
private String submotd;
@Getter
private int maxPlayers;
private String defaultGamemode;
@Getter
private UUID serverId;
private long nextTick;
private int tickCounter;
Expand All @@ -114,7 +118,7 @@ public Server(LocalManager localManager, Logger logger, String dataPath) {
//Language manager
this.localManager = localManager;
this.language = localManager.getLanguage(this.properties.get(ServerPropertiesKeys.LANGUAGE, DEFAULT_LANGUAGE));
this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_SELECTED_LANGUAGE, List.of(this.language.getName())));
this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_SELECTED_LANGUAGE, List.of(TextFormat.DARK_AQUA + this.language.getName() + TextFormat.RESET)));

//Load server properties
this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_LOADING));
Expand All @@ -132,6 +136,7 @@ public Server(LocalManager localManager, Logger logger, String dataPath) {
this.eventManager = injector.getInstance(EventManager.class);
this.scheduler = injector.getInstance(Scheduler.class);
this.pluginManager = new PluginManager(this);
this.resourcePackManager = new ResourcePackManager();
this.simpleCommandMap = new SimpleCommandMap(this);

this.console = new TerminalConsole(this);
Expand All @@ -149,7 +154,7 @@ public void start() {
this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_ONLINE_MODE_DISABLED, TextFormat.RED));
}

log.info(language.translate(LanguageKeys.SCULK_SERVER_STARTING, List.of(TextFormat.DARK_AQUA + CODE_NAME + TextFormat.WHITE, TextFormat.AQUA + CODE_VERSION + TextFormat.WHITE)));
log.info(language.translate(LanguageKeys.SCULK_SERVER_STARTING, List.of(TextFormat.DARK_AQUA + CODE_NAME + TextFormat.RESET, TextFormat.AQUA + CODE_VERSION + TextFormat.WHITE)));

InetSocketAddress bindAddress = new InetSocketAddress(this.getProperties().get(ServerPropertiesKeys.SERVER_IP, "0.0.0.0"), this.getProperties().get(ServerPropertiesKeys.SERVER_PORT, 19132));
this.serverId = UUID.randomUUID();
Expand All @@ -166,6 +171,8 @@ public void start() {

this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_DISTRIBUTED_UNDER, List.of(TextFormat.AQUA + "GNU GENERAL PUBLIC LICENSE")));

this.resourcePackManager.loadResourcePacks();

this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_LOADING_COMMANDS));

this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_LOADING_PLUGINS));
Expand Down Expand Up @@ -200,8 +207,6 @@ public void shutdown() {
pluginManager.disableAllPlugins();
this.logger.info(this.language.translate(LanguageKeys.SCULK_PLUGINS_DISABLED));

Sculk.shutdown();

this.logger.info(this.language.translate(LanguageKeys.SCULK_NETWORK_INTERFACES_STOPPING));
for (SourceInterface sourceInterface : this.network.getInterfaces()) {
sourceInterface.shutdown();
Expand Down Expand Up @@ -262,14 +267,29 @@ public Injector getInjector() {
return injector;
}

/**
* Checks whether the server is currently running.
*
* @return {@code true} if the server is running, {@code false} if it has been shut down.
*/
public boolean isRunning() {
return !this.shutdown;
}

/**
* Returns the singleton instance of the Server.
*
* @return the single instance of the Server.
*/
public static Server getInstance() {
return instance;
}

/**
* Retrieves the logger instance associated with this class.
*
* @return the logger instance
*/
public Logger getLogger() {
return logger;
}
Expand All @@ -282,6 +302,12 @@ public Path getPluginDataPath() {
return pluginDataPath;
}

/**
* Retrieves a map of online players currently active in the system.
*
* @return An unmodifiable map containing the online players, where the keys are their UUIDs
* and the values are the corresponding Player objects.
*/
public Map<UUID, Player> getOnlinePlayers() {
return Collections.unmodifiableMap(playerList);
}
Expand All @@ -300,24 +326,16 @@ public void broadcastMessage(String message) {
}
}

public int getMaxPlayers() {
return maxPlayers;
}

public String getDefaultGamemode() {
return defaultGamemode;
}

public String getMotd() {
return motd;
public Integer getDefaultGamemode() {
return this.getProperties().get(ServerPropertiesKeys.GAMEMODE, GameMode.SURVIVAL.getId());
}

public String getSubMotd() {
return submotd;
}

public UUID getServerId() {
return serverId;
public Operators getOperators() {
return this.operators;
}

public void addPlayer(SocketAddress socketAddress, Player player) {
Expand All @@ -328,44 +346,6 @@ public void addOnlinePlayer(Player player) {
this.playerList.put(player.getUniqueId(), player);
}

public void sendFullPlayerList(Player player) {
PlayerListPacket packet = new PlayerListPacket();
packet.setAction(PlayerListPacket.Action.ADD);
packet.getEntries().addAll(this.playerList.values().stream().map(p -> {
PlayerListPacket.Entry entry = new PlayerListPacket.Entry(p.getUniqueId());
entry.setEntityId(p.getEntityId());
entry.setName(p.getName());
entry.setSkin(SkinUtils.toSerialized(p.getSkin()));
entry.setPlatformChatId("");
return entry;
}).toList());
player.sendDataPacket(packet);
}

public void removeFromTabList(Player player) {
PlayerListPacket packet = new PlayerListPacket();
packet.setAction(PlayerListPacket.Action.REMOVE);
packet.getEntries().add(new PlayerListPacket.Entry(player.getUniqueId()));
broadcastPacket(packet);
}

public void addToTabList(UUID uuid, long entityId, String name, ClientChainData chainData, String xuid, Skin skin) {
PlayerListPacket playerListPacket = new PlayerListPacket();
playerListPacket.setAction(PlayerListPacket.Action.ADD);

PlayerListPacket.Entry entry = new PlayerListPacket.Entry(uuid);
entry.setEntityId(entityId);
entry.setName(name);
entry.setXuid(xuid);
entry.setPlatformChatId(chainData.getDeviceId());
entry.setBuildPlatform(chainData.getDeviceOS());
entry.setSkin(SkinUtils.toSerialized(skin));
entry.setTrustedSkin(skin.isTrusted());

playerListPacket.getEntries().add(entry);
this.broadcastPacket(playerListPacket);
}

public Scheduler getScheduler() {
return scheduler;
}
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/sculk/api/player/GameMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.sculk.api.player;

/*
* ____ _ _
* / ___| ___ _ _| | | __
* \___ \ / __| | | | | |/ /
* ___) | (__| |_| | | <
* |____/ \___|\__,_|_|_|\_\
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author: SculkTeams
* @link: http://www.sculkmp.org/
*/

public enum GameMode {
SURVIVAL("Survival", 0),
CREATIVE("Creative", 1),
ADVENTURE("Adventure", 2),
SPECTATOR("Spectator", 3);

private final String identifier;
private final int id;

GameMode(String identifier, int id) {
this.identifier = identifier;
this.id = id;
}

public String getIdentifier() {
return identifier;
}

public int getId() {
return id;
}

public static GameMode fromId(int id) {
switch (id) {
case 0:
return SURVIVAL;
case 1:
return CREATIVE;
case 2:
return ADVENTURE;
default:
return SPECTATOR;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sculk.server;
package org.sculk.api.server;

public interface Operators {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.sculk.server;
package org.sculk.api.server;

public interface Whitelist {

Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/sculk/command/defaults/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ public void onRun(CommandSender sender, String commandLabel, Map<String, Object>
actualPage = 1;
}
} else if (args.containsKey("command")) {
Command command = (Command) args.get("command");
String commandName = args.get("command").toString();
Command command = Server.getInstance().getCommandMap().getCommand(commandName);
if (command != null) {
sender.sendMessage(new RawTextBuilder().add(new TranslaterBuilder()
sender.sendMessage(new RawTextBuilder().add(new TranslaterBuilder<RawTextBuilder>()
.setTranslate("§6/%%s: §f%%s\nUsage: §e%%s")
.setWith(new RawTextBuilder()
.add(new TextBuilder().setText(command.getLabel()))
Expand All @@ -71,8 +72,8 @@ public void onRun(CommandSender sender, String commandLabel, Map<String, Object>
)
));
} else {
sender.sendMessage(new RawTextBuilder().add(new TranslaterBuilder().setTranslate("§4/%%s§c does not seem to exist, check the list of commands with §4/help§c.")
.setWith(new RawTextBuilder().add(new TextBuilder().setText(args.get("command").toString())))));
sender.sendMessage(new RawTextBuilder().add(new TranslaterBuilder<RawTextBuilder>().setTranslate("§4/%%s§c does not seem to exist, check the list of commands with §4/help§c.")
.setWith(new RawTextBuilder().add(new TextBuilder().setText(commandName)))));
}
return;
}
Expand All @@ -88,7 +89,7 @@ public void onRun(CommandSender sender, String commandLabel, Map<String, Object>
}
}

TranslaterBuilder translaterBuilder = new TranslaterBuilder();
TranslaterBuilder<RawTextBuilder> translaterBuilder = new TranslaterBuilder<>();
translaterBuilder.setTranslate("§6-------------- §fHelp - %%s command(s) §7[%%s/%%s] §6--------------\n%%s");
translaterBuilder.setWith(new RawTextBuilder()
.add(new TextBuilder()
Expand All @@ -103,4 +104,4 @@ public void onRun(CommandSender sender, String commandLabel, Map<String, Object>
sender.sendMessage(new RawTextBuilder().add(translaterBuilder));
}

}
}
5 changes: 4 additions & 1 deletion src/main/java/org/sculk/config/ServerProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.sculk.config;

import org.sculk.api.player.GameMode;

import java.io.File;
import java.nio.file.Path;

Expand All @@ -24,7 +26,7 @@ private ConfigSection getDefaultValues() {
defaults.put(ServerPropertiesKeys.SERVER_PORT.toString(), 19132);
defaults.put(ServerPropertiesKeys.WHITELIST.toString(), "off");
defaults.put(ServerPropertiesKeys.MAX_PLAYERS.toString(), 20);
defaults.put(ServerPropertiesKeys.GAMEMODE.toString(), 0);
defaults.put(ServerPropertiesKeys.GAMEMODE.toString(), GameMode.SURVIVAL.getId());
defaults.put(ServerPropertiesKeys.PVP.toString(), "on");
defaults.put(ServerPropertiesKeys.DIFFICULTY.toString(), 1);
defaults.put(ServerPropertiesKeys.LEVEL_NAME.toString(), "world");
Expand All @@ -34,6 +36,7 @@ private ConfigSection getDefaultValues() {
defaults.put(ServerPropertiesKeys.SPAWN_MONSTERS.toString(), "on");
defaults.put(ServerPropertiesKeys.AUTO_SAVE.toString(), "on");
defaults.put(ServerPropertiesKeys.XBOX_AUTH.toString(), "on");
defaults.put(ServerPropertiesKeys.FORCE_RESOURCE_PACKS.toString(), "off");
return defaults;
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/sculk/config/ServerPropertiesKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum ServerPropertiesKeys {
SPAWN_ANIMALS("spawn-animals"),
SPAWN_MONSTERS("spawn-monsters"),
AUTO_SAVE("auto-save"),
XBOX_AUTH("xbox-auth");
XBOX_AUTH("xbox-auth"),
FORCE_RESOURCE_PACKS("force-resource-packs");

private final String key;

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/sculk/console/ConsoleThread.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.sculk.console;

import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec;

/*
* ____ _ _
* / ___| ___ _ _| | | __
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/sculk/console/TerminalConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,17 @@ public Locale getLocale() {

@Override
public Language getLanguage() {
return null;
return this.server.getLanguage();
}

@Override
public void sendMessage(String message) {
this.server.getLogger().info(message);

}

@Override
public void sendMessage(RawTextBuilder textBuilder) {
this.server.getLogger().info(textBuilder.toString());
this.server.getLogger().info(this.getLanguage().translate(textBuilder));
}

@Override
Expand Down
Loading

0 comments on commit 8f9f730

Please sign in to comment.