@@ -2,7 +2,10 @@ import * as Server from "@minecraft/server";
22import * as Editor from "@minecraft/server-editor" ;
33import * as EditorUtilities from "editor-utilities/index" ;
44import { Color } from "../color/index" ;
5- export default function ( uiSession ) {
5+ /**
6+ * @param {import("@minecraft/server-editor").IPlayerUISession } uiSession
7+ */
8+ export default ( uiSession ) => {
69 const tool = uiSession . toolRail . addTool (
710 {
811 displayString : "Cube (CTRL + B)" ,
@@ -11,26 +14,26 @@ export default function(uiSession) {
1114 } ,
1215 ) ;
1316
14- const currentCursorState = uiSession . extensionContext . cursor . getState ( ) ;
15- currentCursorState . color = new Color ( 1 , 1 , 0 , 1 ) ;
16- currentCursorState . controlMode = Editor . CursorControlMode . KeyboardAndMouse ;
17- currentCursorState . targetMode = Editor . CursorTargetMode . Block ;
18- currentCursorState . visible = true ;
19-
20- const previewSelection = uiSession . extensionContext . selectionManager . createSelection ( ) ;
17+ const previewSelection = uiSession . extensionContext . selectionManager . create ( ) ;
2118 previewSelection . visible = true ;
22- previewSelection . borderColor = new Color ( 0 , 0.5 , 0.5 , 0.2 ) ;
23- previewSelection . fillColor = new Color ( 0 , 0 , 0.5 , 0.1 ) ;
19+ previewSelection . setOutlineColor ( new Color ( 0 , 0.5 , 0.5 , 0.2 ) ) ;
20+ previewSelection . setFillColor ( new Color ( 0 , 0 , 0.5 , 0.1 ) ) ;
2421
2522 uiSession . scratchStorage = {
26- currentCursorState,
23+ currentCursorState : {
24+ outlineColor : new Color ( 1 , 1 , 0 , 1 ) ,
25+ controlMode : Editor . CursorControlMode . KeyboardAndMouse ,
26+ targetMode : Editor . CursorTargetMode . Block ,
27+ visible : true ,
28+ fixedModeDistance : 5
29+ } ,
2730 previewSelection,
2831 } ;
2932
3033 tool . onModalToolActivation . subscribe (
3134 eventData => {
3235 if ( eventData . isActiveTool )
33- uiSession . extensionContext . cursor . setState ( uiSession . scratchStorage . currentCursorState ) ;
36+ uiSession . extensionContext . cursor . setProperties ( uiSession . scratchStorage . currentCursorState ) ;
3437 } ,
3538 ) ;
3639
@@ -80,19 +83,23 @@ export default function(uiSession) {
8083 titleAltText : "Hollow" ,
8184 }
8285 ) ;
83- pane . addBool ( settings , "face" , {
84- titleAltText : "Face Mode" ,
85- onChange : ( _obj , _property , _oldValue , _newValue ) => {
86- if ( uiSession . scratchStorage === undefined ) {
87- console . error ( 'Cylinder storage was not initialized.' ) ;
88- return ;
89- }
90- uiSession . scratchStorage . currentCursorState . targetMode = settings . face
91- ? Editor . CursorTargetMode . Face
92- : Editor . CursorTargetMode . Block ;
93- uiSession . extensionContext . cursor . setState ( uiSession . scratchStorage . currentCursorState ) ;
94- } ,
95- } ) ;
86+ pane . addBool (
87+ settings ,
88+ "face" ,
89+ {
90+ titleAltText : "Face Mode" ,
91+ onChange : ( _obj , _property , _oldValue , _newValue ) => {
92+ if ( uiSession . scratchStorage === undefined ) {
93+ console . error ( 'Cube storage was not initialized.' ) ;
94+ return ;
95+ }
96+ uiSession . scratchStorage . currentCursorState . targetMode = settings . face
97+ ? Editor . CursorTargetMode . Face
98+ : Editor . CursorTargetMode . Block ;
99+ uiSession . extensionContext . cursor . setProperties ( uiSession . scratchStorage . currentCursorState ) ;
100+ } ,
101+ }
102+ ) ;
96103 pane . addBlockPicker (
97104 settings ,
98105 "blockType" ,
@@ -111,7 +118,7 @@ export default function(uiSession) {
111118
112119 const previewSelection = uiSession . scratchStorage . previewSelection ;
113120 const player = uiSession . extensionContext . player ;
114- const targetBlock = player . dimension . getBlock ( uiSession . extensionContext . cursor . position ) ;
121+ const targetBlock = player . dimension . getBlock ( uiSession . extensionContext . cursor . getPosition ( ) ) ;
115122 if ( ! targetBlock ) return ;
116123
117124 const rotationY = uiSession . extensionContext . player . getRotation ( ) . y ;
@@ -131,18 +138,28 @@ export default function(uiSession) {
131138 z : location . z + fromOffset . z ,
132139 } ;
133140 const to = { x : from . x + toOffset . x , y : from . y + toOffset . y , z : from . z + toOffset . z } ;
134- const blockVolume = new Editor . BlockVolume ( from , to ) ;
141+ const blockVolume = { from, to } ;
135142
136- if ( uiSession . scratchStorage . lastVolumePlaced ?. equals ( blockVolume . boundingBox ) ) return ;
143+ if ( uiSession . scratchStorage . lastVolumePlaced && Server . BoundingBoxUtils . equals ( uiSession . scratchStorage . lastVolumePlaced , Server . BlockVolumeUtils . getBoundingBox ( blockVolume ) ) ) return ;
137144
138- previewSelection . pushVolume ( Editor . SelectionBlockVolumeAction . add , blockVolume ) ;
139- uiSession . scratchStorage . lastVolumePlaced = blockVolume . boundingBox ;
145+ previewSelection . pushVolume (
146+ {
147+ action : Server . CompoundBlockVolumeAction . Add ,
148+ volume : blockVolume
149+ }
150+ ) ;
151+ uiSession . scratchStorage . lastVolumePlaced = Server . BlockVolumeUtils . getBoundingBox ( blockVolume ) ;
140152 if ( settings . hollow &&
141153 blockVolume . boundingBox . spanX > 2 &&
142154 blockVolume . boundingBox . spanY > 2 &&
143155 blockVolume . boundingBox . spanZ > 2 ) {
144- const subtractBlockVolume = new Editor . BlockVolume ( { x : from . x , y : from . y + 1 , z : from . z } , { x : to . x , y : to . y - 1 , z : to . z } ) ;
145- previewSelection . pushVolume ( Editor . SelectionBlockVolumeAction . subtract , subtractBlockVolume ) ;
156+ const subtractBlockVolume = { from : { x : from . x , y : from . y + 1 , z : from . z } , to : { x : to . x , y : to . y - 1 , z : to . z } } ;
157+ previewSelection . pushVolume (
158+ {
159+ action : Server . CompoundBlockVolumeAction . Subtract ,
160+ volume : subtractBlockVolume
161+ }
162+ ) ;
146163 } ;
147164 } ;
148165
@@ -161,9 +178,15 @@ export default function(uiSession) {
161178
162179 uiSession . extensionContext . transactionManager . trackBlockChangeSelection ( uiSession . scratchStorage . previewSelection ) ;
163180 await Editor . executeLargeOperation ( uiSession . scratchStorage . previewSelection , blockLocation => {
164- const block = player . dimension . getBlock ( blockLocation ) ;
165- block . setType ( settings . blockType ) ;
166- } ) . catch ( ( ) => {
181+ if (
182+ blockLocation . y >= - 64
183+ && blockLocation . y <= 320
184+ ) {
185+ const block = player . dimension . getBlock ( blockLocation ) ;
186+ block . setType ( settings . blockType ) ;
187+ } ;
188+ } ) . catch ( ( e ) => {
189+ console . warn ( e ) ;
167190 uiSession . extensionContext . transactionManager . commitOpenTransaction ( ) ;
168191 uiSession . scratchStorage ?. previewSelection . clear ( ) ;
169192 } ) . then ( ( ) => {
0 commit comments