diff --git a/README.md b/README.md
index d4b23645..0d23f20b 100644
--- a/README.md
+++ b/README.md
@@ -4,6 +4,39 @@ MCProtocolLib is a simple library for communicating with a Minecraft client/serv
## Example Code
See [example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java](https://github.com/Steveice10/MCProtocolLib/tree/master/example/com/github/steveice10/mc/protocol/test)
+## Adding as a Dependency
+
+The recommended way of fetching MCProtocolLib is through jitpack.io. See [here](https://jitpack.io/#Steveice10/MCProtocolLib) for more details on how to include MCProtocolLib in your project.
+
+Maven:
+```xml
+
+
+ jitpack.io
+ https://jitpack.io
+
+
+
+
+ com.github.Steveice10
+ MCProtocolLib
+ (version here)
+
+```
+
+Gradle:
+```groovy
+allprojects {
+ repositories {
+ maven { url 'https://jitpack.io' }
+ }
+}
+
+dependencies {
+ implementation 'com.github.Steveice10:MCProtocolLib:(version here)'
+}
+```
+
## Building the Source
MCProtocolLib uses Maven to manage dependencies. Simply run 'mvn clean install' in the source's directory.
diff --git a/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java b/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java
index b9fc67f3..f1c0a737 100644
--- a/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java
+++ b/example/com/github/steveice10/mc/protocol/test/MinecraftProtocolTest.java
@@ -7,12 +7,14 @@ import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.ServerLoginHandler;
import com.github.steveice10.mc.protocol.data.SubProtocol;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
+import com.github.steveice10.mc.protocol.data.game.world.WorldType;
import com.github.steveice10.mc.protocol.data.message.ChatColor;
import com.github.steveice10.mc.protocol.data.message.ChatFormat;
import com.github.steveice10.mc.protocol.data.message.Message;
-import com.github.steveice10.mc.protocol.data.message.MessageStyle;
import com.github.steveice10.mc.protocol.data.message.TextMessage;
-import com.github.steveice10.mc.protocol.data.message.TranslationMessage;
+import com.github.steveice10.mc.protocol.data.message.style.ChatColor;
+import com.github.steveice10.mc.protocol.data.message.style.ChatFormat;
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
import com.github.steveice10.mc.protocol.data.status.PlayerInfo;
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
import com.github.steveice10.mc.protocol.data.status.VersionInfo;
@@ -59,7 +61,7 @@ public class MinecraftProtocolTest {
return new ServerStatusInfo(
new VersionInfo(MinecraftConstants.GAME_VERSION, MinecraftConstants.PROTOCOL_VERSION),
new PlayerInfo(100, 0, new GameProfile[0]),
- new TextMessage("Hello world!"),
+ new TextMessage.Builder().text("Hello world!").build(),
null
);
}
@@ -88,11 +90,28 @@ public class MinecraftProtocolTest {
ClientChatPacket packet = event.getPacket();
GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
System.out.println(profile.getName() + ": " + packet.getMessage());
- Message msg = new TextMessage("Hello, ").setStyle(new MessageStyle().setColor(ChatColor.GREEN));
- Message name = new TextMessage(profile.getName()).setStyle(new MessageStyle().setColor(ChatColor.AQUA).addFormat(ChatFormat.UNDERLINED));
- Message end = new TextMessage("!");
- msg.addExtra(name);
- msg.addExtra(end);
+
+ MessageStyle green = new MessageStyle.Builder()
+ .color(ChatColor.GREEN)
+ .build();
+ MessageStyle aquaUnderline = new MessageStyle.Builder()
+ .color(ChatColor.AQUA)
+ .formats(ChatFormat.UNDERLINED)
+ .build();
+
+ Message msg = new TextMessage.Builder()
+ .text("Hello, ")
+ .style(green)
+ .extra(new TextMessage.Builder()
+ .text(profile.getName())
+ .style(aquaUnderline)
+ .build())
+ .extra(new TextMessage.Builder()
+ .text("!")
+ .style(green)
+ .build())
+ .build();
+
event.getSession().send(new ServerChatPacket(msg));
}
}
@@ -126,8 +145,8 @@ public class MinecraftProtocolTest {
System.out.println("Version: " + info.getVersionInfo().getVersionName() + ", " + info.getVersionInfo().getProtocolVersion());
System.out.println("Player Count: " + info.getPlayerInfo().getOnlinePlayers() + " / " + info.getPlayerInfo().getMaxPlayers());
System.out.println("Players: " + Arrays.toString(info.getPlayerInfo().getPlayers()));
- System.out.println("Description: " + info.getDescription().getFullText());
- System.out.println("Icon: " + info.getIcon());
+ System.out.println("Description: " + info.getDescription());
+ System.out.println("Icon: " + info.getIconPng());
}
});
@@ -171,18 +190,14 @@ public class MinecraftProtocolTest {
event.getSession().send(new ClientChatPacket("Hello, this is a test of MCProtocolLib."));
} else if(event.getPacket() instanceof ServerChatPacket) {
Message message = event.getPacket().getMessage();
- System.out.println("Received Message: " + message.getFullText());
- if(message instanceof TranslationMessage) {
- System.out.println("Received Translation Components: " + Arrays.toString(((TranslationMessage) message).getTranslationParams()));
- }
-
+ System.out.println("Received Message: " + message);
event.getSession().disconnect("Finished");
}
}
@Override
public void disconnected(DisconnectedEvent event) {
- System.out.println("Disconnected: " + Message.fromString(event.getReason()).getFullText());
+ System.out.println("Disconnected: " + event.getReason());
if(event.getCause() != null) {
event.getCause().printStackTrace();
}
diff --git a/pom.xml b/pom.xml
index 31bf5b6b..cbae9ed1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -62,13 +62,13 @@
com.github.steveice10
packetlib
- 43b394dfdc
+ 1.6
compile
com.github.steveice10
mcauthlib
- 401c99c722
+ 1.3
compile
diff --git a/src/main/java/com/github/steveice10/mc/protocol/ClientListener.java b/src/main/java/com/github/steveice10/mc/protocol/ClientListener.java
index d34bf41d..730750f7 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/ClientListener.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/ClientListener.java
@@ -56,7 +56,9 @@ public class ClientListener extends SessionAdapter {
throw new IllegalStateException("Failed to generate shared key.", e);
}
- SessionService sessionService = new SessionService(event.getSession().getFlag(MinecraftConstants.AUTH_PROXY_KEY, Proxy.NO_PROXY));
+ SessionService sessionService = new SessionService();
+ sessionService.setProxy(event.getSession().getFlag(MinecraftConstants.AUTH_PROXY_KEY, Proxy.NO_PROXY));
+
GameProfile profile = event.getSession().getFlag(MinecraftConstants.PROFILE_KEY);
String serverId = sessionService.getServerId(packet.getServerId(), packet.getPublicKey(), key);
String accessToken = event.getSession().getFlag(MinecraftConstants.ACCESS_TOKEN_KEY);
@@ -81,7 +83,7 @@ public class ClientListener extends SessionAdapter {
protocol.setSubProtocol(SubProtocol.GAME, true, event.getSession());
} else if(event.getPacket() instanceof LoginDisconnectPacket) {
LoginDisconnectPacket packet = event.getPacket();
- event.getSession().disconnect(packet.getReason().getFullText());
+ event.getSession().disconnect(packet.getReason().toString());
} else if(event.getPacket() instanceof LoginSetCompressionPacket) {
event.getSession().setCompressionThreshold(event.getPacket().getThreshold());
}
@@ -107,7 +109,7 @@ public class ClientListener extends SessionAdapter {
if(event.getPacket() instanceof ServerKeepAlivePacket) {
event.getSession().send(new ClientKeepAlivePacket(event.getPacket().getPingId()));
} else if(event.getPacket() instanceof ServerDisconnectPacket) {
- event.getSession().disconnect(event.getPacket().getReason().getFullText());
+ event.getSession().disconnect(event.getPacket().getReason().toString());
} else if(event.getPacket() instanceof ServerSetCompressionPacket) {
event.getSession().setCompressionThreshold(event.getPacket().getThreshold());
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java b/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java
index b0e83882..9952f72b 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/MinecraftProtocol.java
@@ -103,12 +103,12 @@ import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.Serv
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerHealthPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerPositionRotationPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerSetExperiencePacket;
-import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnExpOrbPacket;
-import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnWeatherEntityPacket;
-import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnLivingEntityPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnEntityPacket;
+import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnExpOrbPacket;
+import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnLivingEntityPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPaintingPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnPlayerPacket;
+import com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn.ServerSpawnWeatherEntityPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerDisplayScoreboardPacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerScoreboardObjectivePacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard.ServerTeamPacket;
@@ -229,7 +229,8 @@ public class MinecraftProtocol extends PacketProtocol {
private MinecraftProtocol(String username, String clientToken, String using, boolean token, Proxy authProxy) throws RequestException {
this(SubProtocol.LOGIN);
- AuthenticationService auth = new AuthenticationService(clientToken, authProxy);
+ AuthenticationService auth = new AuthenticationService(clientToken);
+ auth.setProxy(authProxy);
auth.setUsername(username);
if(token) {
auth.setAccessToken(using);
diff --git a/src/main/java/com/github/steveice10/mc/protocol/ServerListener.java b/src/main/java/com/github/steveice10/mc/protocol/ServerListener.java
index 6913dc97..f4b17b29 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/ServerListener.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/ServerListener.java
@@ -4,7 +4,7 @@ import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.auth.service.SessionService;
import com.github.steveice10.mc.protocol.data.SubProtocol;
-import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.TextMessage;
import com.github.steveice10.mc.protocol.data.status.PlayerInfo;
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
import com.github.steveice10.mc.protocol.data.status.VersionInfo;
@@ -127,7 +127,7 @@ public class ServerListener extends SessionAdapter {
builder = session -> new ServerStatusInfo(
VersionInfo.CURRENT,
new PlayerInfo(0, 20, new GameProfile[0]),
- Message.fromString("A Minecraft Server"),
+ new TextMessage.Builder().text("A Minecraft Server").build(),
null
);
}
@@ -190,7 +190,9 @@ public class ServerListener extends SessionAdapter {
public void run() {
GameProfile profile = null;
if(this.key != null) {
- SessionService sessionService = new SessionService(this.session.getFlag(MinecraftConstants.AUTH_PROXY_KEY, Proxy.NO_PROXY));
+ SessionService sessionService = new SessionService();
+ sessionService.setProxy(this.session.getFlag(MinecraftConstants.AUTH_PROXY_KEY, Proxy.NO_PROXY));
+
try {
profile = sessionService.getProfileByServer(username, sessionService.getServerId(SERVER_ID, KEY_PAIR.getPublic(), this.key));
} catch(RequestException e) {
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java b/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java
index 9eb94bc3..f9e3c674 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/MagicValues.java
@@ -23,6 +23,8 @@ import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierOper
import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierType;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.MetadataType;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose;
+import com.github.steveice10.mc.protocol.data.game.entity.object.HangingDirection;
+import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType;
import com.github.steveice10.mc.protocol.data.game.entity.player.Animation;
import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage;
import com.github.steveice10.mc.protocol.data.game.entity.player.CombatState;
@@ -32,11 +34,9 @@ import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerState;
import com.github.steveice10.mc.protocol.data.game.entity.player.PositionElement;
-import com.github.steveice10.mc.protocol.data.game.entity.type.WeatherEntityType;
import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType;
import com.github.steveice10.mc.protocol.data.game.entity.type.PaintingType;
-import com.github.steveice10.mc.protocol.data.game.entity.object.HangingDirection;
-import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType;
+import com.github.steveice10.mc.protocol.data.game.entity.type.WeatherEntityType;
import com.github.steveice10.mc.protocol.data.game.recipe.RecipeType;
import com.github.steveice10.mc.protocol.data.game.scoreboard.CollisionRule;
import com.github.steveice10.mc.protocol.data.game.scoreboard.NameTagVisibility;
@@ -125,6 +125,7 @@ public class MagicValues {
register(AttributeType.GENERIC_FLYING_SPEED, "generic.flying_speed");
register(AttributeType.HORSE_JUMP_STRENGTH, "horse.jump_strength");
register(AttributeType.ZOMBIE_SPAWN_REINFORCEMENTS, "zombie.spawn_reinforcements");
+ // Forge-only
register(AttributeType.SWIM_SPEED, "forge.swim_speed");
register(AttributeType.NAMETAG_DISTANCE, "forge.name_tag_distance");
register(AttributeType.ENTITY_GRAVITY, "forge.entity_gravity");
@@ -151,6 +152,8 @@ public class MagicValues {
register(ModifierType.CHESTPLATE_MODIFIER, UUID.fromString("9F3D476D-C118-4544-8365-64846904B48E"));
register(ModifierType.HELMET_MODIFIER, UUID.fromString("2AD3F246-FEE1-4E67-B886-69FD380BB150"));
register(ModifierType.COVERED_ARMOR_BONUS, UUID.fromString("7E0292F2-9434-48D5-A29F-9583AF7DF27F"));
+ // Forge-only
+ register(ModifierType.SLOW_FALLING, UUID.fromString("A5B6CF2A-2F7C-31EF-9022-7C3E7D5E6ABA"));
register(ModifierOperation.ADD, 0);
register(ModifierOperation.ADD_MULTIPLIED, 1);
@@ -1209,7 +1212,7 @@ public class MagicValues {
}
}
- throw new IllegalArgumentException("Value " + value + " has no mapping for key class " + keyType.getName() + ".");
+ throw new UnmappedValueException(value, keyType);
}
@SuppressWarnings("unchecked")
@@ -1236,6 +1239,6 @@ public class MagicValues {
}
}
- throw new IllegalArgumentException("Key " + key + " has no mapping for value class " + valueType.getName() + ".");
+ throw new UnmappedKeyException(key, valueType);
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedKeyException.java b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedKeyException.java
new file mode 100644
index 00000000..5e0c420c
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedKeyException.java
@@ -0,0 +1,13 @@
+package com.github.steveice10.mc.protocol.data;
+
+import lombok.Getter;
+
+@Getter
+public class UnmappedKeyException extends IllegalArgumentException {
+ private Enum> key;
+ private Class> valueType;
+
+ public UnmappedKeyException(Object key, Class> valueType) {
+ super("Key " + key + " has no mapping for value class " + valueType.getName() + ".");
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedValueException.java b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedValueException.java
new file mode 100644
index 00000000..e4bb5dbe
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/UnmappedValueException.java
@@ -0,0 +1,13 @@
+package com.github.steveice10.mc.protocol.data;
+
+import lombok.Getter;
+
+@Getter
+public class UnmappedValueException extends IllegalArgumentException {
+ private Object value;
+ private Class> keyType;
+
+ public UnmappedValueException(Object value, Class> keyType) {
+ super("Value " + value + " has no mapping for key class " + keyType.getName() + ".");
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/Chunk.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/Chunk.java
index e68d86f0..c48c4ca8 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/Chunk.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/chunk/Chunk.java
@@ -1,6 +1,5 @@
package com.github.steveice10.mc.protocol.data.game.chunk;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import lombok.AccessLevel;
@@ -18,12 +17,12 @@ import java.util.List;
@Setter(AccessLevel.NONE)
@AllArgsConstructor
public class Chunk {
- private static final BlockState AIR = new BlockState(0);
+ private static final int AIR = 0;
private int blockCount;
private int bitsPerEntry;
- private @NonNull List states;
+ private @NonNull List states;
private @NonNull FlexibleStorage storage;
public Chunk() {
@@ -34,10 +33,10 @@ public class Chunk {
int blockCount = in.readShort();
int bitsPerEntry = in.readUnsignedByte();
- List states = new ArrayList<>();
- int stateCount = (bitsPerEntry > 8 || bitsPerEntry == 0) ? 0 : in.readVarInt();
+ List states = new ArrayList<>();
+ int stateCount = bitsPerEntry > 8 || bitsPerEntry == 0 ? 0 : in.readVarInt();
for(int i = 0; i < stateCount; i++) {
- states.add(BlockState.read(in));
+ states.add(in.readVarInt());
}
FlexibleStorage storage = new FlexibleStorage(bitsPerEntry, in.readLongs(in.readVarInt()));
@@ -50,8 +49,8 @@ public class Chunk {
if(chunk.getBitsPerEntry() <= 8) {
out.writeVarInt(chunk.getStates().size());
- for (BlockState state : chunk.getStates()) {
- BlockState.write(out, state);
+ for (int state : chunk.getStates()) {
+ out.writeVarInt(state);
}
}
@@ -64,21 +63,21 @@ public class Chunk {
return y << 8 | z << 4 | x;
}
- public BlockState get(int x, int y, int z) {
+ public int get(int x, int y, int z) {
int id = this.storage.get(index(x, y, z));
- return this.bitsPerEntry <= 8 ? (id >= 0 && id < this.states.size() ? this.states.get(id) : AIR) : new BlockState(id);
+ return this.bitsPerEntry <= 8 ? (id >= 0 && id < this.states.size() ? this.states.get(id) : AIR) : id;
}
- public void set(int x, int y, int z, @NonNull BlockState state) {
- int id = this.bitsPerEntry <= 8 ? this.states.indexOf(state) : state.getId();
+ public void set(int x, int y, int z, @NonNull int state) {
+ int id = this.bitsPerEntry <= 8 ? this.states.indexOf(state) : state;
if(id == -1) {
this.states.add(state);
if(this.states.size() > 1 << this.bitsPerEntry) {
this.bitsPerEntry++;
- List oldStates = this.states;
+ List oldStates = this.states;
if(this.bitsPerEntry > 8) {
- oldStates = new ArrayList(this.states);
+ oldStates = new ArrayList(this.states);
this.states.clear();
this.bitsPerEntry = 13;
}
@@ -86,18 +85,18 @@ public class Chunk {
FlexibleStorage oldStorage = this.storage;
this.storage = new FlexibleStorage(this.bitsPerEntry, this.storage.getSize());
for(int index = 0; index < this.storage.getSize(); index++) {
- this.storage.set(index, this.bitsPerEntry <= 8 ? oldStorage.get(index) : oldStates.get(index).getId());
+ this.storage.set(index, this.bitsPerEntry <= 8 ? oldStorage.get(index) : oldStates.get(index));
}
}
- id = this.bitsPerEntry <= 8 ? this.states.indexOf(state) : state.getId();
+ id = this.bitsPerEntry <= 8 ? this.states.indexOf(state) : state;
}
int ind = index(x, y, z);
int curr = this.storage.get(ind);
- if(state.getId() != AIR.getId() && curr == AIR.getId()) {
+ if(state != AIR && curr == AIR) {
this.blockCount++;
- } else if(state.getId() == AIR.getId() && curr != AIR.getId()) {
+ } else if(state == AIR && curr != AIR) {
this.blockCount--;
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java
index f578acc6..2fd1c92a 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeModifier.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.data.game.entity.attribute;
import com.github.steveice10.mc.protocol.data.MagicValues;
+import com.github.steveice10.mc.protocol.data.UnmappedValueException;
import lombok.Data;
import lombok.NonNull;
@@ -25,7 +26,7 @@ public class AttributeModifier {
ModifierType type = null;
try {
type = MagicValues.key(ModifierType.class, uuid);
- } catch(IllegalArgumentException e) {
+ } catch(UnmappedValueException e) {
}
this.type = type;
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeType.java
index f63ec1f2..5f17f506 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeType.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/AttributeType.java
@@ -12,10 +12,11 @@ public enum AttributeType {
GENERIC_MOVEMENT_SPEED(0.699999988079071, 0, 1024),
GENERIC_ATTACK_DAMAGE(2, 0, 2048),
GENERIC_ATTACK_SPEED(4, 0, 1024),
+ GENERIC_FLYING_SPEED(0.4000000059604645, 0, 1024),
GENERIC_ARMOR(0, 0, 30),
GENERIC_ARMOR_TOUGHNESS(0, 0, 20),
+ GENERIC_ATTACK_KNOCKBACK(0, 0, 5),
GENERIC_LUCK(0, -1024, 1024),
- GENERIC_FLYING_SPEED(0.4000000059604645, 0, 1024),
HORSE_JUMP_STRENGTH(0.7, 0, 2),
ZOMBIE_SPAWN_REINFORCEMENTS(0, 0, 1),
/**
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierType.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierType.java
index b585afc4..f086258a 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierType.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/attribute/ModifierType.java
@@ -22,5 +22,10 @@ public enum ModifierType {
LEGGINGS_MODIFIER,
CHESTPLATE_MODIFIER,
HELMET_MODIFIER,
- COVERED_ARMOR_BONUS;
+ COVERED_ARMOR_BONUS,
+ /**
+ * Only available for clients/servers using Minecraft Forge.
+ * Source: MinecraftForge/patches/minecraft/net/minecraft/entity/LivingEntity.java.patch#9
+ */
+ SLOW_FALLING
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java
index 4b3d216d..c9e60412 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/entity/metadata/EntityMetadata.java
@@ -3,11 +3,11 @@ package com.github.steveice10.mc.protocol.data.game.entity.metadata;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.NBT;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.mc.protocol.data.game.world.particle.Particle;
import com.github.steveice10.mc.protocol.data.game.world.particle.ParticleData;
import com.github.steveice10.mc.protocol.data.game.world.particle.ParticleType;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
@@ -40,6 +40,7 @@ public class EntityMetadata {
value = in.readByte();
break;
case INT:
+ case BLOCK_STATE:
value = in.readVarInt();
break;
case FLOAT:
@@ -56,7 +57,7 @@ public class EntityMetadata {
// Intentional fall-through
case CHAT:
- value = Message.fromString(in.readString());
+ value = MessageSerializer.fromString(in.readString());
break;
case ITEM:
value = ItemStack.read(in);
@@ -86,9 +87,6 @@ public class EntityMetadata {
value = in.readUUID();
}
- break;
- case BLOCK_STATE:
- value = BlockState.read(in);
break;
case NBT_TAG:
value = NBT.read(in);
@@ -142,7 +140,7 @@ public class EntityMetadata {
// Intentional fall-through
case CHAT:
- out.writeString(((Message) meta.getValue()).toJsonString());
+ out.writeString(MessageSerializer.toJsonString((Message) meta.getValue()));
break;
case ITEM:
ItemStack.write(out, (ItemStack) meta.getValue());
@@ -174,7 +172,7 @@ public class EntityMetadata {
break;
case BLOCK_STATE:
- BlockState.write(out, (BlockState) meta.getValue());
+ out.writeVarInt((int) meta.getValue());
break;
case NBT_TAG:
NBT.write(out, (CompoundTag) meta.getValue());
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/block/BlockChangeRecord.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/block/BlockChangeRecord.java
index 32abeb05..876dea8e 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/block/BlockChangeRecord.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/block/BlockChangeRecord.java
@@ -9,5 +9,5 @@ import lombok.NonNull;
@AllArgsConstructor
public class BlockChangeRecord {
private final @NonNull Position position;
- private final @NonNull BlockState block;
+ private final @NonNull int block;
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/block/BlockState.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/block/BlockState.java
deleted file mode 100644
index 9b5fc994..00000000
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/block/BlockState.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package com.github.steveice10.mc.protocol.data.game.world.block;
-
-import com.github.steveice10.packetlib.io.NetInput;
-import com.github.steveice10.packetlib.io.NetOutput;
-import lombok.AllArgsConstructor;
-import lombok.Data;
-
-import java.io.IOException;
-
-@Data
-@AllArgsConstructor
-public class BlockState {
- private final int id;
-
- public static BlockState read(NetInput in) throws IOException {
- return new BlockState(in.readVarInt());
- }
-
- public static void write(NetOutput out, BlockState blockState) throws IOException {
- out.writeVarInt(blockState.getId());
- }
-}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/effect/BreakBlockEffectData.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/effect/BreakBlockEffectData.java
index 06cf9df2..bf681337 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/effect/BreakBlockEffectData.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/effect/BreakBlockEffectData.java
@@ -1,6 +1,5 @@
package com.github.steveice10.mc.protocol.data.game.world.effect;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
@@ -8,5 +7,5 @@ import lombok.NonNull;
@Data
@AllArgsConstructor
public class BreakBlockEffectData implements WorldEffectData {
- private final @NonNull BlockState blockState;
+ private final @NonNull int blockState;
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/BlockParticleData.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/BlockParticleData.java
index 561b2e0d..8a8bb94d 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/BlockParticleData.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/BlockParticleData.java
@@ -1,6 +1,5 @@
package com.github.steveice10.mc.protocol.data.game.world.particle;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
@@ -8,5 +7,5 @@ import lombok.NonNull;
@Data
@AllArgsConstructor
public class BlockParticleData implements ParticleData {
- private final @NonNull BlockState blockState;
+ private final @NonNull int blockState;
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/FallingDustParticleData.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/FallingDustParticleData.java
index 5a1a8b43..20549c4c 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/FallingDustParticleData.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/FallingDustParticleData.java
@@ -1,6 +1,5 @@
package com.github.steveice10.mc.protocol.data.game.world.particle;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
@@ -8,5 +7,5 @@ import lombok.NonNull;
@Data
@AllArgsConstructor
public class FallingDustParticleData implements ParticleData {
- private final @NonNull BlockState blockState;
+ private final @NonNull int blockState;
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/ParticleData.java b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/ParticleData.java
index ffdd70e7..4f87e929 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/ParticleData.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/game/world/particle/ParticleData.java
@@ -1,7 +1,6 @@
package com.github.steveice10.mc.protocol.data.game.world.particle;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
@@ -11,7 +10,7 @@ public interface ParticleData {
public static ParticleData read(NetInput in, ParticleType type) throws IOException {
switch (type) {
case BLOCK:
- return new BlockParticleData(BlockState.read(in));
+ return new BlockParticleData(in.readVarInt());
case DUST:
float red = in.readFloat();
float green = in.readFloat();
@@ -19,7 +18,7 @@ public interface ParticleData {
float scale = in.readFloat();
return new DustParticleData(red, green, blue, scale);
case FALLING_DUST:
- return new FallingDustParticleData(BlockState.read(in));
+ return new FallingDustParticleData(in.readVarInt());
case ITEM:
return new ItemParticleData(ItemStack.read(in));
default:
@@ -30,7 +29,7 @@ public interface ParticleData {
public static void write(NetOutput out, ParticleType type, ParticleData data) throws IOException {
switch (type) {
case BLOCK:
- BlockState.write(out, ((BlockParticleData) data).getBlockState());
+ out.writeVarInt(((BlockParticleData) data).getBlockState());
break;
case DUST:
out.writeFloat(((DustParticleData) data).getRed());
@@ -39,7 +38,7 @@ public interface ParticleData {
out.writeFloat(((DustParticleData) data).getScale());
break;
case FALLING_DUST:
- BlockState.write(out, ((FallingDustParticleData) data).getBlockState());
+ out.writeVarInt(((FallingDustParticleData) data).getBlockState());
break;
case ITEM:
ItemStack.write(out, ((ItemParticleData) data).getItemStack());
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/BlockNbtMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/BlockNbtMessage.java
new file mode 100644
index 00000000..e1f46c93
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/BlockNbtMessage.java
@@ -0,0 +1,43 @@
+package com.github.steveice10.mc.protocol.data.message;
+
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+
+import java.util.List;
+
+@EqualsAndHashCode(callSuper = true)
+public class BlockNbtMessage extends NbtMessage {
+ public static class Builder extends NbtMessage.Builder {
+ @NonNull
+ private String pos = "";
+
+ public Builder pos(@NonNull String pos) {
+ this.pos = pos;
+ return this;
+ }
+
+ @Override
+ public Builder copy(@NonNull BlockNbtMessage message) {
+ super.copy(message);
+ this.pos = message.getPos();
+ return this;
+ }
+
+ @Override
+ public BlockNbtMessage build() {
+ return new BlockNbtMessage(this.style, this.extra, this.path, this.interpret, this.pos);
+ }
+ }
+
+ private final String pos;
+
+ private BlockNbtMessage(MessageStyle style, List extra, String path, boolean interpret, String pos) {
+ super(style, extra, path, interpret);
+ this.pos = pos;
+ }
+
+ public String getPos() {
+ return this.pos;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/ClickEvent.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/ClickEvent.java
deleted file mode 100644
index 9966004d..00000000
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/ClickEvent.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.github.steveice10.mc.protocol.data.message;
-
-import lombok.AllArgsConstructor;
-import lombok.Data;
-
-@Data
-@AllArgsConstructor
-public class ClickEvent {
- private final ClickAction action;
- private final String value;
-}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/EntityNbtMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/EntityNbtMessage.java
new file mode 100644
index 00000000..30dfa25b
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/EntityNbtMessage.java
@@ -0,0 +1,43 @@
+package com.github.steveice10.mc.protocol.data.message;
+
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+
+import java.util.List;
+
+@EqualsAndHashCode(callSuper = true)
+public class EntityNbtMessage extends NbtMessage {
+ public static class Builder extends NbtMessage.Builder {
+ @NonNull
+ private String selector = "";
+
+ public Builder selector(@NonNull String selector) {
+ this.selector = selector;
+ return this;
+ }
+
+ @Override
+ public Builder copy(@NonNull EntityNbtMessage message) {
+ super.copy(message);
+ this.selector = message.getSelector();
+ return this;
+ }
+
+ @Override
+ public EntityNbtMessage build() {
+ return new EntityNbtMessage(this.style, this.extra, this.path, this.interpret, this.selector);
+ }
+ }
+
+ private final String selector;
+
+ private EntityNbtMessage(MessageStyle style, List extra, String path, boolean interpret, String selector) {
+ super(style, extra, path, interpret);
+ this.selector = selector;
+ }
+
+ public String getSelector() {
+ return this.selector;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/HoverEvent.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/HoverEvent.java
deleted file mode 100644
index 2d3a4790..00000000
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/HoverEvent.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package com.github.steveice10.mc.protocol.data.message;
-
-import lombok.AllArgsConstructor;
-import lombok.Data;
-
-@Data
-@AllArgsConstructor
-public class HoverEvent {
- private final HoverAction action;
- private final Message value;
-}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/KeybindMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/KeybindMessage.java
index 9a006a58..1e1486f6 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/KeybindMessage.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/KeybindMessage.java
@@ -1,36 +1,43 @@
package com.github.steveice10.mc.protocol.data.message;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import lombok.AllArgsConstructor;
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
import lombok.EqualsAndHashCode;
-import lombok.Getter;
+import lombok.NonNull;
+
+import java.util.List;
-@Getter
@EqualsAndHashCode(callSuper = true)
-@AllArgsConstructor
public class KeybindMessage extends Message {
- private final String keybind;
+ public static class Builder extends Message.Builder {
+ @NonNull
+ private String keybind = "";
- @Override
- public String getText() {
- return this.keybind;
- }
+ public Builder keybind(@NonNull String keybind) {
+ this.keybind = keybind;
+ return this;
+ }
- @Override
- public KeybindMessage clone() {
- return (KeybindMessage) new KeybindMessage(this.keybind).setStyle(this.getStyle().clone()).setExtra(this.getExtra());
- }
+ @Override
+ public Builder copy(@NonNull KeybindMessage message) {
+ super.copy(message);
+ this.keybind = message.getKeybind();
+ return this;
+ }
- @Override
- public JsonElement toJson() {
- JsonElement e = super.toJson();
- if(e.isJsonObject()) {
- JsonObject json = e.getAsJsonObject();
- json.addProperty("keybind", this.keybind);
- return json;
- } else {
- return e;
+ @Override
+ public KeybindMessage build() {
+ return new KeybindMessage(this.style, this.extra, this.keybind);
}
}
+
+ private final String keybind;
+
+ private KeybindMessage(MessageStyle style, List extra, String keybind) {
+ super(style, extra);
+ this.keybind = keybind;
+ }
+
+ public String getKeybind() {
+ return this.keybind;
+ }
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/Message.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/Message.java
index e0f403dd..9a0989b3 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/Message.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/Message.java
@@ -1,206 +1,64 @@
package com.github.steveice10.mc.protocol.data.message;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParser;
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
import lombok.EqualsAndHashCode;
-import lombok.Getter;
+import lombok.NonNull;
import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
-@Getter
@EqualsAndHashCode
-public abstract class Message implements Cloneable {
- private MessageStyle style = new MessageStyle();
- private final List extra = new ArrayList();
+public abstract class Message {
+ public abstract static class Builder, M extends Message> {
+ @NonNull
+ protected MessageStyle style = MessageStyle.DEFAULT;
+ @NonNull
+ protected List extra = new ArrayList<>();
- public static Message fromString(String str) {
- try {
- return fromJson(new JsonParser().parse(str));
- } catch(Exception e) {
- return new TextMessage(str);
- }
- }
-
- public static Message fromJson(JsonElement e) {
- if(e.isJsonPrimitive()) {
- return new TextMessage(e.getAsString());
- } else if(e.isJsonArray()) {
- JsonArray array = e.getAsJsonArray();
- if(array.size() == 0) {
- return new TextMessage("");
- }
-
- Message msg = Message.fromJson(array.get(0));
- for(int index = 1; index < array.size(); index++) {
- msg.addExtra(Message.fromJson(array.get(index)));
- }
-
- return msg;
- } else if(e.isJsonObject()) {
- JsonObject json = e.getAsJsonObject();
- Message msg = null;
- if(json.has("text")) {
- msg = new TextMessage(json.get("text").getAsString());
- } else if(json.has("translate")) {
- Message with[] = new Message[0];
- if(json.has("with")) {
- JsonArray withJson = json.get("with").getAsJsonArray();
- with = new Message[withJson.size()];
- for(int index = 0; index < withJson.size(); index++) {
- JsonElement el = withJson.get(index);
- if(el.isJsonPrimitive()) {
- with[index] = new TextMessage(el.getAsString());
- } else {
- with[index] = Message.fromJson(el.getAsJsonObject());
- }
- }
- }
-
- msg = new TranslationMessage(json.get("translate").getAsString(), with);
- } else if(json.has("keybind")) {
- msg = new KeybindMessage(json.get("keybind").getAsString());
- } else {
- throw new IllegalArgumentException("Unknown message type in json: " + json.toString());
- }
-
- MessageStyle style = new MessageStyle();
- if(json.has("color")) {
- style.setColor(json.get("color").getAsString());
- }
-
- for(ChatFormat format : ChatFormat.values()) {
- if(json.has(format.toString()) && json.get(format.toString()).getAsBoolean()) {
- style.addFormat(format);
- }
- }
-
- if(json.has("clickEvent")) {
- JsonObject click = json.get("clickEvent").getAsJsonObject();
- style.setClickEvent(new ClickEvent(ClickAction.byName(click.get("action").getAsString()), click.get("value").getAsString()));
- }
-
- if(json.has("hoverEvent")) {
- JsonObject hover = json.get("hoverEvent").getAsJsonObject();
- style.setHoverEvent(new HoverEvent(HoverAction.byName(hover.get("action").getAsString()), Message.fromJson(hover.get("value"))));
- }
-
- if(json.has("insertion")) {
- style.setInsertion(json.get("insertion").getAsString());
- }
-
- msg.setStyle(style);
-
- if(json.has("extra")) {
- JsonArray extraJson = json.get("extra").getAsJsonArray();
- for(int index = 0; index < extraJson.size(); index++) {
- msg.addExtra(Message.fromJson(extraJson.get(index)));
- }
- }
-
- return msg;
- } else {
- throw new IllegalArgumentException("Cannot convert " + e.getClass().getSimpleName() + " to a message.");
- }
- }
-
- public abstract String getText();
-
- public String getFullText() {
- StringBuilder build = new StringBuilder(this.getText());
- for(Message msg : this.extra) {
- build.append(msg.getFullText());
+ public B style(@NonNull MessageStyle style) {
+ this.style = style;
+ return (B) this;
}
- return build.toString();
+ public B extra(@NonNull Message... extra) {
+ return this.extra(Arrays.asList(extra));
+ }
+
+ public B extra(@NonNull Collection extra) {
+ this.extra.addAll(extra);
+ return (B) this;
+ }
+
+ public B copy(@NonNull M message) {
+ this.style = message.getStyle();
+ this.extra = new ArrayList<>(message.getExtra());
+ return (B) this;
+ }
+
+ public abstract M build();
}
- public Message setStyle(MessageStyle style) {
+ private final MessageStyle style;
+ private final List extra;
+
+ protected Message(MessageStyle style, List extra) {
this.style = style;
- return this;
+ this.extra = Collections.unmodifiableList(extra);
}
- public Message setExtra(List extra) {
- this.clearExtra();
- for(Message msg : extra) {
- this.addExtra(msg.clone());
- }
-
- return this;
+ public MessageStyle getStyle() {
+ return this.style;
}
- public Message addExtra(Message message) {
- this.extra.add(message);
- message.getStyle().setParent(this.style);
- return this;
+ public List getExtra() {
+ return this.extra;
}
- public Message removeExtra(Message message) {
- this.extra.remove(message);
- message.getStyle().setParent(null);
- return this;
- }
-
- public Message clearExtra() {
- for(Message msg : this.extra) {
- msg.getStyle().setParent(null);
- }
-
- this.extra.clear();
- return this;
- }
-
- @Override
- public abstract Message clone();
-
@Override
public String toString() {
- return this.getFullText();
- }
-
- public String toJsonString() {
- return this.toJson().toString();
- }
-
- public JsonElement toJson() {
- JsonObject json = new JsonObject();
- if(this.style.hasColor()) {
- json.addProperty("color", this.style.getColor().toString());
- }
-
- for(ChatFormat format : this.style.getFormats()) {
- json.addProperty(format.toString(), true);
- }
-
- if(this.style.getClickEvent() != null) {
- JsonObject click = new JsonObject();
- click.addProperty("action", this.style.getClickEvent().getAction().toString());
- click.addProperty("value", this.style.getClickEvent().getValue());
- json.add("clickEvent", click);
- }
-
- if(this.style.getHoverEvent() != null) {
- JsonObject hover = new JsonObject();
- hover.addProperty("action", this.style.getHoverEvent().getAction().toString());
- hover.add("value", this.style.getHoverEvent().getValue().toJson());
- json.add("hoverEvent", hover);
- }
-
- if(this.style.getInsertion() != null) {
- json.addProperty("insertion", this.style.getInsertion());
- }
-
- if(this.extra.size() > 0) {
- JsonArray extra = new JsonArray();
- for(Message msg : this.extra) {
- extra.add(msg.toJson());
- }
-
- json.add("extra", extra);
- }
-
- return json;
+ return MessageSerializer.toJsonString(this);
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageSerializer.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageSerializer.java
new file mode 100644
index 00000000..7e119e78
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageSerializer.java
@@ -0,0 +1,255 @@
+package com.github.steveice10.mc.protocol.data.message;
+
+import com.github.steveice10.mc.protocol.data.message.style.ChatColor;
+import com.github.steveice10.mc.protocol.data.message.style.ChatFormat;
+import com.github.steveice10.mc.protocol.data.message.style.ClickAction;
+import com.github.steveice10.mc.protocol.data.message.style.ClickEvent;
+import com.github.steveice10.mc.protocol.data.message.style.HoverAction;
+import com.github.steveice10.mc.protocol.data.message.style.HoverEvent;
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class MessageSerializer {
+ public static Message fromString(String str) {
+ try {
+ return fromJson(new JsonParser().parse(str));
+ } catch(Exception e) {
+ return new TextMessage.Builder().text(str).build();
+ }
+ }
+
+ public static Message fromJson(JsonElement e) {
+ return builderFromJson(e).build();
+ }
+
+ private static Message.Builder, ?> builderFromJson(JsonElement e) {
+ if(e.isJsonPrimitive()) {
+ return new TextMessage.Builder().text(e.getAsString());
+ } else if(e.isJsonArray()) {
+ JsonArray array = e.getAsJsonArray();
+ if(array.size() == 0) {
+ return new TextMessage.Builder().text("");
+ }
+
+ Message.Builder, ?> msg = builderFromJson(array.get(0));
+ for(int index = 1; index < array.size(); index++) {
+ msg.extra(fromJson(array.get(index)));
+ }
+
+ return msg;
+ } else if(e.isJsonObject()) {
+ JsonObject json = e.getAsJsonObject();
+
+ Message.Builder, ?> msg = dataFromJson(json);
+ msg.style(styleFromJson(json));
+ msg.extra(extraFromJson(json));
+ return msg;
+ } else {
+ throw new IllegalArgumentException("Cannot convert JSON type " + e.getClass().getSimpleName() + " to a message.");
+ }
+ }
+
+ public static String toJsonString(Message message) {
+ return toJson(message).toString();
+ }
+
+ public static JsonElement toJson(Message message) {
+ if(message instanceof TextMessage && message.getStyle().equals(MessageStyle.DEFAULT) && message.getExtra().isEmpty()) {
+ return new JsonPrimitive(((TextMessage) message).getText());
+ }
+
+ JsonObject json = new JsonObject();
+ dataToJson(json, message);
+ styleToJson(json, message.getStyle());
+ extraToJson(json, message.getExtra());
+ return json;
+ }
+
+ private static Message.Builder, ?> dataFromJson(JsonObject json) {
+ if(json.has("text")) {
+ return new TextMessage.Builder()
+ .text(json.get("text").getAsString());
+ } else if(json.has("translate")) {
+ List with = new ArrayList<>();
+ if(json.has("with")) {
+ JsonArray withJson = json.get("with").getAsJsonArray();
+ for(int index = 0; index < withJson.size(); index++) {
+ with.add(fromJson(withJson.get(index)));
+ }
+ }
+
+ return new TranslationMessage.Builder()
+ .key(json.get("translate").getAsString())
+ .with(with);
+ } else if(json.has("keybind")) {
+ return new KeybindMessage.Builder()
+ .keybind(json.get("keybind").getAsString());
+ } else if(json.has("score")) {
+ JsonObject score = json.get("score").getAsJsonObject();
+ return new ScoreMessage.Builder()
+ .name(score.get("name").getAsString())
+ .objective(score.get("objective").getAsString())
+ .value(score.has("value") ? score.get("value").getAsString() : null);
+ } else if(json.has("selector")) {
+ return new SelectorMessage.Builder()
+ .selector(json.get("selector").getAsString());
+ } else if(json.has("nbt")) {
+ String path = json.get("nbt").getAsString();
+ boolean interpret = json.has("interpret") && json.get("interpret").getAsBoolean();
+ if(json.has("block")) {
+ return new BlockNbtMessage.Builder()
+ .path(path)
+ .interpret(interpret)
+ .pos(json.get("block").getAsString());
+ } else if(json.has("entity")) {
+ return new EntityNbtMessage.Builder()
+ .path(path)
+ .interpret(interpret)
+ .selector(json.get("entity").getAsString());
+ } else if(json.has("storage")) {
+ return new StorageNbtMessage.Builder()
+ .path(path)
+ .interpret(interpret)
+ .id(json.get("storage").getAsString());
+ } else {
+ throw new IllegalArgumentException("Unknown NBT message type in json: " + json);
+ }
+ } else {
+ throw new IllegalArgumentException("Unknown message type in json: " + json);
+ }
+ }
+
+ private static void dataToJson(JsonObject json, Message message) {
+ if(message instanceof TextMessage) {
+ json.addProperty("text", ((TextMessage) message).getText());
+ } else if(message instanceof TranslationMessage) {
+ TranslationMessage translationMessage = (TranslationMessage) message;
+ json.addProperty("translate", translationMessage.getKey());
+
+ List with = translationMessage.getWith();
+ if(!with.isEmpty()) {
+ JsonArray jsonWith = new JsonArray();
+ for(Message msg : with) {
+ jsonWith.add(toJson(msg));
+ }
+
+ json.add("with", jsonWith);
+ }
+ } else if(message instanceof KeybindMessage) {
+ json.addProperty("keybind", ((KeybindMessage) message).getKeybind());
+ } else if(message instanceof ScoreMessage) {
+ ScoreMessage scoreMessage = (ScoreMessage) message;
+
+ JsonObject score = new JsonObject();
+ score.addProperty("name", scoreMessage.getName());
+ score.addProperty("objective", scoreMessage.getObjective());
+ if(scoreMessage.getValue() != null) {
+ score.addProperty("value", scoreMessage.getValue());
+ }
+
+ json.add("score", score);
+ } else if(message instanceof SelectorMessage) {
+ json.addProperty("selector", ((SelectorMessage) message).getSelector());
+ } else if(message instanceof NbtMessage) {
+ NbtMessage nbtMessage = (NbtMessage) message;
+
+ json.addProperty("nbt", nbtMessage.getPath());
+ json.addProperty("interpret", nbtMessage.shouldInterpret());
+
+ if(message instanceof BlockNbtMessage) {
+ json.addProperty("block", ((BlockNbtMessage) nbtMessage).getPos());
+ } else if(message instanceof EntityNbtMessage) {
+ json.addProperty("entity", ((EntityNbtMessage) nbtMessage).getSelector());
+ } else if(message instanceof StorageNbtMessage) {
+ json.addProperty("storage", ((StorageNbtMessage) nbtMessage).getId());
+ }
+ }
+ }
+
+ private static MessageStyle styleFromJson(JsonObject json) {
+ MessageStyle.Builder style = new MessageStyle.Builder();
+ if(json.has("color")) {
+ style.color(json.get("color").getAsString());
+ }
+
+ for(ChatFormat format : ChatFormat.values()) {
+ if(json.has(format.toString()) && json.get(format.toString()).getAsBoolean()) {
+ style.formats(format);
+ }
+ }
+
+ if(json.has("clickEvent")) {
+ JsonObject click = json.get("clickEvent").getAsJsonObject();
+ style.clickEvent(new ClickEvent(ClickAction.byName(click.get("action").getAsString()), click.get("value").getAsString()));
+ }
+
+ if(json.has("hoverEvent")) {
+ JsonObject hover = json.get("hoverEvent").getAsJsonObject();
+ style.hoverEvent(new HoverEvent(HoverAction.byName(hover.get("action").getAsString()), fromJson(hover.get("value"))));
+ }
+
+ if(json.has("insertion")) {
+ style.insertion(json.get("insertion").getAsString());
+ }
+
+ return style.build();
+ }
+
+ private static void styleToJson(JsonObject json, MessageStyle style) {
+ if(style.getColor() != ChatColor.NONE) {
+ json.addProperty("color", style.getColor().toString());
+ }
+
+ for(ChatFormat format : style.getFormats()) {
+ json.addProperty(format.toString(), true);
+ }
+
+ if(style.getClickEvent() != null) {
+ JsonObject click = new JsonObject();
+ click.addProperty("action", style.getClickEvent().getAction().toString());
+ click.addProperty("value", style.getClickEvent().getValue());
+ json.add("clickEvent", click);
+ }
+
+ if(style.getHoverEvent() != null) {
+ JsonObject hover = new JsonObject();
+ hover.addProperty("action", style.getHoverEvent().getAction().toString());
+ hover.add("value", toJson(style.getHoverEvent().getValue()));
+ json.add("hoverEvent", hover);
+ }
+
+ if(style.getInsertion() != null) {
+ json.addProperty("insertion", style.getInsertion());
+ }
+ }
+
+ private static List extraFromJson(JsonObject json) {
+ List extra = new ArrayList<>();
+ if(json.has("extra")) {
+ JsonArray extraJson = json.get("extra").getAsJsonArray();
+ for(int index = 0; index < extraJson.size(); index++) {
+ extra.add(fromJson(extraJson.get(index)));
+ }
+ }
+
+ return extra;
+ }
+
+ private static void extraToJson(JsonObject json, List extra) {
+ if(!extra.isEmpty()) {
+ JsonArray jsonExtra = new JsonArray();
+ for(Message msg : extra) {
+ jsonExtra.add(toJson(msg));
+ }
+
+ json.add("extra", jsonExtra);
+ }
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageStyle.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageStyle.java
index 6b3232f8..e69de29b 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageStyle.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/MessageStyle.java
@@ -1,89 +0,0 @@
-package com.github.steveice10.mc.protocol.data.message;
-
-import lombok.AccessLevel;
-import lombok.Data;
-import lombok.Setter;
-
-import java.util.ArrayList;
-import java.util.List;
-
-@Data
-@Setter(AccessLevel.NONE)
-public class MessageStyle implements Cloneable {
- private static final MessageStyle DEFAULT = new MessageStyle();
-
- private String color = ChatColor.NONE;
- private List formats = new ArrayList();
- private ClickEvent clickEvent;
- private HoverEvent hoverEvent;
- private String insertion;
- private MessageStyle parent = DEFAULT;
-
- public boolean isDefault() {
- return this.equals(DEFAULT);
- }
-
- public boolean hasColor() {
- return !color.equals(ChatColor.NONE);
- }
-
- public MessageStyle setColor(String color) {
- this.color = color;
- return this;
- }
-
- public MessageStyle setFormats(List formats) {
- this.formats = new ArrayList<>(formats);
- return this;
- }
-
- public MessageStyle setClickEvent(ClickEvent event) {
- this.clickEvent = event;
- return this;
- }
-
- public MessageStyle setHoverEvent(HoverEvent event) {
- this.hoverEvent = event;
- return this;
- }
-
- public MessageStyle setInsertion(String insertion) {
- this.insertion = insertion;
- return this;
- }
-
- protected MessageStyle setParent(MessageStyle parent) {
- if(parent == null) {
- parent = DEFAULT;
- }
-
- this.parent = parent;
- return this;
- }
-
- public MessageStyle addFormat(ChatFormat format) {
- this.formats.add(format);
- return this;
- }
-
- public MessageStyle removeFormat(ChatFormat format) {
- this.formats.remove(format);
- return this;
- }
-
- public MessageStyle clearFormats() {
- this.formats.clear();
- return this;
- }
-
- @Override
- public MessageStyle clone() {
- return new MessageStyle()
- .setParent(this.parent)
- .setColor(this.color)
- .setFormats(this.formats)
- .setClickEvent(this.clickEvent)
- .setHoverEvent(this.hoverEvent)
- .setInsertion(this.insertion);
- }
-}
\ No newline at end of file
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/NbtMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/NbtMessage.java
new file mode 100644
index 00000000..2ec42232
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/NbtMessage.java
@@ -0,0 +1,54 @@
+package com.github.steveice10.mc.protocol.data.message;
+
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+
+import java.util.List;
+
+@EqualsAndHashCode(callSuper = true)
+public abstract class NbtMessage extends Message {
+ public abstract static class Builder, M extends NbtMessage> extends Message.Builder {
+ @NonNull
+ protected String path = "";
+ protected boolean interpret = false;
+
+ public B path(@NonNull String path) {
+ this.path = path;
+ return (B) this;
+ }
+
+ public B interpret(boolean interpret) {
+ this.interpret = interpret;
+ return (B) this;
+ }
+
+ @Override
+ public B copy(@NonNull M message) {
+ super.copy(message);
+ this.path = message.getPath();
+ this.interpret = message.shouldInterpret();
+ return (B) this;
+ }
+
+ @Override
+ public abstract M build();
+ }
+
+ private final String path;
+ private final boolean interpret;
+
+ protected NbtMessage(MessageStyle style, List extra, String path, boolean interpret) {
+ super(style, extra);
+ this.path = path;
+ this.interpret = interpret;
+ }
+
+ public String getPath() {
+ return this.path;
+ }
+
+ public boolean shouldInterpret() {
+ return this.interpret;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/ScoreMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/ScoreMessage.java
new file mode 100644
index 00000000..0cb32741
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/ScoreMessage.java
@@ -0,0 +1,70 @@
+package com.github.steveice10.mc.protocol.data.message;
+
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+
+import java.util.List;
+
+@EqualsAndHashCode(callSuper = true)
+public class ScoreMessage extends Message {
+ public static class Builder extends Message.Builder {
+ @NonNull
+ private String name = "";
+ @NonNull
+ private String objective = "";
+ private String value = null;
+
+ public Builder name(@NonNull String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder objective(@NonNull String objective) {
+ this.objective = objective;
+ return this;
+ }
+
+ public Builder value(String value) {
+ this.value = value;
+ return this;
+ }
+
+ @Override
+ public Builder copy(@NonNull ScoreMessage message) {
+ super.copy(message);
+ this.name = message.getName();
+ this.objective = message.getObjective();
+ this.value = message.getValue();
+ return this;
+ }
+
+ @Override
+ public ScoreMessage build() {
+ return new ScoreMessage(this.style, this.extra, this.name, this.objective, this.value);
+ }
+ }
+
+ private final String name;
+ private final String objective;
+ private final String value;
+
+ private ScoreMessage(MessageStyle style, List extra, String name, String objective, String value) {
+ super(style, extra);
+ this.name = name;
+ this.objective = objective;
+ this.value = value;
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public String getObjective() {
+ return this.objective;
+ }
+
+ public String getValue() {
+ return this.value;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/SelectorMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/SelectorMessage.java
new file mode 100644
index 00000000..96a33687
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/SelectorMessage.java
@@ -0,0 +1,43 @@
+package com.github.steveice10.mc.protocol.data.message;
+
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+
+import java.util.List;
+
+@EqualsAndHashCode(callSuper = true)
+public class SelectorMessage extends Message {
+ public static class Builder extends Message.Builder {
+ @NonNull
+ private String selector = "";
+
+ public Builder selector(@NonNull String selector) {
+ this.selector = selector;
+ return this;
+ }
+
+ @Override
+ public Builder copy(@NonNull SelectorMessage message) {
+ super.copy(message);
+ this.selector = message.getSelector();
+ return this;
+ }
+
+ @Override
+ public SelectorMessage build() {
+ return new SelectorMessage(this.style, this.extra, this.selector);
+ }
+ }
+
+ private final String selector;
+
+ private SelectorMessage(MessageStyle style, List extra, String selector) {
+ super(style, extra);
+ this.selector = selector;
+ }
+
+ public String getSelector() {
+ return this.selector;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/StorageNbtMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/StorageNbtMessage.java
new file mode 100644
index 00000000..50032596
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/StorageNbtMessage.java
@@ -0,0 +1,43 @@
+package com.github.steveice10.mc.protocol.data.message;
+
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+
+import java.util.List;
+
+@EqualsAndHashCode(callSuper = true)
+public class StorageNbtMessage extends NbtMessage {
+ public static class Builder extends NbtMessage.Builder {
+ @NonNull
+ private String id = "";
+
+ public Builder id(@NonNull String id) {
+ this.id = id;
+ return this;
+ }
+
+ @Override
+ public Builder copy(@NonNull StorageNbtMessage message) {
+ super.copy(message);
+ this.id = message.getId();
+ return this;
+ }
+
+ @Override
+ public StorageNbtMessage build() {
+ return new StorageNbtMessage(this.style, this.extra, this.path, this.interpret, this.id);
+ }
+ }
+
+ private final String id;
+
+ private StorageNbtMessage(MessageStyle style, List extra, String path, boolean interpret, String id) {
+ super(style, extra, path, interpret);
+ this.id = id;
+ }
+
+ public String getId() {
+ return this.id;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/TextMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/TextMessage.java
index a14776d6..6e4cee54 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/TextMessage.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/TextMessage.java
@@ -1,36 +1,43 @@
package com.github.steveice10.mc.protocol.data.message;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonPrimitive;
-import lombok.AllArgsConstructor;
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
import lombok.EqualsAndHashCode;
-import lombok.Getter;
+import lombok.NonNull;
+
+import java.util.List;
-@Getter
@EqualsAndHashCode(callSuper = true)
-@AllArgsConstructor
public class TextMessage extends Message {
- private final String text;
+ public static class Builder extends Message.Builder {
+ @NonNull
+ private String text = "";
- @Override
- public TextMessage clone() {
- return (TextMessage) new TextMessage(this.getText()).setStyle(this.getStyle().clone()).setExtra(this.getExtra());
- }
+ public Builder text(@NonNull String text) {
+ this.text = text;
+ return this;
+ }
- @Override
- public JsonElement toJson() {
- if(this.getStyle().isDefault() && this.getExtra().isEmpty()) {
- return new JsonPrimitive(this.text);
- } else {
- JsonElement e = super.toJson();
- if(e.isJsonObject()) {
- JsonObject json = e.getAsJsonObject();
- json.addProperty("text", this.text);
- return json;
- } else {
- return e;
- }
+ @Override
+ public Builder copy(@NonNull TextMessage message) {
+ super.copy(message);
+ this.text = message.getText();
+ return this;
+ }
+
+ @Override
+ public TextMessage build() {
+ return new TextMessage(this.style, this.extra, this.text);
}
}
+
+ private final String text;
+
+ private TextMessage(MessageStyle style, List extra, String text) {
+ super(style, extra);
+ this.text = text;
+ }
+
+ public String getText() {
+ return this.text;
+ }
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/TranslationMessage.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/TranslationMessage.java
index 935ae7bf..8db25ee1 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/TranslationMessage.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/TranslationMessage.java
@@ -1,61 +1,65 @@
package com.github.steveice10.mc.protocol.data.message;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
+import com.github.steveice10.mc.protocol.data.message.style.MessageStyle;
import lombok.EqualsAndHashCode;
-import lombok.Getter;
+import lombok.NonNull;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
-@Getter
@EqualsAndHashCode(callSuper = true)
public class TranslationMessage extends Message {
- private final String translationKey;
- private final Message[] translationParams;
+ public static class Builder extends Message.Builder {
+ @NonNull
+ private String key = "";
+ @NonNull
+ private List with = new ArrayList<>();
- public TranslationMessage(String translationKey, Message... translationParams) {
- this.translationKey = translationKey;
- this.translationParams = new Message[translationParams.length];
- for(int index = 0; index < this.translationParams.length; index++) {
- this.translationParams[index] = translationParams[index].clone();
- this.translationParams[index].getStyle().setParent(this.getStyle());
+ public Builder key(@NonNull String key) {
+ this.key = key;
+ return this;
+ }
+
+ public Builder with(@NonNull Message... with) {
+ return this.with(Arrays.asList(with));
+ }
+
+ public Builder with(@NonNull Collection with) {
+ this.with.addAll(with);
+ return this;
+ }
+
+ @Override
+ public Builder copy(@NonNull TranslationMessage message) {
+ super.copy(message);
+ this.key = message.getKey();
+ this.with = new ArrayList<>(message.getWith());
+ return this;
+ }
+
+ @Override
+ public TranslationMessage build() {
+ return new TranslationMessage(this.style, this.extra, this.key, this.with);
}
}
- @Override
- public String getText() {
- return this.translationKey;
+ private final String key;
+ private final List with;
+
+ private TranslationMessage(MessageStyle style, List extra, String key, List with) {
+ super(style, extra);
+ this.key = key;
+ this.with = Collections.unmodifiableList(with);
}
- @Override
- public Message setStyle(MessageStyle style) {
- super.setStyle(style);
- for(Message param : this.translationParams) {
- param.getStyle().setParent(this.getStyle());
- }
-
- return this;
+ public String getKey() {
+ return this.key;
}
- @Override
- public TranslationMessage clone() {
- return (TranslationMessage) new TranslationMessage(this.translationKey, this.translationParams).setStyle(this.getStyle().clone()).setExtra(this.getExtra());
- }
-
- @Override
- public JsonElement toJson() {
- JsonElement e = super.toJson();
- if(e.isJsonObject()) {
- JsonObject json = e.getAsJsonObject();
- json.addProperty("translate", this.translationKey);
- JsonArray params = new JsonArray();
- for(Message param : this.translationParams) {
- params.add(param.toJson());
- }
-
- json.add("with", params);
- return json;
- } else {
- return e;
- }
+ public List getWith() {
+ return this.with;
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/ChatColor.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ChatColor.java
similarity index 93%
rename from src/main/java/com/github/steveice10/mc/protocol/data/message/ChatColor.java
rename to src/main/java/com/github/steveice10/mc/protocol/data/message/style/ChatColor.java
index 751a0d30..f2560067 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/ChatColor.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ChatColor.java
@@ -1,4 +1,4 @@
-package com.github.steveice10.mc.protocol.data.message;
+package com.github.steveice10.mc.protocol.data.message.style;
public class ChatColor {
public static final String BLACK = "black";
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/ChatFormat.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ChatFormat.java
similarity index 69%
rename from src/main/java/com/github/steveice10/mc/protocol/data/message/ChatFormat.java
rename to src/main/java/com/github/steveice10/mc/protocol/data/message/style/ChatFormat.java
index 51cccd62..770e65ab 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/ChatFormat.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ChatFormat.java
@@ -1,4 +1,4 @@
-package com.github.steveice10.mc.protocol.data.message;
+package com.github.steveice10.mc.protocol.data.message.style;
public enum ChatFormat {
BOLD,
@@ -8,9 +8,9 @@ public enum ChatFormat {
OBFUSCATED;
public static ChatFormat byName(String name) {
- name = name.toLowerCase();
+ String lowerCase = name.toLowerCase();
for(ChatFormat format : values()) {
- if(format.toString().equals(name)) {
+ if(format.toString().equals(lowerCase)) {
return format;
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/ClickAction.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ClickAction.java
similarity index 66%
rename from src/main/java/com/github/steveice10/mc/protocol/data/message/ClickAction.java
rename to src/main/java/com/github/steveice10/mc/protocol/data/message/style/ClickAction.java
index f46100c5..903f2889 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/ClickAction.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ClickAction.java
@@ -1,15 +1,15 @@
-package com.github.steveice10.mc.protocol.data.message;
+package com.github.steveice10.mc.protocol.data.message.style;
public enum ClickAction {
RUN_COMMAND,
SUGGEST_COMMAND,
OPEN_URL,
- OPEN_FILE;
+ CHANGE_PAGE;
public static ClickAction byName(String name) {
- name = name.toLowerCase();
+ String lowerCase = name.toLowerCase();
for(ClickAction action : values()) {
- if(action.toString().equals(name)) {
+ if(action.toString().equals(lowerCase)) {
return action;
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ClickEvent.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ClickEvent.java
new file mode 100644
index 00000000..e3de276e
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/ClickEvent.java
@@ -0,0 +1,27 @@
+package com.github.steveice10.mc.protocol.data.message.style;
+
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+import lombok.ToString;
+
+@ToString
+@EqualsAndHashCode
+public class ClickEvent {
+ @NonNull
+ private final ClickAction action;
+ @NonNull
+ private final String value;
+
+ public ClickEvent(@NonNull ClickAction action, @NonNull String value) {
+ this.action = action;
+ this.value = value;
+ }
+
+ public ClickAction getAction() {
+ return this.action;
+ }
+
+ public String getValue() {
+ return this.value;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/HoverAction.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverAction.java
similarity index 68%
rename from src/main/java/com/github/steveice10/mc/protocol/data/message/HoverAction.java
rename to src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverAction.java
index d307fb9c..616a99d4 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/message/HoverAction.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverAction.java
@@ -1,15 +1,14 @@
-package com.github.steveice10.mc.protocol.data.message;
+package com.github.steveice10.mc.protocol.data.message.style;
public enum HoverAction {
SHOW_TEXT,
SHOW_ITEM,
- SHOW_ACHIEVEMENT,
SHOW_ENTITY;
public static HoverAction byName(String name) {
- name = name.toLowerCase();
+ String lowerCase = name.toLowerCase();
for(HoverAction action : values()) {
- if(action.toString().equals(name)) {
+ if(action.toString().equals(lowerCase)) {
return action;
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverEvent.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverEvent.java
new file mode 100644
index 00000000..3d41915b
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/HoverEvent.java
@@ -0,0 +1,28 @@
+package com.github.steveice10.mc.protocol.data.message.style;
+
+import com.github.steveice10.mc.protocol.data.message.Message;
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+import lombok.ToString;
+
+@ToString
+@EqualsAndHashCode
+public class HoverEvent {
+ @NonNull
+ private final HoverAction action;
+ @NonNull
+ private final Message value;
+
+ public HoverEvent(@NonNull HoverAction action, @NonNull Message value) {
+ this.action = action;
+ this.value = value;
+ }
+
+ public HoverAction getAction() {
+ return this.action;
+ }
+
+ public Message getValue() {
+ return this.value;
+ }
+}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/message/style/MessageStyle.java b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/MessageStyle.java
new file mode 100644
index 00000000..33cd7af3
--- /dev/null
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/message/style/MessageStyle.java
@@ -0,0 +1,103 @@
+package com.github.steveice10.mc.protocol.data.message.style;
+
+import lombok.EqualsAndHashCode;
+import lombok.NonNull;
+import lombok.ToString;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+@ToString
+@EqualsAndHashCode
+public class MessageStyle {
+ public static class Builder {
+ @NonNull
+ private String color = ChatColor.NONE;
+ @NonNull
+ private List formats = new ArrayList<>();
+ private ClickEvent clickEvent;
+ private HoverEvent hoverEvent;
+ private String insertion;
+
+ public Builder color(@NonNull String color) {
+ this.color = color;
+ return this;
+ }
+
+ public Builder formats(@NonNull ChatFormat... formats) {
+ return this.formats(Arrays.asList(formats));
+ }
+
+ public Builder formats(@NonNull Collection formats) {
+ this.formats.addAll(formats);
+ return this;
+ }
+
+ public Builder clickEvent(ClickEvent clickEvent) {
+ this.clickEvent = clickEvent;
+ return this;
+ }
+
+ public Builder hoverEvent(HoverEvent hoverEvent) {
+ this.hoverEvent = hoverEvent;
+ return this;
+ }
+
+ public Builder insertion(String insertion) {
+ this.insertion = insertion;
+ return this;
+ }
+
+ public Builder copy(MessageStyle style) {
+ this.color = style.getColor();
+ this.formats = new ArrayList<>(style.getFormats());
+ this.clickEvent = style.getClickEvent();
+ this.hoverEvent = style.getHoverEvent();
+ this.insertion = style.getInsertion();
+ return this;
+ }
+
+ public MessageStyle build() {
+ return new MessageStyle(this.color, this.formats, this.clickEvent, this.hoverEvent, this.insertion);
+ }
+ }
+
+ public static final MessageStyle DEFAULT = new MessageStyle.Builder().build();
+
+ private final String color;
+ private final List formats;
+ private final ClickEvent clickEvent;
+ private final HoverEvent hoverEvent;
+ private final String insertion;
+
+ private MessageStyle(String color, List formats, ClickEvent clickEvent, HoverEvent hoverEvent, String insertion) {
+ this.color = color;
+ this.formats = Collections.unmodifiableList(formats);
+ this.clickEvent = clickEvent;
+ this.hoverEvent = hoverEvent;
+ this.insertion = insertion;
+ }
+
+ public String getColor() {
+ return this.color;
+ }
+
+ public List getFormats() {
+ return this.formats;
+ }
+
+ public ClickEvent getClickEvent() {
+ return this.clickEvent;
+ }
+
+ public HoverEvent getHoverEvent() {
+ return this.hoverEvent;
+ }
+
+ public String getInsertion() {
+ return this.insertion;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java b/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java
index 054876ae..4291c96c 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/data/status/ServerStatusInfo.java
@@ -7,8 +7,6 @@ import lombok.Data;
import lombok.NonNull;
import lombok.Setter;
-import java.awt.image.BufferedImage;
-
@Data
@Setter(AccessLevel.NONE)
@AllArgsConstructor
@@ -16,5 +14,5 @@ public class ServerStatusInfo {
private @NonNull VersionInfo versionInfo;
private @NonNull PlayerInfo playerInfo;
private @NonNull Message description;
- private BufferedImage icon;
+ private byte[] iconPng;
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerAdvancementsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerAdvancementsPacket.java
index 4fa4c97c..ddb49650 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerAdvancementsPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerAdvancementsPacket.java
@@ -6,6 +6,7 @@ import com.github.steveice10.mc.protocol.data.game.advancement.Advancement.Displ
import com.github.steveice10.mc.protocol.data.game.advancement.Advancement.DisplayData.FrameType;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -59,8 +60,8 @@ public class ServerAdvancementsPacket implements Packet {
String parentId = in.readBoolean() ? in.readString() : null;
DisplayData displayData = null;
if(in.readBoolean()) {
- Message title = Message.fromString(in.readString());
- Message description = Message.fromString(in.readString());
+ Message title = MessageSerializer.fromString(in.readString());
+ Message description = MessageSerializer.fromString(in.readString());
ItemStack icon = ItemStack.read(in);
FrameType frameType = MagicValues.key(FrameType.class, in.readVarInt());
@@ -136,8 +137,8 @@ public class ServerAdvancementsPacket implements Packet {
DisplayData displayData = advancement.getDisplayData();
if(displayData != null) {
out.writeBoolean(true);
- out.writeString(displayData.getTitle().toJsonString());
- out.writeString(displayData.getDescription().toJsonString());
+ out.writeString(MessageSerializer.toJsonString(displayData.getTitle()));
+ out.writeString(MessageSerializer.toJsonString(displayData.getDescription()));
ItemStack.write(out, displayData.getIcon());
out.writeVarInt(MagicValues.value(Integer.class, displayData.getFrameType()));
String backgroundTexture = displayData.getBackgroundTexture();
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerBossBarPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerBossBarPacket.java
index defa838c..07cd444c 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerBossBarPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerBossBarPacket.java
@@ -5,6 +5,7 @@ import com.github.steveice10.mc.protocol.data.game.BossBarAction;
import com.github.steveice10.mc.protocol.data.game.BossBarColor;
import com.github.steveice10.mc.protocol.data.game.BossBarDivision;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -91,7 +92,7 @@ public class ServerBossBarPacket implements Packet {
this.action = MagicValues.key(BossBarAction.class, in.readVarInt());
if(this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_TITLE) {
- this.title = Message.fromString(in.readString());
+ this.title = MessageSerializer.fromString(in.readString());
}
if(this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_HEALTH) {
@@ -117,7 +118,7 @@ public class ServerBossBarPacket implements Packet {
out.writeVarInt(MagicValues.value(Integer.class, this.action));
if(this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_TITLE) {
- out.writeString(this.title.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.title));
}
if(this.action == BossBarAction.ADD || this.action == BossBarAction.UPDATE_HEALTH) {
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerChatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerChatPacket.java
index f825c4a0..166673b7 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerChatPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerChatPacket.java
@@ -3,6 +3,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.MessageType;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -26,7 +28,7 @@ public class ServerChatPacket implements Packet {
private @NonNull UUID senderUuid;
public ServerChatPacket(@NonNull String text) {
- this(Message.fromString(text));
+ this(MessageSerializer.fromString(text));
}
public ServerChatPacket(@NonNull Message message) {
@@ -34,7 +36,7 @@ public class ServerChatPacket implements Packet {
}
public ServerChatPacket(@NonNull String text, @NonNull MessageType type) {
- this(Message.fromString(text), type, new UUID(0, 0));
+ this(MessageSerializer.fromString(text), type, new UUID(0, 0));
}
public ServerChatPacket(@NonNull Message message, @NonNull MessageType type) {
@@ -42,19 +44,19 @@ public class ServerChatPacket implements Packet {
}
public ServerChatPacket(@NonNull String text, @NonNull MessageType type, UUID uuid) {
- this(Message.fromString(text), type, uuid);
+ this(MessageSerializer.fromString(text), type, uuid);
}
@Override
public void read(NetInput in) throws IOException {
- this.message = Message.fromString(in.readString());
+ this.message = MessageSerializer.fromString(in.readString());
this.type = MagicValues.key(MessageType.class, in.readByte());
this.senderUuid = in.readUUID();
}
@Override
public void write(NetOutput out) throws IOException {
- out.writeString(this.message.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.message));
out.writeByte(MagicValues.value(Integer.class, this.type));
out.writeUUID(this.senderUuid);
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerCombatPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerCombatPacket.java
index 44b3c17c..5d7ba813 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerCombatPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerCombatPacket.java
@@ -3,6 +3,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.entity.player.CombatState;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -51,7 +52,7 @@ public class ServerCombatPacket implements Packet {
} else if(this.combatState == CombatState.ENTITY_DEAD) {
this.playerId = in.readVarInt();
this.entityId = in.readInt();
- this.message = Message.fromString(in.readString());
+ this.message = MessageSerializer.fromString(in.readString());
}
}
@@ -64,7 +65,7 @@ public class ServerCombatPacket implements Packet {
} else if(this.combatState == CombatState.ENTITY_DEAD) {
out.writeVarInt(this.playerId);
out.writeInt(this.entityId);
- out.writeString(this.message.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.message));
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerDisconnectPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerDisconnectPacket.java
index dfc2bbd2..8150eb4c 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerDisconnectPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerDisconnectPacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -21,17 +22,17 @@ public class ServerDisconnectPacket implements Packet {
private @NonNull Message reason;
public ServerDisconnectPacket(@NonNull String reason) {
- this(Message.fromString(reason));
+ this(MessageSerializer.fromString(reason));
}
@Override
public void read(NetInput in) throws IOException {
- this.reason = Message.fromString(in.readString());
+ this.reason = MessageSerializer.fromString(in.readString());
}
@Override
public void write(NetOutput out) throws IOException {
- out.writeString(this.reason.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.reason));
}
@Override
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListDataPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListDataPacket.java
index cf199ed0..93dc7c92 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListDataPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListDataPacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -23,14 +24,14 @@ public class ServerPlayerListDataPacket implements Packet {
@Override
public void read(NetInput in) throws IOException {
- this.header = Message.fromString(in.readString());
- this.footer = Message.fromString(in.readString());
+ this.header = MessageSerializer.fromString(in.readString());
+ this.footer = MessageSerializer.fromString(in.readString());
}
@Override
public void write(NetOutput out) throws IOException {
- out.writeString(this.header.toJsonString());
- out.writeString(this.footer.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.header));
+ out.writeString(MessageSerializer.toJsonString(this.footer));
}
@Override
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListEntryPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListEntryPacket.java
index 3ab62062..26036bb8 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListEntryPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerPlayerListEntryPacket.java
@@ -6,6 +6,7 @@ import com.github.steveice10.mc.protocol.data.game.PlayerListEntry;
import com.github.steveice10.mc.protocol.data.game.PlayerListEntryAction;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -61,11 +62,11 @@ public class ServerPlayerListEntryPacket implements Packet {
profile.setProperties(propertyList);
int rawGameMode = in.readVarInt();
- GameMode gameMode = MagicValues.key(GameMode.class, rawGameMode < 0 ? 0 : rawGameMode);
+ GameMode gameMode = MagicValues.key(GameMode.class, Math.max(rawGameMode, 0));
int ping = in.readVarInt();
Message displayName = null;
if(in.readBoolean()) {
- displayName = Message.fromString(in.readString());
+ displayName = MessageSerializer.fromString(in.readString());
}
entry = new PlayerListEntry(profile, gameMode, ping, displayName);
@@ -73,7 +74,7 @@ public class ServerPlayerListEntryPacket implements Packet {
}
case UPDATE_GAMEMODE: {
int rawGameMode = in.readVarInt();
- GameMode mode = MagicValues.key(GameMode.class, rawGameMode < 0 ? 0 : rawGameMode);
+ GameMode mode = MagicValues.key(GameMode.class, Math.max(rawGameMode, 0));
entry = new PlayerListEntry(profile, mode);
break;
@@ -87,7 +88,7 @@ public class ServerPlayerListEntryPacket implements Packet {
case UPDATE_DISPLAY_NAME: {
Message displayName = null;
if(in.readBoolean()) {
- displayName = Message.fromString(in.readString());
+ displayName = MessageSerializer.fromString(in.readString());
}
entry = new PlayerListEntry(profile, displayName);
@@ -125,7 +126,7 @@ public class ServerPlayerListEntryPacket implements Packet {
out.writeVarInt(entry.getPing());
out.writeBoolean(entry.getDisplayName() != null);
if(entry.getDisplayName() != null) {
- out.writeString(entry.getDisplayName().toJsonString());
+ out.writeString(MessageSerializer.toJsonString(entry.getDisplayName()));
}
break;
@@ -138,7 +139,7 @@ public class ServerPlayerListEntryPacket implements Packet {
case UPDATE_DISPLAY_NAME:
out.writeBoolean(entry.getDisplayName() != null);
if(entry.getDisplayName() != null) {
- out.writeString(entry.getDisplayName().toJsonString());
+ out.writeString(MessageSerializer.toJsonString(entry.getDisplayName()));
}
break;
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java
index 6e167ae0..8a9bdad6 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStatisticsPacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.MagicValues;
+import com.github.steveice10.mc.protocol.data.UnmappedValueException;
import com.github.steveice10.mc.protocol.data.game.statistic.BreakBlockStatistic;
import com.github.steveice10.mc.protocol.data.game.statistic.BreakItemStatistic;
import com.github.steveice10.mc.protocol.data.game.statistic.CraftItemStatistic;
@@ -73,7 +74,7 @@ public class ServerStatisticsPacket implements Packet {
default:
throw new IllegalArgumentException();
}
- } catch (IllegalArgumentException e) {
+ } catch (UnmappedValueException e) {
statistic = new CustomStatistic(categoryId, statisticId);
}
this.statistics.put(statistic, in.readVarInt());
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java
index 66a3e42b..dc3bb298 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerStopSoundPacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.MagicValues;
+import com.github.steveice10.mc.protocol.data.UnmappedValueException;
import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.Sound;
@@ -41,7 +42,7 @@ public class ServerStopSoundPacket implements Packet {
String value = in.readString();
try {
this.sound = MagicValues.key(BuiltinSound.class, value);
- } catch(IllegalArgumentException e) {
+ } catch(UnmappedValueException e) {
this.sound = new CustomSound(value);
}
} else {
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTabCompletePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTabCompletePacket.java
index 60a4253e..73ad255c 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTabCompletePacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTabCompletePacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -45,7 +46,7 @@ public class ServerTabCompletePacket implements Packet {
for(int index = 0; index < this.matches.length; index++) {
this.matches[index] = in.readString();
if (in.readBoolean()) {
- this.tooltips[index] = Message.fromString(in.readString());
+ this.tooltips[index] = MessageSerializer.fromString(in.readString());
}
}
}
@@ -61,7 +62,7 @@ public class ServerTabCompletePacket implements Packet {
Message tooltip = this.tooltips[index];
if (tooltip != null) {
out.writeBoolean(true);
- out.writeString(tooltip.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(tooltip));
} else {
out.writeBoolean(false);
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTitlePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTitlePacket.java
index e7d04cae..c177e334 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTitlePacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/ServerTitlePacket.java
@@ -3,6 +3,7 @@ package com.github.steveice10.mc.protocol.packet.ingame.server;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.TitleAction;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -59,7 +60,7 @@ public class ServerTitlePacket implements Packet {
case TITLE:
case SUBTITLE:
case ACTION_BAR:
- this.title = Message.fromString(in.readString());
+ this.title = MessageSerializer.fromString(in.readString());
break;
case TIMES:
this.fadeIn = in.readInt();
@@ -79,7 +80,7 @@ public class ServerTitlePacket implements Packet {
case TITLE:
case SUBTITLE:
case ACTION_BAR:
- out.writeString(this.title.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.title));
break;
case TIMES:
out.writeInt(this.fadeIn);
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/player/ServerPlayerActionAckPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/player/ServerPlayerActionAckPacket.java
index 087febd9..1229223a 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/player/ServerPlayerActionAckPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/player/ServerPlayerActionAckPacket.java
@@ -3,7 +3,6 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.entity.player;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -24,12 +23,12 @@ public class ServerPlayerActionAckPacket implements Packet {
private @NonNull PlayerAction action;
private boolean successful;
private @NonNull Position position;
- private @NonNull BlockState newState;
+ private @NonNull int newState;
@Override
public void read(NetInput in) throws IOException {
this.position = Position.read(in);
- this.newState = BlockState.read(in);
+ this.newState = in.readVarInt();
this.action = MagicValues.key(PlayerAction.class, in.readVarInt());
this.successful = in.readBoolean();
}
@@ -37,7 +36,7 @@ public class ServerPlayerActionAckPacket implements Packet {
@Override
public void write(NetOutput out) throws IOException {
Position.write(out, this.position);
- BlockState.write(out, this.newState);
+ out.writeVarInt(this.newState);
out.writeVarInt(MagicValues.value(Integer.class, this.action));
out.writeBoolean(this.successful);
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnEntityPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnEntityPacket.java
index 07572b97..f17a8e48 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnEntityPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnEntityPacket.java
@@ -1,7 +1,6 @@
package com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn;
import com.github.steveice10.mc.protocol.data.MagicValues;
-import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType;
import com.github.steveice10.mc.protocol.data.game.entity.object.FallingBlockData;
import com.github.steveice10.mc.protocol.data.game.entity.object.GenericObjectData;
import com.github.steveice10.mc.protocol.data.game.entity.object.HangingDirection;
@@ -9,6 +8,7 @@ import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType;
import com.github.steveice10.mc.protocol.data.game.entity.object.ObjectData;
import com.github.steveice10.mc.protocol.data.game.entity.object.ProjectileData;
import com.github.steveice10.mc.protocol.data.game.entity.object.SplashPotionData;
+import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnPaintingPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnPaintingPacket.java
index 66e93886..845c3638 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnPaintingPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/entity/spawn/ServerSpawnPaintingPacket.java
@@ -2,8 +2,8 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.entity.spawn;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
-import com.github.steveice10.mc.protocol.data.game.entity.type.PaintingType;
import com.github.steveice10.mc.protocol.data.game.entity.object.HangingDirection;
+import com.github.steveice10.mc.protocol.data.game.entity.type.PaintingType;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerScoreboardObjectivePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerScoreboardObjectivePacket.java
index 1dfb88b3..71037380 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerScoreboardObjectivePacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerScoreboardObjectivePacket.java
@@ -4,6 +4,7 @@ import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.scoreboard.ObjectiveAction;
import com.github.steveice10.mc.protocol.data.game.scoreboard.ScoreType;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -46,7 +47,7 @@ public class ServerScoreboardObjectivePacket implements Packet {
this.name = in.readString();
this.action = MagicValues.key(ObjectiveAction.class, in.readByte());
if(this.action == ObjectiveAction.ADD || this.action == ObjectiveAction.UPDATE) {
- this.displayName = Message.fromString(in.readString());
+ this.displayName = MessageSerializer.fromString(in.readString());
this.type = MagicValues.key(ScoreType.class, in.readVarInt());
}
}
@@ -56,7 +57,7 @@ public class ServerScoreboardObjectivePacket implements Packet {
out.writeString(this.name);
out.writeByte(MagicValues.value(Integer.class, this.action));
if(this.action == ObjectiveAction.ADD || this.action == ObjectiveAction.UPDATE) {
- out.writeString(this.displayName.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.displayName));
out.writeVarInt(MagicValues.value(Integer.class, this.type));
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java
index 5b50218e..75d909ce 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/scoreboard/ServerTeamPacket.java
@@ -1,11 +1,13 @@
package com.github.steveice10.mc.protocol.packet.ingame.server.scoreboard;
import com.github.steveice10.mc.protocol.data.MagicValues;
+import com.github.steveice10.mc.protocol.data.UnmappedValueException;
import com.github.steveice10.mc.protocol.data.game.scoreboard.CollisionRule;
import com.github.steveice10.mc.protocol.data.game.scoreboard.NameTagVisibility;
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamAction;
import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -91,7 +93,7 @@ public class ServerTeamPacket implements Packet {
this.teamName = in.readString();
this.action = MagicValues.key(TeamAction.class, in.readByte());
if(this.action == TeamAction.CREATE || this.action == TeamAction.UPDATE) {
- this.displayName = Message.fromString(in.readString());
+ this.displayName = MessageSerializer.fromString(in.readString());
byte flags = in.readByte();
this.friendlyFire = (flags & 0x1) != 0;
this.seeFriendlyInvisibles = (flags & 0x2) != 0;
@@ -100,12 +102,12 @@ public class ServerTeamPacket implements Packet {
try {
this.color = MagicValues.key(TeamColor.class, in.readVarInt());
- } catch(IllegalArgumentException e) {
+ } catch(UnmappedValueException e) {
this.color = TeamColor.NONE;
}
- this.prefix = Message.fromString(in.readString());
- this.suffix = Message.fromString(in.readString());
+ this.prefix = MessageSerializer.fromString(in.readString());
+ this.suffix = MessageSerializer.fromString(in.readString());
}
if(this.action == TeamAction.CREATE || this.action == TeamAction.ADD_PLAYER || this.action == TeamAction.REMOVE_PLAYER) {
@@ -121,13 +123,13 @@ public class ServerTeamPacket implements Packet {
out.writeString(this.teamName);
out.writeByte(MagicValues.value(Integer.class, this.action));
if(this.action == TeamAction.CREATE || this.action == TeamAction.UPDATE) {
- out.writeString(this.displayName.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.displayName));
out.writeByte((this.friendlyFire ? 0x1 : 0x0) | (this.seeFriendlyInvisibles ? 0x2 : 0x0));
out.writeString(MagicValues.value(String.class, this.nameTagVisibility));
out.writeString(MagicValues.value(String.class, this.collisionRule));
out.writeVarInt(MagicValues.value(Integer.class, this.color));
- out.writeString(this.prefix.toJsonString());
- out.writeString(this.suffix.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.prefix));
+ out.writeString(MessageSerializer.toJsonString(this.suffix));
}
if(this.action == TeamAction.CREATE || this.action == TeamAction.ADD_PLAYER || this.action == TeamAction.REMOVE_PLAYER) {
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/window/ServerSetSlotPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/window/ServerSetSlotPacket.java
index 50f8b326..271a1140 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/window/ServerSetSlotPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/window/ServerSetSlotPacket.java
@@ -8,7 +8,6 @@ import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
-import lombok.NonNull;
import lombok.Setter;
import java.io.IOException;
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java
index 5d1903c2..ae61a773 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockBreakAnimPacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.MagicValues;
+import com.github.steveice10.mc.protocol.data.UnmappedValueException;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage;
import com.github.steveice10.packetlib.io.NetInput;
@@ -29,7 +30,7 @@ public class ServerBlockBreakAnimPacket implements Packet {
this.position = Position.read(in);
try {
this.stage = MagicValues.key(BlockBreakStage.class, in.readUnsignedByte());
- } catch(IllegalArgumentException e) {
+ } catch(UnmappedValueException e) {
this.stage = BlockBreakStage.RESET;
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacket.java
index b5cc01cc..0ae5d340 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacket.java
@@ -2,7 +2,6 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -24,13 +23,13 @@ public class ServerBlockChangePacket implements Packet {
@Override
public void read(NetInput in) throws IOException {
- this.record = new BlockChangeRecord(Position.read(in), BlockState.read(in));
+ this.record = new BlockChangeRecord(Position.read(in), in.readVarInt());
}
@Override
public void write(NetOutput out) throws IOException {
Position.write(out, this.record.getPosition());
- BlockState.write(out, this.record.getBlock());
+ out.writeVarInt(this.record.getBlock());
}
@Override
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMapDataPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMapDataPacket.java
index 58463da1..742e7967 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMapDataPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMapDataPacket.java
@@ -5,6 +5,7 @@ import com.github.steveice10.mc.protocol.data.game.world.map.MapData;
import com.github.steveice10.mc.protocol.data.game.world.map.MapIcon;
import com.github.steveice10.mc.protocol.data.game.world.map.MapIconType;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -48,7 +49,7 @@ public class ServerMapDataPacket implements Packet {
int rotation = in.readUnsignedByte();
Message displayName = null;
if(in.readBoolean()) {
- displayName = Message.fromString(in.readString());
+ displayName = MessageSerializer.fromString(in.readString());
}
this.icons[index] = new MapIcon(x, z, MagicValues.key(MapIconType.class, type), rotation, displayName);
@@ -81,7 +82,7 @@ public class ServerMapDataPacket implements Packet {
out.writeByte(icon.getIconRotation());
if (icon.getDisplayName() != null) {
out.writeBoolean(false);
- out.writeString(icon.getDisplayName().toJsonString());
+ out.writeString(MessageSerializer.toJsonString(icon.getDisplayName()));
} else {
out.writeBoolean(true);
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMultiBlockChangePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMultiBlockChangePacket.java
index ffa620b5..222fadc7 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMultiBlockChangePacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerMultiBlockChangePacket.java
@@ -2,7 +2,6 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -35,7 +34,7 @@ public class ServerMultiBlockChangePacket implements Packet {
this.records = new BlockChangeRecord[in.readVarInt()];
for(int index = 0; index < this.records.length; index++) {
short pos = in.readShort();
- BlockState block = BlockState.read(in);
+ int block = in.readVarInt();
int x = (chunkX << 4) + (pos >> 12 & 15);
int y = pos & 255;
int z = (chunkZ << 4) + (pos >> 8 & 15);
@@ -52,7 +51,7 @@ public class ServerMultiBlockChangePacket implements Packet {
out.writeVarInt(this.records.length);
for(BlockChangeRecord record : this.records) {
out.writeShort((record.getPosition().getX() - (chunkX << 4)) << 12 | (record.getPosition().getZ() - (chunkZ << 4)) << 8 | record.getPosition().getY());
- BlockState.write(out, record.getBlock());
+ out.writeVarInt(record.getBlock());
}
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerNotifyClientPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerNotifyClientPacket.java
index 451678d3..c49b92d1 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerNotifyClientPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerNotifyClientPacket.java
@@ -2,7 +2,13 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
-import com.github.steveice10.mc.protocol.data.game.world.notify.*;
+import com.github.steveice10.mc.protocol.data.game.world.notify.ClientNotification;
+import com.github.steveice10.mc.protocol.data.game.world.notify.ClientNotificationValue;
+import com.github.steveice10.mc.protocol.data.game.world.notify.DemoMessageValue;
+import com.github.steveice10.mc.protocol.data.game.world.notify.EnterCreditsValue;
+import com.github.steveice10.mc.protocol.data.game.world.notify.RainStrengthValue;
+import com.github.steveice10.mc.protocol.data.game.world.notify.RespawnScreenValue;
+import com.github.steveice10.mc.protocol.data.game.world.notify.ThunderStrengthValue;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlayEffectPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlayEffectPacket.java
index e5d8ad60..282c27d5 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlayEffectPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlayEffectPacket.java
@@ -2,7 +2,6 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.MagicValues;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.mc.protocol.data.game.world.effect.BonemealGrowEffectData;
import com.github.steveice10.mc.protocol.data.game.world.effect.BreakBlockEffectData;
import com.github.steveice10.mc.protocol.data.game.world.effect.BreakPotionEffectData;
@@ -49,7 +48,7 @@ public class ServerPlayEffectPacket implements Packet {
} else if(this.effect == ParticleEffect.SMOKE) {
this.data = MagicValues.key(SmokeEffectData.class, value % 9);
} else if(this.effect == ParticleEffect.BREAK_BLOCK) {
- this.data = new BreakBlockEffectData(new BlockState(value));
+ this.data = new BreakBlockEffectData(value);
} else if(this.effect == ParticleEffect.BREAK_SPLASH_POTION) {
this.data = new BreakPotionEffectData(value);
} else if(this.effect == ParticleEffect.BONEMEAL_GROW) {
@@ -71,7 +70,7 @@ public class ServerPlayEffectPacket implements Packet {
} else if(this.data instanceof SmokeEffectData) {
value = MagicValues.value(Integer.class, (SmokeEffectData) this.data);
} else if(this.data instanceof BreakBlockEffectData) {
- value = ((BreakBlockEffectData) this.data).getBlockState().getId();
+ value = ((BreakBlockEffectData) this.data).getBlockState();
} else if(this.data instanceof BreakPotionEffectData) {
value = ((BreakPotionEffectData) this.data).getPotionId();
} else if(this.data instanceof BonemealGrowEffectData) {
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java
index fc57b1cd..4db28ac8 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerPlaySoundPacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.MagicValues;
+import com.github.steveice10.mc.protocol.data.UnmappedValueException;
import com.github.steveice10.mc.protocol.data.game.world.sound.BuiltinSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.CustomSound;
import com.github.steveice10.mc.protocol.data.game.world.sound.Sound;
@@ -35,7 +36,7 @@ public class ServerPlaySoundPacket implements Packet {
String value = in.readString();
try {
this.sound = MagicValues.key(BuiltinSound.class, value);
- } catch(IllegalArgumentException e) {
+ } catch(UnmappedValueException e) {
this.sound = new CustomSound(value);
}
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacket.java
index c83158a3..4178294d 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacket.java
@@ -1,6 +1,7 @@
package com.github.steveice10.mc.protocol.packet.login.server;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.packetlib.io.NetInput;
import com.github.steveice10.packetlib.io.NetOutput;
import com.github.steveice10.packetlib.packet.Packet;
@@ -21,17 +22,17 @@ public class LoginDisconnectPacket implements Packet {
private @NonNull Message reason;
public LoginDisconnectPacket(String text) {
- this(Message.fromString(text));
+ this(MessageSerializer.fromString(text));
}
@Override
public void read(NetInput in) throws IOException {
- this.reason = Message.fromString(in.readString());
+ this.reason = MessageSerializer.fromString(in.readString());
}
@Override
public void write(NetOutput out) throws IOException {
- out.writeString(this.reason.toJsonString());
+ out.writeString(MessageSerializer.toJsonString(this.reason));
}
@Override
diff --git a/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java b/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java
index 7687c958..3baa24a3 100644
--- a/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java
+++ b/src/main/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacket.java
@@ -3,6 +3,7 @@ package com.github.steveice10.mc.protocol.packet.status.server;
import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.util.Base64;
import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.MessageSerializer;
import com.github.steveice10.mc.protocol.data.status.PlayerInfo;
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
import com.github.steveice10.mc.protocol.data.status.VersionInfo;
@@ -20,10 +21,6 @@ import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
-import javax.imageio.ImageIO;
-import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@@ -54,8 +51,8 @@ public class StatusResponsePacket implements Packet {
PlayerInfo players = new PlayerInfo(plrs.get("max").getAsInt(), plrs.get("online").getAsInt(), profiles);
JsonElement desc = obj.get("description");
- Message description = Message.fromJson(desc);
- BufferedImage icon = null;
+ Message description = MessageSerializer.fromJson(desc);
+ byte[] icon = null;
if(obj.has("favicon")) {
icon = this.stringToIcon(obj.get("favicon").getAsString());
}
@@ -86,9 +83,9 @@ public class StatusResponsePacket implements Packet {
obj.add("version", ver);
obj.add("players", plrs);
- obj.add("description", this.info.getDescription().toJson());
- if(this.info.getIcon() != null) {
- obj.addProperty("favicon", this.iconToString(this.info.getIcon()));
+ obj.add("description", MessageSerializer.toJson(this.info.getDescription()));
+ if(this.info.getIconPng() != null) {
+ obj.addProperty("favicon", this.iconToString(this.info.getIconPng()));
}
out.writeString(obj.toString());
@@ -99,31 +96,15 @@ public class StatusResponsePacket implements Packet {
return false;
}
- private BufferedImage stringToIcon(String str) throws IOException {
+ private byte[] stringToIcon(String str) {
if(str.startsWith("data:image/png;base64,")) {
str = str.substring("data:image/png;base64,".length());
}
- byte[] bytes = Base64.decode(str.getBytes(StandardCharsets.UTF_8));
- ByteArrayInputStream in = new ByteArrayInputStream(bytes);
- BufferedImage icon = ImageIO.read(in);
- in.close();
- if(icon != null && (icon.getWidth() != 64 || icon.getHeight() != 64)) {
- throw new IOException("Icon must be 64x64.");
- }
-
- return icon;
+ return Base64.decode(str.getBytes(StandardCharsets.UTF_8));
}
- private String iconToString(BufferedImage icon) throws IOException {
- if(icon.getWidth() != 64 || icon.getHeight() != 64) {
- throw new IOException("Icon must be 64x64.");
- }
-
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ImageIO.write(icon, "PNG", out);
- out.close();
- byte[] encoded = Base64.encode(out.toByteArray());
- return "data:image/png;base64," + new String(encoded, StandardCharsets.UTF_8);
+ private String iconToString(byte[] icon) {
+ return "data:image/png;base64," + new String(Base64.encode(icon), StandardCharsets.UTF_8);
}
}
diff --git a/src/test/java/com/github/steveice10/mc/protocol/MinecraftProtocolTest.java b/src/test/java/com/github/steveice10/mc/protocol/MinecraftProtocolTest.java
index 681c16fe..37d20e21 100644
--- a/src/test/java/com/github/steveice10/mc/protocol/MinecraftProtocolTest.java
+++ b/src/test/java/com/github/steveice10/mc/protocol/MinecraftProtocolTest.java
@@ -47,7 +47,7 @@ public class MinecraftProtocolTest {
private static final ServerStatusInfo SERVER_INFO = new ServerStatusInfo(
VersionInfo.CURRENT,
new PlayerInfo(100, 0, new GameProfile[0]),
- new TextMessage("Hello world!"),
+ new TextMessage.Builder().text("Hello world!").build(),
null
);
private static final ServerJoinGamePacket JOIN_GAME_PACKET = new ServerJoinGamePacket(0, false, GameMode.SURVIVAL, GameMode.SURVIVAL, 1, new String[]{"minecraft:world"}, getDimensionTag(), "minecraft:overworld", "minecraft:world", 100, 0, 16, false, false, false, false);
diff --git a/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java b/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java
index 29cfc24c..d75d998f 100644
--- a/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java
+++ b/src/test/java/com/github/steveice10/mc/protocol/data/MagicValuesTest.java
@@ -23,6 +23,8 @@ import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierOper
import com.github.steveice10.mc.protocol.data.game.entity.attribute.ModifierType;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.MetadataType;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Pose;
+import com.github.steveice10.mc.protocol.data.game.entity.object.HangingDirection;
+import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType;
import com.github.steveice10.mc.protocol.data.game.entity.player.Animation;
import com.github.steveice10.mc.protocol.data.game.entity.player.BlockBreakStage;
import com.github.steveice10.mc.protocol.data.game.entity.player.CombatState;
@@ -32,11 +34,9 @@ import com.github.steveice10.mc.protocol.data.game.entity.player.InteractAction;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerAction;
import com.github.steveice10.mc.protocol.data.game.entity.player.PlayerState;
import com.github.steveice10.mc.protocol.data.game.entity.player.PositionElement;
-import com.github.steveice10.mc.protocol.data.game.entity.type.WeatherEntityType;
import com.github.steveice10.mc.protocol.data.game.entity.type.EntityType;
import com.github.steveice10.mc.protocol.data.game.entity.type.PaintingType;
-import com.github.steveice10.mc.protocol.data.game.entity.object.HangingDirection;
-import com.github.steveice10.mc.protocol.data.game.entity.object.MinecartType;
+import com.github.steveice10.mc.protocol.data.game.entity.type.WeatherEntityType;
import com.github.steveice10.mc.protocol.data.game.recipe.RecipeType;
import com.github.steveice10.mc.protocol.data.game.scoreboard.CollisionRule;
import com.github.steveice10.mc.protocol.data.game.scoreboard.NameTagVisibility;
diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java
index 6ef7dc73..f616c105 100644
--- a/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java
+++ b/src/test/java/com/github/steveice10/mc/protocol/packet/PacketTest.java
@@ -28,7 +28,7 @@ public abstract class PacketTest {
Packet decoded = this.createPacket(packet.getClass());
decoded.read(new StreamNetInput(new ByteArrayInputStream(encoded)));
- assertEquals("Decoded packet does not match original.", packet, decoded);
+ assertEquals("Decoded packet does not match original: " + packet + " vs " + decoded, packet, decoded);
}
}
diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacketTest.java
index 8bcd051a..530815ea 100644
--- a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacketTest.java
+++ b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerBlockChangePacketTest.java
@@ -2,7 +2,6 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.Position;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockChangeRecord;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.mc.protocol.packet.PacketTest;
import org.junit.Before;
@@ -11,7 +10,7 @@ public class ServerBlockChangePacketTest extends PacketTest {
public void setup() {
this.setPackets(
new ServerBlockChangePacket(new BlockChangeRecord(
- new Position(1, 61, -1), new BlockState(3)
+ new Position(1, 61, -1), 3
))
);
}
diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerChunkDataPacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerChunkDataPacketTest.java
index ff83e9fa..d1d55c7c 100644
--- a/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerChunkDataPacketTest.java
+++ b/src/test/java/com/github/steveice10/mc/protocol/packet/ingame/server/world/ServerChunkDataPacketTest.java
@@ -2,7 +2,6 @@ package com.github.steveice10.mc.protocol.packet.ingame.server.world;
import com.github.steveice10.mc.protocol.data.game.chunk.Chunk;
import com.github.steveice10.mc.protocol.data.game.chunk.Column;
-import com.github.steveice10.mc.protocol.data.game.world.block.BlockState;
import com.github.steveice10.mc.protocol.packet.PacketTest;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import org.junit.Before;
@@ -11,7 +10,7 @@ public class ServerChunkDataPacketTest extends PacketTest {
@Before
public void setup() {
Chunk chunk = new Chunk();
- chunk.set(0, 0, 0, new BlockState(10));
+ chunk.set(0, 0, 0, 10);
this.setPackets(
new ServerChunkDataPacket(
diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacketTest.java
index a559c43f..9c6066fc 100644
--- a/src/test/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacketTest.java
+++ b/src/test/java/com/github/steveice10/mc/protocol/packet/login/server/LoginDisconnectPacketTest.java
@@ -1,6 +1,6 @@
package com.github.steveice10.mc.protocol.packet.login.server;
-import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.TextMessage;
import com.github.steveice10.mc.protocol.packet.PacketTest;
import org.junit.Before;
@@ -8,6 +8,6 @@ public class LoginDisconnectPacketTest extends PacketTest {
@Before
public void setup() {
this.setPackets(new LoginDisconnectPacket("Message"),
- new LoginDisconnectPacket(Message.fromString("Message")));
+ new LoginDisconnectPacket(new TextMessage.Builder().text("Message").build()));
}
}
diff --git a/src/test/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacketTest.java b/src/test/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacketTest.java
index c4b24b78..f6c81149 100644
--- a/src/test/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacketTest.java
+++ b/src/test/java/com/github/steveice10/mc/protocol/packet/status/server/StatusResponsePacketTest.java
@@ -1,7 +1,7 @@
package com.github.steveice10.mc.protocol.packet.status.server;
import com.github.steveice10.mc.auth.data.GameProfile;
-import com.github.steveice10.mc.protocol.data.message.Message;
+import com.github.steveice10.mc.protocol.data.message.TextMessage;
import com.github.steveice10.mc.protocol.data.status.PlayerInfo;
import com.github.steveice10.mc.protocol.data.status.ServerStatusInfo;
import com.github.steveice10.mc.protocol.data.status.VersionInfo;
@@ -19,7 +19,7 @@ public class StatusResponsePacketTest extends PacketTest {
new PlayerInfo(100, 10, new GameProfile[] {
new GameProfile(UUID.randomUUID(), "Username")
}),
- Message.fromString("Description"),
+ new TextMessage.Builder().text("Description").build(),
null
)
));