Skip to content

Commit

Permalink
Get flags loading from yml
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Aug 15, 2024
1 parent cdbb1e6 commit 418a8f6
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 68 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ dependencies {

testImplementation("org.slf4j:slf4j-simple:${DepData.SLF4J_VERSION}")
testImplementation("org.junit.jupiter:junit-jupiter:${DepData.JUNIT_VERSION}")
testImplementation("org.spigotmc:spigot-api:${DepData.SPIGOT_VERSION}")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:${DepData.JUNIT_LAUNCHER_VERSION}")
}

Expand Down
26 changes: 18 additions & 8 deletions src/main/java/com/cjburkey/claimchunk/Utils.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.cjburkey.claimchunk;

import com.cjburkey.claimchunk.placeholder.ClaimChunkPlaceholders;

import lombok.Getter;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -19,13 +23,14 @@

public final class Utils {

private static final Logger log = Logger.getLogger("Minecraft");
private static Logger log;

private static ClaimChunk claimChunk;
@Getter static boolean debugEnableOverride = false;

static void init(ClaimChunk claimChunk) {
Utils.claimChunk = claimChunk;
Utils.log = claimChunk.getLogger();
}

static void overrideDebugEnable() {
Expand All @@ -36,25 +41,32 @@ static void overrideDebugDisable() {
debugEnableOverride = false;
}

private static Logger getLogger() {
if (log == null) {
log = Logger.getLogger("Minecraft");
}
return log;
}

public static void log(String msg, Object... data) {
log.info(prepMsg(msg, data));
getLogger().info(prepMsg(msg, data));
}

public static void debug(String msg, Object... data) {
if (debugEnableOverride
|| claimChunk != null
&& claimChunk.getConfigHandler() != null
&& claimChunk.getConfigHandler().getDebugSpam()) {
log.info(prepMsg("[DEBUG] " + msg, data));
getLogger().info(prepMsg("[DEBUG]" + msg, data));
}
}

public static void err(String msg, Object... data) {
log.severe(prepMsg(msg, data));
getLogger().severe(prepMsg(msg, data));
}

public static void warn(String msg, Object... data) {
log.warning(prepMsg(msg, data));
getLogger().warning(prepMsg(msg, data));
}

public static int clamp(int val, int min, int max) {
Expand Down Expand Up @@ -163,9 +175,7 @@ private static String prepMsg(String msg, Object... data) {
String out = (msg == null) ? "null" : msg;

// Output with the ClaimChunk prefix
return String.format(
"[%s] %s",
claimChunk.getDescription().getPrefix(), color(String.format(out, data)));
return "[ClaimChunk] " + color(String.format(out, data));
}

public static Map<String, Boolean> getDefaultPermissionsMap() {
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/com/cjburkey/claimchunk/flags/PermFlag.java

This file was deleted.

218 changes: 177 additions & 41 deletions src/main/java/com/cjburkey/claimchunk/flags/PermFlags.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,215 @@
package com.cjburkey.claimchunk.flags;

import com.cjburkey.claimchunk.Utils;
import com.google.common.base.Charsets;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Keeps track of loading the permission flags specified in the flags.yml
* configuration file.
* Keeps track of loading the permission flags specified in the flags.yml configuration file.
*
* @since 0.0.26
*/
public class PermFlags {

private final JavaPlugin plugin;
private final File defaultFlagsFile;
private final String defaultFlagsResource;
private final File flagsFile;
private final HashMap<String, BlockFlagData> blockControls = new HashMap<>();
private final HashMap<String, EntityFlagData> entityControls = new HashMap<>();

public PermFlags(JavaPlugin plugin, File defaultFlagsFile, String defaultFlagsResource, File flagsFile) {
this.plugin= plugin;
this.defaultFlagsFile = defaultFlagsFile;
this.defaultFlagsResource = defaultFlagsResource;
this.flagsFile = flagsFile;
public final HashMap<String, BlockFlagData> blockControls = new HashMap<>();
public final HashMap<String, EntityFlagData> entityControls = new HashMap<>();

/** Read the flags defined in the flag definitions file. */
public void load(File flagsFile, JavaPlugin plugin, String defaultFlagsResource) {
// Load the flags.yml file while ensuring the default exists
YamlConfiguration config = readFlagFile(flagsFile, plugin, defaultFlagsResource);
if (config == null) {
throw new RuntimeException("Failed to load flag config file (see ClaimChunk errors)");
}

loadFromConfig(config);
}

public void load() {
// TODO:
public void loadFromConfig(YamlConfiguration config) {
// Read the flag section
ConfigurationSection flagSection = config.getConfigurationSection("permissionFlags");
if (flagSection == null) {
throw new RuntimeException("Flag config file missing permissionFlags section");
}

// Read each flag name
for (String flagName : flagSection.getKeys(false)) {
// Get the list of maps (see src/resources/defaultFlags.yml for format)
List<Map<?, ?>> flagEntries = flagSection.getMapList(flagName);
if (flagEntries.isEmpty()) {
Utils.err("Flag \"%s\" has no protections", flagName);
continue;
}

// Loop through each map
for (Map<?, ?> flagMap : flagEntries) {
String forType = (String) flagMap.get("for");
String interactType = (String) flagMap.get("type");
if (interactType == null) {
Utils.err(
"Missing interaction type in one of the flag protection maps in flag"
+ " \"%s\"",
flagName);
}

// Check if this is for blocks/entities
switch (forType) {
case "BLOCKS" -> {
if (blockControls.containsKey(flagName)) {
Utils.err(
"Flag \"%s\" already has block protections defined", flagName);
continue;
}

// Get the type of interaction to block
BlockFlagType flagType;
try {
flagType = BlockFlagType.valueOf(interactType);
} catch (Exception ignored) {
Utils.err(
"Unknown block interaction type \"%s\" in flag \"%s\"",
interactType, flagName);
continue;
}

// Get the includes/excludes
FlagData flagData = readIncludeExclude(flagMap);
if (flagData == null) {
Utils.err(
"Failed to load flag includes/excludes from flag \"%s\" for"
+ " block protections",
flagName);
}

// Add the protections
BlockFlagData blockFlagData = new BlockFlagData(flagType, flagData);
blockControls.put(flagName, blockFlagData);
}
case "ENTITIES" -> {
if (entityControls.containsKey(flagName)) {
Utils.err(
"Flag \"%s\" already has entity protections defined", flagName);
continue;
}

// Get the type of interaction to block
EntityFlagType flagType;
try {
flagType = EntityFlagType.valueOf(interactType);
} catch (Exception ignored) {
Utils.err(
"Unknown entity interaction type \"%s\" in flag \"%s\"",
interactType, flagName);
continue;
}

// Get the includes/excludes
FlagData flagData = readIncludeExclude(flagMap);
if (flagData == null) {
Utils.err(
"Failed to load flag includes/excludes from flag \"%s\" for"
+ " entity protections",
flagName);
}

// Add the protections
EntityFlagData entityFlagData = new EntityFlagData(flagType, flagData);
entityControls.put(flagName, entityFlagData);
}
default -> Utils.err(
"Invalid flag protection target \"%s\" for flag \"%s\"",
forType, flagName);
}
}

// Player property CJ-made-error safety check :)
if (blockControls.isEmpty() && entityControls.isEmpty()) {
throw new RuntimeException(
"ClaimChunk failed to load any block/entity protection flags, make sure the"
+ " /plugins/ClaimChunk/flags.yml file is set up correctly (or allow it"
+ " to regenerate)");
}
}
}

// -- CLASSES -- //
// Please don't break :|
@SuppressWarnings("unchecked")
private FlagData readIncludeExclude(Map<?, ?> flagMap) {
try {
return new FlagData(
(List<String>) flagMap.get("include"), (List<String>) flagMap.get("exclude"));
} catch (Exception e) {
Utils.err("Failed to read include/exclude data: %s", e.getMessage());
}
return null;
}

public static final class FlagData {
public boolean isListInclude;
public List<String> list;
private YamlConfiguration readFlagFile(
File flagsFile, JavaPlugin plugin, String defaultFlagsResource) {
if (flagsFile.exists()) {
// Just load the config
return YamlConfiguration.loadConfiguration(flagsFile);
} else {
// Load the configuration from the defaultFlags.yml file
YamlConfiguration ymlConfig;
try {
InputStream resource = plugin.getResource(defaultFlagsResource);
ymlConfig =
YamlConfiguration.loadConfiguration(
new InputStreamReader(
Objects.requireNonNull(
resource,
"Failed to locate resource at "
+ defaultFlagsResource),
Charsets.UTF_8));
} catch (Exception e) {
Utils.err(
"Failed to load default flag config (Is your file UTF-8?): %s",
e.getMessage());
return null;
}

public FlagData(boolean isListInclude, List<String> list) {
this.isListInclude = isListInclude;
this.list = list;
// Save the defaults
try {
ymlConfig.options().copyDefaults(true);
ymlConfig.save(flagsFile);
} catch (Exception e) {
Utils.err("Failed to save default flags file: %s", e.getMessage());
}
return ymlConfig;
}
}

// -- CLASSES -- //

public enum BlockFlagType {
BREAK,
PLACE,
INTERACT,
EXPLODE,
}

public static class BlockFlagData {
public BlockFlagType flagType;
public FlagData flagData;

public BlockFlagData(BlockFlagType flagType, boolean isListInclude, List<String> list) {
this.flagType = flagType;
this.flagData = new FlagData(isListInclude, list);
}
}

public enum EntityFlagType {
DAMAGE,
INTERACT,
EXPLODE,
}

public static class EntityFlagData {
public EntityFlagType flagType;
public FlagData flagData;
public record FlagData(@Nullable List<String> include, @Nullable List<String> exclude) {}

public EntityFlagData(EntityFlagType flagType, boolean isListInclude, List<String> list) {
this.flagType = flagType;
this.flagData = new FlagData(isListInclude, list);
}
}
public record BlockFlagData(BlockFlagType flagType, FlagData flagData) {}

public record EntityFlagData(EntityFlagType flagType, FlagData flagData) {}
}
8 changes: 5 additions & 3 deletions src/main/resources/defaultFlags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ permissionFlags:
# Blocks
- for: BLOCKS # BLOCKS or ENTITIES
type: BREAK # For blocks, can be BREAK, PLACE, INTERACT, or EXPLODE
# If no `include` or `exclude` (cannot be used together, btw)
# is present, the default is to include all blocks/entities.
# If `include` is present, only the provided entities/entity
# If no `include` or `exclude` is present, the default is to
# include all blocks/entities.
# If only `include` is present, only the provided entities/entity
# classes will be included.
# The opposite is true of `exclude`, which includes all
# default blocks/items and excludes the provided ones.
# If both are provided, then exclusions are considered after
# inclusions.
placeBlocks:
- for: BLOCKS
type: PLACE
Expand Down
Loading

0 comments on commit 418a8f6

Please sign in to comment.