Fix handling offline players

This commit is contained in:
Mathieu Strypsteen 2024-09-14 20:27:13 +02:00
parent 2712ee60b3
commit e745193228
2 changed files with 20 additions and 11 deletions

View file

@ -2,11 +2,9 @@ package gent.zeus.mc13dtl.gamestate;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import gent.zeus.mc13dtl.MC13DTL;
import io.papermc.paper.command.brigadier.BasicCommand;
@ -24,15 +22,8 @@ public class GameStateCommand implements BasicCommand {
}
world.getPersistentDataContainer().set(key, PersistentDataType.STRING, "active");
GameStateUtil.loadWorld(world);
for (OfflinePlayer i : Bukkit.getOfflinePlayers()) {
Player player = i.getPlayer();
player.getInventory().clear();
player.heal(100);
player.getActivePotionEffects().clear();
for (PotionEffect effect : player.getActivePotionEffects()) {
player.removePotionEffect(effect.getType());
}
GameStateUtil.loadPlayer(player);
for (Player i : Bukkit.getOnlinePlayers()) {
GameStateUtil.processPlayer(i);
}
}

View file

@ -8,6 +8,7 @@ import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import gent.zeus.mc13dtl.MC13DTL;
import net.md_5.bungee.api.ChatColor;
@ -41,9 +42,26 @@ public class GameStateUtil {
player.sendMessage(ChatColor.DARK_PURPLE + "Game hasn't started yet, please wait...");
}
if (gameState.equals("active")) {
NamespacedKey processedKey = new NamespacedKey(MC13DTL.instance, "player-processed");
if (!player.getPersistentDataContainer().getOrDefault(processedKey, PersistentDataType.BOOLEAN, false)) {
processPlayer(player);
return;
}
player.setGameMode(GameMode.SURVIVAL);
player.setInvulnerable(false);
player.sendMessage(ChatColor.DARK_PURPLE + "Game is active, have fun!");
}
}
static void processPlayer(Player player) {
player.getInventory().clear();
player.heal(100);
player.getActivePotionEffects().clear();
for (PotionEffect effect : player.getActivePotionEffects()) {
player.removePotionEffect(effect.getType());
}
NamespacedKey key = new NamespacedKey(MC13DTL.instance, "player-processed");
player.getPersistentDataContainer().set(key, PersistentDataType.BOOLEAN, true);
loadPlayer(player);
}
}