Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset offline players on next spawn #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/main/java/dev/qixils/fahare/Fahare.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public final class Fahare extends JavaPlugin implements Listener {
private boolean autoReset = true;
private boolean anyDeath = false;
private int lives = 1;
private final List<UUID> playersToClear = new ArrayList<>();

private static @NotNull World overworld() {
return Objects.requireNonNull(Bukkit.getWorld(REAL_OVERWORLD_KEY), "Overworld not found");
Expand Down Expand Up @@ -164,7 +165,7 @@ public int getDeathsFor(UUID player) {
}

public void addDeathTo(UUID player) {
deaths.put(player, getDeathsFor(player)+1);
deaths.put(player, getDeathsFor(player) + 1);
}

public boolean isDead(UUID player) {
Expand Down Expand Up @@ -231,24 +232,30 @@ private void deleteNextWorld(List<World> worlds, @Nullable Path backupDestinatio
Bukkit.getScheduler().runTaskLater(this, () -> deleteNextWorld(worlds, backupDestination), 1);
}

private void resetPlayer(Player player, World world) {
player.setGameMode(GameMode.SPECTATOR);
player.getInventory().clear();
player.getEnderChest().clear();
player.setLevel(0);
player.setExp(0);
player.teleport(new Location(world, 0, 100, 0));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

world here is only used for their final destination, which is overridden by the new usage, so might as well just pass in the Location directly instead of the World. Could do the same for gamemode as well but that involves much less resources so not as worried about it.

player.setHealth(20);
player.setFoodLevel(20);
player.setSaturation(5);
}

public synchronized void reset() {
if (resetting)
return;
if (limboWorld == null)
return;
deaths.clear();
// teleport all players to limbo
Location destination = new Location(limboWorld, 0, 100, 0);
for (Player player : Bukkit.getOnlinePlayers()) {
player.setGameMode(GameMode.SPECTATOR);
player.getInventory().clear();
player.getEnderChest().clear();
player.setLevel(0);
player.setExp(0);
player.teleport(destination);
player.setHealth(20);
player.setFoodLevel(20);
player.setSaturation(5);
resetPlayer(player, limboWorld);
}
for (OfflinePlayer player : Bukkit.getOfflinePlayers()) {
playersToClear.add(player.getUniqueId());
}
// check if worlds are ticking
if (Bukkit.isTickingWorlds()) {
Expand Down Expand Up @@ -345,12 +352,13 @@ public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if (player.getWorld().getKey().equals(REAL_OVERWORLD_KEY))
player.teleport(fakeOverworld().getSpawnLocation());
}

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerRespawn(PlayerRespawnEvent event) {
Location destination = event.getRespawnLocation();
if (destination.getWorld().getKey().equals(REAL_OVERWORLD_KEY))
event.setRespawnLocation(fakeOverworld().getSpawnLocation());
Comment on lines -350 to -354
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

if (playersToClear.contains(player.getUniqueId())) {
playersToClear.remove(player.getUniqueId());
resetPlayer(player, Bukkit.getWorld(limboWorldKey));
World overworld = fakeOverworld();
Location spawn = overworld.getSpawnLocation();
player.setGameMode(GameMode.SURVIVAL);
player.teleport(spawn);
}
}
}