FlowContext 增加更新、获取元信息的便捷使用
iohao opened this issue · comments
渔民小镇 commented
FlowContext 增加更新、获取元信息的便捷使用
渔民小镇 commented
使用示例
class MyAttachment implements Attachment {
@Getter
long userId;
String nickname;
}
void attachment(FlowContext flowContext) {
/*
* 不想扩展 FlowContext 子类时,推荐使用
*/
// 获取元信息
MyAttachment attachment = flowContext.getAttachment(MyAttachment.class);
attachment.nickname = "渔民小镇";
// [同步]更新 - 将元信息同步到玩家所在的游戏对外服中
flowContext.updateAttachment(attachment);
// [异步无阻塞]更新 - 将元信息同步到玩家所在的游戏对外服中
flowContext.updateAttachmentAsync(attachment);
}
渔民小镇 commented
扩展 FlowContext 方式的使用示例
通过扩展 FlowContext ,内部已经确定了自定义元信息;在重写 getAttachment 方法后,能更便捷的使用元信息。
void attachment(MyFlowContext flowContext) {
// 获取元信息
MyAttachment attachment = flowContext.getAttachment();
attachment.nickname = "渔民小镇";
// [同步]更新 - 将元信息同步到玩家所在的游戏对外服中
flowContext.updateAttachment();
// [异步无阻塞]更新 - 将元信息同步到玩家所在的游戏对外服中
flowContext.updateAttachmentAsync();
}
class MyFlowContext extends FlowContext {
MyAttachment attachment;
@Override
@SuppressWarnings("unchecked")
public MyAttachment getAttachment() {
if (Objects.isNull(attachment)) {
this.attachment = this.getAttachment(MyAttachment.class);
}
return this.attachment;
}
}
渔民小镇 commented