-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdbb1e6
commit 418a8f6
Showing
7 changed files
with
269 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
218 changes: 177 additions & 41 deletions
218
src/main/java/com/cjburkey/claimchunk/flags/PermFlags.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.