Skip to content

Commit

Permalink
Merge branch 'main' into plotsquared-v7
Browse files Browse the repository at this point in the history
  • Loading branch information
dordsor21 authored Jul 20, 2023
2 parents ea06cd1 + 9543adc commit d50dea7
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,29 @@ public CompoundTag getNbtData() {
values.put("MaxNearbyEntities", new ShortTag(maxNearbyEntities));
values.put("RequiredPlayerRange", new ShortTag(requiredPlayerRange));
if (spawnData == null) {
values.put("SpawnData", new CompoundTag(ImmutableMap.of("id", new StringTag(mobType))));
values.put(
"SpawnData",
new CompoundTag(ImmutableMap.of("entity", new CompoundTag(ImmutableMap.of("id", new StringTag(mobType)))))
);
} else {
values.put("SpawnData", new CompoundTag(spawnData.getValue()));
}
if (spawnPotentials == null) {
values.put("SpawnPotentials", new ListTag(CompoundTag.class, ImmutableList.of(
new CompoundTag(ImmutableMap.of("Weight", new IntTag(1), "Entity",
new CompoundTag(ImmutableMap.of("id", new StringTag(mobType)))
)))));
values.put(
"SpawnPotentials",
new ListTag(
CompoundTag.class,
ImmutableList.of(new CompoundTag(ImmutableMap.of(
"weight",
new IntTag(1),
"data",
new CompoundTag(ImmutableMap.of(
"entity",
new CompoundTag(ImmutableMap.of("id", new StringTag(mobType)))
))
)))
)
);
} else {
values.put("SpawnPotentials", new ListTag(CompoundTag.class, spawnPotentials.getValue()));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.fastasyncworldedit.core.extension.factory.parser.pattern;

import com.fastasyncworldedit.core.configuration.Caption;
import com.fastasyncworldedit.core.extension.factory.parser.RichParser;
import com.fastasyncworldedit.core.function.pattern.TypeSwapPattern;
import com.fastasyncworldedit.core.util.Permission;
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.Pattern;
import com.sk89q.worldedit.util.formatting.text.TextComponent;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.stream.Stream;

public class TypeSwapPatternParser extends RichParser<Pattern> {

private static final List<String> SUGGESTIONS = List.of("oak", "spruce", "stone", "sandstone");

/**
* Create a new rich parser with a defined prefix for the result, e.g. {@code #simplex}.
*
* @param worldEdit the worldedit instance.
*/
public TypeSwapPatternParser(WorldEdit worldEdit) {
super(worldEdit, "#typeswap", "#ts", "#swaptype");
}

@Override
public Stream<String> getSuggestions(String argumentInput, int index) {
if (index > 2) {
return Stream.empty();
}
return SUGGESTIONS.stream();
}

@Override
public Pattern parseFromInput(@Nonnull String[] input, ParserContext context) throws InputParseException {
if (input.length != 2) {
throw new InputParseException(Caption.of(
"fawe.error.command.syntax",
TextComponent.of(getPrefix() + "[input][output] (e.g. " + getPrefix() + "[spruce][oak])")
));
}
return new TypeSwapPattern(
context.requireExtent(),
input[0],
input[1],
Permission.hasPermission(context.requireActor(), "fawe.pattern.typeswap.regex")
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.math.BlockVector3;

import java.util.concurrent.atomic.AtomicInteger;

public class IdMask extends AbstractExtentMask implements ResettableMask {

private transient int id = -1;
private final AtomicInteger id = new AtomicInteger(-1);

public IdMask(Extent extent) {
super(extent);
}

@Override
public boolean test(Extent extent, BlockVector3 vector) {
if (id != -1) {
return extent.getBlock(vector).getInternalBlockTypeId() == id;
} else {
id = extent.getBlock(vector).getInternalBlockTypeId();
return true;
}
int blockID = extent.getBlock(vector).getInternalBlockTypeId();
int testId = id.compareAndExchange(-1, blockID);
return blockID == testId || testId == -1;
}

@Override
Expand All @@ -30,12 +29,12 @@ public boolean test(BlockVector3 vector) {

@Override
public void reset() {
this.id = -1;
this.id.set(-1);
}

@Override
public Mask copy() {
return new IdMask(getExtent());
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.fastasyncworldedit.core.function.pattern;

import com.fastasyncworldedit.core.extent.filter.block.FilterBlock;
import com.fastasyncworldedit.core.util.StringMan;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;

import java.util.regex.Pattern;

/**
* Pattern that replaces blocks based on their ID, matching for an "input" and replacing with an "output" string. The "input"
* string may be regex. Keeps as much of the block state as possible, excluding NBT data.
*
* @since TODO
*/
public class TypeSwapPattern extends AbstractExtentPattern {

private static final Pattern SPLITTER = Pattern.compile("[|,]");

private final String inputString;
private final String outputString;
private final String[] inputs;
private Pattern inputPattern = null;

/**
* Create a new instance
*
* @param extent extent to use
* @param inputString string to replace. May be regex.
* @param outputString string to replace with
* @param allowRegex if regex should be allowed for input string matching
* @since TODO
*/
public TypeSwapPattern(Extent extent, String inputString, String outputString, boolean allowRegex) {
super(extent);
this.inputString = inputString;
this.outputString = outputString;
if (!StringMan.isAlphanumericUnd(inputString)) {
if (allowRegex) {
this.inputPattern = Pattern.compile(inputString.replace(",", "|"));
inputs = null;
} else {
inputs = SPLITTER.split(inputString);
}
} else {
inputs = null;
}
}

@Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
BlockState existing = get.getBlock(extent);
BlockState newBlock = getNewBlock(existing);
if (newBlock == null) {
return false;
}
return set.setBlock(extent, newBlock);
}

@Override
public void applyBlock(final FilterBlock block) {
BlockState existing = block.getBlock();
BlockState newState = getNewBlock(existing);
if (newState != null) {
block.setBlock(newState);
}
}

@Override
public BaseBlock applyBlock(final BlockVector3 position) {
BaseBlock existing = position.getFullBlock(getExtent());
BlockState newState = getNewBlock(existing.toBlockState());
return newState == null ? existing : newState.toBaseBlock();
}

private BlockState getNewBlock(BlockState existing) {
String oldId = existing.getBlockType().getId();
String newId = oldId;
if (inputPattern != null) {
newId = inputPattern.matcher(oldId).replaceAll(outputString);
} else if (inputs != null && inputs.length > 0) {
for (String input : inputs) {
newId = newId.replace(input, outputString);
}
} else {
newId = oldId.replace(inputString, outputString);
}
if (newId.equals(oldId)) {
return null;
}
BlockType newType = BlockTypes.get(newId);
if (newType == null) {
return null;
}
return newType.getDefaultState().withProperties(existing);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.DoubleTag;
import com.sk89q.jnbt.IntArrayTag;
import com.sk89q.jnbt.ListTag;
import com.sk89q.jnbt.LongTag;
import com.sk89q.jnbt.NBTUtils;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
Expand Down
Loading

0 comments on commit d50dea7

Please sign in to comment.