Begin adding support for creative

Still need to complete item mappings
This commit is contained in:
AJ Ferguson 2019-11-09 17:20:47 -09:00
parent 659d52d18a
commit 2d36a8b43b
5 changed files with 115 additions and 3 deletions

View file

@ -29,7 +29,6 @@ import com.github.steveice10.mc.auth.data.GameProfile;
import com.github.steveice10.mc.auth.exception.request.RequestException;
import com.github.steveice10.mc.protocol.MinecraftProtocol;
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
import com.github.steveice10.mc.protocol.data.game.world.block.BlockFace;
import com.github.steveice10.packetlib.Client;
import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
@ -42,8 +41,10 @@ import com.nukkitx.math.vector.Vector2i;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
import com.nukkitx.protocol.bedrock.data.ContainerId;
import com.nukkitx.protocol.bedrock.data.GamePublishSetting;
import com.nukkitx.protocol.bedrock.data.GameRule;
import com.nukkitx.protocol.bedrock.packet.InventoryContentPacket;
import com.nukkitx.protocol.bedrock.packet.PlayStatusPacket;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import com.nukkitx.protocol.bedrock.packet.TextPacket;
@ -311,5 +312,10 @@ public class GeyserSession implements Player {
PlayStatusPacket playStatusPacket = new PlayStatusPacket();
playStatusPacket.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN);
upstream.sendPacket(playStatusPacket);
InventoryContentPacket creativePacket = new InventoryContentPacket();
creativePacket.setContainerId(ContainerId.CREATIVE);
creativePacket.setContents(Toolbox.CREATIVE_ITEMS);
upstream.sendPacket(creativePacket);
}
}

View file

@ -172,6 +172,7 @@ public class TranslatorsInit {
inventoryTranslators.put(WindowType.GENERIC_9X6, new DoubleChestInventoryTranslator(54));
inventoryTranslators.put(WindowType.BREWING_STAND, new BrewingStandInventoryTranslator());
inventoryTranslators.put(WindowType.ANVIL, new AnvilInventoryTranslator());
inventoryTranslators.put(WindowType.CRAFTING, new CraftingTableInventoryTranslator());
//inventoryTranslators.put(WindowType.ENCHANTMENT, new EnchantmentInventoryTranslator()); //TODO
InventoryTranslator furnace = new FurnaceInventoryTranslator();
@ -182,7 +183,6 @@ public class TranslatorsInit {
inventoryTranslators.put(WindowType.GENERIC_3X3, new BlockInventoryTranslator(9, 23 << 4, ContainerType.DISPENSER));
inventoryTranslators.put(WindowType.HOPPER, new BlockInventoryTranslator(5, 154 << 4, ContainerType.HOPPER));
inventoryTranslators.put(WindowType.SHULKER_BOX, new BlockInventoryTranslator(36, 205 << 4, ContainerType.CONTAINER));
inventoryTranslators.put(WindowType.CRAFTING, new BlockInventoryTranslator(10, 58 << 4, ContainerType.WORKBENCH));
//inventoryTranslators.put(WindowType.BEACON, new BlockInventoryTranslator(1, 138 << 4, ContainerType.BEACON)); //TODO
}
}

View file

@ -26,6 +26,7 @@
package org.geysermc.connector.network.translators.bedrock;
import com.github.steveice10.mc.protocol.data.game.window.*;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientCreativeInventoryActionPacket;
import com.github.steveice10.mc.protocol.packet.ingame.client.window.ClientRenameItemPacket;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.nukkitx.math.vector.Vector3f;
@ -69,6 +70,42 @@ public class BedrockInventoryTransactionTranslator extends PacketTranslator<Inve
inventory = session.getInventory();
InventoryTranslator translator = TranslatorsInit.getInventoryTranslators().get(inventory.getWindowType());
if (session.getGameMode() == GameMode.CREATIVE && inventory.getId() == 0) {
ItemStack javaItem;
for (InventoryAction action : packet.getActions()) {
switch (action.getSource().getContainerId()) {
case ContainerId.INVENTORY:
case ContainerId.ARMOR:
case ContainerId.OFFHAND:
int javaSlot = translator.bedrockSlotToJava(action);
if (action.getToItem().getId() == 0) {
javaItem = new ItemStack(-1, 0, null);
} else {
javaItem = TranslatorsInit.getItemTranslator().translateToJava(action.getToItem());
if (javaItem.getId() == 0) { //item missing mapping
translator.updateInventory(session, inventory);
break;
}
}
ClientCreativeInventoryActionPacket creativePacket = new ClientCreativeInventoryActionPacket(javaSlot, InventoryUtils.fixStack(javaItem));
session.getDownstream().getSession().send(creativePacket);
inventory.getItems()[javaSlot] = javaItem;
break;
case ContainerId.NONE:
if (action.getSource().getType() == InventorySource.Type.WORLD_INTERACTION && action.getSource().getFlag() == InventorySource.Flag.DROP_ITEM) {
javaItem = TranslatorsInit.getItemTranslator().translateToJava(action.getToItem());
if (javaItem.getId() == 0) { //item missing mapping
break;
}
ClientCreativeInventoryActionPacket creativeDropPacket = new ClientCreativeInventoryActionPacket(-1, InventoryUtils.fixStack(javaItem));
session.getDownstream().getSession().send(creativeDropPacket);
}
break;
}
}
return;
}
InventoryAction worldAction = null;
InventoryAction cursorAction = null;
for (InventoryAction action : packet.getActions()) {

View file

@ -0,0 +1,33 @@
package org.geysermc.connector.network.translators.inventory;
import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.data.ContainerType;
import com.nukkitx.protocol.bedrock.packet.ContainerOpenPacket;
import org.geysermc.connector.inventory.Inventory;
import org.geysermc.connector.network.session.GeyserSession;
public class CraftingTableInventoryTranslator extends ContainerInventoryTranslator {
public CraftingTableInventoryTranslator() {
super(10);
}
@Override
public void prepareInventory(GeyserSession session, Inventory inventory) {
}
@Override
public void openInventory(GeyserSession session, Inventory inventory) {
ContainerOpenPacket containerOpenPacket = new ContainerOpenPacket();
containerOpenPacket.setWindowId((byte) inventory.getId());
containerOpenPacket.setType((byte) ContainerType.WORKBENCH.id());
containerOpenPacket.setBlockPosition(inventory.getHolderPosition());
containerOpenPacket.setUniqueEntityId(inventory.getHolderId());
session.getUpstream().sendPacket(containerOpenPacket);
}
@Override
public void closeInventory(GeyserSession session, Inventory inventory) {
}
}

View file

@ -1,7 +1,9 @@
package org.geysermc.connector.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nukkitx.nbt.NbtUtils;
import com.nukkitx.network.VarInts;
import com.nukkitx.protocol.bedrock.data.ItemData;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import com.nukkitx.protocol.bedrock.v361.BedrockUtils;
import gnu.trove.map.TIntObjectMap;
@ -13,13 +15,15 @@ import org.geysermc.connector.network.translators.block.BlockEntry;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.world.GlobalBlockPalette;
import java.io.InputStream;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.util.*;
public class Toolbox {
public static final Collection<StartGamePacket.ItemEntry> ITEMS;
public static final ByteBuf CACHED_PALLETE;
public static final ItemData[] CREATIVE_ITEMS;
public static final TIntObjectMap<ItemEntry> ITEM_ENTRIES;
public static final TIntObjectMap<BlockEntry> BLOCK_ENTRIES;
@ -116,5 +120,37 @@ public class Toolbox {
}
BLOCK_ENTRIES = blockEntries;
InputStream creativeItemStream = Toolbox.class.getClassLoader().getResourceAsStream("bedrock/creative_items.json");
ObjectMapper creativeItemMapper = new ObjectMapper();
List<LinkedHashMap<String, Object>> creativeItemEntries = new ArrayList<>();
try {
creativeItemEntries = creativeItemMapper.readValue(creativeItemStream, ArrayList.class);
} catch (Exception e) {
e.printStackTrace();
}
List<ItemData> creativeItems = new ArrayList<>();
for (Map<String, Object> map : creativeItemEntries) {
short damage = 0;
if (map.containsKey("damage")) {
damage = (short)(int) map.get("damage");
}
if (map.containsKey("nbt_b64")) {
byte[] bytes = DatatypeConverter.parseBase64Binary((String) map.get("nbt_b64"));
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try {
com.nukkitx.nbt.tag.CompoundTag tag = (com.nukkitx.nbt.tag.CompoundTag) NbtUtils.createReaderLE(bais).readTag();
creativeItems.add(ItemData.of((int) map.get("id"), damage, 1, tag));
} catch (IOException e) {
e.printStackTrace();
}
} else {
creativeItems.add(ItemData.of((int) map.get("id"), damage, 1));
}
}
CREATIVE_ITEMS = creativeItems.toArray(new ItemData[0]);
}
}