
Lv.1
人机?定教他有来无回
签名:我是FW
在 【神岛】代码问题来找我() 中回复
class item{
constructor(){
this.it = new Map();
};
add(i,n){
if(n < 0){
console.error('<~NumberError: can‘t add a negative value.')
}
if(this.it.get(i) === undefined)
this.it.set(i,n);
else
this.it.set(i,this.it.get(i)+n);
}
del(i,n){
if(this.it.get(i) === undefined){
console.error("<~ReferenceError: key: ‘" + i + '’ is not definded.');
}
else{
if(this.it.get(i) >= n)
this.it.set(i,this.it.get(i)-n);
else
console.error('<~NumberError: ' + n +' is too big for key: ’' + i + '‘.');
}
}
geti(i){
if(this.it.get(i) === undefined){
console.error("<~ReferenceError: key: ‘" + i + '’ is not definded.');
return undefined;
}
else{
return this.it.get(i);
}
}
has(i){
return this.it.get(i) != undefined;
}
clear(){
this.it.clear()
}
size(){
return this.it.size;
}
}
world.onPlayerJoin(({entity})=>{
console.clear();
entity.item = new item();//开背包类
entity.item.add('114514',-114514);//增加键‘114514’的对应值-114514
entity.item.add('114514',114514);//增加键‘114514’的对应值114514
entity.item.del('1919810',1919810);//删除键‘1919810’的对应值1919810
entity.item.del('114514',1919810)//删除键‘114514’的对应值1919810;
console.log(entity.item.geti('114514'));//返回键‘114514’的对应值
console.log(entity.item.geti('1919810'));//返回键‘1919810’的对应值
console.log(entity.item.has('114514'));//返回item中是否有键‘114514’
entity.item.clear();//清除玩家背包
console.log(entity.item.size());//返回背包大小
})
更好的背包。
2022-07-13T09:03:06 点赞:0