-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.js
More file actions
391 lines (346 loc) · 11.6 KB
/
Copy pathplugins.js
File metadata and controls
391 lines (346 loc) · 11.6 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
const TUTORIALS = {
cpp: [
{
title: "Add the SDK",
text: "The SDK is a CMake project. <code>falcon_add_plugin</code> builds the shared library with the " +
"right settings for your platform.",
file: "CMakeLists.txt",
syntax: "cmake",
code: `include(FetchContent)
FetchContent_Declare(
falcon_plugin_api
GIT_REPOSITORY https://github.com/Falcon-MC/PluginAPI.git
GIT_TAG main
)
FetchContent_MakeAvailable(falcon_plugin_api)
falcon_add_plugin(MyPlugin MyPlugin.cpp)`
},
{
title: "Write the plugin",
text: "Subclass <code>falcon::Plugin</code>, subscribe to events and register commands in " +
"<code>onEnable</code>. Split it into as many classes and folders as you like.",
file: "MyPlugin.cpp",
syntax: "cpp",
code: `#include <falcon/Falcon.hpp>
class MyPlugin : public falcon::Plugin {
public:
bool onEnable() override {
events().on<falcon::PlayerJoinEvent>(
[](falcon::PlayerJoinEvent &event) {
event.player().sendMessage("Welcome!");
});
return true;
}
};
FALCON_PLUGIN(MyPlugin)`
},
{
title: "Install it",
text: "Put the library next to a <code>plugin.json</code> in <code>plugins/MyPlugin/</code> and start " +
"the server. Dependencies are declared here and load in the right order.",
file: "plugin.json",
syntax: "json",
code: `{
"name": "MyPlugin",
"version": "1.0.0",
"api-version": "1.2",
"main": "MyPlugin",
"depend": [],
"softdepend": []
}`
}
],
csharp: [
{
title: "Reference the SDK",
text: "Create a .NET 8 class library and add the <code>Falcon.PluginAPI</code> package.",
file: "MyPlugin.csproj",
syntax: "xml",
code: `<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Falcon.PluginAPI" Version="1.2.0" />
</ItemGroup>
</Project>`
},
{
title: "Write the plugin",
text: "Subclass <code>Plugin</code> and subscribe to events in <code>OnEnable</code>. The events, " +
"commands and content are the same as in C++.",
file: "MyPlugin.cs",
syntax: "csharp",
code: `using Falcon;
namespace MyPlugin;
public class MyPlugin : Plugin
{
public override bool OnEnable()
{
Events.On<PlayerJoinEvent>(e =>
e.Player.SendMessage("Welcome!"));
return true;
}
}`
},
{
title: "Install it",
text: "Copy the build output next to a <code>plugin.json</code> in <code>plugins/MyPlugin/</code>. " +
"The server needs the .NET runtime.",
file: "plugin.json",
syntax: "json",
code: `{
"name": "MyPlugin",
"version": "1.0.0",
"runtime": "dotnet",
"main": "MyPlugin.MyPlugin",
"assembly": "MyPlugin.dll"
}`
}
],
java: [
{
title: "Add the dependency",
text: "Create a Maven project on Java 22 and add the plugin API with the <code>provided</code> scope.",
file: "pom.xml",
syntax: "xml",
code: `<dependency>
<groupId>io.github.falcon-mc</groupId>
<artifactId>falcon-plugin-api</artifactId>
<version>1.2.0</version>
<scope>provided</scope>
</dependency>`
},
{
title: "Write the plugin",
text: "Extend <code>Plugin</code> and subscribe to events in <code>onEnable</code>. The events, " +
"commands and content are the same as in C++.",
file: "MyPlugin.java",
syntax: "java",
code: `package com.example;
import falcon.api.Plugin;
import falcon.api.event.PlayerJoinEvent;
public class MyPlugin extends Plugin {
@Override
public boolean onEnable() {
events().on(PlayerJoinEvent.class, event ->
event.player().sendMessage("Welcome!"));
return true;
}
}`
},
{
title: "Install it",
text: "Put the jar next to a <code>plugin.json</code> in <code>plugins/MyPlugin/</code>. The server " +
"needs Java 22 or newer.",
file: "plugin.json",
syntax: "json",
code: `{
"name": "MyPlugin",
"version": "1.0.0",
"api-version": "1.2",
"runtime": "java",
"main": "com.example.MyPlugin",
"jar": "MyPlugin.jar"
}`
}
]
};
const KEYWORDS = {
cmake: ["include", "FetchContent_Declare", "FetchContent_MakeAvailable", "falcon_add_plugin"],
cpp: ["#include", "class", "public", "bool", "override", "return", "true", "false", "const", "auto"],
csharp: ["using", "namespace", "public", "class", "override", "bool", "return", "true", "false"],
java: ["package", "import", "public", "class", "extends", "boolean", "return", "true", "false", "@Override"],
json: ["true", "false", "null"],
xml: []
};
const STORAGE_KEY = "falcon-plugin-language";
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let generation = 0;
let started = false;
function tokenize(code, syntax) {
const keywords = new Set(KEYWORDS[syntax] || []);
const pattern = syntax === "xml"
? /(<\/?[\w.-]+|\/?>)|("(?:[^"\\]|\\.)*")|([^<>"]+|.)/g
: /("(?:[^"\\]|\\.)*")|([@#]?[A-Za-z_][\w]*)|(<[\w/.]+\.hpp>)|(\s+|.)/g;
const tokens = [];
let match;
while ((match = pattern.exec(code)) !== null) {
const text = match[0];
let type = null;
if (syntax === "xml") {
if (match[1]) {
type = "keyword";
} else if (match[2]) {
type = "string";
}
} else if (match[1] || match[3]) {
type = "string";
} else if (match[2] && keywords.has(text)) {
type = "keyword";
}
tokens.push({text, type});
}
return tokens;
}
function renderTokens(tokens) {
const fragment = document.createDocumentFragment();
for (const token of tokens) {
if (token.type) {
const span = document.createElement("span");
span.className = token.type;
span.textContent = token.text;
fragment.appendChild(span);
} else {
fragment.appendChild(document.createTextNode(token.text));
}
}
return fragment;
}
function wait(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
async function typeInto(target, tokens, current) {
const caret = document.createElement("span");
caret.className = "caret";
target.appendChild(caret);
const total = tokens.reduce((sum, token) => sum + token.text.length, 0);
const perFrame = Math.max(1, Math.round(total / 110));
for (const token of tokens) {
const node = token.type ? document.createElement("span") : document.createTextNode("");
if (token.type) {
node.className = token.type;
}
target.insertBefore(node, caret);
for (let index = 0; index < token.text.length; index += perFrame) {
if (current !== generation) {
return false;
}
node.textContent = token.text.slice(0, index + perFrame);
await new Promise((resolve) => requestAnimationFrame(resolve));
}
}
caret.remove();
return true;
}
function buildStep(step, index) {
const element = document.createElement("div");
element.className = "tutorial-step";
const text = document.createElement("div");
text.className = "tutorial-text";
text.innerHTML = `<span class="step-number">${index + 1}</span><h3>${step.title}</h3><p>${step.text}</p>`;
const frame = document.createElement("div");
frame.className = "code-window";
frame.innerHTML = `<div class="code-window-bar"><span></span><span></span><span></span>` +
`<em>${step.file}</em></div>`;
const pre = document.createElement("pre");
pre.className = "code";
const ghost = document.createElement("span");
ghost.className = "code-ghost";
ghost.setAttribute("aria-hidden", "true");
ghost.textContent = step.code;
const typed = document.createElement("span");
typed.className = "code-typed";
pre.append(ghost, typed);
frame.appendChild(pre);
element.append(text, frame);
return {element, typed};
}
async function showLanguage(language) {
const current = ++generation;
const container = document.getElementById("tutorial");
const steps = TUTORIALS[language];
document.getElementById("language-note").hidden = language === "cpp";
container.replaceChildren();
const built = steps.map((step, index) => buildStep(step, index));
built.forEach((entry) => container.appendChild(entry.element));
for (let index = 0; index < built.length; index++) {
const {element, typed} = built[index];
const tokens = tokenize(steps[index].code, steps[index].syntax);
if (reducedMotion) {
element.classList.add("shown");
typed.appendChild(renderTokens(tokens));
continue;
}
await wait(index === 0 ? 80 : 250);
if (current !== generation) {
return;
}
element.classList.add("shown");
await wait(300);
if (current !== generation || !(await typeInto(typed, tokens, current))) {
return;
}
}
}
function moveIndicator(button) {
const indicator = document.getElementById("language-indicator");
indicator.style.width = `${button.offsetWidth}px`;
indicator.style.transform = `translateX(${button.offsetLeft - 4}px)`;
}
function selectLanguage(language, animate) {
const tabs = document.querySelectorAll("#language-tabs button");
for (const tab of tabs) {
const active = tab.dataset.language === language;
tab.setAttribute("aria-selected", String(active));
if (active) {
moveIndicator(tab);
}
}
try {
localStorage.setItem(STORAGE_KEY, language);
} catch (error) {
}
if (animate) {
showLanguage(language);
}
}
function setupTutorial() {
const tabs = document.getElementById("language-tabs");
const section = document.getElementById("first-plugin");
if (!tabs || !section) {
return;
}
let language = "cpp";
try {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved && TUTORIALS[saved]) {
language = saved;
}
} catch (error) {
}
tabs.hidden = false;
selectLanguage(language, false);
window.addEventListener("resize", () => {
const active = tabs.querySelector('[aria-selected="true"]');
if (active) {
moveIndicator(active);
}
});
for (const tab of tabs.querySelectorAll("button")) {
tab.addEventListener("click", () => {
language = tab.dataset.language;
selectLanguage(language, started);
});
}
const start = () => {
if (started) {
return;
}
started = true;
showLanguage(language);
};
if (!("IntersectionObserver" in window)) {
start();
return;
}
const observer = new IntersectionObserver((entries) => {
if (entries.some((entry) => entry.isIntersecting)) {
observer.disconnect();
start();
}
}, {threshold: 0.2});
observer.observe(section);
}
setupTutorial();