用户:
星辰Lolita查看:20 回复:10 评论:20 创建时间:2021-07-24T08:59:12
今天来做指令
首先检测玩家说话
world.onChat(({message,entity})=>{
if(message[0] === '/'){
const command = message.substring(1);
call(command,entity);
}
})
然后写指令内部
function call(command,entity){
const c = command.split(' ');//按空格分割
switch(c[0]){
}
}
然后新建一个错误类
class wrong{
constructor(message){
this.message = message;
this.isWrong = true;
}
}
之后switch内写一些指令
function call(command,entity){
const c = command.split(' ');//按空格分割
switch(c[0]){
case '喵':
if(c.length > 2){
return new Wrong('指令错误');
}else if(c.length === 1){
entity.hurt(100);
}else{
}
break;
}
}
MC中指定对象使用的是“@”
那我们写一个有关@的
function at(obj){
if(obj[0] !== '@')return new Wrong('指令错误');
if(obj.length > 2){
return new Wrong('没有' + obj + '这个对象');
}else if(obj.length === 1){
return new Wrong('@后面应该填一个对象');
}else{
switch(obj[1]){
case 'a':
return world.querySelectorAll('player');
}
}
}
然后处理“@”结果
function call(command,entity){
const c = command.split(' ');//按空格分割
switch(c[0]){
case '喵':
if(c.length > 2){
return new Wrong('指令错误');
}else if(c.length === 1){
entity.hurt(100);
}else{
const result = at(c[1]);
if(result.isWrong)return result;
result.forEach((e)=>{
e.hurt(100);
})
}
break;
}
}
然后玩家进入地图时将玩家可伤害设为true
world.onPlayerJoin(({entity})=>{
entity.enableDamage = true;
})
还有中途写的时候出了点差错、
所以完整代码是
world.onChat(({message,entity})=>{
if(message[0] === '/'){
let command = message.substring(1);
while(true){
if(command[command.length - 1] !== ' ')break;
command = command.substring(0,command.length - 1);
}
const result = call(command,entity);
if(typeof result === 'string'){
entity.player.directMessage(result);
}
}
})
function call(command,entity){
let c = command.split(' ');//按空格分割
switch(c[0]){
case '喵':
if(c.length > 2){
return '指令错误';
}else if(c.length === 1){
entity.hurt(100);
}else{
const result = at(c[1]);
if(typeof result === 'string')return result;
result.forEach((e)=>{
e.hurt(100);
})
}
break;
default:
return c[0] + '不是一个指令';
}
}
function at(obj){
if(obj[0] !== '@')return '指令错误';
if(obj.length > 2){
return '没有' + obj + '这个对象';
}else if(obj.length === 1){
world.say('s')
return '@后面应该填一个对象';
}else{
switch(obj[1]){
case 'a':
return world.querySelectorAll('player');
default:
return '没有@' + obj[1] + '这个对象';
}
}
}
world.onPlayerJoin(({entity})=>{
entity.enableDamage = true;
})
可以再加