Skip to content

Commit

Permalink
Merge pull request #79 from TheNextLvl-net/update
Browse files Browse the repository at this point in the history
Refactor brush settings and menus to use updated APIs
  • Loading branch information
NonSwag authored Jan 3, 2025
2 parents 5c335e5 + 34c3359 commit 6c47e64
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 145 deletions.
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
}

api("net.thenextlvl.core:i18n:1.0.20")
api("net.thenextlvl.core:paper:1.5.3")
api("net.thenextlvl.core:paper:2.0.3")

annotationProcessor("org.projectlombok:lombok:1.18.36")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.thenextlvl.gopaint.api.brush.setting.PlayerBrushSettings;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jspecify.annotations.NullMarked;

import java.util.Optional;
Expand All @@ -30,14 +29,6 @@ public interface BrushController {
*/
Optional<ItemBrushSettings> parseBrushSettings(ItemStack itemStack);

/**
* Parses the brush settings from the given item metadata.
*
* @param itemMeta The metadata to parse the brush settings from.
* @return An optional containing the parsed brush settings, or empty if the meta does not contain brush settings.
*/
Optional<ItemBrushSettings> parseBrushSettings(ItemMeta itemMeta);

/**
* Removes the brush settings for a specific player.
*
Expand Down
8 changes: 2 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import net.minecrell.pluginyml.paper.PaperPluginDescription

plugins {
id("java")
id("java-library")

id("io.github.goooler.shadow") version "8.1.8"
id("io.papermc.hangar-publish-plugin") version "0.1.2"
Expand All @@ -18,7 +17,6 @@ version = "1.4.0"

repositories {
mavenCentral()
maven("https://jitpack.io")
maven("https://repo.thenextlvl.net/releases")
maven("https://repo.papermc.io/repository/maven-public/")
}
Expand All @@ -27,11 +25,9 @@ dependencies {
compileOnly("org.projectlombok:lombok:1.18.36")
compileOnly("io.papermc.paper:paper-api:1.21.4-R0.1-SNAPSHOT")

implementation("net.thenextlvl.core:adapters:2.0.1")
implementation("org.bstats:bstats-bukkit:3.1.0")

api("net.thenextlvl.core:adapters:2.0.1")

api(project(":api"))
implementation(project(":api"))

annotationProcessor("org.projectlombok:lombok:1.18.36")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jspecify.annotations.NullMarked;

import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

@NullMarked
@RequiredArgsConstructor
Expand All @@ -52,14 +56,7 @@ public PlayerBrushSettings getBrushSettings(Player player) {

@Override
public Optional<ItemBrushSettings> parseBrushSettings(ItemStack itemStack) {
if (!itemStack.hasItemMeta()) return Optional.empty();
return Optional.ofNullable(itemStack.getItemMeta())
.flatMap(this::parseBrushSettings);
}

@Override
public Optional<ItemBrushSettings> parseBrushSettings(ItemMeta itemMeta) {
var container = itemMeta.getPersistentDataContainer();
var container = itemStack.getPersistentDataContainer();

var brushSize = container.get(new NamespacedKey("gopaint", "size"), PersistentDataType.INTEGER);
var maskEnabled = container.get(new NamespacedKey("gopaint", "mask_enabled"), PersistentDataType.BOOLEAN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package net.thenextlvl.gopaint.brush.setting;

import core.paper.gui.AbstractGUI;
import io.papermc.paper.datacomponent.DataComponentTypes;
import io.papermc.paper.datacomponent.item.ItemLore;
import lombok.Getter;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
Expand All @@ -29,7 +31,15 @@
import net.thenextlvl.gopaint.api.brush.setting.ItemBrushSettings;
import net.thenextlvl.gopaint.api.brush.setting.PlayerBrushSettings;
import net.thenextlvl.gopaint.api.model.SurfaceMode;
import net.thenextlvl.gopaint.brush.standard.*;
import net.thenextlvl.gopaint.brush.standard.AngleBrush;
import net.thenextlvl.gopaint.brush.standard.DiskBrush;
import net.thenextlvl.gopaint.brush.standard.FractureBrush;
import net.thenextlvl.gopaint.brush.standard.GradientBrush;
import net.thenextlvl.gopaint.brush.standard.OverlayBrush;
import net.thenextlvl.gopaint.brush.standard.PaintBrush;
import net.thenextlvl.gopaint.brush.standard.SplatterBrush;
import net.thenextlvl.gopaint.brush.standard.SprayBrush;
import net.thenextlvl.gopaint.brush.standard.UnderlayBrush;
import net.thenextlvl.gopaint.menu.BrushesMenu;
import net.thenextlvl.gopaint.menu.MainMenu;
import org.bukkit.Axis;
Expand Down Expand Up @@ -238,62 +248,63 @@ public PatternBrush getPreviousBrush(@Nullable PatternBrush brush) {
@Override
public boolean exportSettings(ItemStack itemStack) {
if (itemStack.getType().equals(plugin.config().brushConfig().defaultBrushType())) return false;
return !itemStack.getType().isBlock() && itemStack.editMeta(itemMeta -> {
var lore = new ArrayList<Component>();
lore.add(Component.empty());
lore.add(plugin.bundle().component(player, "brush.exported.size",
Placeholder.parsed("size", String.valueOf(getBrushSize()))));
if (getBrush() instanceof SprayBrush) {
lore.add(plugin.bundle().component(player, "brush.exported.chance",
Placeholder.parsed("chance", String.valueOf(getChance()))));
} else if (getBrush() instanceof OverlayBrush || getBrush() instanceof UnderlayBrush) {
lore.add(plugin.bundle().component(player, "brush.exported.thickness",
Placeholder.parsed("thickness", String.valueOf(getThickness()))));
} else if (getBrush() instanceof DiskBrush) {
lore.add(plugin.bundle().component(player, "brush.exported.axis",
Placeholder.parsed("axis", getAxis().name())));
} else if (getBrush() instanceof AngleBrush) {
lore.add(plugin.bundle().component(player, "brush.exported.angle.distance",
Placeholder.parsed("distance", String.valueOf(getAngleDistance()))));
lore.add(plugin.bundle().component(player, "brush.exported.angle.height",
Placeholder.parsed("height", String.valueOf(getAngleHeightDifference()))));
} else if (getBrush() instanceof SplatterBrush || getBrush() instanceof PaintBrush) {
lore.add(plugin.bundle().component(player, "brush.exported.falloff",
Placeholder.parsed("falloff", String.valueOf(getFalloffStrength()))));
} else if (getBrush() instanceof GradientBrush) {
lore.add(plugin.bundle().component(player, "brush.exported.mixing",
Placeholder.parsed("mixing", String.valueOf(getMixingStrength()))));
lore.add(plugin.bundle().component(player, "brush.exported.falloff",
Placeholder.parsed("falloff", String.valueOf(getFalloffStrength()))));
} else if (getBrush() instanceof FractureBrush) {
lore.add(plugin.bundle().component(player, "brush.exported.fracture",
Placeholder.parsed("fracture", String.valueOf(getFractureStrength()))));
}
if (!blocks.isEmpty()) {
var blocks = getBlocks().stream()
.map(Material::translationKey)
.map(Component::translatable)
.toList();
lore.add(plugin.bundle().component(player, "brush.exported.blocks",
Placeholder.component("blocks", Component.join(JoinConfiguration.commas(true), blocks))));
}

if (isMaskEnabled()) {
lore.add(plugin.bundle().component(player, "brush.exported.mask",
Placeholder.component("mask", Component.translatable(getMask().translationKey()))));
}

if (!getSurfaceMode().equals(SurfaceMode.DISABLED)) {
var mode = plugin.bundle().component(player, getSurfaceMode().translationKey());
lore.add(plugin.bundle().component(player, "brush.exported.surface-mode",
Placeholder.component("mode", mode)));
}

itemMeta.itemName(plugin.bundle().component(player, "brush.exported.name",
Placeholder.component("brush", getBrush().getName(player))));
itemMeta.lore(lore);
itemMeta.setEnchantmentGlintOverride(true);

var lines = new ArrayList<Component>();
lines.add(Component.empty());
lines.add(plugin.bundle().component(player, "brush.exported.size",
Placeholder.parsed("size", String.valueOf(getBrushSize()))));
if (getBrush() instanceof SprayBrush) {
lines.add(plugin.bundle().component(player, "brush.exported.chance",
Placeholder.parsed("chance", String.valueOf(getChance()))));
} else if (getBrush() instanceof OverlayBrush || getBrush() instanceof UnderlayBrush) {
lines.add(plugin.bundle().component(player, "brush.exported.thickness",
Placeholder.parsed("thickness", String.valueOf(getThickness()))));
} else if (getBrush() instanceof DiskBrush) {
lines.add(plugin.bundle().component(player, "brush.exported.axis",
Placeholder.parsed("axis", getAxis().name())));
} else if (getBrush() instanceof AngleBrush) {
lines.add(plugin.bundle().component(player, "brush.exported.angle.distance",
Placeholder.parsed("distance", String.valueOf(getAngleDistance()))));
lines.add(plugin.bundle().component(player, "brush.exported.angle.height",
Placeholder.parsed("height", String.valueOf(getAngleHeightDifference()))));
} else if (getBrush() instanceof SplatterBrush || getBrush() instanceof PaintBrush) {
lines.add(plugin.bundle().component(player, "brush.exported.falloff",
Placeholder.parsed("falloff", String.valueOf(getFalloffStrength()))));
} else if (getBrush() instanceof GradientBrush) {
lines.add(plugin.bundle().component(player, "brush.exported.mixing",
Placeholder.parsed("mixing", String.valueOf(getMixingStrength()))));
lines.add(plugin.bundle().component(player, "brush.exported.falloff",
Placeholder.parsed("falloff", String.valueOf(getFalloffStrength()))));
} else if (getBrush() instanceof FractureBrush) {
lines.add(plugin.bundle().component(player, "brush.exported.fracture",
Placeholder.parsed("fracture", String.valueOf(getFractureStrength()))));
}
if (!blocks.isEmpty()) {
var blocks = getBlocks().stream()
.map(Material::translationKey)
.map(Component::translatable)
.toList();
lines.add(plugin.bundle().component(player, "brush.exported.blocks",
Placeholder.component("blocks", Component.join(JoinConfiguration.commas(true), blocks))));
}

if (isMaskEnabled()) {
lines.add(plugin.bundle().component(player, "brush.exported.mask",
Placeholder.component("mask", Component.translatable(getMask().translationKey()))));
}

if (!getSurfaceMode().equals(SurfaceMode.DISABLED)) {
var mode = plugin.bundle().component(player, getSurfaceMode().translationKey());
lines.add(plugin.bundle().component(player, "brush.exported.surface-mode",
Placeholder.component("mode", mode)));
}

itemStack.setData(DataComponentTypes.ENCHANTMENT_GLINT_OVERRIDE, true);
itemStack.setData(DataComponentTypes.ITEM_NAME, plugin.bundle().component(player, "brush.exported.name",
Placeholder.component("brush", getBrush().getName(player))));
itemStack.setData(DataComponentTypes.LORE, ItemLore.lore(lines));

return !itemStack.getType().isBlock() && itemStack.editMeta(itemMeta -> {
var container = itemMeta.getPersistentDataContainer();

container.set(new NamespacedKey("gopaint", "size"), PersistentDataType.INTEGER, getBrushSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import core.paper.item.ItemBuilder;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
Expand All @@ -15,13 +14,13 @@
import net.thenextlvl.gopaint.api.brush.PatternBrush;
import net.thenextlvl.gopaint.api.model.GoPaintProvider;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jspecify.annotations.NullMarked;

import java.util.List;

@NullMarked
@RequiredArgsConstructor
@SuppressWarnings("UnstableApiUsage")
public class GoPaintCommand {
private final GoPaintPlugin plugin;

Expand Down Expand Up @@ -171,15 +170,15 @@ private boolean giveWand(Player player) {
}

if (inventory.getItemInMainHand().isEmpty()) {
inventory.setItemInMainHand(new ItemBuilder(type));
inventory.setItemInMainHand(new ItemStack(type));
return true;
}

var empty = inventory.firstEmpty();
if (empty == -1) return false;

inventory.setItem(empty, inventory.getItemInMainHand());
inventory.setItemInMainHand(new ItemBuilder(type));
inventory.setItemInMainHand(new ItemStack(type));
return true;
}
}
12 changes: 6 additions & 6 deletions src/main/java/net/thenextlvl/gopaint/menu/BrushesMenu.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.thenextlvl.gopaint.menu;

import core.paper.gui.PagedGUI;
import core.paper.gui.PaginatedGUI;
import core.paper.item.ActionItem;
import core.paper.item.ItemBuilder;
import lombok.Getter;
Expand All @@ -17,8 +17,8 @@
import java.util.stream.IntStream;

@NullMarked
public class BrushesMenu extends PagedGUI<GoPaintProvider, PatternBrush> {
private final @Getter Options options = new Options(
public class BrushesMenu extends PaginatedGUI<GoPaintProvider, PatternBrush> {
private final @Getter Pagination pagination = new Pagination(
IntStream.range(0, getSize() - 9).toArray(),
getSize() - 6,
getSize() - 4
Expand All @@ -33,14 +33,14 @@ public BrushesMenu(GoPaintProvider plugin, PlayerBrushSettings settings, Player

@Override
public void formatDefault() {
var placeholder = new ItemBuilder(Material.GRAY_STAINED_GLASS_PANE).hideTooltip(true);
var placeholder = ItemBuilder.of(Material.GRAY_STAINED_GLASS_PANE).hideTooltip();
IntStream.range(0, getSize()).forEach(value -> setSlotIfAbsent(value, placeholder));
}

@Override
public ActionItem constructItem(PatternBrush brush) {
return new ItemBuilder(Material.PLAYER_HEAD)
.headValue(brush.getHeadValue())
return ItemBuilder.of(Material.PLAYER_HEAD)
.profileValue(brush.getHeadValue())
.itemName(brush.getName(owner).color(NamedTextColor.GOLD))
.lore(brush.getDescription(owner))
.withAction(() -> {
Expand Down
Loading

0 comments on commit 6c47e64

Please sign in to comment.