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
This commit is contained in:
Ammar Askar
2012-07-10 22:14:23 +01:00
committed by md_5
parent ffa0bc5ebf
commit f5e7379fc9
3 changed files with 119 additions and 28 deletions

View File

@@ -48,6 +48,7 @@ import de.diddiz.LogBlock.QueryParams.SummarizationMode;
import de.diddiz.LogBlock.config.Config; import de.diddiz.LogBlock.config.Config;
import de.diddiz.LogBlock.config.WorldConfig; import de.diddiz.LogBlock.config.WorldConfig;
import de.diddiz.LogBlockQuestioner.LogBlockQuestioner; import de.diddiz.LogBlockQuestioner.LogBlockQuestioner;
import de.diddiz.util.Block;
public class CommandsHandler implements CommandExecutor public class CommandsHandler implements CommandExecutor
{ {
@@ -408,9 +409,9 @@ public class CommandsHandler implements CommandExecutor
params.needType = true; params.needType = true;
params.needData = true; params.needData = true;
params.needPlayer = 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; 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; params.needChestAccess = true;
} }
conn = logblock.getConnection(); conn = logblock.getConnection();
@@ -465,9 +466,9 @@ public class CommandsHandler implements CommandExecutor
params.needType = true; params.needType = true;
params.needData = true; params.needData = true;
params.needPlayer = 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; 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; params.needChestAccess = true;
} }
conn = logblock.getConnection(); conn = logblock.getConnection();

View File

@@ -12,6 +12,9 @@ import static de.diddiz.util.Utils.isInt;
import static de.diddiz.util.Utils.join; import static de.diddiz.util.Utils.join;
import static de.diddiz.util.Utils.listing; import static de.diddiz.util.Utils.listing;
import static de.diddiz.util.Utils.parseTimeSpec; 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.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
@@ -23,9 +26,7 @@ import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.sk89q.worldedit.bukkit.WorldEditPlugin; import de.diddiz.util.Block;
import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldedit.bukkit.selections.Selection;
public class QueryParams implements Cloneable public class QueryParams implements Cloneable
{ {
@@ -38,7 +39,7 @@ public class QueryParams implements Cloneable
public boolean excludePlayersMode = false, prepareToolQuery = false, silent = false; public boolean excludePlayersMode = false, prepareToolQuery = false, silent = false;
public Selection sel = null; public Selection sel = null;
public SummarizationMode sum = SummarizationMode.NONE; public SummarizationMode sum = SummarizationMode.NONE;
public List<Integer> types = new ArrayList<Integer>(); public List<Block> types = new ArrayList<Block>();
public World world = null; public World world = null;
public String match = 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; 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()) { if (!types.isEmpty()) {
final String[] blocknames = new String[types.size()]; final String[] blocknames = new String[types.size()];
for (int i = 0; i < types.size(); i++) 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 ") + " "); title.append(listing(blocknames, ", ", " and ") + " ");
} else } else
title.append("block "); title.append("block ");
@@ -197,8 +198,15 @@ public class QueryParams implements Cloneable
case ALL: case ALL:
if (!types.isEmpty()) { if (!types.isEmpty()) {
where.append('('); where.append('(');
for (final int type : types) for (final Block block : types) {
where.append("type = " + type + " OR replaced = " + type + " OR "); 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.delete(where.length() - 4, where.length() - 1);
where.append(") AND "); where.append(") AND ");
} }
@@ -206,8 +214,15 @@ public class QueryParams implements Cloneable
case BOTH: case BOTH:
if (!types.isEmpty()) { if (!types.isEmpty()) {
where.append('('); where.append('(');
for (final int type : types) for (final Block block : types) {
where.append("type = " + type + " OR replaced = " + type + " OR "); 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.delete(where.length() - 4, where.length());
where.append(") AND "); where.append(") AND ");
} }
@@ -216,8 +231,15 @@ public class QueryParams implements Cloneable
case CREATED: case CREATED:
if (!types.isEmpty()) { if (!types.isEmpty()) {
where.append('('); where.append('(');
for (final int type : types) for (final Block block : types) {
where.append("type = " + type + " OR "); 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.delete(where.length() - 4, where.length());
where.append(") AND "); where.append(") AND ");
} else } else
@@ -227,8 +249,15 @@ public class QueryParams implements Cloneable
case DESTROYED: case DESTROYED:
if (!types.isEmpty()) { if (!types.isEmpty()) {
where.append('('); where.append('(');
for (final int type : types) for (final Block block : types) {
where.append("replaced = " + type + " OR "); 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.delete(where.length() - 4, where.length());
where.append(") AND "); where.append(") AND ");
} else } 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 "); where.append("(type = 23 OR type = 54 OR type = 61 OR type = 62) AND type = replaced AND ");
if (!types.isEmpty()) { if (!types.isEmpty()) {
where.append('('); where.append('(');
for (final int type : types) for (final Block block : types) {
where.append("itemtype = " + type + " OR "); 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.delete(where.length() - 4, where.length());
where.append(") AND "); where.append(") AND ");
} }
@@ -309,10 +345,28 @@ public class QueryParams implements Cloneable
if (values.length < 1) if (values.length < 1)
throw new IllegalArgumentException("No or wrong count of arguments for '" + param + "'"); throw new IllegalArgumentException("No or wrong count of arguments for '" + param + "'");
for (final String blockName : values) { for (final String blockName : values) {
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); final Material mat = Material.matchMaterial(blockName);
if (mat == null) if (mat == null)
throw new IllegalArgumentException("No material matching: '" + blockName + "'"); throw new IllegalArgumentException("No material matching: '" + blockName + "'");
types.add(mat.getId()); types.add(new Block(mat.getId(), -1));
}
} }
} else if (param.equals("area")) { } else if (param.equals("area")) {
if (player == null && !prepareToolQuery) if (player == null && !prepareToolQuery)
@@ -410,15 +464,15 @@ public class QueryParams implements Cloneable
if (types.size() > 0) if (types.size() > 0)
for (final Set<Integer> equivalent : getBlockEquivalents()) { for (final Set<Integer> equivalent : getBlockEquivalents()) {
boolean found = false; boolean found = false;
for (final Integer type : types) for (final Block block : types)
if (equivalent.contains(type)) { if (equivalent.contains(block.getBlock())) {
found = true; found = true;
break; break;
} }
if (found) if (found)
for (final Integer type : equivalent) for (final Integer type : equivalent)
if (!types.contains(type)) if (!Block.inList(types, type))
types.add(type); types.add(new Block(type, -1));
} }
if (!prepareToolQuery && bct != BlockChangeType.CHAT) { if (!prepareToolQuery && bct != BlockChangeType.CHAT) {
if (world == null) if (world == null)
@@ -450,7 +504,7 @@ public class QueryParams implements Cloneable
try { try {
final QueryParams params = (QueryParams)super.clone(); final QueryParams params = (QueryParams)super.clone();
params.players = new ArrayList<String>(players); params.players = new ArrayList<String>(players);
params.types = new ArrayList<Integer>(types); params.types = new ArrayList<Block>(types);
return params; return params;
} catch (final CloneNotSupportedException ex) { } catch (final CloneNotSupportedException ex) {
} }

View File

@@ -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<Block> types, int blockID) {
for (Block block : types) {
if (block.getBlock() == blockID) {
return true;
}
}
return false;
}
}