Skip to content

Commit

Permalink
Use WorldEdit disallowed blocks config in set command
Browse files Browse the repository at this point in the history
Instead of trying to parse patterns ourselves to figure out if invalid
blocks are used, let WE handle it by modifying the disallowed blocks
list. This ensures things like '/p set floor hand' only work for
allowed blocks.

Currently it seems some of the WE pattern parsers don't check against
the disallowed blocks list. E.g. BlockCategoryPatternParser doesn't,
so '/p set floor ##buttons' works even if all buttons are disallowed.
  • Loading branch information
traksag committed Jul 8, 2020
1 parent 6f27da6 commit 3e591b2
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions Core/src/main/java/com/plotsquared/core/command/Set.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@
import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.TabCompletions;
import com.plotsquared.core.util.WorldUtil;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.world.block.BlockCategory;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -82,42 +81,37 @@ public Set() {
String material =
StringMan.join(Arrays.copyOfRange(args, 1, args.length), ",").trim();

final List<String> forbiddenTypes = new ArrayList<>(Settings.General.INVALID_BLOCKS);
final java.util.Set<String> forbiddenTypes = new HashSet<>();

for (String invalidType : Settings.General.INVALID_BLOCKS) {
if (!invalidType.contains(":")) {
// prefix with "minecraft:", so WorldEdit pattern
// parsers correctly match against disallowed blocks set
invalidType = "minecraft:" + invalidType;
}
forbiddenTypes.add(invalidType);
}

if (Settings.Enabled_Components.CHUNK_PROCESSOR) {
forbiddenTypes.addAll(WorldUtil.IMP.getTileEntityTypes().stream().map(
BlockType::getName).collect(Collectors.toList()));
BlockType::getId).collect(Collectors.toList()));
}

LocalConfiguration worldEditConfig = WorldEdit.getInstance().getConfiguration();
java.util.Set<String> worldEditDisallowedBlocks = worldEditConfig.disallowedBlocks;

if (!Permissions.hasPermission(player, Captions.PERMISSION_ADMIN_ALLOW_UNSAFE) &&
!forbiddenTypes.isEmpty()) {
for (String forbiddenType : forbiddenTypes) {
forbiddenType = forbiddenType.toLowerCase(Locale.ENGLISH);
if (forbiddenType.startsWith("minecraft:")) {
forbiddenType = forbiddenType.substring(10);
}
for (String blockType : material.split(",")) {
blockType = blockType.toLowerCase(Locale.ENGLISH);
if (blockType.startsWith("minecraft:")) {
blockType = blockType.substring(10);
}
// Swap out disallowed blocks for ours, so the WorldEdit
// pattern parsers know which blocks to disallow
worldEditConfig.disallowedBlocks = forbiddenTypes;
}

if (blockType.startsWith("##")) {
try {
final BlockCategory category = BlockCategory.REGISTRY.get(blockType.substring(2)
.replaceAll("[*^|]+", "").toLowerCase(Locale.ENGLISH));
if (category == null || !category.contains(BlockTypes.get(forbiddenType))) {
continue;
}
} catch (final Throwable ignored) {
}
} else if (!blockType.equals(forbiddenType)) {
continue;
}
Captions.COMPONENT_ILLEGAL_BLOCK.send(player, forbiddenType);
return true;
}
}
Pattern pattern;
try {
pattern = PatternUtil.parse(player, material, false);
} finally {
worldEditConfig.disallowedBlocks = worldEditDisallowedBlocks;
}

for (String component : components) {
Expand All @@ -135,8 +129,6 @@ public Set() {
return true;
}

Pattern pattern = PatternUtil.parse(player, material, false);

if (plot.getRunning() > 0) {
MainUtil.sendMessage(player, Captions.WAIT_FOR_TIMER);
return false;
Expand Down

0 comments on commit 3e591b2

Please sign in to comment.