Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/main/java/fr/xephi/authme/AuthMe.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import fr.xephi.authme.service.BackupService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.MigrationService;
import fr.xephi.authme.service.SpectateLoginService;
import fr.xephi.authme.service.bungeecord.BungeeReceiver;
import fr.xephi.authme.service.yaml.YamlParseException;
import fr.xephi.authme.settings.Settings;
Expand Down Expand Up @@ -316,6 +317,8 @@ public void onDisable() {
// Wait for tasks and close data source
new TaskCloser(this, database).run();

injector.getIfAvailable(SpectateLoginService.class).removeArmorstands();

// Disabled correctly
Consumer<String> infoLogMethod = logger == null ? getLogger()::info : logger::info;
infoLogMethod.accept("AuthMe " + this.getDescription().getVersion() + " disabled!");
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/fr/xephi/authme/data/limbo/LimboPlayer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.xephi.authme.data.limbo;

import fr.xephi.authme.task.MessageTask;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.scheduler.BukkitTask;

Expand All @@ -22,18 +23,20 @@ public class LimboPlayer {
private final Location loc;
private final float walkSpeed;
private final float flySpeed;
private final GameMode gameMode;
private BukkitTask timeoutTask = null;
private MessageTask messageTask = null;
private LimboPlayerState state = LimboPlayerState.PASSWORD_REQUIRED;

public LimboPlayer(Location loc, boolean operator, Collection<UserGroup> groups, boolean fly, float walkSpeed,
float flySpeed) {
float flySpeed, GameMode gameMode) {
this.loc = loc;
this.operator = operator;
this.groups = new ArrayList<>(groups); // prevent bug #2413
this.canFly = fly;
this.walkSpeed = walkSpeed;
this.flySpeed = flySpeed;
this.gameMode = gameMode;
}

/**
Expand All @@ -45,6 +48,15 @@ public Location getLocation() {
return loc;
}

/**
* Return the player's original gamemode.
*
* @return The player's gamemode
*/
public GameMode getGameMode() {
return gameMode;
}

/**
* Return whether the player is an operator or not (i.e. whether he is an OP).
*
Expand Down
1 change: 1 addition & 0 deletions src/main/java/fr/xephi/authme/data/limbo/LimboService.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public void restoreData(Player player) {
logger.debug("No LimboPlayer found for `{0}` - cannot restore", lowerName);
} else {
player.setOp(limbo.isOperator());
player.setGameMode(limbo.getGameMode());
settings.getProperty(RESTORE_ALLOW_FLIGHT).restoreAllowFlight(player, limbo);
settings.getProperty(RESTORE_FLY_SPEED).restoreFlySpeed(player, limbo);
settings.getProperty(RESTORE_WALK_SPEED).restoreWalkSpeed(player, limbo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.LimboSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;

import javax.inject.Inject;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static fr.xephi.authme.util.Utils.isCollectionEmpty;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -44,6 +46,7 @@ LimboPlayer createLimboPlayer(Player player, boolean isRegistered, Location loca
boolean flyEnabled = player.getAllowFlight();
float walkSpeed = player.getWalkSpeed();
float flySpeed = player.getFlySpeed();
GameMode gameMode = player.getGameMode();
Collection<UserGroup> playerGroups = permissionsManager.hasGroupSupport()
? permissionsManager.getGroups(player) : Collections.emptyList();

Expand All @@ -52,7 +55,7 @@ LimboPlayer createLimboPlayer(Player player, boolean isRegistered, Location loca
.collect(toList());

logger.debug("Player `{0}` has groups `{1}`", player.getName(), String.join(", ", groupNames));
return new LimboPlayer(location, isOperator, playerGroups, flyEnabled, walkSpeed, flySpeed);
return new LimboPlayer(location, isOperator, playerGroups, flyEnabled, walkSpeed, flySpeed, gameMode);
}

/**
Expand Down Expand Up @@ -97,10 +100,11 @@ LimboPlayer merge(LimboPlayer newLimbo, LimboPlayer oldLimbo) {
boolean canFly = newLimbo.isCanFly() || oldLimbo.isCanFly();
float flySpeed = Math.max(newLimbo.getFlySpeed(), oldLimbo.getFlySpeed());
float walkSpeed = Math.max(newLimbo.getWalkSpeed(), oldLimbo.getWalkSpeed());
GameMode gameMode = Objects.isNull(newLimbo.getGameMode()) ? oldLimbo.getGameMode() : newLimbo.getGameMode();
Collection<UserGroup> groups = getLimboGroups(oldLimbo.getGroups(), newLimbo.getGroups());
Location location = firstNotNull(oldLimbo.getLocation(), newLimbo.getLocation());

return new LimboPlayer(location, isOperator, groups, canFly, walkSpeed, flySpeed);
return new LimboPlayer(location, isOperator, groups, canFly, walkSpeed, flySpeed, gameMode);
}

private static Location firstNotNull(Location first, Location second) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import fr.xephi.authme.data.limbo.LimboPlayer;
import fr.xephi.authme.data.limbo.UserGroup;
import fr.xephi.authme.service.BukkitService;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.World;

Expand All @@ -23,6 +24,7 @@

import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.CAN_FLY;
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.FLY_SPEED;
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.GAMEMODE;
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.GROUPS;
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.IS_OP;
import static fr.xephi.authme.data.limbo.persistence.LimboPlayerSerializer.LOCATION;
Expand Down Expand Up @@ -64,8 +66,10 @@ public LimboPlayer deserialize(JsonElement jsonElement, Type type, JsonDeseriali
boolean canFly = getBoolean(jsonObject, CAN_FLY);
float walkSpeed = getFloat(jsonObject, WALK_SPEED, LimboPlayer.DEFAULT_WALK_SPEED);
float flySpeed = getFloat(jsonObject, FLY_SPEED, LimboPlayer.DEFAULT_FLY_SPEED);
int gameModeId = getNumberFromElement(jsonObject.get(GAMEMODE), JsonElement::getAsInt, 0);
GameMode gameMode = GameMode.getByValue(gameModeId);

return new LimboPlayer(loc, operator, groups, canFly, walkSpeed, flySpeed);
return new LimboPlayer(loc, operator, groups, canFly, walkSpeed, flySpeed, gameMode);
}

private Location deserializeLocation(JsonObject jsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class LimboPlayerSerializer implements JsonSerializer<LimboPlayer> {
static final String CAN_FLY = "can-fly";
static final String WALK_SPEED = "walk-speed";
static final String FLY_SPEED = "fly-speed";
static final String GAMEMODE = "gamemode";

private static final Gson GSON = new Gson();

Expand Down Expand Up @@ -66,6 +67,7 @@ public JsonElement serialize(LimboPlayer limboPlayer, Type type, JsonSerializati
obj.addProperty(CAN_FLY, limboPlayer.isCanFly());
obj.addProperty(WALK_SPEED, limboPlayer.getWalkSpeed());
obj.addProperty(FLY_SPEED, limboPlayer.getFlySpeed());
obj.addProperty(GAMEMODE, limboPlayer.getGameMode().getValue());
return obj;
}
}
32 changes: 32 additions & 0 deletions src/main/java/fr/xephi/authme/listener/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fr.xephi.authme.service.AntiBotService;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.JoinMessageService;
import fr.xephi.authme.service.SpectateLoginService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.settings.Settings;
Expand All @@ -19,6 +20,7 @@
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -49,6 +51,8 @@
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerShearEntityEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerToggleSneakEvent;
import org.bukkit.inventory.InventoryView;

import javax.inject.Inject;
Expand Down Expand Up @@ -91,6 +95,8 @@ public class PlayerListener implements Listener {
private PermissionsManager permissionsManager;
@Inject
private QuickCommandsProtectionManager quickCommandsProtectionManager;
@Inject
private SpectateLoginService spectateLoginService;

// Lowest priority to apply fast protection checks
@EventHandler(priority = EventPriority.LOWEST)
Expand Down Expand Up @@ -376,6 +382,32 @@ public void onPlayerRespawn(PlayerRespawnEvent event) {
if (spawn != null && spawn.getWorld() != null) {
event.setRespawnLocation(spawn);
}

if (settings.getProperty(RestrictionSettings.SPECTATE_STAND_LOGIN)
&& spectateLoginService.hasStand(event.getPlayer())) {
bukkitService.runTaskLater(() -> spectateLoginService.createStand(event.getPlayer()), 1L);
Copy link
Member

@ljacqu ljacqu Sep 3, 2023

Choose a reason for hiding this comment

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

I can't confirm the comment about stands not being created if a player is dead but maybe I'm missing the relevant point, but interestingly the logic here specifies a stand should be created if the setting is enabled, or if the player has a stand? I don't think that makes much sense.

Copy link
Author

Choose a reason for hiding this comment

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

Removed unnecessary comment.

Copy link
Member

Choose a reason for hiding this comment

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

Now an armor stand is created for a player every time he respawns, regardless of whether an armor stand already exists or not

Copy link
Author

Choose a reason for hiding this comment

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

Done.

}
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onToggleSneak(PlayerToggleSneakEvent event) {
if (listenerService.shouldCancelEvent(event.getPlayer())
&& (settings.getProperty(RestrictionSettings.SPECTATE_STAND_LOGIN)
|| spectateLoginService.hasStand(event.getPlayer()))) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onTeleport(PlayerTeleportEvent event) {
if (listenerService.shouldCancelEvent(event.getPlayer())
&& event.getCause() == PlayerTeleportEvent.TeleportCause.SPECTATE
&& event.getPlayer().getGameMode() == GameMode.SPECTATOR
&& (settings.getProperty(RestrictionSettings.SPECTATE_STAND_LOGIN)
|| spectateLoginService.hasStand(event.getPlayer()))) {
spectateLoginService.updateTarget(event.getPlayer());
event.setCancelled(true);
}
}

/*
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/fr/xephi/authme/process/join/AsynchronousJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.PluginHookService;
import fr.xephi.authme.service.SessionService;
import fr.xephi.authme.service.SpectateLoginService;
import fr.xephi.authme.service.ValidationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
Expand Down Expand Up @@ -83,6 +84,9 @@ public class AsynchronousJoin implements AsynchronousProcess {
@Inject
private ProxySessionManager proxySessionManager;

@Inject
private SpectateLoginService spectateLoginService;

AsynchronousJoin() {
}

Expand Down Expand Up @@ -199,6 +203,13 @@ private void processJoinSync(Player player, boolean isAuthAvailable) {
int blindTimeOut = (registrationTimeout <= 0) ? 99999 : registrationTimeout;
player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, blindTimeOut, 2));
}

if (service.getProperty(RestrictionSettings.SPECTATE_STAND_LOGIN)) {
// The delay is necessary in order to make sure that the player is teleported to spawn
// and after authorization appears in the same place
bukkitService.runTaskLater(() -> spectateLoginService.createStand(player), 1);
}

commandManager.runCommandsOnJoin(player);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.JoinMessageService;
import fr.xephi.authme.service.SpectateLoginService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.settings.WelcomeMessageConfiguration;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;

Expand Down Expand Up @@ -58,6 +60,9 @@ public class ProcessSyncPlayerLogin implements SynchronousProcess {
@Inject
private PermissionsManager permissionsManager;

@Inject
private SpectateLoginService spectateLoginService;

ProcessSyncPlayerLogin() {
}

Expand Down Expand Up @@ -99,6 +104,11 @@ public void processPlayerLogin(Player player, boolean isFirstLogin, List<String>
player.removePotionEffect(PotionEffectType.BLINDNESS);
}

if (commonService.getProperty(RestrictionSettings.SPECTATE_STAND_LOGIN)
|| spectateLoginService.hasStand(player)) {
spectateLoginService.removeStand(player);
}

// The Login event now fires (as intended) after everything is processed
bukkitService.callEvent(new LoginEvent(player));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.SpectateLoginService;
import fr.xephi.authme.service.TeleportationService;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RegistrationSettings;
Expand Down Expand Up @@ -44,6 +45,9 @@ public class ProcessSyncPlayerLogout implements SynchronousProcess {
@Inject
private CommandManager commandManager;

@Inject
private SpectateLoginService spectateLoginService;

ProcessSyncPlayerLogout() {
}

Expand All @@ -65,6 +69,10 @@ public void processSyncLogout(Player player) {

service.send(player, MessageKey.LOGOUT_SUCCESS);
logger.info(player.getName() + " logged out");

if (service.getProperty(RestrictionSettings.SPECTATE_STAND_LOGIN)) {
spectateLoginService.createStand(player);
}
}

private void applyLogoutEffect(Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import fr.xephi.authme.data.limbo.LimboService;
import fr.xephi.authme.process.SynchronousProcess;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.SpectateLoginService;
import fr.xephi.authme.settings.commandconfig.CommandManager;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import org.bukkit.entity.Player;

import javax.inject.Inject;


public class ProcessSyncPlayerQuit implements SynchronousProcess {

@Inject
private CommonService service;

@Inject
private SpectateLoginService spectateLoginService;

@Inject
private LimboService limboService;

Expand All @@ -26,6 +35,11 @@ public void processSyncQuit(Player player, boolean wasLoggedIn) {
if (wasLoggedIn) {
commandManager.runCommandsOnLogout(player);
} else {
if (service.getProperty(RestrictionSettings.SPECTATE_STAND_LOGIN)
|| spectateLoginService.hasStand(player)) {
spectateLoginService.removeStand(player);
}

limboService.restoreData(player);
player.saveData(); // #1238: Speed is sometimes not restored properly
}
Expand Down
Loading