diff --git a/BP/manifest.json b/BP/manifest.json index 6aa4a80..df796bb 100644 --- a/BP/manifest.json +++ b/BP/manifest.json @@ -25,7 +25,7 @@ "version": [ 0, 2, - 8 + 9 ] }, "modules": [ diff --git a/BP/scripts/commands/brokenBlocks.ts b/BP/scripts/commands/brokenBlocks.ts index b07ed61..54ffab6 100644 --- a/BP/scripts/commands/brokenBlocks.ts +++ b/BP/scripts/commands/brokenBlocks.ts @@ -2,8 +2,9 @@ // ?removescoreboard bb import { world, ObjectiveSortOrder, DisplaySlotId, system, Player, PlayerBreakBlockAfterEvent } from '@minecraft/server'; -import { removePlayerOffline } from "../util" +import { removePlayerOffline, msg } from "../util" const objectiveId = "bb" +const nameType = "Blocks broken" let event: (arg: PlayerBreakBlockAfterEvent) => void; /** * @param {string} name @@ -29,7 +30,6 @@ export function add(name: string, display = DisplaySlotId.Sidebar, sortOrder = O if (playerIdentity === undefined) { console.log(`Could not get playerIdentity. Attempting to run command to add player as ${playerCommand.name}`) playerCommand.runCommand(`scoreboard players add ${player.name} ${objectiveId} 1`) - player.sendMessage("Could not get playerIdentity. Has this player been added to the scoreboard?") return; } world.scoreboard.setObjectiveAtDisplaySlot(display, { @@ -42,14 +42,14 @@ export function add(name: string, display = DisplaySlotId.Sidebar, sortOrder = O objective.setScore(playerIdentity, playerScore + 1); }) }) - playerCommand.sendMessage("§3Blocks broken scoreboard has been added. §e§oPlayers who joined after the scoreboard was added will need to be added manually") + playerCommand.sendMessage(msg.add(nameType)) } export function remove(player: Player) { system.run(() => { world.afterEvents.playerBreakBlock.unsubscribe(event) - player.sendMessage(`Blocks broken scoreboard was removed`) + player.sendMessage(msg.remove(nameType)) let objective = world.scoreboard.getObjective(objectiveId); if (!objective) return diff --git a/BP/scripts/commands/death.ts b/BP/scripts/commands/death.ts index 68d7f37..f489f5e 100644 --- a/BP/scripts/commands/death.ts +++ b/BP/scripts/commands/death.ts @@ -2,8 +2,9 @@ // ?removescoreboard deaths import { world, ObjectiveSortOrder, DisplaySlotId, system, Player, EntityDieAfterEvent } from '@minecraft/server'; -import { removePlayerOffline } from "../util" +import { removePlayerOffline, msg } from "../util" const objectiveId = "deaths" +const nameType = "Deaths" let event: (arg: EntityDieAfterEvent) => void; export function add(name: string, display = DisplaySlotId.Sidebar, sortOrder = ObjectiveSortOrder.Descending, playerCommand: Player) { @@ -48,13 +49,13 @@ export function add(name: string, display = DisplaySlotId.Sidebar, sortOrder = O }) }) - playerCommand.sendMessage("§3Deaths scoreboard has been added. §e§oPlayers who joined after the scoreboard was added will need to be added manually") + playerCommand.sendMessage(msg.add(nameType)) } export function remove(player: Player) { system.run(() => { world.afterEvents.entityDie.unsubscribe(event) - player.sendMessage(`Deaths scoreboard was removed`) + player.sendMessage(msg.remove(nameType)) let objective = world.scoreboard.getObjective(objectiveId); if (!objective) return diff --git a/BP/scripts/commands/index.ts b/BP/scripts/commands/index.ts index 6042e22..408a406 100644 --- a/BP/scripts/commands/index.ts +++ b/BP/scripts/commands/index.ts @@ -1,9 +1,9 @@ import * as deaths from "./death" import * as bb from "./brokenBlocks" - +import * as pb from "./placeBlock" const commands = { - deaths, bb + deaths, bb, pb } export default commands \ No newline at end of file diff --git a/BP/scripts/commands/placeBlock.ts b/BP/scripts/commands/placeBlock.ts new file mode 100644 index 0000000..f943d8e --- /dev/null +++ b/BP/scripts/commands/placeBlock.ts @@ -0,0 +1,59 @@ +// ?newscoreboard bb (NAME) (DisplaySlotId) (SortOrder) +// ?removescoreboard bb + +import { world, ObjectiveSortOrder, DisplaySlotId, system, Player, PlayerBreakBlockAfterEvent } from '@minecraft/server'; +import { removePlayerOffline, msg } from "../util" +const objectiveId = "pb" +const nameType = "Blocks placed" +let event: (arg: PlayerBreakBlockAfterEvent) => void; +/** + * @param {string} name + */ +export function add(name: string, display = DisplaySlotId.Sidebar, sortOrder = ObjectiveSortOrder.Descending, playerCommand: Player) { + system.run(() => { + event = world.afterEvents.playerPlaceBlock.subscribe((event) => { + const player = event.player + + const scoreboardObjectiveId = objectiveId; + const scoreboardObjectiveDisplayName = name; + + let objective = world.scoreboard.getObjective(scoreboardObjectiveId); + + if (!objective) { + objective = world.scoreboard.addObjective(scoreboardObjectiveId, scoreboardObjectiveDisplayName); + } + + removePlayerOffline(objective) + + let playerIdentity = player.scoreboardIdentity; + + if (playerIdentity === undefined) { + console.log(`Could not get playerIdentity. Attempting to run command to add player as ${playerCommand.name}`) + playerCommand.runCommand(`scoreboard players add ${player.name} ${objectiveId} 1`) + return; + } + world.scoreboard.setObjectiveAtDisplaySlot(display, { + objective: objective, + sortOrder: sortOrder, + }); + + const playerScore = objective.getScore(playerIdentity) ?? 0; + + objective.setScore(playerIdentity, playerScore + 1); + }) + }) + playerCommand.sendMessage(msg.add(nameType)) +} + +export function remove(player: Player) { + system.run(() => { + world.afterEvents.playerPlaceBlock.unsubscribe(event) + + player.sendMessage(msg.remove(nameType)) + let objective = world.scoreboard.getObjective(objectiveId); + + if (!objective) return + + world.scoreboard.removeObjective(objective) + }) +} \ No newline at end of file diff --git a/BP/scripts/main.ts b/BP/scripts/main.ts index 0e8e45e..209c2f6 100644 --- a/BP/scripts/main.ts +++ b/BP/scripts/main.ts @@ -4,25 +4,24 @@ const prefix = "?" const types = { deaths: "deaths", - brokenBlocks: "bb" + brokenBlocks: "bb", + placeBlock: "pb" } import { input } from "./util" import { Error } from "./error" -const sortOrderValues = Object.values(ObjectiveSortOrder) -const displaySlotIds = Object.values(DisplaySlotId) // ?newscoreboard (TYPE) [Name] [DisplaySlotId] [SortOrder] // ?removescoreboard (TYPE) world.beforeEvents.chatSend.subscribe((eventData) => { const player = eventData.sender; - if (!player.isOp()) { new Error(player, `You {${player.name}} are not an OP. {E3}`); return } + if (!player.isOp() && eventData.message[0] === prefix) { new Error(player, `You {${player.name}} are not an OP. {E3}`); return } if (eventData.message[0] !== prefix) return; const command = eventData.message.slice(1) // removes ? //@ts-ignore const args: [ - ("newscoreboard" | "new" | "add"), ("deaths" | "bb"), string, (DisplaySlotId | string), string /* <-- this is objectiveSortOrder.*/ - ] | [("remove" | "removescoreboard"), ("deaths" | "bb")] = command.split(" ") + ("newscoreboard" | "new" | "add"), ("deaths" | "bb" | "pb"), string, (DisplaySlotId | string), string /* <-- this is objectiveSortOrder.*/ + ] | [("remove" | "removescoreboard"), ("deaths" | "bb" | "pb")] = command.split(" ") if (args.length < 2) { new Error(player, "Too little arguments were given. {E1}"); return } // not enough args. Handle error. @@ -41,7 +40,7 @@ world.beforeEvents.chatSend.subscribe((eventData) => { if (!sortOrder) { new Error(player, "Invalid Sort Order was given. {E2}"); return } // sortOrder is not valid. Handle error if (!displaySlotId) { new Error(player, "Invalid Display Slot ID was given. {E2}"); return } // displaySlotId is not vaslid. Handle error - commands[args[1]].add(name, displaySlotId, sortOrder, player) + commands[args[1]].add(name.replaceAll("?/", " "), displaySlotId, sortOrder, player) break; case "remove": case "removescoreboard": diff --git a/BP/scripts/util.ts b/BP/scripts/util.ts index 0648703..e3b59d0 100644 --- a/BP/scripts/util.ts +++ b/BP/scripts/util.ts @@ -60,4 +60,25 @@ export const input = { return ObjectiveSortOrder.Descending } } +} + +/** + * Object for messages to show to player. + */ +export const msg = { + /** + * When succesfully adding a scoreboard + */ + add(name: string): string { + const colour1 = "§3" + const colour2 = "§e§o" + return `${colour1}${name} scoreboard has been added. ${colour2} Players who join after, may need to wait a bit before their scores show.` + }, + /** + * When succesfully removing a scoreboard + */ + remove(name: string): string { + const colour1 = "§5" + return `${colour1}${name} scoreboard was removed.` + } } \ No newline at end of file