Skip to content

Instantly share code, notes, and snippets.

@Magnum97
Last active May 6, 2021 06:28
Show Gist options
  • Save Magnum97/ebc0b3f8840b53251b70688cc41e1168 to your computer and use it in GitHub Desktop.
Save Magnum97/ebc0b3f8840b53251b70688cc41e1168 to your computer and use it in GitHub Desktop.
Bukkit / Spigot message sender with placeholder and prefix ability
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.User;
import com.google.common.collect.Maps;
import de.leonhard.storage.Toml;
import org.apache.commons.lang.text.StrSubstitutor;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import java.util.Map;
import java.util.UUID;
public class Messages {
private static final Toml config = new Toml("messages",UnitedAnimals.getMain().getDataFolder().toString(),
UnitedAnimals.class.getClassLoader().getResourceAsStream("messages.toml"));
private final Map<String, Object> placeholders = Maps.newHashMap();
private final String path;
public static Messages of(String path) {
return new Messages(path);
}
private Messages(String path) {
this.path = path;
placeholders.put("prefix", config.get("prefix", ""));
}
public String build() {
String message = config.get(path, path);
message = replace(message);
return ChatColor.translateAlternateColorCodes('&', message);
}
public Messages vars(Map<String, Object> placeholders) {
this.placeholders.putAll(placeholders);
return this;
}
public Messages var(String name, Object value) {
placeholders.put(name, value);
return this;
}
public void send(CommandSender sender) {
if (sender != null)
sender.sendMessage(build());
}
public void sendOffline(OfflinePlayer player) {
if (player.isOnline())
send(player.getPlayer());
}
public void asMail(OfflinePlayer player) {
asMail(player.getUniqueId());
}
public void asMail(UUID uuid) {
Essentials essentials = UnitedAnimals.getMain().getEssentials();
if (essentials == null)
return;
User user = essentials.getUser(uuid);
if (user == null)
return;
String builded = build();
if (user.getMails().contains(builded))
return;
user.addMail(builded);
user.notifyOfMail();
}
private String replace(String string) {
return StrSubstitutor.replace(string, placeholders, "%", "%");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment