Created pattern, mask, and block registries.

Deprecated getBlock, getBlockPattern, and so-on in WorldEdit.
This commit is contained in:
sk89q
2014-04-03 17:52:53 -07:00
parent 589c3e9629
commit 9d08f266bf
32 changed files with 2192 additions and 478 deletions

View File

@ -19,9 +19,8 @@
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.session.request.Request;
import static com.google.common.base.Preconditions.checkNotNull;
@ -95,12 +94,19 @@ public final class Masks {
/**
* Wrap an old-style mask and convert it to a new mask.
* </p>
* Note, however, that this is strongly not recommended because
* {@link com.sk89q.worldedit.masks.Mask#prepare(LocalSession, LocalPlayer, Vector)}
* is not called.
*
* @param editSession the edit session to bind to
* @param mask the old-style mask
* @param editSession the edit session to bind to
* @return a new-style mask
* @deprecated Please avoid if possible
*/
public static Mask wrap(final EditSession editSession, final com.sk89q.worldedit.masks.Mask mask) {
@Deprecated
@SuppressWarnings("deprecation")
public static Mask wrap(final com.sk89q.worldedit.masks.Mask mask, final EditSession editSession) {
checkNotNull(mask);
return new AbstractMask() {
@Override
@ -110,4 +116,44 @@ public final class Masks {
};
}
/**
* Wrap an old-style mask and convert it to a new mask.
* </p>
* As an {@link EditSession} is not provided in this case, one will be
* taken from the {@link Request}, if possible. If not possible, then the
* mask will return false.
*
* @param mask the old-style mask
* @return a new-style mask
*/
@SuppressWarnings("deprecation")
public static Mask wrap(final com.sk89q.worldedit.masks.Mask mask) {
checkNotNull(mask);
return new AbstractMask() {
@Override
public boolean test(Vector vector) {
EditSession editSession = Request.request().getEditSession();
return editSession != null && mask.matches(editSession, vector);
}
};
}
/**
* Convert a new-style mask to an old-style mask.
*
* @param mask the new-style mask
* @return an old-style mask
*/
@SuppressWarnings("deprecation")
public static com.sk89q.worldedit.masks.Mask wrap(final Mask mask) {
checkNotNull(mask);
return new com.sk89q.worldedit.masks.AbstractMask() {
@Override
public boolean matches(EditSession editSession, Vector pos) {
Request.request().setEditSession(editSession);
return mask.test(pos);
}
};
}
}

View File

@ -0,0 +1,90 @@
/*
* 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.function.mask;
import com.sk89q.worldedit.Vector;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Checks whether the provided mask tests true for an offset position.
*/
public class OffsetMask extends AbstractMask {
private Mask mask;
private Vector offset;
/**
* Create a new instance.
*
* @param mask the mask
* @param offset the offset
*/
public OffsetMask(Mask mask, Vector offset) {
checkNotNull(mask);
checkNotNull(offset);
this.mask = mask;
this.offset = offset;
}
/**
* Get the mask.
*
* @return the mask
*/
public Mask getMask() {
return mask;
}
/**
* Set the mask.
*
* @param mask the mask
*/
public void setMask(Mask mask) {
checkNotNull(mask);
this.mask = mask;
}
/**
* Get the offset.
*
* @return the offset
*/
public Vector getOffset() {
return offset;
}
/**
* Set the offset.
*
* @param offset the offset
*/
public void setOffset(Vector offset) {
checkNotNull(offset);
this.offset = offset;
}
@Override
public boolean test(Vector vector) {
return getMask().test(vector.add(offset));
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.function.pattern;
import com.sk89q.worldedit.CuboidClipboard;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A pattern that reads from {@link CuboidClipboard}.
*
* @deprecated May be removed without notice, but there is no direct replacement yet
*/
@Deprecated
public class ClipboardPattern extends AbstractPattern {
private final CuboidClipboard clipboard;
private final Vector size;
/**
* Create a new clipboard pattern.
*
* @param clipboard the clipboard
*/
public ClipboardPattern(CuboidClipboard clipboard) {
checkNotNull(clipboard);
this.clipboard = clipboard;
this.size = clipboard.getSize();
}
@Override
public BaseBlock apply(Vector position) {
int xp = Math.abs(position.getBlockX()) % size.getBlockX();
int yp = Math.abs(position.getBlockY()) % size.getBlockY();
int zp = Math.abs(position.getBlockZ()) % size.getBlockZ();
return clipboard.getPoint(new Vector(xp, yp, zp));
}
}

View File

@ -48,4 +48,25 @@ public final class Patterns {
};
}
/**
* Wrap a new-style pattern and return an old-style pattern.
*
* @param pattern the pattern
* @return an old-style pattern
*/
public static com.sk89q.worldedit.patterns.Pattern wrap(final Pattern pattern) {
checkNotNull(pattern);
return new com.sk89q.worldedit.patterns.Pattern() {
@Override
public BaseBlock next(Vector position) {
return pattern.apply(position);
}
@Override
public BaseBlock next(int x, int y, int z) {
return next(new Vector(x, y, z));
}
};
}
}

View File

@ -0,0 +1,95 @@
/*
* 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.function.pattern;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.extent.Extent;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Returns the blocks from {@link Extent}, repeating when out of bounds.
*/
public class RepeatingExtentPattern extends AbstractPattern {
private Extent extent;
private Vector offset;
/**
* Create a new instance.
*
* @param extent the extent
* @param offset the offset
*/
public RepeatingExtentPattern(Extent extent, Vector offset) {
setExtent(extent);
setOffset(offset);
}
/**
* Get the extent.
*
* @return the extent
*/
public Extent getExtent() {
return extent;
}
/**
* Set the extent.
*
* @param extent the extent
*/
public void setExtent(Extent extent) {
checkNotNull(extent);
this.extent = extent;
}
/**
* Get the offset.
*
* @return the offset
*/
public Vector getOffset() {
return offset;
}
/**
* Set the offset.
*
* @param offset the offset
*/
public void setOffset(Vector offset) {
checkNotNull(offset);
this.offset = offset;
}
@Override
public BaseBlock apply(Vector position) {
Vector base = position.add(offset);
Vector size = extent.getMaximumPoint().subtract(extent.getMinimumPoint());
int x = base.getBlockX() % size.getBlockX();
int y = base.getBlockY() % size.getBlockY();
int z = base.getBlockZ() % size.getBlockZ();
return extent.getBlock(new Vector(x, y, z));
}
}