GithubHelp home page GithubHelp logo

ncklwse / sandstone-scoreboard Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 140 KB

Create custom complex scoreboards with ease!

Home Page: https://github.com/ncklwse/sandstone-scoreboard

License: MIT License

TypeScript 100.00%
sandstone sandstone-library minecraft-datapack

sandstone-scoreboard's Introduction

sandstone-scoreboard

Create complex custom scoreboards with ease!

Installing

Create a new Sandstone project:

npx sandstone-cli create <my-project>

Install sandstone-scoreboard using NPM:

npm install sandstone-scoreboard

Usage

This documentation assumes you're familiar with the sandstone documentation. If you find an issue with this documentation, please file an issue on Github!

Create a new Scoreboard by specifying the initial display name:

import { Scoreboard } from 'sandstone-scoreboard';

// Display names support Minecraft's JSON text formatting
const myScoreboard = new Scoreboard({
    text: 'My Custom Scoreboard!',
    color: 'gold',
    bold: true
});

Scoreboards aren't tied to any specific MCFunction, so you can export it and use it across multiple files.

import { Scoreboard } from 'sandstone-scoreboard';

export const myScoreboard = new Scoreboard({
    text: 'My Custom Scoreboard!',
    color: 'gold',
    bold: true
});

To add a line to your scoreboard, just use the .addLine() method on the scoreboard. It returns an instance which can be used to interact with the line.

import { Scoreboard } from 'sandstone-scoreboard';

const myScoreboard = new Scoreboard({
    text: 'My Custom Scoreboard!',
    color: 'gold',
    bold: true
});

// Adding the line to the scoreboard
myScoreboard.addLine({
    text: 'Hello world!',
    color: 'white'
});

Example 1

Lines must contain at least one non-whitespace character, because of how this library was created. This library works by adding this non-whitespace character to the scoreboard as a player, with all other text being used as prefixes/suffixes on a team to which the scoreboard player is assigned.

// Adds a player called "Hello", assigned to a team with the suffix " world!"
myScoreboard.addLine({
    text: 'a',
    color: 'white'
});

// Throws error
myScoreboard.addLine({
    text: ' ',
    color: 'white'
});

Additionally, if you have too many similar lines of text added to the same scoreboard from which different player names can't be generated, the code won't compile because the same player can't be added to a scoreboard twice.

// Adds a player called "test"
myScoreboard.addLine({
    text: 'test',
    color: 'white'
});

// Adds a player called "tes"
myScoreboard.addLine({
    text: 'test',
    color: 'white'
});

// Adds a player called "a"
myScoreboard.addLine({
    text: 'a',
    color: 'white'
});

// Throws error
myScoreboard.addLine({
    text: 'a',
    color: 'white'
});

Scoreboards and lines both support arrays as of text, meaning you can combine multiple different formatting options:

import { Scoreboard } from 'sandstone-scoreboard';

const myScoreboard = new Scoreboard([{
    text: 'My ',
    color: 'white',
    bold: false
}, {
    text: 'Custom ',
    color: 'gold',
    bold: true
}, {
    text: 'Scoreboard!',
    color: 'white',
    bold: false
}]);

myScoreboard.addLine([{
    text: 'Colors ',
    color: 'red'
}, {
    text: 'are ',
    color: 'gold'
}, {
    text: 'really ',
    color: 'yellow'
}, {
    text: 'fun ',
    color: 'green'
}, {
    text: 'to ',
    color: 'blue'
}, {
    text: 'use!',
    color: 'dark_purple'
}]);

Example 2

Like scoreboards, lines aren't tied to any specific MCFunction, meaning you can export them and use them across multiple files.

import { MCFunction } from 'sandstone';
import { myLine } from './MyOtherFile.ts';

MCFunction('test', () => {
    myLine.setText('Set text from a different function in another file!');
});

When adding a line, you can also specify a priority. The higher the priority, the higher the line will appear on the scoreboard. The default priority on all lines when they're added is 0.

myScoreboard.addLine({
    text: 'Added first'
});

myScoreboard.addLine({
    text: 'Added second'
});

myScoreboard.addLine({
    text: 'Added third, but with priority'
}, 2);

Example 3

To edit a line on a scoreboard, use the .setText() method on the line:

myLine.setText({
    text: 'Wooo!',
    color: 'green'
});

To show or hide your scoreboard, it's as simple as using:

// Show the scoreboard
myScoreboard.show();

// Hide the scoreboard
myScoreboard.hide();

You can also show/hide your scoreboard to a specific display slot:

// Show your scoreboard to teams with a color of red:
myScoreboard.show('sidebar.team.red');

One of the features which I'm most proud of is the ability to animate scoreboard objectives, which can be done using the .animate() method. The sole argument is an array of objects with the display property containing a JSON Text Component, as well as an optional duration property. The duration of each frame is specified in ticks, with the default being 20.

myScoreboard.animate([{
    display: {
        text: 'Hello world!',
        color: 'gold',
        bold: true
    },
    duration: 5
}, {
    display: {
        text: 'Hello world!',
        color: 'white',
        bold: false
    },
    duration: 5
}]);

Example 4

You can also animate lines in a similar way:

myScoreboard.addLine('').animate([{
    display: {
        text: 'Animated'
    },
    duration: 10
}, {
    display: {
        text: 'Line!'
    },
    duration: 10
}])

License

Copyright © 2021 Nickelwise.

Licensed MIT

sandstone-scoreboard's People

Contributors

ncklwse avatar sloanfinger avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

sandstone-scoreboard's Issues

Documentation doesn't represent code

Using "sandstone-scoreboard": "^0.1.3" and when using the addLine() examples from the readme I get the following errors:

Argument of type '{ text: string; color: string; }[]' is not assignable to parameter of type 'Line'.
  Type '{ text: string; color: string; }[]' is missing the following properties from type 'Line': index, updateListeners, rawData, update, and 4 more.

I have to make a variable and fill it with the requisite info.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.