Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * File is distributed under the "do whatever you want with that file as long as the author stays written somewhere" license.
- * @author Anrza
- * @author Weby
- */
- package net.raidstone.functionality;
- import com.sk89q.worldedit.bukkit.WorldEditPlugin;
- import com.sk89q.worldedit.bukkit.selections.Selection;
- import com.sk89q.worldedit.regions.selector.RegionSelectorType;
- import net.raidstone.RaidStone;
- import net.raidstone.includes.Sql;
- import net.raidstone.messages.Colors;
- import org.bukkit.*;
- import org.bukkit.block.Block;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.Action;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.block.BlockDamageEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.meta.ItemMeta;
- import org.bukkit.metadata.FixedMetadataValue;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.scheduler.BukkitTask;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.*;
- public class Tags implements Listener
- {
- static Set<World> loaded = new HashSet<>();
- static HashMap<Block, String> toRemove = new HashMap<>();
- static HashMap<Block, String> toAdd = new HashMap<>();
- static HashMap<Block, String> toLoad = new HashMap<>();
- static FixedMetadataValue fmv = new FixedMetadataValue(RaidStone.plugin, true);
- static volatile boolean taskRunning = false;
- static Set<BukkitTask> tasks = new HashSet<>();
- static int instance = 0;
- Plugin we = Bukkit.getPluginManager().getPlugin("WorldEdit");
- public Tags()
- {
- runTask();
- }
- private static void cancelTasks()
- {
- taskRunning = false;
- Bukkit.getLogger().warning("STOPPING ALL TASKS!!!!");
- for (BukkitTask t : tasks)
- {
- Bukkit.getLogger().warning("Cancelling " + t.toString());
- t.cancel();
- }
- tasks.clear();
- }
- private static void tasker(boolean noLimit)
- {
- taskRunning = true;
- if (toRemove.isEmpty() && toAdd.isEmpty())
- {
- taskRunning = false;
- cancelTasks();
- }
- int limit = 2000;
- int size = toRemove.size();
- if (!noLimit && size > limit) size = limit;
- int sizeadd = toAdd.size();
- if (sizeadd > limit) sizeadd = limit;
- Sql.openConnection();
- try
- {
- PreparedStatement ps = Sql.connection.prepareStatement("DELETE FROM block_tags WHERE value=? AND world=? AND x=? AND y=? AND z=? LIMIT 1");
- for (int pos = 0; pos < size; pos++)
- {
- Iterator<Map.Entry<Block, String>> i = toRemove.entrySet().iterator();
- if (i.hasNext())
- {
- Map.Entry<Block, String> entry = i.next();
- Block bl = entry.getKey();
- String val = entry.getValue();
- Location l = bl.getLocation();
- String world = l.getWorld().getName();
- int x = l.getBlockX();
- int y = l.getBlockY();
- int z = l.getBlockZ();
- if (bl.hasMetadata(val)) bl.removeMetadata(val, RaidStone.plugin);
- ps.setString(1, val);
- ps.setString(2, world);
- ps.setInt(3, x);
- ps.setInt(4, y);
- ps.setInt(5, z);
- ps.addBatch();
- i.remove();
- }
- }
- ps.executeBatch();
- ps.close();
- //That line is to use in case the multicolumn unique key doesn't work.
- /*PreparedStatement pst = Sql.connection.prepareStatement(
- "INSERT INTO `block_tags` (world, x, y, z, value)" +
- "SELECT ?,?,?,?,? FROM `block_tags`" +
- "WHERE NOT EXISTS (" +
- "SELECT * FROM `block_tags` WHERE world=? AND x=? AND y=? AND z=? AND value=?) LIMIT 1");*/
- PreparedStatement pst = Sql.connection.prepareStatement("INSERT IGNORE INTO block_tags(`world`,`x`,`y`,`z`,`value`) VALUES(?,?,?,?,?);");
- for (int pos = 0; pos < sizeadd; pos++)
- {
- Iterator<Map.Entry<Block, String>> i = toAdd.entrySet().iterator();
- if (i.hasNext())
- {
- Map.Entry<Block, String> entry = i.next();
- Block bl = entry.getKey();
- String val = entry.getValue();
- Location l = bl.getLocation();
- String world = l.getWorld().getName();
- int x = l.getBlockX();
- int y = l.getBlockY();
- int z = l.getBlockZ();
- if (!bl.hasMetadata(val)) bl.setMetadata(val, fmv);
- pst.setString(1, world);
- pst.setInt(2, x);
- pst.setInt(3, y);
- pst.setInt(4, z);
- pst.setString(5, val);
- pst.addBatch();
- i.remove();
- }
- }
- pst.executeBatch();
- pst.close();
- if (noLimit)
- Bukkit.getLogger().warning("------ TASKER ENDED SUCCESSFULLY AND SAVED EVERYTHING BEFORE RELOAD");
- } catch (SQLException e)
- {
- e.printStackTrace();
- } finally
- {
- Sql.closeConnection();
- }
- }
- public static void runTask()
- {
- runTask(false);
- }
- public static void runTask(boolean allAtOnce)
- {
- if (allAtOnce)
- {
- taskRunning = true;
- cancelTasks();
- tasker(true);
- } else
- {
- if (!taskRunning)
- {
- instance++;
- Bukkit.getLogger().warning("STARTING BLOCK TAG RUNNABLE INSTANCE " + instance);
- BukkitTask t = new BukkitRunnable()
- {
- @Override
- public void run()
- {
- taskRunning = true;
- tasker(false);
- }
- }.runTaskTimer(RaidStone.plugin, 0L, 8L);
- tasks.add(t);
- }
- }
- }
- public static boolean addTag(Block block, String value)
- {
- if (block != null && value != null)
- {
- World w = block.getWorld();
- if (!loaded.contains(w))
- loadTags(w);
- if (!hasTag(block, value))
- {
- toAdd.put(block, value);
- return false;
- } else return true;
- }
- return false;
- }
- public static void loadTags(World world)
- {
- if (!loaded.contains(world))
- {
- Sql.openConnection();
- try
- {
- PreparedStatement pst = Sql.connection.prepareStatement("SELECT * FROM block_tags WHERE world=?");
- pst.setString(1, world.getName());
- ResultSet rs = pst.executeQuery();
- while (rs.next())
- {
- int x = rs.getInt("x");
- int y = rs.getInt("y");
- int z = rs.getInt("z");
- Block block = world.getBlockAt(x, y, z);
- toLoad.put(block, rs.getString("value"));
- }
- rs.close();
- BukkitTask task = new BukkitRunnable()
- {
- int limit = 500; //Blocks to tag per 1/4 second
- @Override
- public void run()
- {
- if (toLoad.isEmpty())
- {
- this.cancel();
- }
- int size = toLoad.size();
- if (size > limit) size = limit;
- for (int pos = 0; pos < size; pos++)
- {
- Iterator<Map.Entry<Block, String>> i = toLoad.entrySet().iterator();
- if (i.hasNext())
- {
- Map.Entry<Block, String> entry = i.next();
- Block bl = entry.getKey();
- String val = entry.getValue();
- bl.setMetadata(val, fmv);
- i.remove();
- }
- }
- }
- }.runTaskTimer(RaidStone.plugin, 0L, 5L);
- } catch (SQLException e)
- {
- e.printStackTrace();
- } finally
- {
- Sql.closeConnection();
- }
- loaded.add(world);
- }
- }
- public static boolean hasTag(Block block, String tag)
- {
- if (block != null)
- {
- if (block.hasMetadata(tag))
- {
- return true;
- }
- }
- return false;
- }
- public static boolean removeTag(Block block, String tag)
- {
- if (block != null)
- {
- if (hasTag(block, tag))
- {
- toRemove.put(block, tag);
- return true;
- }
- }
- return false;
- }
- public static boolean taskIsDone()
- {
- return (!taskRunning) && tasks.isEmpty();
- }
- public Set<String> checkBlock(Block block)
- {
- return null;
- //Check if block has any tag and return the set
- }
- @EventHandler
- public void onBlockBreak(BlockBreakEvent e)
- {
- checkBlock(e.getBlock());
- }
- public void onDamage(BlockDamageEvent e)
- {
- Block b = e.getBlock();
- }
- public void tagSelection(Player player, boolean add, String tag)
- {
- if (taskRunning)
- {
- player.sendMessage(Colors.tagger_color + "" + ChatColor.RED + "You have to wait a few seconds for the old tags to be saved !");
- return;
- }
- if (we != null)
- {
- WorldEditPlugin wep = (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit");
- Selection sel = wep.getSelection(player);
- if (sel == null)
- {
- player.sendMessage(Colors.tagger_color + "There is no selection ! Tag block-per-block or create a new cuboid selection.");
- return;
- } else if (sel.getArea() < 1)
- {
- player.sendMessage(Colors.tagger_color + "There is no selection ! Tag block-per-block or create a new cuboid selection.");
- return;
- }
- if (!sel.getRegionSelector().getTypeName().equalsIgnoreCase(RegionSelectorType.CUBOID.name()))
- {
- player.sendMessage(Colors.tagger_color + "You can only tag with a cuboid selection !");
- return;
- }
- if (sel.getWorld() != player.getWorld())
- {
- 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.");
- return;
- }
- if (sel.getArea() > 200000)
- {
- player.sendMessage(Colors.tagger_color + "Area is too big (" + sel.getArea() + " blocks/" + 200000 + " blocks). Reduce its size.");
- return;
- }
- Location min = sel.getMinimumPoint();
- Location max = sel.getMaximumPoint();
- int minX = min.getBlockX();
- int minY = min.getBlockY();
- int minZ = min.getBlockZ();
- int maxX = max.getBlockX();
- int maxY = max.getBlockY();
- int maxZ = max.getBlockZ();
- int count = 0;
- for (int posX = minX; posX <= maxX; posX++)
- {
- for (int posY = minY; posY <= maxY; posY++)
- {
- for (int posZ = minZ; posZ <= maxZ; posZ++)
- {
- count++;
- Block b = player.getWorld().getBlockAt(posX, posY, posZ);
- if (add)
- {
- addTag(b, tag);
- } else
- {
- removeTag(b, tag);
- }
- }
- }
- }
- String un = add ? "" : "un";
- player.sendMessage(Colors.tagger_color + "Successfully " + un + "tagged " + count + " blocks !");
- }
- }
- @EventHandler (priority = EventPriority.LOWEST)
- public void onBlockTaggerUsage(PlayerInteractEvent event)
- {
- Player p = event.getPlayer();
- if (p.getGameMode() == GameMode.CREATIVE && p.hasPermission("raidstone.tagger"))
- {
- ItemStack it = p.getItemInHand();
- if (it != null)
- {
- if (it.getType() == Material.FEATHER && it.hasItemMeta())
- {
- ItemMeta im = it.getItemMeta();
- String dispname = im.getDisplayName();
- if (dispname.startsWith("Tag: "))
- {
- String tagValue = dispname.substring(5);
- if (!tagValue.equals(""))
- {
- if (event.getAction() == Action.RIGHT_CLICK_BLOCK)
- {
- Block b = event.getClickedBlock();
- if (b != null)
- {
- event.setCancelled(true);
- boolean existing = addTag(b, tagValue);
- if (existing)
- p.sendMessage(Colors.tagger_color + "This block already is tagged '" + tagValue + "' !");
- else
- p.sendMessage(Colors.tagger_color + "Tagged '" + tagValue + "' !");
- }
- runTask();
- } else if (event.getAction() == Action.LEFT_CLICK_BLOCK)
- {
- Block b = event.getClickedBlock();
- if (b != null)
- {
- event.setCancelled(true);
- boolean existing = removeTag(b, tagValue);
- if (existing)
- p.sendMessage(Colors.tagger_color + "Tag '" + tagValue + "' removed !");
- else
- p.sendMessage(Colors.tagger_color + "This block wasn't tagged '" + tagValue + "'.");
- }
- runTask();
- } else if (event.getAction() == Action.RIGHT_CLICK_AIR)
- {
- tagSelection(p, true, tagValue);
- runTask();
- } else if (event.getAction() == Action.LEFT_CLICK_AIR)
- {
- tagSelection(p, false, tagValue);
- runTask();
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement