Attempt to speed up disguise parsing by caching console perms

This commit is contained in:
libraryaddict 2023-11-12 13:06:53 +13:00
parent bc0cc65549
commit 955278d149
4 changed files with 29 additions and 7 deletions

View File

@ -1,6 +1,7 @@
package me.libraryaddict.disguise.commands.libsdisguises;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.utilities.parser.DisguisePermissions;
import me.libraryaddict.disguise.utilities.sounds.SoundManager;
import me.libraryaddict.disguise.utilities.translations.LibsMsg;
import org.bukkit.command.CommandSender;
@ -26,6 +27,7 @@ public class LDReload implements LDCommand {
public void onCommand(CommandSender sender, String[] args) {
DisguiseConfig.loadConfig();
new SoundManager().load();
DisguisePermissions.onReload();
LibsMsg.RELOADED_CONFIG.send(sender);
}

View File

@ -402,7 +402,7 @@ public class DisguiseParser {
* Get perms for the node. Returns a hashmap of allowed disguisetypes and their options
*/
public static DisguisePermissions getPermissions(CommandSender sender, String commandName) {
return new DisguisePermissions(sender, commandName);
return DisguisePermissions.getPermissions(sender, commandName);
}
private static boolean isInteger(String string) {
@ -600,7 +600,7 @@ public class DisguiseParser {
params = DisguiseParser.parsePlaceholders(params, target, target);
}
DisguiseParser.callMethods(Bukkit.getConsoleSender(), disguise, new DisguisePermissions(Bukkit.getConsoleSender(), "disguise"),
DisguiseParser.callMethods(Bukkit.getConsoleSender(), disguise, DisguisePermissions.getPermissions(Bukkit.getConsoleSender(), "sender"),
new DisguisePerm(disguise.getType()), new ArrayList<>(), params, "Disguise");
}
@ -621,7 +621,8 @@ public class DisguiseParser {
}
public static Disguise parseDisguise(CommandSender sender, Entity target, String disguise) throws Throwable {
return parseDisguise(sender, target, "disguise", DisguiseUtilities.split(disguise), new DisguisePermissions(Bukkit.getConsoleSender(), "disguise"));
return parseDisguise(sender, target, "disguise", DisguiseUtilities.split(disguise),
DisguisePermissions.getPermissions(Bukkit.getConsoleSender(), "disguise"));
}
/**

View File

@ -5,6 +5,7 @@ import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.FlagWatcher;
import org.bukkit.entity.EntityType;
import java.util.Locale;
import java.util.Objects;
/**
@ -14,19 +15,19 @@ public class DisguisePerm {
private final DisguiseType disguiseType;
private String permName;
@Getter
private String regexedName;
private final String regexedName;
private boolean customDisguise;
public DisguisePerm(DisguiseType disguiseType) {
this.disguiseType = disguiseType;
regexedName = toReadable().replaceAll("[ |_]", "").toLowerCase();
regexedName = toReadable().replaceAll("[ |_]", "").toLowerCase(Locale.ROOT);
}
public DisguisePerm(DisguiseType disguiseType, String disguisePerm) {
this.disguiseType = disguiseType;
permName = disguisePerm;
customDisguise = true;
regexedName = toReadable().replaceAll("[ |_]", "").toLowerCase();
regexedName = toReadable().replaceAll("[ |_]", "").toLowerCase(Locale.ROOT);
}
public boolean isCustomDisguise() {

View File

@ -2,6 +2,7 @@ package me.libraryaddict.disguise.utilities.parser;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import org.bukkit.Bukkit;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Monster;
@ -108,6 +109,7 @@ public class DisguisePermissions {
* List of PermissionStorage that the permission holder is able to use
*/
private final List<PermissionStorage> disguises = new ArrayList<>();
private final static Map<String, DisguisePermissions> CONSOLE_PERMISSIONS = new HashMap<>();
/**
* @param permissionHolder The permissions to check
@ -115,6 +117,22 @@ public class DisguisePermissions {
*/
public DisguisePermissions(Permissible permissionHolder, String commandName) {
loadPermissions(permissionHolder, commandName.toLowerCase(Locale.ENGLISH));
if (permissionHolder == Bukkit.getConsoleSender()) {
CONSOLE_PERMISSIONS.put(commandName, this);
}
}
public static DisguisePermissions getPermissions(Permissible permissionHolder, String commandName) {
if (permissionHolder == Bukkit.getConsoleSender() && CONSOLE_PERMISSIONS.containsKey(commandName)) {
return CONSOLE_PERMISSIONS.get(commandName);
}
return new DisguisePermissions(permissionHolder, commandName);
}
public static void onReload() {
CONSOLE_PERMISSIONS.clear();
}
/**