Cancel 15 second timer after removing click type

This commit is contained in:
Eric 2018-08-05 12:19:25 +02:00
parent f516b78776
commit c80129b56f
2 changed files with 31 additions and 42 deletions

View File

@ -28,7 +28,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.List;
@ -382,14 +381,6 @@ class ShopCommandExecutor implements CommandExecutor {
ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.CREATE, product, buyPrice, sellPrice, shopType));
plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(Message.CLICK_CHEST_CREATE));
// Remove ClickType after 15 seconds if player has not clicked a chest
new BukkitRunnable() {
@Override
public void run() {
ClickType.removePlayerClickType(p);
}
}.runTaskLater(plugin, 300);
} else {
plugin.debug("Shop pre create event cancelled");
}
@ -412,14 +403,6 @@ class ShopCommandExecutor implements CommandExecutor {
plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(Message.CLICK_CHEST_REMOVE));
ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.REMOVE));
// Remove ClickType after 15 seconds if player has not clicked a chest
new BukkitRunnable() {
@Override
public void run() {
ClickType.removePlayerClickType(p);
}
}.runTaskLater(plugin, 300);
}
/**
@ -439,14 +422,6 @@ class ShopCommandExecutor implements CommandExecutor {
plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(Message.CLICK_CHEST_INFO));
ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.INFO));
// Remove ClickType after 15 seconds if player has not clicked a chest
new BukkitRunnable() {
@Override
public void run() {
ClickType.removePlayerClickType(p);
}
}.runTaskLater(plugin, 300);
}
/**
@ -466,14 +441,6 @@ class ShopCommandExecutor implements CommandExecutor {
plugin.debug(p.getName() + " can now click a chest");
p.sendMessage(LanguageUtils.getMessage(Message.CLICK_CHEST_OPEN));
ClickType.setPlayerClickType(p, new ClickType(ClickType.EnumClickType.OPEN));
// Remove ClickType after 15 seconds if player has not clicked a chest
new BukkitRunnable() {
@Override
public void run() {
ClickType.removePlayerClickType(p);
}
}.runTaskLater(plugin, 300);
}
private boolean changeConfig(CommandSender sender, String[] args) {

View File

@ -1,14 +1,22 @@
package de.epiceric.shopchest.utils;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop.ShopType;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
public class ClickType {
private static HashMap<OfflinePlayer, ClickType> playerClickType = new HashMap<>();
private static Map<UUID, ClickType> playerClickType = new HashMap<>();
private static Map<UUID, BukkitTask> playerTimers = new HashMap<>();
private EnumClickType enumClickType;
private ItemStack product;
private double buyPrice;
@ -34,28 +42,42 @@ public class ClickType {
* @return The Player's click type or <b>null</b> if he doesn't have one
*/
public static ClickType getPlayerClickType(OfflinePlayer player) {
if (playerClickType.containsKey(player))
return playerClickType.get(player);
else
return null;
return playerClickType.get(player.getUniqueId());
}
/**
* Removes the click type from a player
* Removes the click type from a player and cancels the 15 second timer
* @param player Player to remove the click type from
*/
public static void removePlayerClickType(OfflinePlayer player) {
playerClickType.remove(player);
UUID uuid = player.getUniqueId();
playerClickType.remove(uuid);
// If a timer is still running, cancel it
Optional.ofNullable(playerTimers.get(uuid)).ifPresent(task -> task.cancel());
playerTimers.remove(uuid);
}
/**
* Sets the click type of a player
* Sets the click type of a player and removes it after 15 seconds
*
* @param player Player whose click type should be set
* @param clickType Click type to set
*/
public static void setPlayerClickType(OfflinePlayer player, ClickType clickType) {
playerClickType.put(player, clickType);
UUID uuid = player.getUniqueId();
playerClickType.put(uuid, clickType);
// If a timer is already running, cancel it
Optional.ofNullable(playerTimers.get(uuid)).ifPresent(task -> task.cancel());
// Remove ClickType after 15 seconds if player has not clicked a chest
playerTimers.put(uuid, new BukkitRunnable() {
@Override
public void run() {
playerClickType.remove(uuid);
}
}.runTaskLater(ShopChest.getInstance(), 300));
}
/**