Added protection methods.
This commit is contained in:
parent
ba62989788
commit
f7b6898559
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/bskyblock.iml" filepath="$PROJECT_DIR$/bskyblock.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||||
|
<component name="EclipseModuleManager">
|
||||||
|
<conelement value="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
|
||||||
|
<src_description expected_position="0">
|
||||||
|
<src_folder value="file://$MODULE_DIR$/src/main/java" expected_position="0" />
|
||||||
|
<src_folder value="file://$MODULE_DIR$/locales" expected_position="1" />
|
||||||
|
<src_folder value="file://$MODULE_DIR$/src/test/java" expected_position="2" />
|
||||||
|
</src_description>
|
||||||
|
</component>
|
||||||
|
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||||
|
<output url="file://$MODULE_DIR$/target/classes" />
|
||||||
|
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/locales" type="java-resource" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="jdk" jdkName="1.8 (1)" jdkType="JavaSDK" />
|
||||||
|
<orderEntry type="library" name="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" level="application" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-all:1.10.19" level="project" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.12.2-R0.1-SNAPSHOT" level="project" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:21.0" level="project" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.19" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-module-junit4:1.7.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-module-junit4-common:1.7.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-reflect:1.7.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-core:1.7.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.javassist:javassist:3.21.0-GA" level="project" />
|
||||||
|
<orderEntry type="library" scope="PROVIDED" name="Maven: junit:junit:4.12" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-api-mockito:1.7.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-api-mockito-common:1.7.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-api-support:1.7.1" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.10.19" level="project" />
|
||||||
|
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" />
|
||||||
|
</component>
|
||||||
|
</module>
|
13
pom.xml
13
pom.xml
|
@ -12,6 +12,7 @@
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<powermock.version>1.7.1</powermock.version>
|
||||||
</properties>
|
</properties>
|
||||||
<build>
|
<build>
|
||||||
<defaultGoal>clean package install</defaultGoal>
|
<defaultGoal>clean package install</defaultGoal>
|
||||||
|
@ -75,6 +76,18 @@
|
||||||
<version>1.12.2-R0.1-SNAPSHOT</version>
|
<version>1.12.2-R0.1-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.powermock</groupId>
|
||||||
|
<artifactId>powermock-module-junit4</artifactId>
|
||||||
|
<version>${powermock.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.powermock</groupId>
|
||||||
|
<artifactId>powermock-api-mockito</artifactId>
|
||||||
|
<version>${powermock.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<repositories>
|
<repositories>
|
||||||
<repository>
|
<repository>
|
||||||
|
|
|
@ -104,7 +104,7 @@ public class BSkyBlock extends JavaPlugin {
|
||||||
registerListeners();
|
registerListeners();
|
||||||
|
|
||||||
// Load Flags
|
// Load Flags
|
||||||
flagsManager = new FlagsManager();
|
flagsManager = new FlagsManager(plugin);
|
||||||
|
|
||||||
// Load addons
|
// Load addons
|
||||||
addonsManager = new AddonsManager(plugin);
|
addonsManager = new AddonsManager(plugin);
|
||||||
|
|
|
@ -6,6 +6,7 @@ import org.bukkit.event.Listener;
|
||||||
|
|
||||||
import us.tastybento.bskyblock.BSkyBlock;
|
import us.tastybento.bskyblock.BSkyBlock;
|
||||||
import us.tastybento.bskyblock.api.panels.PanelItem;
|
import us.tastybento.bskyblock.api.panels.PanelItem;
|
||||||
|
import us.tastybento.bskyblock.managers.FlagsManager;
|
||||||
|
|
||||||
public class Flag {
|
public class Flag {
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder;
|
||||||
public class FlagBuilder {
|
public class FlagBuilder {
|
||||||
|
|
||||||
private String id = "";
|
private String id = "";
|
||||||
private PanelItem icon = PanelItem.empty();
|
private PanelItem icon;
|
||||||
private Listener listener;
|
private Listener listener;
|
||||||
|
|
||||||
public FlagBuilder id(String id) {
|
public FlagBuilder id(String id) {
|
||||||
|
|
|
@ -19,13 +19,13 @@ public class PanelItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
private ItemStack icon;
|
private ItemStack icon;
|
||||||
private Optional<ClickHandler> clickHandler;
|
private ClickHandler clickHandler;
|
||||||
private List<String> description;
|
private List<String> description;
|
||||||
private String name;
|
private String name;
|
||||||
private boolean glow;
|
private boolean glow;
|
||||||
private ItemMeta meta;
|
private ItemMeta meta;
|
||||||
|
|
||||||
public PanelItem(ItemStack icon, String name, List<String> description, boolean glow, Optional<ClickHandler> clickHandler) {
|
public PanelItem(ItemStack icon, String name, List<String> description, boolean glow, ClickHandler clickHandler) {
|
||||||
this.icon = icon;
|
this.icon = icon;
|
||||||
// Get the meta
|
// Get the meta
|
||||||
meta = icon.getItemMeta();
|
meta = icon.getItemMeta();
|
||||||
|
@ -70,7 +70,7 @@ public class PanelItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<ClickHandler> getClickHandler() {
|
public Optional<ClickHandler> getClickHandler() {
|
||||||
return clickHandler;
|
return Optional.of(clickHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isGlow() {
|
public boolean isGlow() {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ public class PanelItemBuilder {
|
||||||
private String name = "";
|
private String name = "";
|
||||||
private List<String> description = new ArrayList<>();
|
private List<String> description = new ArrayList<>();
|
||||||
private boolean glow = false;
|
private boolean glow = false;
|
||||||
private Optional<PanelItem.ClickHandler> clickHandler = Optional.empty();
|
private PanelItem.ClickHandler clickHandler;
|
||||||
|
|
||||||
public PanelItemBuilder icon(Material icon) {
|
public PanelItemBuilder icon(Material icon) {
|
||||||
this.icon = new ItemStack(icon);
|
this.icon = new ItemStack(icon);
|
||||||
|
@ -54,11 +55,13 @@ public class PanelItemBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
public PanelItemBuilder clickHandler(ClickHandler clickHandler) {
|
public PanelItemBuilder clickHandler(ClickHandler clickHandler) {
|
||||||
this.clickHandler = Optional.of(clickHandler);
|
this.clickHandler = clickHandler;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PanelItem build() {
|
public PanelItem build() {
|
||||||
|
if (icon == null)
|
||||||
|
Bukkit.getLogger().info("DEBUG: icon is null");
|
||||||
return new PanelItem(icon, name, description, glow, clickHandler);
|
return new PanelItem(icon, name, description, glow, clickHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -88,7 +88,7 @@ public class Island implements DataObject {
|
||||||
private boolean spawn = false;
|
private boolean spawn = false;
|
||||||
private boolean purgeProtected = false;
|
private boolean purgeProtected = false;
|
||||||
//// Protection ////
|
//// Protection ////
|
||||||
private HashMap<Flag, Boolean> flags = new HashMap<>();
|
private HashMap<Flag, Integer> flags = new HashMap<>();
|
||||||
|
|
||||||
private int levelHandicap;
|
private int levelHandicap;
|
||||||
|
|
||||||
|
@ -159,23 +159,23 @@ public class Island implements DataObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Island Guard flag status
|
* Get the Island Guard flag ranking
|
||||||
* @param flag
|
* @param flag
|
||||||
* @return true or false, or false if flag is not in the list
|
* @return flag rank. Players must have at least this rank to bypass this flag
|
||||||
*/
|
*/
|
||||||
public boolean getFlag(Flag flag){
|
public int getFlag(Flag flag){
|
||||||
if(flags.containsKey(flag)) {
|
if(flags.containsKey(flag)) {
|
||||||
return flags.get(flag);
|
return flags.get(flag);
|
||||||
} else {
|
} else {
|
||||||
flags.put(flag, false);
|
flags.put(flag, RanksManager.MEMBER_RANK);
|
||||||
return false;
|
return RanksManager.MEMBER_RANK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the flags
|
* @return the flags
|
||||||
*/
|
*/
|
||||||
public HashMap<Flag, Boolean> getFlags() {
|
public HashMap<Flag, Integer> getFlags() {
|
||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -375,18 +375,18 @@ public class Island implements DataObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the Island Guard flag status
|
* Set the Island Guard flag rank
|
||||||
* @param flag
|
* @param flag
|
||||||
* @param value
|
* @param value
|
||||||
*/
|
*/
|
||||||
public void setFlag(Flag flag, boolean value){
|
public void setFlag(Flag flag, int value){
|
||||||
flags.put(flag, value);
|
flags.put(flag, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param flags the flags to set
|
* @param flags the flags to set
|
||||||
*/
|
*/
|
||||||
public void setFlags(HashMap<Flag, Boolean> flags) {
|
public void setFlags(HashMap<Flag, Integer> flags) {
|
||||||
this.flags = flags;
|
this.flags = flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -462,6 +462,12 @@ public class Island implements DataObject {
|
||||||
*/
|
*/
|
||||||
public void setOwner(UUID owner){
|
public void setOwner(UUID owner){
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
|
// Defensive code: demote any previous owner
|
||||||
|
for (Entry<UUID, Integer> en : members.entrySet()) {
|
||||||
|
if (en.getValue().equals(RanksManager.OWNER_RANK)) {
|
||||||
|
en.setValue(RanksManager.MEMBER_RANK);
|
||||||
|
}
|
||||||
|
}
|
||||||
this.members.put(owner, RanksManager.OWNER_RANK);
|
this.members.put(owner, RanksManager.OWNER_RANK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -516,16 +522,6 @@ public class Island implements DataObject {
|
||||||
this.world = world;
|
this.world = world;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Toggles the Island Guard flag status if it is in the list
|
|
||||||
* @param flag
|
|
||||||
*/
|
|
||||||
public void toggleFlag(Flag flag){
|
|
||||||
if(flags.containsKey(flag)) {
|
|
||||||
flags.put(flag, !flags.get(flag));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the levelHandicap
|
* @return the levelHandicap
|
||||||
*/
|
*/
|
||||||
|
@ -649,4 +645,14 @@ public class Island implements DataObject {
|
||||||
public void setRanks(HashMap<UUID, Integer> ranks) {
|
public void setRanks(HashMap<UUID, Integer> ranks) {
|
||||||
this.members = ranks;
|
this.members = ranks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a user is allowed to bypass the flag or not
|
||||||
|
* @param user - user
|
||||||
|
* @param flag - flag
|
||||||
|
* @return true if allowed, false if not
|
||||||
|
*/
|
||||||
|
public boolean isAllowed(User user, Flag flag) {
|
||||||
|
return (this.getRank(user) >= this.getFlag(flag)) ? true : false;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,68 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package us.tastybento.bskyblock.database.objects;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import us.tastybento.bskyblock.api.commands.User;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A bean to hold name to UUID lookup
|
|
||||||
* @author tastybento
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class NameToUUID implements DataObject {
|
|
||||||
|
|
||||||
public HashMap<String, UUID> namesToUUID;
|
|
||||||
|
|
||||||
public NameToUUID() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the namesToUUID
|
|
||||||
*/
|
|
||||||
public HashMap<String, UUID> getNamesToUUID() {
|
|
||||||
return namesToUUID;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param namesToUUID the namesToUUID to set
|
|
||||||
*/
|
|
||||||
public void setNamesToUUID(HashMap<String, UUID> namesToUUID) {
|
|
||||||
this.namesToUUID = namesToUUID;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see us.tastybento.bskyblock.database.objects.DataObject#getUniqueId()
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public String getUniqueId() {
|
|
||||||
return "names-uuid";
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see us.tastybento.bskyblock.database.objects.DataObject#setUniqueId(java.lang.String)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void setUniqueId(String uniqueId) {
|
|
||||||
// Do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add or update a name
|
|
||||||
* @param user
|
|
||||||
*/
|
|
||||||
public void addName(User user) {
|
|
||||||
this.namesToUUID.put(user.getName(), user.getUniqueId());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get UUID for name
|
|
||||||
* @param name
|
|
||||||
* @return UUID or null if not found
|
|
||||||
*/
|
|
||||||
public UUID getUUID(String name) {
|
|
||||||
return this.namesToUUID.get(name);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,7 +9,13 @@ import us.tastybento.bskyblock.BSkyBlock;
|
||||||
import us.tastybento.bskyblock.api.flags.Flag;
|
import us.tastybento.bskyblock.api.flags.Flag;
|
||||||
import us.tastybento.bskyblock.api.panels.PanelItem;
|
import us.tastybento.bskyblock.api.panels.PanelItem;
|
||||||
|
|
||||||
public final class FlagsManager {
|
public class FlagsManager {
|
||||||
|
|
||||||
|
private BSkyBlock plugin;
|
||||||
|
|
||||||
|
public FlagsManager(BSkyBlock plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
private List<Flag> flags = new ArrayList<>();
|
private List<Flag> flags = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -17,7 +23,7 @@ public final class FlagsManager {
|
||||||
//TODO all the security checks
|
//TODO all the security checks
|
||||||
flags.add(flag);
|
flags.add(flag);
|
||||||
// If there is a listener, register it into Bukkit.
|
// If there is a listener, register it into Bukkit.
|
||||||
flag.getListener().ifPresent(l -> Bukkit.getPluginManager().registerEvents(l, BSkyBlock.getInstance()));
|
flag.getListener().ifPresent(l -> plugin.getServer().getPluginManager().registerEvents(l, plugin));
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Flag> getFlags() {
|
public List<Flag> getFlags() {
|
||||||
|
|
|
@ -0,0 +1,215 @@
|
||||||
|
package us.tastybento.bskyblock.util;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A very simplistic yml parser, that only do the following:
|
||||||
|
* <ol>
|
||||||
|
* <li>Keep track of indentation-levels and sections.</li>
|
||||||
|
* <li>Handle comments.</li>
|
||||||
|
* </ol>
|
||||||
|
*/
|
||||||
|
public class YmlCommentParser {
|
||||||
|
private static final Logger log = Logger.getLogger(YmlCommentParser.class.getName());
|
||||||
|
private static final Pattern SECTION_PATTERN = Pattern.compile("^(?<indent>\\s*)(?<name>[^ \\-][^:]*):(?<value>[^#]*)?(?<comment>#.*)?");
|
||||||
|
private static final Pattern COMMENT_PATTERN = Pattern.compile("^(?<indent>\\s*)(?<comment>#.*)");
|
||||||
|
private Map<String, String> commentMap = new HashMap<>();
|
||||||
|
|
||||||
|
public Map<String, String> getCommentMap() {
|
||||||
|
return Collections.unmodifiableMap(commentMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void addComment(String path, String comment) {
|
||||||
|
commentMap.put(path, comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addComments(Map<String,String> comments) {
|
||||||
|
commentMap.putAll(comments);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readLines(BufferedReader rdr) throws IOException {
|
||||||
|
int indentLevel = 0;
|
||||||
|
Deque<Section> sections = new ArrayDeque<>();
|
||||||
|
StringBuilder comments = new StringBuilder();
|
||||||
|
String baseKey = null;
|
||||||
|
sections.add(new Section(null, 0));
|
||||||
|
String line;
|
||||||
|
int lineNum = 1;
|
||||||
|
boolean isFirstAfterSection = true;
|
||||||
|
while ((line = rdr.readLine()) != null) {
|
||||||
|
Matcher commentM = COMMENT_PATTERN.matcher(line);
|
||||||
|
Matcher sectionM = SECTION_PATTERN.matcher(line);
|
||||||
|
if (commentM.matches()) {
|
||||||
|
comments.append(commentM.group("comment") + "\n");
|
||||||
|
} else if (sectionM.matches()) {
|
||||||
|
String comment = sectionM.group("comment");
|
||||||
|
if (comment != null && !comment.trim().isEmpty()) {
|
||||||
|
comments.append(comment + "\n");
|
||||||
|
}
|
||||||
|
String name = sectionM.group("name").trim();
|
||||||
|
String value = sectionM.group("value");
|
||||||
|
String indent = sectionM.group("indent");
|
||||||
|
if (isFirstAfterSection && indent.length() > indentLevel) {
|
||||||
|
indentLevel = indent.length();
|
||||||
|
sections.peek().setIndentation(indentLevel);
|
||||||
|
} else if (indent.length() < indentLevel) {
|
||||||
|
while (indent.length() < indentLevel && !sections.isEmpty()) {
|
||||||
|
sections.pop();
|
||||||
|
baseKey = sections.peek().getPath();
|
||||||
|
indentLevel = sections.peek().getIndentation();
|
||||||
|
isFirstAfterSection = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String path = getPath(baseKey, name);
|
||||||
|
if (value != null && !value.trim().isEmpty()) {
|
||||||
|
// Scalar with value
|
||||||
|
addComments(path, comments);
|
||||||
|
if (!isFirstAfterSection && indent.length() > indentLevel) {
|
||||||
|
log.warning("line " + lineNum + ": mixed indentation, expected " + indentLevel + " but got " + indent.length());
|
||||||
|
}
|
||||||
|
isFirstAfterSection = false;
|
||||||
|
} else if (indent.length() >= indentLevel) {
|
||||||
|
indentLevel = indent.length();
|
||||||
|
sections.push(createSection(path, indentLevel, comments));
|
||||||
|
baseKey = path;
|
||||||
|
isFirstAfterSection = true;
|
||||||
|
}
|
||||||
|
} else if (line.trim().isEmpty()) {
|
||||||
|
// Currently gathered comments are reset - they are "floating", decoupled from sections.
|
||||||
|
comments.setLength(0);
|
||||||
|
comments.trimToSize();
|
||||||
|
}
|
||||||
|
lineNum++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getPath(String baseKey, String name) {
|
||||||
|
return baseKey != null ? baseKey + "." + name : name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Section createSection(String path, int indentLevel, StringBuilder comments) {
|
||||||
|
Section section = new Section(path, indentLevel);
|
||||||
|
addComments(path, comments);
|
||||||
|
return section;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addComments(String path, StringBuilder comments) {
|
||||||
|
if (comments.length() > 0) {
|
||||||
|
commentMap.put(path, comments.toString());
|
||||||
|
comments.setLength(0);
|
||||||
|
comments.trimToSize();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getComment(String path) {
|
||||||
|
return commentMap.get(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges the comments into the "pure" yml.
|
||||||
|
* @param ymlPure A YML data-tree, without comments.
|
||||||
|
* @return A YML data-tree including comments.
|
||||||
|
*/
|
||||||
|
public String mergeComments(String ymlPure) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
boolean isFirstAfterSection = true;
|
||||||
|
Deque<Section> sections = new ArrayDeque<>();
|
||||||
|
sections.push(new Section(null, 0));
|
||||||
|
int indentLevel = 0;
|
||||||
|
String baseKey = null;
|
||||||
|
int lineNum = 1;
|
||||||
|
// First section shares comments with the header - so ignore that one
|
||||||
|
boolean isHeader = true;
|
||||||
|
for (String line : ymlPure.split("\n")) {
|
||||||
|
// Skip header
|
||||||
|
Matcher commentM = COMMENT_PATTERN.matcher(line);
|
||||||
|
if (isHeader && (commentM.matches() || line.trim().isEmpty())) {
|
||||||
|
continue; // Skip header
|
||||||
|
}
|
||||||
|
isHeader = false;
|
||||||
|
Matcher sectionM = SECTION_PATTERN.matcher(line);
|
||||||
|
if (sectionM.matches()) {
|
||||||
|
String name = sectionM.group("name").trim();
|
||||||
|
String value = sectionM.group("value");
|
||||||
|
String indent = sectionM.group("indent");
|
||||||
|
if (isFirstAfterSection && indent.length() > indentLevel) {
|
||||||
|
indentLevel = indent.length();
|
||||||
|
sections.peek().setIndentation(indentLevel);
|
||||||
|
} else if (indent.length() < indentLevel) {
|
||||||
|
while (indent.length() < indentLevel && !sections.isEmpty()) {
|
||||||
|
sections.pop();
|
||||||
|
baseKey = sections.peek().getPath();
|
||||||
|
indentLevel = sections.peek().getIndentation();
|
||||||
|
isFirstAfterSection = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String path = getPath(baseKey, name);
|
||||||
|
String comment = getComment(path);
|
||||||
|
if (comment != null) {
|
||||||
|
sb.append((lineNum > 1 ? "\n" : "") + comment
|
||||||
|
.replaceAll("^#", Matcher.quoteReplacement(indent + "#"))
|
||||||
|
.replaceAll("\n#", Matcher.quoteReplacement("\n" + indent + "#")));
|
||||||
|
}
|
||||||
|
if (value != null && !value.trim().isEmpty()) {
|
||||||
|
// Scalar with value
|
||||||
|
isFirstAfterSection = false;
|
||||||
|
} else if (indent.length() >= indentLevel) {
|
||||||
|
indentLevel = indent.length();
|
||||||
|
sections.push(new Section(path, indentLevel));
|
||||||
|
baseKey = path;
|
||||||
|
isFirstAfterSection = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lineNum++;
|
||||||
|
sb.append(line + "\n");
|
||||||
|
}
|
||||||
|
return sb.toString().replaceAll("\r\n", "\n").replaceAll("\n\r", "\n").replaceAll("\n", "\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load(Reader reader) throws IOException {
|
||||||
|
readLines(new BufferedReader(reader));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadFromString(String contents) {
|
||||||
|
try {
|
||||||
|
readLines(new BufferedReader(new StringReader(contents)));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException("Unable to read from string", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static class Section {
|
||||||
|
private int indentation;
|
||||||
|
private final String path;
|
||||||
|
|
||||||
|
private Section(String name, int indentation) {
|
||||||
|
this.indentation = indentation;
|
||||||
|
this.path = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getIndentation() {
|
||||||
|
return indentation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIndentation(int indentLevel) {
|
||||||
|
indentation = indentLevel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package us.tastybento.bskyblock.util;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A YamlConfiguration that supports comments
|
||||||
|
*
|
||||||
|
* Note: This includes a VERY SIMPLISTIC Yaml-parser, which sole purpose is to detect and store comments.
|
||||||
|
*/
|
||||||
|
public class YmlConfiguration extends YamlConfiguration {
|
||||||
|
private YmlCommentParser commentParser = new YmlCommentParser();
|
||||||
|
|
||||||
|
public String getComment(String key) {
|
||||||
|
String comment = commentParser.getComment(key);
|
||||||
|
return comment != null ? comment.replaceAll("^# ?", "").replaceAll("\n# ?", "") : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String,String> getComments() {
|
||||||
|
return commentParser.getCommentMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addComment(String path, String comment) {
|
||||||
|
commentParser.addComment(path, comment);
|
||||||
|
}
|
||||||
|
public void addComments(Map<String,String> comments) {
|
||||||
|
commentParser.addComments(comments);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadFromString(String contents) throws InvalidConfigurationException {
|
||||||
|
super.loadFromString(contents);
|
||||||
|
commentParser.loadFromString(contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String saveToString() {
|
||||||
|
String ymlPure = super.saveToString();
|
||||||
|
return commentParser.mergeComments(ymlPure);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,33 +1,22 @@
|
||||||
package bskyblock;
|
package bskyblock;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNotSame;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
|
import org.bukkit.inventory.ItemFactory;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
import org.powermock.api.mockito.PowerMockito;
|
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||||
|
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
|
||||||
|
import org.powermock.modules.junit4.PowerMockRunner;
|
||||||
import us.tastybento.bskyblock.BSkyBlock;
|
import us.tastybento.bskyblock.BSkyBlock;
|
||||||
import us.tastybento.bskyblock.Constants;
|
import us.tastybento.bskyblock.Constants;
|
||||||
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
import us.tastybento.bskyblock.api.commands.CompositeCommand;
|
||||||
|
@ -35,15 +24,30 @@ import us.tastybento.bskyblock.api.commands.User;
|
||||||
import us.tastybento.bskyblock.api.events.IslandBaseEvent;
|
import us.tastybento.bskyblock.api.events.IslandBaseEvent;
|
||||||
import us.tastybento.bskyblock.api.events.team.TeamEvent;
|
import us.tastybento.bskyblock.api.events.team.TeamEvent;
|
||||||
import us.tastybento.bskyblock.database.objects.Island;
|
import us.tastybento.bskyblock.database.objects.Island;
|
||||||
|
import us.tastybento.bskyblock.lists.Flags;
|
||||||
|
import us.tastybento.bskyblock.managers.FlagsManager;
|
||||||
import us.tastybento.bskyblock.managers.RanksManager;
|
import us.tastybento.bskyblock.managers.RanksManager;
|
||||||
import us.tastybento.bskyblock.util.Util;
|
import us.tastybento.bskyblock.util.Util;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||||
|
|
||||||
|
//@RunWith(PowerMockRunner.class)
|
||||||
|
//@SuppressStaticInitializationFor("us.tastybento.BSkyBlock")
|
||||||
|
//@PrepareForTest( { BSkyBlock.class })
|
||||||
public class TestBSkyBlock {
|
public class TestBSkyBlock {
|
||||||
private final UUID playerUUID = UUID.randomUUID();
|
private final UUID playerUUID = UUID.randomUUID();
|
||||||
private static CommandSender sender;
|
private static CommandSender sender;
|
||||||
private static Player player;
|
private static Player player;
|
||||||
private static Location location;
|
private static Location location;
|
||||||
private static BSkyBlock plugin;
|
private static BSkyBlock plugin;
|
||||||
|
private static FlagsManager flagsManager;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void setUp() {
|
public static void setUp() {
|
||||||
|
@ -72,7 +76,19 @@ public class TestBSkyBlock {
|
||||||
Mockito.when(location.getBlockX()).thenReturn(0);
|
Mockito.when(location.getBlockX()).thenReturn(0);
|
||||||
Mockito.when(location.getBlockY()).thenReturn(0);
|
Mockito.when(location.getBlockY()).thenReturn(0);
|
||||||
Mockito.when(location.getBlockZ()).thenReturn(0);
|
Mockito.when(location.getBlockZ()).thenReturn(0);
|
||||||
|
|
||||||
|
// This doesn't work!
|
||||||
|
/*
|
||||||
|
mockStatic(Bukkit.class);
|
||||||
|
ItemFactory itemFactory = PowerMockito.mock(ItemFactory.class);
|
||||||
|
PowerMockito.when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||||
|
PowerMockito.when(itemFactory.getItemMeta(any())).thenReturn(PowerMockito.mock(ItemMeta.class));
|
||||||
|
|
||||||
|
mockStatic(BSkyBlock.class);
|
||||||
|
flagsManager = mock(FlagsManager.class);
|
||||||
|
PowerMockito.when(BSkyBlock.getInstance()).thenReturn(plugin);
|
||||||
|
Mockito.when(plugin.getFlagsManager()).thenReturn(flagsManager);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -265,8 +281,7 @@ public class TestBSkyBlock {
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(User user, List<String> args) {
|
public boolean execute(User user, List<String> args) {
|
||||||
Bukkit.getLogger().info("args are " + args.toString());
|
Bukkit.getLogger().info("args are " + args.toString());
|
||||||
if (args.size() == 3) return true;
|
return args.size() == 3 ? true : false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -325,8 +340,37 @@ public class TestBSkyBlock {
|
||||||
island.removeFromBanList(member1);
|
island.removeFromBanList(member1);
|
||||||
assertFalse(island.getBanned().contains(member1));
|
assertFalse(island.getBanned().contains(member1));
|
||||||
|
|
||||||
|
// Protection
|
||||||
//island.isAllowed(visitor, Flags.BREAK_BLOCKS);
|
// Set up protection settings - members can break blocks, visitors and place blocks
|
||||||
|
// These tests do not work because of static method calls in the code and Bukkit.
|
||||||
|
/*
|
||||||
|
island.setFlag(Flags.BREAK_BLOCKS, RanksManager.MEMBER_RANK);
|
||||||
|
island.setFlag(Flags.PLACE_BLOCKS, RanksManager.VISITOR_RANK);
|
||||||
|
|
||||||
|
// Owner should be able to do anything
|
||||||
|
assertTrue(island.isAllowed(owner, Flags.PLACE_BLOCKS));
|
||||||
|
assertTrue(island.isAllowed(owner, Flags.BREAK_BLOCKS));
|
||||||
|
|
||||||
|
// Visitor can only place blocks
|
||||||
|
assertTrue(island.isAllowed(visitor, Flags.PLACE_BLOCKS));
|
||||||
|
assertFalse(island.isAllowed(visitor, Flags.BREAK_BLOCKS));
|
||||||
|
|
||||||
|
// Check if the members have capability
|
||||||
|
User mem1 = User.getInstance(member1);
|
||||||
|
User mem2 = User.getInstance(member2);
|
||||||
|
User mem3 = User.getInstance(member3);
|
||||||
|
|
||||||
|
assertTrue(island.isAllowed(mem1, Flags.PLACE_BLOCKS));
|
||||||
|
assertTrue(island.isAllowed(mem1, Flags.BREAK_BLOCKS));
|
||||||
|
|
||||||
|
assertTrue(island.isAllowed(mem2, Flags.PLACE_BLOCKS));
|
||||||
|
assertTrue(island.isAllowed(mem2, Flags.BREAK_BLOCKS));
|
||||||
|
|
||||||
|
// Member 3 is no longer a member and is a visitor
|
||||||
|
assertTrue(island.isAllowed(mem3, Flags.PLACE_BLOCKS));
|
||||||
|
assertTrue(island.isAllowed(mem3, Flags.BREAK_BLOCKS));
|
||||||
|
|
||||||
|
*/
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Score approach:
|
* Score approach:
|
||||||
|
|
Loading…
Reference in New Issue