Skip to content

Commit

Permalink
GH-466 Add option to enchant item in hand of another player (#469)
Browse files Browse the repository at this point in the history
* Add option to enchant item in hand of another player

* Update eternalcore-core/src/main/java/com/eternalcode/core/feature/essentials/item/EnchantCommand.java

Co-authored-by: DMK <[email protected]>

* Follow Piotrulla suggestions.

---------

Co-authored-by: DMK <[email protected]>
  • Loading branch information
vLuckyyy and imDMK authored Aug 21, 2023
1 parent e213c74 commit 1ae1d4e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.eternalcode.core.notice.NoticeService;
import dev.rollczi.litecommands.argument.Arg;
import dev.rollczi.litecommands.command.amount.Min;
import dev.rollczi.litecommands.command.amount.Required;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.permission.Permission;
import dev.rollczi.litecommands.command.route.Route;
Expand All @@ -13,6 +14,8 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

import java.util.UUID;

@Route(name = "enchant")
@Permission("eternalcore.enchant")
public class EnchantCommand {
Expand Down Expand Up @@ -41,24 +44,50 @@ void execute(Player player, @Arg Enchantment enchantment, @Arg int level) {
return;
}

if (this.configuration.items.unsafeEnchantments) {
handItem.addUnsafeEnchantment(enchantment, level);
this.enchantItem(player.getUniqueId(), handItem, enchantment, level);
}

@Execute
@Required(3)
@DescriptionDocs(description = "Enchants item in hand", arguments = "<enchantment> <level> <player>")
void execute(Player sender, @Arg Enchantment enchantment, @Arg int level, @Arg Player target) {
PlayerInventory targetInventory = target.getInventory();
ItemStack handItem = targetInventory.getItem(targetInventory.getHeldItemSlot());

if (handItem == null) {
this.noticeService.create()
.player(sender.getUniqueId())
.notice(translation -> translation.argument().noItem())
.send();

return;
}
else {
if (enchantment.getStartLevel() > level || enchantment.getMaxLevel() < level || !enchantment.canEnchantItem(handItem)) {
this.noticeService.create()
.player(player.getUniqueId())
.notice(translation -> translation.argument().noValidEnchantmentLevel())
.send();

return;
}
this.enchantItem(sender.getUniqueId(), handItem, enchantment, level);
}

handItem.addEnchantment(enchantment, level);
private void enchantItem(UUID playerId, ItemStack item, Enchantment enchantment, int level) {
if (this.configuration.items.unsafeEnchantments) {
item.addUnsafeEnchantment(enchantment, level);
this.noticeService.create()
.player(playerId)
.notice(translation -> translation.item().enchantedMessage())
.send();
return;
}

if (enchantment.getStartLevel() > level || enchantment.getMaxLevel() < level || !enchantment.canEnchantItem(item)) {
this.noticeService.create()
.player(playerId)
.notice(translation -> translation.argument().noValidEnchantmentLevel())
.send();
return;
}

item.addEnchantment(enchantment, level);

this.noticeService.create()
.player(player.getUniqueId())
.player(playerId)
.notice(translation -> translation.item().enchantedMessage())
.send();
}
Expand Down
4 changes: 2 additions & 2 deletions eternalcore-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ eternalShadowCompiler {

tasks {
runServer {
minecraftVersion("1.19.4")
minecraftVersion("1.20.1")
}
}
}

0 comments on commit 1ae1d4e

Please sign in to comment.