Skip to content

Commit

Permalink
Add some comments and fix book editing in creative
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Jan 20, 2021
1 parent 2304c13 commit ca620d6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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.player.GameMode;
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.ListTag;
Expand Down Expand Up @@ -54,51 +53,48 @@ public void translate(BookEditPacket packet, GeyserSession session) {
List<Tag> pages = tag.contains("pages") ? new LinkedList<>(((ListTag) tag.get("pages")).getValue()) : new LinkedList<>();

int page = packet.getPageNumber();
// Creative edits the NBT for us
if (session.getGameMode() != GameMode.CREATIVE) {
switch (packet.getAction()) {
case ADD_PAGE: {
switch (packet.getAction()) {
case ADD_PAGE: {
// 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;
}
// 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
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
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;
break;
}
case DELETE_PAGE: {
if (page < pages.size()) {
pages.remove(page);
}
case SIGN_BOOK: {
tag.put(new StringTag("author", packet.getAuthor()));
tag.put(new StringTag("title", packet.getTitle()));
break;
break;
}
case SWAP_PAGES: {
int page2 = packet.getSecondaryPageNumber();
if (page < pages.size() && page2 < pages.size()) {
Collections.swap(pages, page, page2);
}
default:
return;
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
while (pages.size() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import org.geysermc.connector.network.translators.Translator;
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;

/**
* The packet sent for server-authoritative-style inventory transactions.
*/
@Translator(packet = ItemStackRequestPacket.class)
public class BedrockItemStackRequestTranslator extends PacketTranslator<ItemStackRequestPacket> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ public void translate(ServerDeclareRecipesPacket packet, GeyserSession session)
}

//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) {
Map<Set<ItemData>, IntSet> squashedOptions = new HashMap<>();
for (int i = 0; i < ingredients.length; i++) {
Expand Down

0 comments on commit ca620d6

Please sign in to comment.