diff --git a/client/ui/menu/hotbar.js b/client/ui/menu/hotbar.js index 092c7cd..78a36b3 100644 --- a/client/ui/menu/hotbar.js +++ b/client/ui/menu/hotbar.js @@ -5,6 +5,7 @@ import React, {useEffect, useState} from 'react'; import {compose} from '@avocado/core'; import contempo from 'contempo'; // 1st party. +import {usePropertyChange} from '../hooks/use-property-change'; import ItemSlot from './item-slot'; const decorate = compose( @@ -39,16 +40,51 @@ const decorate = compose( `), ); -const HotbarComponent = ({damageProton}) => { - const [active, setActive] = useState(0); +const HotbarComponent = ({app}) => { + const selfEntity = usePropertyChange(app, 'selfEntity'); + const activeSlotIndex = usePropertyChange(selfEntity, 'activeSlotIndex'); + const [slotImageUris, setSlotImageUris] = useState({}); + useEffect(() => { + if (!selfEntity) { + return; + } + const onInventoryChanged = () => { + const imageUris = {}; + const itemPromises = []; + for (let i = 0; i < 10; i++) { + const item = selfEntity.itemInSlot(i); + if (!item) { + continue; + } + itemPromises.push(item.then((item) => { + return item.hydrate().then(() => { + const image = item.imageForSlot(0); + if (image) { + imageUris[i] = image.uri; + } + }); + })); + } + Promise.all(itemPromises).then(() => { + setSlotImageUris(imageUris); + }) + }; + selfEntity.on('inventoryChanged', onInventoryChanged); + onInventoryChanged(); + return () => { + selfEntity.off('inventoryChanged', onInventoryChanged); + }; + }, [selfEntity]); + const hotkeyForSlot = (index) => { return (index + 1) % 10; } const slots = []; for (let i = 0; i < 10; ++i) { const slot = { @@ -64,14 +100,6 @@ const HotbarComponent = ({damageProton}) => { } return
{ - if (event.deltaY > 0) { - setActive((active + 1) % 10); - } - else if (event.deltaY < 0) { - setActive((active + 9) % 10); - } - }} >
{slots}