Skip to content

Commit

Permalink
Migrate to RccLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
Ale32bit committed Nov 22, 2024
1 parent 2813e6d commit 6f7ee73
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 216 deletions.
8 changes: 2 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ repositories {
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.

maven { url 'https://maven.wispforest.io' }

maven { url "https://maven.reconnected.cc/releases" }

maven { url 'https://maven.nucleoid.xyz' }
Expand All @@ -44,11 +42,9 @@ dependencies {

modImplementation include("eu.pb4:placeholder-api:${project.placeholderapi_version}")

annotationProcessor modImplementation("io.wispforest:owo-lib:${project.owo_version}")
include "io.wispforest:owo-sentinel:${project.owo_version}"

modImplementation "cc.reconnected:rcc-library:${project.rcclibrary_version}"
modImplementation "cc.reconnected:rcc-discord:${project.rccdiscord_version}"
modImplementation "cc.reconnected:rcc-server:${project.rccserver_version}"
modImplementation "cc.reconnected:rcc-essentials:${project.rccessentials_version}"

include implementation("org.java-websocket:Java-WebSocket:${project.websocket_version}")

Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ org.gradle.parallel=true
# check these on https://fabricmc.net/develop
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.10
loader_version=0.16.7
loader_version=0.16.9

# Mod Properties
mod_version=1.7.7
mod_version=1.8.0
maven_group=cc.reconnected
archives_base_name=rcc-chatbox

# Dependencies
fabric_version=0.92.2+1.20.1

rccserver_version=1.16.1
rccdiscord_version=1.7.9
rccessentials_version=1.18.0
rcclibrary_version=1.0.0
rccdiscord_version=1.8.0

permissions_api_version=0.2-SNAPSHOT

Expand All @@ -29,5 +30,4 @@ adventure_legacy_serializer_version=4.14.0
placeholderapi_version=2.1.3+1.20.1

websocket_version=1.5.7
owo_version=0.11.2+1.20
jda_version=5.0.2
16 changes: 8 additions & 8 deletions src/main/java/cc/reconnected/chatbox/ClientPacketsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import cc.reconnected.chatbox.utils.DateUtils;
import cc.reconnected.chatbox.utils.TextComponents;
import cc.reconnected.chatbox.ws.ClientErrors;
import cc.reconnected.server.api.PlayerMeta;
import cc.reconnected.library.data.PlayerMeta;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -57,7 +57,7 @@ private static void tickQueue(MinecraftServer server) {

if (msg.type == MessageTypes.SAY) {
mcServer.getPlayerManager().getPlayerList().forEach(player -> player.sendMessage(msg.message));
msg.conn.send(Chatbox.GSON.toJson(new SuccessPacket("message_sent", msg.id)));
msg.conn.send(RccChatbox.GSON.toJson(new SuccessPacket("message_sent", msg.id)));

// Emit chat_chatbox event
if(msg.sayPacket == null)
Expand All @@ -76,27 +76,27 @@ private static void tickQueue(MinecraftServer server) {
chatboxChatPacket.time = DateUtils.getTime(new Date());
chatboxChatPacket.user = msg.ownerUser;

Chatbox.getInstance().wss().broadcastEvent(chatboxChatPacket, Capability.READ);
RccChatbox.getInstance().wss().broadcastEvent(chatboxChatPacket, Capability.READ);
} else if (msg.type == MessageTypes.TELL) {
var player = server.getPlayerManager().getPlayer(msg.player);
if (player == null) {
var err = ClientErrors.UNKNOWN_USER;
msg.conn.send(Chatbox.GSON.toJson(new ErrorPacket(err.getErrorMessage(), err.message, msg.id)));
msg.conn.send(RccChatbox.GSON.toJson(new ErrorPacket(err.getErrorMessage(), err.message, msg.id)));
continue;
}
player.sendMessage(msg.message);

msg.conn.send(Chatbox.GSON.toJson(new SuccessPacket("message_sent", msg.id)));
msg.conn.send(RccChatbox.GSON.toJson(new SuccessPacket("message_sent", msg.id)));
}
}
}

private static String enqueueAndResult(UUID licenseId, ClientMessage message, int id) {
if (tryEnqueue(licenseId, message)) {
return Chatbox.GSON.toJson(new SuccessPacket("message_queued", id));
return RccChatbox.GSON.toJson(new SuccessPacket("message_queued", id));
} else {
var err = ClientErrors.RATE_LIMITED;
return Chatbox.GSON.toJson(new ErrorPacket(err.getErrorMessage(), err.message, id));
return RccChatbox.GSON.toJson(new ErrorPacket(err.getErrorMessage(), err.message, id));
}
}

Expand Down Expand Up @@ -138,7 +138,7 @@ public static void register() {
var player = mcServer.getPlayerManager().getPlayer(packet.user);
if (player == null) {
var err = ClientErrors.UNKNOWN_USER;
client.webSocket.send(Chatbox.GSON.toJson(new ErrorPacket(err.getErrorMessage(), err.message, packet.id)));
client.webSocket.send(RccChatbox.GSON.toJson(new ErrorPacket(err.getErrorMessage(), err.message, packet.id)));
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
package cc.reconnected.chatbox;

import cc.reconnected.chatbox.command.ChatboxCommand;
import cc.reconnected.chatbox.listeners.ChatboxEvents;
import cc.reconnected.chatbox.listeners.DiscordEvents;
import cc.reconnected.chatbox.listeners.EssentialsEvents;
import cc.reconnected.chatbox.packets.serverPackets.PingPacket;
import cc.reconnected.chatbox.state.StateSaverAndLoader;
import com.google.gson.Gson;
import cc.reconnected.chatbox.license.LicenseManager;
import cc.reconnected.chatbox.ws.WsServer;
import cc.reconnected.discordbridge.RccDiscordConfig;
import cc.reconnected.library.config.Config;
import com.google.gson.Gson;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.WorldSavePath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;

public class Chatbox implements ModInitializer {
public class RccChatbox implements ModInitializer {

public static final String MOD_ID = "rcc-chatbox";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final cc.reconnected.chatbox.ChatboxConfig CONFIG = cc.reconnected.chatbox.ChatboxConfig.createAndLoad();
public static RccChatboxConfig CONFIG;
public static final Gson GSON = new Gson();
private static LicenseManager licenseManager;

private static Chatbox INSTANCE;
private static RccChatbox INSTANCE;

public static Chatbox getInstance() {
public static RccChatbox getInstance() {
return INSTANCE;
}

public Chatbox() {
public RccChatbox() {
INSTANCE = this;
}

Expand Down Expand Up @@ -87,5 +93,13 @@ public void onInitialize() {
});

ChatboxEvents.register();

var fabricLoader = FabricLoader.getInstance();
if(fabricLoader.isModLoaded("rcc-essentials")) {
EssentialsEvents.register();
}
if(fabricLoader.isModLoaded("rcc-discord")) {
DiscordEvents.register();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cc.reconnected.chatbox;

import io.wispforest.owo.config.annotation.Config;
import cc.reconnected.library.config.Config;

@Config(name = "rcc-chatbox-config", wrapperName = "ChatboxConfig")
public class ChatboxConfigModel {
@Config(RccChatbox.MOD_ID)
public class RccChatboxConfig {
public String hostname = "127.0.0.1";
public short port = 25580;
public String guestAllowedAddress = "127.0.0.1";
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/cc/reconnected/chatbox/command/AdminSubCommand.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package cc.reconnected.chatbox.command;

import cc.reconnected.chatbox.Chatbox;
import cc.reconnected.chatbox.RccChatbox;
import cc.reconnected.chatbox.license.Capability;
import cc.reconnected.chatbox.license.License;
import cc.reconnected.chatbox.ws.CloseCodes;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.kyori.adventure.text.Component;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.GameProfileArgumentType;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
Expand Down Expand Up @@ -42,9 +39,9 @@ public class AdminSubCommand {
uuid = player.getGameProfile().getId();
}

var license = Chatbox.licenseManager().getLicense(uuid);
var license = RccChatbox.licenseManager().getLicense(uuid);
if (license == null) {
license = Chatbox.licenseManager().getLicenseFromUser(uuid);
license = RccChatbox.licenseManager().getLicenseFromUser(uuid);
}

return license;
Expand Down Expand Up @@ -81,7 +78,7 @@ public static LiteralArgumentBuilder<ServerCommandSource> register(CommandDispat
.stream()
.map(player -> player.getGameProfile().getName())
.toList());
list.addAll(Chatbox.licenseManager().getLicenseList());
list.addAll(RccChatbox.licenseManager().getLicenseList());
return CommandSource.suggestMatching(
list,
builder
Expand Down Expand Up @@ -135,8 +132,8 @@ public static LiteralArgumentBuilder<ServerCommandSource> register(CommandDispat
playerName = player.getGameProfile().getName();
}

Chatbox.licenseManager().deleteLicense(license.uuid());
Chatbox.getInstance().wss().closeLicenseClients(license.uuid(), CloseCodes.CHANGED_LICENSE_KEY);
RccChatbox.licenseManager().deleteLicense(license.uuid());
RccChatbox.getInstance().wss().closeLicenseClients(license.uuid(), CloseCodes.CHANGED_LICENSE_KEY);

final var finalPlayerName = playerName;
context.getSource().sendFeedback(() -> Text.empty().append(prefix).append(Text.literal("Revoked " + finalPlayerName + " license!").setStyle(Style.EMPTY.withColor(Formatting.GREEN))), true);
Expand Down Expand Up @@ -186,7 +183,7 @@ public static LiteralArgumentBuilder<ServerCommandSource> register(CommandDispat
playerName = player.getGameProfile().getName();
}

var licenseManager = Chatbox.licenseManager();
var licenseManager = RccChatbox.licenseManager();

var capability = Capability.valueOf(capabilityName);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cc.reconnected.chatbox.command;

import cc.reconnected.chatbox.Chatbox;
import cc.reconnected.chatbox.RccChatbox;
import cc.reconnected.chatbox.license.Capability;
import cc.reconnected.chatbox.license.License;
import cc.reconnected.chatbox.ws.CloseCodes;
Expand Down Expand Up @@ -90,7 +90,7 @@ public static LiteralArgumentBuilder<ServerCommandSource> register(CommandDispat
context.getSource().sendFeedback(() -> Text.literal("This command can only be executed by players!"), false);
return 0;
}
var manager = Chatbox.licenseManager();
var manager = RccChatbox.licenseManager();
final var userId = context.getSource().getPlayer().getUuid();
var userLicense = manager.getLicenseFromUser(userId);
if (userLicense == null) {
Expand Down Expand Up @@ -119,7 +119,7 @@ public static LiteralArgumentBuilder<ServerCommandSource> register(CommandDispat
context.getSource().sendFeedback(() -> Text.literal("This command can only be executed by players!"), false);
return 0;
}
var manager = Chatbox.licenseManager();
var manager = RccChatbox.licenseManager();
final var userId = context.getSource().getPlayer().getUuid();
var userLicense = manager.getLicenseFromUser(userId);
var createNew = userLicense == null;
Expand All @@ -138,7 +138,7 @@ public static LiteralArgumentBuilder<ServerCommandSource> register(CommandDispat
context.getSource().sendFeedback(() -> Text.literal("This command can only be executed by players!"), false);
return 0;
}
var manager = Chatbox.licenseManager();
var manager = RccChatbox.licenseManager();
var userLicense = manager.getLicenseFromUser(context.getSource().getPlayer().getUuid());
if (userLicense == null) {
var text = Component.empty().append(ChatboxCommand.prefix)
Expand All @@ -155,7 +155,7 @@ public static LiteralArgumentBuilder<ServerCommandSource> register(CommandDispat
var text = Component.empty().append(ChatboxCommand.prefix)
.append(Component.text("Your license has been revoked!").color(NamedTextColor.GREEN));
context.getSource().sendMessage(text);
Chatbox.getInstance().wss().closeLicenseClients(licenseUuid, CloseCodes.CHANGED_LICENSE_KEY);
RccChatbox.getInstance().wss().closeLicenseClients(licenseUuid, CloseCodes.CHANGED_LICENSE_KEY);
} else {
var text = Component.empty().append(ChatboxCommand.prefix)
.append(Component.text("There was an error revoking your license!").color(NamedTextColor.RED));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cc.reconnected.chatbox.command;

import cc.reconnected.chatbox.ChatboxEvents;
import cc.reconnected.chatbox.listeners.ChatboxEvents;
import cc.reconnected.chatbox.state.StateSaverAndLoader;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/cc/reconnected/chatbox/license/LicenseManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cc.reconnected.chatbox.license;

import cc.reconnected.chatbox.Chatbox;
import cc.reconnected.server.api.PlayerMeta;
import cc.reconnected.chatbox.RccChatbox;
import cc.reconnected.library.data.PlayerMeta;
import com.google.gson.reflect.TypeToken;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -33,29 +33,29 @@ public LicenseManager() {

// Manage dedicated data for licenses.
// Minecraft's persistent state loses data on crashes, the chatbox licenses should not depend on a stable state of the world data
licensesPath = Chatbox.dataDirectory().resolve("licenses.json");
licensesPath = RccChatbox.dataDirectory().resolve("licenses.json");

if (licensesPath.toFile().exists()) {
try (var stream = new BufferedReader(new FileReader(licensesPath.toFile(), StandardCharsets.UTF_8))) {
var type = new TypeToken<ConcurrentHashMap<UUID, License>>() {
}.getType();
licenses = Chatbox.GSON.fromJson(stream, type);
licenses = RccChatbox.GSON.fromJson(stream, type);
} catch (FileNotFoundException e) {
Chatbox.LOGGER.error("If you read this I messed up", e);
RccChatbox.LOGGER.error("If you read this I messed up", e);
} catch (IOException e) {
Chatbox.LOGGER.error("Exception reading licenses data", e);
RccChatbox.LOGGER.error("Exception reading licenses data", e);
}
} else {
licenses = new ConcurrentHashMap<>();
}
}

private void saveData() {
var output = Chatbox.GSON.toJson(licenses);
var output = RccChatbox.GSON.toJson(licenses);
try (var stream = new FileWriter(licensesPath.toFile(), StandardCharsets.UTF_8)) {
stream.write(output);
} catch (IOException e) {
Chatbox.LOGGER.error("Exception saving licenses data", e);
RccChatbox.LOGGER.error("Exception saving licenses data", e);
}
}

Expand All @@ -75,7 +75,7 @@ public License getLicense(UUID licenseId) {
return licenses.get(licenseId);
}

var serverState = Chatbox.getInstance().serverState();
var serverState = RccChatbox.getInstance().serverState();
if (!serverState.licenses.containsKey(licenseId))
return null;

Expand Down Expand Up @@ -160,7 +160,7 @@ public boolean deleteLicense(UUID licenseId) {
playerData.delete(KEYS.licenseUuid).join();
playerData.delete(KEYS.capabilities).join();

Chatbox.getInstance().serverState().licenses.remove(license.uuid());
RccChatbox.getInstance().serverState().licenses.remove(license.uuid());
licenses.remove(license.uuid());

saveData();
Expand Down
Loading

0 comments on commit 6f7ee73

Please sign in to comment.