This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EconomicSync.lxl.js
205 lines (177 loc) · 4.83 KB
/
EconomicSync.lxl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//LiteXLoader Dev Helper
/// <reference path="c:\Users\yqs11\.vscode\extensions\moxicat.lxldevhelper-0.1.4/Library/JS/Api.js" />
// 文件名:EconomicSync.lxl.js
// 文件功能:LXL平台下经济自动同步工具
// 作者:yqs112358
// 首发平台:MineBBS
var _VER = '1.0.1'
lxl.requireVersion(0,5,5);
//Helper
String.prototype.replaceAll=function(f,t)
{
var reg=new RegExp(f,"g");
return this.replace(reg,t);
}
var conf = new IniConfigFile("./plugins/EconomicSync/config.ini", "");
logger.setTitle("EconomicSync");
function CheckScoreboard()
{
let economic = conf.getStr("Main","ScoreboardName","money");
if(mc.getScoreObjective(economic) == null)
{
if(!mc.newScoreObjective(economic,economic))
logger.warn("经济记分板创建失败!");
else
logger.info("同步经济记分板 '" + economic + "' 已创建");
}
}
function SyncMoney(pl)
{
let scoreName = conf.getStr("Main","ScoreboardName","money");
let score = pl.getScore(scoreName);
let mon = money.get(pl.xuid);
if(score != mon)
{
if(conf.getStr("Main","MainEconomic") == "Scoreboard")
money.set(pl.xuid, score);
else
pl.setScore(scoreName,mon);
}
}
function ChangeMessage(pl, value)
{
let infoStr = conf.getStr("Info","ChangeMessage");
if(infoStr.trim() != "")
{
infoStr = infoStr.replaceAll("%%Money%%",String(value));
pl.sendText(infoStr);
}
}
let skip = false;
mc.listen("onScoreChanged",function(pl,num,name,displayName)
{
if(skip)
{
skip = false;
return;
}
if(name == conf.getStr("Main","ScoreboardName","money"))
{
let old = money.get(pl.xuid);
if(old < num)
{
skip = true;
money.add(pl.xuid, num-old);
}
else if(old > num)
{
skip = true;
money.reduce(pl.xuid, old-num);
}
ChangeMessage(pl,num);
}
});
function LLMoneyChange(xuid,newValue)
{
let pl = mc.getPlayer(xuid);
if(pl != null)
{
//在线
skip = true;
pl.setScore(conf.getStr("Main","ScoreboardName","money"),newValue);
ChangeMessage(pl,newValue);
}
else
{
let changed = File.readFrom("plugins/EconomicSync/changed.json");
if(changed == null || changed == "")
changed = "[]";
let arr = JSON.parse(changed);
let index = arr.length == 0 ? -1 : arr.indexOf(xuid);
if(index == -1)
{
//离线修改
arr.push(xuid);
File.writeTo("plugins/EconomicSync/changed.json",JSON.stringify(arr));
}
}
}
mc.listen("onMoneyAdd",function(xuid_para,num_para)
{
if(skip)
{
skip = false;
return;
}
(function(xuid,num,old) {
LLMoneyChange(xuid, old + num);
})(xuid_para, num_para, money.get(xuid_para));
});
mc.listen("onMoneyReduce",function(xuid_para,num_para)
{
if(skip)
{
skip = false;
return;
}
let oldMoney = money.get(xuid_para);
//判正
if(oldMoney < num_para)
return;
(function(xuid,num,old) {
LLMoneyChange(xuid, old - num);
})(xuid_para, num_para, oldMoney);
});
mc.listen("onMoneyTrans",function(from_para,to_para,num_para)
{
if(skip)
{
skip = false;
return;
}
(function(from,to,oldFrom,oldTo,num) {
LLMoneyChange(from, oldFrom - num);
LLMoneyChange(to, oldTo + num);
})(from_para,to_para, money.get(from_para), money.get(to_para), num_para);
});
mc.listen("onMoneySet",function(xuid_para,num_para)
{
if(skip)
{
skip = false;
return;
}
(function(xuid,num) {
LLMoneyChange(xuid, num);
})(xuid_para, num_para);
});
mc.listen("onJoin",function(pl){
let changed = File.readFrom("plugins/EconomicSync/changed.json");
let synced = false;
if(changed != null && changed != "")
{
let arr = JSON.parse(changed);
let index = arr.length == 0 ? -1 : arr.indexOf(pl.xuid);
if(index != -1)
{
//离线修改
let num = money.get(pl.xuid);
pl.setScore(conf.getStr("Main","ScoreboardName","money"), num);
ChangeMessage(pl,num);
arr.splice(index,1);
File.writeTo("plugins/EconomicSync/changed.json",JSON.stringify(arr));
synced = true;
}
}
if(!synced)
SyncMoney(pl);
});
mc.listen("onServerStarted",function(){
CheckScoreboard();
});
mc.regConsoleCmd("economicsync reload", "reload economic's config", function(args){
conf.reload();
});
log('[EconomicSync] EconomicSync经济同步插件已装载 当前版本:' + _VER);
log('[EconomicSync] 作者:yqs112358 首发平台:MineBBS');
log('[EconomicSync] 想要联系作者可前往MineBBS论坛');