Refactored CoreProtectAPI

This commit is contained in:
Intelli 2025-03-10 13:41:26 -06:00
parent d6c4755fba
commit 5c965b8a68
11 changed files with 812 additions and 303 deletions

3
.gitignore vendored
View file

@ -179,6 +179,9 @@ context.json
run.sh
run_*.sh
no-log4j2.xml
database.db
*tests.txt
refactor.txt
# Compiled class file
*.class

View file

@ -190,7 +190,12 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>com.github.DeadSilenceIV</groupId>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,129 @@
package net.coreprotect.api.result;
import java.util.Locale;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.data.BlockData;
import net.coreprotect.api.SessionLookup;
import net.coreprotect.utility.EntityUtils;
import net.coreprotect.utility.MaterialUtils;
import net.coreprotect.utility.StringUtils;
import net.coreprotect.utility.WorldUtils;
public class ParseResult {
private final String[] parse;
public ParseResult(String[] data) {
parse = data;
}
public int getActionId() {
return Integer.parseInt(parse[7]);
}
public String getActionString() {
int actionID = Integer.parseInt(parse[7]);
if (parse.length < 13 && Integer.parseInt(parse[6]) == SessionLookup.ID) {
switch (actionID) {
case 0:
return "logout";
case 1:
return "login";
default:
return "unknown";
}
}
String result = "unknown";
if (actionID == 0) {
result = "break";
}
else if (actionID == 1) {
result = "place";
}
else if (actionID == 2) {
result = "click";
}
else if (actionID == 3) {
result = "kill";
}
return result;
}
@Deprecated
public int getData() {
return Integer.parseInt(parse[6]);
}
public String getPlayer() {
return parse[1];
}
@Deprecated
public int getTime() {
return Integer.parseInt(parse[0]);
}
public long getTimestamp() {
return Long.parseLong(parse[0]) * 1000L;
}
public Material getType() {
if (parse.length < 13) {
return null;
}
int actionID = this.getActionId();
int type = Integer.parseInt(parse[5]);
String typeName;
if (actionID == 3) {
typeName = EntityUtils.getEntityType(type).name();
}
else {
typeName = MaterialUtils.getType(type).name().toLowerCase(Locale.ROOT);
typeName = StringUtils.nameFilter(typeName, this.getData());
}
return MaterialUtils.getType(typeName);
}
public BlockData getBlockData() {
if (parse.length < 13) {
return null;
}
String blockData = parse[12];
if (blockData == null || blockData.length() == 0) {
return getType().createBlockData();
}
return Bukkit.getServer().createBlockData(blockData);
}
public int getX() {
return Integer.parseInt(parse[2]);
}
public int getY() {
return Integer.parseInt(parse[3]);
}
public int getZ() {
return Integer.parseInt(parse[4]);
}
public boolean isRolledBack() {
if (parse.length < 13) {
return false;
}
return (Integer.parseInt(parse[8]) == 1 || Integer.parseInt(parse[8]) == 3);
}
public String worldName() {
return WorldUtils.getWorldName(Integer.parseInt(parse.length < 13 ? parse[5] : parse[9]));
}
}

View file

@ -28,7 +28,7 @@ import net.coreprotect.utility.Color;
import net.coreprotect.utility.WorldUtils;
public class LookupCommand {
protected static void runCommand(CommandSender player, Command command, boolean permission, String[] args) {
public static void runCommand(CommandSender player, Command command, boolean permission, String[] args) {
int resultc = args.length;
args = CommandParser.parsePage(args);
Location lo = CommandParser.parseLocation(player, args);
@ -470,10 +470,12 @@ public class LookupCommand {
List<String> rollbackusers = argUsers;
int c = 0;
for (String ruser : rollbackusers) {
List<Player> players = Bukkit.getServer().matchPlayer(ruser);
for (Player p : players) {
if (p.getName().equalsIgnoreCase(ruser)) {
rollbackusers.set(c, p.getName());
if (Bukkit.getServer() != null) {
List<Player> players = Bukkit.getServer().matchPlayer(ruser);
for (Player p : players) {
if (p.getName().equalsIgnoreCase(ruser)) {
rollbackusers.set(c, p.getName());
}
}
}
c++;
@ -524,7 +526,9 @@ public class LookupCommand {
if (lo != null) {
x = lo.getBlockX();
z = lo.getBlockZ();
wid = WorldUtils.getWorldId(lo.getWorld().getName());
if (lo.getWorld() != null) {
wid = WorldUtils.getWorldId(lo.getWorld().getName());
}
}
if (rollbackusers.size() == 1 && rollbackusers.contains("#global") && argAction.contains(9)) {

View file

@ -32,7 +32,7 @@ import net.coreprotect.utility.Color;
import net.coreprotect.utility.WorldUtils;
public class RollbackRestoreCommand {
protected static void runCommand(CommandSender player, Command command, boolean permission, String[] args, Location argLocation, long forceStart, long forceEnd) {
public static void runCommand(CommandSender player, Command command, boolean permission, String[] args, Location argLocation, long forceStart, long forceEnd) {
Location lo = (argLocation != null ? argLocation : CommandParser.parseLocation(player, args));
List<String> argUuids = new ArrayList<>();
List<String> argUsers = CommandParser.parseUsers(args);

View file

@ -157,7 +157,8 @@ public class ActionParser {
argument = argument.trim().toLowerCase(Locale.ROOT);
argument = argument.replaceAll("\\\\", "");
argument = argument.replaceAll("'", "");
if (argument.equals("#count") || argument.equals("#sum")) {
if (argument.equals("#count") || argument.equals("#sum") ||
argument.equals("count") || argument.equals("sum")) {
result = true;
}
}
@ -214,10 +215,11 @@ public class ActionParser {
argument = argument.trim().toLowerCase(Locale.ROOT);
argument = argument.replaceAll("\\\\", "");
argument = argument.replaceAll("'", "");
if (argument.equals("#preview")) {
if (argument.equals("#preview") || argument.equals("preview")) {
result = 1;
}
else if (argument.equals("#preview_cancel")) {
else if (argument.equals("#preview_cancel") || argument.equals("#preview-cancel") ||
argument.equals("preview_cancel") || argument.equals("preview-cancel")) {
result = 2;
}
}

View file

@ -128,6 +128,14 @@ public class UserParser {
next = 0;
}
}
// Handle u:#player format for excluded users
else if (argument.startsWith("u:#")) {
String username = argument.substring(3); // Remove the 'u:#' prefix
if (!username.isEmpty()) {
excluded.add(username);
}
next = 0;
}
else {
next = 0;
}

View file

@ -239,6 +239,16 @@ public enum Phrase {
public static String build(Phrase phrase, String... params) {
String output = phrase.getTranslatedPhrase();
// If translated phrase is null, fall back to the default phrase
if (output == null) {
output = phrase.getPhrase();
// If that's still null, use an empty string to avoid NullPointerException
if (output == null) {
output = "";
}
}
String color = "";
if (HEADERS.contains(phrase)) {
@ -283,6 +293,12 @@ public enum Phrase {
private static String buildInternal(Phrase phrase, String[] params, String color) {
String output = phrase.getPhrase(); // get internal phrase
// If internal phrase is null, use an empty string to avoid NullPointerException
if (output == null) {
output = "";
return output; // Return empty string immediately if no phrase is available
}
int index = 0;
for (String param : params) {
if (index == 0 && COLORS.contains(param)) {
@ -301,6 +317,11 @@ public enum Phrase {
public static String getPhraseSelector(Phrase phrase, String selector) {
String translatedPhrase = phrase.getTranslatedPhrase();
// Return empty string if translated phrase is null
if (translatedPhrase == null) {
return "";
}
Pattern phrasePattern = Pattern.compile("(\\{[a-zA-Z| ]+})");
Matcher patternMatch = phrasePattern.matcher(translatedPhrase);
String match = "";

View file

@ -10,6 +10,7 @@ public class SystemUtils {
private static boolean testMode = Boolean.getBoolean("net.coreprotect.test");
private static String processorInfo = null;
private static boolean log4jInitialized = false;
private SystemUtils() {
throw new IllegalStateException("Utility class");
@ -43,7 +44,17 @@ public class SystemUtils {
else if (System.getProperty("os.name").toLowerCase().contains("android") || System.getProperty("java.runtime.name").toLowerCase().contains("android")) {
return null;
}
Configurator.setLevel("oshi.hardware.common.AbstractCentralProcessor", Level.OFF);
try {
if (!log4jInitialized) {
Configurator.setLevel("oshi.hardware.common.AbstractCentralProcessor", Level.OFF);
log4jInitialized = true;
}
}
catch (Exception e) {
// log4j configuration failure, continue without it
}
SystemInfo systemInfo = new SystemInfo();
result = systemInfo.getHardware().getProcessor();
}
@ -79,6 +90,6 @@ public class SystemUtils {
* @return true if Log4j is disabled
*/
private static boolean isLog4jDisabled() {
return Boolean.getBoolean("log4j2.disable") || System.getProperty("log4j.configurationFile", "").contains("no-log4j2.xml");
return Boolean.getBoolean("log4j2.disable") || Boolean.getBoolean("net.coreprotect.disable.log4j") || System.getProperty("log4j.configurationFile", "").contains("no-log4j2.xml");
}
}

View file

@ -46,7 +46,18 @@ public class VersionUtils {
}
public static String getPluginName() {
String name = CoreProtect.getInstance().getDescription().getName();
CoreProtect instance = CoreProtect.getInstance();
// Return default name if instance is null
if (instance == null) {
return "CoreProtect";
}
// Return default name if description is null
if (instance.getDescription() == null) {
return "CoreProtect";
}
String name = instance.getDescription().getName();
String branch = ConfigHandler.EDITION_BRANCH;
if (branch.startsWith("-edge")) {
@ -107,7 +118,12 @@ public class VersionUtils {
public static String getBranch() {
String branch = "";
try {
InputStreamReader reader = new InputStreamReader(CoreProtect.getInstance().getClass().getResourceAsStream("/plugin.yml"));
CoreProtect instance = CoreProtect.getInstance();
if (instance == null) {
return "";
}
InputStreamReader reader = new InputStreamReader(instance.getClass().getResourceAsStream("/plugin.yml"));
branch = YamlConfiguration.loadConfiguration(reader).getString("branch");
reader.close();
@ -257,4 +273,4 @@ public class VersionUtils {
return result;
}
}
}