sapdon 3.0.3 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -81,8 +81,9 @@ hello_sapdon/
81
81
 
82
82
  ## 📚 学习资源
83
83
 
84
- - [官方文档](https://docs.sapdon.org)
85
- - [示例模组仓库](https://github.com/Meteage/sapdon/examples)
84
+ - [官方文档](./doc/)
85
+ - [入门教程](./doc/hello_sapdon)
86
+ - [示例模组仓库](./examples/)
86
87
 
87
88
 
88
89
  ## ❓ 常见问题
package/dist/cli/build.js CHANGED
@@ -22,7 +22,9 @@ const __dirname = path.dirname(__filename);
22
22
  const rollupIgnores = [
23
23
  'rollup',
24
24
  'typescript',
25
- '@sapdon',
25
+ '@sapdon/core',
26
+ '@sapdon/cli',
27
+ '@minecraft',
26
28
  ];
27
29
  //脚本打包器
28
30
  export const scriptBundler = {
@@ -17,13 +17,16 @@ export async function syncDevFilesServer(projectPath, projectName) {
17
17
  }
18
18
  export async function writeLib(projectPath) {
19
19
  const projectModules = path.join(projectPath, "node_modules");
20
- const rootDir = path.join(dirname(import.meta), '../../');
20
+ const rootDir = path.join(dirname(import.meta), '../');
21
21
  const corePath = path.join(rootDir, 'core');
22
22
  const cliPath = path.join(rootDir, 'cli');
23
+ const ocPath = path.join(rootDir, 'oc');
23
24
  const targetCorePath = path.join(projectModules, '@sapdon/core');
24
25
  const targetCliPath = path.join(projectModules, '@sapdon/cli');
26
+ const targetOcPath = path.join(projectModules, '@sapdon/oc');
25
27
  fs.cpSync(corePath, targetCorePath, { recursive: true, force: true });
26
28
  fs.cpSync(cliPath, targetCliPath, { recursive: true, force: true });
29
+ fs.cpSync(ocPath, targetOcPath, { recursive: true, force: true });
27
30
  fs.writeFileSync(path.join(targetCorePath, 'package.json'), JSON.stringify({
28
31
  name: '@sapdon/core',
29
32
  main: 'index.js',
@@ -34,4 +37,9 @@ export async function writeLib(projectPath) {
34
37
  main: 'index.js',
35
38
  version: '1.0.0',
36
39
  }));
40
+ fs.writeFileSync(path.join(targetOcPath, 'package.json'), JSON.stringify({
41
+ name: '@sapdon/oc',
42
+ main: 'index.js',
43
+ version: '1.0.0',
44
+ }));
37
45
  }
@@ -1,11 +1,6 @@
1
1
  import http from 'http';
2
2
 
3
- declare enum ServerHandles {
4
- WRITE_ADDON = "write-addon",
5
- WRITE_GAME_DEV = "write-dev"
6
- }
7
-
8
- type ServerHandler = (...args: Transferable[]) => void;
3
+ type ServerHandler = (...args: (Uint8Array & string)[]) => void | Promise<void>;
9
4
  type HandlerInterceptor = (handler: ServerHandler) => ServerHandler;
10
5
  declare class DevelopmentServer {
11
6
  readonly cliServerHandlers: Map<string, ServerHandler>;
@@ -18,6 +13,8 @@ declare class DevelopmentServer {
18
13
  }
19
14
  declare const server: DevelopmentServer;
20
15
 
21
- declare function startDevServer(GRegistry: any, UIRegistry: any): void;
16
+ declare namespace client {
17
+ function call(name: any, ...args: any[]): Promise<void>;
18
+ }
22
19
 
23
- export { ServerHandles, server as devServer, startDevServer };
20
+ export { client, server as devServer };
package/dist/cli/index.js CHANGED
@@ -1 +1,90 @@
1
- import e from"path";import t,{readFileSync as r}from"fs";import"crypto";import"node:url";import s from"fs/promises";import n from"http";const i=(r,s)=>{t.mkdirSync(e.dirname(r),{recursive:!0}),t.writeFile(r,s,(e=>{if(e)return console.error(e)}))},o=(r,s)=>{if(t.mkdirSync(e.dirname(s),{recursive:!0}),!t.existsSync(r))return void console.log(`Source path ${r} does not exist.`);t.existsSync(s)||t.mkdirSync(s);t.readdirSync(r).forEach((n=>{const i=e.join(r,n),a=e.join(s,n);t.lstatSync(i).isDirectory()?o(i,a):t.copyFileSync(i,a)}))},a=async(t,r,n="")=>{const i={},o=await s.readdir(t);for(const c of o){const o=e.join(t,c);if((await s.stat(o)).isDirectory()){const e=await a(o,r,n);Object.assign(i,e)}else if(c.endsWith(".png")){const t=e.basename(c,".png"),s=`${n}${e.relative(r,o).replace(/\.png$/,"").replace(/\\/g,"/")}`;i[t]={textures:s}}}return i},c=async(e,t,r)=>{try{const n=JSON.stringify(t,null,2);await s.writeFile(e,n),console.log(r)}catch(e){console.error("Error writing JSON file:",e)}};class l{static item_texture_sets=new Map;static getItemTextureSet(){return this.item_texture_sets}static getItemTextures(){return Object.fromEntries(this.item_texture_sets)}static registerTextureData(e,t){this.item_texture_sets.set(e,t)}static registerTexture(e,t){this.registerTextureData(e,{textures:t})}}class u{static terrain_texture_sets=new Map;static getTerrainTextureSet(){return this.terrain_texture_sets}static getTerrainTextures(){return Object.fromEntries(this.terrain_texture_sets)}static registerTextureData(e,t){this.terrain_texture_sets.set(e,t)}static registerTexture(e,t){this.registerTextureData(e,{textures:t})}}const g={flipbook_textures:[],registerFlipbookTexture(e,t,r,s={}){const n={atlas_tile:e,flipbook_texture:t,ticks_per_frame:r};Object.assign(n,s),this.flipbook_textures.push(n)}};var _;!function(e){e.WRITE_ADDON="write-addon",e.WRITE_GAME_DEV="write-dev"}(_||(_={}));const d=(t,r)=>async(s,n,o)=>{try{const _=e.join(n,`${o}_BP`),d=e.join(n,`${o}_RP`),f=t=>e.join(d,t),x=e.join(_,"blocks.json"),p={format_version:"1.20.20"},m=f("textures/items");!async function(e,t,r,s){try{const n=await a(e,e,"textures/items/");s&&Object.assign(n,s);const i={resource_pack_name:r,texture_name:"atlas.items",texture_data:n};await c(t,i,"Item texture JSON file generated successfully.")}catch(e){console.error("Error generating item texture JSON:",e)}}(m,f("textures/item_texture.json"),o,l.getItemTextures());const h=f("textures/blocks");!async function(e,t,r,s){try{const n=await a(e,e,"textures/blocks/");s&&Object.assign(n,s);const i={texture_name:"atlas.terrain",resource_pack_name:r,padding:8,num_mip_levels:4,texture_data:n};await c(t,i,"Block texture JSON file generated successfully.")}catch(e){console.error("Error generating block texture JSON:",e)}}(h,f("textures/terrain_texture.json"),o,u.getTerrainTextures());const y=f("textures/flipbook_textures.json");i(y,JSON.stringify(g.flipbook_textures,null,2));const j=r.getUIdefList();i(f("ui/_ui_defs.json"),JSON.stringify({ui_defs:j},null,2)),r.getUISystemList().forEach((e=>{i(f(e.path,`${e.name}.json`),JSON.stringify(e.toJson(),null,2))}));t.getDataList().forEach((({name:t,root:r,path:s,data:a})=>{if("blocks/"===s){const e=(e=>{const t=e.textures;if(6!==t.length)return console.warn(`blocks.json 的 textures 数组长度不为 6,实际长度: ${t.length}`),{};const r={};return r[e.identifier]={textures:{up:t[0],down:t[1],east:t[2],west:t[3],south:t[4],north:t[5]}},r})(a);Object.assign(p,e)}const c="behavior"===r?`${o}_BP`:`${o}_RP`,l=e.join(n,c),u=e.join(l,s,`${t}.json`);i(u,JSON.stringify(a.toJson(),null,2))})),console.log("保存 blocks.json 文件:",x),i(x,JSON.stringify(p,null,2)),console.log(`已加载并执行 ${s} 文件!`)}catch(e){console.error(`加载或执行 ${s} 失败:${e.message}`),console.error(e.stack)}},{port:f}={port:49037};const x=new class{cliServerHandlers=new Map;listening=!1;isListening(){return this.listening}bootstrap(){this.listening=!0;const e=n.createServer((async(e,t)=>{const r=this.cliServerHandlers.get((e.url??"/").slice(1));if(r){try{const{promise:t,resolve:s,reject:n}=Promise.withResolvers();let i=Buffer.alloc(0);e.on("data",(e=>i=Buffer.concat([i,e]))),e.on("end",(()=>{try{s(JSON.parse(i))}catch(e){n(e)}})),await r(...await t)}catch(e){return console.error(e),t.writeHead(500),void t.end()}t.writeHead(200),t.end()}else t.writeHead(404),t.end()})).listen(f,(()=>console.log(`Dev Server listening on port ${f}`)));return e.on("error",(()=>this.listening=!1)),e}handle(e,t){this.cliServerHandlers.set(e,t)}getHandler(e){return this.cliServerHandlers.get(e)}interceptHandler(e,t){const r=t(this.getHandler(e)??Function.prototype);return this.cliServerHandlers.set(e,r),r}};async function p(t,s){const n=JSON.parse(r(e.join(t,"build.config"))),i=e.join(t,n.defaultConfig.buildDir),a=e.join(i,`${s}_BP`),c=e.join(i,`${s}_RP`);o(a,e.join(n.mojangPath,"development_behavior_packs/",`${s}_BP/`)),o(c,e.join(n.mojangPath,"development_resource_packs/",`${s}_RP/`))}function m(e,t){x.isListening()||(!function(e,t){x.bootstrap(),x.handle(_.WRITE_ADDON,d(e,t)),x.handle(_.WRITE_GAME_DEV,p)}(e,t),process.send("initialized"))}export{_ as ServerHandles,x as devServer,m as startDevServer};
1
+ import http from 'http';
2
+
3
+ const devServerConfig = {
4
+ port: 49037
5
+ };
6
+
7
+ const {
8
+ port: port$1
9
+ } = devServerConfig;
10
+
11
+ async function cliRequest(path, ...params) {
12
+ try {
13
+ await fetch(`http://localhost:${port$1}/${path}`, {
14
+ method: 'POST',
15
+ headers: {
16
+ 'Content-Type': 'application/json'
17
+ },
18
+ body: JSON.stringify(params)
19
+ });
20
+ } catch (error) {
21
+ console.error(error);
22
+ console.error('尝试在构建脚本中使用 server.startDevServer() 启动开发服务器');
23
+ }
24
+ }
25
+
26
+ const { port } = devServerConfig;
27
+ class DevelopmentServer {
28
+ cliServerHandlers = new Map();
29
+ listening = false;
30
+ isListening() {
31
+ return this.listening;
32
+ }
33
+ bootstrap() {
34
+ this.listening = true;
35
+ const svr = http.createServer(async (req, res) => {
36
+ const handler = this.cliServerHandlers.get((req.url ?? '/').slice(1));
37
+ if (handler) {
38
+ try {
39
+ const { promise, resolve, reject } = Promise.withResolvers();
40
+ let buf = Buffer.alloc(0);
41
+ req.on('data', chunk => buf = Buffer.concat([buf, chunk]));
42
+ req.on('end', () => {
43
+ try {
44
+ resolve(JSON.parse(buf));
45
+ }
46
+ catch (error) {
47
+ reject(error);
48
+ }
49
+ });
50
+ await handler(...await promise);
51
+ }
52
+ catch (error) {
53
+ console.error(error);
54
+ res.writeHead(500);
55
+ res.end();
56
+ return;
57
+ }
58
+ res.writeHead(200);
59
+ res.end();
60
+ }
61
+ else {
62
+ res.writeHead(404);
63
+ res.end();
64
+ }
65
+ }).listen(port, () => console.log(`Dev Server listening on port ${port}`));
66
+ svr.on('error', () => this.listening = false);
67
+ return svr;
68
+ }
69
+ handle(url, handler) {
70
+ this.cliServerHandlers.set(url, handler);
71
+ }
72
+ getHandler(url) {
73
+ return this.cliServerHandlers.get(url);
74
+ }
75
+ interceptHandler(url, interceptor) {
76
+ const currentHandler = this.getHandler(url) ?? Function.prototype;
77
+ const newHandler = interceptor(currentHandler);
78
+ this.cliServerHandlers.set(url, newHandler);
79
+ return newHandler;
80
+ }
81
+ }
82
+ const server = new DevelopmentServer();
83
+
84
+ const client = {
85
+ call: async (name, ...args) => {
86
+ return await cliRequest(name, ...args)
87
+ }
88
+ };
89
+
90
+ export { client, server as devServer };