黄豆 饮料 冰棍 实现
This commit is contained in:
1 parent
38f2ff2b33
commit
f89b95fb99
23 files changed
+1773
-88
No files matched your search
+449
@@ -0,0 +1,449 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
================================================================================
|
||||
乡野饮料·右键转换引擎(服务端)
|
||||
================================================================================
|
||||
处理"乡野饮料"(xy_drink) 分类下的全部【右键切换型】转换:
|
||||
1) 榨汁机(果汁·榨汁饮品): 全流程右键
|
||||
空(juicer) + 水果 → 放入水果(juicer_0, 记录水果类型)
|
||||
→ 榨汁中1/2/3(juicer_1/2/3) → 完成(juicer_4)
|
||||
→ 空手右键取出对应果汁, 还原空
|
||||
【水果类型持久化】放入水果时把水果写进方块实体数据(rtdd_jf), 之后每次
|
||||
换态(SetBlockNew 会清空方块实体)都【先读旧→替换→写回新】, 因此玩家
|
||||
在 榨汁2→榨汁3 过程中退出游戏, 重进后取出的果汁仍是第一步放入的水果。
|
||||
2) 冰棍盒(冰棍):
|
||||
未定型(icelollybox_1) + 冰块 → 启动30秒定型计时(转发给计时服务, 消耗1冰)
|
||||
定型(icelollybox_2) + 水果x2 → 各水果冰棍盒(icelollyboxb~f)
|
||||
各水果冰棍盒 空手右键 → 取出2根冰棍, 还原未定型
|
||||
3) 炒冰盒(冰棍盒·炒冰盒):
|
||||
空(frytheicebox) + 冰块 → 放冰(frytheicebox_1)
|
||||
放冰 + 水果 → 各水果炒冰盒(appleicebox 等)
|
||||
各水果炒冰盒(除西瓜) 空手右键 → 取出1个冰沙, 还原空
|
||||
4) 冰棍堆(组合, 同厨具架逻辑):
|
||||
popsiclepile_2(橙子+西瓜) + 苹果冰棍 → popsiclepile_3(橙子+西瓜+苹果)
|
||||
popsiclepile_3 空手右键 → 取出苹果冰棍(LIFO), 退回 popsiclepile_2
|
||||
|
||||
【与计时转换解耦】
|
||||
冰棍盒"未定型→定型"的 30 秒计时拆到独立类 YinliaoTimerService。
|
||||
本类右键到计时源方块(icelollybox_1 + 冰块)时, 调 FeatureDispatcher.startTimerFor
|
||||
转发给计时服务启动(冰块消耗由本类处理)。
|
||||
|
||||
【事件监听在哪?】
|
||||
本类【不】监听任何事件!
|
||||
所有事件由 FeatureDispatcher 统一监听, 按 blockOwner 路由到本类 handleBlockUse。
|
||||
|
||||
【本类怎么做(自上而下的方法系列)】
|
||||
handleBlockUse ← 入口:分发器转发右键方块事件
|
||||
├─ _handleJuicer ← 榨汁机: 放入水果/逐级榨汁/取出果汁
|
||||
│ ├─ _readFruit / _writeFruit ← 水果类型 读/写(持久化)
|
||||
├─ _handleLollyBox ← 冰棍盒: 加冰块(启动计时)/加水果/取冰棍
|
||||
├─ _handleFryIceBox ← 炒冰盒: 加冰块/加水果/取冰沙
|
||||
└─ _handlePopsiclePile ← 冰棍堆: 插苹果冰棍/取回
|
||||
(通用工具) getCarriedItem / consumeItem / replaceBlock / dropItems / checkCooldown
|
||||
================================================================================
|
||||
"""
|
||||
import time
|
||||
import mod.server.extraServerApi as serverApi
|
||||
|
||||
# ---- 网易API基础对象 ----
|
||||
ServerSystem = serverApi.GetServerSystemCls() # 服务端系统基类
|
||||
comp = serverApi.GetEngineCompFactory() # 组件工厂
|
||||
|
||||
# ---- 继承 BlueFox 框架的系统基类(提供 CreateEngineItemEntity 等方法) ----
|
||||
from ....BlueFoxTools.SpecialFunciton.BlueFoxSystem import BlueFoxServerSystem
|
||||
from ....BlueFoxTools.ToolsConfig import LevelId
|
||||
|
||||
# ---- 方块实体数据组件(记录榨汁机水果类型; SetBlockNew 后带延迟兜底) ----
|
||||
blockEntityDataComp = comp.CreateBlockEntityData(LevelId)
|
||||
# ---- 游戏组件(AddTimer 延迟写入 block entity 兜底) ----
|
||||
gameComp = comp.CreateGame(LevelId)
|
||||
|
||||
# ---- 只导入乡野饮料自己的数据库 ----
|
||||
from ..YinliaoData import (
|
||||
ICE_ITEMS,
|
||||
JUICE_FRUITS, JUICER_CHAIN, JUICER_ADVANCE_TIPS,
|
||||
ICE_LOLLY_TIMER, POPSICLE_FRUITS, POPSICLE_BOX_INFO,
|
||||
POPSICLE_PILE_ADD, POPSICLE_PILE_2, POPSICLE_PILE_3,
|
||||
FRYICE_FRUITS, FRYICE_BOX_INFO,
|
||||
)
|
||||
|
||||
# ---- 统一分发器 ----
|
||||
from ...service.FeatureDispatcher import FeatureDispatcher
|
||||
|
||||
|
||||
class YinliaoService(BlueFoxServerSystem):
|
||||
"""
|
||||
乡野饮料·右键转换引擎
|
||||
|
||||
【核心数据结构】
|
||||
featureBlocks = {方块名, ...} # 本功能管辖的所有右键方块(注册路由用)
|
||||
cooldown = {key: 时间戳} # 连点冷却表
|
||||
"""
|
||||
|
||||
def __init__(self, namespace, systemName):
|
||||
super(YinliaoService, self).__init__(namespace, systemName)
|
||||
print "===== YinliaoService init ====="
|
||||
|
||||
# ---- 收集本功能管辖的所有右键方块 ----
|
||||
self.featureBlocks = set()
|
||||
for b in JUICER_CHAIN: # 榨汁机全态
|
||||
self.featureBlocks.add(b)
|
||||
self.featureBlocks.add("rtdd:icelollybox_1") # 冰棍盒(未定型/定型)
|
||||
self.featureBlocks.add("rtdd:icelollybox_2")
|
||||
for _f, info in POPSICLE_FRUITS.items(): # 各水果冰棍盒
|
||||
self.featureBlocks.add(info[0])
|
||||
self.featureBlocks.add("rtdd:frytheicebox") # 炒冰盒(空/放冰)
|
||||
self.featureBlocks.add("rtdd:frytheicebox_1")
|
||||
for _f, info in FRYICE_FRUITS.items(): # 各水果炒冰盒
|
||||
self.featureBlocks.add(info[0])
|
||||
self.featureBlocks.add(POPSICLE_PILE_2) # 冰棍堆
|
||||
self.featureBlocks.add(POPSICLE_PILE_3)
|
||||
|
||||
# ---- 右键冷却表 ----
|
||||
self.cooldown = {}
|
||||
|
||||
# ---- 注册到统一分发器 ----
|
||||
FeatureDispatcher.register(self)
|
||||
|
||||
# ============================================================================
|
||||
# 一、给 FeatureDispatcher 的接口
|
||||
# ============================================================================
|
||||
|
||||
def getFeatureBlocks(self):
|
||||
"""返回本功能管辖的所有方块名集合(右键路由用)"""
|
||||
return self.featureBlocks
|
||||
|
||||
def getItemUseOnRules(self):
|
||||
"""手持水果右键方块表面不需要单独路由, 返回空。
|
||||
水果为可食用物品, 右键饮料方块时会走 ServerBlockUseEvent(与挂鱼一致),
|
||||
由分发器取消后续"吃"行为。"""
|
||||
return {}
|
||||
|
||||
def handleBlockUse(self, args):
|
||||
"""
|
||||
由 FeatureDispatcher 转发的右键方块入口。
|
||||
返回 True = 本功能处理了这个右键(分发器会取消默认行为/吃食物)
|
||||
返回 False = 当前组合没有匹配规则(交还默认行为)
|
||||
"""
|
||||
blockName = args.get("blockName")
|
||||
playerId = args.get("playerId")
|
||||
x, y, z = args.get("x"), args.get("y"), args.get("z")
|
||||
pos = (x, y, z)
|
||||
dim = comp.CreateDimension(playerId).GetEntityDimensionId()
|
||||
key = (dim, x, y, z)
|
||||
|
||||
itemDict = self.getCarriedItem(playerId)
|
||||
itemName = itemDict.get("newItemName") if itemDict else None
|
||||
|
||||
# ---- 榨汁机 ----
|
||||
if blockName in JUICER_CHAIN:
|
||||
return self._handleJuicer(blockName, playerId, pos, dim, itemDict, itemName, key)
|
||||
# ---- 冰棍盒 ----
|
||||
if blockName == "rtdd:icelollybox_1" or blockName == "rtdd:icelollybox_2" \
|
||||
or blockName in POPSICLE_BOX_INFO:
|
||||
return self._handleLollyBox(blockName, playerId, pos, dim, itemDict, itemName, key)
|
||||
# ---- 炒冰盒 ----
|
||||
if blockName == "rtdd:frytheicebox" or blockName == "rtdd:frytheicebox_1" \
|
||||
or blockName in FRYICE_BOX_INFO:
|
||||
return self._handleFryIceBox(blockName, playerId, pos, dim, itemDict, itemName, key)
|
||||
# ---- 冰棍堆 ----
|
||||
if blockName == POPSICLE_PILE_2 or blockName == POPSICLE_PILE_3:
|
||||
return self._handlePopsiclePile(blockName, playerId, pos, dim, itemDict, itemName, key)
|
||||
return False
|
||||
|
||||
# ============================================================================
|
||||
# 二、榨汁机(全流程右键, 水果类型持久化)
|
||||
# ============================================================================
|
||||
|
||||
def _handleJuicer(self, blockName, playerId, pos, dim, itemDict, itemName, key):
|
||||
"""榨汁机分四步: 放入水果 → 逐级榨汁 → 空手取出果汁"""
|
||||
# ---- 空榨汁机 + 水果 → 放入水果(记录水果类型) ----
|
||||
if blockName == "rtdd:juicer":
|
||||
juiceInfo = JUICE_FRUITS.get(itemName)
|
||||
if not juiceInfo:
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
self.consumeItem(playerId, itemDict, 1)
|
||||
self.replaceBlock(pos, dim, "rtdd:juicer_0", playerId)
|
||||
# 替换后写水果类型(block entity 可能未初始化, 带延迟兜底)
|
||||
self._writeFruit(dim, pos, itemName)
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a已放入水果,右键开始榨汁")
|
||||
return True
|
||||
|
||||
# ---- 榨汁中逐级推进(juicer_0→1→2→3→4): 任意右键, 换态并保留水果类型 ----
|
||||
if blockName in JUICER_ADVANCE_TIPS:
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
# 先读旧方块的水果类型(SetBlockNew 会清空方块实体)
|
||||
fruit = self._readFruit(pos, dim)
|
||||
idx = JUICER_CHAIN.index(blockName)
|
||||
self.replaceBlock(pos, dim, JUICER_CHAIN[idx + 1], playerId)
|
||||
if fruit:
|
||||
self._writeFruit(dim, pos, fruit) # 替换后写回新方块
|
||||
tip = JUICER_ADVANCE_TIPS.get(blockName)
|
||||
if tip:
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, tip)
|
||||
return True
|
||||
|
||||
# ---- 完成(juicer_4) 空手右键 → 取出果汁, 还原空 ----
|
||||
if blockName == "rtdd:juicer_4":
|
||||
if itemName and itemName != "minecraft:air":
|
||||
return False # 必须空手取出, 手持其他物品不处理
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
fruit = self._readFruit(pos, dim)
|
||||
juiceInfo = JUICE_FRUITS.get(fruit) if fruit else None
|
||||
if not juiceInfo:
|
||||
return False # 无水果记录(创造直接放置的完成态) → 不反应
|
||||
juiceBlock, juiceName = juiceInfo
|
||||
self.dropItems(dim, pos, [{"name": juiceBlock, "count": 1}])
|
||||
self.replaceBlock(pos, dim, "rtdd:juicer", playerId) # 还原空, 记录随方块清除
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a取出了" + juiceName)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
# ============================================================================
|
||||
# 三、冰棍盒(加冰块启动定型计时 / 加水果 / 取冰棍)
|
||||
# ============================================================================
|
||||
|
||||
def _handleLollyBox(self, blockName, playerId, pos, dim, itemDict, itemName, key):
|
||||
# ---- 未定型 + 冰块 → 启动30秒定型计时(消耗1块冰) ----
|
||||
if blockName == "rtdd:icelollybox_1":
|
||||
if itemName not in ICE_ITEMS:
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
# 转发给计时服务: True=本次启动, False=已在定型中(不重复消耗冰块)
|
||||
started = FeatureDispatcher.startTimerFor(blockName, dim, pos, playerId)
|
||||
if not started:
|
||||
return True # 已在定型中, 拦截但不消耗
|
||||
self.consumeItem(playerId, itemDict, 1)
|
||||
if ICE_LOLLY_TIMER.get("startTip"):
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, ICE_LOLLY_TIMER["startTip"])
|
||||
return True
|
||||
|
||||
# ---- 定型 + 水果x2 → 各水果冰棍盒 ----
|
||||
if blockName == "rtdd:icelollybox_2":
|
||||
info = POPSICLE_FRUITS.get(itemName)
|
||||
if not info:
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
# 数量检查: 需要2个水果, 不足则红字提示且不转换
|
||||
if (itemDict.get("count") or 0) < 2:
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(
|
||||
playerId, "§c需要2个" + info[2] + "才能做冰棍")
|
||||
return True
|
||||
boxBlock, _popsicle, flavor = info
|
||||
self.consumeItem(playerId, itemDict, 2)
|
||||
self.replaceBlock(pos, dim, boxBlock, playerId)
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a" + flavor + "冰棍做好了!")
|
||||
return True
|
||||
|
||||
# ---- 各水果冰棍盒 空手右键 → 取出2根冰棍, 还原未定型 ----
|
||||
info = POPSICLE_BOX_INFO.get(blockName)
|
||||
if not info:
|
||||
return False
|
||||
if itemName and itemName != "minecraft:air":
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
_fruit, popsicle, flavor = info
|
||||
self.dropItems(dim, pos, [{"name": popsicle, "count": 2}])
|
||||
self.replaceBlock(pos, dim, "rtdd:icelollybox_1", playerId) # 还原未定型
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a取出了2根" + flavor + "冰棍")
|
||||
return True
|
||||
|
||||
# ============================================================================
|
||||
# 四、炒冰盒(加冰块 / 加水果 / 取冰沙)
|
||||
# ============================================================================
|
||||
|
||||
def _handleFryIceBox(self, blockName, playerId, pos, dim, itemDict, itemName, key):
|
||||
# ---- 空 + 冰块 → 放冰 ----
|
||||
if blockName == "rtdd:frytheicebox":
|
||||
if itemName not in ICE_ITEMS:
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
self.consumeItem(playerId, itemDict, 1)
|
||||
self.replaceBlock(pos, dim, "rtdd:frytheicebox_1", playerId)
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a已放入冰块,右键放入水果炒冰")
|
||||
return True
|
||||
|
||||
# ---- 放冰 + 水果 → 各水果炒冰盒 ----
|
||||
if blockName == "rtdd:frytheicebox_1":
|
||||
info = FRYICE_FRUITS.get(itemName)
|
||||
if not info:
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
iceBox, _icecream, flavor = info
|
||||
self.consumeItem(playerId, itemDict, 1)
|
||||
self.replaceBlock(pos, dim, iceBox, playerId)
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a" + flavor + "炒冰做好了!")
|
||||
return True
|
||||
|
||||
# ---- 各水果炒冰盒 空手右键 → 取出1个冰沙, 还原空 ----
|
||||
info = FRYICE_BOX_INFO.get(blockName)
|
||||
if not info:
|
||||
return False
|
||||
_fruit, icecream, flavor = info
|
||||
if not icecream:
|
||||
return False # 该口味无成品冰沙(防御), 正常五种口味均有
|
||||
if itemName and itemName != "minecraft:air":
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
self.dropItems(dim, pos, [{"name": icecream, "count": 1}])
|
||||
self.replaceBlock(pos, dim, "rtdd:frytheicebox", playerId) # 还原空
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a取出了" + flavor + "冰沙")
|
||||
return True
|
||||
|
||||
# ============================================================================
|
||||
# 五、冰棍堆(组合式: 插苹果冰棍 / 空手取回 LIFO)
|
||||
# ============================================================================
|
||||
|
||||
def _handlePopsiclePile(self, blockName, playerId, pos, dim, itemDict, itemName, key):
|
||||
# ---- 堆(橙子+西瓜) + 苹果冰棍 → 堆2(橙子+西瓜+苹果) ----
|
||||
if blockName == POPSICLE_PILE_2:
|
||||
if itemName != POPSICLE_PILE_ADD:
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
self.consumeItem(playerId, itemDict, 1)
|
||||
self.replaceBlock(pos, dim, POPSICLE_PILE_3, playerId)
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a把苹果冰棍插到冰棍堆上")
|
||||
return True
|
||||
|
||||
# ---- 堆2 空手右键 → 取出苹果冰棍(后放先取), 退回堆 ----
|
||||
if blockName == POPSICLE_PILE_3:
|
||||
if itemName and itemName != "minecraft:air":
|
||||
return False
|
||||
if not self.checkCooldown(key, 0.5):
|
||||
return True
|
||||
self.dropItems(dim, pos, [{"name": POPSICLE_PILE_ADD, "count": 1}])
|
||||
self.replaceBlock(pos, dim, POPSICLE_PILE_2, playerId)
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, "§a取走了苹果冰棍")
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
# ============================================================================
|
||||
# 六、水果类型 读/写(榨汁机果汁持久化, 字段 rtdd_jf)
|
||||
# ============================================================================
|
||||
|
||||
def _writeFruit(self, dim, pos, itemName):
|
||||
"""把水果类型写入榨汁机方块实体数据(替换方块后调用, 带 0.1s 延迟兜底)"""
|
||||
try:
|
||||
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
|
||||
if blockData is None:
|
||||
gameComp.AddTimer(0.1, self._writeFruitDelayed, dim, pos, itemName)
|
||||
return
|
||||
blockData["rtdd_jf"] = str(itemName)
|
||||
except Exception:
|
||||
gameComp.AddTimer(0.1, self._writeFruitDelayed, dim, pos, itemName)
|
||||
|
||||
def _writeFruitDelayed(self, dim, pos, itemName):
|
||||
"""延迟写入兜底(block entity 未初始化时)"""
|
||||
try:
|
||||
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
|
||||
if blockData is not None:
|
||||
blockData["rtdd_jf"] = str(itemName)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _readFruit(self, pos, dim):
|
||||
"""从方块实体数据读出水果类型, 无记录返回 None"""
|
||||
try:
|
||||
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
|
||||
if blockData is None:
|
||||
return None
|
||||
return blockData["rtdd_jf"] or None
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
# ============================================================================
|
||||
# 七、工具方法:获取玩家主手物品
|
||||
# ============================================================================
|
||||
|
||||
def getCarriedItem(self, playerId):
|
||||
itemComp = comp.CreateItem(playerId)
|
||||
return itemComp.GetPlayerItem(
|
||||
serverApi.GetMinecraftEnum().ItemPosType.CARRIED, 0, True)
|
||||
|
||||
# ============================================================================
|
||||
# 八、工具方法:消耗物品
|
||||
# ============================================================================
|
||||
|
||||
def consumeItem(self, playerId, itemDict, amount=1):
|
||||
if itemDict is None:
|
||||
return
|
||||
count = itemDict.get("count", 0)
|
||||
if count <= amount:
|
||||
comp.CreateItem(playerId).SetEntityItem(
|
||||
serverApi.GetMinecraftEnum().ItemPosType.CARRIED,
|
||||
{"itemName": "minecraft:air", "count": 0, "auxValue": 0})
|
||||
else:
|
||||
itemDict["count"] = count - amount
|
||||
comp.CreateItem(playerId).SetEntityItem(
|
||||
serverApi.GetMinecraftEnum().ItemPosType.CARRIED,
|
||||
{"itemName": itemDict["newItemName"],
|
||||
"count": itemDict["count"],
|
||||
"auxValue": itemDict.get("newAuxValue", 0),
|
||||
"userData": itemDict.get("userData", {})})
|
||||
|
||||
# ============================================================================
|
||||
# 九、工具方法:替换方块
|
||||
# ============================================================================
|
||||
|
||||
def replaceBlock(self, pos, dim, newBlockName, playerId=None):
|
||||
"""
|
||||
替换指定位置的方块。
|
||||
playerId 可为 None, 用 LevelId 创建组件。
|
||||
"""
|
||||
creator = playerId if playerId is not None else LevelId
|
||||
aux = 0
|
||||
try:
|
||||
oldBlock = comp.CreateBlockInfo(creator).GetBlockNew(pos, dim)
|
||||
aux = oldBlock.get("aux", 0)
|
||||
except Exception:
|
||||
aux = 0
|
||||
comp.CreateBlockInfo(creator).SetBlockNew(
|
||||
pos,
|
||||
blockDict={"name": newBlockName, "aux": aux},
|
||||
oldBlockHandling=0,
|
||||
dimensionId=dim)
|
||||
|
||||
# ============================================================================
|
||||
# 十、工具方法:以掉落物形式生成物品(取果汁/冰棍/冰沙)
|
||||
# ============================================================================
|
||||
|
||||
def dropItems(self, dim, pos, dropList):
|
||||
"""
|
||||
dropList 格式:[{"name": "xxx", "count": N}, ...]
|
||||
在方块上方生成掉落物实体, 让它自然掉到地上。
|
||||
"""
|
||||
for d in dropList:
|
||||
itemDict = {
|
||||
"itemName": d["name"],
|
||||
"count": d.get("count", 1),
|
||||
"auxValue": d.get("aux", 0),
|
||||
}
|
||||
self.CreateEngineItemEntity(itemDict, dim, (pos[0], pos[1] + 0.5, pos[2]))
|
||||
|
||||
# ============================================================================
|
||||
# 十一、工具方法:冷却检查
|
||||
# ============================================================================
|
||||
|
||||
def checkCooldown(self, key, cd=0.5):
|
||||
"""
|
||||
右键冷却:同一方块 cd 秒内只响应一次,防止连点刷物品/刷提示。
|
||||
"""
|
||||
now = time.time()
|
||||
last = self.cooldown.get(key, 0)
|
||||
if now - last < cd:
|
||||
return False
|
||||
self.cooldown[key] = now
|
||||
return True
|
||||
+234
@@ -0,0 +1,234 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
================================================================================
|
||||
乡野饮料·计时转换引擎(服务端)
|
||||
================================================================================
|
||||
只处理"乡野饮料"(xy_drink) 分类下的【计时自动转换】:
|
||||
冰棍盒(未定型 icelollybox_1) → 冰棍盒(定型 icelollybox_2) 约30秒
|
||||
|
||||
【与右键转换解耦】
|
||||
同分类的"右键切换"(榨汁/冰棍盒加水果/炒冰盒/冰棍堆)在 YinliaoService。
|
||||
本类只负责计时,两者通过 FeatureDispatcher 协作:
|
||||
- YinliaoService 手持冰块右键 未定型 → 调 FeatureDispatcher.startTimerFor
|
||||
转发到本类的 startTimer 启动定型计时(冰块消耗由右键服务处理)
|
||||
- 服务器 tick(ServerBlockEntityTickEvent)由 FeatureDispatcher 按 timerOwner
|
||||
路由到本类的 handleBlockEntityTick,到点即转换为 定型
|
||||
|
||||
【计时怎么做到"退出游戏继续计时"】
|
||||
不做倒计时,只记"开始时刻":
|
||||
- 启动时把 开始时间戳(rtdd_tstart)+总时长(rtdd_tdur) 写进方块实体数据
|
||||
- 每帧 tick 判断 当前时间-开始时刻 >= 总时长 → 到点转换
|
||||
- 时间戳是绝对时间(time.time()),服务器重启/玩家离线,真实时间仍在流逝,
|
||||
回来后 tick 直接算差值,剩余时间自然准确
|
||||
|
||||
【事件监听在哪?】
|
||||
本类【不】监听任何事件!
|
||||
ServerBlockEntityTickEvent 由 FeatureDispatcher 统一监听,再按 timerOwner
|
||||
路由到本类的 handleBlockEntityTick。
|
||||
|
||||
【本类怎么做(自上而下的方法系列)】
|
||||
handleBlockEntityTick ← 入口:分发器转发 tick 事件
|
||||
└─ _tickProcess ← 判断是否到点 / autoStart=False 不自动补写
|
||||
└─ _finishTimer ← 到点执行转换 + 完成提示
|
||||
startTimer ← 入口:YinliaoService 转发过来启动定型
|
||||
└─ _writeTimerData ← 写 开始时刻+时长+启动者 到方块实体
|
||||
_clearTimerData ← 清理方块上的计时数据
|
||||
================================================================================
|
||||
"""
|
||||
import time
|
||||
import mod.server.extraServerApi as serverApi
|
||||
|
||||
# ---- 网易API基础对象 ----
|
||||
ServerSystem = serverApi.GetServerSystemCls() # 服务端系统基类
|
||||
comp = serverApi.GetEngineCompFactory() # 组件工厂
|
||||
|
||||
# ---- 继承 BlueFox 框架的系统基类 ----
|
||||
from ....BlueFoxTools.SpecialFunciton.BlueFoxSystem import BlueFoxServerSystem
|
||||
from ....BlueFoxTools.ToolsConfig import LevelId
|
||||
|
||||
# ---- 方块实体数据组件(计时持久化:把开始时间戳+时长写进方块,重启后继续计时) ----
|
||||
blockEntityDataComp = comp.CreateBlockEntityData(LevelId)
|
||||
|
||||
# ---- 只导入乡野饮料自己的数据库 ----
|
||||
from ..YinliaoData import ICE_LOLLY_TIMER
|
||||
|
||||
# ---- 统一分发器(本类把自己注册进计时路由表,让分发器把 tick 事件转发过来) ----
|
||||
from ...service.FeatureDispatcher import FeatureDispatcher
|
||||
|
||||
|
||||
class YinliaoTimerService(BlueFoxServerSystem):
|
||||
"""
|
||||
乡野饮料·计时转换引擎
|
||||
|
||||
【核心数据结构】
|
||||
iceLollyTimers = {计时源方块名: 规则dict} # 目前只有 冰棍盒未定型
|
||||
"""
|
||||
|
||||
def __init__(self, namespace, systemName):
|
||||
super(YinliaoTimerService, self).__init__(namespace, systemName)
|
||||
print "===== YinliaoTimerService init ====="
|
||||
|
||||
# ---- 收集计时型规则(source → 规则) ----
|
||||
self.iceLollyTimers = {ICE_LOLLY_TIMER["source"]: ICE_LOLLY_TIMER}
|
||||
|
||||
# ---- 注册到分发器的计时路由表(tick 事件按方块名路由到这里) ----
|
||||
FeatureDispatcher.registerTimer(self)
|
||||
|
||||
# ============================================================================
|
||||
# 一、给 FeatureDispatcher 的接口
|
||||
# ============================================================================
|
||||
|
||||
def getTimerBlocks(self):
|
||||
"""返回本功能的所有"计时源方块"集合(tick 路由用)"""
|
||||
return set(self.iceLollyTimers.keys())
|
||||
|
||||
def handleBlockEntityTick(self, args):
|
||||
"""由 FeatureDispatcher 转发的计时方块 tick 入口"""
|
||||
dim = args.get("dimension")
|
||||
pos = (args.get("posX"), args.get("posY"), args.get("posZ"))
|
||||
blockName = args.get("blockName", "")
|
||||
self._tickProcess(dim, pos, blockName)
|
||||
|
||||
# ============================================================================
|
||||
# 二、定型计时启动(YinliaoService 手持冰块右键未定型后转发调用)
|
||||
# ============================================================================
|
||||
|
||||
def startTimer(self, dim, pos, blockName, playerId=None):
|
||||
"""
|
||||
启动定型计时:把 开始时间戳+时长 写入方块实体数据。
|
||||
返回 True = 本次真正启动(右键服务据此消耗冰块)
|
||||
返回 False = 已在定型中 / 无规则(不重复消耗)
|
||||
"""
|
||||
recipe = self.iceLollyTimers.get(blockName)
|
||||
if not recipe:
|
||||
return False
|
||||
# 去重:如果该方块已经在本规则计时中,不重置(保持原剩余时间)
|
||||
try:
|
||||
data = blockEntityDataComp.GetBlockEntityData(dim, pos)
|
||||
if data and data["rtdd_tid"] == blockName:
|
||||
return False
|
||||
except Exception:
|
||||
pass
|
||||
self._writeTimerData(dim, pos, recipe, playerId)
|
||||
return True
|
||||
|
||||
# ============================================================================
|
||||
# 三、tick 检查与到点转换
|
||||
# ============================================================================
|
||||
|
||||
def _tickProcess(self, dim, pos, blockName):
|
||||
"""
|
||||
tick 主逻辑:
|
||||
1) 按事件自带的当前方块名判断是否计时源
|
||||
2) 是计时源但没有计时数据 → 只有 autoStart=True 才自动补写。
|
||||
冰棍盒定型 autoStart=False:必须右键(放冰块)激活, 这里不补写
|
||||
3) 有计时数据 → 到点执行转换(重启后时间戳照常生效)
|
||||
注意:GetBlockEntityData 返回的对象不支持 .get(),统一用 [] 读取。
|
||||
"""
|
||||
try:
|
||||
recipe = self.iceLollyTimers.get(blockName)
|
||||
if not recipe:
|
||||
return # 当前方块不是计时源
|
||||
|
||||
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
|
||||
if blockData is None:
|
||||
return
|
||||
|
||||
try:
|
||||
tid = blockData["rtdd_tid"]
|
||||
except Exception:
|
||||
tid = ""
|
||||
if tid != blockName:
|
||||
# 冰棍盒必须右键(放冰块)激活定型: autoStart=False 时不自动补写计时
|
||||
if recipe.get("autoStart", True):
|
||||
self._writeTimerData(dim, pos, recipe, None)
|
||||
return
|
||||
|
||||
tstart = blockData["rtdd_tstart"]
|
||||
tdur = blockData["rtdd_tdur"]
|
||||
|
||||
if time.time() - tstart >= tdur:
|
||||
self._finishTimer(dim, pos, recipe, blockData)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _finishTimer(self, dim, pos, recipe, data):
|
||||
"""计时到点:确认方块仍是计时源,才执行转换并清除计时数据"""
|
||||
try:
|
||||
cur = comp.CreateBlockInfo(LevelId).GetBlockNew(pos, dim)
|
||||
if cur and cur.get("name") != recipe["source"]:
|
||||
# 方块已被玩家改变 → 清掉过期计时
|
||||
self._clearTimerData(dim, pos)
|
||||
return
|
||||
# 替换方块为定型
|
||||
self.replaceBlock(pos, dim, recipe["target"], None)
|
||||
self._clearTimerData(dim, pos)
|
||||
if recipe.get("doneTip"):
|
||||
self._notifyIfOnline(data["rtdd_tp"], recipe["doneTip"])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# ============================================================================
|
||||
# 四、计时数据读写(存在方块实体数据里,重启不丢)
|
||||
# ============================================================================
|
||||
|
||||
def _writeTimerData(self, dim, pos, recipe, playerId=None):
|
||||
"""把 计时标识+开始时刻+时长+启动者 写进方块实体数据"""
|
||||
try:
|
||||
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
|
||||
if blockData is None:
|
||||
return
|
||||
now = time.time()
|
||||
blockData["rtdd_tid"] = recipe["source"]
|
||||
blockData["rtdd_tstart"] = now
|
||||
blockData["rtdd_tdur"] = recipe.get("time", 60)
|
||||
blockData["rtdd_tp"] = playerId if playerId else ""
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _clearTimerData(self, dim, pos):
|
||||
"""清除方块上的计时数据"""
|
||||
try:
|
||||
blockData = blockEntityDataComp.GetBlockEntityData(dim, pos)
|
||||
if blockData is None:
|
||||
return
|
||||
blockData["rtdd_tid"] = ""
|
||||
blockData["rtdd_tstart"] = 0
|
||||
blockData["rtdd_tdur"] = 0
|
||||
blockData["rtdd_tp"] = ""
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# ============================================================================
|
||||
# 五、工具方法:完成提示
|
||||
# ============================================================================
|
||||
|
||||
def _notifyIfOnline(self, playerId, tip):
|
||||
"""若启动者在线,发送完成提示(离线则跳过)"""
|
||||
if not tip or not playerId:
|
||||
return
|
||||
try:
|
||||
ids = comp.CreateGame(LevelId).GetAllPlayerIds()
|
||||
if playerId in ids:
|
||||
comp.CreateMsg(playerId).NotifyOneMessage(playerId, tip)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# ============================================================================
|
||||
# 六、工具方法:替换方块
|
||||
# ============================================================================
|
||||
|
||||
def replaceBlock(self, pos, dim, newBlockName, playerId=None):
|
||||
"""替换指定位置的方块(计时回调时没有玩家, 用 LevelId 创建组件)"""
|
||||
creator = playerId if playerId is not None else LevelId
|
||||
aux = 0
|
||||
try:
|
||||
oldBlock = comp.CreateBlockInfo(creator).GetBlockNew(pos, dim)
|
||||
aux = oldBlock.get("aux", 0)
|
||||
except Exception:
|
||||
aux = 0
|
||||
comp.CreateBlockInfo(creator).SetBlockNew(
|
||||
pos,
|
||||
blockDict={"name": newBlockName, "aux": aux},
|
||||
oldBlockHandling=0,
|
||||
dimensionId=dim)
|
||||
Whitespace-only changes.
Reference in new issue
Block a user