Skip to content

Commit

Permalink
refacto all system entity, language with support RawText System, syst…
Browse files Browse the repository at this point in the history
…em raknet
  • Loading branch information
SenseiTarzan committed Dec 23, 2024
1 parent 8204abf commit 22f1838
Show file tree
Hide file tree
Showing 49 changed files with 1,540 additions and 490 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
72 changes: 46 additions & 26 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<protocol.version>3.0.0.Beta5-SNAPSHOT</protocol.version>
<raklib.version>1.0.0.CR3-SNAPSHOT</raklib.version>
<log4j2.version>2.23.1</log4j2.version>
<log4j2.version>2.17.1</log4j2.version>
<jline.version>3.23.0</jline.version>
<netty.version>4.1.101.Final</netty.version>
</properties>
Expand Down Expand Up @@ -97,12 +97,6 @@
<id>central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>projectlombok.org</id>
Expand Down Expand Up @@ -167,11 +161,6 @@
<version>${log4j2.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand Down Expand Up @@ -293,27 +282,58 @@
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<mainClass>org.sculk.Sculk</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<!-- Replace this with your package! -->
<shadedPattern>org.sculk</shadedPattern>
</relocation>
</relocations>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.sculk.Sculk</mainClass>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<source>21</source>
<detectJavaApiLink>false</detectJavaApiLink>
<doclint>none</doclint>
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
131 changes: 70 additions & 61 deletions src/main/java/org/sculk/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.Logger;
import org.cloudburstmc.nbt.NbtMap;
import org.cloudburstmc.nbt.NbtMapBuilder;
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 @@ -20,6 +23,7 @@
import org.sculk.event.EventManager;
import org.sculk.event.command.CommandEvent;
import org.sculk.event.player.PlayerCreationEvent;
import org.sculk.event.player.PlayerLoginEvent;
import org.sculk.lang.Language;
import org.sculk.lang.LanguageKeys;
import org.sculk.lang.LocalManager;
Expand All @@ -30,12 +34,13 @@
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.player.text.RawTextBuilder;
import org.sculk.player.text.TranslaterBuilder;
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 +72,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,11 +98,15 @@ 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;
@Getter
private final boolean query;
private long nextTick;
private int tickCounter;

Expand All @@ -114,14 +125,16 @@ 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())));
System.out.println(this.properties.get(ServerPropertiesKeys.LANGUAGE, DEFAULT_LANGUAGE));
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));
this.config = new Config(this.dataPath + "/sculk.yml");
this.motd = this.properties.get(ServerPropertiesKeys.MOTD, "A Sculk Server Software");
this.submotd = this.properties.get(ServerPropertiesKeys.SUB_MOTD, "Powered by Sculk");
this.maxPlayers = this.properties.get(ServerPropertiesKeys.MAX_PLAYERS, 20);
this.query = this.properties.get(ServerPropertiesKeys.QUERY, false);

this.operators = new SculkOperators();
this.whitelist = new SculkWhitelist();
Expand All @@ -132,6 +145,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 +163,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 +180,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 All @@ -185,13 +201,13 @@ public void start() {

public void shutdown() {
Map<UUID, Player> onlinePlayers = this.getOnlinePlayers();
if (this.shutdown) {
return;
}
for (Map.Entry<UUID, Player> entry : onlinePlayers.entrySet()) {
Player player = entry.getValue();
player.getNetworkSession().disconnect(SERVER_CLOSED_MESSAGE);
}
if (this.shutdown) {
return;
}
this.logger.info(this.language.translate(LanguageKeys.SCULK_SERVER_STOPPING));

this.shutdown = true;
Expand All @@ -200,8 +216,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 All @@ -222,8 +236,8 @@ public CompletableFuture<Player> createPlayer(SculkServerSession session, Client
Player player;

try {
Constructor<? extends Player> constructor = clazz.getConstructor(SculkServerSession.class, ClientChainData.class);
player = constructor.newInstance(session, info);
Constructor<? extends Player> constructor = clazz.getConstructor(Server.class, SculkServerSession.class, ClientChainData.class, NbtMap.class);
player = constructor.newInstance(this, session, info, NbtMap.EMPTY);
this.addPlayer(session.getSocketAddress(), player);
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
this.logger.warn(FAILED_TO_CREATE_PLAYER, e);
Expand Down Expand Up @@ -262,14 +276,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 +311,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,71 +335,45 @@ 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) {
this.players.put(socketAddress, player);
}

public void addOnlinePlayer(Player player) {
public boolean addOnlinePlayer(Player player) {
PlayerLoginEvent event = new PlayerLoginEvent(player, "Plugin reason");
event.call();
if (event.isCancelled() || !player.getNetworkSession().isConnected())
return false;
for (Player onlinePlayer : this.playerList.values()) {
onlinePlayer.getNetworkSession().onPlayerAdded(player);
player.spawnTo(onlinePlayer);
onlinePlayer.spawnTo(player);
}
this.playerList.put(player.getUniqueId(), player);
return true;
}

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 removeOnlinePlayer(Player player) {
if (this.playerList.containsKey(player.getUniqueId())) {
this.playerList.remove(player.getUniqueId());
for (Player onlinePlayer : this.playerList.values()) {
onlinePlayer.getNetworkSession().onPlayerRemoved(player);
}
}
}

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
Loading

0 comments on commit 22f1838

Please sign in to comment.