Search in sources :

Example 1 with BlockSection

use of org.cubeengine.module.log.action.block.ActionBlock.BlockSection in project modules-extra by CubeEngine.

the class LookupCommands method readBlocks.

private boolean readBlocks(QueryParameter params, String block, User user) {
    if (block == null) {
        return true;
    }
    String[] names = StringUtils.explode(",", block);
    for (String name : names) {
        boolean negate = name.startsWith("!");
        if (negate) {
            name = name.substring(1);
        }
        Byte data = null;
        if (name.contains(":")) {
            String sub = name.substring(name.indexOf(":") + 1);
            try {
                data = Byte.parseByte(sub);
            } catch (NumberFormatException ex) {
                user.sendTranslated(NEGATIVE, "Invalid BlockData: {name#block}", sub);
                return false;
            }
            name = name.substring(0, name.indexOf(":"));
        }
        Material material = Match.material().material(name);
        if (material == null) {
            user.sendTranslated(NEGATIVE, "Unknown Material: {name#material}", name);
            return false;
        }
        BlockSection blockData = new BlockSection(material);
        blockData.data = data == null ? 0 : data;
        if (negate) {
            params.excludeBlock(blockData);
        } else {
            params.includeBlock(blockData);
        }
    }
    return true;
}
Also used : BlockSection(org.cubeengine.module.log.action.block.ActionBlock.BlockSection) Material(org.bukkit.Material)

Example 2 with BlockSection

use of org.cubeengine.module.log.action.block.ActionBlock.BlockSection in project modules-extra by CubeEngine.

the class QueryManager method prepareLookupQuery.

public void prepareLookupQuery(final Lookup lookup, final User user, QueryAction action) {
    if (this.cleanUpRunning) {
        switch(action) {
            case SHOW:
                user.sendTranslated(NEUTRAL, "Lookups cannot return all data while cleaning up the database!");
                break;
            case ROLLBACK:
            case REDO:
            case ROLLBACK_PREVIEW:
            case REDO_PREVIEW:
                user.sendTranslated(NEGATIVE, "This action is not possible while cleaning up the database!");
                user.sendTranslated(NEUTRAL, "Please wait");
                return;
        }
    }
    final QueryParameter params = lookup.getQueryParameter();
    BasicDBObject query = new BasicDBObject();
    if (// has world
    params.world != null) {
        query.append("coord.world-uuid", params.world.getUID().toString());
        if (params.location1 != null) {
            Vector3i loc1 = params.location1;
            if (// has area
            params.location2 != null) {
                Vector3i loc2 = params.location2;
                boolean locX = loc1.x < loc2.x;
                boolean locY = loc1.y < loc2.y;
                boolean locZ = loc1.z < loc2.z;
                query.append("coord.vector.x", new BasicDBObject("$gte", locX ? loc1.x : loc2.x).append("$lte", locX ? loc2.x : loc1.x)).append("coord.vector.y", new BasicDBObject("$gte", locY ? loc1.y : loc2.y).append("$lte", locX ? loc2.y : loc1.y)).append("coord.vector.z", new BasicDBObject("$gte", locZ ? loc1.z : loc2.z).append("$lte", locX ? loc2.z : loc1.z));
            } else if (// has single location
            params.radius == null) {
                query.append("coord.vector.x", loc1.x).append("coord.vector.y", loc1.y).append("coord.vector.z", loc1.z);
            } else // has radius
            {
                query.append("coord.vector.x", new BasicDBObject(QueryOperators.GTE, loc1.x - params.radius).append("$lte", loc1.x + params.radius)).append("coord.vector.y", new BasicDBObject("$gte", loc1.y - params.radius).append("$lte", loc1.y + params.radius)).append("coord.vector.z", new BasicDBObject("$gte", loc1.z - params.radius).append("$lte", loc1.z + params.radius));
            }
        }
    }
    if (!params.actions.isEmpty()) {
        boolean include = params.includeActions();
        Collection<String> actions = new HashSet<>();
        for (Entry<Class<? extends BaseAction>, Boolean> entry : params.actions.entrySet()) {
            if (!include || entry.getValue()) {
                actions.add(entry.getKey().getName());
            }
        }
        if (!include) {
            query.append("action", new BasicDBObject("$nin", actions));
        } else {
            query.append("action", new BasicDBObject("$in", actions));
        }
    }
    if (// has since / before / from-to
    params.hasTime()) {
        if (// before
        params.from_since == null) {
            query.append("date", new BasicDBObject("$lte", params.to_before));
        } else if (// since
        params.to_before == null) {
            query.append("date", new BasicDBObject("$gte", params.from_since));
        } else // from - to
        {
            query.append("date", new BasicDBObject("$gte", params.from_since).append("$lte", params.to_before));
        }
    }
    if (!params.blocks.isEmpty()) {
        // Start filter blocks:
        boolean include = params.includeBlocks();
        List<DBObject> list = new ArrayList<>();
        List<String> mList = new ArrayList<>();
        for (Entry<BlockSection, Boolean> data : params.blocks.entrySet()) {
            if (// all exclude OR only include
            !include || data.getValue()) {
                BlockSection block = data.getKey();
                if (block.data == null) {
                    mList.add(block.material.name());
                } else {
                    list.add(new BasicDBObject("material", block.material.name()).append("data", block.data));
                }
            }
        }
        List<BasicDBObject> blockQuery = new ArrayList<>();
        if (!list.isEmpty()) {
            BasicDBObject dboL = new BasicDBObject("$in", list);
            blockQuery.add(new BasicDBObject("old-block", dboL));
            blockQuery.add(new BasicDBObject("new-block", dboL));
        }
        if (!mList.isEmpty()) {
            BasicDBObject dboML = new BasicDBObject("$in", mList);
            blockQuery.add(new BasicDBObject("old-block.material", dboML));
            blockQuery.add(new BasicDBObject("new-block.material", dboML));
        }
        if (include) {
            query.append("$or", blockQuery);
        } else {
            query.append("$not", new BasicDBObject("$or", blockQuery));
        }
    }
    if (!params.users.isEmpty()) {
        // Start filter users:
        boolean include = params.includeUsers();
        List<String> players = new ArrayList<>();
        for (Entry<UUID, Boolean> data : params.users.entrySet()) {
            if (// all exclude OR only include
            !include || data.getValue()) {
                players.add(data.getKey().toString());
            }
        }
        if (include) {
            query.append("player.uuid", new BasicDBObject("$in", players));
        } else {
            query.append("player.uuid", new BasicDBObject("$nin", players));
        }
    }
    // TODO finish queryParams
    this.module.getLog().debug("{}: Select Query queued!", user.getDisplayName());
    this.queuedLookups.offer(new QueuedSqlParams(lookup, user, query, action));
    if (this.futureLookup == null || this.futureLookup.isDone()) {
        this.futureLookup = lookupExecutor.submit(lookupRunner);
    }
}
Also used : BlockSection(org.cubeengine.module.log.action.block.ActionBlock.BlockSection) ArrayList(java.util.ArrayList) BaseAction(org.cubeengine.module.log.action.BaseAction) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) UUID(java.util.UUID) HashSet(java.util.HashSet)

Aggregations

BlockSection (org.cubeengine.module.log.action.block.ActionBlock.BlockSection)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 Material (org.bukkit.Material)1 BaseAction (org.cubeengine.module.log.action.BaseAction)1