Skip to content

Commit

Permalink
Parse $n arguments in left side of rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wertik committed Jun 14, 2022
1 parent 8c01ac8 commit a1cb4d7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 36 deletions.
14 changes: 7 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>space.devport.wertik.conditionaltext</groupId>
<artifactId>ConditionalTextPlaceholders</artifactId>
<version>1.4.0</version>
<version>1.5.0-SNAPSHOT</version>

<name>ConditionalTextPlaceholders</name>

Expand Down Expand Up @@ -67,32 +67,32 @@
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>devport</id>
<url>https://nexus.devport.space/repository/devport-public/</url>
<id>devport-snapshots</id>
<url>https://nexus.devport.space/snapshots/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.17.1-R0.1-SNAPSHOT</version>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>21.0.1</version>
<version>23.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ public static Condition fromString(String input) {
return operator == null ? null : new Condition(ParseUtil.parseObject(input), operator);
}

public boolean check(Object value, @Nullable OfflinePlayer player) {
public boolean check(Object value, @Nullable OfflinePlayer player, String... arguments) {
Object required = this.required;

// Parse placeholders in String requirements.
if (required instanceof String)
required = ParseUtil.parseObject(PlaceholderUtil.parsePlaceholders(player, (String) required));
if (required instanceof String) {
required = PlaceholderUtil.parsePlaceholders(player, (String) required); // Parse PAPI
required = ParseUtil.parseArguments((String) required, arguments); // Parse $n
required = ParseUtil.parseObject((String) required); // Parse the type again
}

boolean out = operator.apply(value, required);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static Rule fromString(String input) {
return condition == null ? null : new Rule(arr.length > 1 ? arr[1] : "", condition);
}

public boolean check(Object value, @Nullable OfflinePlayer player) {
return !hasCondition() || condition.check(value, player);
public boolean check(Object value, @Nullable OfflinePlayer player, String... arguments) {
return !hasCondition() || condition.check(value, player, arguments);
}

public boolean hasCondition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jetbrains.annotations.Nullable;
import space.devport.dock.configuration.Configuration;
import space.devport.dock.util.StringUtil;
import space.devport.wertik.conditionaltext.system.utils.ParseUtil;
import space.devport.wertik.conditionaltext.system.utils.PlaceholderUtil;

import java.util.Collection;
Expand Down Expand Up @@ -68,28 +69,29 @@ public void addRule(Rule rule) {
rules.add(rule);
}

// Run rules for given Object value.
@Nullable
public String process(@Nullable OfflinePlayer player, Object value) {
for (Rule rule : rules) {
if (rule.check(value, player))
return rule.getOutput();
}
return null;
}

// Process the given value and output the formatted text based on rules.
@Nullable
public String process(@Nullable OfflinePlayer player, Object value, String... arguments) {

// Actually process the value into an output
String output = process(player, value);
String output = null;

for (Rule rule : rules) {
if (rule.check(value, player, arguments)) {
output = rule.getOutput();
break;
}
}

if (output == null) {
return null;
}

// Parse arguments again (in case there were some in the output)
output = parseArguments(output, arguments);
output = ParseUtil.parseArguments(output, arguments);

// Parse PAPI again and color the output.
return StringUtil.color(output != null ? PlaceholderAPI.setPlaceholders(player, output) : null);
return StringUtil.color(PlaceholderAPI.setPlaceholders(player, output));
}

@Nullable
Expand All @@ -106,20 +108,11 @@ public String process(@Nullable OfflinePlayer player, String valueString, String
public String process(@Nullable OfflinePlayer player, String... arguments) {

// Replace $n
String placeholder = parseArguments(this.placeholder, arguments);
String placeholder = ParseUtil.parseArguments(this.placeholder, arguments);

// Parse the placeholder into a comparable object.
Object value = PlaceholderUtil.parsePlaceholderIntoObject(player, placeholder);

return process(player, value, arguments);
}

// Replace $n with arguments from the placeholder.
private String parseArguments(String string, String[] arguments) {
for (int n = 0; n < arguments.length; n++) {
String argument = arguments[n];
string = string.replace("$" + n, argument);
}
return string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ public Object parseObject(@NotNull String input) {

return input;
}

// Replace $n with arguments from the placeholder.
public String parseArguments(String string, String[] arguments) {
for (int n = 0; n < arguments.length; n++) {
String argument = arguments[n];
string = string.replace("$" + n, argument);
}
return string;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ${project.name}
main: ${project.groupId}.ConditionalTextPlugin
version: ${project.version}
authors: [ qwz ]
softdepend: [ PlaceholderAPI ]
depend: [ PlaceholderAPI ]
api-version: 1.13
commands:
conditionaltext:
Expand Down

0 comments on commit a1cb4d7

Please sign in to comment.