generated from GeyserMC/GeyserExampleExtension
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.2.0: Add transfer menu; localization system
- Loading branch information
1 parent
75b5f24
commit 45d222c
Showing
8 changed files
with
357 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 29 additions & 12 deletions
41
src/main/java/dev/onechris/extension/transfertool/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/dev/onechris/extension/transfertool/Destination.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
src/main/java/dev/onechris/extension/transfertool/LanguageManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.