Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rip.koru.axis.cosmetic;
- import com.mongodb.client.MongoCursor;
- import com.mongodb.client.model.Filters;
- import com.mongodb.client.model.UpdateOptions;
- import lombok.Data;
- import lombok.Getter;
- import org.bson.Document;
- import org.bukkit.Bukkit;
- import rip.koru.axis.AxisPlugin;
- import rip.koru.axis.utilities.chat.ColorText;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- @Data
- public class Tag {
- @Getter
- private static List<Tag> tags = new ArrayList<>();
- private String name, prefix, permission, section, serverName;
- public Tag(String name) {
- this.name = name;
- this.section = "DEFAULT";
- tags.add(this);
- }
- public static void loadTags() {
- try (MongoCursor mongoCursor = AxisPlugin.getPlugin().getAxisDatabase().getTags().find().iterator()) {
- while (mongoCursor.hasNext()) {
- Document document = (Document) mongoCursor.next();
- if (document.containsKey("serverName") && document.getString("serverName").equals(Bukkit.getServerName().toUpperCase().replace(" ", ""))) {
- Tag tag = new Tag(document.getString("name"));
- if (document.containsKey("prefix")) {
- tag.setPrefix(ColorText.translate(document.getString("prefix")));
- }
- if (document.containsKey("permission")) {
- tag.setPermission(document.getString("permission"));
- }
- if (document.containsKey("section")) {
- tag.setSection(document.getString("section"));
- }
- if (document.containsKey("serverName")) {
- tag.setServerName(document.getString("serverName"));
- }
- }
- }
- }
- }
- public void save() {
- Document document = new Document();
- document.put("name", name);
- if (prefix != null) {
- document.put("prefix", prefix);
- }
- if (permission != null) {
- document.put("permission", permission);
- }
- document.put("section", section);
- document.put("serverName", serverName.toUpperCase().replace(" ", ""));
- AxisPlugin.getPlugin().getAxisDatabase().getTags().replaceOne(Filters.eq("name", name), document, new UpdateOptions().upsert(true));
- }
- public void remove() {
- AxisPlugin.getPlugin().getAxisDatabase().getTags().deleteOne(Filters.eq("name", name));
- tags.remove(this);
- }
- public static List<Tag> getTagsBySection(String section) {
- return tags.stream().filter(tag -> tag.getSection().equalsIgnoreCase(section) && tag.getServerName().equals(Bukkit.getServerName().toUpperCase().replace(" ", ""))).collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement