mcpipyでの開発を快適に

Pythonのコード編集をNotepad++で行い、コマンドプロンプトから実行していたが、さすがに面倒。

Pythonには標準のIDEのIDLEが付属しているらしいとのことで、スタートメニューを見てみた。

ちゃんとPython 3.6フォルダ内に、IDLE (Python 3.6 32-bit)が入っていた。

起動すると、Pythonのインタプリタが起動した。

ソースを編集するには、メニューからOpen。そのままメニューの[Run]-[Run module]もしくはF5キーで、実行してくれる。

自動補完も[Ctrl]+[Space]で可能。

だいぶ楽だね。

マイクラPE(Pocket Edition)のサーバーを建ててみる

PC用マイクラのサーバーを建てられたので、PE版のサーバーも建ててみる。

まず、環境としては、CentOS7にユーザーminecraftがいて、/home/minecraft/が存在しているとする。

$ su minecraft

で、ユーザーを切り替えて、

$ cd ~/
$ mkdir pocketmine
$ cd pocketmine

で、サーバー用のフォルダを作って移動。

後は次のページを参考に、インストール。

https://pmmp.io/get.html

まず、環境を整えるために、必要なパッケージをインストール。

# yum install perl gcc g++ make automake libtool autoconf m4

続いてサーバー本体をインストール

$ wget -q -O – https://get.pmmp.io | bash -s –
[*] Found PocketMine-MP 1.6.2dev (build 234) using API 3.0.0-ALPHA6
[*] This stable build was released on 2017年 6月 25日 日曜日 22:15:51 JST
[-] This channel should have a signature, none found
[*] Installing/updating PocketMine-MP on directory ./
[1/3] Cleaning…
[2/3] Downloading PocketMine-MP 1.6.2dev phar… done!
[3/3] Obtaining PHP: detecting if build is available…
[3/3] Linux 64-bit PHP build available, downloading PHP_7.0.3_x86-64_Linux.tar.gz… checking… regenerating php.ini… done
[*] Everything done! Run ./start.sh to start PocketMine-MP

ちなみに、こちらのサイトからもインストール可能だが、バージョンが1.4.1。PHPもバージョン5が利用されている。

http://www.pocketmine.net/

先ほどインストールしたバージョンは、ログを見る限り1.6.2で、PHPもバージョン7を利用している。

それでは、サーバーをスタートしてみる。
太字のところ以外はエンターキーで次へ。

$ ./start.sh
[*] PocketMine-MP set-up wizard
[*] Please select a language
العربية => ara
Čeština => ces
中文 (简体) => chs
Deutsch => deu
Ελληνικά => ell
English => eng
Eesti => est
Suomi => fin
Français => fra
Gaeilge => gle
עברית => heb
Bahasa Indonesia => ind
Italiano => ita
日本語 => jpn
한국어 => kor
Latviešu => lav
Malti => mlt
Bahasa Melayu => msa
Nederlands => nld
Norsk => nor
Polski => pol
Português => por
Pyccĸий => rus
Español => spa
Svenska => swe
Tagalog => tgl
ภาษาอังกฤษ => tha
tlhIngan => tlh
Türkçe => tur
Українська => ukr
Tiếng Việt => vie
中文(繁體) => zho
[?] Language (eng):
[*] English has been correctly selected.
[*] Welcome to PocketMine-MP!\nBefore starting setting up your new server you have to accept the license.\nPocketMine-MP is licensed under the LGPL License,\nthat you can read opening the LICENSE file on this folder.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

[?] Do you accept the License? (y/N): y
[?] Do you want to skip the set-up wizard? (y/N):

[*] You are going to set up your server now.
[*] If you don’t want to change the default value, just press Enter.
[*] You can edit them later on the server.properties file.
[?] Give a name to your server (Minecraft: PE Server):
[*] Do not change the default port value if this is your first server.
[?] Server port (19132):
[*] Choose between Creative (1) or Survival (0)
[?] Default Game mode (0):
[?] Max. online players (20):
[*] The spawn protection disallows placing/breaking blocks in the spawn zone except for OPs
[?] Enable spawn protection? (Y/n):
[*] An OP is the player admin of the server. OPs can run more commands than normal players
[?] OP player name (example, your game name): nori
[*] The white-list only allows players in it to join.
[?] Do you want to enable the white-list? (y/N):
[!] Query is a protocol used by different tools to get information of your server and players logged in.
[!] If you disable it, you won’t be able to use server lists.
[?] Do you want to disable Query? (y/N):
[*] RCON is a protocol to remote connect with the server console using a password.
[?] Do you want to enable RCON? (y/N):
[*] Getting your external IP and internal IP
[!] Your external IP is 999.999.999.999. You may have to port-forward to your internal IP 192.168.1.2
[!] Be sure to check it, if you have to forward and you skip that, no external players will be able to join. [Press Enter]

[*] You have finished the set-up wizard correctly
[*] Check the Plugin Repository to add new features, minigames, or advanced protection to your server
[*] PocketMine-MP will now start. Type /help to view the list of available commands.

ここまでが設定ウィザード。

途中「sh: ifconfig: コマンドが見つかりません」と表示されるのは、ifconfigがインストールされていないから。特にサーバー自体は問題なく起動した。

次からが実際に起動時のログ。

[22:28:15] [Server thread/INFO]: Loading pocketmine.yml…
[22:28:15] [Server thread/INFO]: Loading server properties…
[22:28:15] [Server thread/INFO]: Selected English (eng) as the base language
[22:28:15] [Server thread/INFO]: Starting Minecraft: PE server version v1.1.0.55
[22:28:15] [Server thread/INFO]: Opening server on 0.0.0.0:19132
[22:28:15] [Server thread/INFO]: This server is running PocketMine-MP version 1.6.2dev-234 “Unleashed” (API 3.0.0-ALPHA6)
[22:28:15] [Server thread/INFO]: PocketMine-MP is distributed under the LGPL License
[22:28:15] [Server thread/INFO]: Loading recipes…
[22:28:15] [Server thread/INFO]: Loading resource packs…
[22:28:16] [Server thread/NOTICE]: Level “world” not found
[22:28:16] [Server thread/INFO]: Preparing level “world”
[22:28:16] [Server thread/NOTICE]: Spawn terrain for level “world” is being generated in the background
[22:28:16] [Server thread/INFO]: Starting GS4 status listener
[22:28:16] [Server thread/INFO]: Setting query port to 19132
[22:28:16] [Server thread/INFO]: Query running on 0.0.0.0:19132
[22:28:16] [Server thread/INFO]: Default game type: Survival Mode
[22:28:16] [Server thread/INFO]: Done (127.96s)! For help, type “help” or “?”

これでサーバーが起動した。

ちなみにファイアウォールのfirewalldでポートが閉じられている場合。
次のコマンドで開いておく。

# firewall-cmd –add-port=19132/udp –zone=public –permanent
# firewall-cmd –reload

ちなみに、ファイアウォールの設定は、次のコマンドで参照可能。

# firewall-cmd –list-all –zone=public

が、コマンドラインで、Pocketmineが起動中なので、設定は一旦サーバーを停止する必要がある。

stop
[22:42:45] [Server thread/INFO]: [CONSOLE: Stopping the server]
[22:42:46] [Server thread/INFO]: Unloading level “world”
[22:42:51] [Server thread/INFO]: Stopping other threads

再度起動して

$ ./start.sh
[22:46:33] [Server thread/INFO]: Loading pocketmine.yml…
[22:46:33] [Server thread/INFO]: Loading server properties…
[22:46:33] [Server thread/INFO]: Selected English (eng) as the base language
[22:46:33] [Server thread/INFO]: Starting Minecraft: PE server version v1.1.0.55
[22:46:33] [Server thread/INFO]: Opening server on 0.0.0.0:19132
[22:46:33] [Server thread/INFO]: This server is running PocketMine-MP version 1.6.2dev-234 “Unleashed” (API 3.0.0-ALPHA6)
[22:46:33] [Server thread/INFO]: PocketMine-MP is distributed under the LGPL License
[22:46:34] [Server thread/INFO]: Loading recipes…
[22:46:34] [Server thread/INFO]: Loading resource packs…
[22:46:34] [Server thread/INFO]: Preparing level “world”
[22:46:34] [Server thread/INFO]: Starting GS4 status listener
[22:46:34] [Server thread/INFO]: Setting query port to 19132
[22:46:34] [Server thread/INFO]: Query running on 0.0.0.0:19132
[22:46:34] [Server thread/INFO]: Default game type: Survival Mode
[22:46:34] [Server thread/INFO]: Done (0.656s)! For help, type “help” or “?”

スマホのマイクラPEから、サーバーに接続。
ちなみに、スマホが同じローカルネットワーク上にあるか、サーバーがグローバルアドレスで参照可能な必要がある。

世界に入ると、

次のようなログが表示される。

[23:28:44] [Server thread/NOTICE]: Player data not found for “nori”, creating new profile
[23:28:50] [Server thread/INFO]: nori[/192.168.1.3:99999] logged in with entity id 1 at (world, 256, 68, 256)
[23:29:01] [Server thread/INFO]: nori joined the game

だがしかし!!

スポーン付近の地面を掘っても掘っても、土が取れない。地面も元に戻ってしまう。

少し移動すれば土も掘れるし、木も切れる。

なぜ???

マイクラでCentOS7上のサーバーが見つからない時

それはファイアウォールが効いているのかもしれません。

設定確認は、

# firewall-cmd –list-all –zone=public

サーバーで設定したポートが含まれない場合、次のコマンドでポートを開けます。

# firewall-cmd –add-port=25565/tcp –zone=public –permanent

次のコマンドで設定を反映させます。

# firewall-cmd –reload

マイクラを再起動させると、見えた!

マイクラでプログラミング(Scratch成功編)

Scrach2.0とマイクラが連携できたので、1.4でも再挑戦してみる。

結論から言うと、Python3系で挑戦していたため、色々とややこしい話になっていたようだ。でも今回は、そのややこしい問題を解決したので、説明しながら進めていこう。

まず、次のページから1.4をインストール。

https://scratch.mit.edu/scratch_1.4/

インストールしたら起動して次からダウンロードしたサンプルプロジェクトをダウンロードして開いてみる。

「遠隔センサーが有効になりました。」と表示されればScratch側の準備はOKでしょう。

ただ、Windowsのファイヤウォールが何か聞いてくるかもしれません。

続いてScratch2MCPIをインストールする。

http://scratch2mcpi.github.io/

ここではcurlという仕組みを使ってインストールする方法が書かれている。が、Windowsなので、今回は手動で入れていく。

まずはscratch2mcpiのzipファイルをダウンロード。

https://github.com/scratch2mcpi/scratch2mcpi/archive/master.zip

解凍してできたscratch2mcpi-masterフォルダを、scratch2mcpiに改名。

このフォルダ内に、色々入れていきますが、このフォルダ自体はどこにあってもOK。そして、フォルダには色々入っているが、必要なのscratch2mcpi.pyだけなので、その他は削除しちゃう。

そして、Python3でも動くように、scratch2mcpi.pyに以下の変更を加える。

print _”~” → print(“~”)

次のフォルダをscratch2mcpiフォルダ内にコピー。

D:\Users\[ユーザ名]\AppData\Roaming\.minecraft\mcpipy\mcpi

次にMinecraft Graphics Turtleをインストール。

次のzipファイルをダウンロード。

https://github.com/scratch2mcpi/minecraft-turtle/archive/master.zip

解凍してできたminecraft-turtle-masterフォルダを、mcturtleに改名。

mcturtleフォルダを先ほどのscratch2mcpiフォルダ内にコピー。

Python3に対応させるために、minecraftturtle.pyのprint文も同じく修正。

次にMinecraft Stuffをインストール。

次のzipファイルをダウンロード。

https://github.com/scratch2mcpi/minecraft-stuff/archive/master.zip

解凍してできたminecraft-stuff-masterフォルダを、mcstuffに改名。

mcstuffフォルダを同じくscratch2mcpiフォルダ内にコピー。

次のzipファイルをダウンロード。

https://github.com/pilliq/scratchpy/archive/master.zip

解凍してできたscratchpy-master/scratchフォルダ内のscratch.pyを、同じくscratch2mcpiフォルダ内にコピー。

さて、このscratch.pyにも細工が必要。byte列とstring型の扱いがPython2とPython3で異なるため、考えながら修正。

例外のエラー番号、文字列なども仕様が変わっている。

で、最終的に次のファイルになった。

scratch.py

著作権的に良いのか?ダメなら連絡して下さい。

では、Scratch1.4とMinecraftをscratch2mcpi.pyを使って連携してみましょう。

コマンドプロンプトから、

>scratch2mcpi.py
Traceback (most recent call last):
File “D:\scratch2mcpi\scratch2mcpi.py”, line 8, in <module>
import mcturtle.minecraftturtle as turtle
File “D:\scratch2mcpi\mcturtle\minecraftturtle.py”, line 5, in <module>
import minecraft
ModuleNotFoundError: No module named ‘minecraft’

はい。エラーです。

mcturtleパケージ内のminecraftturtle.pyから、同じフォルダ内のminecraft.pyが読み込めない。

なぜだ?

すんなり諦めて、minecraftturtle.pyのimport部分をmcpiフォルダ内のファイルを読み込むように変更。

import mcpi.minecraft as minecraft
import mcpi.block as block

どうすればスマートになるのか?誰か教えてください。

とにかく、再度コマンドプロンプトから、

>scratch2mcpi.py
=================
Sratch2MCPI 2.0.1
=================

Connected to Scratch
Received: (‘sensor-update’, {‘blockTypeId’: 1, ‘mcpiX’: 10, ‘mcpiY’: 40, ‘mcpiZ’: 10})

起動していたMinecraftを見ると、何やらブロックが1つ置かれています。

では、Scratch1.4でmcpi_sphere.sbを開いて、緑旗をクリック!

コマンドプロンプトにずらずらと文字列が流れていきます。

見た目は何も変わりません。でも何かが起きているはず。

では、ちょっと上を見てみましょう。

球が出来てきているはずです。

連携が確認出来たことを喜びましょう。

そして私は寝ます。

Hello World!な請求書

某電力会社の請求書をホームページからPDFでダウンロードした。

そしてEvernoteに保存。

と、タイトルが「Hello world (PHP)!」となった。

なぜ?

調べてみると、PHPのサンプルコードそのままらしい。

著者もサンプルのまま「Rainer Schaaf」となっている

各電力会社の関係者の方。あなたの会社かもしれませんよ~。