From 83b9559dbc5c0982b0b482b4a69ad5ac16dd2264 Mon Sep 17 00:00:00 2001 From: Nico Date: Mon, 15 Jul 2024 10:15:44 +0200 Subject: [PATCH] downgrade to Java 8, fix AddonProtocols in 1.8 --- .../api/AbstractProtocolService.java | 2 +- .../net/labymod/serverapi/api/Protocol.java | 90 +++++++++++++++++-- .../serverapi/api/ProtocolRegistry.java | 1 + .../component/ServerAPIBaseComponent.java | 3 +- .../component/ServerAPITextComponent.java | 3 +- .../api/payload/io/PayloadWriter.java | 3 +- build.gradle.kts | 4 +- .../core/AbstractLabyModProtocolService.java | 11 --- .../labymod/serverapi/core/AddonProtocol.java | 2 +- .../core/model/display/EconomyDisplay.java | 6 +- .../core/model/feature/DiscordRPC.java | 5 +- .../serverapi/core/model/feature/Emote.java | 3 +- .../model/feature/InteractionMenuEntry.java | 3 +- .../core/model/moderation/Permission.java | 3 +- .../model/moderation/RecommendedAddon.java | 3 +- .../core/model/supplement/InputPrompt.java | 3 +- .../model/supplement/ServerSwitchPrompt.java | 3 +- .../DefaultPluginMessageListener.java | 3 +- server/common/build.gradle.kts | 4 +- server/velocity/build.gradle.kts | 5 ++ 20 files changed, 121 insertions(+), 39 deletions(-) diff --git a/api/src/main/java/net/labymod/serverapi/api/AbstractProtocolService.java b/api/src/main/java/net/labymod/serverapi/api/AbstractProtocolService.java index 755f059..98a6b6c 100644 --- a/api/src/main/java/net/labymod/serverapi/api/AbstractProtocolService.java +++ b/api/src/main/java/net/labymod/serverapi/api/AbstractProtocolService.java @@ -125,7 +125,7 @@ public void afterPacketSent( // NO-OP } - protected enum Side { + public enum Side { CLIENT(Direction.CLIENTBOUND), SERVER(Direction.SERVERBOUND), ; diff --git a/api/src/main/java/net/labymod/serverapi/api/Protocol.java b/api/src/main/java/net/labymod/serverapi/api/Protocol.java index 95d5997..924e0a9 100644 --- a/api/src/main/java/net/labymod/serverapi/api/Protocol.java +++ b/api/src/main/java/net/labymod/serverapi/api/Protocol.java @@ -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."); + } } /** @@ -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) @@ -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, @@ -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 { diff --git a/api/src/main/java/net/labymod/serverapi/api/ProtocolRegistry.java b/api/src/main/java/net/labymod/serverapi/api/ProtocolRegistry.java index 557b839..80e1ab5 100644 --- a/api/src/main/java/net/labymod/serverapi/api/ProtocolRegistry.java +++ b/api/src/main/java/net/labymod/serverapi/api/ProtocolRegistry.java @@ -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 registerListener : this.registerListeners) { registerListener.accept(protocol); diff --git a/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPIBaseComponent.java b/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPIBaseComponent.java index bdc0abf..f72dd25 100644 --- a/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPIBaseComponent.java +++ b/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPIBaseComponent.java @@ -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); } diff --git a/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPITextComponent.java b/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPITextComponent.java index b39c59f..573b477 100644 --- a/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPITextComponent.java +++ b/api/src/main/java/net/labymod/serverapi/api/model/component/ServerAPITextComponent.java @@ -60,7 +60,7 @@ public boolean equals(Object o) { return true; } - if (!(o instanceof ServerAPITextComponent that)) { + if (!(o instanceof ServerAPITextComponent)) { return false; } @@ -68,6 +68,7 @@ public boolean equals(Object o) { return false; } + ServerAPITextComponent that = (ServerAPITextComponent) o; return Objects.equals(this.text, that.text); } diff --git a/api/src/main/java/net/labymod/serverapi/api/payload/io/PayloadWriter.java b/api/src/main/java/net/labymod/serverapi/api/payload/io/PayloadWriter.java index 40daaa7..71ca007 100644 --- a/api/src/main/java/net/labymod/serverapi/api/payload/io/PayloadWriter.java +++ b/api/src/main/java/net/labymod/serverapi/api/payload/io/PayloadWriter.java @@ -187,7 +187,8 @@ public void writeOptionalString(@Nullable String value) { public void writeComponent(@NotNull ServerAPIComponent component) { byte id = 0; Consumer 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()); diff --git a/build.gradle.kts b/build.gradle.kts index 426ede9..7f6751d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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) { diff --git a/core/src/main/java/net/labymod/serverapi/core/AbstractLabyModProtocolService.java b/core/src/main/java/net/labymod/serverapi/core/AbstractLabyModProtocolService.java index f6f810d..0dbdbfa 100644 --- a/core/src/main/java/net/labymod/serverapi/core/AbstractLabyModProtocolService.java +++ b/core/src/main/java/net/labymod/serverapi/core/AbstractLabyModProtocolService.java @@ -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 serviceLoader; if (classLoader == null) { serviceLoader = ServiceLoader.load( @@ -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."); } /** diff --git a/core/src/main/java/net/labymod/serverapi/core/AddonProtocol.java b/core/src/main/java/net/labymod/serverapi/core/AddonProtocol.java index e1a8885..56d4527 100644 --- a/core/src/main/java/net/labymod/serverapi/core/AddonProtocol.java +++ b/core/src/main/java/net/labymod/serverapi/core/AddonProtocol.java @@ -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; diff --git a/core/src/main/java/net/labymod/serverapi/core/model/display/EconomyDisplay.java b/core/src/main/java/net/labymod/serverapi/core/model/display/EconomyDisplay.java index b19dd5b..d601ca4 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/display/EconomyDisplay.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/display/EconomyDisplay.java @@ -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); @@ -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); } diff --git a/core/src/main/java/net/labymod/serverapi/core/model/feature/DiscordRPC.java b/core/src/main/java/net/labymod/serverapi/core/model/feature/DiscordRPC.java index 0f8f85f..f7f381a 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/feature/DiscordRPC.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/feature/DiscordRPC.java @@ -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); } diff --git a/core/src/main/java/net/labymod/serverapi/core/model/feature/Emote.java b/core/src/main/java/net/labymod/serverapi/core/model/feature/Emote.java index e43f2d7..53f2472 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/feature/Emote.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/feature/Emote.java @@ -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); } diff --git a/core/src/main/java/net/labymod/serverapi/core/model/feature/InteractionMenuEntry.java b/core/src/main/java/net/labymod/serverapi/core/model/feature/InteractionMenuEntry.java index 681ddc6..751e22d 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/feature/InteractionMenuEntry.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/feature/InteractionMenuEntry.java @@ -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); diff --git a/core/src/main/java/net/labymod/serverapi/core/model/moderation/Permission.java b/core/src/main/java/net/labymod/serverapi/core/model/moderation/Permission.java index 7e7ea98..3dfc51d 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/moderation/Permission.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/moderation/Permission.java @@ -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); } diff --git a/core/src/main/java/net/labymod/serverapi/core/model/moderation/RecommendedAddon.java b/core/src/main/java/net/labymod/serverapi/core/model/moderation/RecommendedAddon.java index 6b6ed16..bfb1b02 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/moderation/RecommendedAddon.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/moderation/RecommendedAddon.java @@ -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); } diff --git a/core/src/main/java/net/labymod/serverapi/core/model/supplement/InputPrompt.java b/core/src/main/java/net/labymod/serverapi/core/model/supplement/InputPrompt.java index 2236090..3c4e7e5 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/supplement/InputPrompt.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/supplement/InputPrompt.java @@ -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) diff --git a/core/src/main/java/net/labymod/serverapi/core/model/supplement/ServerSwitchPrompt.java b/core/src/main/java/net/labymod/serverapi/core/model/supplement/ServerSwitchPrompt.java index e69b925..1c76664 100644 --- a/core/src/main/java/net/labymod/serverapi/core/model/supplement/ServerSwitchPrompt.java +++ b/core/src/main/java/net/labymod/serverapi/core/model/supplement/ServerSwitchPrompt.java @@ -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); } diff --git a/server/bungeecord/src/main/java/net/labymod/serverapi/server/bungeecord/listener/DefaultPluginMessageListener.java b/server/bungeecord/src/main/java/net/labymod/serverapi/server/bungeecord/listener/DefaultPluginMessageListener.java index 3383ee3..4cf997f 100644 --- a/server/bungeecord/src/main/java/net/labymod/serverapi/server/bungeecord/listener/DefaultPluginMessageListener.java +++ b/server/bungeecord/src/main/java/net/labymod/serverapi/server/bungeecord/listener/DefaultPluginMessageListener.java @@ -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; diff --git a/server/common/build.gradle.kts b/server/common/build.gradle.kts index 4497e78..3fbb74e 100644 --- a/server/common/build.gradle.kts +++ b/server/common/build.gradle.kts @@ -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") } diff --git a/server/velocity/build.gradle.kts b/server/velocity/build.gradle.kts index 0a2b898..73cecc8 100644 --- a/server/velocity/build.gradle.kts +++ b/server/velocity/build.gradle.kts @@ -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/") }