From 0b029daa5c62e59642ab3aacc79c032b119c8f91 Mon Sep 17 00:00:00 2001 From: tanyaofei Date: Fri, 2 Aug 2024 19:36:40 +0800 Subject: [PATCH] fix when prevent-kicking is ALWAYS can not kick a normal real player --- .../core/constant/MetadataKeys.java | 14 +++++++++++++ .../fakeplayer/core/entity/FakePlayer.java | 3 ++- .../core/listener/FakeplayerListener.java | 20 ++++++++++--------- pom.xml | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/constant/MetadataKeys.java b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/constant/MetadataKeys.java index 475e416f..16d6b780 100644 --- a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/constant/MetadataKeys.java +++ b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/constant/MetadataKeys.java @@ -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(); + } } diff --git a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/entity/FakePlayer.java b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/entity/FakePlayer.java index ad00b55e..05ec867d 100644 --- a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/entity/FakePlayer.java +++ b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/entity/FakePlayer.java @@ -13,6 +13,7 @@ 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 FakePlayer( */ public CompletableFuture 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); diff --git a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/listener/FakeplayerListener.java b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/listener/FakeplayerListener.java index 56b23b79..886d78f1 100644 --- a/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/listener/FakeplayerListener.java +++ b/fakeplayer-core/src/main/java/io/github/hello09x/fakeplayer/core/listener/FakeplayerListener.java @@ -6,7 +6,7 @@ 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,18 +75,20 @@ public void onLogin(@NotNull PlayerLoginEvent event) { 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() - )); } + 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]")) { diff --git a/pom.xml b/pom.xml index 94e7f9dc..0e639caf 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ 21 UTF-8 - 0.3.6-rc.3 + 0.3.6