Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. package rip.koru.axis.cosmetic;
  2.  
  3. import com.mongodb.client.MongoCursor;
  4. import com.mongodb.client.model.Filters;
  5. import com.mongodb.client.model.UpdateOptions;
  6. import lombok.Data;
  7. import lombok.Getter;
  8. import org.bson.Document;
  9. import org.bukkit.Bukkit;
  10. import rip.koru.axis.AxisPlugin;
  11. import rip.koru.axis.utilities.chat.ColorText;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.stream.Collectors;
  16.  
  17. @Data
  18. public class Tag {
  19.  
  20.     @Getter
  21.     private static List<Tag> tags = new ArrayList<>();
  22.     private String name, prefix, permission, section, serverName;
  23.  
  24.     public Tag(String name) {
  25.         this.name = name;
  26.         this.section = "DEFAULT";
  27.  
  28.         tags.add(this);
  29.     }
  30.  
  31.     public static void loadTags() {
  32.         try (MongoCursor mongoCursor = AxisPlugin.getPlugin().getAxisDatabase().getTags().find().iterator()) {
  33.             while (mongoCursor.hasNext()) {
  34.                 Document document = (Document) mongoCursor.next();
  35.                 if (document.containsKey("serverName") && document.getString("serverName").equals(Bukkit.getServerName().toUpperCase().replace(" ", ""))) {
  36.                     Tag tag = new Tag(document.getString("name"));
  37.  
  38.                     if (document.containsKey("prefix")) {
  39.                         tag.setPrefix(ColorText.translate(document.getString("prefix")));
  40.                     }
  41.                     if (document.containsKey("permission")) {
  42.                         tag.setPermission(document.getString("permission"));
  43.                     }
  44.                     if (document.containsKey("section")) {
  45.                         tag.setSection(document.getString("section"));
  46.                     }
  47.                     if (document.containsKey("serverName")) {
  48.                         tag.setServerName(document.getString("serverName"));
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.     }
  54.  
  55.     public void save() {
  56.         Document document = new Document();
  57.         document.put("name", name);
  58.         if (prefix != null) {
  59.             document.put("prefix", prefix);
  60.         }
  61.         if (permission != null) {
  62.             document.put("permission", permission);
  63.         }
  64.         document.put("section", section);
  65.         document.put("serverName", serverName.toUpperCase().replace(" ", ""));
  66.         AxisPlugin.getPlugin().getAxisDatabase().getTags().replaceOne(Filters.eq("name", name), document, new UpdateOptions().upsert(true));
  67.     }
  68.  
  69.     public void remove() {
  70.         AxisPlugin.getPlugin().getAxisDatabase().getTags().deleteOne(Filters.eq("name", name));
  71.         tags.remove(this);
  72.     }
  73.  
  74.     public static List<Tag> getTagsBySection(String section) {
  75.         return tags.stream().filter(tag -> tag.getSection().equalsIgnoreCase(section) && tag.getServerName().equals(Bukkit.getServerName().toUpperCase().replace(" ", ""))).collect(Collectors.toList());
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement