Last active
May 6, 2021 06:28
-
-
Save Magnum97/ebc0b3f8840b53251b70688cc41e1168 to your computer and use it in GitHub Desktop.
Bukkit / Spigot message sender with placeholder and prefix ability
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
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