HTTP Block
HTTP Block 添加了两个可配置的红石方块,使 Minecraft 能够与外部 HTTP 服务进行通信。HTTP Sender(HTTP 发送器)在接收红石信号时发送 JSON POST 请求,而 HTTP Receiver(HTTP 接收器)则持续轮询一个 HTTP 端点,并在检测到新事件时发出红石脉冲。这使得 Minecraft 与任何能够发送或接收 HTTP 请求的应用程序之间的数据交换变得轻松,无论使用的是何种编程语言或框架。
- HTTP Sender — 在接收到红石上升沿时发送 HTTP
POST 请求。
- HTTP Receiver — 轮询 HTTP 端点,并在检测到新事件时发出短促的红石脉冲。
HTTP Sender
右键点击该方块以进行配置。
URL
接收请求的 HTTP 端点。
示例:
http://localhost:8000/event
Payload
随请求发送的原始 JSON 主体。
示例:
{ "message": "Hello" }
行为
- 仅在红石状态从 OFF 变为 ON 时发送 HTTP
POST 请求。
- 在持续供电期间不会重复发送请求。
- 使用
Content-Type: application/json。
- 请求异步执行,不会阻塞游戏。
HTTP Receiver
右键点击该方块以进行配置。
URL
用于轮询事件的端点。
示例:
http://localhost:8000/event
注意: 端点必须返回符合下方所述 Event Format(事件格式) 的 JSON。接收器期望该结构以正确检测新事件。
Last Message
显示最近接收到的事件中的消息。
行为
- 定期轮询配置的端点。
- 将返回事件的
hash 与上一次接收到的进行比较。
- 仅当 hash 发生变化时才发出红石脉冲。
- 重复的事件不会触发另一次脉冲。
- 脉冲强度始终为 15。
- 脉冲持续时间可配置。
Event Format
接收器期望端点返回以下格式的 JSON:
{
"timestamp": 1750412345,
"message": "Hello",
"hash": "6a8f6c5d..."
}
| 字段 |
描述 |
timestamp |
事件的 Unix 时间戳。 |
message |
在接收器 GUI 中显示的文本。 |
hash |
事件的唯一标识符。新的 hash 会触发脉冲。 |
仅使用 hash 来检测新事件。
下面提供了一个简单的 Python 示例。
Example Relay (FastAPI)
HTTP Receiver 期望一个以上述格式返回事件的端点。以下是满足所需 API 的最小 FastAPI 中继实现。
from fastapi import FastAPI
from pydantic import BaseModel
import hashlib
import time
import uvicorn
app = FastAPI()
latest_event = {
"timestamp": 0,
"message": "",
"hash": "",
}
class EventIn(BaseModel):
message: str
@app.post("/send_event")
async def create_event(data: EventIn):
global latest_event
message = str(data.message)
timestamp = int(time.time())
event_hash = hashlib.sha256(f"{timestamp}:{message}".encode()).hexdigest()
latest_event = {
"timestamp": timestamp,
"message": message,
"hash": event_hash,
}
return {"success": True}
@app.get("/get_event")
async def get_event():
return latest_event
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=9000)
安装所需的包:
pip install fastapi uvicorn
运行中继:
python relay.py
然后配置你的方块:
- HTTP Sender URL:
http://localhost:9000/send_event
- HTTP Receiver URL:
http://localhost:9000/get_event
命令
/http pollrate <ticks>
设置接收器轮询其配置 URL 的频率。
/http pulselength <ticks>
设置接收器脉冲的持续时间。
/http status
显示当前配置以及已注册的发送器/接收器数量。
/http reload
从磁盘重新加载配置。
HTTP Block
HTTP Block adds two configurable redstone blocks that allow Minecraft to communicate with external HTTP services. The HTTP Sender can send JSON POST requests when powered by redstone, while the HTTP Receiver continuously polls an HTTP endpoint and emits a redstone pulse whenever it detects a new event. This makes it easy to exchange data between Minecraft and any application capable of sending or receiving HTTP requests, regardless of the programming language or framework used.
- HTTP Sender — sends an HTTP
POST request when it receives a redstone rising edge.
- HTTP Receiver — polls an HTTP endpoint and emits a short redstone pulse whenever a new event is detected.
HTTP Sender
Right-click the block to configure it.
URL
The HTTP endpoint that will receive the request.
Example:
http://localhost:8000/event
Payload
The raw JSON body that will be sent with the request.
Example:
{ "message": "Hello" }
Behavior
- Sends an HTTP
POST request only when redstone changes from OFF to ON.
- Does not repeatedly send requests while continuously powered.
- Uses
Content-Type: application/json.
- Requests are performed asynchronously and do not block the game.
HTTP Receiver
Right-click the block to configure it.
URL
The endpoint to poll for events.
Example:
http://localhost:8000/event
Note: The endpoint must return JSON matching the Event Format described below. The receiver expects that structure to detect new events correctly.
Last Message
Displays the message from the most recently received event.
Behavior
- Polls the configured endpoint periodically.
- Compares the returned event's
hash with the last one it received.
- Emits a redstone pulse only when the hash changes.
- Duplicate events do not trigger another pulse.
- Pulse strength is always 15.
- Pulse duration is configurable.
Event Format
The receiver expects the endpoint to return JSON in the following format:
{
"timestamp": 1750412345,
"message": "Hello",
"hash": "6a8f6c5d..."
}
| Field |
Description |
timestamp |
Unix timestamp of the event. |
message |
Text shown in the receiver GUI. |
hash |
Unique identifier for the event. A new hash triggers a pulse. |
Only the hash is used to detect new events.
A simple Python Example is shown below.
Example Relay (FastAPI)
The HTTP Receiver expects an endpoint that returns events in the format shown above. Below is a minimal FastAPI relay implementation that satisfies the required API.
from fastapi import FastAPI
from pydantic import BaseModel
import hashlib
import time
import uvicorn
app = FastAPI()
latest_event = {
"timestamp": 0,
"message": "",
"hash": "",
}
class EventIn(BaseModel):
message: str
@app.post("/send_event")
async def create_event(data: EventIn):
global latest_event
message = str(data.message)
timestamp = int(time.time())
event_hash = hashlib.sha256(f"{timestamp}:{message}".encode()).hexdigest()
latest_event = {
"timestamp": timestamp,
"message": message,
"hash": event_hash,
}
return {"success": True}
@app.get("/get_event")
async def get_event():
return latest_event
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=9000)
Install the required packages:
pip install fastapi uvicorn
Run the relay:
python relay.py
Then configure your blocks:
- HTTP Sender URL:
http://localhost:9000/send_event
- HTTP Receiver URL:
http://localhost:9000/get_event
Commands
/http pollrate <ticks>
Sets how often receivers poll their configured URL.
/http pulselength <ticks>
Sets how long receiver pulses last.
/http status
Shows the current configuration and registered sender/receiver counts.
/http reload
Reloads the configuration from disk.
评论 0
登录后参与评分
还没有评论,来抢沙发吧