Skip to content

Instantly share code, notes, and snippets.

@hamza-cskn
Created May 29, 2022 16:41
Show Gist options
  • Save hamza-cskn/3ea04baa594cc2a42661754d45fc960e to your computer and use it in GitHub Desktop.
Save hamza-cskn/3ea04baa594cc2a42661754d45fc960e to your computer and use it in GitHub Desktop.
PlaceholderUtil
package mc.obliviate.masterduels.utils.placeholder;
public class InternalPlaceholder {
private final String placeholder;
private final String value;
protected InternalPlaceholder(final String placeholder, final String value) {
this.placeholder = placeholder;
this.value = value;
}
public String getPlaceholder() {
return placeholder;
}
public String getValue() {
if (value == null) return "";
return value;
}
}
package mc.obliviate.masterduels.utils.placeholder;
import java.util.ArrayList;
import java.util.List;
public class PlaceholderUtil {
private final List<InternalPlaceholder> placeholders = new ArrayList<>();
public PlaceholderUtil add(final String key, final String value) {
placeholders.add(new InternalPlaceholder(key, value));
return this;
}
public List<InternalPlaceholder> getPlaceholders() {
return placeholders;
}
public List<String> apply(final List<String> texts) {
final List<String> result = new ArrayList<>();
for (final String text : texts) {
result.add(apply(text));
}
return result;
}
public String apply(String text) {
for (final InternalPlaceholder placeholder : placeholders) {
text = text.replace(placeholder.getPlaceholder(), placeholder.getValue());
}
return text;
}
}
@hamza-cskn
Copy link
Author

hamza-cskn commented May 29, 2022

Compatible with Message Utils gist.

Example Usage:

MessageUtils.sendMessage(player, "queue.queue-not-found", new PlaceholderUtil().add("{player}", player.getName()));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment