Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #680 from book000/feat/jaobox
Browse files Browse the repository at this point in the history
feat: #679 jaoBoxの実装
  • Loading branch information
book000 authored Jan 28, 2022
2 parents a51738f + 54f5f1b commit 8cdd4fa
Show file tree
Hide file tree
Showing 2 changed files with 206 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java
Original file line number Diff line number Diff line change
@@ -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<CommandSender> 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<CommandSender> context) {
Player player = (Player) context.getSender();

openBox(player, viewerTitleComponent);
}

void jaoBoxRegister(CommandContext<CommandSender> 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<ItemStack>) 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);

}
}
101 changes: 101 additions & 0 deletions src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java
Original file line number Diff line number Diff line change
@@ -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<Material> 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;
}
}

0 comments on commit 8cdd4fa

Please sign in to comment.