typescript 使用
by Josh Wulf
通過喬什·沃爾夫(Josh Wulf)
如何使用TypeScript輕松修改Minecraft (How to modify Minecraft the easy way with TypeScript)
Usually, modifying Minecraft requires coding in Java, and a lot of scaffolding. Now you can write and share Minecraft mods using TypeScript/Javascript.
通常,修改Minecraft需要使用Java進行編碼,并需要大量的腳手架。 現在,您可以使用TypeScript / Javascript編寫和共享Minecraft mod。
ScriptCraft is an open source JavaScript Minecraft modding library, and we’ve written support for TypeScript, and a bunch of tooling to create a familiar developer experience for those coming from JavaScript land (including Yeoman and NPM).
ScriptCraft是一個開放源代碼JavaScript Minecraft改裝庫,我們已經編寫了對TypeScript的支持,以及許多工具,可為來自JavaScript領域(包括Yeoman和NPM)的開發人員創建熟悉的開發人員體驗。
In this article I’ll walk you through getting set up and building your first TypeScript Minecraft mod in under an hour — as little as 20 minutes, depending on your internet connection.
在本文中,我將指導您在不到一個小時的時間內完成設置并構建您的第一個TypeScript Minecraft mod(取決于您的Internet連接,只需20分鐘)。
In this video (click here if the embed doesn’t work above) I show you how to write a basic Minecraft mod using TypeScript, and run it on your local computer with both a desktop and a mobile Minecraft server.
在此視頻中(如果無法正常使用, 請單擊此處 ),我將向您展示如何使用TypeScript編寫基本的Minecraft mod,并在具有臺式機和移動Minecraft服務器的本地計算機上運行它。
Below, I’ll walk you through the steps, with links to resources.
下面,我將引導您完成這些步驟,并提供指向資源的鏈接。
先決條件 (Prerequisites)
You’ll need some software installed on your computer, to run the Minecraft server and the tools for writing your plugin. Install all of the four following:
您需要在計算機上安裝一些軟件,才能運行Minecraft服務器和用于編寫插件的工具。 安裝以下所有四個:
Docker — a containerisation solution.
Docker-一種容器化解決方案。
Node.js — a JavaScript execution engine and library.
Node.js — JavaScript執行引擎和庫。
Portainer — a web-based GUI for managing Docker containers.
Portainer-用于管理Docker容器的基于Web的GUI。
Visual Studio Code — a code editor.
Visual Studio代碼 -代碼編輯器。
我的世界客戶端 (Minecraft Client)
You need a Minecraft client to test your plugin.
您需要一個Minecraft 客戶端來測試您的插件。
Install at least one of the following:
安裝以下至少一項:
Minecraft Java Edition — a desktop client, if you want to test against a Bukkit server.
Minecraft Java Edition-如果要針對Bukkit服務器進行測試,則為桌面客戶端。
Minecraft Pocket Edition — a mobile client, if you want to test against a Nukkit server (phone/tablet/Xbox). If you use this, you can use Minecraft Pocket Edition Bedrock Launcher to run the mobile client on your computer.
Minecraft Pocket Edition-移動客戶端,如果要在Nukkit服務器(電話/平板電腦/ Xbox)上進行測試。 如果使用此功能,則可以使用Minecraft Pocket Edition Bedrock Launcher在計算機上運行移動客戶端。
安裝 (Installation)
Now that you have the prerequisites installed, it is time to install the tools for the server and for plugin development.
現在,您已經安裝了先決條件,是時候安裝用于服務器和插件開發的工具了。
- Run the following command: 運行以下命令:
npm i -g smac yo generator-sma-plugin typescript
This will install four things on your computer:
這將在您的計算機上安裝四件事:
smac
— Scriptcraft Modular Architecture Controller, a program that runs Minecraft Servers for your plugins.smac
— Scriptcraft模塊化體系結構控制器,一個為您的插件運行Minecraft服務器的程序。yo
— Yeoman, a scaffolding tool.yo
— Yeoman ,腳手架工具。generator-sma-plugin
— a Yeoman plugin for generating a new Minecraft plugin using the Scriptcraft Modular Architecture.generator-sma-plugin
—一個Yeoman插件,用于使用Scriptcraft模塊化體系結構生成新的Minecraft插件。typescript
— the TypeScript transpiler, for converting TypeScript code into ES5 JavaScript that can run in Minecraft.typescript
-打字稿transpiler,轉換打字稿代碼為ES5JavaScript可以在我的世界中運行。
創建一個新的插件 (Create a new plugin)
Now that you have the toolset installed, create a new plugin by running this command:
現在您已經安裝了工具集,通過運行以下命令來創建一個新插件:
yo sma-plugin
This starts the plugin wizard:
這將啟動插件向導:
? yo sma-plugin
_-----_ ╭──────────────────────────╮ | | │ Welcome to the │ |--(o)--| │ Scriptcraft SMA Plugin │ `---------′ │ generator by │ ( _′U`_ ) │ Magikcraft.io! │ /___A___\ /╰──────────────────────────╯ | ~ | __'.___.'__ ′ ` |° ′ Y `
? Your package name (workspace)
There is only one question you need to answer here — the name of your plugin. The wizard will create a new folder with the name of the plugin, and place the files for the new plugin in it.
您只需在這里回答一個問題-插件的名稱。 該向導將使用插件名稱創建一個新文件夾,并將新插件的文件放入其中。
This screencast shows you the process:
該截屏視頻向您顯示了該過程:
Scaffold a Minecraft plugin using MagikcraftMagikcraft.io allows you to write Minecraft plugins in TypeScript/JavaScript that will run on Desktop / Mobile.asciinema.org
使用Magikcraft 搭建 Minecraft插件的腳手架 Magikcraft.io允許您以TypeScript / JavaScript編寫Minecraft插件,該插件將在臺式機/移動設備上運行。 asciinema.org
Once the wizard completes, it emits a message similar to this (I chose the name my-sma-plugin
in this example):
向導完成后,它會發出類似于以下的消息(在此示例中,我選擇名稱my-sma-plugin
):
編輯您的新插件 (Edit your new plugin)
Start Visual Studio Code and open the directory containing your new plugin.
啟動Visual Studio代碼,然后打開包含新插件的目錄。
Here is a description of the files in your new plugin:
這是新插件中文件的描述:
__tests__
— a directory containing unit tests for your plugin. These are run with Jasmine. Add more tests in here as you develop your plugin.__tests__
—包含插件的單元測試的目錄。 這些與茉莉一起運行。 開發插件時,請在此處添加更多測試。.vscode
— settings for Visual Studio code..vscode
-Visual Studio代碼的設置。autoload
— any files in here are automatically executed when your plugin is enabled in the Minecraft server. Use this for initialisation tasks, registering event handlers, and so forth.autoload
-在Minecraft服務器中啟用插件后,此處的所有文件都會自動執行。 將此用于初始化任務,注冊事件處理程序等。lib
— A place for you to put files that should not be automatically loaded (or that are required from your autoloaded files). If your plugin provides functionality to other plugins, then you export that vialib/index.ts
.lib
—一個放置不應自動加載的文件(或自動加載的文件所必需的文件)的地方。 如果您的插件提供了其他插件的功能,則可以通過lib/index.ts
導出該插件。node_modules
— modules from npm are installed here. You cannot use modules from npm that use V8 APIs (like fs or http). Many of the features that you need are provided by the Scriptcraft API and by the@magikcraft/core
package.node_modules
-npm中的模塊已安裝在此處。 您不能使用npm中使用V8 API的模塊(例如fs或http)。 Scriptcraft API和@magikcraft/core
軟件包提供了您需要的許多功能。.editorconfig
— settings for the editor..editorconfig
編輯器的設置。.gitattributes
— settings forgit
..gitattributes
-git
設置。.gitignore
— files to ignore forgit
..gitignore
忽略git
文件。.prettierrc
— settings for code formatting..prettierrc
—代碼格式設置。package-lock.json
—versions of installed dependencies.package-lock.json
已安裝依賴項的版本。package.json
—configuration for this plugin, including dependencies and scripts.package.json
此插件的配置,包括依賴項和腳本。README.md
— instructions for developing and testing your plugin.README.md
有關開發和測試插件的說明。smac-nukkit.json
— a configuration for running a Nukkit server with your plugin loaded.smac-nukkit.json
—用于在加載插件的情況下運行Nukkit服務器的配置。smac.json
— a configuration for running a Bukkit server with your plugin loaded.smac.json
—用于在加載了插件的情況下運行Bukkit服務器的配置。tsconfig.json
— the TypeScript configuration for transpiling your plugin to JavaScript.tsconfig.json
—用于將插件轉換為JavaScript的TypeScript配置。
Open autoload/index.ts
:
打開autoload/index.ts
:
This file is automatically executed when the plugin is loaded. Changes that you make here will be visible when you (re)load the plugin.
加載插件后,將自動執行此文件。 (重新)加載插件時,您在此處所做的更改將可見。
啟動開發服務器 (Start a development server)
You can load your plugin in a development server. There are two servers that you can use — one for the desktop Java client, and the other for the mobile Pocket Edition client.
您可以將插件加載到開發服務器中。 您可以使用兩臺服務器-一臺用于桌面Java客戶端,另一臺用于移動Pocket Edition客戶端。
啟動桌面服務器 (Start the desktop server)
Run this to start a desktop server:
運行此命令以啟動桌面服務器:
npm run start:bukkit
This will:
這將:
- Pull the Bukkit server image from Docker Hub. 從Docker Hub中提取Bukkit服務器映像。
- Start the Bukkit server with your plugin loaded. 在加載插件的情況下啟動Bukkit服務器。
- Start the TypeScript transpiler to transpile your code to ES5. 啟動TypeScript轉譯器,以將您的代碼轉譯到ES5。
You can now connect to the server with your desktop client. Click on Multiplayer
then Direct Connect
, then use the server address 127.0.0.1
:
現在,您可以使用桌面客戶??端連接到服務器。 單擊Multiplayer
然后單擊Direct Connect
,然后使用服務器地址127.0.0.1
:
啟動移動服務器 (Start the mobile server)
Run this command to start a mobile server:
運行以下命令以啟動移動服務器:
npm run start:nukkit
This will:
這將:
- Pull the Nukkit server image from Docker Hub. 從Docker Hub中提取Nukkit服務器映像。
- Start the Nukkit server with your plugin loaded. 在加載插件的情況下啟動Nukkit服務器。
- Start the TypeScript transpiler to transpile your code to ES5. 啟動TypeScript轉譯器,以將您的代碼轉譯到ES5。
You can now connect to the server with your pocket edition client. Click on Play
then Servers
, then add a server with the address 127.0.0.1
:
現在,您可以使用袖珍版客戶端連接到服務器。 單擊“ Play
,然后單擊Play
Servers
,然后添加地址為127.0.0.1
的服務器:
重新加載對插件的更改 (Reload changes to your plugin)
As you change your plugin and save the changed TypeScript, it will automatically be transpiled to JavaScript.
在更改插件并保存更改的TypeScript時,它將自動轉換為JavaScript。
To reload the changes in the development server, type the following in the server console:
要在開發服務器中重新加載更改,請在服務器控制臺中鍵入以下內容:
ts onrefresh()
See the screencast below to see what this looks like.
請參見下面的截屏視頻,以了解其外觀。
停止服務器 (Stop the server)
To stop the server, type this command at the server console:
要停止服務器,請在服務器控制臺上鍵入以下命令:
smac stop
See the screencast below to see what it looks like when you run this command.
請參閱以下截屏視頻,以查看運行此命令時的外觀。
截屏視頻:開始,重新加載和停止 (Screencast: Start, Reload, and Stop)
This screencast shows you starting the desktop server, reloading the plugin code, and also stopping the development server.
該截屏視頻顯示啟動桌面服務器,重新加載插件代碼以及停止開發服務器。
Start a Magikcraft Development ServerStart a Magikcraft Development Server.asciinema.org
啟動Magikcraft開發服務器 啟動Magikcraft開發服務器。 asciinema.org
更多資源 (Further Resources)
Magikcraft on GitHub
GitHub上的Magikcraft
Magikcraft on YouTube
YouTube上的Magikcraft
MCT1 Source Code (Example Plugin)
MCT1源代碼(示例插件)
ScriptCraft on GitHub
GitHub上的ScriptCraft
Bukkit API Docs
Bukkit API文件
翻譯自: https://www.freecodecamp.org/news/how-to-mod-minecraft-without-java-f076ddaec01c/
typescript 使用