diff --git a/src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java b/src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java index 6b7b16dc5..50f5666e9 100644 --- a/src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java +++ b/src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java @@ -17,6 +17,7 @@ import com.jaoafa.mymaid4.Main; import com.jaoafa.mymaid4.lib.CommandPremise; import com.jaoafa.mymaid4.lib.MyMaidCommand; +import com.jaoafa.mymaid4.lib.MyMaidData; import com.jaoafa.mymaid4.lib.MyMaidLibrary; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.JoinConfiguration; @@ -31,6 +32,7 @@ import org.bukkit.inventory.ItemStack; import java.io.File; +import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -78,6 +80,11 @@ void jaoBox(CommandContext context) { void jaoBoxRegister(CommandContext context) { Player player = (Player) context.getSender(); + if (!isAMR(player)) { + SendMessage(player, details(), "あなたにはjaoBoxを登録モードで開く権限がありません。"); + return; + } + openBox(player, registerTitleComponent); SendMessage(player, details(), "jaoBoxを登録モードで開きました。Escキーで閉じると保存します。"); } @@ -96,10 +103,13 @@ public static void openBox(Player player, Component title) { @SuppressWarnings("unchecked") // 未チェックのキャスト。YamlConfigurationの仕様によるもの ItemStack[] items = ((List) Objects.requireNonNull(yaml.get("items"))).toArray(new ItemStack[0]); inventory.setContents(items); + + if (title == registerTitleComponent) { + MyMaidData.setBoxPrevious(player.getUniqueId(), Arrays.stream(items).toList()); + } } 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 index d3f20c60e..0d61e5983 100644 --- a/src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java +++ b/src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java @@ -11,9 +11,12 @@ package com.jaoafa.mymaid4.event; +import com.jaoafa.mymaid4.Main; import com.jaoafa.mymaid4.command.Cmd_jaoBox; import com.jaoafa.mymaid4.lib.EventPremise; +import com.jaoafa.mymaid4.lib.MyMaidData; import com.jaoafa.mymaid4.lib.MyMaidLibrary; +import com.jaoafa.mymaid4.lib.NMSManager; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -31,10 +34,16 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import javax.annotation.Nullable; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.util.Arrays; import java.util.List; +import java.util.Objects; public class Event_jaoBox extends MyMaidLibrary implements Listener, EventPremise { @Override @@ -52,6 +61,7 @@ public void onRegisterInventoryClose(InventoryCloseEvent event) { if (event.getView().title() != Cmd_jaoBox.registerTitleComponent) { return; } + Inventory inventory = event.getInventory(); YamlConfiguration yaml = new YamlConfiguration(); yaml.set("items", inventory.getContents()); @@ -60,10 +70,41 @@ public void onRegisterInventoryClose(InventoryCloseEvent event) { } catch (IOException e) { e.printStackTrace(); } + + List oldItems = MyMaidData.getBoxPrevious(player.getUniqueId()); + for (int i = 0; i < inventory.getSize(); i++) { + ItemStack oldItem = oldItems.get(i); + ItemStack newItem = inventory.getItem(i); + + if (Objects.equals(oldItem, newItem)) { + continue; + } + + logging(player, i, oldItem, newItem); + } + player.getWorld().playSound(player.getLocation(), Sound.BLOCK_CHEST_LOCKED, 1.0F, 1.0F); event.getPlayer().sendMessage("[jaoBox] " + ChatColor.GREEN + "jaoBoxを更新しました。"); } + private void logging(Player player, int slot, @Nullable ItemStack oldItem, @Nullable ItemStack newItem) { + Path jaoBoxLogPath = Path.of(Main.getJavaPlugin().getDataFolder().getPath(), "jaoBoxLog.tsv"); + String message = String.join("\t", + player.getName(), + player.getUniqueId().toString(), + Integer.toString(slot), + oldItem != null ? oldItem.getType().toString() : null, + oldItem != null ? NMSManager.getNBT(oldItem) : null, + newItem != null ? newItem.getType().toString() : null, + newItem != null ? NMSManager.getNBT(newItem) : null + ); + try { + Files.writeString(jaoBoxLogPath, message + "\n", StandardOpenOption.CREATE, StandardOpenOption.APPEND); + } catch (IOException e) { + e.printStackTrace(); + } + } + @EventHandler(priority = EventPriority.HIGHEST) public void onClickBox(PlayerInteractEvent event) { Player player = event.getPlayer(); diff --git a/src/main/java/com/jaoafa/mymaid4/lib/MyMaidData.java b/src/main/java/com/jaoafa/mymaid4/lib/MyMaidData.java index 9c5cd012e..adbe236d3 100644 --- a/src/main/java/com/jaoafa/mymaid4/lib/MyMaidData.java +++ b/src/main/java/com/jaoafa/mymaid4/lib/MyMaidData.java @@ -15,6 +15,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.json.JSONObject; import javax.annotation.Nullable; @@ -45,6 +46,7 @@ public class MyMaidData { private static CarrierPigeon carrierPigeon = null; private static Blacklist blacklist; private static Map> creativeInventoryWithNBTs = new HashMap<>(); // material : nbt + private static final Map> jaoBoxPrevious = new HashMap<>(); @Nullable public static TextChannel getReportChannel() { @@ -250,4 +252,15 @@ public static void setCreativeInventoryWithNBTs(Map> crea public static VariableManager getVariableManager() { return variableManager; } + + public static void setBoxPrevious(UUID uuid, List items) { + jaoBoxPrevious.put(uuid, items); + } + + public static List getBoxPrevious(UUID uuid) { + if (!jaoBoxPrevious.containsKey(uuid)) { + return new ArrayList<>(); + } + return jaoBoxPrevious.get(uuid); + } }