Skip to content

Commit

Permalink
Add permissions, reload command and workflows (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
zefir-git authored May 25, 2024
2 parents 912425c + 06ec15b commit a54d4b4
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 42 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "maven"
directory: "/"
schedule:
interval: "weekly"
38 changes: 38 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

name: Java CI with Maven

permissions:
contents: write

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Submit Dependency Snapshot
uses: advanced-security/maven-dependency-submission-action@v3
62 changes: 37 additions & 25 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
name: Maven Release

on:
release:
types: [ published ]
release:
types: [ published ]

jobs:
package-and-upload:
name: Build and Upload JARs
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'

- name: Build and package Maven project
run: mvn clean package

- name: Upload to release
uses: JasonEtco/upload-to-release@master
with:
args: target/EnchantBookPlus-${{ github.event.release.tag_name }}.jar application/java-archive
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
package-and-upload:
name: Build and Upload JARs
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Set project version
run: mvn -B versions:set -DnewVersion=${{ github.event.release.tag_name }} -DgenerateBackupPoms=false

- name: Build and package Maven project
run: mvn clean package

- name: Upload to release
uses: JasonEtco/upload-to-release@master
with:
args: target/EnchantBookPlus-${{ github.event.release.tag_name }}.jar application/java-archive
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload to Modrinth
uses: cloudnode-pro/[email protected]
with:
token: '${{ secrets.MODRINTH_TOKEN }}'
project: '${{ github.event.repository.custom_properties.modrinth_id }}'
file: target/EnchantBookPlus-${{ github.event.release.tag_name }}.jar
changelog: ${{ github.event.release.body }}
loaders: paper
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ This plugin allows you to combine enchantments, e.g. Efficiency V + Efficiency V

You can configure which enchantments can be combined above the Vanilla Minecraft levels. You can set the max level for each enchantment, and add XP cost in levels.

> [!IMPORTANT]
> This plugin requires [AnvilUnlocker](https://github.com/Jikoo/AnvilUnlocker/releases/latest) for anvil costs above 40 lvl.
## Permissions

> [!NOTE]
> `<echantment>` is the enchantment ID, e.g. `swift_sneak`, `efficiency`, etc. Replace with `*` for all enchantments.
>
> Example: `enchantbookplus.enchant.*` or `enchantbookplus.enchant.unbreaking`
| Permission | Description |
|-----------------------------------------|----------------------------------------------------------------------------------------|
| `enchantbookplus.enchant.<enchantment>` | Allow enchanting `<enchantment>` above the vanilla level, as configured in the plugin. |
| `enchantbookplus.reload` | Reload plugin config using `/enchantbookplus reload` |

## Reporting issues
Fixing bugs is the utmost priority for this project. If you find any issue, check the [GitHub Issue Tracker](https://github.com/cloudnode-pro/EnchantBookPlus/issues) to see if it has already been reported. If not, create a new issue.

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>pro.cloudnode.smp</groupId>
<artifactId>EnchantBookPlus</artifactId>
<version>0.0.0</version>
<version>0.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>EnchantBookPlus</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@
import java.util.Optional;
import java.util.stream.Collectors;

public final class ConfigEnchantmentEntry {
public class ConfigEnchantmentEntry {
/**
* Name of the enchantment.
*/
private final @NotNull String name;
public final @NotNull String name;

/**
* Maximum level of the enchantment.
*/
private final @Nullable Integer maxLevel;
protected final @Nullable Integer maxLevel;

/**
* Max level relative
*/
private final boolean maxLevelRelative;
protected final boolean maxLevelRelative;

/**
* Cost of the enchantment.
*/
private final int cost;
protected final int cost;

/**
* Multiply cost by level.
*/
private final boolean multiplyCostByLevel;
protected final boolean multiplyCostByLevel;

/**
* Name of the enchantment.
*/
public @NotNull String getName() {
public final @NotNull String getName() {
return name;
}

/**
* Maximum level of the enchantment.
*/
public @NotNull Optional<Integer> getMaxLevel() {
public final @NotNull Optional<@NotNull Integer> getMaxLevel() {
if (Optional.ofNullable(maxLevel).isEmpty()) return Optional.empty();
if (maxLevelRelative) return Optional.of(getEnchantment().getMaxLevel() + maxLevel);
return Optional.of(maxLevel);
Expand All @@ -57,21 +57,21 @@ public final class ConfigEnchantmentEntry {
/**
* Cost of the enchantment.
*/
public int getCost() {
public final int getCost() {
return cost;
}

/**
* Multiply cost by level.
*/
public boolean getMultiplyCostByLevel() {
public final boolean getMultiplyCostByLevel() {
return multiplyCostByLevel;
}

/**
* Get enchantment
*/
public Enchantment getEnchantment() {
public final Enchantment getEnchantment() {
return Enchantment.getByKey(NamespacedKey.minecraft(name));
}

Expand All @@ -80,7 +80,7 @@ public Enchantment getEnchantment() {
*
* @param enchantment The enchantment
*/
public boolean isEnchantment(final @NotNull Enchantment enchantment) {
public final boolean isEnchantment(final @NotNull Enchantment enchantment) {
return name.equalsIgnoreCase(enchantment.getKey().getKey());
}

Expand All @@ -104,7 +104,7 @@ public ConfigEnchantmentEntry(final @NotNull String name, final @Nullable Intege
*
* @param configValue Config object
*/
public static ConfigEnchantmentEntry configValue(final @NotNull HashMap<@NotNull String, @NotNull Object> configValue) {
public static @NotNull ConfigEnchantmentEntry configValue(final @NotNull HashMap<@NotNull String, @NotNull Object> configValue) {
final @NotNull String name = (String) Objects.requireNonNull(configValue.get("name"));
final @Nullable Integer maxLevel;
final boolean maxLevelRelative;
Expand Down Expand Up @@ -192,4 +192,18 @@ private static boolean isValidConfigValue(final @Nullable Object configValue) {
if (!isValidConfigValue(configValue)) throw new IllegalArgumentException("Invalid config value");
return configArray((ArrayList<HashMap<String, Object>>) configValue);
}

public static final class AllConfigEnchantmentEntry extends ConfigEnchantmentEntry {
private AllConfigEnchantmentEntry(final @Nullable Integer maxLevel, final boolean maxLevelRelative, final int cost, final boolean multiplyCostByLevel) {
super("ALL", maxLevel, maxLevelRelative, cost, multiplyCostByLevel);
}

public static @NotNull AllConfigEnchantmentEntry from(final @NotNull ConfigEnchantmentEntry configEnchantmentEntry) {
return new AllConfigEnchantmentEntry(configEnchantmentEntry.maxLevel, configEnchantmentEntry.maxLevelRelative, configEnchantmentEntry.cost, configEnchantmentEntry.multiplyCostByLevel);
}

public @NotNull ConfigEnchantmentEntry enchant(final @NotNull Enchantment enchantment) {
return new ConfigEnchantmentEntry(enchantment.getKey().getKey(), this.maxLevel, maxLevelRelative, cost, multiplyCostByLevel);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import org.bukkit.enchantments.Enchantment;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pro.cloudnode.smp.enchantbookplus.event.PrepareAnvil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Collectors;

public final class EnchantBookPlus extends JavaPlugin {
/**
Expand All @@ -34,35 +37,54 @@ private void registerEvents() {
* Config enchantments cache
*/
public @NotNull List<@NotNull ConfigEnchantmentEntry> getConfigEnchantments() {
if (configEnchantments.size() == 0) reload();
return configEnchantments;
}

/**
* "ALL" enchantment cache
*/
private @Nullable ConfigEnchantmentEntry.AllConfigEnchantmentEntry allConfigEnchantment;

/**
* "ALL" enchantment cache
*/
public @NotNull Optional<ConfigEnchantmentEntry.@NotNull AllConfigEnchantmentEntry> getAllConfigEnchantment() {
return Optional.ofNullable(allConfigEnchantment);
}

/**
* Get enchantment from cache
*
* @param enchantment The Minecraft enchantment
*/
public @NotNull Optional<@NotNull ConfigEnchantmentEntry> getConfigEnchantment(final @NotNull Enchantment enchantment) {
return getConfigEnchantments().stream().filter(c -> c.isEnchantment(enchantment)).findFirst();
final @NotNull Optional<@NotNull ConfigEnchantmentEntry> entry = getConfigEnchantments().stream().filter(c -> c.isEnchantment(enchantment)).findFirst();
return entry.isEmpty() ? getAllConfigEnchantment().map(a -> a.enchant(enchantment)) : entry;
}

/**
* Reload config
*/
public void reload() {
reloadConfig();
final @NotNull List<@NotNull ConfigEnchantmentEntry> enchants;
try {
configEnchantments = ConfigEnchantmentEntry.config(getConfig().get("enchantments"));
enchants = ConfigEnchantmentEntry.config(getConfig().get("enchantments"));
}
catch (final @NotNull Exception exception) {
getLogger().log(Level.SEVERE, "Failed to load config", exception);
getServer().getPluginManager().disablePlugin(this);
return;
}
allConfigEnchantment = enchants.stream()
.filter(c -> c.name.equalsIgnoreCase("ALL")).findFirst().map(ConfigEnchantmentEntry.AllConfigEnchantmentEntry::from).orElse(null);
configEnchantments = enchants.stream().filter(c -> !c.name.equalsIgnoreCase("ALL")).collect(Collectors.toList());
}

@Override
public void onEnable() {
Objects.requireNonNull(getCommand("enchantbookplus")).setExecutor(new MainCommand());

registerEvents();
saveDefaultConfig();
reload();
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/pro/cloudnode/smp/enchantbookplus/MainCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package pro.cloudnode.smp.enchantbookplus;

import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

public final class MainCommand implements CommandExecutor, TabCompleter {
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) reload(sender, command);
return overview(sender);
}

@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
return null;
}

/**
* Plugin overview
*/
public static boolean overview(final @NotNull CommandSender sender) {
final @NotNull EnchantBookPlus plugin = EnchantBookPlus.getInstance();
sender.sendMessage(MiniMessage.miniMessage()
.deserialize("<green><name></green> <white>v<version> by</white> <gray><author></gray>", Placeholder.unparsed("name", plugin
.getPluginMeta().getName()), Placeholder.unparsed("version", plugin.getPluginMeta()
.getVersion()), Placeholder.unparsed("author", String.join(", ", plugin.getPluginMeta()
.getAuthors()))));

return true;
}

/**
* Reload plugin configuration
*/
public static boolean reload(final @NotNull CommandSender sender, final @NotNull Command command) {
if (!sender.hasPermission(Permissions.RELOAD)) {
sender.sendMessage(Optional.ofNullable(command.permissionMessage()).orElse(sender.getServer().permissionMessage()));
return true;
}
EnchantBookPlus.getInstance().reload();
sender.sendMessage(MiniMessage.miniMessage().deserialize("<green>(!) Plugin configuration reloaded."));
return true;
}
}
12 changes: 12 additions & 0 deletions src/main/java/pro/cloudnode/smp/enchantbookplus/Permissions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pro.cloudnode.smp.enchantbookplus;

import org.bukkit.enchantments.Enchantment;
import org.jetbrains.annotations.NotNull;

public final class Permissions {
public static @NotNull String enchant(final @NotNull Enchantment enchantment) {
return "enchantbookplus.enchant." + enchantment.getKey().getKey();
}

public static @NotNull String RELOAD = "enchantbookplus.reload";
}
Loading

0 comments on commit a54d4b4

Please sign in to comment.