UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

1 lines 5.65 kB
"use strict";(self.webpackChunkmctools_app=self.webpackChunkmctools_app||[]).push([[258],{27258:(e,s,t)=>{t.d(s,{default:()=>k});var n=t(74848),r=t(96540),a=t(67828),c=t(82500),o=t(5256),i=t(40267),l=t(10690),h=t(14466),d=t(74239);class _ extends r.Component{_canvasRef;_engine=null;_scene=null;_camera=null;_blockMeshFactory=null;_currentMesh=null;constructor(e){super(e),this._canvasRef=(0,r.createRef)(),this.state={isLoaded:!1,currentBlockIndex:0,blockTypes:[]},this._handleNextBlock=this._handleNextBlock.bind(this),this._handlePrevBlock=this._handlePrevBlock.bind(this),this._handleBlockSelect=this._handleBlockSelect.bind(this)}async componentDidMount(){await this._initializeScene(),await this._loadBlockTypes()}componentWillUnmount(){this._engine&&this._engine.dispose()}_getBlockFromUrl(){return new URLSearchParams(window.location.search).get("block")||void 0}_isHeadlessMode(){return"true"===new URLSearchParams(window.location.search).get("headless")}async _initializeScene(){const e=this._canvasRef.current;if(!e)return;this._engine=new a.Engine(e,!0,{preserveDrawingBuffer:!0,alpha:!1}),this._scene=new a.Scene(this._engine);const s="true"===new URLSearchParams(window.location.search).get("flatbg");s?(this._scene.clearColor=new a.Color4(.72,.85,.95,1),this._scene.fogMode=a.Scene.FOGMODE_NONE):o.A.configureScene(this._scene),this._camera=new a.ArcRotateCamera("camera",Math.PI/6,Math.PI/3,5,a.Vector3.Zero(),this._scene),this._camera.attachControl(e,!0),this._camera.minZ=.1,this._camera.wheelPrecision=50,o.A.configureLighting(this._scene),s||o.A.createSkyDome(this._scene),this._blockMeshFactory=new c.A(this._scene,{fromX:-1,fromY:-1,fromZ:-1,toX:1,toY:1,toZ:1}),this._engine.runRenderLoop(()=>{this._scene&&this._scene.render()}),window.addEventListener("resize",()=>{this._engine?.resize()})}async _loadBlockTypes(){try{await i.A.loadVanillaCatalog(),await i.A.loadVanillaResourceDefinitions();const e=[];for(const s in i.A.blockTypes){const t=i.A.blockTypes[s],n="air"===s||s.endsWith("_air");!t||n||s.includes("element_")||e.push(t)}e.sort((e,s)=>e.shortId.localeCompare(s.shortId)),this.setState({blockTypes:e,isLoaded:!0},()=>{const s=this._getBlockFromUrl()||this.props.blockName;if(s){const t=e.findIndex(e=>e.shortId===s);t>=0?this.setState({currentBlockIndex:t},()=>{this._renderCurrentBlock()}):e.length>0&&this._renderCurrentBlock()}else e.length>0&&this._renderCurrentBlock()})}catch(e){this.setState({errorMessage:`Failed to load block types: ${e}`})}}_disposeMeshRecursively(e){const s=e.getChildMeshes();for(const e of s)e instanceof a.Mesh&&this._disposeMeshRecursively(e);e.dispose()}_renderCurrentBlock(){if(!this._blockMeshFactory||!this._scene)return;const{blockTypes:e,currentBlockIndex:s}=this.state;if(s<0||s>=e.length)return;this._currentMesh&&(this._disposeMeshRecursively(this._currentMesh),this._currentMesh=null);const t=e[s],n=this._createMockBlock(t);try{const e=this._blockMeshFactory.createMesh(`block_${t.shortId}`,n);e&&(this._currentMesh=e,this._camera&&(this._camera.setTarget(a.Vector3.Zero()),this._camera.radius=3))}catch(e){h.Ay.verbose(`Error rendering block ${t.shortId}: ${e}`),this.setState(()=>({errorMessage:`Error rendering ${t.shortId}: ${e}`}))}}_createMockBlock(e){const s=new l.Ay;return s.setType(e),s}_handleNextBlock(){const{blockTypes:e,currentBlockIndex:s}=this.state;s<e.length-1&&this.setState({currentBlockIndex:s+1},()=>{this._renderCurrentBlock()})}_handlePrevBlock(){const{currentBlockIndex:e}=this.state;e>0&&this.setState({currentBlockIndex:e-1},()=>{this._renderCurrentBlock()})}_handleBlockSelect(e){const s=parseInt(e.target.value,10);this.setState({currentBlockIndex:s},()=>{this._renderCurrentBlock()})}render(){const{heightOffset:e}=this.props,{isLoaded:s,blockTypes:t,currentBlockIndex:r,errorMessage:a}=this.state,c=this._isHeadlessMode(),o=t[r],i=o?.shortId||"Loading...";return c?(0,n.jsx)("div",{className:"bv-container bv-headless",style:{height:"100vh",width:"100vw"},children:(0,n.jsx)("canvas",{ref:this._canvasRef,className:"bv-canvas bv-canvas-headless","data-testid":"block-viewer-canvas"})}):(0,n.jsxs)("div",{className:"bv-container",style:{height:`calc(100vh - ${e}px)`},children:[(0,n.jsxs)("div",{className:"bv-toolbar",children:[(0,n.jsx)("button",{className:"bv-button",onClick:this._handlePrevBlock,disabled:r<=0,children:this.props.intl.formatMessage({id:"viewer.prev"})}),(0,n.jsx)("select",{className:"bv-select",value:r,onChange:this._handleBlockSelect,disabled:!s,"aria-label":"Select block",children:t.map((e,s)=>(0,n.jsx)("option",{value:s,children:e.shortId},e.shortId))}),(0,n.jsx)("button",{className:"bv-button",onClick:this._handleNextBlock,disabled:r>=t.length-1,children:this.props.intl.formatMessage({id:"viewer.next"})}),(0,n.jsx)("span",{className:"bv-info",children:this.props.intl.formatMessage({id:"viewer.block.info"},{current:r+1,total:t.length})})]}),(0,n.jsxs)("div",{className:"bv-block-info",children:[(0,n.jsx)("h2",{children:i}),o&&(0,n.jsxs)("div",{className:"bv-block-details",children:[(0,n.jsx)("span",{children:this.props.intl.formatMessage({id:"viewer.block.base_type"},{name:o.baseType?.name||"unknown"})}),o.mapColor&&(0,n.jsx)("span",{className:"bv-color-swatch",style:{backgroundColor:o.mapColor},role:"img","aria-label":`Map color: ${o.mapColor}`,title:`Map color: ${o.mapColor}`})]})]}),a&&(0,n.jsx)("div",{className:"bv-error",children:a}),(0,n.jsx)("canvas",{ref:this._canvasRef,className:"bv-canvas","data-testid":"block-viewer-canvas"}),(0,n.jsx)("div",{className:"bv-footer",children:(0,n.jsx)("p",{children:this.props.intl.formatMessage({id:"viewer.rotate_hint"})})})]})}}const k=(0,d.g)(_)}}]);