Supports more options for prevent kicking

This commit is contained in:
tanyaofei 2024-07-26 14:37:22 +08:00
parent 0974ac5fd9
commit d5c6bae86c
5 changed files with 77 additions and 28 deletions

View File

@ -32,7 +32,7 @@ public class FakeplayerConfig extends Config<FakeplayerConfig> {
log = Main.getInstance().getLogger();
instance = new FakeplayerConfig(
Main.getInstance(),
"14"
"15"
);
}
@ -101,11 +101,6 @@ public class FakeplayerConfig extends Config<FakeplayerConfig> {
*/
private Pattern namePattern;
/**
* 登陆时防止被踢
*/
private boolean preventKickedOnSpawning;
/**
* 检测更新
*/
@ -127,6 +122,11 @@ public class FakeplayerConfig extends Config<FakeplayerConfig> {
*/
private boolean debug;
/**
* 防止踢出
*/
private PreventKicking preventKicking;
public FakeplayerConfig(@NotNull JavaPlugin plugin, @NotNull String version) {
super(plugin, version);
reload(false);
@ -152,7 +152,7 @@ public class FakeplayerConfig extends Config<FakeplayerConfig> {
this.kickOnDead = file.getBoolean("kick-on-dead", true);
this.checkForUpdates = file.getBoolean("check-for-updates", true);
this.namePattern = getNamePattern(file);
this.preventKickedOnSpawning = file.getBoolean("prevent-kicked-on-spawning", false);
this.preventKicking = this.getPreventKicking(file);
this.nameTemplate = getNameTemplate(file);
this.lifespan = getLifespan(file);
this.allowCommands = file.getStringList("allow-commands")
@ -190,4 +190,13 @@ public class FakeplayerConfig extends Config<FakeplayerConfig> {
return tmpl;
}
private @NotNull PreventKicking getPreventKicking(@NotNull FileConfiguration file) {
if (file.getBoolean("prevent-kicked-on-spawning", false)) {
log.warning("prevent-kicked-on-spawning is deprecated, use prevent-kick instead");
return PreventKicking.ON_SPAWNING;
}
return PreventKicking.valueOf(file.getString("prevent-kicking", PreventKicking.NEVER.toString()));
}
}

View File

@ -0,0 +1,24 @@
package io.github.hello09x.fakeplayer.core.config;
/**
* @author tanyaofei
* @since 2024/7/26
**/
public enum PreventKicking {
/**
* 不进行任何处理
*/
NEVER,
/**
* 创建时
*/
ON_SPAWNING,
/**
* 永远, 除了假人插件自身
*/
ALWAYS
}

View File

@ -4,6 +4,7 @@ import com.google.common.base.Throwables;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import io.github.hello09x.bedrock.i18n.I18n;
import io.github.hello09x.bedrock.util.Components;
import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.config.FakeplayerConfig;
import io.github.hello09x.fakeplayer.core.constant.FakePlayerStatus;
@ -73,22 +74,31 @@ public class FakeplayerListener implements Listener {
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void preventBeingKickedOnSpawning(@NotNull PlayerKickEvent event) {
public void preventKicking(@NotNull PlayerKickEvent event) {
var player = event.getPlayer();
if (!config.isPreventKickedOnSpawning()) {
return;
}
if (player.getMetadata(FakePlayerStatus.METADATA_KEY)
.stream()
.anyMatch(metadata -> metadata.value() == FakePlayerStatus.SPAWNING)
) {
event.setCancelled(true);
log.warning(String.format(
"Fake player '%s' was attempting to be kicked during the spawning which will be canceled cause you are enabled 'prevent-kicked-on-spawning'",
player.getName())
);
switch (config.getPreventKicking()) {
case ON_SPAWNING -> {
if (player.getMetadata(FakePlayerStatus.METADATA_KEY)
.stream()
.anyMatch(metadata -> metadata.value() == FakePlayerStatus.SPAWNING)
) {
event.setCancelled(true);
log.warning(String.format(
"Canceled kicking fake player '%s' on spawning due to your configuration",
player.getName()
));
}
}
case ALWAYS -> {
if (!Components.asString(event.reason()).startsWith("[fakeplayer]")) {
event.setCancelled(true);
log.warning(String.format(
"Canceled kicking fake player '%s' due to your configuration",
player.getName()
));
}
}
}
}

View File

@ -1,6 +1,6 @@
# 配置文件版本, 不要修改这个值
# THE VERSION OF THIS CONFIG FILE, DO NOT MODIFY IT
version: 14
version: 15
# 多国语言配置
# 可选项: zh, en
@ -64,11 +64,17 @@ name-template: ''
# 3. 如果你改了正则表达式, 请确保它以 `^` 开头并且以 `$` 结尾
name-pattern: '^[a-zA-Z0-9_]+$'
# 防止假人在登陆过程中由于其他插件的不兼容问题踢掉假人,
# 开启这个选项可能可以解决一些登陆插件的兼容问题, 但是插件本身应该还是会打印错误日志
# Prevent fake players from being kicked during spawning. Enabling this option may resolve some compatibility issues with login plugins.
# But, these plugins may still print some error logs.
prevent-kicked-on-spawning: false
# 防止假人被其他插件踢掉, 这个选项用来兼容一些插件因为某些问题而踢掉假人
# 可选项:
# NEVER: 不进行任何处理
# ON_SPAWNING: 创建时防止被踢出, 某些登陆插件会在加入时踢掉, 但不支持某些插件会在下 1 tick 时才踢的情况
# ALWAYS: 永远, 即除了本插件以外任何插件包括 /kick 命令都无法将假人踢掉
# Prevent some plugins kick our fake players, enabling this option may resolve some compatibility issues with login plugins.
# Options:
# NEVER: do not prevent kicking
# ON_SPAWNING: only prevent kicking when fake players is spawning, but some plugin will kick them on the next tick, in such causes you should use `ALWAYS`
# ALWAYS: always prevent kicking from other plugins and command `/kick`
prevent-kicking: NEVER
# 跟随下线
# 假人创建者下线时是否也跟着下线

View File

@ -24,7 +24,7 @@
<properties>
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<revision>0.3.5</revision>
<revision>0.3.6-rc1</revision>
</properties>
<repositories>