Added SimplePlaceholder and SimpleInjectablePlaceholder
This commit is contained in:
@@ -64,7 +64,10 @@ public final class StaticPlaceholder implements InjectablePlaceholder {
|
||||
@Override
|
||||
public String tryTranslateQuickly(@NotNull final String text,
|
||||
@NotNull final PlaceholderContext context) {
|
||||
return text.replace("%" + this.identifier + "%", this.getValue(this.identifier, context));
|
||||
return text.replace(
|
||||
"%" + this.identifier + "%",
|
||||
Objects.requireNonNullElse(this.getValue(this.identifier, context), "")
|
||||
);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.willfp.eco.core.placeholder.templates;
|
||||
|
||||
import com.willfp.eco.core.placeholder.InjectablePlaceholder;
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A template class for simple placeholders.
|
||||
*/
|
||||
public abstract class SimpleInjectablePlaceholder implements InjectablePlaceholder {
|
||||
/**
|
||||
* The name of the placeholder.
|
||||
*/
|
||||
private final String identifier;
|
||||
|
||||
/**
|
||||
* The placeholder pattern.
|
||||
*/
|
||||
private final Pattern pattern;
|
||||
|
||||
/**
|
||||
* Create a new simple injectable placeholder.
|
||||
*
|
||||
* @param identifier The identifier.
|
||||
*/
|
||||
protected SimpleInjectablePlaceholder(@NotNull final String identifier) {
|
||||
this.identifier = identifier;
|
||||
this.pattern = Pattern.compile(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tryTranslateQuickly(@NotNull final String text,
|
||||
@NotNull final PlaceholderContext context) {
|
||||
return text.replace(
|
||||
"%" + this.identifier + "%",
|
||||
Objects.requireNonNullElse(this.getValue(this.identifier, context), "")
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Pattern getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimpleInjectablePlaceholder{" +
|
||||
"identifier='" + identifier + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@NotNull final Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof SimpleInjectablePlaceholder that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(identifier, that.identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(identifier);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.willfp.eco.core.placeholder.templates;
|
||||
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import com.willfp.eco.core.placeholder.RegistrablePlaceholder;
|
||||
import com.willfp.eco.core.placeholder.context.PlaceholderContext;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A template class for simple placeholders.
|
||||
*/
|
||||
public abstract class SimplePlaceholder implements RegistrablePlaceholder {
|
||||
/**
|
||||
* The plugin.
|
||||
*/
|
||||
private final EcoPlugin plugin;
|
||||
|
||||
/**
|
||||
* The name of the placeholder.
|
||||
*/
|
||||
private final String identifier;
|
||||
|
||||
/**
|
||||
* The placeholder pattern.
|
||||
*/
|
||||
private final Pattern pattern;
|
||||
|
||||
/**
|
||||
* Create a new simple placeholder.
|
||||
*
|
||||
* @param plugin The plugin.
|
||||
* @param identifier The identifier.
|
||||
*/
|
||||
protected SimplePlaceholder(@NotNull final EcoPlugin plugin,
|
||||
@NotNull final String identifier) {
|
||||
this.plugin = plugin;
|
||||
this.identifier = identifier;
|
||||
this.pattern = Pattern.compile(identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tryTranslateQuickly(@NotNull final String text,
|
||||
@NotNull final PlaceholderContext context) {
|
||||
return text.replace(
|
||||
"%" + this.identifier + "%",
|
||||
Objects.requireNonNullElse(this.getValue(this.identifier, context), "")
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Pattern getPattern() {
|
||||
return pattern;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public EcoPlugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SimplePlaceholder{" +
|
||||
"plugin='" + plugin + '\'' +
|
||||
"identifier='" + identifier + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@NotNull final Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!(o instanceof SimpleInjectablePlaceholder that)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Objects.equals(pattern, that.getPattern())
|
||||
&& Objects.equals(plugin, that.getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(identifier, plugin);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user