|
| 1 | +package net.coreprotect.database; |
| 2 | + |
| 3 | +import java.sql.ResultSet; |
| 4 | +import java.sql.Statement; |
| 5 | + |
| 6 | +import org.bukkit.Material; |
| 7 | +import org.bukkit.block.Block; |
| 8 | +import org.bukkit.block.BlockState; |
| 9 | + |
| 10 | +import net.coreprotect.config.ConfigHandler; |
| 11 | +import net.coreprotect.database.statement.UserStatement; |
| 12 | +import net.coreprotect.thread.CacheHandler; |
| 13 | +import net.coreprotect.utility.MaterialUtils; |
| 14 | +import net.coreprotect.utility.WorldUtils; |
| 15 | + |
| 16 | +public class BlockLookup { |
| 17 | + |
| 18 | + public static String whoPlaced(Statement statement, BlockState block) { |
| 19 | + String result = ""; |
| 20 | + |
| 21 | + try { |
| 22 | + if (block == null) { |
| 23 | + return result; |
| 24 | + } |
| 25 | + |
| 26 | + int x = block.getX(); |
| 27 | + int y = block.getY(); |
| 28 | + int z = block.getZ(); |
| 29 | + int time = (int) (System.currentTimeMillis() / 1000L); |
| 30 | + int worldId = WorldUtils.getWorldId(block.getWorld().getName()); |
| 31 | + String query = "SELECT user,type FROM " + ConfigHandler.prefix + "block " + WorldUtils.getWidIndex("block") + "WHERE wid = '" + worldId + "' AND x = '" + x + "' AND z = '" + z + "' AND y = '" + y + "' AND rolled_back IN(0,2) AND action='1' ORDER BY rowid DESC LIMIT 0, 1"; |
| 32 | + |
| 33 | + ResultSet results = statement.executeQuery(query); |
| 34 | + while (results.next()) { |
| 35 | + int resultUserId = results.getInt("user"); |
| 36 | + int resultType = results.getInt("type"); |
| 37 | + |
| 38 | + if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) { |
| 39 | + UserStatement.loadName(statement.getConnection(), resultUserId); |
| 40 | + } |
| 41 | + |
| 42 | + result = ConfigHandler.playerIdCacheReversed.get(resultUserId); |
| 43 | + if (result.length() > 0) { |
| 44 | + Material resultMaterial = MaterialUtils.getType(resultType); |
| 45 | + CacheHandler.lookupCache.put("" + x + "." + y + "." + z + "." + worldId + "", new Object[] { time, result, resultMaterial }); |
| 46 | + } |
| 47 | + } |
| 48 | + results.close(); |
| 49 | + } |
| 50 | + catch (Exception e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + |
| 54 | + return result; |
| 55 | + } |
| 56 | + |
| 57 | + public static String whoPlacedCache(Block block) { |
| 58 | + if (block == null) { |
| 59 | + return ""; |
| 60 | + } |
| 61 | + |
| 62 | + return whoPlacedCache(block.getState()); |
| 63 | + } |
| 64 | + |
| 65 | + public static String whoPlacedCache(BlockState block) { |
| 66 | + String result = ""; |
| 67 | + |
| 68 | + try { |
| 69 | + if (block == null) { |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + int x = block.getX(); |
| 74 | + int y = block.getY(); |
| 75 | + int z = block.getZ(); |
| 76 | + int worldId = WorldUtils.getWorldId(block.getWorld().getName()); |
| 77 | + |
| 78 | + String cords = "" + x + "." + y + "." + z + "." + worldId + ""; |
| 79 | + Object[] data = CacheHandler.lookupCache.get(cords); |
| 80 | + |
| 81 | + if (data != null) { |
| 82 | + result = (String) data[1]; |
| 83 | + } |
| 84 | + } |
| 85 | + catch (Exception e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + public static String whoRemovedCache(BlockState block) { |
| 93 | + /* |
| 94 | + * Performs a lookup on who removed a block, from memory. Only searches through the last 30 seconds of block removal data. |
| 95 | + */ |
| 96 | + String result = ""; |
| 97 | + |
| 98 | + try { |
| 99 | + if (block != null) { |
| 100 | + int x = block.getX(); |
| 101 | + int y = block.getY(); |
| 102 | + int z = block.getZ(); |
| 103 | + int worldId = WorldUtils.getWorldId(block.getWorld().getName()); |
| 104 | + |
| 105 | + String cords = "" + x + "." + y + "." + z + "." + worldId + ""; |
| 106 | + Object[] data = CacheHandler.breakCache.get(cords); |
| 107 | + |
| 108 | + if (data != null) { |
| 109 | + result = (String) data[1]; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + catch (Exception e) { |
| 114 | + e.printStackTrace(); |
| 115 | + } |
| 116 | + |
| 117 | + return result; |
| 118 | + } |
| 119 | +} |
0 commit comments