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 #683 from book000/fix/jaobox-perms
Browse files Browse the repository at this point in the history
jaoBoxの更新ができるユーザをAMRに制限・登録情報変更ログ
  • Loading branch information
book000 authored Jan 29, 2022
2 parents 8cdd4fa + 6593f78 commit 5d91705
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/com/jaoafa/mymaid4/command/Cmd_jaoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -78,6 +80,11 @@ void jaoBox(CommandContext<CommandSender> context) {
void jaoBoxRegister(CommandContext<CommandSender> context) {
Player player = (Player) context.getSender();

if (!isAMR(player)) {
SendMessage(player, details(), "あなたにはjaoBoxを登録モードで開く権限がありません。");
return;
}

openBox(player, registerTitleComponent);
SendMessage(player, details(), "jaoBoxを登録モードで開きました。Escキーで閉じると保存します。");
}
Expand All @@ -96,10 +103,13 @@ public static void openBox(Player player, Component title) {
@SuppressWarnings("unchecked") // 未チェックのキャスト。YamlConfigurationの仕様によるもの
ItemStack[] items = ((List<ItemStack>) 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);

}
}
41 changes: 41 additions & 0 deletions src/main/java/com/jaoafa/mymaid4/event/Event_jaoBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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());
Expand All @@ -60,10 +70,41 @@ public void onRegisterInventoryClose(InventoryCloseEvent event) {
} catch (IOException e) {
e.printStackTrace();
}

List<ItemStack> 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();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/jaoafa/mymaid4/lib/MyMaidData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class MyMaidData {
private static CarrierPigeon carrierPigeon = null;
private static Blacklist blacklist;
private static Map<Material, List<String>> creativeInventoryWithNBTs = new HashMap<>(); // material : nbt
private static final Map<UUID, List<ItemStack>> jaoBoxPrevious = new HashMap<>();

@Nullable
public static TextChannel getReportChannel() {
Expand Down Expand Up @@ -250,4 +252,15 @@ public static void setCreativeInventoryWithNBTs(Map<Material, List<String>> crea
public static VariableManager getVariableManager() {
return variableManager;
}

public static void setBoxPrevious(UUID uuid, List<ItemStack> items) {
jaoBoxPrevious.put(uuid, items);
}

public static List<ItemStack> getBoxPrevious(UUID uuid) {
if (!jaoBoxPrevious.containsKey(uuid)) {
return new ArrayList<>();
}
return jaoBoxPrevious.get(uuid);
}
}

0 comments on commit 5d91705

Please sign in to comment.