|
| 1 | +package pw.kaboom.commandspy; |
| 2 | + |
| 3 | +import com.google.common.io.Files; |
| 4 | +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; |
| 5 | +import net.kyori.adventure.text.Component; |
| 6 | +import org.bukkit.Bukkit; |
| 7 | +import org.bukkit.entity.Player; |
| 8 | +import org.bukkit.plugin.java.JavaPlugin; |
| 9 | +import org.jetbrains.annotations.NotNull; |
| 10 | +import org.slf4j.Logger; |
| 11 | + |
| 12 | +import java.io.*; |
| 13 | +import java.nio.ByteBuffer; |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.UUID; |
| 16 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 17 | +import java.util.concurrent.locks.StampedLock; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +public final class CommandSpyState { |
| 21 | + private static final Logger LOGGER = JavaPlugin.getPlugin(Main.class).getSLF4JLogger(); |
| 22 | + |
| 23 | + private final ObjectOpenHashSet<UUID> users = new ObjectOpenHashSet<>(); |
| 24 | + private final StampedLock usersLock = new StampedLock(); |
| 25 | + private final AtomicBoolean dirty = new AtomicBoolean(); |
| 26 | + private final File file; |
| 27 | + |
| 28 | + public CommandSpyState(final @NotNull File file) { |
| 29 | + this.file = file; |
| 30 | + |
| 31 | + try { |
| 32 | + this.load(); |
| 33 | + } catch (final FileNotFoundException exception) { |
| 34 | + try { |
| 35 | + this.save(); // Create file if it doesn't exist |
| 36 | + } catch (IOException ignored) { |
| 37 | + } |
| 38 | + } catch (final IOException exception) { |
| 39 | + LOGGER.error("Failed to load state file:", exception); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private void load() throws IOException { |
| 44 | + final InputStream reader = new BufferedInputStream(new FileInputStream(this.file)); |
| 45 | + |
| 46 | + int read; |
| 47 | + final ByteBuffer buffer = ByteBuffer.wrap(new byte[16]); |
| 48 | + |
| 49 | + // Loop until we read less than 16 bytes |
| 50 | + while ((read = reader.readNBytes(buffer.array(), 0, 16)) == 16) { |
| 51 | + this.users.add(new UUID(buffer.getLong(0), buffer.getLong(8))); |
| 52 | + } |
| 53 | + |
| 54 | + reader.close(); |
| 55 | + if (read != 0) { |
| 56 | + throw new IOException("Found " + read + " bytes extra whilst reading file"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void save() throws IOException { |
| 61 | + Files.createParentDirs(this.file); |
| 62 | + final OutputStream writer = new BufferedOutputStream(new FileOutputStream(this.file)); |
| 63 | + final ByteBuffer buffer = ByteBuffer.wrap(new byte[16]); |
| 64 | + |
| 65 | + final long stamp = this.usersLock.readLock(); |
| 66 | + for (final UUID uuid : this.users) { |
| 67 | + buffer.putLong(0, uuid.getMostSignificantBits()); |
| 68 | + buffer.putLong(8, uuid.getLeastSignificantBits()); |
| 69 | + writer.write(buffer.array()); |
| 70 | + } |
| 71 | + this.usersLock.unlockRead(stamp); |
| 72 | + |
| 73 | + writer.flush(); |
| 74 | + writer.close(); |
| 75 | + } |
| 76 | + |
| 77 | + public void trySave() { |
| 78 | + // If the state is not dirty, then we don't need to do anything. |
| 79 | + if (!this.dirty.compareAndExchange(true, false)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + try { |
| 84 | + this.save(); |
| 85 | + } catch (final IOException exception) { |
| 86 | + LOGGER.error("Failed to save state file:", exception); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public boolean getCommandSpyState(final @NotNull UUID playerUUID) { |
| 91 | + final long stamp = this.usersLock.readLock(); |
| 92 | + final boolean result = this.users.contains(playerUUID); |
| 93 | + this.usersLock.unlockRead(stamp); |
| 94 | + |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + public void setCommandSpyState(final @NotNull UUID playerUUID, final boolean state) { |
| 99 | + final long stamp = this.usersLock.writeLock(); |
| 100 | + |
| 101 | + final boolean dirty; |
| 102 | + if (state) { |
| 103 | + dirty = this.users.add(playerUUID); |
| 104 | + } else { |
| 105 | + dirty = this.users.remove(playerUUID); |
| 106 | + } |
| 107 | + |
| 108 | + this.usersLock.unlockWrite(stamp); |
| 109 | + if (dirty) { |
| 110 | + this.dirty.set(true); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public void broadcastSpyMessage(final @NotNull Component message) { |
| 115 | + // Raw access here, so we can get more performance by not locking/unlocking over and over |
| 116 | + final long stamp = this.usersLock.readLock(); |
| 117 | + final Collection<Player> players = Bukkit.getOnlinePlayers() |
| 118 | + .stream() |
| 119 | + .filter(p -> this.users.contains(p.getUniqueId())) |
| 120 | + .collect(Collectors.toUnmodifiableSet()); |
| 121 | + this.usersLock.unlockRead(stamp); |
| 122 | + |
| 123 | + for (final Player recipient : players) { |
| 124 | + recipient.sendMessage(message); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments