From 9224d6f9a6bda9622e170b0fd740d15bc58c59c5 Mon Sep 17 00:00:00 2001 From: Chaos Date: Fri, 21 Jan 2022 13:48:07 +0100 Subject: [PATCH] Safe Node --- .../schematicsystem/SafeSchematicNode.java | 87 +++++++++++++++++++ pom.xml | 5 ++ 2 files changed, 92 insertions(+) create mode 100644 SchematicSystem_Core/src/de/steamwar/schematicsystem/SafeSchematicNode.java diff --git a/SchematicSystem_Core/src/de/steamwar/schematicsystem/SafeSchematicNode.java b/SchematicSystem_Core/src/de/steamwar/schematicsystem/SafeSchematicNode.java new file mode 100644 index 0000000..9957695 --- /dev/null +++ b/SchematicSystem_Core/src/de/steamwar/schematicsystem/SafeSchematicNode.java @@ -0,0 +1,87 @@ +package de.steamwar.schematicsystem; + +import de.steamwar.schematicsystem.commands.SchematicCommandUtils; +import de.steamwar.sql.SchematicNode; +import de.steamwar.sql.SteamwarUser; +import lombok.NonNull; +import org.bukkit.Material; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public class SafeSchematicNode { + + public static Result setParent(@NonNull SteamwarUser user, @NonNull SchematicNode node, SchematicNode newParent) { + if(newParent == null) { + if(SchematicNode.getSchematicsAccessibleByUser(user.getId(), 0) + .stream().map(SchematicNode::getName).anyMatch(s -> s.equalsIgnoreCase(node.getName()))) { + return Result.ALREADY_IN_DIRECTORY; + } + + node.setParent(0); + } else { + if(!newParent.isDir()) { + return Result.NOT_A_DIR; + } + + if(SchematicNode.getSchematicsAccessibleByUser(user.getId(), newParent.getParent()) + .stream().map(SchematicNode::getName).anyMatch(s -> s.equalsIgnoreCase(node.getName()))) { + return Result.ALREADY_IN_DIRECTORY; + } + + node.setParent(newParent.getId()); + } + return Result.DONE; + } + + public static Result setName(@NonNull SteamwarUser user, @NonNull SchematicNode node, @NonNull String name) { + if(invalidSchemName(name)) { + return Result.INVALID_NAME; + } + + if(SchematicNode.getSchematicsAccessibleByUser(user.getId(), node.getParent()).stream().map(SchematicNode::getName).anyMatch(s -> s.equalsIgnoreCase(name))) { + return Result.ALREADY_IN_DIRECTORY; + } + + node.setName(name); + return Result.DONE; + } + + public static Result setItem(@NonNull SteamwarUser user, @NonNull SchematicNode node, @NonNull Material material) { + + } + + private static final List FORBIDDEN_NAMES = Collections.unmodifiableList(Arrays.asList("public")); + public static boolean invalidSchemName(String name) { + if (name.isEmpty()) { + return true; + } + if(name.length() > 64) { + return true; + } + if (name.contains("/") || + name.contains("\\") || + name.contains("<") || + name.contains(">") || + name.contains("^") || + name.contains("°") || + name.contains("'") || + name.contains("\"") || + name.contains(" ")) { + return true; + } + return FORBIDDEN_NAMES.contains(name.toLowerCase()); + } + + public enum Result { + DONE, + NOT_A_DIR, + ALREADY_IN_DIRECTORY, + INVALID_NAME; + + public boolean isSuccessful() { + return this == DONE; + } + } +} diff --git a/pom.xml b/pom.xml index b1a8c5c..cc39eaa 100644 --- a/pom.xml +++ b/pom.xml @@ -45,5 +45,10 @@ system ${main.basedir}/lib/SpigotCore.jar + + org.projectlombok + lombok + 1.18.22 + \ No newline at end of file