Skip to content

Commit

Permalink
Add locale change interval limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Jun 3, 2024
1 parent 85917ea commit 8b41b4b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/main/java/cn/dreeam/caeruleum/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,30 @@ public interface Config {
@AnnotationBasedSorter.Order(3)
List<String> mainLocale();

@ConfKey("i18nPerm.locale-change-interval-limit")
@ConfComments({
"The maximum interval for locale change, to prevent using packet exploit to lag server, locale change between this interval will be discard.",
"(unit: ms)"
})
@ConfDefault.DefaultInteger(1000)
@AnnotationBasedSorter.Order(4)
int localeChangeIntervalLimit();

@ConfKey("i18nPerm.locale-change-interval-limit-message")
@ConfComments({
"The message sent to player when reached the interval limit."
})
@ConfDefault.DefaultString("<red>You can not change locale so fast!")
@AnnotationBasedSorter.Order(5)
String localeChangeIntervalLimitMessage();

@ConfKey("i18nPerm.locale-list.fallback-locale")
@ConfComments({
"The fallback locale, if any unknown or unsupported locale found when player join/change locale,",
"CaeruleumCore will assign lang permission based on the fallback locale here."
})
@ConfDefault.DefaultString("en_US")
@AnnotationBasedSorter.Order(4)
@AnnotationBasedSorter.Order(6)
String localeBlackWhiteListFallback();

@ConfKey("i18nPerm.locale-list.use-whitelist")
Expand All @@ -68,7 +85,7 @@ public interface Config {
"Set to false to use blacklist mode."
})
@ConfDefault.DefaultBoolean(true)
@AnnotationBasedSorter.Order(5)
@AnnotationBasedSorter.Order(7)
boolean localeBlackWhiteListUseWhitelist();

@ConfKey("i18nPerm.locale-list.locales")
Expand All @@ -83,7 +100,7 @@ public interface Config {
"lzh",
"en_US"
})
@AnnotationBasedSorter.Order(6)
@AnnotationBasedSorter.Order(8)
List<String> localeBlackWhiteList();
}

12 changes: 11 additions & 1 deletion src/main/java/cn/dreeam/caeruleum/listener/LocaleChange.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import cn.dreeam.caeruleum.CaeruleumCore;
import cn.dreeam.caeruleum.utils.PermUtil;
import cn.dreeam.caeruleum.utils.Util;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLocaleChangeEvent;
Expand All @@ -13,11 +16,18 @@ public class LocaleChange implements Listener {

@EventHandler
public void onLocaleChange(PlayerLocaleChangeEvent e) {
// Should add threshold to prevent lag Or use async queue to execute all logic.
// Use threshold limit to prevent lag using packets.
if (Util.reachThreshold()) {
final Component message = MiniMessage.miniMessage().deserialize(CaeruleumCore.config.localeChangeIntervalLimitMessage());
e.getPlayer().sendMessage(message);
return;
}

String locale = e.locale().toString();
UUID uuid = e.getPlayer().getUniqueId();
List<String> langPerms = PermUtil.getLangPerm(uuid);

// Remove invalid perms
if (langPerms.size() > 1) {
PermUtil.clearLangPerm(uuid);
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/cn/dreeam/caeruleum/utils/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cn.dreeam.caeruleum.utils;

import cn.dreeam.caeruleum.CaeruleumCore;

public class Util {

private static long lastTime = System.nanoTime();

public static boolean reachThreshold() {
long now = System.nanoTime();
if ((now - lastTime) / 1_000_000 > CaeruleumCore.config.localeChangeIntervalLimit()) {
lastTime = now;
return true;
}

return false;
}
}

0 comments on commit 8b41b4b

Please sign in to comment.