Advertisement
notjacob

Channel.java

Jan 11th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package functions;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.List;
  8.  
  9. import javax.annotation.Nullable;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.World;
  14. import org.bukkit.configuration.file.YamlConfiguration;
  15. import org.bukkit.entity.Player;
  16.  
  17.  
  18.  
  19. public class Channel {
  20.     private String name;
  21.     private char channelchar;
  22.     private boolean worldSpecific;
  23.     @Nullable
  24.     private String world;
  25.     private ChatColor prefixcolor;
  26.     public Channel(String name, char cchar, boolean worldSpecific, @Nullable String world, ChatColor color) {
  27.         this.name = name;
  28.         this.channelchar = cchar;
  29.         this.worldSpecific = worldSpecific;
  30.         this.world = world;
  31.         this.prefixcolor = color;
  32.     }
  33.     public Channel(String name, char cchar, ChatColor color) {
  34.         this.name = name;
  35.         this.channelchar = cchar;
  36.         this.worldSpecific = false;
  37.         this.world = null;
  38.         this.prefixcolor = color;
  39.     }
  40.  
  41.     public void sendMessage(String msg, Player sender) {
  42.         for (Player a : Bukkit.getOnlinePlayers()) {
  43.             if (this.worldSpecific) {
  44.                 if (a.getWorld().getName().equalsIgnoreCase(world)) {
  45.                     if (a.hasPermission("core.channels." + name)) {
  46.                     a.sendMessage(prefixcolor + "[" + name + "] " + util.formatMsg(sender, msg));
  47.                 }
  48.                 }
  49.                
  50.             } else {
  51.                 a.sendMessage(prefixcolor + "[" + name + "] " + util.formatMsg(sender, msg));
  52.             }
  53.         }
  54.        
  55.     }
  56.     public void saveFile() throws IOException {
  57.         File f = new File("plugins/channels/" + name + ".yml");
  58.         YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
  59.         if (!f.exists()) {
  60.             f.createNewFile();
  61.         }
  62.         yml.set("name", name);
  63.         yml.set("char", channelchar);
  64.         yml.set("specific", this.worldSpecific);
  65.         yml.set("world", world);
  66.         yml.set("color", prefixcolor.getChar());
  67.         yml.save(f);
  68.     }
  69.     public boolean isSaved() {
  70.         File f = new File("plugins/channels/" + name + ".yml");
  71.         return f.exists() ? true : false;
  72.     }
  73.     public void loadSaved() {
  74.         File f = new File("plugins/channels/" + name + ".yml");
  75.         YamlConfiguration yml = YamlConfiguration.loadConfiguration(f);
  76.         if (!f.exists()) {
  77.             util.log("Error: Channel not saved as file", LogType.WARNING, new Date());
  78.             return;
  79.         }
  80.         name = yml.getString("name");
  81.         this.channelchar = (char) yml.get("char");
  82.         this.worldSpecific = yml.getBoolean("specific");
  83.         world = yml.getString("world");
  84.         this.prefixcolor = ChatColor.getByChar((char) yml.get("color"));
  85.     }
  86.     public boolean canReceive(Player p) {
  87.         if (this.worldSpecific) {
  88.             if (p.hasPermission("core.channels." + name) && p.getWorld().getName().equalsIgnoreCase(world)) {
  89.                 return true;
  90.             } else {
  91.                 if (p.hasPermission("core.channels" + name)) {
  92.                     return true;
  93.                 }
  94.             }
  95.         }
  96.         return false;
  97.     }
  98.    
  99.     public String getName() {
  100.         return name;
  101.     }
  102.     public char getChar() {
  103.         return this.channelchar;
  104.     }
  105.     public boolean getSpecific() {
  106.         return this.worldSpecific;
  107.     }
  108.     public String getWorldName() {
  109.         return this.world;
  110.     }
  111.     public ChatColor getPrefixColor() {
  112.         return this.prefixcolor;
  113.     }
  114.     public World getWorld() {
  115.         return Bukkit.getWorld(this.getWorldName());
  116.     }
  117.     public List<Player> getRecipients() {
  118.         List<Player> var = new ArrayList<>();
  119.         for (Player a : Bukkit.getOnlinePlayers()) {
  120.                 if (this.worldSpecific) {
  121.                     if (a.hasPermission("core.channels." + name) && a.getWorld().getName().equalsIgnoreCase(this.getWorldName())) {
  122.                     var.add(a);
  123.                 }
  124.             } else {
  125.                 if (a.hasPermission("core.channels." + name)) {
  126.                     var.add(a);
  127.                 }
  128.             }
  129.         }
  130.         return var;
  131.     }
  132.    
  133.     public void setName(String name) {
  134.         this.name = name;
  135.     }
  136.     public void setChar(char car) {
  137.         this.channelchar = car;
  138.     }
  139.     public void setSpecific(boolean specific, World world) {
  140.         this.worldSpecific = specific;
  141.         this.world = world.getName();
  142.     }
  143.     public void setWorld(World world) {
  144.         this.world = world.getName();
  145.     }
  146.     public void setColor(ChatColor color) {
  147.         this.prefixcolor = color;
  148.     }
  149.  
  150.    
  151.    
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement