1
mirror of https://github.com/Shiewk/SModeration.git synced 2026-04-28 05:54:16 +02:00

Add punishments and container

This commit is contained in:
Shy
2024-06-07 16:05:01 +02:00
parent 914ed4b2de
commit 88cb2b3cb6
5 changed files with 175 additions and 0 deletions
@@ -1,9 +1,12 @@
package de.shiewk.smoderation; package de.shiewk.smoderation;
import de.shiewk.smoderation.storage.PunishmentContainer;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public final class SModeration extends JavaPlugin { public final class SModeration extends JavaPlugin {
public static final PunishmentContainer container = new PunishmentContainer();
@Override @Override
public void onEnable() { public void onEnable() {
// Plugin startup logic // Plugin startup logic
@@ -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 +
'}';
}
}
@@ -0,0 +1,7 @@
package de.shiewk.smoderation.punishments;
public enum PunishmentType {
MUTE,
KICK,
BAN
}
@@ -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<Punishment> 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<Punishment> 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);
}
}
@@ -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);
}
}