Add some comments and fix book editing in creative

This commit is contained in:
Camotoy 2021-01-19 19:01:51 -05:00
parent 2304c1375e
commit ca620d6dd8
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
3 changed files with 44 additions and 39 deletions

View file

@ -26,7 +26,6 @@
package org.geysermc.connector.network.translators.bedrock; package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientEditBookPacket; import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientEditBookPacket;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag; import com.github.steveice10.opennbt.tag.builtin.ListTag;
@ -54,51 +53,48 @@ public class BedrockBookEditTranslator extends PacketTranslator<BookEditPacket>
List<Tag> pages = tag.contains("pages") ? new LinkedList<>(((ListTag) tag.get("pages")).getValue()) : new LinkedList<>(); List<Tag> pages = tag.contains("pages") ? new LinkedList<>(((ListTag) tag.get("pages")).getValue()) : new LinkedList<>();
int page = packet.getPageNumber(); int page = packet.getPageNumber();
// Creative edits the NBT for us switch (packet.getAction()) {
if (session.getGameMode() != GameMode.CREATIVE) { case ADD_PAGE: {
switch (packet.getAction()) { // Add empty pages in between
case ADD_PAGE: { for (int i = pages.size(); i < page; i++) {
pages.add(i, new StringTag("", ""));
}
pages.add(page, new StringTag("", packet.getText()));
break;
}
// Called whenever a page is modified
case REPLACE_PAGE: {
if (page < pages.size()) {
pages.set(page, new StringTag("", packet.getText()));
} else {
// Add empty pages in between // Add empty pages in between
for (int i = pages.size(); i < page; i++) { for (int i = pages.size(); i < page; i++) {
pages.add(i, new StringTag("", "")); pages.add(i, new StringTag("", ""));
} }
pages.add(page, new StringTag("", packet.getText())); pages.add(page, new StringTag("", packet.getText()));
break;
} }
// Called whenever a page is modified break;
case REPLACE_PAGE: {
if (page < pages.size()) {
pages.set(page, new StringTag("", packet.getText()));
} else {
// Add empty pages in between
for (int i = pages.size(); i < page; i++) {
pages.add(i, new StringTag("", ""));
}
pages.add(page, new StringTag("", packet.getText()));
}
break;
}
case DELETE_PAGE: {
if (page < pages.size()) {
pages.remove(page);
}
break;
}
case SWAP_PAGES: {
int page2 = packet.getSecondaryPageNumber();
if (page < pages.size() && page2 < pages.size()) {
Collections.swap(pages, page, page2);
}
break;
}
case SIGN_BOOK: {
tag.put(new StringTag("author", packet.getAuthor()));
tag.put(new StringTag("title", packet.getTitle()));
break;
}
default:
return;
} }
case DELETE_PAGE: {
if (page < pages.size()) {
pages.remove(page);
}
break;
}
case SWAP_PAGES: {
int page2 = packet.getSecondaryPageNumber();
if (page < pages.size() && page2 < pages.size()) {
Collections.swap(pages, page, page2);
}
break;
}
case SIGN_BOOK: {
tag.put(new StringTag("author", packet.getAuthor()));
tag.put(new StringTag("title", packet.getTitle()));
break;
}
default:
return;
} }
// Remove empty pages at the end // Remove empty pages at the end
while (pages.size() > 0) { while (pages.size() > 0) {

View file

@ -32,6 +32,9 @@ import org.geysermc.connector.network.translators.PacketTranslator;
import org.geysermc.connector.network.translators.Translator; import org.geysermc.connector.network.translators.Translator;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator; import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
/**
* The packet sent for server-authoritative-style inventory transactions.
*/
@Translator(packet = ItemStackRequestPacket.class) @Translator(packet = ItemStackRequestPacket.class)
public class BedrockItemStackRequestTranslator extends PacketTranslator<ItemStackRequestPacket> { public class BedrockItemStackRequestTranslator extends PacketTranslator<ItemStackRequestPacket> {

View file

@ -196,6 +196,12 @@ public class JavaDeclareRecipesTranslator extends PacketTranslator<ServerDeclare
} }
//TODO: rewrite //TODO: rewrite
/**
* The Java server sends an array of items for each ingredient you can use per slot in the crafting grid.
* Bedrock recipes take only one ingredient per crafting grid slot.
*
* @return the Java ingredient list as an array that Bedrock can understand
*/
private ItemData[][] combinations(GeyserSession session, Ingredient[] ingredients) { private ItemData[][] combinations(GeyserSession session, Ingredient[] ingredients) {
Map<Set<ItemData>, IntSet> squashedOptions = new HashMap<>(); Map<Set<ItemData>, IntSet> squashedOptions = new HashMap<>();
for (int i = 0; i < ingredients.length; i++) { for (int i = 0; i < ingredients.length; i++) {