From b2aa71b8ce35a8b9e48a2837e569169743c396a7 Mon Sep 17 00:00:00 2001 From: "Tomachi [ICHIGO]" Date: Fri, 25 Mar 2022 23:56:34 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20light=E3=82=B3=E3=83=9E=E3=83=B3?= =?UTF-8?q?=E3=83=89=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jaoafa/mymaid4/command/Cmd_Light.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/main/java/com/jaoafa/mymaid4/command/Cmd_Light.java diff --git a/src/main/java/com/jaoafa/mymaid4/command/Cmd_Light.java b/src/main/java/com/jaoafa/mymaid4/command/Cmd_Light.java new file mode 100644 index 000000000..cd993502c --- /dev/null +++ b/src/main/java/com/jaoafa/mymaid4/command/Cmd_Light.java @@ -0,0 +1,97 @@ +/* + * jaoLicense + * + * Copyright (c) 2022 jao Minecraft Server + * + * The following license applies to this project: jaoLicense + * + * Japanese: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE.md + * English: https://github.com/jaoafa/jao-Minecraft-Server/blob/master/jaoLICENSE-en.md + */ + +package com.jaoafa.mymaid4.command; + +import cloud.commandframework.ArgumentDescription; +import cloud.commandframework.Command; +import cloud.commandframework.bukkit.parsers.PlayerArgument; +import cloud.commandframework.context.CommandContext; +import cloud.commandframework.meta.CommandMeta; +import com.jaoafa.mymaid4.lib.CommandPremise; +import com.jaoafa.mymaid4.lib.MyMaidCommand; +import com.jaoafa.mymaid4.lib.MyMaidLibrary; +import org.bukkit.Material; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +public class Cmd_Light extends MyMaidLibrary implements CommandPremise { + @Override + public MyMaidCommand.Detail details() { + return new MyMaidCommand.Detail( + "light", + "ライトブロックを入手します。" + ); + } + + @Override + public MyMaidCommand.Cmd register(Command.Builder builder) { + return new MyMaidCommand.Cmd( + builder + .meta(CommandMeta.DESCRIPTION, "ライトブロックをコマンド実行者のメインハンドのアイテムと置き換えます。") + .senderType(Player.class) + .handler(this::giveLight) + .build(), + builder + .meta(CommandMeta.DESCRIPTION, "ライトブロックを指定したプレイヤーのメインハンドのアイテムと置き換えます。") + .argument(PlayerArgument.of("player"), ArgumentDescription.of("ライトブロックを付与するプレイヤー名")) + .handler(this::giveLightToPlayer) + .build() + ); + } + + void giveLight(CommandContext context) { + Player player = (Player) context.getSender(); + ItemStack is = new ItemStack(Material.LIGHT); + + PlayerInventory inv = player.getInventory(); + ItemStack main = inv.getItemInMainHand(); + + inv.setItemInMainHand(is); + SendMessage(player, details(), "ライトブロックをメインハンドのアイテムと置きかえました。"); + + if (main.getType() != Material.AIR) { + if (player.getInventory().firstEmpty() == -1) { + player.getLocation().getWorld().dropItem(player.getLocation(), main); + SendMessage(player, details(), "インベントリがいっぱいだったため、既に持っていたアイテムはあなたの足元にドロップしました。"); + } else { + inv.addItem(main); + } + } + } + + void giveLightToPlayer(CommandContext context) { + Player player = context.getOrDefault("player", null); + if (player == null) { + SendMessage(context.getSender(), details(), "プレイヤーは指定されていないか存在しません。"); + return; + } + + ItemStack is = new ItemStack(Material.LIGHT); + + PlayerInventory inv = player.getInventory(); + ItemStack main = inv.getItemInMainHand(); + + inv.setItemInMainHand(is); + SendMessage(context.getSender(), details(), "ライトブロックをプレイヤー「" + player.getName() + "」のメインハンドのアイテムと置きかえました。"); + + if (main.getType() != Material.AIR) { + if (player.getInventory().firstEmpty() == -1) { + player.getLocation().getWorld().dropItem(player.getLocation(), main); + SendMessage(player, details(), "インベントリがいっぱいだったため、既に持っていたアイテムはあなたの足元にドロップしました。"); + } else { + inv.addItem(main); + } + } + } +}