diff --git a/src/main/java/de/shiewk/smoderation/SModeration.java b/src/main/java/de/shiewk/smoderation/SModeration.java index 9749d25..60338a8 100644 --- a/src/main/java/de/shiewk/smoderation/SModeration.java +++ b/src/main/java/de/shiewk/smoderation/SModeration.java @@ -1,9 +1,12 @@ package de.shiewk.smoderation; +import de.shiewk.smoderation.storage.PunishmentContainer; import org.bukkit.plugin.java.JavaPlugin; public final class SModeration extends JavaPlugin { + public static final PunishmentContainer container = new PunishmentContainer(); + @Override public void onEnable() { // Plugin startup logic diff --git a/src/main/java/de/shiewk/smoderation/punishments/Punishment.java b/src/main/java/de/shiewk/smoderation/punishments/Punishment.java new file mode 100644 index 0000000..c0fa0c4 --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/punishments/Punishment.java @@ -0,0 +1,81 @@ +package de.shiewk.smoderation.punishments; + +import de.shiewk.smoderation.util.ByteUtil; +import org.jetbrains.annotations.NotNull; + +import java.nio.ByteBuffer; +import java.util.UUID; + +public class Punishment { + public final PunishmentType type; + public final long time; + public final long until; + public final UUID by; + public final UUID to; + + protected Punishment(PunishmentType type, long time, long until, UUID by, UUID to) { + this.type = type; + this.time = time; + this.until = until; + this.by = by; + this.to = to; + } + + public static Punishment mute(long time, long until, UUID by, UUID to){ + return new Punishment(PunishmentType.MUTE, time, until, by, to); + } + + public static Punishment ban(long time, long until, UUID by, UUID to){ + return new Punishment(PunishmentType.BAN, time, until, by, to); + } + + public static Punishment kick(long time, UUID by, UUID to){ + return new Punishment(PunishmentType.KICK, time, time, by, to); + } + + public static final int BUFFER_LENGTH = 52; + + public byte[] toBytes(){ + ByteBuffer buffer = ByteBuffer.allocate(BUFFER_LENGTH); + buffer.putInt(0, type.ordinal()); + buffer.putLong(4, time); + buffer.putLong(12, until); + buffer.put(20, ByteUtil.uuidToBytes(by)); + buffer.put(36, ByteUtil.uuidToBytes(to)); + return buffer.array(); + } + + public static @NotNull Punishment fromBytes(byte[] bytes){ + if (bytes.length != BUFFER_LENGTH){ + throw new IllegalArgumentException("the array has to be %s in length".formatted(BUFFER_LENGTH)); + } + ByteBuffer buffer = ByteBuffer.wrap(bytes); + final int ptypeIndex = buffer.getInt(0); + PunishmentType ptype; + try { + ptype = PunishmentType.values()[ptypeIndex]; + } catch (IndexOutOfBoundsException e){ + throw new IllegalArgumentException("The punishment type %s does not exist.".formatted(ptypeIndex)); + } + final long time = buffer.getLong(4); + final long until = buffer.getLong(12); + final byte[] byBytes = new byte[16]; + System.arraycopy(bytes, 20, byBytes, 0, 16); + final byte[] toBytes = new byte[16]; + System.arraycopy(bytes, 36, toBytes, 0, 16); + final UUID by = ByteUtil.bytesToUuid(byBytes); + final UUID to = ByteUtil.bytesToUuid(toBytes); + return new Punishment(ptype, time, until, by, to); + } + + @Override + public String toString() { + return "Punishment{" + + "type=" + type + + ", time=" + time + + ", until=" + until + + ", by=" + by + + ", to=" + to + + '}'; + } +} diff --git a/src/main/java/de/shiewk/smoderation/punishments/PunishmentType.java b/src/main/java/de/shiewk/smoderation/punishments/PunishmentType.java new file mode 100644 index 0000000..3197474 --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/punishments/PunishmentType.java @@ -0,0 +1,7 @@ +package de.shiewk.smoderation.punishments; + +public enum PunishmentType { + MUTE, + KICK, + BAN +} diff --git a/src/main/java/de/shiewk/smoderation/storage/PunishmentContainer.java b/src/main/java/de/shiewk/smoderation/storage/PunishmentContainer.java new file mode 100644 index 0000000..1299b00 --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/storage/PunishmentContainer.java @@ -0,0 +1,39 @@ +package de.shiewk.smoderation.storage; + +import de.shiewk.smoderation.punishments.Punishment; +import org.jetbrains.annotations.Nullable; + +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.function.Predicate; + +public class PunishmentContainer { + + private final CopyOnWriteArrayList punishments = new CopyOnWriteArrayList<>(); + + public PunishmentContainer(){} + + public void add(Punishment punishment){ + punishments.add(punishment); + } + + public @Nullable Punishment remove(int index){ + return punishments.remove(index); + } + + public boolean remove(Punishment punishment){ + return punishments.remove(punishment); + } + + public @Nullable Punishment find(Predicate predicate){ + for (Punishment punishment : new CopyOnWriteArrayList<>(punishments)) { + if (predicate.test(punishment)){ + return punishment; + } + } + return null; + } + + public @Nullable Punishment findByTimestamp(long timestamp){ + return find(punishment -> punishment.time == timestamp); + } +} diff --git a/src/main/java/de/shiewk/smoderation/util/ByteUtil.java b/src/main/java/de/shiewk/smoderation/util/ByteUtil.java new file mode 100644 index 0000000..1eae96e --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/util/ByteUtil.java @@ -0,0 +1,45 @@ +package de.shiewk.smoderation.util; + +import java.nio.ByteBuffer; +import java.util.UUID; + + +/** + * Utility class for byte-based saving of integers, longs and UUIDs + */ +public abstract class ByteUtil { + private ByteUtil(){} + + public static byte[] longToBytes(long v){ + ByteBuffer buffer = ByteBuffer.allocate(8); + buffer.putLong(v); + return buffer.array(); + } + + public static long bytesToLong(byte[] i){ + if (i.length != 8){ + throw new IllegalArgumentException("length must be 8"); + } + ByteBuffer buffer = ByteBuffer.allocate(8); + buffer.put(0, i); + return buffer.getLong(0); + } + + public static byte[] uuidToBytes(UUID uuid){ + byte[] l = longToBytes(uuid.getLeastSignificantBits()); + byte[] m = longToBytes(uuid.getMostSignificantBits()); + return new byte[]{ + m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], + l[0], l[1], l[2], l[3], l[4], l[5], l[6], l[7] + }; + } + + public static UUID bytesToUuid(byte[] i){ + if (i.length != 16){ + throw new IllegalArgumentException("length must be 16, was " + i.length); + } + long l = bytesToLong(new byte[]{ i[8], i[9], i[10], i[11], i[12], i[13], i[14], i[15] }); + long m = bytesToLong(new byte[]{ i[0], i[1], i[2], i[3], i[4], i[5], i[6], i[7] }); + return new UUID(m, l); + } +}