Skip to content
Permalink
Browse files

Make BlockStateHolder extend Pattern.

Removes the need for wrapping them in BlockPattern.
  • Loading branch information...
wizjany committed Jul 28, 2019
1 parent f4c238c commit 6631b6bdf0638b674b0f0dabad4eb77c0510e5df
Showing with 59 additions and 53 deletions.
  1. +12 −16 worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java
  2. +1 −3 worldedit-core/src/main/java/com/sk89q/worldedit/command/BrushCommands.java
  3. +5 −5 worldedit-core/src/main/java/com/sk89q/worldedit/command/ToolCommands.java
  4. +2 −3 worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/BlockReplacer.java
  5. +1 −2 worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/CylinderBrush.java
  6. +1 −2 worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/HollowCylinderBrush.java
  7. +1 −2 worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/HollowSphereBrush.java
  8. +1 −2 worldedit-core/src/main/java/com/sk89q/worldedit/command/tool/brush/SphereBrush.java
  9. +2 −3 ...rc/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/BlockCategoryPatternParser.java
  10. +1 −2 .../src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/RandomStatePatternParser.java
  11. +1 −2 .../src/main/java/com/sk89q/worldedit/extension/factory/parser/pattern/SingleBlockPatternParser.java
  12. +6 −7 worldedit-core/src/main/java/com/sk89q/worldedit/function/generator/FloraGenerator.java
  13. +2 −3 worldedit-core/src/main/java/com/sk89q/worldedit/function/generator/GardenPatchGenerator.java
  14. +3 −0 worldedit-core/src/main/java/com/sk89q/worldedit/function/pattern/BlockPattern.java
  15. +6 −0 worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BaseBlock.java
  16. +6 −0 worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockState.java
  17. +8 −1 worldedit-core/src/main/java/com/sk89q/worldedit/world/block/BlockStateHolder.java
@@ -62,7 +62,6 @@
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.OperationQueue;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.function.pattern.WaterloggedRemover;
import com.sk89q.worldedit.function.util.RegionOffset;
@@ -820,7 +819,7 @@ public int countBlocks(Region region, Mask searchMask) {
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public <B extends BlockStateHolder<B>> int fillXZ(BlockVector3 origin, B block, double radius, int depth, boolean recursive) throws MaxChangedBlocksException {
return fillXZ(origin, new BlockPattern(block), radius, depth, recursive);
return fillXZ(origin, (Pattern) block, radius, depth, recursive);
}

/**
@@ -885,8 +884,7 @@ public int removeAbove(BlockVector3 position, int apothem, int height) throws Ma
getWorld(), // Causes clamping of Y range
position.add(-apothem + 1, 0, -apothem + 1),
position.add(apothem - 1, height - 1, apothem - 1));
Pattern pattern = new BlockPattern(BlockTypes.AIR.getDefaultState());
return setBlocks(region, pattern);
return setBlocks(region, BlockTypes.AIR.getDefaultState());
}

/**
@@ -907,8 +905,7 @@ public int removeBelow(BlockVector3 position, int apothem, int height) throws Ma
getWorld(), // Causes clamping of Y range
position.add(-apothem + 1, 0, -apothem + 1),
position.add(apothem - 1, -height + 1, apothem - 1));
Pattern pattern = new BlockPattern(BlockTypes.AIR.getDefaultState());
return setBlocks(region, pattern);
return setBlocks(region, BlockTypes.AIR.getDefaultState());
}

/**
@@ -929,8 +926,7 @@ public int removeNear(BlockVector3 position, Mask mask, int apothem) throws MaxC
getWorld(), // Causes clamping of Y range
position.add(adjustment.multiply(-1)),
position.add(adjustment));
Pattern pattern = new BlockPattern(BlockTypes.AIR.getDefaultState());
return replaceBlocks(region, mask, pattern);
return replaceBlocks(region, mask, BlockTypes.AIR.getDefaultState());
}

/**
@@ -942,7 +938,7 @@ public int removeNear(BlockVector3 position, Mask mask, int apothem) throws MaxC
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public <B extends BlockStateHolder<B>> int setBlocks(Region region, B block) throws MaxChangedBlocksException {
return setBlocks(region, new BlockPattern(block));
return setBlocks(region, (Pattern) block);
}

/**
@@ -974,7 +970,7 @@ public int setBlocks(Region region, Pattern pattern) throws MaxChangedBlocksExce
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public <B extends BlockStateHolder<B>> int replaceBlocks(Region region, Set<BaseBlock> filter, B replacement) throws MaxChangedBlocksException {
return replaceBlocks(region, filter, new BlockPattern(replacement));
return replaceBlocks(region, filter, (Pattern) replacement);
}

/**
@@ -1046,7 +1042,7 @@ public int center(Region region, Pattern pattern) throws MaxChangedBlocksExcepti
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public <B extends BlockStateHolder<B>> int makeCuboidFaces(Region region, B block) throws MaxChangedBlocksException {
return makeCuboidFaces(region, new BlockPattern(block));
return makeCuboidFaces(region, block);
}

/**
@@ -1098,7 +1094,7 @@ public int makeFaces(final Region region, Pattern pattern) throws MaxChangedBloc
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public <B extends BlockStateHolder<B>> int makeCuboidWalls(Region region, B block) throws MaxChangedBlocksException {
return makeCuboidWalls(region, new BlockPattern(block));
return makeCuboidWalls(region, (Pattern) block);
}

/**
@@ -1165,7 +1161,7 @@ protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial)
public <B extends BlockStateHolder<B>> int overlayCuboidBlocks(Region region, B block) throws MaxChangedBlocksException {
checkNotNull(block);

return overlayCuboidBlocks(region, new BlockPattern(block));
return overlayCuboidBlocks(region, (Pattern) block);
}

/**
@@ -1255,7 +1251,7 @@ public int moveRegion(Region region, BlockVector3 dir, int distance, boolean cop
// Remove the original blocks
Pattern pattern = replacement != null ?
replacement :
new BlockPattern(BlockTypes.AIR.getDefaultState());
BlockTypes.AIR.getDefaultState();
BlockReplace remove = new BlockReplace(this, pattern);

// Copy to a buffer so we don't destroy our original before we can copy all the blocks from it
@@ -1334,7 +1330,7 @@ public int drainArea(BlockVector3 origin, double radius, boolean waterlogged) th
if (waterlogged) {
replace = new BlockReplace(this, new WaterloggedRemover(this));
} else {
replace = new BlockReplace(this, new BlockPattern(BlockTypes.AIR.getDefaultState()));
replace = new BlockReplace(this, BlockTypes.AIR.getDefaultState());
}
RecursiveVisitor visitor = new RecursiveVisitor(mask, replace);

@@ -1376,7 +1372,7 @@ public int fixLiquid(BlockVector3 origin, double radius, BlockType fluid) throws
blockMask
);

BlockReplace replace = new BlockReplace(this, new BlockPattern(fluid.getDefaultState()));
BlockReplace replace = new BlockReplace(this, fluid.getDefaultState());
NonRisingVisitor visitor = new NonRisingVisitor(mask, replace);

// Around the origin in a 3x3 block
@@ -47,7 +47,6 @@
import com.sk89q.worldedit.function.mask.BlockTypeMask;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.factory.RegionFactory;
@@ -207,8 +206,7 @@ public void extinguishBrush(Player player, LocalSession session,
worldEdit.checkMaxBrushRadius(radius);

BrushTool tool = session.getBrushTool(player.getItemInHand(HandSide.MAIN_HAND).getType());
Pattern fill = new BlockPattern(BlockTypes.AIR.getDefaultState());
tool.setFill(fill);
tool.setFill(BlockTypes.AIR.getDefaultState());
tool.setSize(radius);
tool.setMask(new BlockTypeMask(new RequestExtent(), BlockTypes.FIRE));
tool.setBrush(new SphereBrush(), "worldedit.brush.ex");
@@ -37,10 +37,10 @@
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.util.HandSide;
import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.item.ItemType;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
@@ -209,11 +209,11 @@ public void longrangebuildtool(Player player, LocalSession session,
player.print("Long-range building tool bound to " + itemStack.getType().getName() + ".");
String primaryName = "pattern";
String secondaryName = "pattern";
if (primary instanceof BlockPattern) {
primaryName = ((BlockPattern) primary).getBlock().getBlockType().getName();
if (primary instanceof BlockStateHolder) {
primaryName = ((BlockStateHolder<?>) primary).getBlockType().getName();
}
if (secondary instanceof BlockPattern) {
secondaryName = ((BlockPattern) secondary).getBlock().getBlockType().getName();
if (secondary instanceof BlockStateHolder) {
secondaryName = ((BlockStateHolder<?>) secondary).getBlockType().getName();
}
player.print("Left-click set to " + primaryName + "; right-click set to "
+ secondaryName + ".");
@@ -27,7 +27,6 @@
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extension.platform.Platform;
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.util.Location;
@@ -57,7 +56,7 @@ public boolean actPrimary(Platform server, LocalConfiguration config, Player pla
try {
editSession.disableBuffering();
BlockVector3 position = clicked.toVector().toBlockPoint();
editSession.setBlock(position, pattern.apply(position));
editSession.setBlock(position, pattern);
} catch (MaxChangedBlocksException ignored) {
} finally {
session.remember(editSession);
@@ -77,7 +76,7 @@ public boolean actSecondary(Platform server, LocalConfiguration config, Player p
BaseBlock targetBlock = player.getWorld().getFullBlock(clicked.toVector().toBlockPoint());

if (targetBlock != null) {
pattern = new BlockPattern(targetBlock);
pattern = targetBlock;
player.print("Replacer tool switched to: " + targetBlock.getBlockType().getName());
}

@@ -21,7 +21,6 @@

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockTypes;
@@ -37,7 +36,7 @@ public CylinderBrush(int height) {
@Override
public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws MaxChangedBlocksException {
if (pattern == null) {
pattern = new BlockPattern(BlockTypes.COBBLESTONE.getDefaultState());
pattern = BlockTypes.COBBLESTONE.getDefaultState();
}
editSession.makeCylinder(position, pattern, size, size, height, true);
}
@@ -21,7 +21,6 @@

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockTypes;
@@ -37,7 +36,7 @@ public HollowCylinderBrush(int height) {
@Override
public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws MaxChangedBlocksException {
if (pattern == null) {
pattern = new BlockPattern(BlockTypes.COBBLESTONE.getDefaultState());
pattern = BlockTypes.COBBLESTONE.getDefaultState();
}
editSession.makeCylinder(position, pattern, size, size, height, false);
}
@@ -21,7 +21,6 @@

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockTypes;
@@ -31,7 +30,7 @@
@Override
public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws MaxChangedBlocksException {
if (pattern == null) {
pattern = new BlockPattern(BlockTypes.COBBLESTONE.getDefaultState());
pattern = BlockTypes.COBBLESTONE.getDefaultState();
}
editSession.makeSphere(position, pattern, size, size, size, false);
}
@@ -21,7 +21,6 @@

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockTypes;
@@ -31,7 +30,7 @@
@Override
public void build(EditSession editSession, BlockVector3 position, Pattern pattern, double size) throws MaxChangedBlocksException {
if (pattern == null) {
pattern = new BlockPattern(BlockTypes.COBBLESTONE.getDefaultState());
pattern = BlockTypes.COBBLESTONE.getDefaultState();
}
editSession.makeSphere(position, pattern, size, size, size, true);
}
@@ -23,7 +23,6 @@
import com.sk89q.worldedit.command.util.SuggestionHelper;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.function.pattern.RandomPattern;
import com.sk89q.worldedit.internal.registry.InputParser;
@@ -70,10 +69,10 @@ public Pattern parseFromInput(String input, ParserContext context) throws InputP

if (anyState) {
blocks.stream().flatMap(blockType -> blockType.getAllStates().stream()).forEach(state ->
randomPattern.add(new BlockPattern(state), 1.0));
randomPattern.add(state, 1.0));
} else {
for (BlockType blockType : blocks) {
randomPattern.add(new BlockPattern(blockType.getDefaultState()), 1.0);
randomPattern.add(blockType.getDefaultState(), 1.0);
}
}

@@ -22,7 +22,6 @@
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.function.pattern.RandomStatePattern;
import com.sk89q.worldedit.internal.registry.InputParser;
@@ -60,7 +59,7 @@ public Pattern parseFromInput(String input, ParserContext context) throws InputP
context.setPreferringWildcard(wasFuzzy);
if (block.getStates().size() == block.getBlockType().getPropertyMap().size()) {
// they requested random with *, but didn't leave any states empty - simplify
return new BlockPattern(block);
return block;
} else if (block.toImmutableState() instanceof FuzzyBlockState) {
return new RandomStatePattern((FuzzyBlockState) block.toImmutableState());
} else {
@@ -22,7 +22,6 @@
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.internal.registry.InputParser;

@@ -41,7 +40,7 @@ public SingleBlockPatternParser(WorldEdit worldEdit) {

@Override
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException {
return new BlockPattern(worldEdit.getBlockFactory().parseFromInput(input, context));
return worldEdit.getBlockFactory().parseFromInput(input, context);
}

}
@@ -22,7 +22,6 @@
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.function.pattern.RandomPattern;
import com.sk89q.worldedit.math.BlockVector3;
@@ -83,9 +82,9 @@ public void setBiomeAware(boolean biomeAware) {
*/
public static Pattern getDesertPattern() {
RandomPattern pattern = new RandomPattern();
pattern.add(new BlockPattern(BlockTypes.DEAD_BUSH.getDefaultState()), 30);
pattern.add(new BlockPattern(BlockTypes.CACTUS.getDefaultState()), 20);
pattern.add(new BlockPattern(BlockTypes.AIR.getDefaultState()), 300);
pattern.add(BlockTypes.DEAD_BUSH.getDefaultState(), 30);
pattern.add(BlockTypes.CACTUS.getDefaultState(), 20);
pattern.add(BlockTypes.AIR.getDefaultState(), 300);
return pattern;
}

@@ -96,9 +95,9 @@ public static Pattern getDesertPattern() {
*/
public static Pattern getTemperatePattern() {
RandomPattern pattern = new RandomPattern();
pattern.add(new BlockPattern(BlockTypes.GRASS.getDefaultState()), 300);
pattern.add(new BlockPattern(BlockTypes.POPPY.getDefaultState()), 5);
pattern.add(new BlockPattern(BlockTypes.DANDELION.getDefaultState()), 5);
pattern.add(BlockTypes.GRASS.getDefaultState(), 300);
pattern.add(BlockTypes.POPPY.getDefaultState(), 5);
pattern.add(BlockTypes.DANDELION.getDefaultState(), 5);
return pattern;
}

@@ -23,7 +23,6 @@
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.pattern.BlockPattern;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BlockState;
@@ -187,7 +186,7 @@ public boolean apply(BlockVector3 position) throws WorldEditException {
* @return a pumpkin pattern
*/
public static Pattern getPumpkinPattern() {
return new BlockPattern(BlockTypes.PUMPKIN.getDefaultState());
return BlockTypes.PUMPKIN.getDefaultState();
}

/**
@@ -208,6 +207,6 @@ public static Pattern getPumpkinPattern() {
* @return a melon pattern
*/
public static Pattern getMelonPattern() {
return new BlockPattern(BlockTypes.MELON.getDefaultState());
return BlockTypes.MELON.getDefaultState();
}
}
@@ -27,7 +27,10 @@

/**
* A pattern that returns the same {@link BaseBlock} each time.
*
* @deprecated all BlockStateHolders can be used directly as a pattern
*/
@Deprecated
public class BlockPattern extends AbstractPattern {

private BaseBlock block;

1 comment on commit 6631b6b

@MattBDev

This comment has been minimized.

Copy link

commented on 6631b6b Jul 28, 2019

🥳

Please sign in to comment.
You can’t perform that action at this time.