Skip to content

Commit

Permalink
Code cleanup, add info command to display current link info
Browse files Browse the repository at this point in the history
  • Loading branch information
onebeastchris committed Jan 10, 2025
1 parent 3128689 commit d8812ad
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package org.geysermc.globallinkserver;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

Expand All @@ -18,6 +20,7 @@
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandSendEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
Expand Down Expand Up @@ -88,7 +91,10 @@ public void onEnable() {
commands.register(
Commands.literal("linkinfo")
.requires(ctx -> ctx.getSender() instanceof Player)
.executes(commandUtils::info)
.executes(ctx -> {
Utils.sendCurrentLinkInfo(Utils.getPlayer(ctx));
return 1;
})
.build(),
"Use this command to show information whether you are currently linked.",
List.of("info")
Expand All @@ -98,6 +104,28 @@ public void onEnable() {
LOGGER.info("Started Global Linking Server plugin!");
}

@EventHandler
public void onCommands(PlayerCommandSendEvent event) {
Collection<String> toRemove = new ArrayList<>();
for (String command : event.getCommands()) {
if (command.startsWith("link")) {
continue;
}

if (command.startsWith("unlink")) {
continue;
}

if (command.contains("info")) {
continue;
}

toRemove.add(command);
}

event.getCommands().removeAll(toRemove);
}

@EventHandler
public void onPlayerLoad(PlayerJoinEvent event) {
Utils.processJoin(event.getPlayer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,18 @@
*/
package org.geysermc.globallinkserver.config;

import com.google.gson.Gson;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ConfigReader {
private static final Gson GSON = new Gson();
private static Path CONFIG_PATH;

public static Config readConfig(JavaPlugin plugin) {
plugin.saveDefaultConfig();
var config = plugin.getConfig();
plugin.saveConfig();

return new Config(config.getString("hostname"),
config.getString("username"),
config.getString("password"),
config.getString("database"));

// CONFIG_PATH = path;
// LOGGER.info("Reading config from " + CONFIG_PATH.toAbsolutePath());
// String data = configContent();
// if (data == null) {
// createConfig();
// }
// data = configContent();
//
// return GSON.fromJson(data, Config.class);
}

private static String configContent() {
try {
return Files.readString(CONFIG_PATH);
} catch (IOException exception) {
return null;
}
}

private static void createConfig() {
try {
//noinspection DataFlowIssue
Files.copy(ConfigReader.class.getResourceAsStream("/config.json"), CONFIG_PATH);
} catch (IOException exception) {
throw new RuntimeException("Failed to copy config", exception);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/geysermc/globallinkserver/link/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class Link {
private UUID bedrockId;
private UUID javaId;
private String javaUsername;
private String bedrockUsername;

public Link() {
}
Expand Down Expand Up @@ -67,4 +68,13 @@ public Link javaUsername(String javaUsername) {
this.javaUsername = javaUsername;
return this;
}

public String bedrockUsername() {
return bedrockUsername;
}

public Link bedrockUsername(String bedrockUsername) {
this.bedrockUsername = bedrockUsername;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.geysermc.floodgate.api.FloodgateApi;
import org.geysermc.globallinkserver.config.Config;
import org.geysermc.globallinkserver.util.Utils;
import org.mariadb.jdbc.MariaDbPoolDataSource;
Expand Down Expand Up @@ -123,9 +124,8 @@ public CompletableFuture<Boolean> unlinkAccount(Player player) {

PreparedStatement query;
if (Utils.isBedrockPlayerId(player)) { // Should never happen
throw new RuntimeException("Floodgate linking was disabled!!!");
//query = connection.prepareStatement("DELETE FROM `links` WHERE `bedrock_id` = ?;");
//query.setLong(1, player.getUniqueId().getLeastSignificantBits());
query = connection.prepareStatement("DELETE FROM `links` WHERE `bedrock_id` = ?;");
query.setLong(1, player.getUniqueId().getLeastSignificantBits());
} else {
query = connection.prepareStatement("DELETE FROM `links` WHERE `java_id` = ?;");
query.setString(1, player.getUniqueId().toString());
Expand All @@ -150,8 +150,12 @@ public CompletableFuture<Optional<Link>> attemptFindJavaLink(Player player) {

try (ResultSet resultSet = query.executeQuery()) {
if (resultSet.next()) {
long bedrockId = resultSet.getLong("bedrock_id");
String bedrockTag = FloodgateApi.getInstance().getGamertagFor(bedrockId).join();
return Optional.of(
new Link(player).bedrockId(new UUID(0, resultSet.getLong("bedrock_id")))
new Link(player)
.bedrockId(new UUID(0, bedrockId))
.bedrockUsername(bedrockTag)
);
} else {
return Optional.empty(); // No match found
Expand Down
30 changes: 11 additions & 19 deletions src/main/java/org/geysermc/globallinkserver/util/CommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.geysermc.globallinkserver.link.Link;
import org.geysermc.globallinkserver.link.LinkManager;
import org.geysermc.globallinkserver.link.TempLink;

import static org.geysermc.globallinkserver.util.Utils.getPlayer;

@SuppressWarnings("UnstableApiUsage")
public class CommandUtils {

Expand All @@ -28,7 +29,9 @@ public int startLink(CommandContext<CommandSourceStack> ctx) {
Player player = getPlayer(ctx);

if (Utils.isLinked(player)) {
player.sendMessage("You are already linked!");
player.sendMessage(Component.text("You are already linked! You need to unlink first before linking again.")
.color(NamedTextColor.RED));
Utils.sendCurrentLinkInfo(player);
return 0;
}

Expand Down Expand Up @@ -101,6 +104,12 @@ public int linkWithCode(CommandContext<CommandSourceStack> ctx) {

public int unlink(CommandContext<CommandSourceStack> ctx) {
Player player = getPlayer(ctx);

if (!Utils.isLinked(player)) {
player.sendMessage(Component.text("You are not currently linked!").color(NamedTextColor.RED));
return 0;
}

linkManager.unlinkAccount(player).whenComplete((result, error) -> {
if (error != null) {
error.printStackTrace();
Expand All @@ -118,21 +127,4 @@ public int unlink(CommandContext<CommandSourceStack> ctx) {
});
return 1;
}

public static Player getPlayer(CommandContext<CommandSourceStack> ctx) {
return (Player) ctx.getSource().getExecutor();
}

public int info(CommandContext<CommandSourceStack> ctx) {
Player player = getPlayer(ctx);

Link link = Utils.getLink(player);
if (link == null) {
player.sendMessage(Component.text("You are not currently linked!").color(NamedTextColor.RED));
return 0;
}

player.sendMessage(Component.text("You are currently linked!").color(NamedTextColor.GREEN));
return 1;
}
}
42 changes: 25 additions & 17 deletions src/main/java/org/geysermc/globallinkserver/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
package org.geysermc.globallinkserver.util;

import com.mojang.brigadier.context.CommandContext;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;
Expand All @@ -17,9 +18,9 @@
import org.geysermc.globallinkserver.link.Link;

import java.util.Map;
import java.util.Set;
import java.util.UUID;

@SuppressWarnings("UnstableApiUsage")
public class Utils {

private static final Map<UUID, Link> linkedPlayers = new Object2ObjectOpenHashMap<>();
Expand All @@ -36,21 +37,8 @@ public static boolean isLinked(Player player) {
return linkedPlayers.get(player.getUniqueId());
}

// TODO we do not have bedrock player names!
public static Component getLinkInfo(Player player) {
Link link = getLink(player);
if (link == null) {
return null;
}

FloodgatePlayer floodgatePlayer = FloodgateApi.getInstance().getPlayer(player.getUniqueId());
if (floodgatePlayer == null) {
// Java player
return Component.empty();
} else {
// Bedrock player
return Component.empty();
}
public static Player getPlayer(CommandContext<CommandSourceStack> ctx) {
return (Player) ctx.getSource().getExecutor();
}

public static void processJoin(Player player) {
Expand All @@ -72,6 +60,7 @@ public static void processJoin(Player player) {
linkedPlayers.put(player.getUniqueId(), new Link()
.javaUsername(player.getName())
.javaId(player.getUniqueId())
.bedrockUsername(floodgatePlayer.getUsername())
.bedrockId(floodgatePlayer.getJavaUniqueId()));
}
}
Expand All @@ -80,4 +69,23 @@ public static void processJoin(Player player) {
public static void processLeave(Player player) {
linkedPlayers.remove(player.getUniqueId());
}

public static void sendCurrentLinkInfo(Player player) {
Link link = linkedPlayers.get(player.getUniqueId());
if (link == null) {
player.sendMessage(Component.text("You are not currently linked.").color(NamedTextColor.AQUA));
return;
}

if (FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId())) {
// Bedrock player, show Java info
player.sendMessage(Component.text("You are currently linked to the Java player %s (%s).".formatted(
link.javaUsername(), link.javaId())).color(NamedTextColor.GREEN)
);
} else {
player.sendMessage(Component.text("You are currently linked to the Bedrock player %s (%s).".formatted(
link.bedrockUsername(), link.bedrockId())).color(NamedTextColor.GREEN)
);
}
}
}

0 comments on commit d8812ad

Please sign in to comment.