Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add break-blocks and place-blocks flags #1778

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.base.Predicate;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
Expand All @@ -43,6 +44,7 @@
import com.sk89q.worldguard.config.WorldConfiguration;
import com.sk89q.worldguard.protection.association.RegionAssociable;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.flags.MapFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.StateFlag.State;
import com.sk89q.worldguard.protection.regions.RegionQuery;
Expand Down Expand Up @@ -150,6 +152,16 @@ private boolean isWhitelisted(Cause cause, World world, boolean pvp) {
}
}

private boolean testBuildType(RegionQuery query, Location target, RegionAssociable associable, MapFlag<BlockType, State> mapFlag, Material type, StateFlag fallback, StateFlag... flag) {
BlockType blockType;

if (mapFlag == null || type == null || (blockType = BukkitAdapter.asBlockType(type)) == null) {
return query.testBuild(BukkitAdapter.adapt(target), associable, flag);
}

return query.testBuild(BukkitAdapter.adapt(target), associable, mapFlag, blockType, fallback, flag);
}

@EventHandler(ignoreCancelled = true)
public void onPlaceBlock(final PlaceBlockEvent event) {
if (event.getResult() == Result.ALLOW) return; // Don't care about events that have been pre-allowed
Expand Down Expand Up @@ -181,16 +193,16 @@ public void onPlaceBlock(final PlaceBlockEvent event) {
flags.add(Flags.LIGHTER);
if (fire) flags.add(Flags.FIRE_SPREAD);
if (lava) flags.add(Flags.LAVA_FIRE);
canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, flags.toArray(new StateFlag[flags.size()])));
canPlace = testBuildType(query, target, associable, Flags.PLACE_BLOCKS, type, null, combine(event, flags.toArray(new StateFlag[flags.size()])));
what = "place fire";

} else if (type == Material.FROSTED_ICE) {
event.setSilent(true); // gets spammy
canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_PLACE, Flags.FROSTED_ICE_FORM));
canPlace = testBuildType(query, target, associable, Flags.PLACE_BLOCKS, type, null, combine(event, Flags.BLOCK_PLACE, Flags.FROSTED_ICE_FORM));
what = "use frostwalker"; // hidden anyway
/* Everything else */
} else {
canPlace = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_PLACE));
canPlace = testBuildType(query, target, associable, Flags.PLACE_BLOCKS, type, null, combine(event, Flags.BLOCK_PLACE));
what = "place that block";
}

Expand All @@ -209,6 +221,7 @@ public void onBreakBlock(final BreakBlockEvent event) {
if (!isRegionSupportEnabled(BukkitAdapter.adapt(event.getWorld()))) return; // Region support disabled
if (isWhitelisted(event.getCause(), event.getWorld(), false)) return; // Whitelisted cause

final Material type = event.getEffectiveMaterial();
final RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();

if (!event.isCancelled()) {
Expand All @@ -220,12 +233,12 @@ public void onBreakBlock(final BreakBlockEvent event) {

/* TNT */
if (event.getCause().find(EntityType.PRIMED_TNT, EntityType.MINECART_TNT) != null) {
canBreak = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_BREAK, Flags.TNT));
canBreak = testBuildType(query, target, associable, Flags.BREAK_BLOCKS, type, null, combine(event, Flags.BLOCK_BREAK, Flags.TNT));
what = "use dynamite";

/* Everything else */
} else {
canBreak = query.testBuild(BukkitAdapter.adapt(target), associable, combine(event, Flags.BLOCK_BREAK));
canBreak = testBuildType(query, target, associable, Flags.BREAK_BLOCKS, type, null, combine(event, Flags.BLOCK_BREAK));
what = "break that block";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import com.sk89q.worldedit.util.formatting.text.format.TextColor;
import com.sk89q.worldedit.util.formatting.text.format.TextDecoration;
import com.sk89q.worldedit.util.formatting.text.serializer.legacy.LegacyComponentSerializer;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.entity.EntityType;
import com.sk89q.worldedit.world.gamemode.GameMode;
import com.sk89q.worldedit.world.weather.WeatherType;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.flags.StateFlag.State;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;

Expand Down Expand Up @@ -55,6 +57,8 @@ public final class Flags {
// are still checked for DENY), so they are false by default
public static final StateFlag BLOCK_BREAK = register(new StateFlag("block-break", false));
public static final StateFlag BLOCK_PLACE = register(new StateFlag("block-place", false));
public static final MapFlag<BlockType, State> BREAK_BLOCKS = register(new MapFlag<>("break-blocks", new RegistryFlag<>(null, BlockType.REGISTRY), new StateFlag(null, false)));
public static final MapFlag<BlockType, State> PLACE_BLOCKS = register(new MapFlag<>("place-blocks", new RegistryFlag<>(null, BlockType.REGISTRY), new StateFlag(null, false)));
public static final StateFlag USE = register(new StateFlag("use", false));
public static final StateFlag INTERACT = register(new StateFlag("interact", false));
public static final StateFlag DAMAGE_ANIMALS = register(new StateFlag("damage-animals", false));
Expand Down