Skip to content

Commit

Permalink
Merge pull request #14 from TheNextLvl-net/mask
Browse files Browse the repository at this point in the history
add mask mode setting
  • Loading branch information
NonSwag authored Jun 20, 2024
2 parents 925bd76 + 2bcdb5b commit 44fb873
Show file tree
Hide file tree
Showing 23 changed files with 251 additions and 180 deletions.
33 changes: 19 additions & 14 deletions api/src/main/java/net/thenextlvl/gopaint/api/brush/Brush.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,20 @@ public abstract class Brush {
* @param player The player who is performing the paint action.
* @param brushSettings The brush settings to be applied while painting.
* @see #performEdit(Player, Consumer)
* @see #setBlock(EditSession, Block, Material)
* @see #setBlock(EditSession, BlockVector3, Material)
*/
public abstract void paint(Location location, Player player, BrushSettings brushSettings);

/**
* Sets the material of a block in an EditSession.
*
* @param session The EditSession to perform the block update in.
* @param block The block to update.
* @param vector3 The block to update.
* @param material The material to set the block to.
* @throws MaxChangedBlocksException If the maximum number of changed blocks is exceeded.
*/
protected void setBlock(EditSession session, Block block, Material material) throws MaxChangedBlocksException {
BlockVector3 vector = BlockVector3.at(block.getX(), block.getY(), block.getZ());
if (session.getMask() == null || session.getMask().test(vector)) {
session.setBlock(vector, BukkitAdapter.asBlockType(material));
}
public void setBlock(EditSession session, BlockVector3 vector3, Material material) throws MaxChangedBlocksException {
session.setBlock(vector3, BukkitAdapter.asBlockType(material));
}

/**
Expand All @@ -70,7 +67,7 @@ protected void setBlock(EditSession session, Block block, Material material) thr
* @param player The player performing the edit.
* @param edit A Consumer functional interface that defines the actions to be taken within the edit session.
*/
protected void performEdit(Player player, Consumer<EditSession> edit) {
public void performEdit(Player player, Consumer<EditSession> edit) {
BukkitPlayer wrapped = BukkitAdapter.adapt(player);
LocalSession localSession = WorldEdit.getInstance().getSessionManager().get(wrapped);
try (EditSession editsession = localSession.createEditSession(wrapped)) {
Expand All @@ -87,11 +84,12 @@ protected void performEdit(Player player, Consumer<EditSession> edit) {
*
* @param brushSettings The brush settings to be checked against.
* @param player The player using the brush.
* @param session The EditSession object used for performing the checks.
* @param block The block being checked.
* @return true if the block passes all the default checks, false otherwise.
* @return true if the block passes the default checks, false otherwise.
*/
protected boolean passesDefaultChecks(BrushSettings brushSettings, Player player, Block block) {
return passesMaskCheck(brushSettings, block) && passesSurfaceCheck(brushSettings, player, block);
public boolean passesDefaultChecks(BrushSettings brushSettings, Player player, EditSession session, Block block) {
return passesMaskCheck(brushSettings, session, block) && passesSurfaceCheck(brushSettings, player, block);
}

/**
Expand All @@ -102,18 +100,25 @@ protected boolean passesDefaultChecks(BrushSettings brushSettings, Player player
* @param block The block being checked.
* @return true if the block passes the surface check, false otherwise.
*/
protected boolean passesSurfaceCheck(BrushSettings brushSettings, Player player, Block block) {
public boolean passesSurfaceCheck(BrushSettings brushSettings, Player player, Block block) {
return Surface.isOnSurface(block, brushSettings.getSurfaceMode(), player.getLocation());
}

/**
* Checks if a given block passes the mask check defined by the brush settings.
*
* @param brushSettings The brush settings to be checked against.
* @param session The EditSession object used for performing the mask check.
* @param block The block being checked.
* @return true if the block passes the mask check, false otherwise.
*/
protected boolean passesMaskCheck(BrushSettings brushSettings, Block block) {
return !brushSettings.isMaskEnabled() || block.getType().equals(brushSettings.getMask());
public boolean passesMaskCheck(BrushSettings brushSettings, EditSession session, Block block) {
return switch (brushSettings.getMaskMode()) {
case INTERFACE -> block.getType().equals(brushSettings.getMask());
case WORLDEDIT -> session.getMask() == null || session.getMask().test(
BlockVector3.at(block.getX(), block.getY(), block.getZ())
);
case DISABLED -> true;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package net.thenextlvl.gopaint.api.brush.setting;

import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.model.MaskMode;
import net.thenextlvl.gopaint.api.model.SurfaceMode;
import org.bukkit.Axis;
import org.bukkit.Material;
import org.jetbrains.annotations.ApiStatus;

import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -40,18 +40,15 @@ public interface BrushSettings {
* Retrieves the mask material used by the brush settings.
*
* @return The mask material.
* @deprecated the mask-material is going to be replaced with a WorldEdit Mask
*/
@Deprecated(since = "1.1.0-SNAPSHOT")
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
Material getMask();

/**
* Checks if the mask is enabled.
* Retrieves the mask mode used by the brush settings.
*
* @return true if the mask is enabled, false otherwise.
* @return The mask mode used by the brush settings.
*/
boolean isMaskEnabled();
MaskMode getMaskMode();

/**
* Returns the surface mode used by the brush settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.bukkit.Material;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
Expand Down Expand Up @@ -49,6 +48,11 @@ public interface PlayerBrushSettings extends BrushSettings, InventoryHolder {
*/
void cycleBrushForward();

/**
* Cycle the mask mode of the brush.
*/
void cycleMaskMode();

/**
* Cycle the surface mode of the brush.
*/
Expand Down Expand Up @@ -216,10 +220,7 @@ public interface PlayerBrushSettings extends BrushSettings, InventoryHolder {
* The mask determines whether the brush applies its effect only to blocks matching the material specified
*
* @param type The Material type to set as the mask.
* @deprecated the mask-material is going to be replaced with a WorldEdit Mask
*/
@Deprecated(since = "1.1.1")
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
void setMask(Material type);

/**
Expand All @@ -231,16 +232,6 @@ public interface PlayerBrushSettings extends BrushSettings, InventoryHolder {
*/
void setSize(int size);

/**
* Toggles the mask state of the brush.
* <p>
* The mask state determines whether the brush applies its effect only to blocks
* matching the material specified by {@link PlayerBrushSettings#setMask(Material)}.
* If the mask is enabled, the brush will only affect blocks of the specified material.
* If the mask is disabled, the brush will affect blocks of any material.
*/
void toggleMask();

/**
* Updates the inventory of the player brush settings.
* <p>
Expand Down
40 changes: 40 additions & 0 deletions api/src/main/java/net/thenextlvl/gopaint/api/model/MaskMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.thenextlvl.gopaint.api.model;

import com.sk89q.worldedit.EditSession;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.brush.setting.BrushSettings;
import org.bukkit.block.Block;

import java.util.Arrays;
import java.util.Optional;

@Getter
@RequiredArgsConstructor
public enum MaskMode {
/**
* This enumeration represents that no mask should be applied.
*/
DISABLED("Disabled"),
/**
* This enumeration represents that the mask material defined in the interface should be applied
*
* @see Brush#passesMaskCheck(BrushSettings, EditSession, Block)
*/
INTERFACE("Interface"),
/**
* This enumeration represents the complex mask defined by WorldEdit
*
* @see Brush#passesMaskCheck(BrushSettings, EditSession, Block)
*/
WORLDEDIT("WorldEdit");

private final String name;

public static Optional<MaskMode> byName(String name) {
return Arrays.stream(values())
.filter(maskMode -> maskMode.getName().equals(name))
.findAny();
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/thenextlvl/gopaint/GoPaintPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.thenextlvl.gopaint.api.brush.BrushController;
import net.thenextlvl.gopaint.api.brush.BrushRegistry;
import net.thenextlvl.gopaint.api.model.MaskMode;
import net.thenextlvl.gopaint.brush.CraftBrushController;
import net.thenextlvl.gopaint.brush.CraftBrushRegistry;
import net.thenextlvl.gopaint.command.GoPaintCommand;
Expand Down Expand Up @@ -51,7 +52,7 @@ public class GoPaintPlugin extends JavaPlugin implements Listener {
)).build());

private final FileIO<PluginConfig> configFile = new GsonFile<>(IO.of(getDataFolder(), "config.json"), new PluginConfig(
new PluginConfig.Generic(Material.FEATHER, 100, 10, 50, Axis.Y, 50, 50, new ArrayList<>(), true, true, Material.SPONGE, SurfaceMode.DIRECT),
new PluginConfig.Generic(Material.FEATHER, 100, 10, 50, Axis.Y, 50, 50, new ArrayList<>(), true, Material.SPONGE, MaskMode.INTERFACE, SurfaceMode.DIRECT),
new PluginConfig.Thickness(1, 5),
new PluginConfig.Angle(2, 5, 10, 40, 85),
new PluginConfig.Fracture(2, 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import lombok.Getter;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.brush.setting.ItemBrushSettings;
import net.thenextlvl.gopaint.api.model.MaskMode;
import net.thenextlvl.gopaint.api.model.SurfaceMode;
import org.bukkit.Axis;
import org.bukkit.Material;
Expand All @@ -38,6 +39,7 @@ public final class CraftItemBrushSettings implements ItemBrushSettings {
private final List<Material> blocks;
private final Axis axis;
private final SurfaceMode surfaceMode;
private final MaskMode maskMode;
private final int size;
private final int chance;
private final int thickness;
Expand All @@ -49,11 +51,6 @@ public final class CraftItemBrushSettings implements ItemBrushSettings {

private static final Random random = new Random();

@Override
public boolean isMaskEnabled() {
return getMask() != null;
}

@Override
public Material getRandomBlock() {
return getBlocks().get(getRandom().nextInt(getBlocks().size()));
Expand Down Expand Up @@ -95,6 +92,8 @@ public static ItemBrushSettings parse(Brush brush, ItemMeta itemMeta) {
.toList());
} else if (line.startsWith("Mask: ")) {
builder.mask(Material.matchMaterial(line.substring(6)));
} else if (line.startsWith("Mask Mode: ")) {
MaskMode.byName(line.substring(11)).ifPresent(builder::maskMode);
} else if (line.startsWith("Surface Mode: ")) {
SurfaceMode.byName(line.substring(14)).ifPresent(builder::surfaceMode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.thenextlvl.gopaint.GoPaintPlugin;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.brush.setting.PlayerBrushSettings;
import net.thenextlvl.gopaint.api.model.MaskMode;
import net.thenextlvl.gopaint.api.model.SurfaceMode;
import net.thenextlvl.gopaint.brush.standard.*;
import net.thenextlvl.gopaint.util.GUI;
Expand All @@ -50,7 +51,6 @@ public final class CraftPlayerBrushSettings implements PlayerBrushSettings {

private final GoPaintPlugin plugin;

private boolean maskEnabled;
private boolean enabled;
private int size;
private int chance;
Expand All @@ -61,6 +61,7 @@ public final class CraftPlayerBrushSettings implements PlayerBrushSettings {
private int mixingStrength;
private double angleHeightDifference;
private Axis axis;
private MaskMode maskMode;
private SurfaceMode surfaceMode;

private @Setter Brush brush;
Expand All @@ -73,7 +74,7 @@ public CraftPlayerBrushSettings(GoPaintPlugin plugin) {
this.plugin = plugin;

surfaceMode = plugin.config().generic().surfaceMode();
maskEnabled = plugin.config().generic().maskEnabled();
maskMode = plugin.config().generic().maskMode();
enabled = plugin.config().generic().enabledByDefault();
chance = plugin.config().generic().defaultChance();
thickness = plugin.config().thickness().defaultThickness();
Expand Down Expand Up @@ -261,8 +262,12 @@ public void decreaseAngleHeightDifference(int amount) {
}

@Override
public void toggleMask() {
maskEnabled = !maskEnabled;
public void cycleMaskMode() {
maskMode = switch (maskMode) {
case INTERFACE -> MaskMode.WORLDEDIT;
case WORLDEDIT -> MaskMode.DISABLED;
case DISABLED -> MaskMode.INTERFACE;
};
updateInventory();
}

Expand Down Expand Up @@ -331,11 +336,15 @@ public void export(ItemStack itemStack) {
.map(NamespacedKey::asMinimalString)
.collect(Collectors.joining(", "))));

if (isMaskEnabled()) {
if (!getMaskMode().equals(MaskMode.DISABLED)) {
lore.add("Mask Mode: " + getMaskMode().getName());
}
if (getMaskMode().equals(MaskMode.INTERFACE)) {
lore.add("Mask: " + getMask().getKey().asMinimalString());
}

if (!getSurfaceMode().equals(SurfaceMode.DISABLED)) {
lore.add("Surface Mode: " + surfaceMode.getName());
lore.add("Surface Mode: " + getSurfaceMode().getName());
}

itemStack.editMeta(itemMeta -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package net.thenextlvl.gopaint.brush.standard;

import com.sk89q.worldedit.math.BlockVector3;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.brush.setting.BrushSettings;
import net.thenextlvl.gopaint.api.math.Height;
Expand All @@ -42,11 +43,12 @@ public AngleBrush() {
public void paint(Location location, Player player, BrushSettings brushSettings) {
performEdit(player, session -> {
Stream<Block> blocks = Sphere.getBlocksInRadius(location, brushSettings.getSize(), null, false);
blocks.filter(block -> passesDefaultChecks(brushSettings, player, block))
blocks.filter(block -> passesDefaultChecks(brushSettings, player, session, block))
.filter(block -> Height.getAverageHeightDiffAngle(block.getLocation(), 1) >= 0.1
&& Height.getAverageHeightDiffAngle(block.getLocation(), brushSettings.getAngleDistance())
>= Math.tan(Math.toRadians(brushSettings.getAngleHeightDifference())))
.forEach(block -> setBlock(session, block, brushSettings.getRandomBlock()));
.map(block -> BlockVector3.at(block.getX(), block.getY(), block.getZ()))
.forEach(vector3 -> setBlock(session, vector3, brushSettings.getRandomBlock()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package net.thenextlvl.gopaint.brush.standard;

import com.sk89q.worldedit.math.BlockVector3;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.brush.setting.BrushSettings;
import net.thenextlvl.gopaint.api.math.ConnectedBlocks;
Expand All @@ -44,8 +45,9 @@ public void paint(Location location, Player player, BrushSettings brushSettings)
performEdit(player, session -> {
List<Block> blocks = Sphere.getBlocksInRadius(location, brushSettings.getSize(), null, false).toList();
Stream<Block> connectedBlocks = ConnectedBlocks.getConnectedBlocks(location, blocks);
connectedBlocks.filter(block -> passesDefaultChecks(brushSettings, player, block))
.forEach(block -> setBlock(session, block, brushSettings.getRandomBlock()));
connectedBlocks.filter(block -> passesDefaultChecks(brushSettings, player, session, block))
.map(block -> BlockVector3.at(block.getX(), block.getY(), block.getZ()))
.forEach(vector3 -> setBlock(session, vector3, brushSettings.getRandomBlock()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package net.thenextlvl.gopaint.brush.standard;

import com.sk89q.worldedit.math.BlockVector3;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.brush.setting.BrushSettings;
import net.thenextlvl.gopaint.api.math.Sphere;
Expand All @@ -41,8 +42,9 @@ public DiscBrush() {
public void paint(Location location, Player player, BrushSettings brushSettings) {
performEdit(player, session -> {
Stream<Block> blocks = Sphere.getBlocksInRadius(location, brushSettings.getSize(), brushSettings.getAxis(), false);
blocks.filter(block -> passesDefaultChecks(brushSettings, player, block))
.forEach(block -> setBlock(session, block, brushSettings.getRandomBlock()));
blocks.filter(block -> passesDefaultChecks(brushSettings, player, session, block))
.map(block -> BlockVector3.at(block.getX(), block.getY(), block.getZ()))
.forEach(vector3 -> setBlock(session, vector3, brushSettings.getRandomBlock()));
});
}
}
Loading

0 comments on commit 44fb873

Please sign in to comment.