Skip to content

Commit

Permalink
Add punish packet
Browse files Browse the repository at this point in the history
  • Loading branch information
acrylic-style committed Nov 11, 2024
1 parent 4558cbc commit f498c12
Show file tree
Hide file tree
Showing 21 changed files with 710 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.azisaba.azipluginmessaging.api.entity.Player;
import net.azisaba.azipluginmessaging.api.entity.PlayerAdapter;
import net.azisaba.azipluginmessaging.api.protocol.PacketQueue;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundPunishMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ServerboundCheckRankExpirationMessage;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import net.azisaba.azipluginmessaging.api.util.SQLThrowableConsumer;
Expand Down Expand Up @@ -92,6 +93,10 @@ default void loadConfig(@NotNull YamlObject obj) {
default void checkRankAsync(@NotNull UUID uuid) {
throw new UnsupportedOperationException("Unsupported in current environment.");
}

default void handle(@NotNull ProxyboundPunishMessage msg) {
throw new UnsupportedOperationException("Unsupported in current environment.");
}
}

interface Server {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.azisaba.azipluginmessaging.api;

import net.azisaba.azipluginmessaging.api.yaml.YamlArray;
import net.azisaba.azipluginmessaging.api.yaml.YamlConfiguration;
import net.azisaba.azipluginmessaging.api.yaml.YamlObject;
import org.jetbrains.annotations.NotNull;
Expand All @@ -11,6 +12,7 @@
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class AziPluginMessagingConfig {
Expand All @@ -23,6 +25,7 @@ public class AziPluginMessagingConfig {
public static final Map<String, String> servers = new ConcurrentHashMap<>();
public static final Map<String, String> saraShowServers = new ConcurrentHashMap<>();
public static final Map<String, String> rankableServers = new ConcurrentHashMap<>();
public static final Set<String> unpunishableServers = ConcurrentHashMap.newKeySet();
@SuppressWarnings("DeprecatedIsStillUsed") // not used outside this class
@Deprecated
public static final Map<String, String> contextualServers = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -97,13 +100,17 @@ public static void reload() {
" life: life",
" lifepve: life",
"",
"# List of servers that ProxyboundPunishPacket is not allowed on.",
"unpunishableServers: # this is meaningless in spigot",
" - life9000",
"",
"# Database settings (required)",
"# This setting is used for setting the rank temporary.",
"database:",
" # (scheme)://(host):(port)/(database)",
" # Keep the line commented out unless you get an obvious error message indicating that the driver was not found.",
" # Default driver (net.azisaba.taxoffice.libs.org.mariadb.jdbc.Driver) points to the bundled MariaDB driver in the TaxOffice jar.",
" #driver: net.azisaba.taxoffice.libs.org.mariadb.jdbc.Driver",
" #driver: net.azisaba.azipluginmessaging.libs.org.mariadb.jdbc.Driver",
"",
" # change to jdbc:mysql if you want to use MySQL instead of MariaDB",
" scheme: jdbc:mariadb",
Expand Down Expand Up @@ -139,6 +146,7 @@ public static void reload() {
readMap(servers, obj, "servers");
readMap(rankableServers, obj, "rankableServers");
readMap(saraShowServers, obj, "saraShowServers");
readSet(unpunishableServers, obj, "unpunishableServers");
AziPluginMessagingProvider.get().getProxy().loadConfig(obj);
}
} catch (IOException ex) {
Expand All @@ -159,4 +167,11 @@ private static void readMap(@NotNull Map<String, String> to, @NotNull YamlObject
mapObject.getRawData().forEach((key, value) -> to.put(key, String.valueOf(value)));
}
}

private static void readSet(@NotNull Set<String> to, @NotNull YamlObject obj, @NotNull String configKey) {
YamlArray array = obj.getArray(configKey);
if (array != null) {
to.addAll(array.mapToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public boolean isChallengeEquals(@NotNull String challenge) {
return false;
}

@Override
public String toString() {
return "SimplePlayer{" +
"uuid=" + uuid +
", username='" + username + '\'' +
'}';
}

@Contract("_ -> new")
@NotNull
public static SimplePlayer read(@NotNull DataInputStream in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,8 @@
import net.azisaba.azipluginmessaging.api.AziPluginMessagingConfig;
import net.azisaba.azipluginmessaging.api.AziPluginMessagingProvider;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.protocol.handler.MessageHandler;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyMessageHandler;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundCheckRankExpirationPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundClearPrefixPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundEncryptionPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundEncryptionResponsePacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundGiveGamingSaraPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundGiveNitroSaraPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundGiveSaraPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundSetPrefixPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundSetRankPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundToggleGamingSaraPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundToggleNitroSaraPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundToggleSaraHidePacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ProxyboundToggleSaraShowPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ServerMessageHandler;
import net.azisaba.azipluginmessaging.api.protocol.handler.ServerboundActionResponsePacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ServerboundCheckRankExpirationPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ServerboundEncryptionPacket;
import net.azisaba.azipluginmessaging.api.protocol.handler.ServerboundEncryptionResponsePacket;
import net.azisaba.azipluginmessaging.api.protocol.message.EmptyMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.EncryptionMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.Message;
import net.azisaba.azipluginmessaging.api.protocol.message.PlayerMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundCheckRankExpirationMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundClearPrefixMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundGiveNitroSaraMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundGiveSaraMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundSetPrefixMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundSetRankMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundToggleSaraHideMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundToggleSaraShowMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ServerboundActionResponseMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ServerboundCheckRankExpirationMessage;
import net.azisaba.azipluginmessaging.api.protocol.handler.*;
import net.azisaba.azipluginmessaging.api.protocol.message.*;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import net.azisaba.azipluginmessaging.api.server.ServerConnection;
import net.azisaba.azipluginmessaging.api.util.EncryptionUtil;
Expand Down Expand Up @@ -81,6 +49,7 @@ public final class Protocol<T extends MessageHandler<M>, M extends Message> {
public static final Protocol<ProxyboundGiveNitroSaraPacket, ProxyboundGiveNitroSaraMessage> P_GIVE_NITRO_SARA = new Protocol<>(PacketFlow.TO_PROXY, 0x0A, new ProxyboundGiveNitroSaraPacket());
public static final Protocol<ProxyboundToggleNitroSaraPacket, PlayerMessage> P_TOGGLE_NITRO_SARA = new Protocol<>(PacketFlow.TO_PROXY, 0x0B, new ProxyboundToggleNitroSaraPacket());
public static final Protocol<ProxyboundCheckRankExpirationPacket, ProxyboundCheckRankExpirationMessage> P_CHECK_RANK_EXPIRATION = new Protocol<>(PacketFlow.TO_PROXY, 0x0C, new ProxyboundCheckRankExpirationPacket());
public static final Protocol<ProxyboundPunishPacket, ProxyboundPunishMessage> P_PUNISH = new Protocol<>(PacketFlow.TO_PROXY, 0x0D, new ProxyboundPunishPacket());

public static final Protocol<ServerboundEncryptionPacket, EncryptionMessage> S_ENCRYPTION = new Protocol<>(PacketFlow.TO_SERVER, 0x00, new ServerboundEncryptionPacket());
public static final Protocol<ServerboundEncryptionResponsePacket, EmptyMessage> S_ENCRYPTION_RESPONSE = new Protocol<>(PacketFlow.TO_SERVER, 0x01, new ServerboundEncryptionResponsePacket());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.azisaba.azipluginmessaging.api.protocol.handler;

import net.azisaba.azipluginmessaging.api.AziPluginMessagingConfig;
import net.azisaba.azipluginmessaging.api.AziPluginMessagingProvider;
import net.azisaba.azipluginmessaging.api.Logger;
import net.azisaba.azipluginmessaging.api.protocol.Protocol;
import net.azisaba.azipluginmessaging.api.protocol.message.EmptyMessage;
import net.azisaba.azipluginmessaging.api.protocol.message.ProxyboundPunishMessage;
import net.azisaba.azipluginmessaging.api.server.PacketSender;
import net.azisaba.azipluginmessaging.api.server.ServerConnection;
import org.jetbrains.annotations.NotNull;

import java.io.DataInputStream;
import java.io.IOException;

public class ProxyboundPunishPacket implements ProxyMessageHandler<ProxyboundPunishMessage> {
@Override
public @NotNull ProxyboundPunishMessage read(@NotNull ServerConnection server, @NotNull DataInputStream in) throws IOException {
String serverName = server.getServerInfo().getName();
serverName = AziPluginMessagingConfig.servers.getOrDefault(serverName, serverName);
return ProxyboundPunishMessage.read(serverName, in);
}

@Override
public void handle(@NotNull PacketSender sender, @NotNull ProxyboundPunishMessage msg) throws Exception {
AziPluginMessagingProvider.get().getProxy().handle(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void write(@NotNull DataOutputStream out) throws IOException {
super.write(out);
}

@Contract("null, _ -> fail; _, _ -> new")
@Contract("_, _ -> new")
@NotNull
public static PlayerWithServerMessage read(@Nullable String server, @NotNull DataInputStream in) throws IOException {
public static PlayerWithServerMessage read(@NotNull String server, @NotNull DataInputStream in) throws IOException {
UUID uuid = UUID.fromString(in.readUTF());
String username = null;
if (in.readBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public TimeUnit getUnit() {
public void write(@NotNull DataOutputStream out) throws IOException {
super.write(out);
out.writeInt(time);
out.writeUTF(unit.name()); // ordinal might be different with other versions of java
out.writeUTF(unit.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package net.azisaba.azipluginmessaging.api.protocol.message;

import net.azisaba.azipluginmessaging.api.entity.Player;
import net.azisaba.azipluginmessaging.api.entity.SimplePlayer;
import net.azisaba.azipluginmessaging.api.punishment.PunishmentType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class ProxyboundPunishMessage extends PlayerWithServerMessage {
private final Player sender;
private final PunishmentType type;
private final String reason;
private final int time;
private final TimeUnit unit;

public ProxyboundPunishMessage(
@NotNull String server,
@NotNull Player player,
@NotNull Player sender,
@NotNull PunishmentType type,
@NotNull String reason,
int time,
@Nullable TimeUnit unit) {
super(server, player);
if (type.name().startsWith("TEMP_")) {
if (time <= 0 || unit == null) {
throw new IllegalArgumentException("Time and unit must be set for temporary punishments");
}
} else {
if (time != 0 || unit != null) {
throw new IllegalArgumentException("Time and unit must not be set for permanent punishments");
}
}
this.sender = Objects.requireNonNull(sender, "sender");
this.type = Objects.requireNonNull(type, "type");
this.reason = Objects.requireNonNull(reason, "reason");
this.time = time;
this.unit = unit;
}

public @NotNull Player getSender() {
return sender;
}

public @NotNull PunishmentType getType() {
return type;
}

public @NotNull String getReason() {
return reason;
}

/**
* Returns the duration of the punishment. If the punishment is permanent, it returns 0.
* @return the duration of the punishment
*/
public int getTime() {
return unit == null ? 0 : time;
}

/**
* Returns the unit of the duration. If the unit is null, it means the punishment is permanent.
* @return the unit of the duration
*/
public @Nullable TimeUnit getUnit() {
return unit;
}

/**
* Returns whether the punishment is temporary or not.
* @return whether the punishment is temporary or not
*/
public boolean isTemporary() {
return getTime() > 0 && unit != null;
}

/**
* Returns whether the punishment is permanent or not.
* @return whether the punishment is permanent or not
*/
public boolean isPermanent() {
return !isTemporary(); // time == 0 || unit == null
}

@Override
public void write(@NotNull DataOutputStream out) throws IOException {
super.write(out);
out.writeUTF(sender.getUniqueId().toString());
out.writeBoolean(sender.getUsername() != null);
if (sender.getUsername() != null) {
out.writeUTF(sender.getUsername());
}
out.writeUTF(type.name());
out.writeUTF(reason);
out.writeInt(time);
out.writeBoolean(unit != null);
if (unit != null) {
out.writeUTF(unit.name());
}
}

@Override
public String toString() {
return "ProxyboundPunishMessage{" +
"sender=" + sender +
", type=" + type +
", reason='" + reason + '\'' +
", time=" + time +
", unit=" + unit +
", server='" + server + '\'' +
", player=" + player +
'}';
}

public static @NotNull ProxyboundPunishMessage read(@NotNull String server, @NotNull DataInputStream in) throws IOException {
return new ProxyboundPunishMessage(
server,
SimplePlayer.read(in),
SimplePlayer.read(in),
PunishmentType.valueOf(in.readUTF()),
in.readUTF(),
in.readInt(),
in.readBoolean() ? TimeUnit.valueOf(in.readUTF()) : null
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.azisaba.azipluginmessaging.api.punishment;

public enum PunishmentType {
BAN,
TEMP_BAN,
IP_BAN,
TEMP_IP_BAN,
MUTE,
TEMP_MUTE,
IP_MUTE,
TEMP_IP_MUTE,
WARNING,
CAUTION,
KICK,
}
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ plugins {
java
`java-library`
`maven-publish`
id("com.github.johnrengelman.shadow") version "7.1.2" apply false
id("com.gradleup.shadow") version "8.3.3" apply false
}

group = "net.azisaba.azipluginmessaging"
version = "4.0.5"
version = "4.1.0"

repositories {
mavenCentral()
Expand All @@ -29,7 +29,7 @@ subprojects {
plugin("java")
plugin("java-library")
plugin("maven-publish")
plugin("com.github.johnrengelman.shadow")
plugin("com.gradleup.shadow")
}

repositories {
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit f498c12

Please sign in to comment.