Skip to content

Commit

Permalink
fix small bugs and code refactoring has been done
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisIndenbom committed Jun 10, 2022
1 parent 01f24f0 commit 5662a25
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 99 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.denisindenbom</groupId>
<artifactId>CyberAuth</artifactId>
<version>1.0.6</version>
<version>1.0.7</version>
<name>CyberAuth</name>

<build>
Expand Down Expand Up @@ -55,11 +55,13 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
70 changes: 34 additions & 36 deletions src/main/java/com/denisindenbom/cyberauth/CyberAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import com.denisindenbom.cyberauth.listeners.PlayerListener;

import com.denisindenbom.cyberauth.user.UserAuthManager;
import com.denisindenbom.cyberauth.managers.UserAuthManager;
import com.denisindenbom.cyberauth.database.CyberAuthDB;
import com.denisindenbom.cyberauth.passwordauth.PasswordAuth;
import com.denisindenbom.cyberauth.commands.*;
Expand All @@ -18,18 +18,16 @@

import java.io.File;
import java.sql.SQLException;
import java.util.Objects;

import org.apache.logging.log4j.core.Logger;

public class CyberAuth extends JavaPlugin
{
private FileConfiguration messages;
private FileConfiguration messagesConfig;

private UserAuthManager authManager;
private CyberAuthDB authDB;
private PasswordAuth passwordAuth;

private PlayerListener playerListener;

@Override
Expand Down Expand Up @@ -64,9 +62,9 @@ public void loadPlugin()
{
// save default configs
this.saveDefaultConfig();
this.saveDefaultMessages();
this.saveDefaultMessagesConfig();

this.loadMessages();
this.loadMessagesConfig();
// load db
try
{
Expand All @@ -92,23 +90,35 @@ public void loadPlugin()
long authTime = this.getConfig().getLong("auth_time");

// register commands executors
Objects.requireNonNull(this.getCommand("login")).setExecutor(
new Login(this, this.messages, kick));
Objects.requireNonNull(this.getCommand("register")).setExecutor(
new Register(this, this.messages, minPasswordLength, maxPasswordLength));
Objects.requireNonNull(this.getCommand("change_password")).setExecutor(
new ChangePassword(this, this.messages, minPasswordLength, maxPasswordLength));
Objects.requireNonNull(this.getCommand("logout")).setExecutor(
new Logout(this, kick, authTime));
Objects.requireNonNull(this.getCommand("reload_cyberauth")).setExecutor(new Reload(this, this.messages));
Objects.requireNonNull(this.getCommand("remove_user")).setExecutor(new Remove(this, this.messages));
this.getCommand("login").setExecutor(new Login(this, kick));
this.getCommand("register").setExecutor(new Register(this, minPasswordLength, maxPasswordLength));
this.getCommand("change_password").setExecutor(new ChangePassword(this, minPasswordLength, maxPasswordLength));
this.getCommand("logout").setExecutor(new Logout(this, authTime));
this.getCommand("reload_cyberauth").setExecutor(new Reload(this));
this.getCommand("remove_user").setExecutor(new Remove(this));

// create player listener
this.playerListener = new PlayerListener(this, this.messages, kick, authTime);
this.playerListener = new PlayerListener(this, authTime);
// register player listener
this.getServer().getPluginManager().registerEvents(this.playerListener, this);
}

private void disablePlugin()
{
try
{
// disable database
this.authDB.disable();
}
catch (SQLException e)
{
this.getLogger().warning("Failed to close database connection!");
}

HandlerList.unregisterAll(this.playerListener);
this.playerListener = null;
}

public void reloadPlugin()
{
// reload config
Expand All @@ -131,41 +141,29 @@ public CyberAuthDB getAuthDB()
public PlayerListener getPlayerListener()
{return this.playerListener;}

private void disablePlugin()
{
try
{
// disable database
this.authDB.disable();
}
catch (SQLException e)
{
this.getLogger().warning("Failed to close database connection!");
}

HandlerList.unregisterAll(this.playerListener);
this.playerListener = null;
}
public FileConfiguration getMessagesConfig()
{return this.messagesConfig;}

private void saveDefaultMessages()
private void saveDefaultMessagesConfig()
{
File messagesFile = new File(getDataFolder(), "messages.yml");

if (!messagesFile.exists()) saveResource("messages.yml", false);
}

private void loadMessages()
private void loadMessagesConfig()
{
File messagesFile = new File(getDataFolder(), "messages.yml");

this.messages = new YamlConfiguration();
this.messagesConfig = new YamlConfiguration();
try
{
this.messages.load(messagesFile);
this.messagesConfig.load(messagesFile);
}
catch (Exception e)
{
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.bukkit.command.CommandSender;

import com.denisindenbom.cyberauth.CyberAuth;
import com.denisindenbom.cyberauth.messagesender.MessageSender;
import com.denisindenbom.cyberauth.utils.MessageSender;
import com.denisindenbom.cyberauth.user.User;

import org.bukkit.configuration.file.FileConfiguration;
Expand All @@ -22,13 +22,14 @@ public class ChangePassword implements CommandExecutor
private final long minPasswordLength;
private final long maxPasswordLength;

public ChangePassword(CyberAuth plugin, FileConfiguration messages, long minPasswordLength, long maxPasswordLength)
public ChangePassword(CyberAuth plugin, long minPasswordLength, long maxPasswordLength)
{
this.plugin = plugin;
this.messages = messages;

this.minPasswordLength = minPasswordLength;
this.maxPasswordLength = maxPasswordLength;

this.messages = this.plugin.getMessagesConfig();
}

@Override
Expand All @@ -42,7 +43,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
if (!this.plugin.getAuthManager().userExists(player.getName()))
{
this.messageSender.sendMessage(sender, this.messages.getString("error.not_logged_in"));
return false;
return true;
}
if (args.length < 2)
{
Expand All @@ -56,12 +57,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N
char[] newPassword = args[1].toCharArray();

// check password length
if (newPassword.length <= this.minPasswordLength)
if (newPassword.length < this.minPasswordLength)
{
this.messageSender.sendMessage(sender, this.messages.getString("error.short_password"), "{%min_password_length%}", ""+this.minPasswordLength);
return true;
}
if (newPassword.length >= this.maxPasswordLength)
if (newPassword.length > this.maxPasswordLength)
{
this.messageSender.sendMessage(sender, this.messages.getString("error.long_password"), "{%max_password_length%}", ""+this.maxPasswordLength);
return true;
Expand All @@ -84,7 +85,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

if (!changePasswordResult)
this.messageSender.sendMessage(sender, this.messages.getString("error.change_password"));
else this.messageSender.sendMessage(sender, "Password changed successfully!");
else this.messageSender.sendMessage(sender, this.messages.getString("change_password.changed"));

return true;
}
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/com/denisindenbom/cyberauth/commands/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import org.bukkit.entity.Player;

import com.denisindenbom.cyberauth.CyberAuth;
import com.denisindenbom.cyberauth.formattext.FormatText;
import com.denisindenbom.cyberauth.messagesender.MessageSender;
import com.denisindenbom.cyberauth.utils.FormatText;
import com.denisindenbom.cyberauth.utils.MessageSender;

import com.denisindenbom.cyberauth.user.User;

Expand All @@ -19,19 +19,18 @@
public class Login implements CommandExecutor
{
private final CyberAuth plugin;

private final MessageSender messageSender = new MessageSender();
private final FormatText formatText = new FormatText();

private final FileConfiguration messages;
private final boolean kick;

private final MessageSender messageSender = new MessageSender();
private final FormatText formatText = new FormatText();

public Login(CyberAuth plugin, FileConfiguration messages, boolean kick)
public Login(CyberAuth plugin, boolean kick)
{
this.plugin = plugin;
this.messages = messages;
this.kick = kick;

this.messages = this.plugin.getMessagesConfig();
}

@Override
Expand All @@ -42,19 +41,19 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

Player player = (Player) sender;

if (args.length == 0)
if (this.plugin.getAuthManager().userExists(player.getName()))
{
this.messageSender.sendMessage(sender, this.messages.getString("error.arguments"));
return false;
this.messageSender.sendMessage(sender, this.messages.getString("error.logged_in"));
return true;
}
if (!this.plugin.getAuthDB().userExists(player.getName()))
{
this.messageSender.sendMessage(sender, this.messages.getString("error.not_registered"));
return true;
}
if (this.plugin.getAuthManager().userExists(player.getName()))
if (args.length == 0)
{
this.messageSender.sendMessage(sender, this.messages.getString("error.logged_in"));
this.messageSender.sendMessage(sender, this.messages.getString("error.arguments"));
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ public class Logout implements CommandExecutor
{
private final CyberAuth plugin;

private final boolean kick;
private final long authTime;

public Logout(CyberAuth plugin, boolean kick, long authTime)
public Logout(CyberAuth plugin, long authTime)
{
this.plugin = plugin;

this.kick = kick;
this.authTime = authTime;
}

Expand All @@ -34,7 +32,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @N

this.plugin.getAuthManager().removeUserByName(player.getName());

if (this.kick) this.plugin.getPlayerListener().kickTimer(player, this.authTime);
this.plugin.getPlayerListener().kickTimer(player, this.authTime);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.bukkit.entity.Player;

import com.denisindenbom.cyberauth.CyberAuth;
import com.denisindenbom.cyberauth.messagesender.MessageSender;
import com.denisindenbom.cyberauth.utils.MessageSender;
import com.denisindenbom.cyberauth.user.User;

import org.jetbrains.annotations.NotNull;
Expand All @@ -24,14 +24,14 @@ public class Register implements CommandExecutor
private final long minPasswordLength;
private final long maxPasswordLength;

public Register(CyberAuth plugin, FileConfiguration messages, long minPasswordLength, long maxPasswordLength)
public Register(CyberAuth plugin, long minPasswordLength, long maxPasswordLength)
{
this.plugin = plugin;

this.messages = messages;

this.minPasswordLength = minPasswordLength;
this.maxPasswordLength = maxPasswordLength;

this.messages = this.plugin.getMessagesConfig();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.bukkit.entity.Player;

import com.denisindenbom.cyberauth.CyberAuth;
import com.denisindenbom.cyberauth.messagesender.MessageSender;
import com.denisindenbom.cyberauth.utils.MessageSender;

import org.jetbrains.annotations.NotNull;

Expand All @@ -19,10 +19,11 @@ public class Reload implements CommandExecutor

private final FileConfiguration messages;

public Reload(CyberAuth plugin, FileConfiguration messages)
public Reload(CyberAuth plugin)
{
this.plugin = plugin;
this.messages = messages;

this.messages = this.plugin.getMessagesConfig();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.bukkit.entity.Player;

import com.denisindenbom.cyberauth.CyberAuth;
import com.denisindenbom.cyberauth.messagesender.MessageSender;
import com.denisindenbom.cyberauth.utils.MessageSender;

import org.jetbrains.annotations.NotNull;

Expand All @@ -19,10 +19,11 @@ public class Remove implements CommandExecutor

private final FileConfiguration messages;

public Remove(CyberAuth plugin, FileConfiguration messages)
public Remove(CyberAuth plugin)
{
this.plugin = plugin;
this.messages = messages;

this.messages = this.plugin.getMessagesConfig();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.denisindenbom.cyberauth.user.User;
import org.jetbrains.annotations.NotNull;

public class CyberAuthDB extends DBManager
public class CyberAuthDB extends DataBase
{
public CyberAuthDB(String path) throws SQLException
{
Expand Down Expand Up @@ -117,6 +117,7 @@ public boolean userExists(String name)
{
// make a sql query to get player data from the database
ResultSet resultSet = this.executeQuery(sqlRequest, name);

return resultSet.next();
}
catch (SQLException e)
Expand Down
Loading

0 comments on commit 5662a25

Please sign in to comment.