Made some masks use Extents rather than EditSessions.

This commit is contained in:
sk89q 2014-03-29 21:07:02 -07:00
parent 70f409975e
commit e7bbd1ac53
7 changed files with 91 additions and 16 deletions

View File

@ -1,6 +1,7 @@
package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
@ -8,7 +9,7 @@ import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public class BlockMask extends AbstractMask {
public class BlockMask extends ExtentAwareMask {
private final Set<BaseBlock> blocks;
@ -42,7 +43,8 @@ public class BlockMask extends AbstractMask {
@Override
public boolean matches(EditSession editSession, Vector pos) {
BaseBlock block = editSession.getBlock(pos);
Extent extent = getExtent(editSession);
BaseBlock block = extent.getBlock(pos);
return blocks.contains(block)
|| blocks.contains(new BaseBlock(block.getType(), -1));
}

View File

@ -23,9 +23,9 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockID;
public class ExistingBlockMask extends AbstractMask {
public class ExistingBlockMask extends ExtentAwareMask {
@Override
public boolean matches(EditSession editSession, Vector pos) {
return editSession.getBlockType(pos) != BlockID.AIR;
return getExtent(editSession).getBlockType(pos) != BlockID.AIR;
}
}

View File

@ -0,0 +1,71 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
/**
* Extended by masks to make them potentially use an {@link Extent} rather than
* the {@link EditSession}.
* </p>
* At the moment, masks are coupled to {@link EditSession} when they should
* not be. However, because a change to {@link Mask} would cause massive breakage in
* the API, that change is deferred until further notice and this class exists as
* an opt-in mixin for adding support for {@link Extent}s.
*/
public abstract class ExtentAwareMask extends AbstractMask {
private Extent extent;
/**
* Get the extent that will be used for lookups.
*
* @return the extent, or null if the {@link EditSession} is to be used
*/
public Extent getExtent() {
return extent;
}
/**
* Set the extent that will be used for lookups.
*
* @param extent the extent, or null if the {@link EditSession} is to be used
*/
public void setExtent(Extent extent) {
this.extent = extent;
}
/**
* Get the extent to use for operations. Subclasses should call this method
* rather than access the passed {@link EditSession} directly.
*
* @param editSession the passed in {@link EditSession}
* @return an extent
*/
protected Extent getExtent(EditSession editSession) {
if (extent != null) {
return extent;
} else {
return editSession;
}
}
}

View File

@ -20,6 +20,7 @@
package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
@ -30,7 +31,7 @@ import java.util.Set;
* Uses {@link BaseBlock#containsFuzzy(java.util.Collection, BaseBlock)} to
* match blocks.
*/
public class FuzzyBlockMask extends AbstractMask {
public class FuzzyBlockMask extends ExtentAwareMask {
private final Set<BaseBlock> filter;
@ -58,7 +59,8 @@ public class FuzzyBlockMask extends AbstractMask {
@Override
public boolean matches(EditSession editSession, Vector pos) {
BaseBlock compare = new BaseBlock(editSession.getBlockType(pos), editSession.getBlockData(pos));
Extent extent = getExtent(editSession);
BaseBlock compare = new BaseBlock(extent.getBlockType(pos), extent.getBlockData(pos));
return BaseBlock.containsFuzzy(filter, compare);
}
}

View File

@ -48,8 +48,8 @@ public interface Mask {
* that position matches the filter. Block information is not provided
* as getting a BaseBlock has unneeded overhead in most block querying
* situations (enumerating a chest's contents is a waste, for example).
*
* @param editSession
*
* @param editSession
* @param pos
* @return
*/

View File

@ -1,15 +1,17 @@
package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockType;
/**
* Works like {@link ExistingBlockMask}, except also dealing with non-solid non-air blocks the same way as with air.
*/
public class SolidBlockMask extends AbstractMask {
public class SolidBlockMask extends ExtentAwareMask {
@Override
public boolean matches(EditSession editSession, Vector pos) {
return !BlockType.canPassThrough(editSession.getBlockType(pos), editSession.getBlockData(pos));
Extent extent = getExtent(editSession);
return !BlockType.canPassThrough(extent.getBlockType(pos), extent.getBlockData(pos));
}
}

View File

@ -21,17 +21,14 @@ package com.sk89q.worldedit.masks;
import java.util.Set;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock;
/**
*
* @author 1337
*/
public class UnderOverlayMask extends AbstractMask {
public class UnderOverlayMask extends ExtentAwareMask {
private final int yMod;
private Mask mask;
@ -68,6 +65,7 @@ public class UnderOverlayMask extends AbstractMask {
@Override
public boolean matches(EditSession editSession, Vector pos) {
return !mask.matches(editSession, pos) && mask.matches(editSession, pos.add(0, yMod, 0));
Extent extent = getExtent(editSession);
return !mask.matches(extent, pos) && mask.matches(extent, pos.add(0, yMod, 0));
}
}