На этом канале публикуются все истории (как и что я делаю в Майнкрафте).
Основной канал: https://t.me/botikdeadpriz
Post #115
12
БО @botikst
const TelegramBot = require('node-telegram-bot-api');
const WebSocket = require('ws');
const WebSocketServer = require('ws').WebSocketServer;
const token = 'ВАШ_ТОКЕН_БОТА';
const bot = new TelegramBot(token, { polling: true });
let mcWebSocket = null;
let chatId = null;
const wss = new WebSocketServer({ port: 8000, host: '0.0.0.0' }); //port НужноЗаписатьСвободныйПорт_ИспользуйтеОт_2000_До_9000_порт8000МожетБытьЗанят
console.log('WebSocket');
wss.on('connection', (ws) => {
console.log('player connect');
mcWebSocket = ws;
if (chatId) {
bot.sendMessage(chatId, 'bot connect');
}
ws.on('message', (data) => {
try {
const message = JSON.parse(data);
console.log('message play', JSON.stringify(message, null, 2));
if (message.header && message.header.event === 'chat') {
const playerName = message.body.sender || 'Player';
const text = message.body.message || 'Message';
if (chatId) {
bot.sendMessage(chatId, `${playerName}: ${text}`);
}
}
} catch (error) {
console.log('error', error);
}
});
ws.on('close', () => {
console.log('disabled player');
mcWebSocket = null;
if (chatId) {
bot.sendMessage(chatId, 'disabled bot');
}
});
ws.on('error', (error) => {
console.log('error WebSocket ', error.message);
});
});
bot.onText(/\/start/, (msg) => {
chatId = msg.chat.id;
bot.sendMessage(chatId, 'bot launched');
bot.sendMessage(chatId, 'connect localhost:');
});
bot.on('message', (msg) => {
if (msg.text && msg.text.startsWith('/')) return;
chatId = msg.chat.id;
if (mcWebSocket && mcWebSocket.readyState === WebSocket.OPEN) {
const command = `tellraw @a {"rawtext":[{"text":"§b<Telegram> §f${msg.text}"}]}`;
const message = {
header: {
requestId: Math.random().toString(36).substring(2, 10),
messagePurpose: "commandRequest",
version: 1
},
body: {
commandLine: command,
origin: {
type: "player"
}
}
};
mcWebSocket.send(JSON.stringify(message));
console.log(`play: ${msg.text}`);
} else {
bot.sendMessage(chatId, 'bot not connected');
}
});
console.log('bot connect > message');termux-setup-storagepkg update && pkg upgrade -ypkg install nodejs-lts nano -ymkdir mc-telegram-botcd mc-telegram-botnano bot.jsnpm init -ynpm install node-telegram-bot-api@0.64.0npm install wsnode bot.js/connect localhost:8000
ifconfigimport { world, system } from "@minecraft/server";
const speed = 0.25;
const grav = 0.08;
system.runInterval(() => {
const dimension = world.getDimension("overworld");
const entities = dimension.getEntities({ tags: ["my_mob"] });
for (const entity of entities) {
const vel = entity.getVelocity();
let impulseX = 0;
let impulseY = 0;
let impulseZ = 0;
if (entity.hasTag("x")) {
impulseX = speed - vel.x;
}
if (entity.hasTag("y")) {
impulseY = speed - vel.y + grav;
}
if (entity.hasTag("z")) {
impulseZ = speed - vel.z;
}
if (impulseX !== 0 || impulseY !== 0 || impulseZ !== 0) {
entity.applyImpulse({
x: impulseX,
y: impulseY,
z: impulseZ
});
}
}
});import { world, system } from "@minecraft/server";
const speed = 0.25;
const grav = 0.08;
system.runInterval(() => {
for (const player of world.getPlayers()) {
const vel = player.getVelocity();
let impulseX = 0;
let impulseY = 0;
let impulseZ = 0;
if (player.hasTag("x")) {
impulseX = speed - vel.x;
}
if (player.hasTag("y")) {
impulseY = speed - vel.y + grav;
}
if (player.hasTag("z")) {
impulseZ = speed - vel.z;
}
if (impulseX !== 0 || impulseY !== 0 || impulseZ !== 0) {
player.applyImpulse({
x: impulseX,
y: impulseY,
z: impulseZ
});
}
}
});import { world, system } from '@minecraft/server';
const stgpref = 'data_server';
const maxm = 5;
const chksz = 32000;
const appsz = 10000;
function getm() {
const m = [];
for (let i = 1; i <= maxm; i++) {
const t = stgpref + i;
const e = world.getDimension('overworld').getEntities({
type: 'minecraft:armor_stand',
tags: [t]
});
if (e.length > 0) {
m.push({ mob: e[0], idx: i });
} else {
m.push(null);
}
}
return m;
}
function svd(d) {
const m = getm();
const c = [];
for (let i = 0; i < d.length; i += chksz) {
c.push(d.substring(i, i + chksz));
}
if (c.length > maxm) {
return false;
}
for (let i = 0; i < c.length; i++) {
const mi = m[i];
if (!mi) {
return false;
}
mi.mob.setDynamicProperty('data', c[i]);
}
for (let i = c.length; i < maxm; i++) {
if (m[i]) {
m[i].mob.setDynamicProperty('data', undefined);
}
}
return true;
}
function ldd() {
const m = getm();
const p = [];
for (const mi of m) {
if (!mi) continue;
const d = mi.mob.getDynamicProperty('data');
if (d !== undefined && d !== null && d !== '') {
p.push(d);
}
}
return p.length > 0 ? p.join('') : null;
}
function gtsz() {
let t = 0;
const m = getm();
for (const mi of m) {
if (!mi) continue;
const d = mi.mob.getDynamicProperty('data');
if (d) t += d.length;
}
return t;
}
function shinf() {
const m = getm();
let msg = 'DAT ser\n';
let t = 0;
for (const mi of m) {
if (!mi) {
msg += `${stgpref}?\n`;
continue;
}
const d = mi.mob.getDynamicProperty('data');
const u = d ? d.length : 0;
msg += `${stgpref}${mi.idx}: ${u}/${chksz} byte (${(u / 1024).toFixed(1)} KB)\n`;
t += u;
}
msg += `tot: ${t} byte, ${(t / 1024).toFixed(1)} KB`;
world.sendMessage(msg);
}
function rndstr(l) {
const c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let r = '';
for (let i = 0; i < l; i++) {
r += c.charAt(Math.floor(Math.random() * c.length));
}
return r;
}
function appnd() {
const m = getm();
for (const mi of m) {
if (!mi) {
world.sendMessage('Создать всех data_server');
return;
}
}
let ed = ldd();
let o;
if (ed) {
try {
o = JSON.parse(ed);
} catch (e) {
world.sendMessage('error data');
o = {};
}
} else {
o = {};
}
let fi = 0;
while (o.hasOwnProperty('field' + fi)) {
fi++;
}
o['field' + fi] = rndstr(appsz);
const js = JSON.stringify(o);
const ms = maxm * chksz;
if (js.length > ms) {
world.sendMessage(`result (${js.length} byte) limit (${ms} byte)`);
return;
}
if (svd(js)) {
world.sendMessage(`data: +${appsz} failed${fi}. tot: ${js.length} byte (${(js.length/1024).toFixed(1)} KB).`);
} else {
world.sendMessage('failed data');
}
}
function deld() {
const m = getm();
for (const mi of m) {
if (!mi) continue;
mi.mob.setDynamicProperty('data', undefined);
}
world.sendMessage('delete');
}
function prc() {
const m = getm();
for (const mi of m) {
if (!mi) continue;
const mob = mi.mob;
const t = mob.getTags();
if (t.includes('dynamic')) {
shinf();
mob.removeTag('dynamic');
}
if (t.includes('over')) {
appnd();
mob.removeTag('over');
}
if (t.includes('delete_data')) {
deld();
mob.removeTag('delete_data');
}
}
}
system.runInterval(() => {
prc();
}, 10);import { world, system } from "@minecraft/server";
world.beforeEvents.chatSend.subscribe((data) => {
fik(data)
})
function fik(data) {
const send = data.sender;
const message = data.message;
const cach1 = message === "!start";
const cach2 = message === "!help";
const cach3 = message === "!menu";
const cach4 = message === "!admin";
const ff = message === "!";
const cancel = (cach1 || cach2 || cach3 || cach4 || ff) && (data.cancel = true);
const che1 = cach1 && system.run(() => {
send.runCommand(`title @a title команда start`);
});
const che2 = cach2 && system.run(() => {
send.runCommand(`title @a title команда help`);
});
const che3 = cach3 && system.run(() => {
send.runCommand(`title @a title команда menu`);
});
const che4 = cach4 && system.run(() => {
if (send.hasTag("op")) {
send.runCommand(`title @a title команда admin`);
} else {
send.runCommand(`title @a title нет тега "op"`);
}
});
const ffi = ff && system.run(() => {
send.sendMessage(`!start\n!help\n!menu\n!admin (tag=op)`);
});
return;
}ОСТАЛЬНЫЕ ФАЙЛЫ УДАЛЕНЫ, ВЕРНУТЬ НЕВОЗМОЖНО.