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

GUI Improvements #15

Merged
merged 9 commits into from
Jun 20, 2024
Merged
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
3 changes: 3 additions & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies {
compileOnlyApi(platform("com.intellectualsites.bom:bom-newest:1.45"))
compileOnlyApi("com.fastasyncworldedit:FastAsyncWorldEdit-Bukkit")

api("net.thenextlvl.core:i18n:1.0.18")
api("net.thenextlvl.core:paper:1.3.4")

annotationProcessor("org.projectlombok:lombok:1.18.32")
}

Expand Down
19 changes: 18 additions & 1 deletion api/src/main/java/net/thenextlvl/gopaint/api/brush/Brush.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitPlayer;
import com.sk89q.worldedit.math.BlockVector3;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.key.Keyed;
import net.thenextlvl.gopaint.api.brush.setting.BrushSettings;
import net.thenextlvl.gopaint.api.math.Surface;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

/**
* This interface represents a brush used for painting blocks in a world.
*/
@Getter
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public abstract class Brush {
public abstract class Brush implements Comparable<Brush>, Keyed {
/**
* Retrieves the name of the brush.
*/
Expand All @@ -36,6 +44,10 @@ public abstract class Brush {
* Retrieves the base64 head value.
*/
private final String headValue;
/**
* The key that identifies this brush
*/
private final @Accessors(fluent = true) Key key;

/**
* Performs a painting action using the provided location, player, and brush settings.
Expand Down Expand Up @@ -121,4 +133,9 @@ public boolean passesMaskCheck(BrushSettings brushSettings, EditSession session,
case DISABLED -> true;
};
}

@Override
public int compareTo(@NotNull Brush brush) {
return key().compareTo(brush.key());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import net.thenextlvl.gopaint.api.brush.setting.ItemBrushSettings;
import net.thenextlvl.gopaint.api.brush.setting.PlayerBrushSettings;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

Expand Down Expand Up @@ -38,16 +39,6 @@ public interface BrushController {
*/
ItemBrushSettings parseBrushSettings(Brush brush, ItemMeta itemMeta);

/**
* Retrieves the brush for the given name.
*
* @param name The name of the brush to look for.
* @return An optional containing the brush handler, or empty if not found.
*/
@Deprecated(forRemoval = true, since = "1.1.1")
@ApiStatus.ScheduledForRemoval(inVersion = "1.2.0")
Optional<Brush> getBrushHandler(String name);

/**
* Removes the brush settings for a specific player.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package net.thenextlvl.gopaint.api.brush;

import java.util.List;
import net.kyori.adventure.key.Key;

import java.util.Optional;
import java.util.stream.Stream;

/**
* The BrushRegistry interface represents a registry for brushes.
* It provides methods to access, register, and unregister brushes.
*/
public interface BrushRegistry {
/**
* Returns an immutable list of available brushes.
* Returns a stream of registered brushes.
*
* @return The list of available brushes.
* @return The stream of available brushes.
*/
List<Brush> getBrushes();
Stream<Brush> getBrushes();

/**
* Checks if a brush is registered in the BrushController.
Expand All @@ -37,4 +40,12 @@ public interface BrushRegistry {
* @throws IllegalStateException if the brush is not registered.
*/
void unregisterBrush(Brush brush) throws IllegalStateException;

/**
* Retrieves the brush associated with the provided NamespacedKey.
*
* @param key The NamespacedKey of the brush to retrieve.
* @return An Optional containing the brush if found, or an empty Optional if not found.
*/
Optional<Brush> getBrush(Key key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public interface BrushSettings {
*
* @return The size of the brush settings.
*/
int getSize();
int getBrushSize();

/**
* Returns the thickness used by the brush settings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package net.thenextlvl.gopaint.api.brush.setting;

import core.paper.gui.AbstractGUI;
import net.thenextlvl.gopaint.api.brush.Brush;
import org.bukkit.Material;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

/**
* The PlayerBrushSettings interface extends the BrushSettings interface and the InventoryHolder interface.
* It represents the configuration settings for a brush specifically used by a player.
*/
public interface PlayerBrushSettings extends BrushSettings, InventoryHolder {
public interface PlayerBrushSettings extends BrushSettings {
/**
* Checks whether the brush is enabled.
*
Expand Down Expand Up @@ -230,15 +230,21 @@ public interface PlayerBrushSettings extends BrushSettings, InventoryHolder {
*
* @param size The size of the brush. Must be a positive integer.
*/
void setSize(int size);
void setBrushSize(int size);

/**
* Updates the inventory of the player brush settings.
* <p>
* This method should be called whenever there is a change
* in the brush settings that requires updating the inventory display.
* Retrieves the main menu for the player.
*
* @return The main menu.
*/
AbstractGUI getMainMenu();

/**
* Retrieves the brushes menu for the player.
*
* @return The brushes menu.
*/
void updateInventory();
AbstractGUI getBrushesMenu();

/**
* Retrieves the next brush in the list of available brushes.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.thenextlvl.gopaint.api.model;

import core.i18n.file.ComponentBundle;
import net.thenextlvl.gopaint.api.brush.BrushController;
import net.thenextlvl.gopaint.api.brush.BrushRegistry;
import org.bukkit.plugin.Plugin;

public interface GoPaintProvider extends Plugin {
String USE_PERMISSION = "gopaint.use";
String ADMIN_PERMISSION = "gopaint.admin";
String WORLD_BYPASS_PERMISSION = "gopaint.world.bypass";

ComponentBundle bundle();

BrushRegistry brushRegistry();

BrushController brushController();

PluginConfig config();
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package net.thenextlvl.gopaint.model;
package net.thenextlvl.gopaint.api.model;

import com.google.gson.annotations.SerializedName;
import net.thenextlvl.gopaint.api.model.MaskMode;
import net.thenextlvl.gopaint.api.model.SurfaceMode;
import net.thenextlvl.gopaint.api.brush.Brush;
import org.bukkit.Axis;
import org.bukkit.Material;

import java.util.List;

public record PluginConfig(
@SerializedName("generic") Generic generic,
@SerializedName("thickness") Thickness thickness,
@SerializedName("angle") Angle angle,
@SerializedName("fracture") Fracture fracture
@SerializedName("brush") BrushConfig brushConfig,
@SerializedName("thickness") ThicknessConfig thicknessConfig,
@SerializedName("angle") AngleConfig angleConfig,
@SerializedName("fracture") FractureConfig fractureConfig
) {

public record Generic(
@SerializedName("default-brush") Material defaultBrush,
public record BrushConfig(
@SerializedName("default-brush-type") Material defaultBrushType,
@SerializedName("default-brush") Brush defaultBrush,
@SerializedName("max-size") int maxSize,
@SerializedName("default-size") int defaultSize,
@SerializedName("default-chance") int defaultChance,
Expand All @@ -31,13 +31,13 @@ public record Generic(
) {
}

public record Thickness(
public record ThicknessConfig(
@SerializedName("default-thickness") int defaultThickness,
@SerializedName("max-thickness") int maxThickness
) {
}

public record Angle(
public record AngleConfig(
@SerializedName("default-angle-distance") int defaultAngleDistance,
@SerializedName("max-angle-distance") int maxAngleDistance,
@SerializedName("min-angle-height-difference") double minAngleHeightDifference,
Expand All @@ -46,7 +46,7 @@ public record Angle(
) {
}

public record Fracture(
public record FractureConfig(
@SerializedName("default-fracture-distance") int defaultFractureDistance,
@SerializedName("max-fracture-distance") int maxFractureDistance
) {
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ dependencies {

implementation("org.bstats:bstats-bukkit:3.0.2")

api("net.thenextlvl.core:i18n:1.0.18")
api("net.thenextlvl.core:adapters:1.0.9")

api(project(":api"))
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/net/thenextlvl/gopaint/GoPaintPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.thenextlvl.gopaint.adapter.BrushAdapter;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.brush.BrushController;
import net.thenextlvl.gopaint.api.brush.BrushRegistry;
import net.thenextlvl.gopaint.api.model.GoPaintProvider;
import net.thenextlvl.gopaint.api.model.MaskMode;
import net.thenextlvl.gopaint.api.model.PluginConfig;
import net.thenextlvl.gopaint.api.model.SurfaceMode;
import net.thenextlvl.gopaint.brush.CraftBrushController;
import net.thenextlvl.gopaint.brush.CraftBrushRegistry;
import net.thenextlvl.gopaint.brush.standard.SphereBrush;
import net.thenextlvl.gopaint.command.GoPaintCommand;
import net.thenextlvl.gopaint.model.PluginConfig;
import net.thenextlvl.gopaint.listener.ConnectListener;
import net.thenextlvl.gopaint.listener.InteractListener;
import net.thenextlvl.gopaint.listener.InventoryListener;
import net.thenextlvl.gopaint.api.model.SurfaceMode;
import org.bstats.bukkit.Metrics;
import org.bukkit.Axis;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -36,10 +39,7 @@
import java.util.Locale;

@Accessors(fluent = true)
public class GoPaintPlugin extends JavaPlugin implements Listener {
public static final String USE_PERMISSION = "gopaint.use";
public static final String ADMIN_PERMISSION = "gopaint.admin";
public static final String WORLD_BYPASS_PERMISSION = "gopaint.world.bypass";
public class GoPaintPlugin extends JavaPlugin implements GoPaintProvider {

private final File translations = new File(getDataFolder(), "translations");
private final @Getter ComponentBundle bundle = new ComponentBundle(translations, audience ->
Expand All @@ -51,20 +51,22 @@ public class GoPaintPlugin extends JavaPlugin implements Listener {
Placeholder.component("prefix", bundle.component(Locale.US, "prefix"))
)).build());

private final @Getter BrushController brushController = new CraftBrushController(this);
private final @Getter BrushRegistry brushRegistry = new CraftBrushRegistry(this);

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, Material.SPONGE, MaskMode.INTERFACE, SurfaceMode.DIRECT),
new PluginConfig.Thickness(1, 5),
new PluginConfig.Angle(2, 5, 10, 40, 85),
new PluginConfig.Fracture(2, 5)
new PluginConfig.BrushConfig(Material.FEATHER, SphereBrush.INSTANCE, 100, 10, 50, Axis.Y, 50, 50,
new ArrayList<>(), true, Material.SPONGE, MaskMode.INTERFACE, SurfaceMode.DIRECT),
new PluginConfig.ThicknessConfig(1, 5),
new PluginConfig.AngleConfig(2, 5, 10, 40, 85),
new PluginConfig.FractureConfig(2, 5)
), new GsonBuilder()
.registerTypeAdapter(Material.class, MaterialAdapter.NotNull.INSTANCE)
.registerTypeAdapter(Brush.class, new BrushAdapter(this))
.setPrettyPrinting()
.create()
).validate().save();

private final @Getter BrushController brushController = new CraftBrushController(this);
private final @Getter BrushRegistry brushRegistry = new CraftBrushRegistry(this);

private final Metrics metrics = new Metrics(this, 22279);

public GoPaintPlugin() {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/thenextlvl/gopaint/adapter/BrushAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.thenextlvl.gopaint.adapter;

import com.google.gson.*;
import core.annotation.MethodsReturnNotNullByDefault;
import core.annotation.ParametersAreNotNullByDefault;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.key.Key;
import net.thenextlvl.gopaint.api.brush.Brush;
import net.thenextlvl.gopaint.api.model.GoPaintProvider;

import java.lang.reflect.Type;

@RequiredArgsConstructor
@MethodsReturnNotNullByDefault
@ParametersAreNotNullByDefault
public class BrushAdapter implements JsonDeserializer<Brush>, JsonSerializer<Brush> {
private final GoPaintProvider provider;

@Override
@SuppressWarnings("PatternValidation")
public Brush deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
return provider.brushRegistry().getBrush(Key.key(element.getAsString())).orElseThrow(() ->
new IllegalArgumentException("unknown brush: " + element.getAsString()));
}

@Override
public JsonElement serialize(Brush brush, Type typ, JsonSerializationContext context) {
return new JsonPrimitive(brush.key().asString());
}
}
Loading
Loading