Some flag fixes

Use ItemType for the flags, since they accept items, not blocks.
Fixes #2571
This commit is contained in:
Jesse Boyd 2019-11-12 20:38:18 +00:00
parent 7e21107120
commit 765a021ecd
No known key found for this signature in database
GPG key ID: 59F1DE6293AF6E1F
11 changed files with 133 additions and 77 deletions

View file

@ -31,6 +31,7 @@ import com.github.intellectualsites.plotsquared.plot.util.UpdateUtility;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.item.ItemType;
import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -1099,10 +1100,10 @@ import java.util.regex.Pattern;
return;
}
if (!plot.isAdded(plotPlayer.getUUID())) {
Optional<Set<BlockType>> destroy = plot.getFlag(Flags.BREAK);
Optional<Set<ItemType>> destroy = plot.getFlag(Flags.BREAK);
Block block = event.getBlock();
if (destroy.isPresent() && destroy.get()
.contains(BukkitAdapter.asBlockType(block.getType()))) {
.contains(BukkitAdapter.asItemType(block.getType()))) {
return;
}
if (Permissions
@ -1384,10 +1385,10 @@ import java.util.regex.Pattern;
}
PlotPlayer plotPlayer = BukkitUtil.getPlayer(player);
if (!plot.isAdded(plotPlayer.getUUID())) {
Optional<Set<BlockType>> destroy = plot.getFlag(Flags.BREAK);
Optional<Set<ItemType>> destroy = plot.getFlag(Flags.BREAK);
Block block = event.getBlock();
if (destroy.isPresent() && destroy.get()
.contains(BukkitBlockUtil.get(block)) || Permissions
.contains(BukkitAdapter.asItemType(block.getType())) || Permissions
.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_DESTROY_OTHER)) {
return;
}
@ -1914,8 +1915,8 @@ import java.util.regex.Pattern;
Location location = BukkitUtil.getLocation(block.getLocation());
Material finalType = type;
if (!EventUtil.manager
.checkPlayerBlockEvent(pp, PlayerBlockEventType.SPAWN_MOB, location, () -> BukkitAdapter.asBlockType(
finalType).getDefaultState(), true)) {
.checkPlayerBlockEvent(pp, PlayerBlockEventType.SPAWN_MOB, location, () -> BukkitAdapter.asItemType(
finalType), true)) {
event.setCancelled(true);
event.setUseItemInHand(Event.Result.DENY);
}
@ -1933,14 +1934,14 @@ import java.util.regex.Pattern;
return;
}
PlayerBlockEventType eventType = null;
Supplier<BlockState> lb;
Supplier<ItemType> lazyItem;
Location location;
Action action = event.getAction();
switch (action) {
case PHYSICAL: {
eventType = PlayerBlockEventType.TRIGGER_PHYSICAL;
Block block = event.getClickedBlock();
lb = BukkitBlockUtil.supply(block);
lazyItem = BukkitBlockUtil.supplyItem(block);
location = BukkitUtil.getLocation(block.getLocation());
break;
}
@ -2109,7 +2110,7 @@ import java.util.regex.Pattern;
eventType = PlayerBlockEventType.INTERACT_BLOCK;
}
}
lb = BukkitBlockUtil.supply(block);
lazyItem = BukkitBlockUtil.supplyItem(block);
if (eventType != null && (eventType != PlayerBlockEventType.INTERACT_BLOCK
|| !player.isSneaking())) {
break;
@ -2128,7 +2129,7 @@ import java.util.regex.Pattern;
type = offType;
}
// in the following, lb needs to have the material of the item in hand i.e. type
lb = BukkitBlockUtil.supply(type);
lazyItem = BukkitBlockUtil.supplyItem(type);
if (type.isBlock()) {
location = BukkitUtil
.getLocation(block.getRelative(event.getBlockFace()).getLocation());
@ -2211,7 +2212,7 @@ import java.util.regex.Pattern;
Block block = event.getClickedBlock();
location = BukkitUtil.getLocation(block.getLocation());
eventType = PlayerBlockEventType.BREAK_BLOCK;
lb = BukkitBlockUtil.supply(block);
lazyItem = BukkitBlockUtil.supplyItem(block);
break;
default:
return;
@ -2222,7 +2223,7 @@ import java.util.regex.Pattern;
return;
}
}
if (!EventUtil.manager.checkPlayerBlockEvent(pp, eventType, location, lb, true)) {
if (!EventUtil.manager.checkPlayerBlockEvent(pp, eventType, location, lazyItem, true)) {
event.setCancelled(true);
}
}
@ -2470,7 +2471,7 @@ import java.util.regex.Pattern;
Captions.PERMISSION_ADMIN_BUILD_UNOWNED);
event.setCancelled(true);
} else if (!plot.isAdded(pp.getUUID())) {
if (Flags.USE.contains(plot, BukkitBlockUtil.get(block))) {
if (Flags.USE.contains(plot, BukkitAdapter.asItemType(block.getType()))) {
return;
}
if (Permissions.hasPermission(pp, Captions.PERMISSION_ADMIN_BUILD_OTHER)) {
@ -2530,9 +2531,9 @@ import java.util.regex.Pattern;
Captions.PERMISSION_ADMIN_BUILD_UNOWNED);
event.setCancelled(true);
} else if (!plot.isAdded(plotPlayer.getUUID())) {
Optional<Set<BlockType>> use = plot.getFlag(Flags.USE);
Optional<Set<ItemType>> use = plot.getFlag(Flags.USE);
Block block = event.getBlockClicked();
if (use.isPresent() && use.get().contains(BukkitBlockUtil.get(block))) {
if (use.isPresent() && use.get().contains(BukkitAdapter.asItemType(block.getType()))) {
return;
}
if (Permissions.hasPermission(plotPlayer, Captions.PERMISSION_ADMIN_BUILD_OTHER)) {
@ -3088,10 +3089,10 @@ import java.util.regex.Pattern;
return;
}
} else if (!plot.isAdded(pp.getUUID())) {
Set<BlockType> place = plot.getFlag(Flags.PLACE, null);
Set<ItemType> place = plot.getFlag(Flags.PLACE, null);
if (place != null) {
Block block = event.getBlock();
if (place.contains(BukkitBlockUtil.get(block))) {
if (place.contains(BukkitAdapter.asItemType(block.getType()))) {
return;
}
}

View file

@ -2,18 +2,19 @@ package com.github.intellectualsites.plotsquared.bukkit.object;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.item.ItemType;
import org.bukkit.Material;
import org.bukkit.block.Block;
import java.util.function.Supplier;
public class BukkitBlockUtil {
public static Supplier<BlockState> supply(Block block) {
return () -> BukkitAdapter.asBlockType(block.getType()).getDefaultState();
public static Supplier<ItemType> supplyItem(Block block) {
return () -> BukkitAdapter.asItemType(block.getType());
}
public static Supplier<BlockState> supply(Material type) {
return () -> BukkitAdapter.asBlockType(type).getDefaultState();
public static Supplier<ItemType> supplyItem(Material type) {
return () -> BukkitAdapter.asItemType(type);
}
public static BlockState get(Block block) {

View file

@ -14,6 +14,7 @@ import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import io.papermc.lib.PaperLib;
import org.bukkit.GameMode;
import org.bukkit.Sound;
@ -283,7 +284,7 @@ public class BukkitPlayer extends PlotPlayer {
}
@Override public void playMusic(@NotNull final Location location, @NotNull final ItemType id) {
if (id.getBlockType().getMaterial().isAir()) {
if (id == ItemTypes.AIR) {
// Let's just stop all the discs because why not?
for (final Sound sound : Arrays.stream(Sound.values())
.filter(sound -> sound.name().contains("DISC")).collect(Collectors.toList())) {

View file

@ -44,7 +44,7 @@ import java.util.Locale;
plot.removeFlag(Flags.MUSIC);
Captions.FLAG_REMOVED.send(player);
} else if (item.name.toLowerCase(Locale.ENGLISH).contains("disc")) {
plot.setFlag(Flags.MUSIC, item);
plot.setFlag(Flags.MUSIC, item.getType().getId());
Captions.FLAG_ADDED.send(player);
} else {
Captions.FLAG_NOT_ADDED.send(player);

View file

@ -58,9 +58,9 @@ public final class Flags {
public static final BooleanFlag BLOCK_IGNITION = new BooleanFlag("block-ignition");
public static final BooleanFlag SOIL_DRY = new BooleanFlag("soil-dry");
public static final StringListFlag BLOCKED_CMDS = new StringListFlag("blocked-cmds");
public static final BlockStateListFlag USE = new BlockStateListFlag("use");
public static final BlockStateListFlag BREAK = new BlockStateListFlag("break");
public static final BlockStateListFlag PLACE = new BlockStateListFlag("place");
public static final ItemTypeListFlag USE = new ItemTypeListFlag("use");
public static final ItemTypeListFlag BREAK = new ItemTypeListFlag("break");
public static final ItemTypeListFlag PLACE = new ItemTypeListFlag("place");
public static final BooleanFlag DEVICE_INTERACT = new BooleanFlag("device-interact");
public static final BooleanFlag VEHICLE_BREAK = new BooleanFlag("vehicle-break");
public static final BooleanFlag VEHICLE_PLACE = new BooleanFlag("vehicle-place");

View file

@ -0,0 +1,39 @@
package com.github.intellectualsites.plotsquared.plot.flag;
import com.github.intellectualsites.plotsquared.plot.config.Captions;
import com.github.intellectualsites.plotsquared.plot.object.Plot;
import com.github.intellectualsites.plotsquared.plot.util.StringMan;
import com.github.intellectualsites.plotsquared.plot.util.world.BlockUtil;
import com.github.intellectualsites.plotsquared.plot.util.world.ItemUtil;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class ItemTypeListFlag extends ListFlag<Set<ItemType>> {
public ItemTypeListFlag(String name) {
super(Captions.FLAG_CATEGORY_BLOCK_LIST, name);
}
@Override public String valueToString(Object value) {
return StringMan.join((Set<BlockType>) value, ",");
}
@Override public Set<ItemType> parseValue(final String value) {
return Arrays.stream(ItemUtil.parse(value)).filter(Objects::nonNull).collect(Collectors.toSet());
}
@Override public String getValueDescription() {
return Captions.FLAG_ERROR_PLOTBLOCKLIST.getTranslated();
}
public boolean contains(Plot plot, BlockState value) {
return contains(plot, value.getBlockType().getItemType());
}
}

View file

@ -152,12 +152,12 @@ public class PlotListener {
Location location = player.getLocation();
Location lastLocation = player.getMeta("music");
if (lastLocation != null) {
player.playMusic(lastLocation, ItemTypes.AIR);
player.playMusic(lastLocation, item);
if (item == ItemTypes.AIR) {
player.deleteMeta("music");
}
}
if (!(item == ItemTypes.AIR)) {
if (item != ItemTypes.AIR) {
try {
player.setMeta("music", location);
player.playMusic(location, item);

View file

@ -17,6 +17,8 @@ import com.github.intellectualsites.plotsquared.plot.util.expiry.ExpireManager;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@ -103,7 +105,7 @@ public abstract class EventUtil {
}
public boolean checkPlayerBlockEvent(PlotPlayer player, PlayerBlockEventType type,
Location location, Supplier<BlockState> block, boolean notifyPerms) {
Location location, Supplier<ItemType> item, boolean notifyPerms) {
PlotArea area = location.getPlotArea();
assert area != null;
Plot plot = area.getPlot(location);
@ -129,19 +131,19 @@ public abstract class EventUtil {
Captions.PERMISSION_ADMIN_INTERACT_UNOWNED.getTranslated(),
notifyPerms);
}
Optional<Set<BlockType>> use = plot.getFlag(Flags.USE);
Optional<Set<ItemType>> use = plot.getFlag(Flags.USE);
if (use.isPresent()) {
Set<BlockType> value = use.get();
if (value.contains(BlockTypes.AIR) || value
.contains(block.get())) {
Set<ItemType> value = use.get();
if (value.contains(ItemTypes.AIR) || value
.contains(item.get())) {
return true;
}
}
Optional<Set<BlockType>> destroy = plot.getFlag(Flags.BREAK);
Optional<Set<ItemType>> destroy = plot.getFlag(Flags.BREAK);
if (destroy.isPresent()) {
Set<BlockType> value = destroy.get();
if (value.contains(BlockTypes.AIR) || value
.contains(block.get())) {
Set<ItemType> value = destroy.get();
if (value.contains(ItemTypes.AIR) || value
.contains(item.get())) {
return true;
}
}
@ -223,10 +225,10 @@ public abstract class EventUtil {
Captions.PERMISSION_ADMIN_INTERACT_UNOWNED.getTranslated(),
notifyPerms);
}
Optional<Set<BlockType>> flagValue = plot.getFlag(Flags.USE);
Set<BlockType> value = flagValue.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flagValue = plot.getFlag(Flags.USE);
Set<ItemType> value = flagValue.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
return Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(), false)
|| !(!notifyPerms || MainUtil
@ -246,10 +248,10 @@ public abstract class EventUtil {
Captions.PERMISSION_ADMIN_BUILD_UNOWNED.getTranslated(),
notifyPerms);
}
Optional<Set<BlockType>> flagValue = plot.getFlag(Flags.PLACE);
Set<BlockType> value = flagValue.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flagValue = plot.getFlag(Flags.PLACE);
Set<ItemType> value = flagValue.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_BUILD_OTHER.getTranslated(), false)) {
return true;
@ -273,10 +275,10 @@ public abstract class EventUtil {
if (plot.getFlag(Flags.DEVICE_INTERACT).orElse(false)) {
return true;
}
Optional<Set<BlockType>> flagValue = plot.getFlag(Flags.USE);
Set<BlockType> value = flagValue.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flagValue = plot.getFlag(Flags.USE);
Set<ItemType> value = flagValue.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(),
false)) {
@ -300,10 +302,10 @@ public abstract class EventUtil {
if (plot.getFlag(Flags.HOSTILE_INTERACT).orElse(false)) {
return true;
}
Optional<Set<BlockType>> flagValue = plot.getFlag(Flags.USE);
Set<BlockType> value = flagValue.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flagValue = plot.getFlag(Flags.USE);
Set<ItemType> value = flagValue.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(),
false)) {
@ -329,10 +331,10 @@ public abstract class EventUtil {
if (plot.getFlag(Flags.MISC_INTERACT).orElse(false)) {
return true;
}
Optional<Set<BlockType>> flag = plot.getFlag(Flags.USE);
Set<BlockType> value = flag.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flag = plot.getFlag(Flags.USE);
Set<ItemType> value = flag.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(),
false)) {
@ -359,10 +361,10 @@ public abstract class EventUtil {
if (plot.getFlag(Flags.VEHICLE_USE).orElse(false)) {
return true;
}
Optional<Set<BlockType>> flag = plot.getFlag(Flags.USE);
Set<BlockType> value = flag.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flag = plot.getFlag(Flags.USE);
Set<ItemType> value = flag.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(),
false)) {
@ -389,10 +391,10 @@ public abstract class EventUtil {
if (plot.getFlag(Flags.MOB_PLACE).orElse(false)) {
return true;
}
Optional<Set<BlockType>> flagValue = plot.getFlag(Flags.PLACE);
Set<BlockType> value = flagValue.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flagValue = plot.getFlag(Flags.PLACE);
Set<ItemType> value = flagValue.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(),
false)) {
@ -419,10 +421,10 @@ public abstract class EventUtil {
if (plot.getFlag(Flags.MISC_PLACE).orElse(false)) {
return true;
}
Optional<Set<BlockType>> flag = plot.getFlag(Flags.PLACE);
Set<BlockType> value = flag.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flag = plot.getFlag(Flags.PLACE);
Set<ItemType> value = flag.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(),
false)) {
@ -450,10 +452,10 @@ public abstract class EventUtil {
if (plot.getFlag(Flags.VEHICLE_PLACE).orElse(false)) {
return true;
}
Optional<Set<BlockType>> flag = plot.getFlag(Flags.PLACE);
Set<BlockType> value = flag.orElse(null);
if (value == null || !value.contains(BlockTypes.AIR) && !value
.contains(block.get())) {
Optional<Set<ItemType>> flag = plot.getFlag(Flags.PLACE);
Set<ItemType> value = flag.orElse(null);
if (value == null || !value.contains(ItemTypes.AIR) && !value
.contains(item.get())) {
if (Permissions.hasPermission(player,
Captions.PERMISSION_ADMIN_INTERACT_OTHER.getTranslated(),
false)) {

View file

@ -42,7 +42,7 @@ public final class BlockUtil {
return FuzzyBlockState.builder().type(BlockTypes.AIR).build();
}
id = id.toLowerCase();
BlockType type = BlockType.REGISTRY.get(id);
BlockType type = BlockTypes.get(id);
if (type != null) {
return type.getDefaultState();
}

View file

@ -1,6 +1,7 @@
package com.github.intellectualsites.plotsquared.plot.util.world;
import com.github.intellectualsites.plotsquared.plot.util.MathMan;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.registry.LegacyMapper;
@ -27,4 +28,13 @@ public final class ItemUtil {
if (!input.split("\\[", 2)[0].contains(":")) input = "minecraft:" + input;
return ItemTypes.get(input);
}
public static final ItemType[] parse(String commaDelimited) {
String[] split = commaDelimited.split(",(?![^\\(\\[]*[\\]\\)])");
ItemType[] result = new ItemType[split.length];
for (int i = 0; i < split.length; i++) {
result[i] = get(split[i]);
}
return result;
}
}

View file

@ -10,6 +10,8 @@ import com.github.intellectualsites.plotsquared.plot.util.EventUtil;
import com.github.intellectualsites.plotsquared.plot.util.EventUtilTest;
import com.github.intellectualsites.plotsquared.plot.util.world.BlockUtil;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import org.junit.Before;
import org.junit.Test;
@ -22,7 +24,7 @@ import static org.junit.Assert.assertEquals;
public class FlagTest {
private Object testBlock;
private ItemType testBlock;
private Flag<? extends Collection<?>> use = Flags.USE;
@Before public void setUp() throws Exception {
@ -37,11 +39,11 @@ public class FlagTest {
Optional<? extends Collection> flag = plot.getFlag(use);
if (flag.isPresent()) {
System.out.println(Flags.USE.valueToString(flag.get()));
testBlock = BlockUtil.get((short) 1, (byte) 0);
testBlock = ItemTypes.BONE_BLOCK;
flag.get().add(testBlock);
}
flag.ifPresent(collection -> System.out.println(Flags.USE.valueToString(collection)));
Optional<Set<BlockType>> flag2 = plot.getFlag(Flags.USE);
Optional<Set<ItemType>> flag2 = plot.getFlag(Flags.USE);
if (flag2.isPresent()) {
// assertThat(flag2.get(), (Matcher<? super Set<BlockType>>) IsCollectionContaining.hasItem(testBlock));
}