Skip to content

Commit

Permalink
Async updated
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanOff committed Jan 18, 2021
1 parent 9b32912 commit 8cfa403
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
18 changes: 9 additions & 9 deletions com/gaetan/staffpin/StaffPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import com.gaetan.staffpin.runnable.MoveRunnable;
import com.google.common.collect.Maps;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.Map;
import java.util.UUID;

public final class StaffPlugin extends GCore {
/**
* Map to stock the PayerData
*/
private final Map<Player, PlayerData> players = Maps.newConcurrentMap();
private final Map<UUID, PlayerData> players = Maps.newConcurrentMap();

/**
* Reference to the ConfigManager
Expand Down Expand Up @@ -59,22 +59,22 @@ protected void registerListener() {
}

/**
* Getter to get the PlayerData of a specific player.
* Getter to get the PlayerData of a specific player with his UUID.
* Note: The player must have the pin permission.
*
* @param player player The choosen player
* @return The PlayerData of the choosen player
* @param uuid The choosen UUID
* @return The PlayerData of the choosen UUID
*/
public PlayerData getPlayer(final Player player) {
return this.players.get(player);
public PlayerData getPlayer(final UUID uuid) {
return this.players.get(uuid);
}

/**
* Getter to get the Map of all PlayerData.
*
* @return The map containing all the players and PlayerData with the pin permission
* @return The map containing all the UUID and PlayerData with the pin permission
*/
public Map<Player, PlayerData> getPlayers() {
public Map<UUID, PlayerData> getPlayers() {
return this.players;
}
}
2 changes: 1 addition & 1 deletion com/gaetan/staffpin/command/PinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void handleCommand_Set(final Context<ConsoleCommandSender> context, final
final Player player = (Player) context.getSender();

if (player.hasPermission(this.configManager.getPinPermission())) {
this.staffPlugin.getPlayer(player).setPin(pin);
this.staffPlugin.getPlayer(player.getUniqueId()).setPin(pin);
Message.tell(player, this.configManager.getPinSet());
}
}
Expand Down
18 changes: 11 additions & 7 deletions com/gaetan/staffpin/listener/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.UUID;

public final class PlayerListener implements Listener {
/**
* Reference to the main class
Expand Down Expand Up @@ -46,7 +48,7 @@ public void onJoin(final PlayerJoinEvent event) {

if (player.hasPermission(this.configManager.getPinPermission())) {
final PlayerData playerData = new PlayerData(player, this.staffPlugin, this.configManager);
this.staffPlugin.getPlayers().put(player, playerData);
this.staffPlugin.getPlayers().put(player.getUniqueId(), playerData);

playerData.join();
}
Expand All @@ -57,10 +59,10 @@ public void onJoin(final PlayerJoinEvent event) {
*/
@EventHandler
public void onQuit(final PlayerQuitEvent event) {
final Player player = event.getPlayer();
final UUID uuid = event.getPlayer().getUniqueId();

if (player.hasPermission(this.configManager.getPinPermission()) && this.staffPlugin.getPlayers().containsKey(player))
this.staffPlugin.getPlayers().remove(player).leave();
if (event.getPlayer().hasPermission(this.configManager.getPinPermission()) && this.staffPlugin.getPlayers().containsKey(uuid))
this.staffPlugin.getPlayers().remove(uuid).leave();
}

/**
Expand All @@ -70,11 +72,12 @@ public void onQuit(final PlayerQuitEvent event) {
@EventHandler
public void onChat(final AsyncPlayerChatEvent event) {
final Player player = event.getPlayer();
final UUID uuid = event.getPlayer().getUniqueId();

if (player.hasPermission(this.configManager.getPinPermission())) {
final PlayerData playerData = this.staffPlugin.getPlayer(player);
final PlayerData playerData = this.staffPlugin.getPlayer(uuid);

if (this.staffPlugin.getPlayers().containsKey(player) && !playerData.isLogin()) {
if (this.staffPlugin.getPlayers().containsKey(uuid) && !playerData.isLogin()) {
event.setCancelled(true);

if (event.getMessage().equals(playerData.getPin())) {
Expand All @@ -93,9 +96,10 @@ public void onChat(final AsyncPlayerChatEvent event) {
@EventHandler
public void onCommand(final PlayerCommandPreprocessEvent event) {
final Player player = event.getPlayer();
final UUID uuid = event.getPlayer().getUniqueId();

if (player.hasPermission(this.configManager.getPinPermission())) {
if (this.staffPlugin.getPlayers().containsKey(player) && !this.staffPlugin.getPlayer(player).isLogin()) {
if (this.staffPlugin.getPlayers().containsKey(uuid) && !this.staffPlugin.getPlayer(uuid).isLogin()) {
Message.tell(player, this.configManager.getEnterPin());
event.setCancelled(true);
}
Expand Down
2 changes: 1 addition & 1 deletion com/gaetan/staffpin/runnable/LoadPlayerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public LoadPlayerConfig(final StaffPlugin staffPlugin, final PlayerData playerDa
}

/**
* Loading the playerdata from a config
* Loading the pin from a config
* Note: This must be executed in async
*/
@Override
Expand Down
15 changes: 9 additions & 6 deletions com/gaetan/staffpin/runnable/MoveRunnable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gaetan.staffpin.runnable;

import com.gaetan.api.message.Message;
import com.gaetan.api.runnable.TaskUtil;
import com.gaetan.staffpin.StaffPlugin;
import com.gaetan.staffpin.config.ConfigManager;
import com.gaetan.staffpin.data.PlayerData;
Expand All @@ -27,20 +28,22 @@ public final class MoveRunnable extends BukkitRunnable {
public MoveRunnable(final StaffPlugin staffPlugin, final ConfigManager configManager) {
this.staffPlugin = staffPlugin;
this.configManager = configManager;
this.runTaskTimer(this.staffPlugin, 0L, 40L);
this.runTaskTimerAsynchronously(this.staffPlugin, 0L, 40L);
}

/**
* Teleport player to the waiting pin location
* Note: This will be executed every 2 seconds
* Note: This will be executed async every 2 seconds
*/
@Override
public void run() {
this.staffPlugin.getPlayers().keySet().forEach(player -> {
final PlayerData playerData = this.staffPlugin.getPlayer(player);
this.staffPlugin.getPlayers().keySet().forEach(uuid -> {
final PlayerData playerData = this.staffPlugin.getPlayer(uuid);
if (!playerData.isLogin() && playerData.getPin() != null) {
Message.tell(player, this.configManager.getEnterPin());
player.teleport(playerData.getLocation());
TaskUtil.run(() -> {
Message.tell(playerData.getPlayer(), this.configManager.getEnterPin());
playerData.getPlayer().teleport(playerData.getLocation());
});
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion com/gaetan/staffpin/runnable/SavePlayerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public SavePlayerConfig(final StaffPlugin staffPlugin, final PlayerData playerDa
}

/**
* Saving the playerdata to a config
* Saving the pin to a config
* Note: This must be executed in async
*/
@Override
Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: StaffPin
author: Gaetan_Off
version: 0.4-SNAPSHOT
version: 0.5-SNAPSHOT
main: com.gaetan.staffpin.StaffPlugin

0 comments on commit 8cfa403

Please sign in to comment.