-
Notifications
You must be signed in to change notification settings - Fork 14
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
Showing
10 changed files
with
279 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>fr.utarwyn</groupId> | ||
<artifactId>endercontainers</artifactId> | ||
<version>2.2.3-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>endercontainers-dependency-factionsuuid</artifactId> | ||
<name>EnderContainers Dependency FactionsUUID</name> | ||
|
||
<properties> | ||
<factions.version>1.6.9.5-U0.6.6</factions.version> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>enderzone</id> | ||
<url>https://ci.ender.zone/plugin/repository/everything/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>endercontainers-api</artifactId> | ||
<version>2.2.3-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.massivecraft</groupId> | ||
<artifactId>Factions</artifactId> | ||
<version>${factions.version}</version> | ||
<scope>provided</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
</project> |
80 changes: 80 additions & 0 deletions
80
...tionsuuid/src/main/java/fr/utarwyn/endercontainers/dependency/FactionsUUIDDependency.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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package fr.utarwyn.endercontainers.dependency; | ||
|
||
import com.massivecraft.factions.*; | ||
import com.massivecraft.factions.perms.PermissibleActions; | ||
import fr.utarwyn.endercontainers.configuration.LocaleKey; | ||
import fr.utarwyn.endercontainers.dependency.exceptions.BlockChestOpeningException; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.Collections; | ||
|
||
/** | ||
* FactionsUUID dependency. Protect enderchests in enemy factions. | ||
* Works with FactionsUUID: https://www.spigotmc.org/resources/factionsuuid.1035/ | ||
* | ||
* @author Utarwyn | ||
* @since 2.2.3 | ||
*/ | ||
public class FactionsUUIDDependency extends Dependency { | ||
|
||
/** | ||
* Construct the FactionsUUID dependency object. | ||
* | ||
* @param plugin plugin instance | ||
*/ | ||
public FactionsUUIDDependency(Plugin plugin) { | ||
super(plugin); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void validateBlockChestOpening(Block block, Player player) | ||
throws BlockChestOpeningException { | ||
FPlayer fPlayer = FPlayers.getInstance().getByPlayer(player); | ||
|
||
// Bypass the check? | ||
if (fPlayer == null || fPlayer.isAdminBypassing()) return; | ||
|
||
// Init checking variables | ||
Faction playerFac = fPlayer.getFaction(); | ||
Faction currentFac = Board.getInstance().getFactionAt(new FLocation(block)); | ||
|
||
boolean playerFacIsReal = this.isRealFaction(playerFac); | ||
boolean currentFacIsReal = this.isRealFaction(currentFac); | ||
|
||
// Check permission between the two factions if there are real | ||
if (playerFacIsReal && currentFacIsReal) { | ||
if (currentFac != playerFac || !playerFac.hasAccess(fPlayer, PermissibleActions.CONTAINER, new FLocation(block))) { | ||
// Exception without message error in this case | ||
throw new BlockChestOpeningException(); | ||
} | ||
} | ||
// If the current player does not have a faction | ||
// but trying to open a chest in a real faction | ||
else if (currentFacIsReal) { | ||
throw new BlockChestOpeningException( | ||
LocaleKey.ERR_DEP_FACTIONS, | ||
Collections.singletonMap("faction", currentFac.getTag()) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a faction is real or fictive. | ||
* Handles Warzone, Safezone and Wilderness. | ||
* | ||
* @param faction faction to check | ||
* @return true if the faction is real | ||
*/ | ||
private boolean isRealFaction(Faction faction) { | ||
return faction != null | ||
&& !faction.isWilderness() | ||
&& !faction.isWarZone() | ||
&& !faction.isSafeZone(); | ||
} | ||
|
||
} |
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 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,15 +1,15 @@ | ||
package fr.utarwyn.endercontainers.dependency; | ||
package fr.utarwyn.endercontainers.dependency.resolve; | ||
|
||
import fr.utarwyn.endercontainers.dependency.Dependency; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.plugin.PluginManager; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Resolve a dependency instance with a custom version matching. | ||
* Resolve a dependency instance using custom matching patterns. | ||
* | ||
* @author Utarwyn <[email protected]> | ||
* @since 2.2.0 | ||
|
@@ -24,7 +24,7 @@ public class DependencyResolver { | |
/** | ||
* Patterns to match dependency's versions | ||
*/ | ||
private final Map<Pattern, Class<? extends Dependency>> patterns; | ||
private final List<DependencyResolverPattern> patterns; | ||
|
||
/** | ||
* Name of the dependency instance to build | ||
|
@@ -38,7 +38,7 @@ public class DependencyResolver { | |
*/ | ||
public DependencyResolver(PluginManager pluginManager) { | ||
this.pluginManager = pluginManager; | ||
this.patterns = new HashMap<>(); | ||
this.patterns = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
|
@@ -61,7 +61,19 @@ public DependencyResolver name(String name) { | |
* @return this instance | ||
*/ | ||
public DependencyResolver matchVersion(String expression, Class<? extends Dependency> clazz) { | ||
this.patterns.put(Pattern.compile(expression), clazz); | ||
this.patterns.add(new DependencyResolverPatternVersion(expression, clazz)); | ||
return this; | ||
} | ||
|
||
/** | ||
* Match a specific dependency class from an author name. | ||
* | ||
* @param author dependency author to match | ||
* @param clazz class to use for for the targeted author | ||
* @return this instance | ||
*/ | ||
public DependencyResolver matchAuthor(String author, Class<? extends Dependency> clazz) { | ||
this.patterns.add(new DependencyResolverPatternAuthor(author, clazz)); | ||
return this; | ||
} | ||
|
||
|
@@ -103,19 +115,10 @@ public Optional<Dependency> resolve() { | |
* @return dependency instance if present | ||
*/ | ||
private Optional<Dependency> constructInstance(Plugin plugin) { | ||
String pluginVersion = plugin.getDescription().getVersion(); | ||
|
||
return this.patterns.entrySet().stream() | ||
.filter(entry -> entry.getKey().matcher(pluginVersion).find()) | ||
.map(Map.Entry::getValue) | ||
return this.patterns.stream() | ||
.filter(pattern -> pattern.matchWith(plugin)) | ||
.findFirst() | ||
.map(clazz -> { | ||
try { | ||
return clazz.getDeclaredConstructor(Plugin.class).newInstance(plugin); | ||
} catch (ReflectiveOperationException e) { | ||
return null; | ||
} | ||
}); | ||
.map(pattern -> pattern.construct(plugin)); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...rc/main/java/fr/utarwyn/endercontainers/dependency/resolve/DependencyResolverPattern.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package fr.utarwyn.endercontainers.dependency.resolve; | ||
|
||
import fr.utarwyn.endercontainers.dependency.Dependency; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
/** | ||
* Represents a custom matching | ||
* pattern used to resolve a dependency. | ||
* | ||
* @author Utarwyn | ||
* @since 2.2.3 | ||
*/ | ||
public abstract class DependencyResolverPattern { | ||
|
||
private final Class<? extends Dependency> clazz; | ||
|
||
public DependencyResolverPattern(Class<? extends Dependency> clazz) { | ||
this.clazz = clazz; | ||
} | ||
|
||
/** | ||
* Checks if a plugin matches with stored resolving pattern. | ||
* | ||
* @param plugin plugin to check | ||
* @return true if plugin matches this pattern | ||
*/ | ||
public abstract boolean matchWith(Plugin plugin); | ||
|
||
/** | ||
* Constructs an instance of registered dependency class for this matcher. | ||
* | ||
* @param plugin plugin managed by the dependency | ||
* @return constructed dependency instance | ||
*/ | ||
public Dependency construct(Plugin plugin) { | ||
try { | ||
return this.clazz.getDeclaredConstructor(Plugin.class).newInstance(plugin); | ||
} catch (ReflectiveOperationException e) { | ||
throw new IllegalStateException("cannot instanciate dependency class", e); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...n/java/fr/utarwyn/endercontainers/dependency/resolve/DependencyResolverPatternAuthor.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package fr.utarwyn.endercontainers.dependency.resolve; | ||
|
||
import fr.utarwyn.endercontainers.dependency.Dependency; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
/** | ||
* Represents a dependency resolver using an author pattern. | ||
* | ||
* @author Utarwyn | ||
* @since 2.2.3 | ||
*/ | ||
public class DependencyResolverPatternAuthor extends DependencyResolverPattern { | ||
|
||
private final String author; | ||
|
||
public DependencyResolverPatternAuthor(String author, Class<? extends Dependency> clazz) { | ||
super(clazz); | ||
this.author = author; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean matchWith(Plugin plugin) { | ||
return plugin.getDescription().getAuthors().contains(this.author); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
.../java/fr/utarwyn/endercontainers/dependency/resolve/DependencyResolverPatternVersion.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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package fr.utarwyn.endercontainers.dependency.resolve; | ||
|
||
import fr.utarwyn.endercontainers.dependency.Dependency; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Represents a dependency resolver using a version pattern. | ||
* | ||
* @author Utarwyn | ||
* @since 2.2.3 | ||
*/ | ||
public class DependencyResolverPatternVersion extends DependencyResolverPattern { | ||
|
||
private final Pattern pattern; | ||
|
||
public DependencyResolverPatternVersion(String expression, Class<? extends Dependency> clazz) { | ||
super(clazz); | ||
this.pattern = Pattern.compile(expression); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean matchWith(Plugin plugin) { | ||
return this.pattern.matcher(plugin.getDescription().getVersion()).find(); | ||
} | ||
|
||
} |
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.