Search in sources :

Example 11 with BlockBag

use of com.sk89q.worldedit.extent.inventory.BlockBag in project FastAsyncWorldEdit by IntellectualSites.

the class BrushTool method act.

public boolean act(BrushAction action, Player player, LocalSession session) {
    switch(action) {
        case PRIMARY:
            setContext(primary);
            break;
        case SECONDARY:
            setContext(secondary);
            break;
        default:
            throw new IllegalStateException("Unexpected value: " + action);
    }
    BrushSettings current = getContext();
    Brush brush = current.getBrush();
    if (brush == null) {
        return false;
    }
    if (!current.canUse(player)) {
        player.print(Caption.of("fawe.error.no-perm", StringMan.join(current.getPermissions(), ",")));
        return false;
    }
    try (EditSession editSession = session.createEditSession(player, current.toString())) {
        Location target = player.getBlockTrace(getRange(), true, traceMask);
        if (target == null) {
            editSession.cancel();
            player.print(Caption.of("worldedit.tool.no-block"));
            return true;
        }
        BlockBag bag = session.getBlockBag(player);
        Request.request().setEditSession(editSession);
        Mask mask = current.getMask();
        if (mask != null) {
            Mask existingMask = editSession.getMask();
            if (existingMask == null) {
                editSession.setMask(mask);
            } else if (existingMask instanceof MaskIntersection) {
                ((MaskIntersection) existingMask).add(mask);
            } else {
                MaskIntersection newMask = new MaskIntersection(existingMask);
                newMask.add(mask);
                editSession.setMask(newMask);
            }
        }
        Mask sourceMask = current.getSourceMask();
        if (sourceMask != null) {
            editSession.addSourceMask(sourceMask);
        }
        ResettableExtent transform = current.getTransform();
        if (transform != null) {
            editSession.addTransform(transform);
        }
        try {
            new PatternTraverser(current).reset(editSession);
            double size = current.getSize();
            WorldEdit.getInstance().checkMaxBrushRadius(size);
            brush.build(editSession, target.toBlockPoint(), current.getMaterial(), size);
        } catch (MaxChangedBlocksException e) {
            player.print(Caption.of("worldedit.tool.max-block-changes"));
        } finally {
            session.remember(editSession);
            if (bag != null) {
                bag.flushChanges();
            }
        }
    } finally {
        Request.reset();
    }
    return true;
}
Also used : MaskIntersection(com.sk89q.worldedit.function.mask.MaskIntersection) BlockBag(com.sk89q.worldedit.extent.inventory.BlockBag) SolidBlockMask(com.sk89q.worldedit.function.mask.SolidBlockMask) Mask(com.sk89q.worldedit.function.mask.Mask) PatternTraverser(com.fastasyncworldedit.core.function.pattern.PatternTraverser) EditSession(com.sk89q.worldedit.EditSession) BrushSettings(com.fastasyncworldedit.core.command.tool.brush.BrushSettings) Brush(com.sk89q.worldedit.command.tool.brush.Brush) ResettableExtent(com.fastasyncworldedit.core.extent.ResettableExtent) Location(com.sk89q.worldedit.util.Location) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException)

Aggregations

BlockBag (com.sk89q.worldedit.extent.inventory.BlockBag)11 EditSession (com.sk89q.worldedit.EditSession)8 MaxChangedBlocksException (com.sk89q.worldedit.MaxChangedBlocksException)5 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)5 LocalSession (com.sk89q.worldedit.LocalSession)3 Player (com.sk89q.worldedit.entity.Player)3 Location (com.sk89q.worldedit.util.Location)3 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)3 BlockType (com.sk89q.worldedit.world.block.BlockType)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)2 Command (org.enginehub.piston.annotation.Command)2 BrushSettings (com.fastasyncworldedit.core.command.tool.brush.BrushSettings)1 ResettableExtent (com.fastasyncworldedit.core.extent.ResettableExtent)1 SlottableBlockBag (com.fastasyncworldedit.core.extent.inventory.SlottableBlockBag)1 PatternTraverser (com.fastasyncworldedit.core.function.pattern.PatternTraverser)1 AbstractChangeSet (com.fastasyncworldedit.core.history.changeset.AbstractChangeSet)1 BlockBagChangeSet (com.fastasyncworldedit.core.history.changeset.BlockBagChangeSet)1 NBTException (com.fastasyncworldedit.core.jnbt.NBTException)1