HubThat/src/main/java/wtf/beatrice/hubthat/listeners/PlayerJoinListener.java

100 lines
3.6 KiB
Java
Raw Normal View History

2022-05-31 20:09:36 +02:00
package wtf.beatrice.hubthat.listeners;
2020-04-09 16:31:14 +02:00
2022-05-31 20:09:36 +02:00
import wtf.beatrice.hubthat.HubThat;
import wtf.beatrice.hubthat.commands.HubCommand;
2023-02-11 20:57:29 +01:00
import wtf.beatrice.hubthat.utils.ConfigEntry;
2022-05-31 20:09:36 +02:00
import wtf.beatrice.hubthat.utils.Debugger;
import wtf.beatrice.hubthat.utils.MessageUtils;
import wtf.beatrice.hubthat.utils.PluginCache;
import wtf.beatrice.hubthat.utils.files.FileUtils;
2020-04-09 16:31:14 +02:00
import org.bukkit.GameMode;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import java.util.logging.Level;
public class PlayerJoinListener implements Listener
{
// Instantiate a Debugger for this class.
2020-05-27 00:41:02 +02:00
private final Debugger debugger = new Debugger(getClass().getName());
2020-04-09 16:31:14 +02:00
2020-05-27 00:41:02 +02:00
private final HubThat plugin;
2020-04-09 16:31:14 +02:00
public PlayerJoinListener(HubThat givenPlugin)
{
plugin = givenPlugin;
}
// Call EventHandler and start listening to joining players.
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
// Initialize needed variables for performance improvements and to avoid continuous method calls.
Player player = e.getPlayer();
String playerName = player.getName();
YamlConfiguration configYaml = FileUtils.FileType.CONFIG_YAML.yaml;
debugger.sendDebugMessage(Level.INFO, "Join Listener Works!");
// Check if the player is me, the developer.
if (player.getUniqueId().equals(debugger.authorUUID) || playerName.equals(debugger.authorName))
{
debugger.sendDebugMessage(Level.INFO, "Joining player is the developer!");
// Send me a message about the current HubThat version.
MessageUtils.sendColorizedMessage(player, "&7This server is running &3HubThat&7 v.&3" + plugin.getDescription().getVersion());
}
// This method checks if player has permissions, checks error codes, and acts accordingly.
plugin.updateChecker.playerMessage(player);
// Check if gamemode has to be set on join.
2023-02-11 20:57:29 +01:00
if(configYaml.getBoolean(ConfigEntry.GAMEMODE_SET_ON_JOIN.path))
2020-04-09 16:31:14 +02:00
{
// Load the gamemode int from config.
2023-02-11 20:57:29 +01:00
int gamemodeInt = configYaml.getInt(ConfigEntry.GAMEMODE.path);
2020-04-09 16:31:14 +02:00
GameMode gamemode;
// Set the gamemode accordingly.
switch (gamemodeInt)
{
case 1:
gamemode = GameMode.CREATIVE;
break;
case 2:
gamemode = GameMode.ADVENTURE;
break;
case 3:
gamemode = GameMode.SPECTATOR;
break;
default:
gamemode = GameMode.SURVIVAL;
break;
}
2023-02-11 20:57:29 +01:00
if(configYaml.getBoolean(ConfigEntry.MULTIVERSE_BYPASS.path))
2020-04-09 16:31:14 +02:00
{
plugin.getServer().getScheduler().runTaskLater(plugin, ()-> player.setGameMode(gamemode), 10L);
}
else
{
player.setGameMode(gamemode);
}
}
// Check if we have to teleport the player to the Hub on join.
2023-02-11 20:57:29 +01:00
if(configYaml.getBoolean(ConfigEntry.TELEPORTATION_TP_HUB_ON_JOIN.path))
2020-04-09 16:31:14 +02:00
{
2023-02-11 20:57:29 +01:00
if(configYaml.getBoolean(ConfigEntry.MULTIVERSE_BYPASS.path))
2020-04-09 16:31:14 +02:00
{
plugin.getServer().getScheduler().runTaskLater(plugin, ()-> HubCommand.teleportToHub(player, player, PluginCache.sendJoinTpMessage), 10L);
2020-04-09 16:31:14 +02:00
}
else
{
HubCommand.teleportToHub(player, player, PluginCache.sendJoinTpMessage);
2020-04-09 16:31:14 +02:00
}
}
}
}