use of com.viaversion.viaversion.api.minecraft.TagData in project ViaBackwards by ViaVersion.
the class Protocol1_16_4To1_17 method registerPackets.
@Override
protected void registerPackets() {
executeAsyncAfterLoaded(Protocol1_17To1_16_4.class, MAPPINGS::load);
translatableRewriter.registerChatMessage(ClientboundPackets1_17.CHAT_MESSAGE);
translatableRewriter.registerBossBar(ClientboundPackets1_17.BOSSBAR);
translatableRewriter.registerDisconnect(ClientboundPackets1_17.DISCONNECT);
translatableRewriter.registerTabList(ClientboundPackets1_17.TAB_LIST);
translatableRewriter.registerOpenWindow(ClientboundPackets1_17.OPEN_WINDOW);
translatableRewriter.registerPing();
blockItemPackets = new BlockItemPackets1_17(this);
blockItemPackets.register();
entityRewriter.register();
SoundRewriter soundRewriter = new SoundRewriter(this);
soundRewriter.registerSound(ClientboundPackets1_17.SOUND);
soundRewriter.registerSound(ClientboundPackets1_17.ENTITY_SOUND);
soundRewriter.registerNamedSound(ClientboundPackets1_17.NAMED_SOUND);
soundRewriter.registerStopSound(ClientboundPackets1_17.STOP_SOUND);
TagRewriter tagRewriter = new TagRewriter(this);
registerClientbound(ClientboundPackets1_17.TAGS, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Map<String, List<TagData>> tags = new HashMap<>();
int length = wrapper.read(Type.VAR_INT);
for (int i = 0; i < length; i++) {
String resourceKey = wrapper.read(Type.STRING);
if (resourceKey.startsWith("minecraft:")) {
resourceKey = resourceKey.substring(10);
}
List<TagData> tagList = new ArrayList<>();
tags.put(resourceKey, tagList);
int tagLength = wrapper.read(Type.VAR_INT);
for (int j = 0; j < tagLength; j++) {
String identifier = wrapper.read(Type.STRING);
int[] entries = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
tagList.add(new TagData(identifier, entries));
}
}
// Put them into the hardcoded order of Vanilla tags (and only those), rewrite ids
for (RegistryType type : RegistryType.getValues()) {
List<TagData> tagList = tags.get(type.getResourceLocation());
IdRewriteFunction rewriter = tagRewriter.getRewriter(type);
wrapper.write(Type.VAR_INT, tagList.size());
for (TagData tagData : tagList) {
int[] entries = tagData.entries();
if (rewriter != null) {
// Handle id rewriting now
IntList idList = new IntArrayList(entries.length);
for (int id : entries) {
int mappedId = rewriter.rewrite(id);
if (mappedId != -1) {
idList.add(mappedId);
}
}
entries = idList.toArray(EMPTY_ARRAY);
}
wrapper.write(Type.STRING, tagData.identifier());
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, entries);
}
// Stop after the entity types
if (type == RegistryType.ENTITY) {
break;
}
}
});
}
});
new StatisticsRewriter(this).register(ClientboundPackets1_17.STATISTICS);
registerClientbound(ClientboundPackets1_17.RESOURCE_PACK, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
wrapper.passthrough(Type.STRING);
wrapper.passthrough(Type.STRING);
// Required
wrapper.read(Type.BOOLEAN);
// Prompt message
wrapper.read(Type.OPTIONAL_COMPONENT);
});
}
});
registerClientbound(ClientboundPackets1_17.EXPLOSION, new PacketRemapper() {
@Override
public void registerMap() {
// X
map(Type.FLOAT);
// Y
map(Type.FLOAT);
// Z
map(Type.FLOAT);
// Strength
map(Type.FLOAT);
handler(wrapper -> {
// Collection length
wrapper.write(Type.INT, wrapper.read(Type.VAR_INT));
});
}
});
registerClientbound(ClientboundPackets1_17.SPAWN_POSITION, new PacketRemapper() {
@Override
public void registerMap() {
map(Type.POSITION1_14);
handler(wrapper -> {
// Angle (which Mojang just forgot to write to the buffer, lol)
wrapper.read(Type.FLOAT);
});
}
});
registerClientbound(ClientboundPackets1_17.PING, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
wrapper.cancel();
int id = wrapper.read(Type.INT);
short shortId = (short) id;
if (id == shortId && ViaBackwards.getConfig().handlePingsAsInvAcknowledgements()) {
wrapper.user().get(PingRequests.class).addId(shortId);
// Send inventory acknowledgement to replace ping packet functionality in the unsigned byte range
PacketWrapper acknowledgementPacket = wrapper.create(ClientboundPackets1_16_2.WINDOW_CONFIRMATION);
// Inventory id
acknowledgementPacket.write(Type.UNSIGNED_BYTE, (short) 0);
// Confirmation id
acknowledgementPacket.write(Type.SHORT, shortId);
// Accepted
acknowledgementPacket.write(Type.BOOLEAN, false);
acknowledgementPacket.send(Protocol1_16_4To1_17.class);
return;
}
// Plugins expecting a real response will have to handle this accordingly themselves
PacketWrapper pongPacket = wrapper.create(ServerboundPackets1_17.PONG);
pongPacket.write(Type.INT, id);
pongPacket.sendToServer(Protocol1_16_4To1_17.class);
});
}
});
registerServerbound(ServerboundPackets1_16_2.CLIENT_SETTINGS, new PacketRemapper() {
@Override
public void registerMap() {
// Locale
map(Type.STRING);
// View distance
map(Type.BYTE);
// Chat mode
map(Type.VAR_INT);
// Chat colors
map(Type.BOOLEAN);
// Chat flags
map(Type.UNSIGNED_BYTE);
// Main hand
map(Type.VAR_INT);
handler(wrapper -> {
// Text filtering
wrapper.write(Type.BOOLEAN, false);
});
}
});
// TODO translatables
mergePacket(ClientboundPackets1_17.TITLE_TEXT, ClientboundPackets1_16_2.TITLE, 0);
mergePacket(ClientboundPackets1_17.TITLE_SUBTITLE, ClientboundPackets1_16_2.TITLE, 1);
mergePacket(ClientboundPackets1_17.ACTIONBAR, ClientboundPackets1_16_2.TITLE, 2);
mergePacket(ClientboundPackets1_17.TITLE_TIMES, ClientboundPackets1_16_2.TITLE, 3);
registerClientbound(ClientboundPackets1_17.CLEAR_TITLES, ClientboundPackets1_16_2.TITLE, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
if (wrapper.read(Type.BOOLEAN)) {
// Reset times
wrapper.write(Type.VAR_INT, 5);
} else {
// Simple clear
wrapper.write(Type.VAR_INT, 4);
}
});
}
});
cancelClientbound(ClientboundPackets1_17.ADD_VIBRATION_SIGNAL);
}
use of com.viaversion.viaversion.api.minecraft.TagData in project ViaVersion by ViaVersion.
the class TagRewriter method handle.
public void handle(PacketWrapper wrapper, @Nullable IdRewriteFunction rewriteFunction, @Nullable List<TagData> newTags) throws Exception {
int tagsSize = wrapper.read(Type.VAR_INT);
// add new tags count
wrapper.write(Type.VAR_INT, newTags != null ? tagsSize + newTags.size() : tagsSize);
for (int i = 0; i < tagsSize; i++) {
wrapper.passthrough(Type.STRING);
int[] ids = wrapper.read(Type.VAR_INT_ARRAY_PRIMITIVE);
if (rewriteFunction != null) {
// Map ids and filter out new blocks
IntList idList = new IntArrayList(ids.length);
for (int id : ids) {
int mappedId = rewriteFunction.rewrite(id);
if (mappedId != -1) {
idList.add(mappedId);
}
}
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, idList.toArray(EMPTY_ARRAY));
} else {
// Write the original array
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, ids);
}
}
// Send new tags if present
if (newTags != null) {
for (TagData tag : newTags) {
wrapper.write(Type.STRING, tag.identifier());
wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, tag.entries());
}
}
}
use of com.viaversion.viaversion.api.minecraft.TagData in project ViaVersion by ViaVersion.
the class MappingDataBase method loadTags.
private void loadTags(RegistryType type, JsonObject object, Object2IntMap<String> typeMapping) {
JsonObject tags = object.getAsJsonObject(type.resourceLocation());
List<TagData> tagsList = new ArrayList<>(tags.size());
for (Map.Entry<String, JsonElement> entry : tags.entrySet()) {
JsonArray array = entry.getValue().getAsJsonArray();
int[] entries = new int[array.size()];
int i = 0;
for (JsonElement element : array) {
String stringId = element.getAsString();
if (!typeMapping.containsKey(stringId) && !typeMapping.containsKey(stringId = stringId.replace("minecraft:", ""))) {
// aaa
getLogger().warning(type + " Tags contains invalid type identifier " + stringId + " in tag " + entry.getKey());
continue;
}
entries[i++] = typeMapping.getInt(stringId);
}
tagsList.add(new TagData(entry.getKey(), entries));
}
this.tags.put(type, tagsList);
}
use of com.viaversion.viaversion.api.minecraft.TagData in project ViaVersion by ViaVersion.
the class TagRewriter method addTag.
/**
* Adds a tag type to be filled with the given type ids after being mapped to new ids.
*
* @param tagType registry tag type
* @param tagId tag id
* @param unmappedIds unmapped type ids
*/
public void addTag(RegistryType tagType, String tagId, int... unmappedIds) {
List<TagData> newTags = getOrComputeNewTags(tagType);
IdRewriteFunction rewriteFunction = getRewriter(tagType);
for (int i = 0; i < unmappedIds.length; i++) {
int oldId = unmappedIds[i];
unmappedIds[i] = rewriteFunction.rewrite(oldId);
}
newTags.add(new TagData(tagId, unmappedIds));
}
Aggregations