refactor: rendered distributing

This commit is contained in:
cha0s 2024-09-29 06:43:58 -05:00
parent 9e82b3a5c1
commit 0bfd4b3a9f

View File

@ -46,7 +46,7 @@ function Ui({disconnected}) {
const [showDisconnected, setShowDisconnected] = useState(false); const [showDisconnected, setShowDisconnected] = useState(false);
const [bufferSlot, setBufferSlot] = useState(); const [bufferSlot, setBufferSlot] = useState();
const hadBufferSlot = useRef(); const hadBufferSlot = useRef();
const distributing = useRef(new Map()); const [distributing, setDistributing] = useState({});
const [devtoolsIsOpen, setDevtoolsIsOpen] = useState(false); const [devtoolsIsOpen, setDevtoolsIsOpen] = useState(false);
const ratio = (RESOLUTION.x * (devtoolsIsOpen ? 2 : 1)) / RESOLUTION.y; const ratio = (RESOLUTION.x * (devtoolsIsOpen ? 2 : 1)) / RESOLUTION.y;
const [camera, setCamera] = useState({x: 0, y: 0}); const [camera, setCamera] = useState({x: 0, y: 0});
@ -438,7 +438,10 @@ function Ui({disconnected}) {
}, [bufferSlot, client, keepHotbarOpen, losing, mainEntityRef, trading]); }, [bufferSlot, client, keepHotbarOpen, losing, mainEntityRef, trading]);
const hotbarOnSlotMouseMove = useCallback((i) => { const hotbarOnSlotMouseMove = useCallback((i) => {
if (hadBufferSlot.current) { if (hadBufferSlot.current) {
distributing.current.set([mainEntityRef.current, i + 1].join(':'), true); setDistributing((distributing) => ({
[[mainEntityRef.current, i + 1].join(':')]: true,
...distributing,
}))
} }
}, [mainEntityRef]); }, [mainEntityRef]);
const hotbarOnSlotMouseUp = useCallback((i, event) => { const hotbarOnSlotMouseUp = useCallback((i, event) => {
@ -449,18 +452,18 @@ function Ui({disconnected}) {
else { else {
switch (event.button) { switch (event.button) {
case 0: case 0:
if (distributing.current.size > 0) { if (Object.keys(distributing).length > 0) {
client.send({ client.send({
type: 'Action', type: 'Action',
payload: { payload: {
type: 'distribute', type: 'distribute',
value: [ value: [
0, 0,
Array.from(distributing.current.keys()).map((idAndSlot) => idAndSlot.split(':')), Object.keys(distributing).map((idAndSlot) => idAndSlot.split(':')),
], ],
}, },
}); });
distributing.current.clear(); setDistributing({});
} }
else if (hadBufferSlot.current) { else if (hadBufferSlot.current) {
client.send({ client.send({
@ -483,7 +486,7 @@ function Ui({disconnected}) {
} }
} }
hadBufferSlot.current = undefined; hadBufferSlot.current = undefined;
}, [client, keepHotbarOpen, mainEntityRef, trading]); }, [client, distributing, keepHotbarOpen, mainEntityRef, trading]);
const bagOnSlotMouseDown = useCallback((i) => { const bagOnSlotMouseDown = useCallback((i) => {
if (trading) { if (trading) {
const index = losing.indexOf(i + 11); const index = losing.indexOf(i + 11);