Add cooldown per portal feature

This commit is contained in:
Tomasz Piowczyk 2020-04-04 17:12:09 +02:00
parent 067865f726
commit dafb14ffb4
4 changed files with 79 additions and 34 deletions

View File

@ -148,12 +148,14 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
boolean isBungeePortal = false;
boolean needsPermission = false;
boolean executesCommand = false;
boolean hasCooldownDelay = false;
String destination = null;
String portalName = null;
String triggerBlock = null;
String serverName = null;
String permission = null;
String portalCommand = null;
String cooldownDelay = null;
ArrayList<PortalArg> extraData = new ArrayList<>();
@ -223,6 +225,9 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
return true;
}
extraData.add(new PortalArg("command.1", portalCommand));
} else if (startsWithPortalArg("cooldowndelay:", args[i])) {
hasCooldownDelay = true;
cooldownDelay = args[i].toLowerCase().replaceFirst("cooldowndelay:", "");
}
}
if (!hasName) {
@ -275,13 +280,22 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
player.sendMessage("\u00A7acommand: \u00A7e" + portalCommand);
}
int cooldown = 5;
if(hasCooldownDelay) {
try {
cooldown = Integer.parseInt(cooldownDelay);
} catch (Exception e) {
}
}
player.sendMessage("\u00A7acooldowndelay: \u00A7e" + cooldown);
if (hasTriggerBlock) {
Set<Material> materialSet = Portal.getMaterialSet(triggerBlock.toUpperCase().split(","));
if (materialSet.size() != 0) {
player.sendMessage("\u00A7atriggerBlock: \u00A7e" + triggerBlock.toUpperCase());
PortalArg[] portalArgs = new PortalArg[extraData.size()];
portalArgs = extraData.toArray(portalArgs);
player.sendMessage(Portal.create(pos1, pos2, portalName, destination, materialSet, serverName, portalArgs));
player.sendMessage(Portal.create(pos1, pos2, portalName, destination, materialSet, serverName, cooldown, portalArgs));
} else {
ConfigAccessor Config = new ConfigAccessor(plugin, "config.yml");
player.sendMessage("\u00A7ctriggerBlock: \u00A7edefault(" + Config.getConfig().getString("DefaultPortalTriggerBlock") + ")");
@ -289,14 +303,14 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
player.sendMessage("\u00A7c" + triggerBlock.toUpperCase() + " no valid blocks were listed so the default has been set.");
PortalArg[] portalArgs = new PortalArg[extraData.size()];
portalArgs = extraData.toArray(portalArgs);
player.sendMessage(Portal.create(pos1, pos2, portalName, destination, serverName, portalArgs));
player.sendMessage(Portal.create(pos1, pos2, portalName, destination, serverName, cooldown, portalArgs));
}
} else {
ConfigAccessor Config = new ConfigAccessor(plugin, "config.yml");
player.sendMessage("\u00A7atriggerBlock: \u00A7edefault(" + Config.getConfig().getString("DefaultPortalTriggerBlock") + ")");
PortalArg[] portalArgs = new PortalArg[extraData.size()];
portalArgs = extraData.toArray(portalArgs);
player.sendMessage(Portal.create(pos1, pos2, portalName, destination, serverName, portalArgs));
player.sendMessage(Portal.create(pos1, pos2, portalName, destination, serverName, cooldown, portalArgs));
}
} else {
sender.sendMessage(PluginMessages.customPrefixFail + " A portal by that name already exists!");
@ -317,7 +331,7 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
}
break;
case "variables" :
sender.sendMessage(PluginMessages.customPrefix + " \u00A77Variables \u00A7c: \u00A7aname, triggerBlock, desti, destination, bungee, permission, command");
sender.sendMessage(PluginMessages.customPrefix + " \u00A77Variables \u00A7c: \u00A7aname, triggerBlock, desti, destination, bungee, permission, command, cooldowndelay");
sender.sendMessage("");
sender.sendMessage("\u00A7aExample command: \u00A7e/portal create name:test triggerId:portal");
break;
@ -694,6 +708,7 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
boolean isBungeePortal = false;
boolean needsPermission = false;
boolean hasCommand = false;
boolean hasCooldownDelay = false;
// TODO change auto complete when quotes are opened and closed. Such as autocomplete @Player and stuff when specifying commands
@ -726,6 +741,9 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
case "command":
hasCommand = true;
break;
case "cooldowndelay":
hasCooldownDelay = true;
break;
}
}
@ -753,6 +771,9 @@ public class AdvancedPortalsCommand implements CommandExecutor, TabCompleter {
if (!hasCommand) {
autoComplete.add("command:");
}
if (!hasCooldownDelay) {
autoComplete.add("cooldowndelay:");
}
}
}
if (args.length == 2 && args[0].equalsIgnoreCase("warp")){

View File

@ -64,7 +64,8 @@ public class Listeners implements Listener {
@EventHandler
public void onJoinEvent(PlayerJoinEvent event) {
Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
// TODO: make relability
// Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
/* if (plugin.PlayerDestiMap.containsKey(event.getPlayer())) {
String desti = plugin.PlayerDestiMap.get(event.getPlayer());
@ -75,12 +76,14 @@ public class Listeners implements Listener {
@EventHandler
public void onWorldChangeEvent(PlayerChangedWorldEvent event) {
Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
// TODO: make relability
// Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
}
@EventHandler(ignoreCancelled = true)
public void onTeleportEvent(PlayerTeleportEvent event) {
Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
// TODO: make relability
// Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
}
@EventHandler(ignoreCancelled = true)

View File

@ -21,6 +21,8 @@ public class AdvancedPortal {
private String portalName = null;
private int cooldownDelay = 0;
// TODO store destinations also as variables like portals
private String destiation = null; // Could possibly store the destination name to stop the server having to read the config file
@ -33,31 +35,32 @@ public class AdvancedPortal {
public HashSet<UUID> inPortal = new HashSet<UUID>();
// TODO think of relaying out the data input to a more logical format.
public AdvancedPortal(String portalName, Material trigger, String destination, Location pos1, Location pos2, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, pos2.getWorld().getName(), portalArgs);
public AdvancedPortal(String portalName, Material trigger, String destination, Location pos1, Location pos2, int cooldown, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, pos2.getWorld().getName(), cooldown, portalArgs);
this.destiation = destination;
}
public AdvancedPortal(String portalName, Material trigger, Location pos1, Location pos2, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, pos2.getWorld().getName(), portalArgs);
public AdvancedPortal(String portalName, Material trigger, Location pos1, Location pos2, int cooldown, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, pos2.getWorld().getName(), cooldown, portalArgs);
}
public AdvancedPortal(String portalName, Material trigger, String destination, Location pos1, Location pos2, String worldName, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, worldName, portalArgs);
public AdvancedPortal(String portalName, Material trigger, String destination, Location pos1, Location pos2, String worldName, int cooldown, PortalArg... portalArgs) {
this(portalName, trigger, pos1, pos2, worldName, cooldown, portalArgs);
this.destiation = destination;
}
public AdvancedPortal(String portalName, Material trigger, Location pos1, Location pos2, String worldName, PortalArg... portalArgs) {
this(portalName, new HashSet<>(Collections.singletonList(trigger)), pos1, pos2, worldName, portalArgs);
public AdvancedPortal(String portalName, Material trigger, Location pos1, Location pos2, String worldName, int cooldown, PortalArg... portalArgs) {
this(portalName, new HashSet<>(Collections.singletonList(trigger)), pos1, pos2, worldName, cooldown, portalArgs);
}
public AdvancedPortal(String portalName, Set<Material> triggers, Location pos1, Location pos2, String worldName, PortalArg... portalArgs) {
public AdvancedPortal(String portalName, Set<Material> triggers, Location pos1, Location pos2, String worldName, int cooldown, PortalArg... portalArgs) {
this.portalName = portalName;
this.triggers = triggers;
this.pos1 = pos1;
this.pos2 = pos2;
this.worldName = worldName;
this.portalArgs = portalArgs;
this.cooldownDelay = cooldown;
}
public String getArg(String arg) {
@ -97,6 +100,14 @@ public class AdvancedPortal {
return this.portalName;
}
public int getCooldownDelay() {
return this.cooldownDelay;
}
public void setCooldownDelay(int cooldownDelay) {
this.cooldownDelay = cooldownDelay;
}
public String getDestiation() {
return this.destiation;
}

View File

@ -20,14 +20,13 @@ import java.util.stream.Collectors;
public class Portal {
public static HashMap<String, Long> cooldown = new HashMap<String, Long>();
public static HashMap<String, HashMap<String, Long>> cooldown = new HashMap<String, HashMap<String, Long>>();
// Config values
public static boolean portalsActive = false;
public static AdvancedPortal[] portals = new AdvancedPortal[0];
private static AdvancedPortalsPlugin plugin;
public static ConfigAccessor portalData = new ConfigAccessor(plugin, "portals.yml");
private static boolean showBungeeMessage;
private static int cooldelay;
private static double throwback;
private static Sound portalSound;
private static int portalProtectionRadius;
@ -38,7 +37,6 @@ public class Portal {
public Portal(AdvancedPortalsPlugin plugin) {
ConfigAccessor config = new ConfigAccessor(plugin, "config.yml");
this.showBungeeMessage = config.getConfig().getBoolean("ShowBungeeWarpMessage", false);
this.cooldelay = config.getConfig().getInt("PortalCooldown", 5);
this.portalProtectionRadius = config.getConfig().getInt("PortalProtectionRadius");
@ -104,11 +102,12 @@ public class Portal {
World world = Bukkit.getWorld(worldName);
Location pos1 = new Location(world, portalData.getConfig().getInt(portal.toString() + ".pos1.X"), portalData.getConfig().getInt(portal.toString() + ".pos1.Y"), portalData.getConfig().getInt(portal.toString() + ".pos1.Z"));
Location pos2 = new Location(world, portalData.getConfig().getInt(portal.toString() + ".pos2.X"), portalData.getConfig().getInt(portal.toString() + ".pos2.Y"), portalData.getConfig().getInt(portal.toString() + ".pos2.Z"));
int cooldown = portalData.getConfig().getInt(portal.toString() + ".cooldowndelay");
PortalArg[] portalArgs = new PortalArg[extraData.size()];
extraData.toArray(portalArgs);
portals[portalId] = new AdvancedPortal(portal.toString(), blockTypes, pos1, pos2, worldName, portalArgs);
portals[portalId] = new AdvancedPortal(portal.toString(), blockTypes, pos1, pos2, worldName, cooldown, portalArgs);
portals[portalId].setBungee(portalConfigSection.getString("bungee"));
@ -146,11 +145,11 @@ public class Portal {
return blockTypes;
}
public static String create(Location pos1, Location pos2, String name, String destination, Set<Material> triggerBlocks, PortalArg... extraData) {
return create(pos1, pos2, name, destination, triggerBlocks, null, extraData);
public static String create(Location pos1, Location pos2, String name, String destination, Set<Material> triggerBlocks, int cooldown, PortalArg... extraData) {
return create(pos1, pos2, name, destination, triggerBlocks, null, cooldown, extraData);
}
public static String create(Location pos1, Location pos2, String name, String destination, Set<Material> triggerBlocks, String serverName, PortalArg... portalArgs) {
public static String create(Location pos1, Location pos2, String name, String destination, Set<Material> triggerBlocks, String serverName, int cooldown, PortalArg... portalArgs) {
if (!pos1.getWorld().equals(pos2.getWorld())) {
plugin.getLogger().log(Level.WARNING, "pos1 and pos2 must be in the same world!");
@ -202,6 +201,8 @@ public class Portal {
portalData.getConfig().set(name + ".destination", destination);
portalData.getConfig().set(name + ".cooldowndelay", cooldown);
portalData.getConfig().set(name + ".bungee", serverName);
portalData.getConfig().set(name + ".pos1.X", HighX);
@ -276,7 +277,7 @@ public class Portal {
return false;
}
public static String create(Location pos1, Location pos2, String name, String destination, String serverName, PortalArg... extraData) { // add stuff for destination names or coordinates
public static String create(Location pos1, Location pos2, String name, String destination, String serverName, int cooldown, PortalArg... extraData) { // add stuff for destination names or coordinates
ConfigAccessor config = new ConfigAccessor(plugin, "config.yml");
Material triggerBlockType;
@ -287,7 +288,7 @@ public class Portal {
triggerBlockType = Material.NETHER_PORTAL;
}
return create(pos1, pos2, name, destination, new HashSet<>(Collections.singletonList(triggerBlockType)), serverName, extraData);
return create(pos1, pos2, name, destination, new HashSet<>(Collections.singletonList(triggerBlockType)), serverName, cooldown, extraData);
}
public static void redefine(Location pos1, Location pos2, String name) {
@ -384,17 +385,26 @@ public class Portal {
return false;
}
if (cooldown.get(player.getName()) != null) {
int diff = (int) ((System.currentTimeMillis() - cooldown.get(player.getName())) / 1000);
if (diff < cooldelay) {
int time = (cooldelay - diff);
player.sendMessage(ChatColor.RED + "Please wait " + ChatColor.YELLOW + time + ChatColor.RED + (time == 1 ? " second" : " seconds") + " until attempting to enter this portal again.");
failSound(player, portal);
throwPlayerBack(player);
return false;
HashMap<String, Long> cds = cooldown.get(player.getName());
if (cds != null) {
if (cds.get(portal.getName()) != null) {
long cd = cds.get(portal.getName());
int diff = (int) ((System.currentTimeMillis() - cd) / 1000);
if (diff < portal.getCooldownDelay()) {
int time = (portal.getCooldownDelay() - diff);
player.sendMessage(ChatColor.RED + "Please wait " + ChatColor.YELLOW + time + ChatColor.RED + (time == 1 ? " second" : " seconds") + " until attempting to enter this portal again.");
failSound(player, portal);
throwPlayerBack(player);
return false;
}
}
}
cooldown.put(player.getName(), System.currentTimeMillis());
if(cds == null) {
cds = new HashMap<String, Long>();
player.sendMessage("cds is set");
}
cds.put(portal.getName(), System.currentTimeMillis());
cooldown.put(player.getName(), cds);
boolean showFailMessage = !portal.hasArg("command.1");
boolean hasMessage = portal.getArg("message") != null;