Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spark integration #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ Placeholders are delimited by '-'. Since they're applied with a replace, errors
Maximum supported cooldown is 12 Hours aka 43200 seconds. Cooldowns are not saved to file, so they reset on server reload/restart.
Use for example the player placeholder inside aac's configuration as part of the arbitrarykey.

#### With [spark](https://spark.lucko.me/]) installed
- ```tps:<window>```\
**Windows:** 5s, 10s, 1m, 5m, 15m
- ```mspt:<function[<arg>, <arg2*>]>```\
**Functions:** min[window], max[window], mean[window], median[window], percentile[percentile, window]\
**Windows:** 10s, 1m\
**Returns:** the tick time in milliseconds\
**Examples**:
- mspt:min[10s]
- mspt:percentile[95, 1m]

- ```cpu:<function[<window>]>```\
**Functions:** sys[window], proc[window]\
**Windows:** 10s, 1m, 15m\
**Returns:** on a scale from 0.0 to 1.0\
**Examples**:
- mspt:sys[10s]
- mspt:proc[1m]

**Note:** ```tps``` without parameters will stop working if spark is installed!

### Multi command / delayed commands
In the 'do' clause of the statement, multiple commands can be executed at once, and selected commands can be delayed if desired. The command delimiter is ```/<delay>/```, where the integer between ```/``` and ```/``` denotes the delay before the command should be executed in ticks. Here are some examples:

Expand Down
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -34,17 +38,23 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>me.lucko</groupId>
<artifactId>spark-api</artifactId>
<version>0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<version>3.11.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.konsolas.conditionalcommands;

import me.konsolas.conditionalcommands.placeholders.PlaceholderCooldown;
import me.konsolas.conditionalcommands.placeholders.PlaceholderTPSBukkit;
import me.konsolas.conditionalcommands.placeholders.PlaceholderTPSSpark;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
Expand All @@ -20,6 +22,10 @@ public void onEnable() {
saveDefaultConfig();

getLogger().info("Initializing placeholders...");
Placeholders.TPS.setPlaceHolder(Bukkit.getPluginManager().isPluginEnabled("spark")
? new PlaceholderTPSSpark()
: new PlaceholderTPSBukkit());

for (Placeholders placeholder : Placeholders.values()) {
placeholder.getPlaceholder().init(this);
}
Expand Down Expand Up @@ -182,7 +188,7 @@ private void dispatchCommand(final CommandSender sender, String command) {
}

final String cmd = command.substring(cmdStart, cmdEnd).trim();

if (delay <= 0) {
protectedDispatch(sender, cmd);
} else {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/me/konsolas/conditionalcommands/Placeholders.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

enum Placeholders {
PING(new PlaceholderPing()),
TPS(new PlaceholderTPS()),
TPS(null),
MSPT(new PlaceholderMSPT()),
CPU(new PlaceholderCPU()),
TIME_ONLINE(new PlaceholderTimeOnline()),
PLAYER_COUNT(new PlaceholderPlayerCount()),
UPTIME(new PlaceholderUptime()),
Expand All @@ -14,7 +16,7 @@ enum Placeholders {
CHANCE(new PlaceholderChance()),
COOLDOWN(new PlaceholderCooldown());

private final Placeholder placeholder;
private Placeholder placeholder;

Placeholders(Placeholder placeholder) {
this.placeholder = placeholder;
Expand All @@ -23,4 +25,8 @@ enum Placeholders {
public Placeholder getPlaceholder() {
return placeholder;
}

public void setPlaceHolder(Placeholder placeholder) {
this.placeholder = placeholder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class AbstractParameteredPlaceholder implements Placeholder {
private final Pattern pattern;

AbstractParameteredPlaceholder(String base) {
this.pattern = Pattern.compile("-" + base + ":([A-Za-z0-9%._]*)-");
this.pattern = Pattern.compile("-" + base + ":([A-Za-z0-9%._]+(\\[[^]]*])?)-");
}

@Override
Expand All @@ -25,7 +25,9 @@ public String doSubstitution(String input, Player player) {
Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
input = input.replaceAll(Pattern.quote(matcher.group()), getSub(player, matcher.group(1)));
String fullMatch = matcher.group(0);
String param = matcher.group(1);
input = input.replace(fullMatch, getSub(player, param));
}

return input;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.konsolas.conditionalcommands.placeholders;

import me.konsolas.conditionalcommands.providers.CPUProviderSpark;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class PlaceholderCPU extends AbstractParameteredPlaceholder {

private CPUProviderSpark cpu;

public PlaceholderCPU() {
super("cpu");
}

@Override
protected String getSub(Player player, String param) {
if (cpu == null) {
throw new RuntimeException("Cannot do cpu placeholder because spark is not installed");
}

return cpu.getValue(param).toString();
}

@Override
public void init(Plugin plugin) {
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
if (Bukkit.getPluginManager().isPluginEnabled("spark")) {
cpu = new CPUProviderSpark();
} else {
Bukkit.getLogger().warning("Spark is not installed!");
}
}
}, 20);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package me.konsolas.conditionalcommands.placeholders;

import me.konsolas.conditionalcommands.providers.MSPTProviderSpark;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class PlaceholderMSPT extends AbstractParameteredPlaceholder {

private MSPTProviderSpark mspt;

public PlaceholderMSPT() {
super("mspt");
}

@Override
protected String getSub(Player player, String param) {
if (mspt == null) {
throw new RuntimeException("Cannot do mspt placeholder because spark is not installed");
}

return mspt.getValue(param).toString();
}

@Override
public void init(Plugin plugin) {
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
if (Bukkit.getPluginManager().isPluginEnabled("spark")) {
mspt = new MSPTProviderSpark();
} else {
Bukkit.getLogger().warning("Spark is not installed!");
}
}
}, 20);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.konsolas.conditionalcommands.placeholders;

import me.konsolas.conditionalcommands.providers.Provider;
import me.konsolas.conditionalcommands.providers.TPSProviderBukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class PlaceholderTPSBukkit extends AbstractStandardPlaceholder {
private Provider<Double> tps;

public PlaceholderTPSBukkit() {
super("tps");
}

@Override
protected double getStat(Player player) {
return tps.getValue();
}

@Override
public void init(Plugin plugin) {
tps = new TPSProviderBukkit(plugin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package me.konsolas.conditionalcommands.placeholders;

import me.konsolas.conditionalcommands.providers.ParameteredProvider;
import me.konsolas.conditionalcommands.providers.TPSProviderSpark;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

public class PlaceholderTPSSpark extends AbstractParameteredPlaceholder {
private ParameteredProvider<Double> tps;

public PlaceholderTPSSpark() {
super("tps");
}

@Override
protected String getSub(Player player, String param) {
return tps.getValue(param).toString();
}

@Override
public void init(Plugin plugin) {
Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
@Override
public void run() {
if (Bukkit.getPluginManager().isPluginEnabled("spark")) {
tps = new TPSProviderSpark();
}
}
}, 20);
}
}
Loading