Search in sources :

Example 1 with PacketPlayInSteerVehicleHandle

use of com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayInSteerVehicleHandle in project BKCommonLib by bergerhealer.

the class CommonMapController method onPacketReceive.

@Override
public synchronized void onPacketReceive(PacketReceiveEvent event) {
    // Handle input coming from the player for the map
    if (event.getType() == PacketType.IN_STEER_VEHICLE) {
        Player p = event.getPlayer();
        MapPlayerInput input = playerInputs.get(p);
        if (input != null) {
            PacketPlayInSteerVehicleHandle packet = PacketPlayInSteerVehicleHandle.createHandle(event.getPacket().getHandle());
            int dx = (int) -Math.signum(packet.getSideways());
            int dy = (int) -Math.signum(packet.getForwards());
            int dz = 0;
            if (packet.isUnmount()) {
                dz -= 1;
            }
            if (packet.isJump()) {
                dz += 1;
            }
            // Receive input. If it will be handled, it will cancel further handling of this packet
            event.setCancelled(input.receiveInput(dx, dy, dz));
        }
    }
    // We have to prevent that in here
    if (event.getType() == PacketType.IN_SET_CREATIVE_SLOT) {
        ItemStack item = event.getPacket().read(PacketType.IN_SET_CREATIVE_SLOT.item);
        UUID mapUUID = CommonMapUUIDStore.getMapUUID(item);
        if (mapUUID != null && CommonMapUUIDStore.getStaticMapId(mapUUID) == -1) {
            // Dynamic Id map. Since we do not refresh NBT data over the network, this packet contains incorrect data
            // Find the original item the player took (by UUID). If it exists, merge its NBT data with this item.
            // For this we also have the map item cache, which is filled with data the moment a player picks up an item
            // This data is kept around for 10 minutes (unlikely a player will hold onto it for that long...)
            ItemStack originalMapItem = null;
            CreativeDraggedMapItem cachedItem = this.creativeDraggedMapItems.get(mapUUID);
            if (cachedItem != null) {
                cachedItem.life = CreativeDraggedMapItem.CACHED_ITEM_MAX_LIFE;
                originalMapItem = cachedItem.item;
            } else {
                for (ItemStack oldItem : event.getPlayer().getInventory()) {
                    if (mapUUID.equals(CommonMapUUIDStore.getMapUUID(oldItem))) {
                        originalMapItem = oldItem.clone();
                        break;
                    }
                }
            }
            if (originalMapItem != null) {
                // Original item was found. Restore all properties of that item.
                // Keep metadata the player can control, replace everything else
                ItemUtil.setMetaTag(item, ItemUtil.getMetaTag(originalMapItem));
                event.getPacket().write(PacketType.IN_SET_CREATIVE_SLOT.item, item);
            } else {
                // Dynamic Id. Force a map id value of 0 to prevent creation of new World Map instances
                item = ItemUtil.cloneItem(item);
                CommonMapUUIDStore.setItemMapId(item, 0);
                event.getPacket().write(PacketType.IN_SET_CREATIVE_SLOT.item, item);
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) MapPlayerInput(com.bergerkiller.bukkit.common.map.MapPlayerInput) PacketPlayInSteerVehicleHandle(com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayInSteerVehicleHandle) ItemStack(org.bukkit.inventory.ItemStack) MapUUID(com.bergerkiller.bukkit.common.map.util.MapUUID) UUID(java.util.UUID)

Aggregations

MapPlayerInput (com.bergerkiller.bukkit.common.map.MapPlayerInput)1 MapUUID (com.bergerkiller.bukkit.common.map.util.MapUUID)1 PacketPlayInSteerVehicleHandle (com.bergerkiller.generated.net.minecraft.network.protocol.game.PacketPlayInSteerVehicleHandle)1 UUID (java.util.UUID)1 Player (org.bukkit.entity.Player)1 ItemStack (org.bukkit.inventory.ItemStack)1