Skip to content

Commit 20b22c0

Browse files
committed
recovered back to old codec system GsonCodec.java -> MessagePackCodec.java
1 parent a143135 commit 20b22c0

File tree

5 files changed

+101
-56
lines changed

5 files changed

+101
-56
lines changed

bridge/src/main/java/net/lightcode/bridge/listener/PlayerServerConnectListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public PlayerServerConnectListener(BridgePlugin plugin) {
1818

1919
@Subscribe
2020
public void onServerPreConnect(ServerPreConnectEvent event) {
21-
Player player = event.getPlayer();
22-
;
21+
final Player player = event.getPlayer();
2322

2423
Sector sector = this.plugin.sectorService().find(
2524
this.plugin.networkService().databaseConnection().sync().get(player.getUsername())

common/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@
6767
<artifactId>guava</artifactId>
6868
<version>33.5.0-jre</version>
6969
</dependency>
70+
<dependency>
71+
<groupId>org.msgpack</groupId>
72+
<artifactId>jackson-dataformat-msgpack</artifactId>
73+
<version>0.9.10</version>
74+
<scope>compile</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.fasterxml.jackson.core</groupId>
78+
<artifactId>jackson-databind</artifactId>
79+
<version>2.20.0</version>
80+
<scope>provided</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.fasterxml.jackson.core</groupId>
84+
<artifactId>jackson-core</artifactId>
85+
<version>2.20.0</version>
86+
<scope>provided</scope>
87+
</dependency>
88+
7089
<dependency>
7190
<groupId>io.lettuce</groupId>
7291
<artifactId>lettuce-core</artifactId>

common/src/main/java/net/lightcode/NetworkService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
77
import net.lightcode.packet.Packet;
88
import net.lightcode.redis.PacketListener;
9-
import net.lightcode.redis.codec.GsonCodec;
9+
import net.lightcode.redis.codec.MessagePackCodec;
1010

1111
import java.util.ArrayList;
1212
import java.util.List;
@@ -32,10 +32,10 @@ public NetworkService(String address,
3232
.withPassword(password)
3333
.build());
3434

35-
GsonCodec gsonCodec = new GsonCodec();
35+
MessagePackCodec messagePackCodec = new MessagePackCodec();
3636

37-
this.pubSubConnection = redisClient.connectPubSub(gsonCodec);
38-
this.connection = redisClient.connect(gsonCodec);
37+
this.pubSubConnection = redisClient.connectPubSub(messagePackCodec);
38+
this.connection = redisClient.connect(messagePackCodec);
3939
this.databaseConnection = redisClient.connect();
4040
}
4141

common/src/main/java/net/lightcode/redis/codec/GsonCodec.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package net.lightcode.redis.codec;
2+
3+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.core.JsonGenerator;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.DeserializationFeature;
8+
import com.fasterxml.jackson.databind.MapperFeature;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.databind.SerializationFeature;
11+
import io.lettuce.core.codec.RedisCodec;
12+
import net.lightcode.packet.Packet;
13+
import org.msgpack.jackson.dataformat.MessagePackFactory;
14+
15+
import java.io.IOException;
16+
import java.nio.ByteBuffer;
17+
import java.nio.charset.Charset;
18+
import java.nio.charset.StandardCharsets;
19+
20+
public class MessagePackCodec implements RedisCodec<String, Packet> {
21+
22+
private final Charset charset = StandardCharsets.UTF_8;
23+
24+
private final ObjectMapper objectMapper;
25+
26+
public MessagePackCodec() {
27+
this.objectMapper = new ObjectMapper(new MessagePackFactory());
28+
29+
this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
30+
this.objectMapper.setVisibility(
31+
this.objectMapper
32+
.getSerializationConfig()
33+
.getDefaultVisibilityChecker()
34+
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
35+
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
36+
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
37+
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
38+
this.objectMapper
39+
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
40+
.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN)
41+
.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
42+
.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
43+
44+
this.objectMapper.registerSubtypes(Packet.class);
45+
}
46+
47+
@Override
48+
public String decodeKey(ByteBuffer bytes) {
49+
return charset.decode(bytes).toString();
50+
}
51+
52+
@Override
53+
public Packet decodeValue(ByteBuffer bytes) {
54+
byte[] buffer = new byte[bytes.remaining()];
55+
bytes.get(buffer);
56+
57+
try {
58+
return this.objectMapper.readValue(buffer, Packet.class);
59+
} catch (IOException exception) {
60+
throw new RuntimeException(exception);
61+
}
62+
}
63+
64+
@Override
65+
public ByteBuffer encodeKey(String key) {
66+
return charset.encode(key);
67+
}
68+
69+
@Override
70+
public ByteBuffer encodeValue(Packet value) {
71+
try {
72+
return ByteBuffer.wrap(this.objectMapper.writeValueAsBytes(value));
73+
} catch (JsonProcessingException exception) {
74+
throw new RuntimeException(exception);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)