Skip to content

Commit

Permalink
fix when prevent-kicking is ALWAYS can not kick a normal real player
Browse files Browse the repository at this point in the history
  • Loading branch information
tanyaofei committed Aug 2, 2024
1 parent 84dc446 commit 0b029da
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
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 {


String SELECTION = "fakeplayer:selection";

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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,7 +115,7 @@ public 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]")) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 0b029da

Please sign in to comment.