SHARE
TWEET

Untitled

a guest Feb 12th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.nedcraft.dpasi314.RegionModule;
  2.  
  3. import java.util.Iterator;
  4. import java.util.Map;
  5.  
  6. import com.nedcraft.dpasi314.RegionModule.Database.Database;
  7. import com.sk89q.minecraft.util.commands.CommandException;
  8. import com.sk89q.worldedit.BlockVector;
  9. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  10. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
  11. import com.sk89q.worldguard.domains.DefaultDomain;
  12. import com.sk89q.worldguard.protection.managers.RegionManager;
  13.  
  14. import org.bukkit.ChatColor;
  15. import org.bukkit.Location;
  16. import org.bukkit.World;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.Listener;
  20. import org.bukkit.event.player.PlayerJoinEvent;
  21. import org.bukkit.plugin.Plugin;
  22.  
  23. public class Protect implements Listener{
  24.    
  25.     RegionModule plugin;
  26.     WorldGuardPlugin wg;
  27.     WorldEditPlugin we;
  28.     Database db;
  29.    
  30.     public Protect(RegionModule instance){
  31.         plugin = instance;
  32.         db = new Database(instance);
  33.         db.loadDB("protections.db");
  34.         hookWorldEdit();
  35.        
  36.         instance.getServer().getScheduler().scheduleAsyncRepeatingTask(instance, new Runnable() {
  37.  
  38.                public void run() {
  39.                    
  40.                    for (Player p: plugin.getServer().getOnlinePlayers()){
  41.                         if (p.hasPermission("regionmoduler.moderator")){
  42.                             alertMod(p);
  43.                             db.saveDB("protections.db");
  44.                         }
  45.                     }
  46.                }
  47.             }, 100L, 10000L);
  48.     }
  49.    
  50.     public void saveProts(){
  51.         db.saveDB("protections.db");
  52.     }
  53.    
  54.     public void alertMod(Player p){
  55.         p.sendMessage(ChatColor.AQUA+"There are "+ChatColor.RED+db.getLength()+ChatColor.AQUA+" awaiting protections.");
  56.     }
  57.    
  58.     @SuppressWarnings("rawtypes")
  59.     public void listProts(Player p){
  60.         p.sendMessage(ChatColor.AQUA + "Temporary Awating Protections ("+ChatColor.RED+db.getLength()+ChatColor.AQUA + "):");
  61.         Iterator it = db.getDatabase().entrySet().iterator();
  62.         while (it.hasNext()) {
  63.             Map.Entry pairs = (Map.Entry)it.next();
  64.             String s = "";
  65.             if (it.hasNext()){
  66.              s = "";
  67.             p.sendMessage(ChatColor.RED + "Protection: "+ChatColor.AQUA+pairs.getKey().toString()+s);
  68.         }
  69.     }
  70. }
  71.        
  72.     public void teleportProt(Player p, String prot){
  73.         RegionManager rgm = wg.getRegionManager(p.getWorld());
  74.         BlockVector vec = rgm.getRegion(prot).getMaximumPoint();
  75.         Location loc = new Location(p.getWorld(), vec.getX(), p.getWorld().getHighestBlockYAt(new Location(p.getWorld(), vec.getX(), vec.getY(), vec.getZ()))+3, vec.getZ());
  76.         p.teleport(loc);
  77.     }
  78.    
  79.     public Location blockVectorToLocation(World w, BlockVector vec){
  80.         double x = vec.getX();
  81.         double y = vec.getY();
  82.         double z = vec.getZ();
  83.         return new Location(w, x, y, z);
  84.     }
  85.    
  86.     public void addProtection(String name, String plyname){
  87.         db.set(name, null);
  88.         for (Player p: plugin.getServer().getOnlinePlayers()){
  89.             if (p.hasPermission("regionmodule.moderator")){
  90.                 p.sendMessage(ChatColor.AQUA +plyname+ ChatColor.GOLD +": Has just made the protection "+name);
  91.             }
  92.         }
  93.     }
  94.    
  95.     public void removeProtection(String name){
  96.         db.deleteKey(name);
  97.         //db.saveDB("protections.db");
  98.     }
  99.    
  100.     @SuppressWarnings("static-access")
  101.     public void removeProtectionWhole(String name, World w){
  102.         try{
  103.             removeProtection(name);
  104.             DefaultDomain owners = wg.getRegionManager(w).getRegion(name).getOwners();
  105.             System.out.println(owners.getPlayers());
  106.             Player ply = (Player) plugin.getServer().getPlayer((String) owners.getPlayers().toArray()[0]);
  107.             ply.sendMessage(ChatColor.AQUA + "Your protection: "+ChatColor.GOLD+name+ChatColor.AQUA+" has been denied");
  108.             BlockVector pos1 = wg.getRegionManager(w).getRegion(name).getMaximumPoint();
  109.             BlockVector pos2 = wg.getRegionManager(w).getRegion(name).getMinimumPoint();
  110.             double cost = ((Math.abs(((int) pos1.getX() - (int) pos2.getX())) * Math.abs(((int) pos1.getZ() - (int) pos2.getZ()))) * .5);
  111.             plugin.vault.economy.depositPlayer(ply.getName(), cost);
  112.             ply.sendMessage(ChatColor.AQUA + "You have been re-embursed: "+ChatColor.GREEN + cost + ChatColor.AQUA + " for your denied protection.");
  113.             wg.getRegionManager(w).removeRegion(name);
  114.         }
  115.         catch (Exception ex){
  116.             DefaultDomain owners = wg.getRegionManager(w).getRegion(name).getOwners();
  117.             Player ply = (Player) plugin.getServer().getPlayer((String) owners.getPlayers().toArray()[0]);
  118.             ply.sendMessage(ChatColor.RED+"An error has occured, please remove this protection manually.");
  119.         }
  120.        
  121.     }
  122.     public void hookWorldEdit() {
  123.         Plugin p = plugin.getServer().getPluginManager().getPlugin("WorldGuard");
  124.      
  125.         // WorldGuard may not be loaded
  126.         if (p == null || !(p instanceof WorldGuardPlugin)) {
  127.             return;
  128.         }
  129.         wg = (WorldGuardPlugin) p;
  130.         try {
  131.             we = wg.getWorldEdit();
  132.         } catch (CommandException e) {
  133.             e.printStackTrace();
  134.         }
  135.     }
  136.    
  137.     @EventHandler
  138.     public void onPlayerJoin(PlayerJoinEvent event){
  139.         for(Player p: plugin.getServer().getOnlinePlayers()){
  140.         if (event.getPlayer().hasPermission("regionmodule.moderator"));
  141.             alertMod(p);
  142.         }
  143.     }
  144. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top