use of com.viaversion.viaversion.rewriter.ComponentRewriter in project ViaVersion by ViaVersion.
the class Protocol1_16To1_15_2 method registerPackets.
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
WorldPackets.register(this);
tagRewriter = new TagRewriter(this);
tagRewriter.register(ClientboundPackets1_15.TAGS, RegistryType.ENTITY);
new StatisticsRewriter(this).register(ClientboundPackets1_15.STATISTICS);
// Login Success
registerClientbound(State.LOGIN, 0x02, 0x02, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Transform string to int array
UUID uuid = UUID.fromString(wrapper.read(Type.STRING));
wrapper.write(Type.UUID_INT_ARRAY, uuid);
});
}
});
// Motd Status - line breaks are no longer allowed for player samples
registerClientbound(State.STATUS, 0x00, 0x00, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
String original = wrapper.passthrough(Type.STRING);
JsonObject object = GsonUtil.getGson().fromJson(original, JsonObject.class);
JsonObject players = object.getAsJsonObject("players");
if (players == null)
return;
JsonArray sample = players.getAsJsonArray("sample");
if (sample == null)
return;
JsonArray splitSamples = new JsonArray();
for (JsonElement element : sample) {
JsonObject playerInfo = element.getAsJsonObject();
String name = playerInfo.getAsJsonPrimitive("name").getAsString();
if (name.indexOf('\n') == -1) {
splitSamples.add(playerInfo);
continue;
}
String id = playerInfo.getAsJsonPrimitive("id").getAsString();
for (String s : name.split("\n")) {
JsonObject newSample = new JsonObject();
newSample.addProperty("name", s);
newSample.addProperty("id", id);
splitSamples.add(newSample);
}
}
// Replace data if changed
if (splitSamples.size() != sample.size()) {
players.add("sample", splitSamples);
wrapper.set(Type.STRING, 0, object.toString());
}
});
}
});
ComponentRewriter componentRewriter = new TranslationMappings(this);
// Handle (relevant) component cases for translatable and score changes
registerClientbound(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.COMPONENT);
map(Type.BYTE);
handler(wrapper -> {
componentRewriter.processText(wrapper.get(Type.COMPONENT, 0));
// Sender uuid - always send as 'system'
wrapper.write(Type.UUID, ZERO_UUID);
});
}
});
componentRewriter.registerBossBar(ClientboundPackets1_15.BOSSBAR);
componentRewriter.registerTitle(ClientboundPackets1_15.TITLE);
componentRewriter.registerCombatEvent(ClientboundPackets1_15.COMBAT_EVENT);
SoundRewriter soundRewriter = new SoundRewriter(this);
soundRewriter.registerSound(ClientboundPackets1_15.SOUND);
soundRewriter.registerSound(ClientboundPackets1_15.ENTITY_SOUND);
registerServerbound(ServerboundPackets1_16.INTERACT_ENTITY, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Entity Id
wrapper.passthrough(Type.VAR_INT);
int action = wrapper.passthrough(Type.VAR_INT);
if (action == 0 || action == 2) {
if (action == 2) {
// Location
wrapper.passthrough(Type.FLOAT);
wrapper.passthrough(Type.FLOAT);
wrapper.passthrough(Type.FLOAT);
}
// Hand
wrapper.passthrough(Type.VAR_INT);
}
// New boolean: Whether the client is sneaking/pressing shift
wrapper.read(Type.BOOLEAN);
});
}
});
if (Via.getConfig().isIgnoreLong1_16ChannelNames()) {
registerServerbound(ServerboundPackets1_16.PLUGIN_MESSAGE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
String channel = wrapper.passthrough(Type.STRING);
if (channel.length() > 32) {
if (!Via.getConfig().isSuppressConversionWarnings()) {
Via.getPlatform().getLogger().warning("Ignoring incoming plugin channel, as it is longer than 32 characters: " + channel);
}
wrapper.cancel();
} else if (channel.equals("minecraft:register") || channel.equals("minecraft:unregister")) {
String[] channels = new String(wrapper.read(Type.REMAINING_BYTES), StandardCharsets.UTF_8).split("\0");
List<String> checkedChannels = new ArrayList<>(channels.length);
for (String registeredChannel : channels) {
if (registeredChannel.length() > 32) {
if (!Via.getConfig().isSuppressConversionWarnings()) {
Via.getPlatform().getLogger().warning("Ignoring incoming plugin channel register of '" + registeredChannel + "', as it is longer than 32 characters");
}
continue;
}
checkedChannels.add(registeredChannel);
}
if (checkedChannels.isEmpty()) {
wrapper.cancel();
return;
}
wrapper.write(Type.REMAINING_BYTES, Joiner.on('\0').join(checkedChannels).getBytes(StandardCharsets.UTF_8));
}
});
}
});
}
registerServerbound(ServerboundPackets1_16.PLAYER_ABILITIES, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
wrapper.passthrough(Type.BYTE);
// Flying and walking speed - not important anyways
wrapper.write(Type.FLOAT, 0.05F);
wrapper.write(Type.FLOAT, 0.1F);
});
}
});
cancelServerbound(ServerboundPackets1_16.GENERATE_JIGSAW);
cancelServerbound(ServerboundPackets1_16.UPDATE_JIGSAW_BLOCK);
}
use of com.viaversion.viaversion.rewriter.ComponentRewriter in project ViaVersion by ViaVersion.
the class Protocol1_14To1_13_2 method registerPackets.
@Override
protected void registerPackets() {
metadataRewriter.register();
itemRewriter.register();
EntityPackets.register(this);
WorldPackets.register(this);
PlayerPackets.register(this);
new SoundRewriter(this).registerSound(ClientboundPackets1_13.SOUND);
new StatisticsRewriter(this).register(ClientboundPackets1_13.STATISTICS);
ComponentRewriter componentRewriter = new ComponentRewriter1_14(this);
componentRewriter.registerComponentPacket(ClientboundPackets1_13.CHAT_MESSAGE);
CommandRewriter1_14 commandRewriter = new CommandRewriter1_14(this);
commandRewriter.registerDeclareCommands(ClientboundPackets1_13.DECLARE_COMMANDS);
registerClientbound(ClientboundPackets1_13.TAGS, new PacketRemapper() {
@Override
public void registerMap() {
handler(new PacketHandler() {
@Override
public void handle(PacketWrapper wrapper) throws Exception {
int blockTagsSize = wrapper.read(Type.VAR_INT);
// block tags
wrapper.write(Type.VAR_INT, blockTagsSize + 6);
for (int i = 0; i < blockTagsSize; i++) {
wrapper.passthrough(Type.STRING);
int[] blockIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
for (int j = 0; j < blockIds.length; j++) {
blockIds[j] = getMappingData().getNewBlockId(blockIds[j]);
}
}
// Minecraft crashes if we not send signs tags
wrapper.write(Type.STRING, "minecraft:signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewBlockId(150), getMappingData().getNewBlockId(155) });
wrapper.write(Type.STRING, "minecraft:wall_signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewBlockId(155) });
wrapper.write(Type.STRING, "minecraft:standing_signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewBlockId(150) });
// Fences and walls tags - used for block connections
wrapper.write(Type.STRING, "minecraft:fences");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 189, 248, 472, 473, 474, 475 });
wrapper.write(Type.STRING, "minecraft:walls");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 271, 272 });
wrapper.write(Type.STRING, "minecraft:wooden_fences");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 189, 472, 473, 474, 475 });
int itemTagsSize = wrapper.read(Type.VAR_INT);
// item tags
wrapper.write(Type.VAR_INT, itemTagsSize + 2);
for (int i = 0; i < itemTagsSize; i++) {
wrapper.passthrough(Type.STRING);
int[] itemIds = wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
for (int j = 0; j < itemIds.length; j++) {
itemIds[j] = getMappingData().getNewItemId(itemIds[j]);
}
}
// Should fix fuel shift clicking
wrapper.write(Type.STRING, "minecraft:signs");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { getMappingData().getNewItemId(541) });
// Arrows tag (used by bow)
wrapper.write(Type.STRING, "minecraft:arrows");
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, new int[] { 526, 825, 826 });
// fluid tags
int fluidTagsSize = wrapper.passthrough(Type.VAR_INT);
for (int i = 0; i < fluidTagsSize; i++) {
wrapper.passthrough(Type.STRING);
wrapper.passthrough(Type.VAR_INT_ARRAY_PRIMITIVE);
}
// new entity tags - do we need to send this?
wrapper.write(Type.VAR_INT, 0);
}
});
}
});
// Set Difficulty packet added in 19w11a
cancelServerbound(ServerboundPackets1_14.SET_DIFFICULTY);
// Lock Difficulty packet added in 19w11a
cancelServerbound(ServerboundPackets1_14.LOCK_DIFFICULTY);
// Unknown packet added in 19w13a
cancelServerbound(ServerboundPackets1_14.UPDATE_JIGSAW_BLOCK);
}
Aggregations