Skip to content

Commit

Permalink
add initNameFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
CaaMoe committed May 12, 2024
1 parent a050a51 commit 4510bff
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package moe.caa.multilogin.bukkit.injector

import com.mojang.authlib.minecraft.MinecraftSessionService
import moe.caa.multilogin.api.internal.injector.Injector
import moe.caa.multilogin.api.internal.logger.LoggerProvider
import moe.caa.multilogin.api.internal.main.MultiCoreAPI
import moe.caa.multilogin.bukkit.injector.protocol.PacketHandler
import moe.caa.multilogin.bukkit.injector.proxy.SignatureValidatorInvocationHandler
import moe.caa.multilogin.bukkit.injector.proxy.YggdrasilMinecraftSessionServiceInvocationHandler
import moe.caa.multilogin.bukkit.main.MultiLoginBukkit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ public Signal run(ValidateContext validateContext) {
}
}

String fixName = loginName;
String sourceName = loginName;

String fixName = validateContext.getBaseServiceAuthenticationResult().getServiceConfig().generateName(sourceName);
if (core.getPluginConfig().isNameCorrect()) {
int i = 0;
boolean modified = false;
UUID ownerUUID;
while ((ownerUUID = core.getSqlManager().getInGameProfileTable().getInGameUUIDIgnoreCase(fixName)) != null) {
if(ownerUUID.equals(inGameUUID)) break;
fixName = loginName + ++i;
sourceName = incrementString(sourceName);
fixName = validateContext.getBaseServiceAuthenticationResult().getServiceConfig().generateName(sourceName);
modified = true;
}

if(modified){
UUID finalInGameUUID = inGameUUID;
String finalFixName = fixName;
Expand Down Expand Up @@ -126,4 +129,20 @@ public Signal run(ValidateContext validateContext) {
}
}
}

private String incrementString(String source){
if (source.isEmpty()) return "1";

char c = source.charAt(source.length() - 1);
if (Character.isDigit(c)) {
int i = Character.getNumericValue(c);
if(i == 9){
return incrementString(source.substring(0, source.length() - 1)) + "0";
} else {
return source.substring(0, source.length() - 1) + (i + 1);
}
}

return source + "1";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ private BaseServiceConfig readServiceConfig(CommentedConfigurationNode load) thr
boolean whitelist = load.node("whitelist").getBoolean(false);
SkinRestorerConfig skinRestorer = SkinRestorerConfig.read(load.node("skinRestorer"));

String initNameFormat = load.node("initNameFormat").getString("{name}");

if (serviceType.isYggdrasilService()) {
CommentedConfigurationNode yggdrasilAuthNode = load.node("yggdrasilAuth");
boolean trackIp = yggdrasilAuthNode.node("trackIp").getBoolean(false);
Expand Down Expand Up @@ -218,14 +220,14 @@ private BaseServiceConfig readServiceConfig(CommentedConfigurationNode load) thr
String trackIpContent = customNode.node("trackIpContent").getString();
String postContent = customNode.node("postContent").getString();

return new CustomYggdrasilServiceConfig(id, name, initUUID, whitelist,
return new CustomYggdrasilServiceConfig(id, name, initUUID, initNameFormat, whitelist,
skinRestorer, trackIp, timeout, retry, retryDelay,
authProxy, url, postContent, trackIpContent, method);
}
}

if (serviceType == ServiceType.FLOODGATE) {
return new FloodgateServiceConfig(id, name, initUUID, whitelist, skinRestorer);
return new FloodgateServiceConfig(id, name, initUUID, initNameFormat, whitelist, skinRestorer);
}

throw new ConfException("Unknown service type " + serviceType.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ public abstract class BaseServiceConfig implements IService {
private final int id;
private final String name;
private final InitUUID initUUID;
private final String initNameFormat;
private final boolean whitelist;
private final SkinRestorerConfig skinRestorer;

protected BaseServiceConfig(int id, String name, InitUUID initUUID,
protected BaseServiceConfig(int id, String name, InitUUID initUUID, String initNameFormat,
boolean whitelist, SkinRestorerConfig skinRestorer) throws ConfException {
this.id = id;
this.name = name;
this.initUUID = initUUID;
this.initNameFormat = initNameFormat;
this.whitelist = whitelist;
this.skinRestorer = skinRestorer;

Expand All @@ -38,6 +40,10 @@ protected void checkValid() throws ConfException {
));
}

public String generateName(String loginName){
return initNameFormat.replace("{name}", loginName);
}

@Override
public int getServiceId() {
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.jetbrains.annotations.NotNull;

public class FloodgateServiceConfig extends BaseServiceConfig {
public FloodgateServiceConfig(int id, String name, InitUUID initUUID,
public FloodgateServiceConfig(int id, String name, InitUUID initUUID, String initNameFormat,
boolean whitelist, SkinRestorerConfig skinRestorer) throws ConfException {
super(id, name, initUUID, whitelist, skinRestorer);
super(id, name, initUUID, initNameFormat, whitelist, skinRestorer);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public abstract class BaseYggdrasilServiceConfig extends BaseServiceConfig {
private final long retryDelay;
private final ProxyConfig authProxy;

protected BaseYggdrasilServiceConfig(int id, String name, InitUUID initUUID, boolean whitelist, SkinRestorerConfig skinRestorer,
protected BaseYggdrasilServiceConfig(int id, String name, InitUUID initUUID, String initNameFormat, boolean whitelist, SkinRestorerConfig skinRestorer,
boolean trackIp, int timeout, int retry, long retryDelay, ProxyConfig authProxy) throws ConfException {
super(id, name, initUUID, whitelist, skinRestorer);
super(id, name, initUUID, initNameFormat, whitelist, skinRestorer);
this.trackIp = trackIp;
this.timeout = timeout;
this.retry = retry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class CustomYggdrasilServiceConfig extends BaseYggdrasilServiceConfig {
private final String trackIpContent;
private final HttpRequestMethod method;

public CustomYggdrasilServiceConfig(int id, String name, InitUUID initUUID, boolean whitelist, SkinRestorerConfig skinRestorer, boolean trackIp, int timeout, int retry, long retryDelay, ProxyConfig authProxy, String url, String postContent, String trackIpContent, HttpRequestMethod method) throws ConfException {
super(id, name, initUUID, whitelist, skinRestorer, trackIp, timeout, retry, retryDelay, authProxy);
public CustomYggdrasilServiceConfig(int id, String name, InitUUID initUUID, String initNameFormat, boolean whitelist, SkinRestorerConfig skinRestorer, boolean trackIp, int timeout, int retry, long retryDelay, ProxyConfig authProxy, String url, String postContent, String trackIpContent, HttpRequestMethod method) throws ConfException {
super(id, name, initUUID, initNameFormat, whitelist, skinRestorer, trackIp, timeout, retry, retryDelay, authProxy);
this.url = url;
this.postContent = postContent;
this.trackIpContent = trackIpContent;
Expand Down
6 changes: 4 additions & 2 deletions core/src/main/resources/examples/floodgate.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Below, only the most basic configuration is provided.
# You can refer to the template file to complete all configurations.

# Before enabling the service, you need to set floodgateSupport to true in config.yml, otherwise it cannot be used.
Expand All @@ -8,4 +7,7 @@ id: -1

name: 'Bedrock'
# Don't change it unless you really want to.
serviceType: FLOODGATE
serviceType: FLOODGATE

# Please edit before use.
initNameFormat: 'BE_{name}'
11 changes: 11 additions & 0 deletions core/src/main/resources/examples/template_cn_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ yggdrasilAuth:
# 默认值 ‘DEFAULT’
initUUID: 'DEFAULT'

# 设置玩家第一次登录到这个服务器时,系统所分配给玩家的 Name 生成模板。
#
# 例子:
# "BE_{name}", 当玩家 Steve445 第一次通过此 service 进来时,系统将分配 “BE_Steve445” 用户名给他。
#
# 搭配 nameCorrect 使用可以做到以下效果:
# 如果 initNameFormat 值为 abc10001, 则分配到的用户名将会从abc10001递增到abc99999, 比如(abc10001, abc10002, abc10003)
#
# 默认值 ’{name}‘
initNameFormat: '{name}'

# 是否为当前验证服务的玩家单独开启白名单功能。
#
# 默认值 ‘false’
Expand Down

0 comments on commit 4510bff

Please sign in to comment.