use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.
the class BannerHandler method transform.
@Override
public int transform(UserConnection user, CompoundTag tag) {
BlockStorage storage = user.get(BlockStorage.class);
Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));
if (!storage.contains(position)) {
Via.getPlatform().getLogger().warning("Received an banner color update packet, but there is no banner! O_o " + tag);
return -1;
}
int blockId = storage.get(position).getOriginal();
Tag base = tag.get("Base");
int color = 0;
if (base != null) {
color = ((NumberTag) tag.get("Base")).asInt();
}
// Standing banner
if (blockId >= BANNER_START && blockId <= BANNER_STOP) {
blockId += ((15 - color) * 16);
// Wall banner
} else if (blockId >= WALL_BANNER_START && blockId <= WALL_BANNER_STOP) {
blockId += ((15 - color) * 4);
} else {
Via.getPlatform().getLogger().warning("Why does this block have the banner block entity? :(" + tag);
}
if (tag.get("Patterns") instanceof ListTag) {
for (Tag pattern : (ListTag) tag.get("Patterns")) {
if (pattern instanceof CompoundTag) {
Tag c = ((CompoundTag) pattern).get("Color");
if (c instanceof IntTag) {
// Invert color id
((IntTag) c).setValue(15 - (int) c.getValue());
}
}
}
}
Tag name = tag.get("CustomName");
if (name instanceof StringTag) {
((StringTag) name).setValue(ChatRewriter.legacyTextToJsonString(((StringTag) name).getValue()));
}
return blockId;
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.
the class WorldPackets method register.
public static void register(Protocol1_17To1_16_4 protocol) {
BlockRewriter blockRewriter = new BlockRewriter(protocol, Type.POSITION1_14);
blockRewriter.registerBlockAction(ClientboundPackets1_16_2.BLOCK_ACTION);
blockRewriter.registerBlockChange(ClientboundPackets1_16_2.BLOCK_CHANGE);
blockRewriter.registerVarLongMultiBlockChange(ClientboundPackets1_16_2.MULTI_BLOCK_CHANGE);
blockRewriter.registerAcknowledgePlayerDigging(ClientboundPackets1_16_2.ACKNOWLEDGE_PLAYER_DIGGING);
protocol.registerClientbound(ClientboundPackets1_16_2.WORLD_BORDER, null, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
// Border packet actions have been split into individual packets (the content hasn't changed)
int type = wrapper.read(Type.VAR_INT);
ClientboundPacketType packetType;
switch(type) {
case 0:
packetType = ClientboundPackets1_17.WORLD_BORDER_SIZE;
break;
case 1:
packetType = ClientboundPackets1_17.WORLD_BORDER_LERP_SIZE;
break;
case 2:
packetType = ClientboundPackets1_17.WORLD_BORDER_CENTER;
break;
case 3:
packetType = ClientboundPackets1_17.WORLD_BORDER_INIT;
break;
case 4:
packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DELAY;
break;
case 5:
packetType = ClientboundPackets1_17.WORLD_BORDER_WARNING_DISTANCE;
break;
default:
throw new IllegalArgumentException("Invalid world border type received: " + type);
}
wrapper.setId(packetType.getId());
});
}
});
protocol.registerClientbound(ClientboundPackets1_16_2.UPDATE_LIGHT, new PacketRemapper() {
@Override
public void registerMap() {
// x
map(Type.VAR_INT);
// y
map(Type.VAR_INT);
// trust edges
map(Type.BOOLEAN);
handler(wrapper -> {
int skyLightMask = wrapper.read(Type.VAR_INT);
int blockLightMask = wrapper.read(Type.VAR_INT);
// Now all written as a representation of BitSets
// Sky light mask
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(skyLightMask));
// Block light mask
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(blockLightMask));
// Empty sky light mask
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
// Empty block light mask
wrapper.write(Type.LONG_ARRAY_PRIMITIVE, toBitSetLongArray(wrapper.read(Type.VAR_INT)));
writeLightArrays(wrapper, skyLightMask);
writeLightArrays(wrapper, blockLightMask);
});
}
private void writeLightArrays(PacketWrapper wrapper, int bitMask) throws Exception {
List<byte[]> light = new ArrayList<>();
for (int i = 0; i < 18; i++) {
if (isSet(bitMask, i)) {
light.add(wrapper.read(Type.BYTE_ARRAY_PRIMITIVE));
}
}
// Now needs the length of the bytearray-array
wrapper.write(Type.VAR_INT, light.size());
for (byte[] bytes : light) {
wrapper.write(Type.BYTE_ARRAY_PRIMITIVE, bytes);
}
}
private long[] toBitSetLongArray(int bitmask) {
return new long[] { bitmask };
}
private boolean isSet(int mask, int i) {
return (mask & (1 << i)) != 0;
}
});
protocol.registerClientbound(ClientboundPackets1_16_2.CHUNK_DATA, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
Chunk chunk = wrapper.read(new Chunk1_16_2Type());
if (!chunk.isFullChunk()) {
// All chunks are full chunk packets now (1.16 already stopped sending non-full chunks)
// Construct multi block change packets instead
// Height map updates are lost (unless we want to fully cache and resend entire chunks)
// Block entities are always empty for non-full chunks in Vanilla
writeMultiBlockChangePacket(wrapper, chunk);
wrapper.cancel();
return;
}
// Normal full chunk writing
wrapper.write(new Chunk1_17Type(chunk.getSections().length), chunk);
// 1.17 uses a bitset for the mask
chunk.setChunkMask(BitSet.valueOf(new long[] { chunk.getBitmask() }));
for (int s = 0; s < chunk.getSections().length; s++) {
ChunkSection section = chunk.getSections()[s];
if (section == null)
continue;
for (int i = 0; i < section.getPaletteSize(); i++) {
int old = section.getPaletteEntry(i);
section.setPaletteEntry(i, protocol.getMappingData().getNewBlockStateId(old));
}
}
});
}
});
protocol.registerClientbound(ClientboundPackets1_16_2.JOIN_GAME, new PacketRemapper() {
@Override
public void registerMap() {
// Entity ID
map(Type.INT);
// Hardcore
map(Type.BOOLEAN);
// Gamemode
map(Type.UNSIGNED_BYTE);
// Previous Gamemode
map(Type.BYTE);
// World List
map(Type.STRING_ARRAY);
// Registry
map(Type.NBT);
// Current dimension
map(Type.NBT);
handler(wrapper -> {
// Add new dimension fields
CompoundTag dimensionRegistry = wrapper.get(Type.NBT, 0).get("minecraft:dimension_type");
ListTag dimensions = dimensionRegistry.get("value");
for (Tag dimension : dimensions) {
CompoundTag dimensionCompound = ((CompoundTag) dimension).get("element");
addNewDimensionData(dimensionCompound);
}
CompoundTag currentDimensionTag = wrapper.get(Type.NBT, 1);
addNewDimensionData(currentDimensionTag);
UserConnection user = wrapper.user();
user.getEntityTracker(Protocol1_17To1_16_4.class).addEntity(wrapper.get(Type.INT, 0), Entity1_17Types.PLAYER);
});
}
});
protocol.registerClientbound(ClientboundPackets1_16_2.RESPAWN, new PacketRemapper() {
@Override
public void registerMap() {
handler(wrapper -> {
CompoundTag dimensionData = wrapper.passthrough(Type.NBT);
addNewDimensionData(dimensionData);
});
}
});
blockRewriter.registerEffect(ClientboundPackets1_16_2.EFFECT, 1010, 2001);
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.
the class ItemRewriter method toClient.
public static void toClient(Item item) {
if (item != null) {
if (item.identifier() == 383 && item.data() != 0) {
// Monster Egg
CompoundTag tag = item.tag();
if (tag == null) {
tag = new CompoundTag();
}
CompoundTag entityTag = new CompoundTag();
String entityName = ENTTIY_ID_TO_NAME.get((int) item.data());
if (entityName != null) {
StringTag id = new StringTag(entityName);
entityTag.put("id", id);
tag.put("EntityTag", entityTag);
}
item.setTag(tag);
item.setData((short) 0);
}
if (item.identifier() == 373) {
// Potion
CompoundTag tag = item.tag();
if (tag == null) {
tag = new CompoundTag();
}
if (item.data() >= 16384) {
// splash id
item.setIdentifier(438);
item.setData((short) (item.data() - 8192));
}
String name = potionNameFromDamage(item.data());
StringTag potion = new StringTag("minecraft:" + name);
tag.put("Potion", potion);
item.setTag(tag);
item.setData((short) 0);
}
if (item.identifier() == 387) {
// WRITTEN_BOOK
CompoundTag tag = item.tag();
if (tag == null) {
tag = new CompoundTag();
}
ListTag pages = tag.get("pages");
if (pages == null) {
pages = new ListTag(Collections.singletonList(new StringTag(Protocol1_9To1_8.fixJson("").toString())));
tag.put("pages", pages);
item.setTag(tag);
return;
}
for (int i = 0; i < pages.size(); i++) {
if (!(pages.get(i) instanceof StringTag))
continue;
StringTag page = pages.get(i);
page.setValue(Protocol1_9To1_8.fixJson(page.getValue()).toString());
}
item.setTag(tag);
}
}
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.
the class ItemRewriter method rewriteBookToServer.
public static void rewriteBookToServer(Item item) {
int id = item.identifier();
if (id != 387) {
return;
}
CompoundTag tag = item.tag();
ListTag pages = tag.get("pages");
if (pages == null) {
// is this even possible?
return;
}
for (int i = 0; i < pages.size(); i++) {
Tag pageTag = pages.get(i);
if (!(pageTag instanceof StringTag)) {
continue;
}
StringTag stag = (StringTag) pageTag;
String value = stag.getValue();
if (value.replaceAll(" ", "").isEmpty()) {
value = "\"" + fixBookSpaceChars(value) + "\"";
} else {
value = fixBookSpaceChars(value);
}
stag.setValue(value);
}
}
use of com.github.steveice10.opennbt.tag.builtin.ListTag in project ViaVersion by ViaVersion.
the class TagStringReader method list.
public ListTag list() throws StringTagParseException {
final ListTag listTag = new ListTag();
this.buffer.expect(Tokens.ARRAY_BEGIN);
final boolean prefixedIndex = this.acceptLegacy && this.buffer.peek() == '0' && this.buffer.peek(1) == ':';
if (!prefixedIndex && this.buffer.takeIf(Tokens.ARRAY_END)) {
return listTag;
}
while (this.buffer.hasMore()) {
if (prefixedIndex) {
this.buffer.takeUntil(':');
}
final Tag next = this.tag();
listTag.add(next);
if (this.separatorOrCompleteWith(Tokens.ARRAY_END)) {
return listTag;
}
}
throw this.buffer.makeError("Reached end of file without end of list tag!");
}
Aggregations