Skip to content

Commit

Permalink
Merge pull request #63 from LielAmar/spawn-locations
Browse files Browse the repository at this point in the history
Spawn locations
  • Loading branch information
LielAmar authored Oct 15, 2021
2 parents 6a6c852 + 1546898 commit b109a36
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.lielamar</groupId>
<artifactId>2fa</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void setupUpdateChecker() {
private void registerListeners() {
PluginManager pm = Bukkit.getPluginManager();

pm.registerEvents(new OnAuthStateChange(), this);
pm.registerEvents(new OnAuthStateChange(this), this);
pm.registerEvents(new OnPlayerConnection(this), this);
pm.registerEvents(new DisabledEvents(this), this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlayerStateChangeEvent extends Event implements Cancellable {

Expand All @@ -16,11 +18,18 @@ public class PlayerStateChangeEvent extends Event implements Cancellable {

private final Player player;
private boolean cancelled = false;
private AuthHandler.AuthState authState;
private AuthHandler.AuthState oldAuthState, newAuthState;

@Deprecated
public PlayerStateChangeEvent(Player player, AuthHandler.AuthState authState) {
this.player = player;
this.authState = authState;
this.newAuthState = authState;
}

public PlayerStateChangeEvent(Player player, AuthHandler.AuthState oldAuthState, AuthHandler.AuthState newAuthState) {
this.player = player;
this.oldAuthState = oldAuthState;
this.newAuthState = newAuthState;
}

@Override
Expand All @@ -46,11 +55,30 @@ public Player getPlayer() {
return player;
}

public AuthHandler.AuthState getAuthState() {
return authState;
public @Nullable AuthHandler.AuthState getOldAuthState() {
return oldAuthState;
}

public void setOldAuthState(@NotNull AuthHandler.AuthState oldAuthState) {
this.oldAuthState = oldAuthState;
}

public @NotNull AuthHandler.AuthState getNewAuthState() {
return newAuthState;
}

public void setNewAuthState(@NotNull AuthHandler.AuthState newAuthState) {
this.newAuthState = newAuthState;
}


@Deprecated
public @NotNull AuthHandler.AuthState getAuthState() {
return newAuthState;
}

public void setAuthState(AuthHandler.AuthState authState) {
this.authState = authState;
@Deprecated
public void setAuthState(@NotNull AuthHandler.AuthState authState) {
this.newAuthState = newAuthState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private void handlePlayerJoin(UUID uuid) {
// The reason there's a 1 tick delay before every message is that if you send a ChannelMessage too fast, sometimes bungeecord doesn't register the message.
Bukkit.getScheduler().runTaskLater(this.main, () -> {
Player player = Bukkit.getPlayer(uuid);

if(player == null || !player.isOnline()) {
return;
}
Expand Down Expand Up @@ -196,12 +197,14 @@ public void changeState(UUID uuid, AuthState authState, boolean updateBungeecord

Player player = Bukkit.getPlayer(uuid);
if(player != null) {
PlayerStateChangeEvent event = new PlayerStateChangeEvent(player, authState);
PlayerStateChangeEvent event = new PlayerStateChangeEvent(player, authStates.get(uuid), authState);

Bukkit.getPluginManager().callEvent(event);

if(event.isCancelled())
return;

authState = event.getAuthState();
authState = event.getNewAuthState();
}

authStates.put(uuid, authState);
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/lielamar/auth/bukkit/handlers/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.lielamar.auth.shared.storage.StorageMethod;
import com.lielamar.lielsutils.files.FileManager;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;

import java.util.Arrays;
Expand Down Expand Up @@ -149,6 +152,54 @@ public void reload() {
} else
super.requireOnEveryLogin = config.getBoolean("require-when.every-login");

if(!config.contains("tp-before-auth")) {
config.set("tp-before-auth.enable", super.tpBeforeAuth);
config.set("tp-before-auth.location.x", 0);
config.set("tp-before-auth.location.y", 100);
config.set("tp-before-auth.location.z", 0);
config.set("tp-before-auth.location.yaw", 0);
config.set("tp-before-auth.location.pitch", 0);
config.set("tp-before-auth.location.world", "world");
config.addComment("tp-before-auth", "# Should the plugin teleport players that need to authenticate to a designated location?");
} else {
super.tpBeforeAuth = config.getBoolean("tp-before-auth.enable");
World world = Bukkit.getWorld(config.getString("tp-before-auth.location.world", "world"));

if(world != null) {
super.tpBeforeAuthLocation = new Location(
world,
config.getDouble("tp-before-auth.location.x"),
config.getDouble("tp-before-auth.location.y"),
config.getDouble("tp-before-auth.location.z"),
(float) config.getDouble("tp-before-auth.location.yaw"),
(float) config.getDouble("tp-before-auth.location.pitch"));
}
}

if(!config.contains("tp-after-auth")) {
config.set("tp-after-auth.enable", super.tpAfterAuth);
config.set("tp-after-auth.location.x", 0);
config.set("tp-after-auth.location.y", 100);
config.set("tp-after-auth.location.z", 0);
config.set("tp-after-auth.location.yaw", 0);
config.set("tp-after-auth.location.pitch", 0);
config.set("tp-after-auth.location.world", "world");
config.addComment("tp-after-auth", "# Should the plugin teleport players to a designated location right after they authenticated?");
} else {
super.tpAfterAuth = config.getBoolean("tp-after-auth.enable");
World world = Bukkit.getWorld(config.getString("tp-after-auth.location.world", "world"));

if(world != null) {
super.tpAfterAuthLocation = new Location(
world,
config.getDouble("tp-after-auth.location.x"),
config.getDouble("tp-after-auth.location.y"),
config.getDouble("tp-after-auth.location.z"),
(float) config.getDouble("tp-after-auth.location.yaw"),
(float) config.getDouble("tp-after-auth.location.pitch"));
}
}

if(!config.contains("storage-method")) {
config.set("storage-method", super.storageMethod.toString());
config.addComments("storage-method", new String[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
package com.lielamar.auth.bukkit.listeners;

import com.lielamar.auth.bukkit.TwoFactorAuthentication;
import com.lielamar.auth.bukkit.events.PlayerStateChangeEvent;
import com.lielamar.auth.shared.handlers.AuthHandler;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class OnAuthStateChange implements Listener {

private final TwoFactorAuthentication main;

public OnAuthStateChange(TwoFactorAuthentication main) {
this.main = main;
}

@EventHandler
public void onAuthState(PlayerStateChangeEvent event) {
if(event.getAuthState().equals(AuthHandler.AuthState.AUTHENTICATED)) {
public void onStateChange(PlayerStateChangeEvent event) {
if(event.getNewAuthState().equals(AuthHandler.AuthState.AUTHENTICATED)) {
event.getPlayer().setFlySpeed((float) 0.1);
event.getPlayer().setWalkSpeed((float) 0.2);
}
}

@EventHandler
public void onPreAuthPostAuth(PlayerStateChangeEvent event) {
Player player = event.getPlayer();

if(player == null || !player.isOnline())
return;

if(event.getNewAuthState() == AuthHandler.AuthState.PENDING_LOGIN) {
if(main.getConfigHandler().shouldTeleportBeforeAuth() && main.getConfigHandler().teleportBeforeAuthLocation() != null)
player.teleport(main.getConfigHandler().teleportBeforeAuthLocation());
}

if(event.getNewAuthState() == AuthHandler.AuthState.AUTHENTICATED || event.getNewAuthState() == AuthHandler.AuthState.DISABLED) {
if(main.getConfigHandler().shouldTeleportAfterAuth() && main.getConfigHandler().teleportAfterAuthLocation() != null)
player.teleport(main.getConfigHandler().teleportAfterAuthLocation());
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/lielamar/auth/shared/handlers/ConfigHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lielamar.auth.shared.handlers;

import com.lielamar.auth.shared.storage.StorageMethod;
import org.bukkit.Location;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
Expand Down Expand Up @@ -42,6 +43,11 @@ public abstract class ConfigHandler {
protected boolean requireOnIPChange = true;
protected boolean requireOnEveryLogin = false;

protected boolean tpBeforeAuth = false;
protected Location tpBeforeAuthLocation = null;
protected boolean tpAfterAuth = false;
protected Location tpAfterAuthLocation = null;

protected StorageMethod storageMethod = StorageMethod.JSON;

protected String host = "localhost";
Expand Down Expand Up @@ -90,6 +96,13 @@ public boolean shouldRequiredOnEveryLogin() {
return this.requireOnEveryLogin;
}


public boolean shouldTeleportBeforeAuth() { return this.tpBeforeAuth; }
public Location teleportBeforeAuthLocation() { return this.tpBeforeAuthLocation; }
public boolean shouldTeleportAfterAuth() { return this.tpAfterAuth; }
public Location teleportAfterAuthLocation() { return this.tpAfterAuthLocation; }


public StorageMethod getStorageMethod() { return this.storageMethod; }

public String getHost() { return this.host; }
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 2FA
version: "1.5.3"
version: "1.5.4"
author: "LielAmar"
main: com.lielamar.auth.bungee.TwoFactorAuthentication
description: Add another layer of protection to your server
22 changes: 22 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ require-when:
# On every login
every-login: false

# Should the plugin teleport players that need to authenticate to a designated location?
tp-before-auth:
enable: false
location:
x: 0
y: 100
z: 0
yaw: 0
pitch: 0
world: "world"

# Should the plugin teleport players to a designated location right after they authenticated OR if they don't have 2FA enabled?
tp-after-auth:
enable: false
location:
x: 0
y: 100
z: 0
yaw: 0
pitch: 0
world: "world"

# Possible methods for the plugin to store data
#
# Local
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 2FA
version: "1.5.3"
version: "1.5.4"
authors: [LielAmar, SadGhost]
main: com.lielamar.auth.bukkit.TwoFactorAuthentication
description: Add another layer of protection to your server
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/PlayerStateChangeEventExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class PlayerStateChangeEventExample implements Listener {

@EventHandler
public void onStateChange(PlayerStateChangeEvent event) {
if(event.getAuthState() == AuthHandler.AuthState.PENDING_LOGIN) {
if(event.getNewAuthState() == AuthHandler.AuthState.PENDING_LOGIN) {
event.getPlayer().sendMessage(ChatColor.GREEN + "We are waiting for you to authenticate!");
}
}
Expand Down

0 comments on commit b109a36

Please sign in to comment.