Skip to content

Commit

Permalink
[CodeFactor] Apply fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
code-factor authored and Visual1mpact committed Jun 25, 2022
1 parent 13f82a6 commit 3665477
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions scripts/misc/dpwrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { crypto } from "../util.js";

if (config.dynamicPropertyWrapper.enabled) {
// cancel registration
PropertyRegistry.prototype.registerEntityTypeDynamicProperties = () => {}
PropertyRegistry.prototype.registerWorldDynamicProperties = () => {}
PropertyRegistry.prototype.registerEntityTypeDynamicProperties = () => {};
PropertyRegistry.prototype.registerWorldDynamicProperties = () => {};

// thing?
const dpw = config.dynamicPropertyWrapper.uniqueID.slice(0, 6);
Expand All @@ -18,109 +18,109 @@ if (config.dynamicPropertyWrapper.enabled) {
uniqueID = String(crypto(dpw, pw)).slice(0, 6);
} else {
uniqueID = dpw;
};
}

const getPropertyDatas = (scoreObj) => {
const data = Object.create(null)
const data = Object.create(null);
for (const [name] of scoreObj.getScores()) {
// parse the saved value
const [match, id, type] = name.match(/(.*?)\|\|(string|number|boolean)\|\|/) ?? []
const [match, id, type] = name.match(/(.*?)\|\|(string|number|boolean)\|\|/) ?? [];

// skip if match is undefined (misformatted)
if (typeof match == 'undefined') continue
if (typeof match == 'undefined') continue;

// parse value
const valueStr = name.substring(match.length),
value = type == 'number' ? +valueStr
: type == 'boolean' ? valueStr == 'true'
: JSON.parse(`"${valueStr}"`)
: JSON.parse(`"${valueStr}"`);

// add the value in property list
data[JSON.parse(`"${id}"`)] = {
cachedValue: value,
scoreboardName: `${id}||${type}||${value}`
}
};
}
return data
}
return data;
};

// world dynamic property
const worldObj = scoreboard.objective.for(`${uniqueID}:wld`).dummies;
const worldPropertyList = getPropertyDatas(worldObj)
const worldPropertyList = getPropertyDatas(worldObj);

World.prototype.getDynamicProperty = (id) => {
return worldPropertyList[id]?.cachedValue
}
return worldPropertyList[id]?.cachedValue;
};
World.prototype.setDynamicProperty = (id, value) => {
const vtype = typeof value
if (vtype != 'string' && vtype != 'number' && vtype != 'boolean') throw new TypeError(`Unexpected value type ${vtype}`)
const vtype = typeof value;
if (vtype != 'string' && vtype != 'number' && vtype != 'boolean') throw new TypeError(`Unexpected value type ${vtype}`);

// delete existing
const propData = worldPropertyList[id] ??= {}
if (propData.scoreboardName !== undefined) worldObj.delete(propData.scoreboardName)
const propData = worldPropertyList[id] ??= {};
if (propData.scoreboardName !== undefined) worldObj.delete(propData.scoreboardName);

// set
propData.cachedValue = value
worldObj.set(propData.scoreboardName = `${id}||${vtype}||${value}`, 0)
}
propData.cachedValue = value;
worldObj.set(propData.scoreboardName = `${id}||${vtype}||${value}`, 0);
};
World.prototype.removeDynamicProperty = (id) => {
if (!(id in worldPropertyList)) return false
if (!(id in worldPropertyList)) return false;

// delete
worldObj.delete(worldPropertyList[id].scoreboardName)
delete worldPropertyList[id]
worldObj.delete(worldPropertyList[id].scoreboardName);
delete worldPropertyList[id];

return true
}
return true;
};

// player dynamic property
Player.prototype.getDynamicProperty = function(id) {
return getDataOfPlayer(this).data[id]?.cachedValue
}
return getDataOfPlayer(this).data[id]?.cachedValue;
};
Player.prototype.setDynamicProperty = function(id, value) {
const vtype = typeof value
if (vtype != 'string' && vtype != 'number' && vtype != 'boolean') throw new TypeError(`Unexpected value type ${vtype}`)
const vtype = typeof value;
if (vtype != 'string' && vtype != 'number' && vtype != 'boolean') throw new TypeError(`Unexpected value type ${vtype}`);

const {obj, data} = getDataOfPlayer(this)
const {obj, data} = getDataOfPlayer(this);

// delete existing
const propData = data[id] ??= {}
if (propData.scoreboardName !== undefined) obj.delete(propData.scoreboardName)
const propData = data[id] ??= {};
if (propData.scoreboardName !== undefined) obj.delete(propData.scoreboardName);

// set
propData.cachedValue = value
obj.set(propData.scoreboardName = `${id}||${vtype}||${value}`, 0)
}
propData.cachedValue = value;
obj.set(propData.scoreboardName = `${id}||${vtype}||${value}`, 0);
};
Player.prototype.removeDynamicProperty = function (id) {
const {obj, data} = getDataOfPlayer(this)
if (!(id in data)) return false
const {obj, data} = getDataOfPlayer(this);
if (!(id in data)) return false;

// delete
obj.delete(data[id].scoreboardName)
delete data[id]
obj.delete(data[id].scoreboardName);
delete data[id];

return true
}
return true;
};

SimulatedPlayer.prototype.getDynamicProperty = Player.prototype.getDynamicProperty
SimulatedPlayer.prototype.setDynamicProperty = Player.prototype.setDynamicProperty
SimulatedPlayer.prototype.removeDynamicProperty = Player.prototype.removeDynamicProperty
SimulatedPlayer.prototype.getDynamicProperty = Player.prototype.getDynamicProperty;
SimulatedPlayer.prototype.setDynamicProperty = Player.prototype.setDynamicProperty;
SimulatedPlayer.prototype.removeDynamicProperty = Player.prototype.removeDynamicProperty;

// player data & uid registry
const staticUidRegistry = scoreboard.objective.for(`${uniqueID}:uidreg`).players;
const dataList = new WeakMap()
const dataList = new WeakMap();
const getDataOfPlayer = (plr) => {
// return if player data has already been set
if (dataList.has(plr)) return dataList.get(plr)
if (dataList.has(plr)) return dataList.get(plr);

// set the player uid
staticUidRegistry.set(plr, 0)
staticUidRegistry.set(plr, 0);

// sets the player obj & data
const obj = scoreboard.objective.for(`${uniqueID}:plr:${plr.scoreboard.id.toString(36)}`).dummies
dataList.set(plr, { obj: obj, data: getPropertyDatas(obj) })
const obj = scoreboard.objective.for(`${uniqueID}:plr:${plr.scoreboard.id.toString(36)}`).dummies;
dataList.set(plr, { obj: obj, data: getPropertyDatas(obj) });

// return player obj & data
return dataList.get(plr)
}
return dataList.get(plr);
};
}

0 comments on commit 3665477

Please sign in to comment.