Add setExpires to disguise, disguises expire after a scheduled amount of time

This commit is contained in:
libraryaddict
2019-03-06 15:16:14 +13:00
parent ec5bf18399
commit d164ea36dc
8 changed files with 143 additions and 18 deletions

View File

@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
public class DisguiseParser {
private static void doCheck(CommandSender sender, DisguisePermissions permissions, DisguisePerm disguisePerm,
@@ -204,6 +205,43 @@ public class DisguiseParser {
return args;
}
public static long parseStringToTime(String string) throws DisguiseParseException {
string = string.toLowerCase();
if (!string.matches("([0-9]+[a-z]+)+")) {
throw new DisguiseParseException(LibsMsg.PARSE_INVALID_TIME_SEQUENCE, string);
}
String[] split = string.split("((?<=[a-zA-Z])(?=[0-9]))|((?<=[0-9])(?=[a-zA-Z]))");
long time = 0;
for (int i = 0; i < split.length; i += 2) {
String t = split[i + 1];
long v = Long.parseLong(split[i]);
if (t.equals("s") || t.equals("sec") || t.equals("secs") || t.equals("seconds")) {
time += v;
} else if (t.equals("m") || t.equals("min") || t.equals("minute") || t.equals("minutes")) {
time += TimeUnit.MINUTES.toSeconds(v);
} else if (t.equals("h") || t.equals("hour") || t.equals("hours")) {
time += TimeUnit.HOURS.toSeconds(v);
} else if (t.equals("d") || t.equals("day") || t.equals("days")) {
time += TimeUnit.DAYS.toSeconds(v);
} else if (t.equals("w") || t.equals("week") || t.equals("weeks")) {
time += TimeUnit.DAYS.toSeconds(v) * 7;
} else if (t.equals("mon") || t.equals("month") || t.equals("months")) {
time += TimeUnit.DAYS.toSeconds(v) * 31;
} else if (t.equals("y") || t.equals("year") || t.equals("years")) {
time += TimeUnit.DAYS.toSeconds(v) * 365;
} else {
throw new DisguiseParseException(LibsMsg.PARSE_INVALID_TIME, t);
}
}
return time;
}
/**
* Experimentally parses the arguments to test if this is a valid disguise
*

View File

@@ -4,7 +4,6 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher;
import me.libraryaddict.disguise.utilities.parser.DisguisePerm;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfoTypes;
import org.bukkit.ChatColor;
@@ -104,9 +103,10 @@ public class ParamInfoManager {
// Add these last as it's what we want to present to be called the least
for (String methodName : new String[]{"setViewSelfDisguise", "setHideHeldItemFromSelf", "setHideArmorFromSelf",
"setHearSelfDisguise", "setHidePlayer"}) {
"setHearSelfDisguise", "setHidePlayer", "setExpires"}) {
try {
methods.add(Disguise.class.getMethod(methodName, boolean.class));
methods.add(Disguise.class
.getMethod(methodName, methodName.equals("setExpires") ? long.class : boolean.class));
}
catch (Exception ex) {
ex.printStackTrace();
@@ -117,7 +117,7 @@ public class ParamInfoManager {
}
/**
* Value of the method, used namely for displaying the more unique methods to a disguise
* Value of the method, used namely for ordering the more unique methods to a disguise
*/
public static int getValue(Method method) {
ChatColor methodColor = ChatColor.YELLOW;

View File

@@ -82,6 +82,9 @@ public class ParamInfoTypes {
paramInfos.add(new ParamInfoGameProfile(WrappedGameProfile.class, "GameProfile",
"Get the gameprofile here https://sessionserver.mojang" +
".com/session/minecraft/profile/PLAYER_UUID_GOES_HERE?unsigned=false"));
paramInfos.add(new ParamInfoTime(long.class, "Expiry Time",
"Set how long the disguise lasts, <Num><Time><Num>... where <Time> is (s/sec)(m/min)(h/hour)(d/day) " +
"etc. 30m20secs = 30 minutes, 20 seconds"));
// Register base types
Map<String, Object> booleanMap = new HashMap<>();

View File

@@ -0,0 +1,30 @@
package me.libraryaddict.disguise.utilities.parser.params.types.custom;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.utilities.parser.DisguiseParseException;
import me.libraryaddict.disguise.utilities.parser.DisguiseParser;
import me.libraryaddict.disguise.utilities.parser.params.ParamInfo;
/**
* Created by libraryaddict on 6/03/2019.
*/
public class ParamInfoTime extends ParamInfo {
public ParamInfoTime(Class paramClass, String name, String description) {
super(paramClass, name, description);
}
@Override
protected Object fromString(String string) throws DisguiseParseException {
long time = DisguiseParser.parseStringToTime(string);
// If disguise expires X ticks afterwards
if (DisguiseConfig.isDynamicExpiry()) {
time *= 20;
} else if (!DisguiseConfig.isDynamicExpiry()) { // If disguise expires at a set time
time *= 1000; // Multiply for milliseconds
time += System.currentTimeMillis(); // Add current time to expiry time
}
return time;
}
}