stoneskin

learn coding from scratch and python

Session 3: Python Function, Pixel Art and Minecraft Server

Session 3.2 Minecraft Server network concept

Network, Router, firewall and ports

1. How Minecraft works in Network

Below diagram show how your pc and and the game server in the network.

minecraft-network

1.1 Minecraft client and server
1.2 Router (Modem Router)
1.3 Private and public network
1.4 IP Addresses

ip-address

1.5 [homework] Find why the the ip address biggest value is 255

2. Router settings, port and firewall

2.1 View the Router settings

if you visit your router by the address 192.168.0.1 or 192.168.1.1 (base on your router default setting). You will be asking the password, if you have password, you could get in and see some page like below.

router-setting

2.2 The ports

In computer networking, a port is a communication endpoint. if a software application or service needs to communicate with others, it will expose a port. Ports are identified with positive 16-bit unsigned integers, ranging from 0 to 65535.

Common network ports number

Port Number Usage
20 File Transfer Protocol (FTP) Data Transfer
21 File Transfer Protocol (FTP) Command Control
22 Secure Shell (SSH)
23 Telnet - Remote login service, unencrypted text messages
25 Simple Mail Transfer Protocol (SMTP) E-mail Routing
53 Domain Name System (DNS) service
80 Hypertext Transfer Protocol (HTTP) used in World Wide Web
443 HTTP Secure (HTTPS) HTTP over TLS/SSL

As you already know, Minecraft client software communicate with the server through the port number “25565”, and you could change that if you host multiple Minecraft servers in one computer. ip-port

2.3 Network Firewall

Network Firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. Most of your router has build in firewall in the hardware, it add a layer of security between your home network and the internet. firewall

So by default, if you host a minecraft server on your home computer. You friends couldn’t access it because the firewall on your router didn’t allow other computer to connect through the port not open. You could go to the router -> firewall setting and open the port forwarding for it. ( more minecraft port forward)

router-firewall

Simply you just need let router forward the request to port 25565 to the computer address where your Minecraft server is on. There are two type of protocols could use the port for different type of data, minecraft need support both TCP/IP(Transmission control Protocol/Internet Protocol) and UDP (User Datagram protocol), so for verizon ‘Fios’ router, I choice both in the dropdown.

If you also want other player run the python on the server, you could add the RaspberryJuice plugin default port 4711 (TCP/IP) on the firewall porting forwarding. port-forward


3 Minecraft Servers Mods and Plugin

The different of Mods and Plugin is tha Plugin only install on the Minecraft server, but Mods must be install on client.

3.1 Minecraft Plugins used in our server

Our server had installed 7 plugins. plugin

We could get bukkit/spigot plugin from the curseforge bukkit plugin or spigot resource site

3.2 How to write python code for MCPI plugin and running on server

To see how to use pytho mcpi plugin, could check this video

The test python code in the c:\python\test\ is like below:

import sys
import time
from mcpi_e.minecraft import Minecraft

serverAddress="127.0.0.1" # change to your minecraft server
pythonApiPort=4711 #default port for RaspberryJuice plugin is 4711, it could be changed in plugins\RaspberryJuice\config.yml
playerName="your name" # change to your username

#the playerName could come from args or will use default
if len(sys.argv)-1>0: #if have args
    playerName=sys.argv[1]
print(playerName)

mc = Minecraft.create(serverAddress,pythonApiPort,playerName)
(x,y,z)=pos = mc.player.getTilePos()

print("pos: x:{},y:{},z:{}".format(pos.x,pos.y,pos.z))
#mc.player.setTilePos(0,0,0)
mc.postToChat("hi")
mc.postToChat("{} pos: x:{},y:{},z:{}".format(playerName, pos.x,pos.y,pos.z))


By default the python mcpi plugin will run the python script in the c:\python\test\ folder. you could change it on the \plugins\config.yml files

config in config.yml


py:
    python_command_template: 'py C:\Python\test\{name}.py'

The {name} will be replace bey the name you input in the game.

In our example when we run a python code, the command is like

py c:\python\test.py

And you could pass one or more arguments

py c:\python\test.py myName

But in you python code, you need code to received the arguments pass through outside.

import sys

name="n/a"
value=0

#get first arguments
if len(sys.argv)-1>0:
    ame=sys.argv[1]
print("name="+name)

#get 2nd arguments
if len(sys.argv)-1>1:
    try:
        value=int(sys.argv[2])
    except:
        print("2nd argument is not a number")

For MCPI plugin, the first argument is always the playId, below is the example of python code could call by MCPI plugin

mcpi-plugin

################################################
# pyramid.py
# call from minecraft /py pyramid 5
###############################################
from mcpi_e.minecraft import Minecraft
from mcpi_e import block
import sys

#--------------------------------------------------------
# default value for connect minecraft
serverAddress = "127.0.0.1"
playerName = "default name"
pythonApiPort = 4711

#------------------------------------------------------
# default value for create pyramid
blockId=block.SANDSTONE.id #default
size=5

# code of get value from arguments
if len(sys.argv)-1>0:
    playerName=sys.argv[1]
if len(sys.argv)-1>1:
    try:
        size=int(sys.argv[2])
    except:
        print("1nd argument is not a number, will use default size=5")
if len(sys.argv)-1>2:
    try:
        blockId=int(sys.argv[3])
    except:
        print("2nd argument is not a number, will use default blockId= Sand")

#------------------------------------------------------
# code of build a pyramid
mc = Minecraft.create(serverAddress,pythonApiPort,playerName)

pos = mc.player.getPos()
x = pos.x
y = pos.y
z = pos.z

for i in range(0,size):
    for j in range(i,size-i):
        for k in range(i,size-i):
            mc.setBlock(x+j,y+i,z+k,blockId)

3.3 [homework] write a python code you could used in game.
  1. Install MCPI plugin to your local server
  2. Test the pyramid code in your local server
  3. Write your own code to implement function want used in minecraft.
  4. [Challenge] Write multiple functions in your code, and pass the args from minecraft to choice which function be called.

4 Some Students homeworks