forked from jbaragry/mcpi-scratch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcpi-scratch.js
286 lines (261 loc) · 12 KB
/
mcpi-scratch.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
(function (ext) {
var blockHits = false;
ext.postToChat = function(str) {
var cmdUrl = "http://localhost:4715/postToChat/" + encodeURIComponent(str);
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("postToChat success");
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error postToChat: ", error);
}
}); // nb: GET is including the javascript callback. Do I need this for one-way call?
};
ext.playerPosToChat = function() {
var cmdUrl = "http://localhost:4715/playerPosToChat";
$.ajax({
type: "GET",
url: cmdUrl,
// dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("playerPosToChat success");
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error playerPosToChat: ", error);
}
}); // nb: GET is including the javascript callback. Do I need this for one-way call?
};
ext.setPlayerPos = function(x, y, z) {
var cmdUrl = "http://localhost:4715/setPlayerPos/" + x + "/" + y + "/" + z;
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("setPlayerPos success");
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error setPlayerPos: ", error);
}
}); // nb: GET is including the javascript callback. Do I need this for one-way call?
};
ext.setBlock = function(x, y, z, blockType, blockData, posType) {
var cmdUrl = "http://localhost:4715/setBlock/" + x + "/" + y + "/" + z + "/" + blockType + "/" + blockData + "/" + posType;
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("setBlock success");
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error setBlock: ", error);
}
}); // nb: GET is including the javascript callback. Do I need this for one-way call?
};
ext.setBlocks = function(x1, y1, z1, x2, y2, z2, blockType, blockData) {
var cmdUrl = "http://localhost:4715/setBlocks/" + x1 + "/" + y1 + "/" + z1 + "/"
+ x2 + "/" + y2 + "/" + z2 + "/" + blockType + "/" + blockData;
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("setBlocks success");
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error setBlocks: ", error);
}
}); // nb: GET is including the javascript callback. Do I need this for one-way call?
};
ext.setLine = function(x1, z1, x2, z2, y, blockType, blockData) {
var cmdUrl = "http://localhost:4715/setLine/" + x1 + "/" + z1 + "/"
+ x2 + "/" + z2 + "/" + y + "/" + blockType + "/" + blockData;
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("setLine success");
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error setLine: ", error);
}
}); // nb: GET is including the javascript callback. Do I need this for one-way call?
};
ext.setCircle = function(x, z, r, y, blockType, blockData) {
var cmdUrl = "http://localhost:4715/setCircle/" + x + "/" + z + "/"
+ r + "/" + y + "/" + blockType + "/" + blockData;
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("setCircle success");
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error setCircle: ", error);
}
}); // nb: GET is including the javascript callback. Do I need this for one-way call?
};
// get one coord (x, y, or z) for playerPos
ext.getPlayerPos = function(posCoord, callback) {
var cmdUrl = "http://localhost:4715/getPlayerPos/" + posCoord;
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("getPlayerPos success ", data.trim());
callback(data.trim());
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error getPlayerPos: ", error);
callback(null);
}
});
};
// get block.id or block.data
ext.getBlock = function(blockData, x, y, z, posType, callback) {
var cmdUrl = "http://localhost:4715/getBlock/" + blockData + "/" + x + "/" + y + "/" + z + "/" + posType;
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("getBlock success ", data.trim());
callback(data.trim());
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error getBlock: ", error);
callback(null);
}
});
};
function checkMC_Events() {
var cmdUrl = "http://localhost:4715/pollBlockHit/";
$.ajax({
type: "GET",
url: cmdUrl,
//dataType: "jsonp", // hack for the not origin problem - replace with CORS based solution
success: function(data) {
console.log("checkMC_Events success ", data.trim());
if (parseInt(data) == 1)
blockHits = true;
else
blockHits = false;
},
error: function(jqxhr, textStatus, error) { // have to change this coz jasonp parse error
console.log("Error checkMC_Events: ", error);
callback(null);
}
});
};
ext.whenBlockHit = function(str) {
if (!blockHits)
return;
else
return true;
};
ext._getStatus = function() {
return { status:2, msg:'Ready' };
};
ext._shutdown = function() {
if (poller) {
clearInterval(poller);
poller = null;
}
};
var TRANSLATIONS = {
en: {
postToChat: 'post to chat %s',
playerPosToChat: "post Player.pos chat",
setPlayerPos: "set Player pos to x:%n y:%n z:%n",
setBlock: "set block pos x:%n y:%n z:%n to type %n data %n %m.blockPos",
setBlocks: "set blocks pos x1:%n y1:%n z1:%n to x2:%n y2:%n z2:%n to type %n data %n",
setLine: "set line pos x1:%n z1:%n to x2:%n z2:%n height y:%n to type %n data %n",
setCircle: "set circle center x1:%n z1:%n radius r:%n at height y:%n to type %n data %n",
getPlayerPos:"get player pos %m.pos",
getBlock:"get block %m.blockData pos x:%n y:%n z:%n %m.blockPos",
whenBlockHit: "when blockHit",
message:"message"
},
pt: {
postToChat: "escreve no chat %s",
playerPosToChat: "escreve posição do jogador no chat",
setPlayerPos: "muda a pos do Jogador para x:%n y:%n z:%n",
setBlock: "muda o bloco na pos x:%n y:%n z:%n para o tipo %n subtipo %n %m.blockPos",
setBlocks: "coloca blocos da pos x1:%n y1:%n z1:%n até x2:%n y2:%n z2:%n do tipo %n subtipo %n",
setLine: "desenha linha da pos x1:%n z1:%n até x2:%n z2:%n à altura de y:%n com blocos tipo %n subtipo %n",
setCircle: "desenha circulo com centro x1:%n z1:%n, raio r:%n à altura y:%n com blocos tipo %n subtipo %n",
getPlayerPos:"posição do Jogador no eixo do %m.pos",
getBlock:"bloco %m.blockData na pos x:%n y:%n z:%n %m.blockPos",
whenBlockHit: "quando bloco atingido",
message:"mensagem"
},
de: {
postToChat: 'Zeige Nachricht %s',
playerPosToChat: "Zeige Spieler-Position als Nachricht",
setPlayerPos: "Stelle Spieler auf x:%n y:%n z:%n",
setBlock: "Stelle Block auf x:%n y:%n z:%n Block %n Status %n %m.blockPos",
setBlocks: "Setze Bloecke von x1:%n y1:%n z1:%n bis x2:%n y2:%n z2:%n Block %n data %n",
setLine: "Linie von x1:%n z1:%n bis x2:%n z2:%n Hoehe y:%n mit Block %n Status %n",
setCircle: "Kreis mit Mittelpunkt x1:%n z1:%n Radius r:%n Hoehe y:%n Block %n Status %n",
getPlayerPos:"Spieler-Position %m.pos",
getBlock:"Block %m.blockData fuer x:%n y:%n z:%n %m.blockPos",
whenBlockHit: "Wenn Block berührt",
message:"Nachricht"
},
}
function getTranslationForLang( lang ){
switch (lang){
case "pt":
case "pt-PT":
case "pt-BR":
return TRANSLATIONS.pt;
case "de":
case "de-DE":
case "de-AT":
case "de-CH":
case "de-LI":
case "de-LU":
return TRANSLATIONS.de;
default:
return TRANSLATIONS.en;
}
}
// how which language translation is chosen (increasing priority):
// 1 - explicit 'lang' parameter in the url (e.g: http://scratchx.org/?url=https://paulolc.neocities.org/mcpi-scratch/mcpi-scratch.js&lang=pt#scratch)
// 2 - browser first preferred language (navigator.languages[0])
// 3 - default (english)
var urlParams = new URLSearchParams(window.location.search);
var lang = urlParams.get('lang') || navigator.languages[0];
var translate = getTranslationForLang(lang);
// Block and block menu descriptions
var descriptor = {
blocks: [
['', translate.postToChat, "postToChat", translate.message],
[" ", translate.playerPosToChat,"playerPosToChat"],
[" ", translate.setPlayerPos,"setPlayerPos", 0, 0, 0],
[" ", translate.setBlock,"setBlock", 0, 0, 0, 1, -1],
[" ", translate.setBlocks,"setBlocks", 0, 0, 0, 0, 0, 0, 1, -1],
[" ", translate.setLine,"setLine", 0, 0, 0, 0, 0, 1, -1],
[" ", translate.setCircle,"setCircle", 0, 0, 0, 0, 0, 1, -1],
["R", translate.getPlayerPos,"getPlayerPos", 'x'],
["R", translate.getBlock,"getBlock", '', 0, 0, 0],
["h", translate.whenBlockHit,'whenBlockHit'],
],
menus: {
pos: ['x', 'y', 'z'],
blockPos: ['abs', 'rel'],
blockData: ['id', 'data']
}
};
// Register the extension
ScratchExtensions.register('MCPI-Scratch', descriptor, ext);
checkMC_Events();
var poller = setInterval(checkMC_Events, 2000);
})({});