Skip to content

Commit d8812ad

Browse files
committed
Code cleanup, add info command to display current link info
1 parent 3128689 commit d8812ad

File tree

6 files changed

+84
-77
lines changed

6 files changed

+84
-77
lines changed

src/main/java/org/geysermc/globallinkserver/GlobalLinkServer.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package org.geysermc.globallinkserver;
77

8+
import java.util.ArrayList;
9+
import java.util.Collection;
810
import java.util.List;
911
import java.util.logging.Logger;
1012

@@ -18,6 +20,7 @@
1820
import org.bukkit.entity.Player;
1921
import org.bukkit.event.EventHandler;
2022
import org.bukkit.event.Listener;
23+
import org.bukkit.event.player.PlayerCommandSendEvent;
2124
import org.bukkit.event.player.PlayerJoinEvent;
2225
import org.bukkit.event.player.PlayerQuitEvent;
2326
import org.bukkit.plugin.Plugin;
@@ -88,7 +91,10 @@ public void onEnable() {
8891
commands.register(
8992
Commands.literal("linkinfo")
9093
.requires(ctx -> ctx.getSender() instanceof Player)
91-
.executes(commandUtils::info)
94+
.executes(ctx -> {
95+
Utils.sendCurrentLinkInfo(Utils.getPlayer(ctx));
96+
return 1;
97+
})
9298
.build(),
9399
"Use this command to show information whether you are currently linked.",
94100
List.of("info")
@@ -98,6 +104,28 @@ public void onEnable() {
98104
LOGGER.info("Started Global Linking Server plugin!");
99105
}
100106

107+
@EventHandler
108+
public void onCommands(PlayerCommandSendEvent event) {
109+
Collection<String> toRemove = new ArrayList<>();
110+
for (String command : event.getCommands()) {
111+
if (command.startsWith("link")) {
112+
continue;
113+
}
114+
115+
if (command.startsWith("unlink")) {
116+
continue;
117+
}
118+
119+
if (command.contains("info")) {
120+
continue;
121+
}
122+
123+
toRemove.add(command);
124+
}
125+
126+
event.getCommands().removeAll(toRemove);
127+
}
128+
101129
@EventHandler
102130
public void onPlayerLoad(PlayerJoinEvent event) {
103131
Utils.processJoin(event.getPlayer());

src/main/java/org/geysermc/globallinkserver/config/ConfigReader.java

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,18 @@
55
*/
66
package org.geysermc.globallinkserver.config;
77

8-
import com.google.gson.Gson;
9-
import org.bukkit.configuration.serialization.ConfigurationSerializable;
10-
import org.bukkit.configuration.serialization.ConfigurationSerialization;
118
import org.bukkit.plugin.java.JavaPlugin;
129

13-
import java.io.IOException;
14-
import java.nio.file.Files;
15-
import java.nio.file.Path;
16-
1710
public class ConfigReader {
18-
private static final Gson GSON = new Gson();
19-
private static Path CONFIG_PATH;
2011

2112
public static Config readConfig(JavaPlugin plugin) {
2213
plugin.saveDefaultConfig();
2314
var config = plugin.getConfig();
2415
plugin.saveConfig();
16+
2517
return new Config(config.getString("hostname"),
2618
config.getString("username"),
2719
config.getString("password"),
2820
config.getString("database"));
29-
30-
// CONFIG_PATH = path;
31-
// LOGGER.info("Reading config from " + CONFIG_PATH.toAbsolutePath());
32-
// String data = configContent();
33-
// if (data == null) {
34-
// createConfig();
35-
// }
36-
// data = configContent();
37-
//
38-
// return GSON.fromJson(data, Config.class);
39-
}
40-
41-
private static String configContent() {
42-
try {
43-
return Files.readString(CONFIG_PATH);
44-
} catch (IOException exception) {
45-
return null;
46-
}
47-
}
48-
49-
private static void createConfig() {
50-
try {
51-
//noinspection DataFlowIssue
52-
Files.copy(ConfigReader.class.getResourceAsStream("/config.json"), CONFIG_PATH);
53-
} catch (IOException exception) {
54-
throw new RuntimeException("Failed to copy config", exception);
55-
}
5621
}
5722
}

src/main/java/org/geysermc/globallinkserver/link/Link.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class Link {
3232
private UUID bedrockId;
3333
private UUID javaId;
3434
private String javaUsername;
35+
private String bedrockUsername;
3536

3637
public Link() {
3738
}
@@ -67,4 +68,13 @@ public Link javaUsername(String javaUsername) {
6768
this.javaUsername = javaUsername;
6869
return this;
6970
}
71+
72+
public String bedrockUsername() {
73+
return bedrockUsername;
74+
}
75+
76+
public Link bedrockUsername(String bedrockUsername) {
77+
this.bedrockUsername = bedrockUsername;
78+
return this;
79+
}
7080
}

src/main/java/org/geysermc/globallinkserver/link/LinkManager.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.kyori.adventure.text.format.NamedTextColor;
2424
import org.bukkit.Bukkit;
2525
import org.bukkit.entity.Player;
26+
import org.geysermc.floodgate.api.FloodgateApi;
2627
import org.geysermc.globallinkserver.config.Config;
2728
import org.geysermc.globallinkserver.util.Utils;
2829
import org.mariadb.jdbc.MariaDbPoolDataSource;
@@ -123,9 +124,8 @@ public CompletableFuture<Boolean> unlinkAccount(Player player) {
123124

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

151151
try (ResultSet resultSet = query.executeQuery()) {
152152
if (resultSet.next()) {
153+
long bedrockId = resultSet.getLong("bedrock_id");
154+
String bedrockTag = FloodgateApi.getInstance().getGamertagFor(bedrockId).join();
153155
return Optional.of(
154-
new Link(player).bedrockId(new UUID(0, resultSet.getLong("bedrock_id")))
156+
new Link(player)
157+
.bedrockId(new UUID(0, bedrockId))
158+
.bedrockUsername(bedrockTag)
155159
);
156160
} else {
157161
return Optional.empty(); // No match found

src/main/java/org/geysermc/globallinkserver/util/CommandUtils.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
import net.kyori.adventure.text.format.NamedTextColor;
1212
import org.bukkit.Bukkit;
1313
import org.bukkit.entity.Player;
14-
import org.geysermc.globallinkserver.link.Link;
1514
import org.geysermc.globallinkserver.link.LinkManager;
1615
import org.geysermc.globallinkserver.link.TempLink;
1716

17+
import static org.geysermc.globallinkserver.util.Utils.getPlayer;
18+
1819
@SuppressWarnings("UnstableApiUsage")
1920
public class CommandUtils {
2021

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

3031
if (Utils.isLinked(player)) {
31-
player.sendMessage("You are already linked!");
32+
player.sendMessage(Component.text("You are already linked! You need to unlink first before linking again.")
33+
.color(NamedTextColor.RED));
34+
Utils.sendCurrentLinkInfo(player);
3235
return 0;
3336
}
3437

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

102105
public int unlink(CommandContext<CommandSourceStack> ctx) {
103106
Player player = getPlayer(ctx);
107+
108+
if (!Utils.isLinked(player)) {
109+
player.sendMessage(Component.text("You are not currently linked!").color(NamedTextColor.RED));
110+
return 0;
111+
}
112+
104113
linkManager.unlinkAccount(player).whenComplete((result, error) -> {
105114
if (error != null) {
106115
error.printStackTrace();
@@ -118,21 +127,4 @@ public int unlink(CommandContext<CommandSourceStack> ctx) {
118127
});
119128
return 1;
120129
}
121-
122-
public static Player getPlayer(CommandContext<CommandSourceStack> ctx) {
123-
return (Player) ctx.getSource().getExecutor();
124-
}
125-
126-
public int info(CommandContext<CommandSourceStack> ctx) {
127-
Player player = getPlayer(ctx);
128-
129-
Link link = Utils.getLink(player);
130-
if (link == null) {
131-
player.sendMessage(Component.text("You are not currently linked!").color(NamedTextColor.RED));
132-
return 0;
133-
}
134-
135-
player.sendMessage(Component.text("You are currently linked!").color(NamedTextColor.GREEN));
136-
return 1;
137-
}
138130
}

src/main/java/org/geysermc/globallinkserver/util/Utils.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
*/
66
package org.geysermc.globallinkserver.util;
77

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

1920
import java.util.Map;
20-
import java.util.Set;
2121
import java.util.UUID;
2222

23+
@SuppressWarnings("UnstableApiUsage")
2324
public class Utils {
2425

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

39-
// TODO we do not have bedrock player names!
40-
public static Component getLinkInfo(Player player) {
41-
Link link = getLink(player);
42-
if (link == null) {
43-
return null;
44-
}
45-
46-
FloodgatePlayer floodgatePlayer = FloodgateApi.getInstance().getPlayer(player.getUniqueId());
47-
if (floodgatePlayer == null) {
48-
// Java player
49-
return Component.empty();
50-
} else {
51-
// Bedrock player
52-
return Component.empty();
53-
}
40+
public static Player getPlayer(CommandContext<CommandSourceStack> ctx) {
41+
return (Player) ctx.getSource().getExecutor();
5442
}
5543

5644
public static void processJoin(Player player) {
@@ -72,6 +60,7 @@ public static void processJoin(Player player) {
7260
linkedPlayers.put(player.getUniqueId(), new Link()
7361
.javaUsername(player.getName())
7462
.javaId(player.getUniqueId())
63+
.bedrockUsername(floodgatePlayer.getUsername())
7564
.bedrockId(floodgatePlayer.getJavaUniqueId()));
7665
}
7766
}
@@ -80,4 +69,23 @@ public static void processJoin(Player player) {
8069
public static void processLeave(Player player) {
8170
linkedPlayers.remove(player.getUniqueId());
8271
}
72+
73+
public static void sendCurrentLinkInfo(Player player) {
74+
Link link = linkedPlayers.get(player.getUniqueId());
75+
if (link == null) {
76+
player.sendMessage(Component.text("You are not currently linked.").color(NamedTextColor.AQUA));
77+
return;
78+
}
79+
80+
if (FloodgateApi.getInstance().isFloodgatePlayer(player.getUniqueId())) {
81+
// Bedrock player, show Java info
82+
player.sendMessage(Component.text("You are currently linked to the Java player %s (%s).".formatted(
83+
link.javaUsername(), link.javaId())).color(NamedTextColor.GREEN)
84+
);
85+
} else {
86+
player.sendMessage(Component.text("You are currently linked to the Bedrock player %s (%s).".formatted(
87+
link.bedrockUsername(), link.bedrockId())).color(NamedTextColor.GREEN)
88+
);
89+
}
90+
}
8391
}

0 commit comments

Comments
 (0)