Skip to content

Commit

Permalink
Added automatic detection of player languages
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Mar 11, 2022
1 parent a35ad2a commit 1aec8de
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 2 deletions.
Expand Up @@ -543,6 +543,11 @@ public interface SettingsManager {
*/
long getRecalcTaskTimeout();

/**
* Whether to detect the player's language automatically when he first joins the server.
* Config-path: auto-language-detection
*/
boolean isAutoLanguageDetection();

interface Database {

Expand Down
Expand Up @@ -182,6 +182,7 @@ public final class SettingsContainer {
public final double chargeOnWarp;
public final boolean publicWarps;
public final long recalcTaskTimeout;
public final boolean autoLanguageDetection;

public SettingsContainer(SuperiorSkyblockPlugin plugin, YamlConfiguration config) throws HandlerLoadException {
databaseType = config.getString("database.type");
Expand Down Expand Up @@ -471,6 +472,7 @@ else if (sections.length == 3)
chargeOnWarp = config.getDouble("charge-on-warp", 0D);
publicWarps = config.getBoolean("public-warps");
recalcTaskTimeout = config.getLong("recalc-task-timeout");
autoLanguageDetection = config.getBoolean("auto-language-detection", true);
}

private List<String> loadInteractables(SuperiorSkyblockPlugin plugin) {
Expand Down
Expand Up @@ -513,6 +513,11 @@ public long getRecalcTaskTimeout() {
return this.container.recalcTaskTimeout;
}

@Override
public boolean isAutoLanguageDetection() {
return this.container.autoLanguageDetection;
}

public void updateValue(String path, Object value) throws IOException {
SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();
File file = new File(plugin.getDataFolder(), "config.yml");
Expand Down
Expand Up @@ -19,7 +19,7 @@ public final class PlayerLocales {

private static final Pattern RTL_LOCALE_PATTERN = Pattern.compile(
"^(ar|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Arab|Hebr|Thaa|Nkoo|Tfng))(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)");
private static final Pattern LOCALE_PATTERN = Pattern.compile("^[a-z]{2}[_|-][A-Z]{2}$");
private static final Pattern LOCALE_PATTERN = Pattern.compile("^[a-zA-Z]{2}[_|-][a-zA-Z]{2}$");

private static final Set<Locale> locales = new HashSet<>();
private static final Set<UUID> noInteractMessages = new HashSet<>();
Expand Down
Expand Up @@ -70,6 +70,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -149,6 +150,15 @@ public void onPlayerJoin(PlayerJoinEvent e) {
}
}, 10L);

if (plugin.getSettings().isAutoLanguageDetection() && !e.getPlayer().hasPlayedBefore()) {
Executor.sync(() -> superiorPlayer.runIfOnline(player -> {
Locale playerLocale = plugin.getNMSPlayers().getPlayerLocale(player);
if (playerLocale != null && PlayerLocales.isValidLocale(playerLocale)) {
superiorPlayer.setUserLocale(playerLocale);
}
}), 2L);
}

Executor.async(() -> superiorPlayer.runIfOnline(player -> {
java.util.Locale locale = superiorPlayer.getUserLocale();
if (!Message.GOT_INVITE.isEmpty(locale)) {
Expand Down
Expand Up @@ -6,6 +6,9 @@
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Locale;

public interface NMSPlayers {

void clearInventory(OfflinePlayer offlinePlayer);
Expand All @@ -20,4 +23,7 @@ public interface NMSPlayers {

boolean wasThrownByPlayer(Item item, Player player);

@Nullable
Locale getPlayerLocale(Player player);

}
7 changes: 6 additions & 1 deletion src/main/resources/config.yml
Expand Up @@ -738,4 +738,9 @@ public-warps: false

# Timeout for the recalculate task, in seconds.
# If you want to disable the timeout, set this to 0 or below.
recalc-task-timeout: 10
recalc-task-timeout: 10

# Detect the player's language automatically when he first joins the server.
# The language will only get changed if there is a valid translation available,
# otherwise the default language will be chosen for the player.
auto-language-detection: true
Expand Up @@ -2,6 +2,7 @@

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.nms.NMSPlayers;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
Expand All @@ -22,6 +23,8 @@
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Optional;

public final class NMSPlayersImpl implements NMSPlayers {
Expand Down Expand Up @@ -87,4 +90,14 @@ public boolean wasThrownByPlayer(org.bukkit.entity.Item item, Player player) {
return entity instanceof EntityItem && player.getName().equals(((EntityItem) entity).n());
}

@Nullable
@Override
public Locale getPlayerLocale(Player player) {
try {
return PlayerLocales.getLocale(player.getLocale());
} catch (IllegalArgumentException error) {
return null;
}
}

}
@@ -1,7 +1,9 @@
package com.bgsoftware.superiorskyblock.nms.v1_16_R3;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.nms.NMSPlayers;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
Expand All @@ -24,10 +26,13 @@
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Optional;

public final class NMSPlayersImpl implements NMSPlayers {

private static final ReflectMethod<Locale> PLAYER_LOCALE = new ReflectMethod<>(Player.class, "locale");
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

@Override
Expand Down Expand Up @@ -92,4 +97,17 @@ public boolean wasThrownByPlayer(Item item, Player player) {
return entity instanceof EntityItem && player.getUniqueId().equals(((EntityItem) entity).getThrower());
}

@Nullable
@Override
public Locale getPlayerLocale(Player player) {
if (PLAYER_LOCALE.isValid()) {
return player.locale();
} else try {
//noinspection deprecation
return PlayerLocales.getLocale(player.getLocale());
} catch (IllegalArgumentException error) {
return null;
}
}

}
@@ -1,7 +1,9 @@
package com.bgsoftware.superiorskyblock.nms.v1_17_R1;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.nms.NMSPlayers;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
Expand All @@ -23,10 +25,13 @@
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Optional;

public final class NMSPlayersImpl implements NMSPlayers {

private static final ReflectMethod<Locale> PLAYER_LOCALE = new ReflectMethod<>(Player.class, "locale");
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

@Override
Expand Down Expand Up @@ -91,4 +96,17 @@ public boolean wasThrownByPlayer(Item item, Player player) {
return entity instanceof EntityItem && player.getUniqueId().equals(((EntityItem) entity).getThrower());
}

@Nullable
@Override
public Locale getPlayerLocale(Player player) {
if (PLAYER_LOCALE.isValid()) {
return player.locale();
} else try {
//noinspection deprecation
return PlayerLocales.getLocale(player.getLocale());
} catch (IllegalArgumentException error) {
return null;
}
}

}
@@ -1,7 +1,9 @@
package com.bgsoftware.superiorskyblock.nms.v1_18_R1;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.nms.NMSPlayers;
import com.bgsoftware.superiorskyblock.nms.v1_18_R1.mapping.level.WorldServer;
import com.bgsoftware.superiorskyblock.nms.v1_18_R1.mapping.world.entity.Entity;
Expand All @@ -22,10 +24,13 @@
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Optional;

public final class NMSPlayersImpl implements NMSPlayers {

private static final ReflectMethod<Locale> PLAYER_LOCALE = new ReflectMethod<>(Player.class, "locale");
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

@Override
Expand Down Expand Up @@ -92,4 +97,17 @@ public boolean wasThrownByPlayer(Item item, Player player) {
return entity.getHandle() instanceof EntityItem && player.getUniqueId().equals(entity.getThrower());
}

@Nullable
@Override
public Locale getPlayerLocale(Player player) {
if (PLAYER_LOCALE.isValid()) {
return player.locale();
} else try {
//noinspection deprecation
return PlayerLocales.getLocale(player.getLocale());
} catch (IllegalArgumentException error) {
return null;
}
}

}
@@ -1,7 +1,9 @@
package com.bgsoftware.superiorskyblock.nms.v1_18_R2;

import com.bgsoftware.common.reflection.ReflectMethod;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.nms.NMSPlayers;
import com.bgsoftware.superiorskyblock.nms.v1_18_R2.mapping.level.WorldServer;
import com.bgsoftware.superiorskyblock.nms.v1_18_R2.mapping.world.entity.Entity;
Expand All @@ -22,10 +24,13 @@
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Optional;

public final class NMSPlayersImpl implements NMSPlayers {

private static final ReflectMethod<Locale> PLAYER_LOCALE = new ReflectMethod<>(Player.class, "locale");
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

@Override
Expand Down Expand Up @@ -92,4 +97,17 @@ public boolean wasThrownByPlayer(Item item, Player player) {
return entity.getHandle() instanceof EntityItem && player.getUniqueId().equals(entity.getThrower());
}

@Nullable
@Override
public Locale getPlayerLocale(Player player) {
if (PLAYER_LOCALE.isValid()) {
return player.locale();
} else try {
//noinspection deprecation
return PlayerLocales.getLocale(player.getLocale());
} catch (IllegalArgumentException error) {
return null;
}
}

}
Expand Up @@ -2,6 +2,7 @@

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.lang.PlayerLocales;
import com.bgsoftware.superiorskyblock.nms.NMSPlayers;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
Expand All @@ -24,6 +25,8 @@
import org.bukkit.craftbukkit.v1_8_R3.util.CraftChatMessage;
import org.bukkit.entity.Player;

import javax.annotation.Nullable;
import java.util.Locale;
import java.util.Optional;

public final class NMSPlayersImpl implements NMSPlayers {
Expand Down Expand Up @@ -103,4 +106,14 @@ public boolean wasThrownByPlayer(org.bukkit.entity.Item item, Player player) {
return entity instanceof EntityItem && player.getName().equals(((EntityItem) entity).n());
}

@Nullable
@Override
public Locale getPlayerLocale(Player player) {
try {
return PlayerLocales.getLocale(player.spigot().getLocale());
} catch (IllegalArgumentException error) {
return null;
}
}

}

0 comments on commit 1aec8de

Please sign in to comment.