From 2285c5bc1cdf3108bc38719659adf504e92512bf Mon Sep 17 00:00:00 2001 From: Zrips Date: Fri, 23 Sep 2016 16:30:22 +0300 Subject: [PATCH] optimize for vergy block check --- .../jobs/config/RestrictedBlockManager.java | 275 ++++++++++-------- .../listeners/PistonProtectionListener.java | 250 ++++++++-------- 2 files changed, 263 insertions(+), 262 deletions(-) diff --git a/com/gamingmesh/jobs/config/RestrictedBlockManager.java b/com/gamingmesh/jobs/config/RestrictedBlockManager.java index 28915973..9db8ad87 100644 --- a/com/gamingmesh/jobs/config/RestrictedBlockManager.java +++ b/com/gamingmesh/jobs/config/RestrictedBlockManager.java @@ -1,127 +1,148 @@ -package com.gamingmesh.jobs.config; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; - -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; - -import com.gamingmesh.jobs.Jobs; -import com.gamingmesh.jobs.container.LocaleReader; -import com.gamingmesh.jobs.stuff.ChatColor; - -public class RestrictedBlockManager { - - public ArrayList restrictedBlocks = new ArrayList(); - public ArrayList restrictedBlocksTimer = new ArrayList(); - public ArrayList restrictedPlaceBlocksTimer = new ArrayList(); - - private Jobs plugin; - - public RestrictedBlockManager(Jobs plugin) { - this.plugin = plugin; - } - - /** - * Method to load the restricted areas configuration - * - * loads from Jobs/restrictedAreas.yml - */ - public synchronized void load() { - File f = new File(plugin.getDataFolder(), "restrictedBlocks.yml"); - YamlConfiguration config = YamlConfiguration.loadConfiguration(f); - CommentedYamlConfiguration writer = new CommentedYamlConfiguration(); - - LocaleReader c = new LocaleReader(config, writer); - - config.options().copyDefaults(true); - - c.getW().addComment("restrictedblocks", "All block to be protected from place/break exploit.", "This will prevent piston moving all blocks in list", - "Dont count in vegetables or any single click break blocks"); - restrictedBlocks.add("14"); - restrictedBlocks.add("15"); - restrictedBlocks.add("16"); - restrictedBlocks.add("21"); - restrictedBlocks.add("48"); - restrictedBlocks.add("56"); - restrictedBlocks.add("73"); - restrictedBlocks.add("74"); - restrictedBlocks.add("129"); - restrictedBlocks.add("153"); - c.getC().addDefault("restrictedblocks", restrictedBlocks); - restrictedBlocks = (ArrayList) c.getC().getStringList("restrictedblocks"); - c.copySetting("restrictedblocks"); - - - Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + restrictedBlocks.size() + " restricted blocks!"); - - c.getW().addComment("blockstimer", "Block protected by timer in sec", - "141-60 means that carrot can be harvested after 60 sec (remember to use id's from placed objects, not from your inventory)"); - restrictedBlocksTimer.add("2-60"); - restrictedBlocksTimer.add("3-60"); - restrictedBlocksTimer.add("6-60"); - restrictedBlocksTimer.add("12-60"); - restrictedBlocksTimer.add("18-60"); - restrictedBlocksTimer.add("31-60"); - restrictedBlocksTimer.add("32-60"); - restrictedBlocksTimer.add("37-60"); - restrictedBlocksTimer.add("38-60"); - restrictedBlocksTimer.add("39-60"); - restrictedBlocksTimer.add("40-60"); - restrictedBlocksTimer.add("55-60"); - restrictedBlocksTimer.add("59-60"); - restrictedBlocksTimer.add("80-60"); - restrictedBlocksTimer.add("81-60"); - restrictedBlocksTimer.add("83-60"); - restrictedBlocksTimer.add("103-60"); - restrictedBlocksTimer.add("106-60"); - restrictedBlocksTimer.add("111-60"); - restrictedBlocksTimer.add("141-60"); - restrictedBlocksTimer.add("142-60"); - restrictedBlocksTimer.add("161-60"); - restrictedBlocksTimer.add("171-60"); - restrictedBlocksTimer.add("175-60"); - c.getC().addDefault("blockstimer", restrictedBlocksTimer); - restrictedBlocksTimer = (ArrayList) c.getC().getStringList("blockstimer"); - c.copySetting("blockstimer"); - - Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + restrictedBlocksTimer.size() + " restricted blocks timers!"); - - c.getW().addComment("PlacedBlockTimer", "Block place protected by timer in sec", "For this to work CoreProtect plugin should be installed"); - restrictedPlaceBlocksTimer.add(2); - restrictedPlaceBlocksTimer.add(3); - restrictedPlaceBlocksTimer.add(6); - restrictedPlaceBlocksTimer.add(12); - restrictedPlaceBlocksTimer.add(18); - restrictedPlaceBlocksTimer.add(31); - restrictedPlaceBlocksTimer.add(32); - restrictedPlaceBlocksTimer.add(37); - restrictedPlaceBlocksTimer.add(38); - restrictedPlaceBlocksTimer.add(39); - restrictedPlaceBlocksTimer.add(40); - restrictedPlaceBlocksTimer.add(55); - restrictedPlaceBlocksTimer.add(59); - restrictedPlaceBlocksTimer.add(80); - restrictedPlaceBlocksTimer.add(81); - restrictedPlaceBlocksTimer.add(83); - restrictedPlaceBlocksTimer.add(103); - restrictedPlaceBlocksTimer.add(106); - restrictedPlaceBlocksTimer.add(111); - restrictedPlaceBlocksTimer.add(141); - restrictedPlaceBlocksTimer.add(142); - restrictedPlaceBlocksTimer.add(161); - restrictedPlaceBlocksTimer.add(171); - restrictedPlaceBlocksTimer.add(175); - c.getC().addDefault("PlacedBlockTimer", restrictedPlaceBlocksTimer); - restrictedPlaceBlocksTimer = (ArrayList) c.getC().getIntegerList("PlacedBlockTimer"); - c.copySetting("PlacedBlockTimer"); - Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + restrictedPlaceBlocksTimer.size() + " restricted place blocks timers!"); - - try { - writer.save(f); - } catch (IOException e) { - e.printStackTrace(); - } - } -} +package com.gamingmesh.jobs.config; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.YamlConfiguration; + +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.container.LocaleReader; +import com.gamingmesh.jobs.stuff.ChatColor; + +public class RestrictedBlockManager { + + public ArrayList restrictedBlocks = new ArrayList(); + public HashMap restrictedBlocksTimer = new HashMap(); + public ArrayList restrictedPlaceBlocksTimer = new ArrayList(); + + private Jobs plugin; + + public RestrictedBlockManager(Jobs plugin) { + this.plugin = plugin; + } + + /** + * Method to load the restricted areas configuration + * + * loads from Jobs/restrictedAreas.yml + */ + public synchronized void load() { + File f = new File(plugin.getDataFolder(), "restrictedBlocks.yml"); + YamlConfiguration config = YamlConfiguration.loadConfiguration(f); + CommentedYamlConfiguration writer = new CommentedYamlConfiguration(); + + LocaleReader c = new LocaleReader(config, writer); + + config.options().copyDefaults(true); + + c.getW().addComment("restrictedblocks", "All block to be protected from place/break exploit.", "This will prevent piston moving all blocks in list", + "Dont count in vegetables or any single click break blocks"); + restrictedBlocks.add("14"); + restrictedBlocks.add("15"); + restrictedBlocks.add("16"); + restrictedBlocks.add("21"); + restrictedBlocks.add("48"); + restrictedBlocks.add("56"); + restrictedBlocks.add("73"); + restrictedBlocks.add("74"); + restrictedBlocks.add("129"); + restrictedBlocks.add("153"); + c.getC().addDefault("restrictedblocks", restrictedBlocks); + restrictedBlocks = (ArrayList) c.getC().getStringList("restrictedblocks"); + c.copySetting("restrictedblocks"); + + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + restrictedBlocks.size() + " restricted blocks!"); + + c.getW().addComment("blockstimer", "Block protected by timer in sec", + "141-60 means that carrot can be harvested after 60 sec (remember to use id's from placed objects, not from your inventory)"); + ArrayList ls = new ArrayList(); + ls.addAll(Arrays.asList("2-60", + "3-60", + "6-60", + "12-60", + "18-60", + "31-60", + "32-60", + "37-60", + "38-60", + "39-60", + "40-60", + "55-60", + "59-60", + "80-60", + "81-60", + "83-60", + "103-60", + "106-60", + "111-60", + "141-60", + "142-60", + "161-60", + "171-60", + "175-60")); + c.getC().addDefault("blockstimer", ls); + ls = (ArrayList) c.getC().getStringList("blockstimer"); + + for (String one : ls) { + + if (!one.contains("-")) + continue; + + int id = 0; + int timer = 0; + + try { + id = Integer.parseInt(one.split("-")[0]); + timer = Integer.parseInt(one.split("-")[1]); + } catch (NumberFormatException e) { + continue; + } + restrictedBlocksTimer.put(id, timer); + + } + + c.copySetting("blockstimer"); + + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + restrictedBlocksTimer.size() + " restricted blocks timers!"); + + c.getW().addComment("PlacedBlockTimer", "Block place protected by timer in sec", "For this to work CoreProtect plugin should be installed"); + restrictedPlaceBlocksTimer.add(2); + restrictedPlaceBlocksTimer.add(3); + restrictedPlaceBlocksTimer.add(6); + restrictedPlaceBlocksTimer.add(12); + restrictedPlaceBlocksTimer.add(18); + restrictedPlaceBlocksTimer.add(31); + restrictedPlaceBlocksTimer.add(32); + restrictedPlaceBlocksTimer.add(37); + restrictedPlaceBlocksTimer.add(38); + restrictedPlaceBlocksTimer.add(39); + restrictedPlaceBlocksTimer.add(40); + restrictedPlaceBlocksTimer.add(55); + restrictedPlaceBlocksTimer.add(59); + restrictedPlaceBlocksTimer.add(80); + restrictedPlaceBlocksTimer.add(81); + restrictedPlaceBlocksTimer.add(83); + restrictedPlaceBlocksTimer.add(103); + restrictedPlaceBlocksTimer.add(106); + restrictedPlaceBlocksTimer.add(111); + restrictedPlaceBlocksTimer.add(141); + restrictedPlaceBlocksTimer.add(142); + restrictedPlaceBlocksTimer.add(161); + restrictedPlaceBlocksTimer.add(171); + restrictedPlaceBlocksTimer.add(175); + c.getC().addDefault("PlacedBlockTimer", restrictedPlaceBlocksTimer); + restrictedPlaceBlocksTimer = (ArrayList) c.getC().getIntegerList("PlacedBlockTimer"); + c.copySetting("PlacedBlockTimer"); + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded " + restrictedPlaceBlocksTimer.size() + " restricted place blocks timers!"); + + try { + writer.save(f); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/com/gamingmesh/jobs/listeners/PistonProtectionListener.java b/com/gamingmesh/jobs/listeners/PistonProtectionListener.java index 3d15e3f2..adb75019 100644 --- a/com/gamingmesh/jobs/listeners/PistonProtectionListener.java +++ b/com/gamingmesh/jobs/listeners/PistonProtectionListener.java @@ -1,135 +1,115 @@ -package com.gamingmesh.jobs.listeners; - -import java.util.List; - -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.BlockPistonExtendEvent; -import org.bukkit.event.block.BlockPistonRetractEvent; - -import com.gamingmesh.jobs.Jobs; - -public class PistonProtectionListener implements Listener { - - @SuppressWarnings("unused") - private Jobs plugin; - - public PistonProtectionListener(Jobs plugin) { - this.plugin = plugin; - } - - @SuppressWarnings("deprecation") - public boolean CheckBlock(Block block) { - for (String BlockId : Jobs.getRestrictedBlockManager().restrictedBlocks) { - if (BlockId.equalsIgnoreCase(String.valueOf(block.getTypeId()))) { - return true; - } - } - return false; - } - - @SuppressWarnings("deprecation") - public boolean CheckPlaceBlock(Block block) { - for (int BlockId : Jobs.getRestrictedBlockManager().restrictedPlaceBlocksTimer) { - if (BlockId == block.getTypeId()) { - return true; - } - } - return false; - } - - @SuppressWarnings("deprecation") - public boolean CheckVegy(Block block) { - for (String ConfigOneBlock : Jobs.getRestrictedBlockManager().restrictedBlocksTimer) { - int ConfigPlacedBlockId = 0; - try { - ConfigPlacedBlockId = Integer.parseInt(ConfigOneBlock.split("-")[0]); - } catch (NumberFormatException e) { - continue; - } - if (block.getTypeId() == ConfigPlacedBlockId) { - return true; - } - } - return false; - } - - @SuppressWarnings("deprecation") - public boolean checkVegybreak(Block block, Player player) { - for (String ConfigOneBlock : Jobs.getRestrictedBlockManager().restrictedBlocksTimer) { - int ConfigPlacedBlockId = 0; - int ConfigPlacedBlockTimer = 0; - try { - ConfigPlacedBlockId = Integer.parseInt(ConfigOneBlock.split("-")[0]); - ConfigPlacedBlockTimer = Integer.parseInt(ConfigOneBlock.split("-")[1]); - } catch (NumberFormatException e) { - continue; - } - if (block.getTypeId() == ConfigPlacedBlockId) { - if (CheckVegyTimer(block, ConfigPlacedBlockTimer, player)) { - return true; - } - } - } - return false; - } - - public boolean CheckVegyTimer(Block block, int time, Player player) { - long currentTime = System.currentTimeMillis(); - if (!block.hasMetadata(JobsPaymentListener.VegyMetadata)) - return false; - long BlockTime = block.getMetadata(JobsPaymentListener.VegyMetadata).get(0).asLong(); - - if (currentTime >= BlockTime + time * 1000) { - return false; - } - - int sec = Math.round((((BlockTime + time * 1000) - currentTime)) / 1000); - - Jobs.getActionBar().send(player, Jobs.getLanguage().getMessage("message.blocktimer", "[time]", sec)); - return true; - } - - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void OnBlockMove(BlockPistonExtendEvent event) { - //disabling plugin in world - if (event.getBlock() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) - return; - if (event.isCancelled()) - return; - - if (!Jobs.getGCManager().useBlockPiston) - return; - - List block = event.getBlocks(); - for (Block OneBlock : block) { - if (CheckBlock(OneBlock)) { - event.setCancelled(true); - break; - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) - public void OnBlockRetractMove(BlockPistonRetractEvent event) { - //disabling plugin in world - if (event.getBlock() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) - return; - if (event.isCancelled()) - return; - - if (!Jobs.getGCManager().useBlockPiston) - return; - - List block = Jobs.getNms().getPistonRetractBlocks(event); - for (Block OneBlock : block) { - if (CheckBlock(OneBlock)) { - event.setCancelled(true); - break; - } - } - } -} +package com.gamingmesh.jobs.listeners; + +import java.util.List; + +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.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; + +import com.gamingmesh.jobs.Jobs; + +public class PistonProtectionListener implements Listener { + + @SuppressWarnings("unused") + private Jobs plugin; + + public PistonProtectionListener(Jobs plugin) { + this.plugin = plugin; + } + + @SuppressWarnings("deprecation") + public boolean CheckBlock(Block block) { + for (String BlockId : Jobs.getRestrictedBlockManager().restrictedBlocks) { + if (BlockId.equalsIgnoreCase(String.valueOf(block.getTypeId()))) { + return true; + } + } + return false; + } + + @SuppressWarnings("deprecation") + public boolean CheckPlaceBlock(Block block) { + for (int BlockId : Jobs.getRestrictedBlockManager().restrictedPlaceBlocksTimer) { + if (BlockId == block.getTypeId()) { + return true; + } + } + return false; + } + + @SuppressWarnings("deprecation") + public boolean CheckVegy(Block block) { + if (!Jobs.getRestrictedBlockManager().restrictedBlocksTimer.containsKey(block.getTypeId())) + return false; + return true; + } + + @SuppressWarnings("deprecation") + public boolean checkVegybreak(Block block, Player player) { + if (!Jobs.getRestrictedBlockManager().restrictedBlocksTimer.containsKey(block.getTypeId())) + return false; + if (CheckVegyTimer(block, Jobs.getRestrictedBlockManager().restrictedBlocksTimer.get(block.getTypeId()), player)) + return true; + return false; + } + + public boolean CheckVegyTimer(Block block, int time, Player player) { + long currentTime = System.currentTimeMillis(); + if (!block.hasMetadata(JobsPaymentListener.VegyMetadata)) + return false; + long BlockTime = block.getMetadata(JobsPaymentListener.VegyMetadata).get(0).asLong(); + + if (currentTime >= BlockTime + time * 1000) { + return false; + } + + int sec = Math.round((((BlockTime + time * 1000) - currentTime)) / 1000); + + Jobs.getActionBar().send(player, Jobs.getLanguage().getMessage("message.blocktimer", "[time]", sec)); + return true; + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void OnBlockMove(BlockPistonExtendEvent event) { + //disabling plugin in world + if (event.getBlock() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) + return; + if (event.isCancelled()) + return; + + if (!Jobs.getGCManager().useBlockPiston) + return; + + List block = event.getBlocks(); + for (Block OneBlock : block) { + if (CheckBlock(OneBlock)) { + event.setCancelled(true); + break; + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void OnBlockRetractMove(BlockPistonRetractEvent event) { + //disabling plugin in world + if (event.getBlock() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) + return; + if (event.isCancelled()) + return; + + if (!Jobs.getGCManager().useBlockPiston) + return; + + List block = Jobs.getNms().getPistonRetractBlocks(event); + for (Block OneBlock : block) { + if (CheckBlock(OneBlock)) { + event.setCancelled(true); + break; + } + } + } +}