-
Notifications
You must be signed in to change notification settings - Fork 1
Limit command execution
Sven Rahn edited this page Jun 13, 2018
·
14 revisions
Context: The player won the lottery/a reward. The winner is not online.
The reward command is
/give %player% minecraft:cookie 5
You delay the command until the player joins the next time
/cu execute whenOnline %player% *give %player% minecraft:cookie 5
This works fine. But you want to make the player only get their reward if they join in the next 3 days. So you use the /cu execute before ... command:
/cu execute before <timestamp> *cu execute whenOnline %player% *give %player% minecraft:cookie 5
But where do we get the <timestamp> from? We can use a JavaScript Placeholder. Here is an example for our use-case:
config/placeholderapi/javascript/timestamp.js
function isEmpty(string) {
return (!string || 0 === string.length());
}
function toNumberOrZero(string) {
return isEmpty(string) ? 0 : parseInt(string);
}
with(new JavaImporter(java.lang, java.time, java.time.temporal, java.util, java.util.regex, java.text)) {
var duration = args[0];
var now = Instant.now();
var durationRegex = Pattern.compile("(?:(\\d+)d)?(?:(\\d+)h)?(?:(\\d+)m)?(?:(\\d+)s)?(?:(\\d+)ms)?");
var matcher = durationRegex.matcher(duration);
if (!matcher.find()) {
throw new IllegalArgumentException("'" + duration + "' isn't a valid duration! Use something like '3d2h12m30s800ms'")
}
var days = toNumberOrZero(matcher.group(1));
var hours = toNumberOrZero(matcher.group(2));
var minutes = toNumberOrZero(matcher.group(3));
var seconds = toNumberOrZero(matcher.group(4));
var milliseconds = toNumberOrZero(matcher.group(5));
var time = now
.plus(days, ChronoUnit.DAYS)
.plus(hours, ChronoUnit.HOURS)
.plus(minutes, ChronoUnit.MINUTES)
.plus(seconds, ChronoUnit.SECONDS)
.plus(milliseconds, ChronoUnit.MILLIS);
var format = new SimpleDateFormat("HH:mm:ss.SSS-dd.MM.yyyy");
out = format.format(java.util.Date.from(time));
}
So now the placeholder %javascript_timestamp% takes duration argument and returns a <timestamp> which can be used in the after and before commands.
This is our final command:
/cu execute before %javascript_timestamp_3d% *cu execute whenOnline %player% *give %player% minecraft:cookie 5