forked from JaylyDev/ScriptAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (115 loc) · 3.72 KB
/
index.js
File metadata and controls
129 lines (115 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Script example for ScriptAPI
// Author: Ciosciaa <https://github.com/Ciosciaa>
// Project: https://github.com/JaylyDev/ScriptAPI
import * as server from "@minecraft/server"
import * as editor from "@minecraft/server-editor"
const entityTypes = [...server.EntityTypes.getAll()].map(({id}) => id)
/**
* @typedef ScratchStorage
* @property {editor.CursorProperties} spawnerCursorState
*/
editor.registerEditorExtension(
"entitySpawner",
/**
*
* @param {import("@minecraft/server-editor").IPlayerUISession<ScratchStorage>} uiSession
* @returns
*/
uiSession => {
const tool = uiSession.toolRail.addTool(
{
displayAltText: "Spawn Entity (Ctrl + E)",
displayStringId: "editor.toolRail.entitySpawnerTool.title",
tooltipAltText: "Spawns an entity at the selected position",
tooltipStringId: "editor.toolRail.entitySpawnerTool.description",
icon: "pack://textures/editor/entity.png?filtering=point"
}
)
const currentCursorState = uiSession.extensionContext.cursor.getProperties()
currentCursorState.outlineColor = {red: 1, green: 0, blue: 1, alpha: 1}
currentCursorState.controlMode = editor.CursorControlMode.KeyboardAndMouse
currentCursorState.targetMode = editor.CursorTargetMode.Face
currentCursorState.visible = true
uiSession.scratchStorage = {spawnerCursorState: currentCursorState}
tool.onModalToolActivation.subscribe(
eventData => {
if (eventData.isActiveTool) uiSession.extensionContext.cursor.setProperties(uiSession.scratchStorage.spawnerCursorState)
}
)
uiSession.inputManager.registerKeyBinding(
editor.EditorInputContext.GlobalToolMode,
uiSession.actionManager.createAction(
{
actionType: editor.ActionTypes.NoArgsAction,
onExecute: () => {
uiSession.toolRail.setSelectedOptionId(tool.id, true)
},
}
),
editor.KeyboardKey.KEY_E,
editor.InputModifier.Control
)
const settings = {
entityType: "minecraft:creeper"
}
const pane = uiSession.createPropertyPane(
{
titleStringId: "editor.toolRail.entitySpawnerTool.pane.title",
titleAltText: "Entity Spawner Settings",
}
)
const binding = editor.bindDataSource(
pane,
settings
)
pane.addDropdown(
binding,
"entityType",
{
titleStringId: "editor.toolRail.entitySpawnerTool.pane.controls.entityType",
titleAltText: "Entity Type",
dropdownItems: entityTypes.map(
entityType => (
{
value: entityType,
displayAltText: entityType,
displayStringId: `entity.${entityType.replace("minecraft:", "")}.name`
}
)
)
}
)
tool.bindPropertyPane(pane)
tool.registerMouseButtonBinding(
uiSession.actionManager.createAction(
{
actionType: editor.ActionTypes.MouseRayCastAction,
onExecute:
async (mouseRay, mouseProps) => {
if (mouseProps.mouseAction === editor.MouseActionType.LeftButton) {
if (mouseProps.inputType === editor.MouseInputType.ButtonDown) {
uiSession.extensionContext.transactionManager.openTransaction("tool.spawnEntity")
uiSession.extensionContext.selectionManager.selection.clear()
const cursorPosition = mouseRay.cursorBlockLocation
server.world.getDimension("minecraft:overworld").spawnEntity(
settings.entityType,
{
x: cursorPosition.x + 0.5,
y: cursorPosition.y,
z: cursorPosition.z + 0.5
}
)
}
else if (mouseProps.inputType === editor.MouseInputType.ButtonUp) {
uiSession.extensionContext.transactionManager.commitOpenTransaction()
uiSession.extensionContext.selectionManager.selection.clear()
}
}
}
}
)
)
return [] // Likely unneeded, but untested
},
() => {}
)