fix when prevent-kicking is ALWAYS can not kick a normal real player

This commit is contained in:
tanyaofei 2024-08-02 19:36:40 +08:00
parent 84dc446225
commit 0b029daa5c
4 changed files with 28 additions and 11 deletions

View File

@ -1,5 +1,10 @@
package io.github.hello09x.fakeplayer.core.constant;
import com.google.common.collect.Iterables;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public interface MetadataKeys {
@ -7,5 +12,14 @@ public interface MetadataKeys {
String REPLENISH = "fakeplayer:replenish";
String SPAWNED_AT = "fakeplayer:spawned_at";
static @Nullable Integer getSpawnedAt(@NotNull Player player) {
var value = Iterables.getFirst(player.getMetadata(SPAWNED_AT), null);
if (value == null) {
return null;
}
return value.asInt();
}
}

View File

@ -13,6 +13,7 @@ import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.config.Config;
import io.github.hello09x.fakeplayer.core.config.PreventKicking;
import io.github.hello09x.fakeplayer.core.constant.FakePlayerStatus;
import io.github.hello09x.fakeplayer.core.constant.MetadataKeys;
import io.github.hello09x.fakeplayer.core.manager.FakeplayerManager;
import io.github.hello09x.fakeplayer.core.manager.action.ActionManager;
import io.github.hello09x.fakeplayer.core.manager.naming.SequenceName;
@ -114,7 +115,7 @@ public class FakePlayer {
*/
public CompletableFuture<Void> spawnAsync(@NotNull SpawnOption option) {
var address = ipGen.next();
this.player.setMetadata(FakePlayerStatus.METADATA_KEY, new FixedMetadataValue(Main.getInstance(), FakePlayerStatus.SPAWNING));
this.player.setMetadata(MetadataKeys.SPAWNED_AT, new FixedMetadataValue(Main.getInstance(), Bukkit.getCurrentTick()));
return SchedulerUtils
.runTaskAsynchronously(Main.getInstance(), () -> {
var event = this.callPreLoginEvent(address);

View File

@ -6,7 +6,7 @@ import com.google.inject.Singleton;
import io.github.hello09x.devtools.core.utils.ComponentUtils;
import io.github.hello09x.fakeplayer.core.Main;
import io.github.hello09x.fakeplayer.core.config.Config;
import io.github.hello09x.fakeplayer.core.constant.FakePlayerStatus;
import io.github.hello09x.fakeplayer.core.constant.MetadataKeys;
import io.github.hello09x.fakeplayer.core.manager.FakeplayerManager;
import io.github.hello09x.fakeplayer.core.repository.UsedIdRepository;
import io.github.hello09x.fakeplayer.core.util.InternalAddressGenerator;
@ -75,19 +75,21 @@ public class FakeplayerListener implements Listener {
public void preventKicking(@NotNull PlayerKickEvent event) {
var player = event.getPlayer();
if (manager.isNotFake(event.getPlayer())) {
return;
}
switch (config.getPreventKicking()) {
case ON_SPAWNING -> {
if (player.getMetadata(FakePlayerStatus.METADATA_KEY)
.stream()
.anyMatch(metadata -> metadata.value() == FakePlayerStatus.SPAWNING)
) {
var spawnAt = MetadataKeys.getSpawnedAt(player);
if (spawnAt != null && Bukkit.getCurrentTick() - spawnAt < 20) {
event.setCancelled(true);
}
log.warning(String.format(
"Canceled kicking fake player '%s' on spawning due to your configuration",
player.getName()
));
}
}
case ALWAYS -> {
if (!ComponentUtils.toString(event.reason()).startsWith("[fakeplayer]")) {
event.setCancelled(true);

View File

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