Skip to content

Commit

Permalink
Update to ViaVersion 3.0 for 1.16 support
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR committed Jun 25, 2020
1 parent 647bcc8 commit ee4b60e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
4 changes: 2 additions & 2 deletions bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>fr.mrmicky</groupId>
<artifactId>viachatfixer</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</parent>

<artifactId>viachatfixer-bukkit</artifactId>
Expand All @@ -17,7 +17,7 @@
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>

Expand Down
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>fr.mrmicky</groupId>
<artifactId>viachatfixer</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</parent>

<artifactId>viachatfixer-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,73 @@

import fr.mrmicky.viachatfixer.ViaChatFixerPlatform;
import fr.mrmicky.viachatfixer.handlers.ChatHandler;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_11to1_10.Protocol1_11To1_10;
import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.ServerboundPackets1_9_3;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class ViaChatHandler implements ChatHandler {

private final Set<UUID> unknownPlayers = new HashSet<>();

private ViaChatFixerPlatform platform;
private final ViaChatFixerPlatform platform;

public ViaChatHandler(ViaChatFixerPlatform platform) {
this.platform = platform;
}

@Override
public void init() throws Exception {
public void init() {
if (ProtocolRegistry.SERVER_PROTOCOL >= ProtocolVersion.v1_11.getId()) {
platform.getLogger().warning("This plugin is not required on 1.11+ servers, you can just remove it :)");
return;
}

Field registryMapField = ProtocolRegistry.class.getDeclaredField("registryMap");
registryMapField.setAccessible(true);

//noinspection unchecked
Map<Integer, Map<Integer, Protocol>> registryMap = (Map<Integer, Map<Integer, Protocol>>) registryMapField.get(null);

Protocol protocol = null;
for (Map<Integer, Protocol> protocolMap : registryMap.values()) {
for (Protocol prot : protocolMap.values()) {
if (prot instanceof Protocol1_11To1_10) {
protocol = prot;
break;
}
}
}
Protocol<?, ?, ?, ServerboundPackets1_9_3> protocol = ProtocolRegistry.getProtocol(Protocol1_11To1_10.class);

if (protocol == null) {
throw new RuntimeException("Protocol 1_11To1_10 not found");
throw new IllegalStateException("Protocol 1_11To1_10 not found");
}

protocol.registerIncoming(State.PLAY, 0x02, 0x02, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.STRING); // 0 - Message
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
String msg = wrapper.get(Type.STRING, 0);
handler(wrapper -> {
// 100 character limit on older servers
String message = wrapper.get(Type.STRING, 0);

if (msg.length() > 100) {
wrapper.set(Type.STRING, 0, msg.substring(0, 100));
if (message.length() <= 100) {
return;
}

UserConnection connection = wrapper.user();
ChatTracker chatTracker = connection.get(ChatTracker.class);
wrapper.set(Type.STRING, 0, message.substring(0, 100));

if (chatTracker == null) {
chatTracker = new ChatTracker(connection);
connection.put(chatTracker);
}
UserConnection connection = wrapper.user();
ChatTracker chatTracker = connection.get(ChatTracker.class);

// don't allow messages longer than 256 characters
if (msg.length() > 256) {
msg = msg.substring(0, 256);
}
if (chatTracker == null) {
chatTracker = new ChatTracker(connection);
connection.put(chatTracker);
}

chatTracker.updateLastMessage(msg);
}
// don't allow messages longer than 256 characters
if (message.length() > 256) {
message = message.substring(0, 256);
}

chatTracker.updateLastMessage(message);
});
}
}, true);
Expand All @@ -102,17 +87,19 @@ public String handle(UUID uuid) {

ChatTracker chatTracker = connection.get(ChatTracker.class);

if (chatTracker != null && chatTracker.getLastMessage() != null) {
if (!chatTracker.isValid(100)) {
chatTracker.reset();
return null;
}
if (chatTracker == null || chatTracker.getLastMessage() == null) {
return null;
}

String message = chatTracker.getLastMessage();
if (!chatTracker.isValid(100)) {
chatTracker.reset();
return message;
return null;
}

return null;
String message = chatTracker.getLastMessage();

chatTracker.reset();

return message;
}
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.mrmicky</groupId>
<artifactId>viachatfixer</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
<packaging>pom</packaging>

<name>ViaChatFixer</name>
Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>us.myles</groupId>
<artifactId>viaversion</artifactId>
<version>2.1.3</version>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion universal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>fr.mrmicky</groupId>
<artifactId>viachatfixer</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</parent>

<artifactId>viachatfixer-universal</artifactId>
Expand Down

0 comments on commit ee4b60e

Please sign in to comment.