From e3a425eb0c429a8076077b60630399f2dfd65211 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sat, 1 May 2021 10:27:10 +0100 Subject: [PATCH] Added level support in /talgive --- build.gradle | 2 +- .../talismans/commands/CommandTalgive.java | 25 ++++++++++++++++--- .../commands/TabcompleterTalgive.java | 24 +++++++++++++++++- .../core-plugin/src/main/resources/lang.yml | 1 + 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/build.gradle b/build.gradle index 8e4f6b3..a693472 100644 --- a/build.gradle +++ b/build.gradle @@ -47,7 +47,7 @@ allprojects { } dependencies { - compileOnly 'com.willfp:eco:5.0.0' + compileOnly 'com.willfp:eco:5.2.0' compileOnly 'org.jetbrains:annotations:19.0.0' diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalgive.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalgive.java index 83948d3..f1fcfd2 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalgive.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/CommandTalgive.java @@ -4,6 +4,7 @@ import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.command.AbstractCommand; import com.willfp.eco.core.command.AbstractTabCompleter; import com.willfp.talismans.talismans.Talisman; +import com.willfp.talismans.talismans.TalismanLevel; import com.willfp.talismans.talismans.Talismans; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -42,11 +43,21 @@ public class CommandTalgive extends AbstractCommand { return; } - int amount = 1; + int level = 1; if (args.size() > 2) { try { - amount = Integer.parseInt(args.get(2)); + level = Integer.parseInt(args.get(2)); + } catch (NumberFormatException ignored) { + // do nothing + } + } + + int amount = 1; + + if (args.size() > 3) { + try { + level = Integer.parseInt(args.get(3)); } catch (NumberFormatException ignored) { // do nothing } @@ -67,11 +78,17 @@ public class CommandTalgive extends AbstractCommand { return; } + TalismanLevel talismanLevel = talisman.getLevel(level); + if (talismanLevel == null) { + sender.sendMessage(this.getPlugin().getLangYml().getMessage("invalid-level")); + return; + } + String message = this.getPlugin().getLangYml().getMessage("give-success"); - message = message.replace("%talisman%", talisman.getFormattedName()).replace("%recipient%", reciever.getName()); + message = message.replace("%talisman%", talismanLevel.getName()).replace("%recipient%", reciever.getName()); sender.sendMessage(message); - ItemStack itemStack = talisman.getItemStack(); + ItemStack itemStack = talismanLevel.getItemStack(); itemStack.setAmount(amount); reciever.getInventory().addItem(itemStack); } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/TabcompleterTalgive.java b/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/TabcompleterTalgive.java index 7fbb523..748c0b1 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/TabcompleterTalgive.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/talismans/commands/TabcompleterTalgive.java @@ -2,9 +2,12 @@ package com.willfp.talismans.commands; import com.willfp.eco.core.command.AbstractTabCompleter; import com.willfp.eco.core.config.ConfigUpdater; +import com.willfp.talismans.TalismansPlugin; import com.willfp.talismans.talismans.Talisman; +import com.willfp.talismans.talismans.TalismanLevel; import com.willfp.talismans.talismans.Talismans; import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.util.StringUtil; @@ -83,7 +86,26 @@ public class TabcompleterTalgive extends AbstractTabCompleter { } if (args.size() == 3) { - StringUtil.copyPartialMatches(args.get(2), NUMBERS, completions); + Talisman talisman = Talismans.getByKey(TalismansPlugin.getInstance().getNamespacedKeyFactory().create(args.get(1).toLowerCase())); + if (talisman == null) { + return new ArrayList<>(); + } + + List levels = talisman.getLevels().stream().map(TalismanLevel::getLevel).map(String::valueOf).collect(Collectors.toList()); + + StringUtil.copyPartialMatches(args.get(2), levels, completions); + + completions.sort((s1, s2) -> { + int t1 = Integer.parseInt(s1); + int t2 = Integer.parseInt(s2); + return t1 - t2; + }); + + return completions; + } + + if (args.size() == 4) { + StringUtil.copyPartialMatches(args.get(3), NUMBERS, completions); completions.sort((s1, s2) -> { int t1 = Integer.parseInt(s1); diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index f6d1638..21221f4 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -7,6 +7,7 @@ messages: invalid-player: "&cInvalid player!" needs-talisman: "&cYou must specify a talisman" invalid-talisman: "&cInvalid talisman!" + invalid-level: "&cLevel doesn't exist!" give-success: "Gave &a%talisman%&r to &a%recipient%" description-color: "&8" \ No newline at end of file