From 54f5f1b9f9904f5619d4e5063511f5059684fa0b Mon Sep 17 00:00:00 2001 From: "Tomachi [ICHIGO]" Date: Sat, 29 Jan 2022 06:26:02 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20#679=20jaoBox=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jaoafa/mymaid4/command/Cmd_jaoBox.java | 105 ++++++++++++++++++ .../jaoafa/mymaid4/event/Event_jaoBox.java | 101 +++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java create mode 100644 src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java diff --git a/src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java b/src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java new file mode 100644 index 000000000..6b7b16dc5 --- /dev/null +++ b/src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java @@ -0,0 +1,105 @@ +/* + * 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.Command; +import cloud.commandframework.context.CommandContext; +import cloud.commandframework.meta.CommandMeta; +import com.jaoafa.mymaid4.Main; +import com.jaoafa.mymaid4.lib.CommandPremise; +import com.jaoafa.mymaid4.lib.MyMaidCommand; +import com.jaoafa.mymaid4.lib.MyMaidLibrary; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.JoinConfiguration; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.Bukkit; +import org.bukkit.Sound; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import java.io.File; +import java.util.List; +import java.util.Objects; + +public class Cmd_jaoBox extends MyMaidLibrary implements CommandPremise { + public static Component viewerTitleComponent = Component.text("jaoBox", NamedTextColor.YELLOW); + public static Component registerTitleComponent = Component.join( + JoinConfiguration.noSeparators(), + Component.text("jaoBox"), + Component.space(), + Component.text("(登録モード)", NamedTextColor.RED, TextDecoration.UNDERLINED) + ); + public static File file; + + @Override + public MyMaidCommand.Detail details() { + return new MyMaidCommand.Detail( + "jaobox", + "jao Minecraft Serverでの便利アイテムを集めたjaoBoxを開きます。" + ); + } + + @Override + public MyMaidCommand.Cmd register(Command.Builder builder) { + return new MyMaidCommand.Cmd( + builder + .senderType(Player.class) + .meta(CommandMeta.DESCRIPTION, "jaoBoxを開きます。") + .handler(this::jaoBox) + .build(), + builder + .senderType(Player.class) + .meta(CommandMeta.DESCRIPTION, "jaoBoxを設定します。") + .literal("register") + .handler(this::jaoBoxRegister) + .build() + ); + } + + void jaoBox(CommandContext context) { + Player player = (Player) context.getSender(); + + openBox(player, viewerTitleComponent); + } + + void jaoBoxRegister(CommandContext context) { + Player player = (Player) context.getSender(); + + openBox(player, registerTitleComponent); + SendMessage(player, details(), "jaoBoxを登録モードで開きました。Escキーで閉じると保存します。"); + } + + public static void openBox(Player player, Component title) { + if (file == null) { + file = new File(Main.getJavaPlugin().getDataFolder(), "jaoBox.yml"); + } + + Inventory inventory = Bukkit.getServer().createInventory( + player, + 6 * 9, + title); + if (file.exists()) { + YamlConfiguration yaml = YamlConfiguration.loadConfiguration(file); + @SuppressWarnings("unchecked") // 未チェックのキャスト。YamlConfigurationの仕様によるもの + ItemStack[] items = ((List) Objects.requireNonNull(yaml.get("items"))).toArray(new ItemStack[0]); + inventory.setContents(items); + } + + player.getWorld().playSound(player.getLocation(), Sound.BLOCK_CHEST_OPEN, 1.0F, 1.0F); + player.openInventory(inventory); + + } +} diff --git a/src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java b/src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java new file mode 100644 index 000000000..d3f20c60e --- /dev/null +++ b/src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java @@ -0,0 +1,101 @@ +/* + * 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.event; + +import com.jaoafa.mymaid4.command.Cmd_jaoBox; +import com.jaoafa.mymaid4.lib.EventPremise; +import com.jaoafa.mymaid4.lib.MyMaidLibrary; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.type.Sign; +import org.bukkit.block.data.type.WallSign; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.Inventory; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +public class Event_jaoBox extends MyMaidLibrary implements Listener, EventPremise { + @Override + public String description() { + return "jaoBoxに関するイベントを管理します。"; + } + + @EventHandler + public void onRegisterInventoryClose(InventoryCloseEvent event) { + Player player = (Player) event.getPlayer(); + if (event.getView().title() == Cmd_jaoBox.viewerTitleComponent) { + player.getWorld().playSound(player.getLocation(), Sound.BLOCK_CHEST_CLOSE, 1.0F, 1.0F); + return; + } + if (event.getView().title() != Cmd_jaoBox.registerTitleComponent) { + return; + } + Inventory inventory = event.getInventory(); + YamlConfiguration yaml = new YamlConfiguration(); + yaml.set("items", inventory.getContents()); + try { + yaml.save(Cmd_jaoBox.file); + } catch (IOException e) { + e.printStackTrace(); + } + player.getWorld().playSound(player.getLocation(), Sound.BLOCK_CHEST_LOCKED, 1.0F, 1.0F); + event.getPlayer().sendMessage("[jaoBox] " + ChatColor.GREEN + "jaoBoxを更新しました。"); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onClickBox(PlayerInteractEvent event) { + Player player = event.getPlayer(); + Action action = event.getAction(); + Block block = event.getClickedBlock(); + + if (action != Action.RIGHT_CLICK_BLOCK || block == null || block.getType() != Material.CHEST) { + return; + } + + org.bukkit.block.Sign sign = getNearSign(block); + if (sign == null) { + return; + } + if (sign.lines().stream().noneMatch(line -> PlainTextComponentSerializer.plainText().serialize(line).equals("[jaoBox]"))) { + return; + } + + event.setCancelled(true); + Cmd_jaoBox.openBox(player, Cmd_jaoBox.viewerTitleComponent); + } + + private org.bukkit.block.Sign getNearSign(Block block) { + List signs = Arrays + .stream(Material.values()) + .filter(material -> material.data == Sign.class || material.data == WallSign.class) + .toList(); + for (BlockFace face : BlockFace.values()) { + if (signs.contains(block.getRelative(face).getType())) { + return (org.bukkit.block.Sign) block.getRelative(face).getState(); + } + } + return null; + } +} \ No newline at end of file