Skip to content

Commit

Permalink
somerhtinfv
Browse files Browse the repository at this point in the history
  • Loading branch information
YetNT committed Dec 31, 2023
1 parent 8faac5a commit b9a205a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 17 deletions.
2 changes: 1 addition & 1 deletion BP/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"version": [
0,
2,
8
9
]
},
"modules": [
Expand Down
8 changes: 4 additions & 4 deletions BP/scripts/commands/brokenBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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, {
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions BP/scripts/commands/death.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions BP/scripts/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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
59 changes: 59 additions & 0 deletions BP/scripts/commands/placeBlock.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
13 changes: 6 additions & 7 deletions BP/scripts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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":
Expand Down
21 changes: 21 additions & 0 deletions BP/scripts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
}
}

0 comments on commit b9a205a

Please sign in to comment.