From f5e7379fc917a43e8115403751d3ff72e4afc17a Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Tue, 10 Jul 2012 22:14:23 +0100 Subject: [PATCH] Implemented a way to filter by data type in the block param. Addresses #93 Wrapped up blocks from block param into a neat little class for easy usage --- src/de/diddiz/LogBlock/CommandsHandler.java | 9 +- src/de/diddiz/LogBlock/QueryParams.java | 102 +++++++++++++++----- src/de/diddiz/util/Block.java | 36 +++++++ 3 files changed, 119 insertions(+), 28 deletions(-) create mode 100644 src/de/diddiz/util/Block.java diff --git a/src/de/diddiz/LogBlock/CommandsHandler.java b/src/de/diddiz/LogBlock/CommandsHandler.java index 695dfc0..79f2935 100644 --- a/src/de/diddiz/LogBlock/CommandsHandler.java +++ b/src/de/diddiz/LogBlock/CommandsHandler.java @@ -48,6 +48,7 @@ import de.diddiz.LogBlock.QueryParams.SummarizationMode; import de.diddiz.LogBlock.config.Config; import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlockQuestioner.LogBlockQuestioner; +import de.diddiz.util.Block; public class CommandsHandler implements CommandExecutor { @@ -408,9 +409,9 @@ public class CommandsHandler implements CommandExecutor params.needType = true; params.needData = true; params.needPlayer = true; - if (params.types.size() == 0 || params.types.contains(63) || params.types.contains(68)) + if (params.types.size() == 0 || Block.inList(params.types, 63) || Block.inList(params.types, 68)) params.needSignText = true; - if (params.bct == BlockChangeType.CHESTACCESS || params.types.size() == 0 || params.types.contains(23) || params.types.contains(54) || params.types.contains(61) || params.types.contains(62)) + if (params.bct == BlockChangeType.CHESTACCESS || params.types.size() == 0 || Block.inList(params.types, 23) || Block.inList(params.types, 54) || Block.inList(params.types, 61) || Block.inList(params.types, 62)) params.needChestAccess = true; } conn = logblock.getConnection(); @@ -465,9 +466,9 @@ public class CommandsHandler implements CommandExecutor params.needType = true; params.needData = true; params.needPlayer = true; - if (params.types.size() == 0 || params.types.contains(63) || params.types.contains(68)) + if (params.types.size() == 0 || Block.inList(params.types, 63) || Block.inList(params.types, 68)) params.needSignText = true; - if (params.types.size() == 0 || params.types.contains(23) || params.types.contains(54) || params.types.contains(61) || params.types.contains(62)) + if (params.types.size() == 0 || Block.inList(params.types, 23) || Block.inList(params.types, 54) || Block.inList(params.types, 61) || Block.inList(params.types, 62)) params.needChestAccess = true; } conn = logblock.getConnection(); diff --git a/src/de/diddiz/LogBlock/QueryParams.java b/src/de/diddiz/LogBlock/QueryParams.java index 0e5fd13..d6eed28 100644 --- a/src/de/diddiz/LogBlock/QueryParams.java +++ b/src/de/diddiz/LogBlock/QueryParams.java @@ -12,6 +12,9 @@ import static de.diddiz.util.Utils.isInt; import static de.diddiz.util.Utils.join; import static de.diddiz.util.Utils.listing; import static de.diddiz.util.Utils.parseTimeSpec; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldedit.bukkit.selections.CuboidSelection; +import com.sk89q.worldedit.bukkit.selections.Selection; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -23,9 +26,7 @@ import org.bukkit.World; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -import com.sk89q.worldedit.bukkit.WorldEditPlugin; -import com.sk89q.worldedit.bukkit.selections.CuboidSelection; -import com.sk89q.worldedit.bukkit.selections.Selection; +import de.diddiz.util.Block; public class QueryParams implements Cloneable { @@ -38,7 +39,7 @@ public class QueryParams implements Cloneable public boolean excludePlayersMode = false, prepareToolQuery = false, silent = false; public Selection sel = null; public SummarizationMode sum = SummarizationMode.NONE; - public List types = new ArrayList(); + public List types = new ArrayList(); public World world = null; public String match = null; public boolean needCount = false, needId = false, needDate = false, needType = false, needData = false, needPlayer = false, needCoords = false, needSignText = false, needChestAccess = false, needMessage = false; @@ -134,7 +135,7 @@ public class QueryParams implements Cloneable if (!types.isEmpty()) { final String[] blocknames = new String[types.size()]; for (int i = 0; i < types.size(); i++) - blocknames[i] = materialName(types.get(i)); + blocknames[i] = materialName(types.get(i).getBlock()); title.append(listing(blocknames, ", ", " and ") + " "); } else title.append("block "); @@ -197,8 +198,15 @@ public class QueryParams implements Cloneable case ALL: if (!types.isEmpty()) { where.append('('); - for (final int type : types) - where.append("type = " + type + " OR replaced = " + type + " OR "); + for (final Block block : types) { + where.append("((type = " + block.getBlock() + " OR replaced = " + block.getBlock()); + if (block.getData() != -1) { + where.append(") AND data = " + block.getData()); + } else { + where.append(")"); + } + where.append(") OR "); + } where.delete(where.length() - 4, where.length() - 1); where.append(") AND "); } @@ -206,8 +214,15 @@ public class QueryParams implements Cloneable case BOTH: if (!types.isEmpty()) { where.append('('); - for (final int type : types) - where.append("type = " + type + " OR replaced = " + type + " OR "); + for (final Block block : types) { + where.append("((type = " + block.getBlock() + " OR replaced = " + block.getBlock()); + if (block.getData() != -1) { + where.append(") AND data = " + block.getData()); + } else { + where.append(")"); + } + where.append(") OR "); + } where.delete(where.length() - 4, where.length()); where.append(") AND "); } @@ -216,8 +231,15 @@ public class QueryParams implements Cloneable case CREATED: if (!types.isEmpty()) { where.append('('); - for (final int type : types) - where.append("type = " + type + " OR "); + for (final Block block : types) { + where.append("((type = " + block.getBlock()); + if (block.getData() != -1) { + where.append(") AND data = " + block.getData()); + } else { + where.append(")"); + } + where.append(") OR "); + } where.delete(where.length() - 4, where.length()); where.append(") AND "); } else @@ -227,8 +249,15 @@ public class QueryParams implements Cloneable case DESTROYED: if (!types.isEmpty()) { where.append('('); - for (final int type : types) - where.append("replaced = " + type + " OR "); + for (final Block block : types) { + where.append("((replaced = " + block.getBlock()); + if (block.getData() != -1) { + where.append(") AND data = " + block.getData()); + } else { + where.append(")"); + } + where.append(") OR "); + } where.delete(where.length() - 4, where.length()); where.append(") AND "); } else @@ -239,8 +268,15 @@ public class QueryParams implements Cloneable where.append("(type = 23 OR type = 54 OR type = 61 OR type = 62) AND type = replaced AND "); if (!types.isEmpty()) { where.append('('); - for (final int type : types) - where.append("itemtype = " + type + " OR "); + for (final Block block : types) { + where.append("((itemtype = " + block.getBlock()); + if (block.getData() != -1) { + where.append(") AND itemdata = " + block.getData()); + } else { + where.append(")"); + } + where.append(") OR "); + } where.delete(where.length() - 4, where.length()); where.append(") AND "); } @@ -309,10 +345,28 @@ public class QueryParams implements Cloneable if (values.length < 1) throw new IllegalArgumentException("No or wrong count of arguments for '" + param + "'"); for (final String blockName : values) { - final Material mat = Material.matchMaterial(blockName); - if (mat == null) - throw new IllegalArgumentException("No material matching: '" + blockName + "'"); - types.add(mat.getId()); + if (blockName.contains(":")) { + String[] blockNameSplit = blockName.split(":"); + if (blockNameSplit.length > 2) + throw new IllegalArgumentException("No material matching: '" + blockName + "'"); + final int data; + try { + data = Integer.parseInt(blockNameSplit[1]); + } catch (NumberFormatException e) { + throw new IllegalArgumentException("Data type not a valid number: '" + blockNameSplit[1] + "'"); + } + if (data > 255 || data < 0) + throw new IllegalArgumentException("Data type out of range (0-255): '" + data + "'"); + final Material mat = Material.matchMaterial(blockNameSplit[0]); + if (mat == null) + throw new IllegalArgumentException("No material matching: '" + blockName + "'"); + types.add(new Block(mat.getId(), data)); + } else { + final Material mat = Material.matchMaterial(blockName); + if (mat == null) + throw new IllegalArgumentException("No material matching: '" + blockName + "'"); + types.add(new Block(mat.getId(), -1)); + } } } else if (param.equals("area")) { if (player == null && !prepareToolQuery) @@ -410,15 +464,15 @@ public class QueryParams implements Cloneable if (types.size() > 0) for (final Set equivalent : getBlockEquivalents()) { boolean found = false; - for (final Integer type : types) - if (equivalent.contains(type)) { + for (final Block block : types) + if (equivalent.contains(block.getBlock())) { found = true; break; } if (found) for (final Integer type : equivalent) - if (!types.contains(type)) - types.add(type); + if (!Block.inList(types, type)) + types.add(new Block(type, -1)); } if (!prepareToolQuery && bct != BlockChangeType.CHAT) { if (world == null) @@ -450,7 +504,7 @@ public class QueryParams implements Cloneable try { final QueryParams params = (QueryParams)super.clone(); params.players = new ArrayList(players); - params.types = new ArrayList(types); + params.types = new ArrayList(types); return params; } catch (final CloneNotSupportedException ex) { } diff --git a/src/de/diddiz/util/Block.java b/src/de/diddiz/util/Block.java new file mode 100644 index 0000000..e411c52 --- /dev/null +++ b/src/de/diddiz/util/Block.java @@ -0,0 +1,36 @@ +package de.diddiz.util; + +import java.util.List; + +public class Block +{ + private int block; + private int data; + + /** + * @param block The id of the block + * @param data The data for the block, -1 for any data + * + */ + public Block(int block, int data) { + this.block = block; + this.data = data; + } + + public int getBlock() { + return this.block; + } + + public int getData() { + return this.data; + } + + public static boolean inList(List types, int blockID) { + for (Block block : types) { + if (block.getBlock() == blockID) { + return true; + } + } + return false; + } +}