Advertisement
Guest User

Spigot 1.8 Block tagger.

a guest
Dec 22nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.89 KB | None | 0 0
  1. /**
  2.  * File is distributed under the "do whatever you want with that file as long as the author stays written somewhere" license.
  3.  * @author Anrza
  4.  * @author Weby
  5.  */
  6.  
  7. package net.raidstone.functionality;
  8.  
  9. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
  10. import com.sk89q.worldedit.bukkit.selections.Selection;
  11. import com.sk89q.worldedit.regions.selector.RegionSelectorType;
  12. import net.raidstone.RaidStone;
  13. import net.raidstone.includes.Sql;
  14. import net.raidstone.messages.Colors;
  15. import org.bukkit.*;
  16. import org.bukkit.block.Block;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.event.EventHandler;
  19. import org.bukkit.event.EventPriority;
  20. import org.bukkit.event.Listener;
  21. import org.bukkit.event.block.Action;
  22. import org.bukkit.event.block.BlockBreakEvent;
  23. import org.bukkit.event.block.BlockDamageEvent;
  24. import org.bukkit.event.player.PlayerInteractEvent;
  25. import org.bukkit.inventory.ItemStack;
  26. import org.bukkit.inventory.meta.ItemMeta;
  27. import org.bukkit.metadata.FixedMetadataValue;
  28. import org.bukkit.plugin.Plugin;
  29. import org.bukkit.scheduler.BukkitRunnable;
  30. import org.bukkit.scheduler.BukkitTask;
  31.  
  32. import java.sql.PreparedStatement;
  33. import java.sql.ResultSet;
  34. import java.sql.SQLException;
  35. import java.util.*;
  36.  
  37. public class Tags implements Listener
  38. {
  39.     static Set<World> loaded = new HashSet<>();
  40.     static HashMap<Block, String> toRemove = new HashMap<>();
  41.     static HashMap<Block, String> toAdd = new HashMap<>();
  42.     static HashMap<Block, String> toLoad = new HashMap<>();
  43.     static FixedMetadataValue fmv = new FixedMetadataValue(RaidStone.plugin, true);
  44.     static volatile boolean taskRunning = false;
  45.     static Set<BukkitTask> tasks = new HashSet<>();
  46.     static int instance = 0;
  47.     Plugin we = Bukkit.getPluginManager().getPlugin("WorldEdit");
  48.  
  49.     public Tags()
  50.     {
  51.         runTask();
  52.     }
  53.  
  54.     private static void cancelTasks()
  55.     {
  56.         taskRunning = false;
  57.         Bukkit.getLogger().warning("STOPPING ALL TASKS!!!!");
  58.         for (BukkitTask t : tasks)
  59.         {
  60.             Bukkit.getLogger().warning("Cancelling " + t.toString());
  61.             t.cancel();
  62.         }
  63.         tasks.clear();
  64.     }
  65.  
  66.     private static void tasker(boolean noLimit)
  67.     {
  68.         taskRunning = true;
  69.         if (toRemove.isEmpty() && toAdd.isEmpty())
  70.         {
  71.             taskRunning = false;
  72.             cancelTasks();
  73.         }
  74.  
  75.         int limit = 2000;
  76.         int size = toRemove.size();
  77.         if (!noLimit && size > limit) size = limit;
  78.  
  79.         int sizeadd = toAdd.size();
  80.         if (sizeadd > limit) sizeadd = limit;
  81.  
  82.         Sql.openConnection();
  83.         try
  84.         {
  85.             PreparedStatement ps = Sql.connection.prepareStatement("DELETE FROM block_tags WHERE value=? AND world=? AND x=? AND y=? AND z=? LIMIT 1");
  86.             for (int pos = 0; pos < size; pos++)
  87.             {
  88.                 Iterator<Map.Entry<Block, String>> i = toRemove.entrySet().iterator();
  89.                 if (i.hasNext())
  90.                 {
  91.                     Map.Entry<Block, String> entry = i.next();
  92.  
  93.                     Block bl = entry.getKey();
  94.                     String val = entry.getValue();
  95.  
  96.                     Location l = bl.getLocation();
  97.                     String world = l.getWorld().getName();
  98.                     int x = l.getBlockX();
  99.                     int y = l.getBlockY();
  100.                     int z = l.getBlockZ();
  101.                     if (bl.hasMetadata(val)) bl.removeMetadata(val, RaidStone.plugin);
  102.                     ps.setString(1, val);
  103.                     ps.setString(2, world);
  104.                     ps.setInt(3, x);
  105.                     ps.setInt(4, y);
  106.                     ps.setInt(5, z);
  107.                     ps.addBatch();
  108.                     i.remove();
  109.                 }
  110.             }
  111.             ps.executeBatch();
  112.             ps.close();
  113.  
  114.             //That line is to use in case the multicolumn unique key doesn't work.
  115.             /*PreparedStatement pst = Sql.connection.prepareStatement(
  116.             "INSERT INTO `block_tags` (world, x, y, z, value)" +
  117.             "SELECT ?,?,?,?,? FROM `block_tags`" +
  118.             "WHERE NOT EXISTS (" +
  119.             "SELECT * FROM `block_tags` WHERE world=? AND x=? AND y=? AND z=? AND value=?) LIMIT 1");*/
  120.  
  121.             PreparedStatement pst = Sql.connection.prepareStatement("INSERT IGNORE INTO block_tags(`world`,`x`,`y`,`z`,`value`) VALUES(?,?,?,?,?);");
  122.  
  123.             for (int pos = 0; pos < sizeadd; pos++)
  124.             {
  125.                 Iterator<Map.Entry<Block, String>> i = toAdd.entrySet().iterator();
  126.                 if (i.hasNext())
  127.                 {
  128.                     Map.Entry<Block, String> entry = i.next();
  129.  
  130.                     Block bl = entry.getKey();
  131.                     String val = entry.getValue();
  132.  
  133.                     Location l = bl.getLocation();
  134.                     String world = l.getWorld().getName();
  135.                     int x = l.getBlockX();
  136.                     int y = l.getBlockY();
  137.                     int z = l.getBlockZ();
  138.                     if (!bl.hasMetadata(val)) bl.setMetadata(val, fmv);
  139.  
  140.                     pst.setString(1, world);
  141.                     pst.setInt(2, x);
  142.                     pst.setInt(3, y);
  143.                     pst.setInt(4, z);
  144.                     pst.setString(5, val);
  145.  
  146.                     pst.addBatch();
  147.                     i.remove();
  148.                 }
  149.             }
  150.             pst.executeBatch();
  151.             pst.close();
  152.             if (noLimit)
  153.                 Bukkit.getLogger().warning("------ TASKER ENDED SUCCESSFULLY AND SAVED EVERYTHING BEFORE RELOAD");
  154.         } catch (SQLException e)
  155.         {
  156.             e.printStackTrace();
  157.         } finally
  158.         {
  159.             Sql.closeConnection();
  160.         }
  161.  
  162.     }
  163.  
  164.     public static void runTask()
  165.     {
  166.         runTask(false);
  167.     }
  168.  
  169.     public static void runTask(boolean allAtOnce)
  170.     {
  171.         if (allAtOnce)
  172.         {
  173.             taskRunning = true;
  174.             cancelTasks();
  175.             tasker(true);
  176.         } else
  177.         {
  178.             if (!taskRunning)
  179.             {
  180.                 instance++;
  181.                 Bukkit.getLogger().warning("STARTING BLOCK TAG RUNNABLE INSTANCE " + instance);
  182.                 BukkitTask t = new BukkitRunnable()
  183.                 {
  184.                     @Override
  185.                     public void run()
  186.                     {
  187.                         taskRunning = true;
  188.                         tasker(false);
  189.                     }
  190.                 }.runTaskTimer(RaidStone.plugin, 0L, 8L);
  191.                 tasks.add(t);
  192.             }
  193.         }
  194.     }
  195.  
  196.     public static boolean addTag(Block block, String value)
  197.     {
  198.         if (block != null && value != null)
  199.         {
  200.             World w = block.getWorld();
  201.             if (!loaded.contains(w))
  202.                 loadTags(w);
  203.  
  204.             if (!hasTag(block, value))
  205.             {
  206.                 toAdd.put(block, value);
  207.                 return false;
  208.             } else return true;
  209.         }
  210.         return false;
  211.     }
  212.  
  213.     public static void loadTags(World world)
  214.     {
  215.         if (!loaded.contains(world))
  216.         {
  217.             Sql.openConnection();
  218.             try
  219.             {
  220.                 PreparedStatement pst = Sql.connection.prepareStatement("SELECT * FROM block_tags WHERE world=?");
  221.                 pst.setString(1, world.getName());
  222.                 ResultSet rs = pst.executeQuery();
  223.  
  224.                 while (rs.next())
  225.                 {
  226.                     int x = rs.getInt("x");
  227.                     int y = rs.getInt("y");
  228.                     int z = rs.getInt("z");
  229.                     Block block = world.getBlockAt(x, y, z);
  230.                     toLoad.put(block, rs.getString("value"));
  231.                 }
  232.                 rs.close();
  233.                 BukkitTask task = new BukkitRunnable()
  234.                 {
  235.                     int limit = 500; //Blocks to tag per 1/4 second
  236.  
  237.                     @Override
  238.                     public void run()
  239.                     {
  240.                         if (toLoad.isEmpty())
  241.                         {
  242.                             this.cancel();
  243.                         }
  244.  
  245.                         int size = toLoad.size();
  246.                         if (size > limit) size = limit;
  247.                         for (int pos = 0; pos < size; pos++)
  248.                         {
  249.                             Iterator<Map.Entry<Block, String>> i = toLoad.entrySet().iterator();
  250.                             if (i.hasNext())
  251.                             {
  252.                                 Map.Entry<Block, String> entry = i.next();
  253.                                 Block bl = entry.getKey();
  254.                                 String val = entry.getValue();
  255.                                 bl.setMetadata(val, fmv);
  256.                                 i.remove();
  257.                             }
  258.                         }
  259.                     }
  260.                 }.runTaskTimer(RaidStone.plugin, 0L, 5L);
  261.             } catch (SQLException e)
  262.             {
  263.                 e.printStackTrace();
  264.             } finally
  265.             {
  266.                 Sql.closeConnection();
  267.             }
  268.             loaded.add(world);
  269.         }
  270.     }
  271.  
  272.     public static boolean hasTag(Block block, String tag)
  273.     {
  274.         if (block != null)
  275.         {
  276.             if (block.hasMetadata(tag))
  277.             {
  278.                 return true;
  279.             }
  280.         }
  281.         return false;
  282.     }
  283.  
  284.     public static boolean removeTag(Block block, String tag)
  285.     {
  286.         if (block != null)
  287.         {
  288.             if (hasTag(block, tag))
  289.             {
  290.                 toRemove.put(block, tag);
  291.                 return true;
  292.             }
  293.         }
  294.         return false;
  295.     }
  296.  
  297.     public static boolean taskIsDone()
  298.     {
  299.         return (!taskRunning) && tasks.isEmpty();
  300.     }
  301.  
  302.     public Set<String> checkBlock(Block block)
  303.     {
  304.         return null;
  305.         //Check if block has any tag and return the set
  306.     }
  307.  
  308.     @EventHandler
  309.     public void onBlockBreak(BlockBreakEvent e)
  310.     {
  311.         checkBlock(e.getBlock());
  312.     }
  313.  
  314.     public void onDamage(BlockDamageEvent e)
  315.     {
  316.         Block b = e.getBlock();
  317.  
  318.     }
  319.  
  320.     public void tagSelection(Player player, boolean add, String tag)
  321.     {
  322.         if (taskRunning)
  323.         {
  324.             player.sendMessage(Colors.tagger_color + "" + ChatColor.RED + "You have to wait a few seconds for the old tags to be saved !");
  325.             return;
  326.         }
  327.         if (we != null)
  328.         {
  329.             WorldEditPlugin wep = (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit");
  330.             Selection sel = wep.getSelection(player);
  331.             if (sel == null)
  332.             {
  333.                 player.sendMessage(Colors.tagger_color + "There is no selection ! Tag block-per-block or create a new cuboid selection.");
  334.                 return;
  335.             } else if (sel.getArea() < 1)
  336.             {
  337.                 player.sendMessage(Colors.tagger_color + "There is no selection ! Tag block-per-block or create a new cuboid selection.");
  338.                 return;
  339.             }
  340.             if (!sel.getRegionSelector().getTypeName().equalsIgnoreCase(RegionSelectorType.CUBOID.name()))
  341.             {
  342.                 player.sendMessage(Colors.tagger_color + "You can only tag with a cuboid selection !");
  343.                 return;
  344.             }
  345.             if (sel.getWorld() != player.getWorld())
  346.             {
  347.                 player.sendMessage(Colors.tagger_color + "Can't tag blocks that aren't in your world. Re-select blocks in this world or teleport to the selection.");
  348.                 return;
  349.             }
  350.             if (sel.getArea() > 200000)
  351.             {
  352.                 player.sendMessage(Colors.tagger_color + "Area is too big (" + sel.getArea() + " blocks/" + 200000 + " blocks). Reduce its size.");
  353.                 return;
  354.             }
  355.  
  356.             Location min = sel.getMinimumPoint();
  357.             Location max = sel.getMaximumPoint();
  358.  
  359.             int minX = min.getBlockX();
  360.             int minY = min.getBlockY();
  361.             int minZ = min.getBlockZ();
  362.  
  363.             int maxX = max.getBlockX();
  364.             int maxY = max.getBlockY();
  365.             int maxZ = max.getBlockZ();
  366.  
  367.             int count = 0;
  368.             for (int posX = minX; posX <= maxX; posX++)
  369.             {
  370.                 for (int posY = minY; posY <= maxY; posY++)
  371.                 {
  372.                     for (int posZ = minZ; posZ <= maxZ; posZ++)
  373.                     {
  374.                         count++;
  375.                         Block b = player.getWorld().getBlockAt(posX, posY, posZ);
  376.                         if (add)
  377.                         {
  378.                             addTag(b, tag);
  379.                         } else
  380.                         {
  381.                             removeTag(b, tag);
  382.                         }
  383.                     }
  384.                 }
  385.             }
  386.             String un = add ? "" : "un";
  387.             player.sendMessage(Colors.tagger_color + "Successfully " + un + "tagged " + count + " blocks !");
  388.         }
  389.     }
  390.  
  391.     @EventHandler (priority = EventPriority.LOWEST)
  392.     public void onBlockTaggerUsage(PlayerInteractEvent event)
  393.     {
  394.         Player p = event.getPlayer();
  395.         if (p.getGameMode() == GameMode.CREATIVE && p.hasPermission("raidstone.tagger"))
  396.         {
  397.             ItemStack it = p.getItemInHand();
  398.             if (it != null)
  399.             {
  400.                 if (it.getType() == Material.FEATHER && it.hasItemMeta())
  401.                 {
  402.                     ItemMeta im = it.getItemMeta();
  403.                     String dispname = im.getDisplayName();
  404.                     if (dispname.startsWith("Tag: "))
  405.                     {
  406.                         String tagValue = dispname.substring(5);
  407.                         if (!tagValue.equals(""))
  408.                         {
  409.                             if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
  410.                             {
  411.                                 Block b = event.getClickedBlock();
  412.                                 if (b != null)
  413.                                 {
  414.                                     event.setCancelled(true);
  415.                                     boolean existing = addTag(b, tagValue);
  416.                                     if (existing)
  417.                                         p.sendMessage(Colors.tagger_color + "This block already is tagged '" + tagValue + "' !");
  418.                                     else
  419.                                         p.sendMessage(Colors.tagger_color + "Tagged '" + tagValue + "' !");
  420.                                 }
  421.                                 runTask();
  422.                             } else if (event.getAction() == Action.LEFT_CLICK_BLOCK)
  423.                             {
  424.                                 Block b = event.getClickedBlock();
  425.                                 if (b != null)
  426.                                 {
  427.                                     event.setCancelled(true);
  428.                                     boolean existing = removeTag(b, tagValue);
  429.                                     if (existing)
  430.                                         p.sendMessage(Colors.tagger_color + "Tag '" + tagValue + "' removed !");
  431.                                     else
  432.                                         p.sendMessage(Colors.tagger_color + "This block wasn't tagged '" + tagValue + "'.");
  433.                                 }
  434.                                 runTask();
  435.                             } else if (event.getAction() == Action.RIGHT_CLICK_AIR)
  436.                             {
  437.                                 tagSelection(p, true, tagValue);
  438.                                 runTask();
  439.                             } else if (event.getAction() == Action.LEFT_CLICK_AIR)
  440.                             {
  441.                                 tagSelection(p, false, tagValue);
  442.                                 runTask();
  443.                             }
  444.                         }
  445.                     }
  446.                 }
  447.             }
  448.         }
  449.     }
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement