Skip to content

Commit

Permalink
1.2.0: Add transfer menu; localization system
Browse files Browse the repository at this point in the history
  • Loading branch information
onebeastchris committed Oct 30, 2024
1 parent 75b5f24 commit 45d222c
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 110 deletions.
58 changes: 41 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ A Geyser extension that allows you to map Java -> Geyser/Bedrock servers to tran
# Introduction
Since 1.20.5/6, there is a native Java transfer packet. For once, this is a feature that Bedrock already did have!
However, there is one notable difference: Geyser receives the Java client IP/port to transfer players to. If Geyser
just passes that to the Bedrock client, the client likely would not find a Bedrock server there*.
just passes that to the Bedrock client, the client likely would not find a Bedrock server there[^1].
In most cases, this would not happen. Hence, this Geyser extension allows you to customize which Geyser - or Bedrock server -
to transfer players to upon Geyser receives a Java transfer packet.


*unless Geyser is also running on the same Java port, but that's not always the case.
[^1]: Technically it could work if Geyser is also running on the same Java port, but that's usually not the case.

# Configuration
There are two main configuration options:
Expand All @@ -24,10 +24,13 @@ and is already sending a Geyser server IP/port to connect to.
use the following format: `example.com:12892`, or `198.51.100.0:13132`. Alternatively, you can leave out the port,
this would fall back to the default Java/Bedrock ports (25565 for Java, and 19132 for Bedrock).

Further, there is an option to add a Bedrock-only `/transfer` command. If enabled, Bedrock players can:
- Transfer to pre-defined servers (set in `transfer-shortcuts`) by running '/transfer <server>'
Further, there is an option to add a Bedrock-only `/transfertool transfer` command. If enabled, Bedrock players can:
- Transfer to pre-defined servers (set in `transfer-shortcuts`) by running '/transfertool transfer <server>'
- Transfer to any server - if they have the `transfertool.command.transfer.any` permission. Usage:
`/transfer test.geysermc.org` or `/transfer test.geysermc.org:19132` or `/transfer test.geysermc.org 19132`
`/transfertool transfer test.geysermc.org` or `/transfertool transfer test.geysermc.org:19132` or `/transfertool transfer test.geysermc.org 19132`
- See a pretty menu to select their options by running `/transfertool transfer` command without arguments.

- `default-locale`: See the "localization" section below for info on this config option.

Example config:
```yaml
Expand All @@ -36,31 +39,42 @@ Example config:
# Documentation: https://onechris.dev/docs/extensions/transfertool/
# Source: https://github.com/onebeastchris/TransferTool/

# Whether to pass the IP/Port sent by the Java server to the Bedrock client.
# This option will only work properly if the Java server already accounts for Geyser clients
# when sending transfer requests.
# Whether to pass the IP/Port sent by the Java server to the Bedrock client.
# This option will only work properly if the Java server already accounts for Geyser clients
# when sending transfer requests.
forward-original-target: false

# A map of Java IP/Port combinations to Geyser IP/Port combinations.
# If you do not specify a port with a ":<port>" addition,
# it will use the default ports for Bedrock/Java respectively.
# A map of Java IP/Port combinations to Geyser IP/Port combinations.
# If you do not specify a port with a ":<port>" addition,
# it will use the default ports for Bedrock/Java respectively.
transfer-mappings:
127.0.0.1:25565: 127.0.0.1:19132
javaip.com: bedrockip.com

# Whether to add a '/transfertool transfer <server>' command to the server that can only be used by Bedrock players.
# This uses the "server-names" below. Command permission: "transfertool.command.transfer".
# Whether to add a '/transfertool transfer <server>' command to the server that can only be used by Bedrock players.
# This uses the "server-names" below. Command permission: "transfertool.command.transfer".

# Alternatively, users can transfer to any IP and Port combination if they additionally have the
# "transfertool.command.transfer.any" permission.

# Alternatively, users can transfer to any IP and Port combination if they additionally have the
# "transfertool.command.transfer.any" permission.
# Further, if ran without any server-name or IP/Port combination, it will open a menu.
add-transfer-command: false

# Allows configuring shortcuts for servers to use in the optional '/transfer <server>' command.
# Has no use while "add-transfer-command" is false.
# Allows configuring shortcuts for servers to use in the optional '/transfertool transfer <server>' command.
# Has no use while "add-transfer-command" is false.
#
# To restrict who can connect to which server, you can remove permissions from users.
# By default, all shortcuts are available to all players. The permissions follow the following format:
# "transfertool.shortcuts.<server>"
#
# For example, for the shortcut "lobby", it would be "transfertool.shortcuts.lobby".
transfer-shortcuts:
lobby: bedrockip.com
vanilla: 127.0.0.1:19132

# The default locale. For example, TransferTool's transfer shortcuts menu can be localized.
default-locale: en_US

# The config version. DO NOT CHANGE!
version: 1
```
Expand All @@ -71,9 +85,19 @@ Note for ipv6 addresses, you'll need to wrap the ipv6 with square brackets (see
Note:
If you enable `forward-original-target` and also provide server mappings, the server mappings will be checked first (and override the Java IP/port provided with the mapped Bedrock destination).

> [!TIP]
> If you're making use of `transfer-shortcuts`, or are enabling the transfer command,
> and are using Geyser on a platform without a default permissions system (e.g. Geyser-Velocity, -BungeeCord, and -Fabric),
> I'd recommend also installing [LuckLink](https://github.com/onebeastchris/LuckLink) to allow automatic registration of permissions.

# Installation
To install this Geyser extension, download the TransferTool.jar from the Releases tab, and add it to Geyser's extension folder.
One restart later, and this extension is ready to be used! To reload the config, you can use Geyser's reload command (`/geyser reload`).

# Localization
This extension has multi-language support. However, the default language is English (default-locale in the config) - to add more languages, copy the en_US.properties file from the `/extensions/transfertool/translations` folder, and translate the strings.
Then, save the file (e.g. de_DE.properties for German). You can select a default locale in the config.yml. Otherwise, all locales will be loaded & used automatically if a player's language matches.
To see all possible locales, check the Geyser locales [here](https://github.com/GeyserMC/languages/tree/master/texts).

# Getting Help
Help is provided via discord: https://discord.gg/WdmrRHRJhS. Alternatively, if you see errors or run into complications, feel free to open an issue here! Feature requests can also be made that way.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
id("com.gradleup.shadow") version "8.3.3"
}

version = "1.1.0"
version = "1.2.0"

repositories {
mavenCentral()
Expand Down
41 changes: 29 additions & 12 deletions src/main/java/dev/onechris/extension/transfertool/Config.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,66 @@
package dev.onechris.extension.transfertool;

import lombok.Getter;
import lombok.experimental.Accessors;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;

import java.util.Map;

@Getter
@Accessors(fluent = true)
@ConfigSerializable
public class Config {

@Comment("""
Whether to pass the IP/Port sent by the Java server to the Bedrock client.
This option will only work properly if the Java server already accounts for Geyser clients
when sending transfer requests.
Whether to pass the IP/Port sent by the Java server to the Bedrock client.
This option will only work properly if the Java server already accounts for Geyser clients
when sending transfer requests.
""")
private boolean forwardOriginalTarget = false;

@Comment("""
A map of Java IP/Port combinations to Geyser IP/Port combinations.
If you do not specify a port with a ":<port>" addition,
it will use the default ports for Bedrock/Java respectively.
A map of Java IP/Port combinations to Geyser IP/Port combinations.
If you do not specify a port with a ":<port>" addition,
it will use the default ports for Bedrock/Java respectively.
""")
private Map<String, String> transferMappings = Map.of(
"127.0.0.1:25565", "127.0.0.1:19132",
"javaip.com", "bedrockip.com");

@Comment("""
Whether to add a '/transfertool transfer <server>' command to the server that can only be used by Bedrock players.
This uses the "server-names" below. Command permission: "transfertool.command.transfer".
Whether to add a '/transfertool transfer <server>' command to the server that can only be used by Bedrock players.
This uses the "server-names" below. Command permission: "transfertool.command.transfer".
Alternatively, users can transfer to any IP and Port combination if they additionally have the
"transfertool.command.transfer.any" permission.
Alternatively, users can transfer to any IP and Port combination if they additionally have the
"transfertool.command.transfer.any" permission.
Further, if ran without any server-name or IP and Port combination, it will open a Cumulus menu.
This can be disabled by setting the "transfertool.command.transfer.menu" permission to "false".
""")
private boolean addTransferCommand = false;

@Comment("""
Allows configuring shortcuts for servers to use in the optional '/transfertool transfer <server>' command.
Has no use while "add-transfer-command" is false.
Allows configuring shortcuts for servers to use in the optional '/transfertool transfer <server>' command.
Has no use while "add-transfer-command" is false.
To restrict who can connect to which server, you can remove permissions from users.
By default, all shortcuts are available to all players. The permissions follow the following format:
"transfertool.shortcuts.<server>"
For example, for the shortcut "lobby", it would be "transfertool.shortcuts.lobby".
""")
private Map<String, String> transferShortcuts = Map.of(
"vanilla", "127.0.0.1:19132",
"lobby", "bedrockip.com"
);

@Comment("""
The default locale. For example, TransferTool's transfer shortcuts menu can be localized.
""")
private String defaultLocale = "en_US";

@Comment("""
The config version. DO NOT CHANGE!
""")
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/dev/onechris/extension/transfertool/Destination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dev.onechris.extension.transfertool;

import java.util.function.Consumer;

import static dev.onechris.extension.transfertool.TransferTool.logger;

public record Destination(String ip, int port) {

public boolean invalidIp() {
return this.ip == null || ip.isBlank();
}

public boolean invalidPort() {
return port < 0 || port > 65535;
}

public static Destination fromCombined(String input, int fallbackPort) {
return fromCombined(input, fallbackPort, logger::error);
}

public static Destination fromCombined(String input, int fallbackPort, Consumer<String> caller) {
String ip = input;
int port = fallbackPort;

if (input.contains(":")) {
String parsePort;
if (input.startsWith("[")) {
// Handle IPv6 addresses... since Bedrock technically supports these?
int closingBracketIndex = input.indexOf("]");
ip = input.substring(1, closingBracketIndex);
parsePort = input.substring(closingBracketIndex + 2);
} else {
// Handle IPv4 addresses or domain names
int colonIndex = input.lastIndexOf(":");
ip = input.substring(0, colonIndex);
parsePort = input.substring(colonIndex + 1);
}

try {
port = Integer.parseInt(parsePort);
} catch (NumberFormatException e) {
caller.accept("Invalid port found: " + parsePort + " in input: " + input + "! " +
"Defaulting to default port (" + fallbackPort + ").");
}
}

return new Destination(ip, port);
}

@Override
public String toString() {
return ip + ":" + port;
}
}
106 changes: 106 additions & 0 deletions src/main/java/dev/onechris/extension/transfertool/LanguageManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package dev.onechris.extension.transfertool;

import org.geysermc.geyser.api.command.CommandSource;
import org.geysermc.geyser.api.extension.ExtensionLogger;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.*;

public class LanguageManager {
public static String DEFAULT_LOCALE = "en_us";
private static final String EN_US_PROPERTIES = "en_US.properties";
public static final Map<String, Properties> LOCALE_PROPERTIES = new HashMap<>();
private final Config config;
private final ExtensionLogger logger;

public LanguageManager(Path languageFolder, Config config, ExtensionLogger logger) throws IOException {
this.config = config;
this.logger = logger;

// Ensure it exists
if (!languageFolder.toFile().exists()) {
if (!languageFolder.toFile().mkdirs()) {
throw new RuntimeException("Failed to create language folder!");
}
}

List<Path> languageFiles;
try {
languageFiles = new ArrayList<>(Files.list(languageFolder).toList());
} catch (IOException e) {
throw new RuntimeException("Failed to list language files!", e);
}

if (config.defaultLocale() != null) {
DEFAULT_LOCALE = config.defaultLocale().toLowerCase().replace(".properties", "");
}

//Check: Does default locale exist? Fallback to en_us if it does not.
if (languageFiles.stream().noneMatch(path -> path.getFileName().toString().equalsIgnoreCase(DEFAULT_LOCALE + ".properties"))) {

// Check: Is default locale not english?
if (!DEFAULT_LOCALE.equalsIgnoreCase("en_us")) {
logger.warning("Default configured locale " + DEFAULT_LOCALE + " not found, falling back to en_us.properties");
DEFAULT_LOCALE = "en_us";
}

try (InputStream input = TransferTool.class.getClassLoader().getResourceAsStream(EN_US_PROPERTIES)) {
assert input != null;
Path defaultLocalePath = languageFolder.resolve(EN_US_PROPERTIES);
Files.copy(input, defaultLocalePath, StandardCopyOption.REPLACE_EXISTING);
languageFiles.add(defaultLocalePath);
}
}

for (Path languageFile : languageFiles) {
if (!languageFile.toFile().isFile()) {
continue;
}

String fileName = languageFile.getFileName().toString();
if (!fileName.endsWith(".properties")) {
continue;
}

// Load the locale
try (InputStream localeStream = Files.newInputStream(languageFile)) {
Properties localeProp = new Properties();
try (InputStreamReader reader = new InputStreamReader(localeStream, StandardCharsets.UTF_8)) {
localeProp.load(reader);
} catch (Exception e) {
throw new AssertionError("Failed to load locale " + fileName);
} finally {
localeStream.close();
}

// Insert the locale into the mappings, all lowercase
LOCALE_PROPERTIES.put(fileName.substring(0, 5).toLowerCase(), localeProp);
}
}
}

public String getLocaleString(CommandSource source, String key) {
return getLocaleString(source.locale(), key);
}

public String getLocaleString(String key) {
return getLocaleString(config.defaultLocale(), key);
}

public String getLocaleString(String locale, String key) {
String translation = LOCALE_PROPERTIES.getOrDefault(locale.toLowerCase(), LOCALE_PROPERTIES.get(DEFAULT_LOCALE))
.getProperty(key);
if (translation != null) {
return translation;
} else {
logger.warning("No translation fallback found for translation key: " + key);
return key;
}
}
}
Loading

0 comments on commit 45d222c

Please sign in to comment.