Skip to content

Commit

Permalink
downgrade to Java 8, fix AddonProtocols in 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
jumpingpxl committed Jul 15, 2024
1 parent 626dda6 commit 83b9559
Show file tree
Hide file tree
Showing 20 changed files with 121 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void afterPacketSent(
// NO-OP
}

protected enum Side {
public enum Side {
CLIENT(Direction.CLIENTBOUND),
SERVER(Direction.SERVERBOUND),
;
Expand Down
90 changes: 81 additions & 9 deletions api/src/main/java/net/labymod/serverapi/api/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public Protocol(
this.protocolSide = protocolService.getSide();
this.packets = new HashSet<>();
this.awaitingResponses = new HashSet<>();

if (this.identifier.toString().length() > 20) {
this.protocolService.logger().warn("The identifier of the protocol " + this.identifier + " "
+ "is longer than 20 characters. This will cause issues with 1.8 & 1.12.2 users.");
}
}

/**
Expand Down Expand Up @@ -302,7 +307,8 @@ private ProtocolPacket getProtocolPacketById(int id) {
}

private void handlePacket(ProtocolPacket protocolPacket, UUID sender, Packet packet) {
if (packet instanceof IdentifiablePacket identifiablePacket) {
if (packet instanceof IdentifiablePacket) {
IdentifiablePacket identifiablePacket = (IdentifiablePacket) packet;
AwaitingResponse responsePacket = null;
for (AwaitingResponse awaitingResponse : this.awaitingResponses) {
if (awaitingResponse.recipient.equals(sender)
Expand Down Expand Up @@ -355,7 +361,8 @@ private void sendPacketInternal(
packet.write(writer);
this.protocolService.send(this.identifier, recipient, writer);
if (responseClass != null && responseCallback != null
&& packet instanceof IdentifiablePacket identifiablePacket) {
&& packet instanceof IdentifiablePacket) {
IdentifiablePacket identifiablePacket = (IdentifiablePacket) packet;
synchronized (this.awaitingResponses) {
this.awaitingResponses.add(new AwaitingResponse(
recipient,
Expand All @@ -370,14 +377,79 @@ private void sendPacketInternal(
this.protocolService.afterPacketSent(this, packet, recipient);
}

private record AwaitingResponse(
UUID recipient,
IdentifiablePacket initialPacket,
int identifier,
Class responseClass,
Predicate responseCallback
) {
private final class AwaitingResponse {

private final UUID recipient;
private final IdentifiablePacket initialPacket;
private final int identifier;
private final Class responseClass;
private final Predicate responseCallback;

private AwaitingResponse(
UUID recipient,
IdentifiablePacket initialPacket,
int identifier,
Class responseClass,
Predicate responseCallback
) {
this.recipient = recipient;
this.initialPacket = initialPacket;
this.identifier = identifier;
this.responseClass = responseClass;
this.responseCallback = responseCallback;
}

public UUID recipient() {
return this.recipient;
}

public IdentifiablePacket initialPacket() {
return this.initialPacket;
}

public int identifier() {
return this.identifier;
}

public Class responseClass() {
return this.responseClass;
}

public Predicate responseCallback() {
return this.responseCallback;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
AwaitingResponse that = (AwaitingResponse) obj;
return Objects.equals(this.recipient, that.recipient) &&
Objects.equals(this.initialPacket, that.initialPacket) &&
this.identifier == that.identifier &&
Objects.equals(this.responseClass, that.responseClass) &&
Objects.equals(this.responseCallback, that.responseCallback);
}

@Override
public int hashCode() {
return Objects.hash(this.recipient, this.initialPacket, this.identifier, this.responseClass,
this.responseCallback);
}

@Override
public String toString() {
return "AwaitingResponse[" +
"recipient=" + this.recipient + ", " +
"initialPacket=" + this.initialPacket + ", " +
"identifier=" + this.identifier + ", " +
"responseClass=" + this.responseClass + ", " +
"responseCallback=" + this.responseCallback + ']';
}
}

private static class ProtocolPacket {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected ProtocolRegistry() {
public void registerProtocol(@NotNull Protocol protocol) {
Objects.requireNonNull(protocol, "Protocol cannot be null");
this.protocols.add(protocol);
System.out.println("Registered protocol: " + protocol.identifier());

for (Consumer<Protocol> registerListener : this.registerListeners) {
registerListener.accept(protocol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof ServerAPIBaseComponent<?> that)) {
if (!(o instanceof ServerAPIBaseComponent<?>)) {
return false;
}

ServerAPIBaseComponent<?> that = (ServerAPIBaseComponent<?>) o;
return Objects.equals(this.textColor, that.textColor) && Objects.equals(
this.decorations, that.decorations) && Objects.equals(this.children, that.children);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof ServerAPITextComponent that)) {
if (!(o instanceof ServerAPITextComponent)) {
return false;
}

if (!super.equals(o)) {
return false;
}

ServerAPITextComponent that = (ServerAPITextComponent) o;
return Objects.equals(this.text, that.text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public void writeOptionalString(@Nullable String value) {
public void writeComponent(@NotNull ServerAPIComponent component) {
byte id = 0;
Consumer<PayloadWriter> dataWriter = null;
if (component instanceof ServerAPITextComponent textComponent) {
if (component instanceof ServerAPITextComponent) {
ServerAPITextComponent textComponent = (ServerAPITextComponent) component;
if (!textComponent.getText().isEmpty()) {
id = 1;
dataWriter = writer -> writer.writeString(textComponent.getText());
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ subprojects {
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

fun adjustArchiveFileName(property: Property<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public final LabyModProtocol labyModProtocol() {
* class loader returned by {@link #getIntegrationClassLoader()} is used.
*/
protected void loadLabyModProtocolIntegrations(@Nullable ClassLoader classLoader) {
System.out.println("Loading LabyMod Protocol Integrations...");
ServiceLoader<LabyModProtocolIntegration> serviceLoader;
if (classLoader == null) {
serviceLoader = ServiceLoader.load(
Expand All @@ -132,24 +131,14 @@ protected void loadLabyModProtocolIntegrations(@Nullable ClassLoader classLoader
serviceLoader = ServiceLoader.load(LabyModProtocolIntegration.class, classLoader);
}

int found = 0;
for (LabyModProtocolIntegration labyModProtocolIntegration : serviceLoader) {
try {
System.out.println(
"Loading LabyModProtocolIntegration: " + labyModProtocolIntegration.getClass()
.getName());
labyModProtocolIntegration.initialize(this);
this.integrations.add(labyModProtocolIntegration);
System.out.println(
"Loaded LabyModProtocolIntegration: " + labyModProtocolIntegration.getClass()
.getName());
found++;
} catch (Exception e) {
e.printStackTrace();
}
}

System.out.println("Found and loaded" + found + " Integrations.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public AddonProtocol(
) {
super(
protocolService,
PayloadChannelIdentifier.create("labymod", "neo/addon/" + namespace)
PayloadChannelIdentifier.create("labymod", namespace)
);
Objects.requireNonNull(namespace, "Namespace cannot be null");
this.namespace = namespace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof EconomyDisplay that)) {
if (!(o instanceof EconomyDisplay)) {
return false;
}

EconomyDisplay that = (EconomyDisplay) o;
return this.visible == that.visible && Double.compare(this.balance, that.balance) == 0
&& Objects.equals(this.key, that.key) && Objects.equals(this.iconUrl, that.iconUrl)
&& Objects.equals(this.decimalFormat, that.decimalFormat);
Expand Down Expand Up @@ -150,10 +151,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof DecimalFormat that)) {
if (!(o instanceof DecimalFormat)) {
return false;
}

DecimalFormat that = (DecimalFormat) o;
return Double.compare(this.divisor, that.divisor) == 0 && Objects.equals(this.format,
that.format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DiscordRPC that)) {

if (!(o instanceof DiscordRPC)) {
return false;
}

DiscordRPC that = (DiscordRPC) o;
return this.startTime == that.startTime && this.endTime == that.endTime && Objects.equals(
this.gameMode, that.gameMode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof Emote emote)) {
if (!(o instanceof Emote)) {
return false;
}

Emote emote = (Emote) o;
return this.emoteId == emote.emoteId && Objects.equals(this.uniqueId, emote.uniqueId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof InteractionMenuEntry that)) {
if (!(o instanceof InteractionMenuEntry)) {
return false;
}

InteractionMenuEntry that = (InteractionMenuEntry) o;
return Objects.equals(this.displayName, that.displayName) && this.type == that.type
&& Objects.equals(this.value, that.value) && Objects.equals(this.iconUrl,
that.iconUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof Permission that)) {
if (!(o instanceof Permission)) {
return false;
}

Permission that = (Permission) o;
return this.identifier.equals(that.identifier);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof RecommendedAddon that)) {
if (!(o instanceof RecommendedAddon)) {
return false;
}

RecommendedAddon that = (RecommendedAddon) o;
return this.required == that.required && Objects.equals(this.namespace, that.namespace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof InputPrompt that)) {
if (!(o instanceof InputPrompt)) {
return false;
}

InputPrompt that = (InputPrompt) o;
return this.maxLength == that.maxLength
&& Objects.equals(this.title, that.title)
&& Objects.equals(this.placeholder, that.placeholder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ public boolean equals(Object o) {
return true;
}

if (!(o instanceof ServerSwitchPrompt that)) {
if (!(o instanceof ServerSwitchPrompt)) {
return false;
}

ServerSwitchPrompt that = (ServerSwitchPrompt) o;
return this.showPreview == that.showPreview && Objects.equals(this.title, that.title)
&& Objects.equals(this.address, that.address);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ public DefaultPluginMessageListener(Protocol protocol) {

@EventHandler(priority = EventPriority.HIGHEST)
public void onPluginMessage(PluginMessageEvent event) {
if (!(event.getSender() instanceof ProxiedPlayer player)) {
if (!(event.getSender() instanceof ProxiedPlayer)) {
return;
}

ProxiedPlayer player = (ProxiedPlayer) event.getSender();
String channel = event.getTag();
if (!channel.equals(this.protocol.identifier().toString())) {
return;
Expand Down
4 changes: 2 additions & 2 deletions server/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ val integrationVersion = "1.0.0"

dependencies {
compile(project(":core"))
integration("net.labymod.serverapi.integration:voicechat:$integrationVersion")
integration("net.labymod.serverapi.integration:betterperspective:$integrationVersion")
//integration("net.labymod.serverapi.integration:voicechat:$integrationVersion")
//integration("net.labymod.serverapi.integration:betterperspective:$integrationVersion")
}
5 changes: 5 additions & 0 deletions server/velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ plugins {
id("java")
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

repositories {
maven("https://repo.papermc.io/repository/maven-public/")
}
Expand Down

0 comments on commit 83b9559

Please sign in to comment.