forked from GeyserMC/Geyser
Implement support for changing held item
This commit is contained in:
parent
9399296908
commit
eaf57550e5
8 changed files with 126 additions and 13 deletions
|
@ -33,32 +33,40 @@ import lombok.Setter;
|
|||
public class Inventory {
|
||||
|
||||
@Getter
|
||||
private int id;
|
||||
protected int id;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private boolean open;
|
||||
protected boolean open;
|
||||
|
||||
@Getter
|
||||
private WindowType windowType;
|
||||
protected WindowType windowType;
|
||||
|
||||
@Getter
|
||||
private int size;
|
||||
protected int size;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String title;
|
||||
protected String title;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private ItemStack[] items;
|
||||
protected ItemStack[] items;
|
||||
|
||||
public Inventory(int id, WindowType windowType, int size) {
|
||||
this("Inventory", id, windowType, size);
|
||||
}
|
||||
|
||||
public Inventory(String title, int id, WindowType windowType, int size) {
|
||||
this.title = title;
|
||||
this.id = id;
|
||||
this.windowType = windowType;
|
||||
this.size = size;
|
||||
|
||||
this.title = "Inventory";
|
||||
this.items = new ItemStack[size];
|
||||
}
|
||||
|
||||
public ItemStack getItem(int slot) {
|
||||
return items[slot];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright (c) 2019 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.connector.inventory;
|
||||
|
||||
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class PlayerInventory extends Inventory {
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private int heldItemSlot;
|
||||
|
||||
public PlayerInventory() {
|
||||
super(0, null, 45);
|
||||
|
||||
heldItemSlot = 0;
|
||||
}
|
||||
|
||||
public ItemStack getItemInHand() {
|
||||
return items[heldItemSlot];
|
||||
}
|
||||
}
|
|
@ -139,4 +139,8 @@ public class UpstreamPacketHandler extends LoggingPacketHandler {
|
|||
return translateAndDefault(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(MobEquipmentPacket packet) {
|
||||
return translateAndDefault(packet);
|
||||
}
|
||||
}
|
|
@ -54,6 +54,7 @@ import org.geysermc.api.window.FormWindow;
|
|||
import org.geysermc.connector.GeyserConnector;
|
||||
import org.geysermc.connector.entity.PlayerEntity;
|
||||
import org.geysermc.connector.entity.type.EntityType;
|
||||
import org.geysermc.connector.inventory.PlayerInventory;
|
||||
import org.geysermc.connector.network.session.cache.DataCache;
|
||||
import org.geysermc.connector.network.session.cache.EntityCache;
|
||||
import org.geysermc.connector.network.session.cache.InventoryCache;
|
||||
|
@ -76,6 +77,8 @@ public class GeyserSession implements PlayerSession, Player {
|
|||
private AuthData authenticationData;
|
||||
|
||||
private PlayerEntity playerEntity;
|
||||
private PlayerInventory inventory;
|
||||
|
||||
private EntityCache entityCache;
|
||||
private InventoryCache inventoryCache;
|
||||
private WindowCache windowCache;
|
||||
|
@ -94,17 +97,20 @@ public class GeyserSession implements PlayerSession, Player {
|
|||
this.connector = connector;
|
||||
this.upstream = bedrockServerSession;
|
||||
|
||||
this.playerEntity = new PlayerEntity(UUID.randomUUID(), 1, 1, EntityType.PLAYER, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0, 0, 0));
|
||||
|
||||
this.entityCache = new EntityCache(this);
|
||||
this.inventoryCache = new InventoryCache(this);
|
||||
this.windowCache = new WindowCache(this);
|
||||
this.scoreboardCache = new ScoreboardCache(this);
|
||||
|
||||
this.playerEntity = new PlayerEntity(UUID.randomUUID(), 1, 1, EntityType.PLAYER, new Vector3f(0, 0, 0), new Vector3f(0, 0, 0), new Vector3f(0, 0, 0));
|
||||
this.inventory = new PlayerInventory();
|
||||
|
||||
this.javaPacketCache = new DataCache<Packet>();
|
||||
|
||||
this.spawned = false;
|
||||
this.loggedIn = false;
|
||||
|
||||
this.inventoryCache.getInventories().put(0, inventory);
|
||||
}
|
||||
|
||||
public void connect(RemoteServer remoteServer) {
|
||||
|
|
|
@ -52,9 +52,6 @@ public class InventoryCache {
|
|||
|
||||
public InventoryCache(GeyserSession session) {
|
||||
this.session = session;
|
||||
|
||||
// This is the player's inventory
|
||||
inventories.put(0, new Inventory(0, null, 45));
|
||||
}
|
||||
|
||||
public Inventory getPlayerInventory() {
|
||||
|
|
|
@ -59,10 +59,12 @@ import com.nukkitx.nbt.stream.NBTOutputStream;
|
|||
import com.nukkitx.nbt.tag.CompoundTag;
|
||||
import com.nukkitx.protocol.bedrock.packet.AnimatePacket;
|
||||
import com.nukkitx.protocol.bedrock.packet.CommandRequestPacket;
|
||||
import com.nukkitx.protocol.bedrock.packet.MobEquipmentPacket;
|
||||
import com.nukkitx.protocol.bedrock.packet.TextPacket;
|
||||
import lombok.Getter;
|
||||
import org.geysermc.connector.network.translators.bedrock.BedrockAnimateTranslator;
|
||||
import org.geysermc.connector.network.translators.bedrock.BedrockCommandRequestTranslator;
|
||||
import org.geysermc.connector.network.translators.bedrock.BedrockMobEquipmentTranslator;
|
||||
import org.geysermc.connector.network.translators.bedrock.BedrockTextTranslator;
|
||||
import org.geysermc.connector.network.translators.inventory.GenericInventoryTranslator;
|
||||
import org.geysermc.connector.network.translators.inventory.InventoryTranslator;
|
||||
|
@ -161,6 +163,7 @@ public class TranslatorsInit {
|
|||
Registry.registerBedrock(AnimatePacket.class, new BedrockAnimateTranslator());
|
||||
Registry.registerBedrock(CommandRequestPacket.class, new BedrockCommandRequestTranslator());
|
||||
Registry.registerBedrock(TextPacket.class, new BedrockTextTranslator());
|
||||
Registry.registerBedrock(MobEquipmentPacket.class, new BedrockMobEquipmentTranslator());
|
||||
|
||||
itemTranslator = new ItemTranslator();
|
||||
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
|
||||
package org.geysermc.connector.network.translators.bedrock;
|
||||
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.client.ClientChatPacket;
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright (c) 2019 GeyserMC. http://geysermc.org
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @author GeyserMC
|
||||
* @link https://github.com/GeyserMC/Geyser
|
||||
*/
|
||||
|
||||
package org.geysermc.connector.network.translators.bedrock;
|
||||
|
||||
import com.github.steveice10.mc.protocol.packet.ingame.client.player.ClientPlayerChangeHeldItemPacket;
|
||||
import com.nukkitx.protocol.bedrock.data.ContainerId;
|
||||
import com.nukkitx.protocol.bedrock.packet.MobEquipmentPacket;
|
||||
import org.geysermc.connector.network.session.GeyserSession;
|
||||
import org.geysermc.connector.network.translators.PacketTranslator;
|
||||
|
||||
public class BedrockMobEquipmentTranslator extends PacketTranslator<MobEquipmentPacket> {
|
||||
|
||||
@Override
|
||||
public void translate(MobEquipmentPacket packet, GeyserSession session) {
|
||||
if (packet.getHotbarSlot() > 8)
|
||||
return;
|
||||
|
||||
if (packet.getContainerId() != ContainerId.INVENTORY)
|
||||
return;
|
||||
|
||||
session.getInventory().setHeldItemSlot(packet.getHotbarSlot());
|
||||
|
||||
ClientPlayerChangeHeldItemPacket changeHeldItemPacket = new ClientPlayerChangeHeldItemPacket(packet.getHotbarSlot());
|
||||
session.getDownstream().getSession().send(changeHeldItemPacket);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue