use of net.minecraft.network.packet.c2s.play.BookUpdateC2SPacket in project Client by MatHax.
the class EditBookTitleAndAuthorScreen method initWidgets.
@Override
public void initWidgets() {
WTable t = add(theme.table()).expandX().widget();
t.add(theme.label("Title"));
WTextBox title = t.add(theme.textBox(itemStack.getOrCreateNbt().getString("title"))).minWidth(220).expandX().widget();
t.row();
t.add(theme.label("Author"));
WTextBox author = t.add(theme.textBox(itemStack.getNbt().getString("author"))).minWidth(220).expandX().widget();
t.row();
t.add(theme.button("Done")).expandX().widget().action = () -> {
itemStack.getNbt().putString("author", author.get());
itemStack.getNbt().putString("title", title.get());
BookScreen.Contents contents = new BookScreen.WrittenBookContents(itemStack);
List<String> pages = new ArrayList<>(contents.getPageCount());
for (int i = 0; i < contents.getPageCount(); i++) pages.add(contents.getPage(i).getString());
mc.getNetworkHandler().sendPacket(new BookUpdateC2SPacket(hand == Hand.MAIN_HAND ? mc.player.getInventory().selectedSlot : 40, pages, Optional.of(title.get())));
close();
};
}
use of net.minecraft.network.packet.c2s.play.BookUpdateC2SPacket in project Client by MatHax.
the class BookBot method writeBook.
private void writeBook(PrimitiveIterator.OfInt chars) {
ArrayList<String> pages = new ArrayList<>();
for (int pageI = 0; pageI < (mode.get() == Mode.File ? 100 : this.pages.get()); pageI++) {
// Check if the stream is empty before creating a new page
if (!chars.hasNext())
break;
StringBuilder page = new StringBuilder();
for (int lineI = 0; lineI < 13; lineI++) {
// Check if the stream is empty before creating a new line
if (!chars.hasNext())
break;
double lineWidth = 0;
StringBuilder line = new StringBuilder();
while (true) {
// Check if the stream is empty
if (!chars.hasNext())
break;
// Get the next character
int nextChar = chars.nextInt();
// Ignore newline chars when writing lines, should already be organised
if (nextChar == '\r' || nextChar == '\n')
break;
// Make sure the character will fit on the line
double charWidth = ((TextHandlerAccessor) mc.textRenderer.getTextHandler()).getWidthRetriever().getWidth(nextChar, Style.EMPTY);
if (lineWidth + charWidth > 114)
break;
// Append it to the line
line.appendCodePoint(nextChar);
lineWidth += charWidth;
}
// Append the line to the page
page.append(line).append('\n');
}
// Append page to the page list
pages.add(page.toString());
}
// Get the title with count
String title = name.get();
if (count.get() && bookCount != 0)
title += " #" + bookCount;
// Write data to book
mc.player.getMainHandStack().setSubNbt("title", NbtString.of(title));
mc.player.getMainHandStack().setSubNbt("author", NbtString.of(mc.player.getGameProfile().getName()));
// Write pages NBT
NbtList pageNbt = new NbtList();
pages.stream().map(NbtString::of).forEach(pageNbt::add);
if (!pages.isEmpty())
mc.player.getMainHandStack().setSubNbt("pages", pageNbt);
// Send book update to server
mc.player.networkHandler.sendPacket(new BookUpdateC2SPacket(mc.player.getInventory().selectedSlot, pages, Optional.of(title)));
bookCount++;
}
Aggregations