Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ Sets the interval for the carousel to change statistics.
**Usage: `./stat toggle [total/offline]`**
Toggles whether the total or offline players should be shown.

**Usage: `./stat random`**
Displays a random stat on the scoreboard.

**Usage: `./stat clear`**
Resets and clears all available stats. This is different from `stat all reset` in that events that have not yet occurred after this command is executed are also removed from the stat list.
<strong style="color:red;">A SERVER RESTART IS REQUIRED TO COMPLETE THIS COMMAND, AND WILL CAUSE BASE STAT CATEGORIES TO NOT INCREMENT PROPERLY IF RESTART IS NOT PERFORMED.</strong>

**Usage: `./transferstats`**
A utility command used to transfer statistics from v1.0.0 of this addon to v1.1.0. This command is only present in v1.1.0 & v1.1.1, and is removed in future versions.

Expand Down
12 changes: 6 additions & 6 deletions scripts/src/classes/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Event {
this.eventID = eventID;
this.displayName = displayName;
this.#dpIdentifier = EVENT_ID_PREFIX + eventID;

if (!world.getDynamicPropertyIds().includes(this.#dpIdentifier)) {
this.#initializeDynamicProperty();
}
Expand All @@ -32,15 +32,15 @@ class Event {

hasParticipant(player) {
const data = this.getData();
const participantNames = data.participants.map(participant => participant.name)
const participantNames = data.participants.map(participant => participant.name);
return participantNames.includes(player.name);
}

reset() {
this.#initializeDynamicProperty();
Display.update(this);
}

updateCount(player, count, operation = 'add') {
const data = this.getData();
if (!this.hasParticipant(player)) {
Expand All @@ -50,7 +50,7 @@ class Event {
});
}
const participant = data.participants.find(participant => participant.name === player.name);

switch (operation) {
case 'add':
participant.score += count;
Expand All @@ -64,15 +64,15 @@ class Event {
world.setDynamicProperty(this.#dpIdentifier, JSON.stringify(data));
Display.update(this);
}

getCount(player) {
const data = this.getData();
const participant = data.participants.find(participant => participant.name === player.name);
if (participant === undefined)
return 0;
return participant.score;
}

getTotal() {
const data = this.getData();
let total = 0;
Expand Down
16 changes: 13 additions & 3 deletions scripts/src/classes/EventManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EventManager {
this.eventsToRegister.push({ eventID, displayName, setupCallback });
}
}

getEventIDs() {
return BulkDP.load(this.EVENT_LIST_ID);
}
Expand All @@ -39,7 +39,7 @@ class EventManager {
isRegistered(eventID) {
return this.events[eventID] !== undefined;
}

increment(eventID, player) {
if (!this.exists(eventID))
throw new Error(`[Stats] Could not increment. Event '${eventID}' not found.`);
Expand Down Expand Up @@ -76,14 +76,24 @@ class EventManager {
}
}

/**
* not only reset all events, but also clear the event list
*/
clear() {
this.resetAll(); // must be called before clearing the event list
// reset event list.. because of how events immediately register upon file import the easiest less error prone way to to apply a full clear is
// to restart the server after running this command
BulkDP.save(this.EVENT_LIST_ID, []);
}

validateEventID(eventID) {
if (this.isRegistered(eventID)) {
return true;
} else if (this.exists(eventID)) {
const displayName = JSON.parse(world.getDynamicProperty(EVENT_ID_PREFIX + eventID)).displayName;
if (!displayName)
return false;
this.registerEvent(eventID, displayName, () => {});
this.registerEvent(eventID, displayName, () => { });
return true;
} else {
return false;
Expand Down
26 changes: 23 additions & 3 deletions scripts/src/commands/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const statCommand = new Command({
{ usage: 'stat carousel <add/remove> <statistic>', description: { text: 'Adds or removes a statistic from the carousel.' } },
{ usage: 'stat carousel list', description: { text: 'Lists all statistics in the carousel.' } },
{ usage: 'stat carousel interval [seconds]', description: { text: 'Sets the interval for the statistic carousel.' } },
{ usage: 'stat toggle [total/offline]', description: { text: 'Toggles whether the total field or offline players should be shown.' } }
{ usage: 'stat toggle [total/offline]', description: { text: 'Toggles whether the total field or offline players should be shown.' } },
{ usage: 'stat clear', description: { text: 'Clears all statistics from the stat list. THIS COMMAND REQUIRES A SERVER RESTART TO FULLY TAKE AFFECT.' } },
{ usage: 'stat random', description: { text: 'Displays a random stat on the scoreboard.' } }
]
});
extension.addCommand(statCommand);
Expand All @@ -33,7 +35,6 @@ function statCommandCallback(sender, args) {
let { argOne, argTwo, argThree } = args;
if (argOne === null)
return statCommand.sendUsage(sender);

if (argOne === 'hide') {
Carousel.stop();
Display.hide();
Expand Down Expand Up @@ -67,14 +68,33 @@ function statCommandCallback(sender, args) {
} else {
sender.sendMessage('§cFailed to set the statistics display.');
}
} else if (argOne == 'clear') {
eventManager.clear();
sender.sendMessage('§cMINECRAFT SERVER RESTART REQUIRED.');
} else if (argOne == 'random') {
const idList = eventManager.getEventIDs();
if (idList != null && idList.length > 0) {
// best rand option available afaik for javascript api
let eventIndex = Math.floor(Math.random() * idList.length);
const eventKey = idList[eventIndex];
const success = Display.set(eventKey);
if (success) {
Carousel.stop();
sender.sendMessage(`§7Set the statistics display to '${eventKey}'.`);
} else {
sender.sendMessage('§cFailed to set the statistics display.');
}
} else {
sender.sendMessage('§cFailed to set the statistics display.');
}
} else {
sender.sendMessage(`§cStatistic '${argOne}' not found.`);
}
}

function formatStatNames() {
const eventList = eventManager.getEventIDs();
const baseEvents = []
const baseEvents = [];
const subEvents = {};
const heirarchySeparator = ':';

Expand Down