├── .gitignore
├── LICENSE.txt
├── README.md
├── pom.xml
└── src
└── main
└── java
└── me
└── hfox
└── spigboard
├── Spigboard.java
└── SpigboardEntry.java
/.gitignore:
--------------------------------------------------------------------------------
1 | ### Java ###
2 | *.class
3 |
4 | # Package Files #
5 | *.jar
6 | *.war
7 | *.ear
8 |
9 | # virtual machine crash logs
10 | hs_err_pid*
11 |
12 | ### Eclipse ###
13 | *.pydevproject
14 | .metadata
15 | .gradle
16 | bin/
17 | tmp/
18 | *.tmp
19 | *.bak
20 | *.swp
21 | *~.nib
22 | local.properties
23 | .settings/
24 | .loadpath
25 | .classpath
26 | .project
27 |
28 | # External tool builders
29 | .externalToolBuilders/
30 |
31 | # Locally stored "Eclipse launch configurations"
32 | *.launch
33 |
34 | # CDT-specific
35 | .cproject
36 |
37 | # PDT-specific
38 | .buildpath
39 |
40 | # sbteclipse plugin
41 | .target
42 |
43 | # TeXlipse plugin
44 | .texlipse
45 |
46 | ### Maven ###
47 | target/
48 | pom.xml.tag
49 | pom.xml.releaseBackup
50 | pom.xml.versionsBackup
51 | pom.xml.next
52 | release.properties
53 |
54 | ## Directory-based project format
55 | .idea/
56 |
57 | ## File-based project format
58 | *.ipr
59 | *.iws
60 | *.iml
61 |
62 | ## Additional for IntelliJ
63 | ./out/
64 |
65 | # generated by mpeltonen/sbt-idea plugin
66 | .idea_modules/
67 |
68 | # generated by JIRA plugin
69 | atlassian-ide-plugin.xml
70 |
71 | # generated by Crashlytics plugin (for Android Studio and Intellij)
72 | com_crashlytics_export_strings.xml
73 |
74 | ### NetBeans ###
75 | ./nbproject/private/
76 | ./build/
77 | ./nbbuild/
78 | ./dist/
79 | ./nbdist/
80 | ./nbactions.xml
81 | ./nb-configuration.xml
82 |
83 | # Editor preferences for a class etc
84 | *.ctxt
85 |
86 | # Duplicate of project file
87 | bluej.pkh
88 |
89 | ### Processing ###
90 | .DS_Store
91 | applet
92 | application.linux32
93 | application.linux64
94 | application.windows32
95 | application.windows64
96 | application.macosx
97 |
98 | ### Some others ###
99 |
100 | MANIFEST.MF
101 | dependency-reduced-pom.xml
102 |
103 | Thumbs.db
104 | src/main/resources/rebel.xml
105 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright 2015 - 2018 Harry Fox
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Spigboard
2 | =========
3 |
4 | A simple scoreboard manager library for Spigot plugins.
5 | Credit to RainoBoy97 is mentioned in the location where his code is used :)
6 |
7 | ## Using Spigboard in your project
8 | ### Maven
9 |
10 | **Repository**
11 | ```xml
12 |
13 | hfox-public
14 | https://nexus.hfox.uk/repository/maven-public/
15 |
16 | ```
17 |
18 | **Dependency**
19 | ```xml
20 |
21 | me.hfox
22 | spigboard
23 | 1.1.0
24 |
25 | ```
26 |
27 | You will have to shade this dependency if you don't expect it to be compiled in any other plugins
28 |
29 | ### Creating a Spigboard
30 |
31 | ```java
32 | Spigboard board = new Spigboard("my scoreboard");
33 | ```
34 |
35 | ### Add players to a Spigboard
36 |
37 | ```java
38 | board.add(player);
39 | ```
40 |
41 | ### Adding scores to a Spigboard
42 |
43 | If you would like to overwrite existing scores with the supplied name
44 | ```java
45 | board.add("kills", 0, true);
46 | ```
47 |
48 | Create a score with the supplied name, if the that name already exists, attempt to create another score with the same name
49 | "key" is the key used to reference this score later on if you wish to update it.
50 | You can also use an Enum, this will use the `Enum#name()` method as the key
51 | ```java
52 | board.add("key", "kills", 0);
53 | ```
54 |
55 | Any `Spigboard#add()` methods that create Scores will return a score, you can store this to update later
56 |
57 | ### Updating existing scores
58 |
59 | If you have the Score you want to update
60 | ```java
61 | score.update("deaths");
62 | ```
63 |
64 | If you don't have the score you want to update, you should check if the score is null or not, incase the key is not present.
65 | ```java
66 | Spigboard score = spigboard.getEntry("key");
67 | if (score != null) {
68 | score.update("deaths");
69 | }
70 | ```
71 |
72 | In some cases, you may not know the key for a score. If the score is unique and less than or equal to 48 characters, you can use `Spigboard#getEntryByName(name)`
73 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | me.hfox
8 | spigboard
9 | 1.1.0
10 |
11 |
12 |
13 | hfox-releases
14 | https://nexus.hfox.uk/repository/maven-releases/
15 |
16 |
17 | hfox-snapshots
18 | https://nexus.hfox.uk/repository/maven-snapshots/
19 |
20 |
21 |
22 |
23 |
24 | spigot-repo
25 | https://hub.spigotmc.org/nexus/content/repositories/snapshots/
26 |
27 |
28 |
29 |
30 |
31 | org.spigotmc
32 | spigot-api
33 | 1.12.2-R0.1-SNAPSHOT
34 | provided
35 |
36 |
37 |
38 |
39 |
40 |
41 | org.apache.maven.plugins
42 | maven-compiler-plugin
43 | 3.1
44 |
45 | true
46 | 1.7
47 | 1.7
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/src/main/java/me/hfox/spigboard/Spigboard.java:
--------------------------------------------------------------------------------
1 | package me.hfox.spigboard;
2 |
3 | import com.google.common.collect.BiMap;
4 | import com.google.common.collect.HashBiMap;
5 | import org.bukkit.Bukkit;
6 | import org.bukkit.ChatColor;
7 | import org.bukkit.entity.Player;
8 | import org.bukkit.scoreboard.DisplaySlot;
9 | import org.bukkit.scoreboard.Objective;
10 | import org.bukkit.scoreboard.Scoreboard;
11 |
12 | import java.util.HashMap;
13 | import java.util.Map;
14 | import java.util.Map.Entry;
15 | import java.util.Set;
16 |
17 | public class Spigboard {
18 |
19 | private Scoreboard scoreboard;
20 | private Objective objective;
21 | private BiMap entries;
22 |
23 | private int teamId;
24 |
25 | public Spigboard(String title) {
26 | this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
27 | this.objective = scoreboard.registerNewObjective("spigobjective", "dummy");
28 | this.objective.setDisplaySlot(DisplaySlot.SIDEBAR);
29 | setTitle(title);
30 |
31 | this.entries = HashBiMap.create();
32 | this.teamId = 1;
33 | }
34 |
35 | public Scoreboard getScoreboard() {
36 | return scoreboard;
37 | }
38 |
39 | public Objective getObjective() {
40 | return objective;
41 | }
42 |
43 | public void setTitle(String title) {
44 | objective.setDisplayName(title);
45 | }
46 |
47 | public BiMap getEntries() {
48 | return HashBiMap.create(entries);
49 | }
50 |
51 | public SpigboardEntry getEntry(String key) {
52 | return entries.get(key);
53 | }
54 |
55 | public SpigboardEntry add(String name, int value) {
56 | return add((String) null, name, value, true);
57 | }
58 |
59 | public SpigboardEntry add(Enum key, String name, int value) {
60 | return add(key.name(), name, value);
61 | }
62 |
63 | public SpigboardEntry add(String key, String name, int value) {
64 | return add(key, name, value, false);
65 | }
66 |
67 | public SpigboardEntry add(Enum key, String name, int value, boolean overwrite) {
68 | return add(key.name(), name, value, overwrite);
69 | }
70 |
71 | public SpigboardEntry add(String key, String name, int value, boolean overwrite) {
72 | if (key == null && !contains(name)) {
73 | throw new IllegalArgumentException("Entry could not be found with the supplied name and no key was supplied");
74 | }
75 |
76 | if (overwrite && contains(name)) {
77 | SpigboardEntry entry = getEntryByName(name);
78 | if (key != null && entries.get(key) != entry) {
79 | throw new IllegalArgumentException("Supplied key references a different score than the one to be overwritten");
80 | }
81 |
82 | entry.setValue(value);
83 | return entry;
84 | }
85 |
86 | if (entries.get(key) != null) {
87 | throw new IllegalArgumentException("Score already exists with that key");
88 | }
89 |
90 | int count = 0;
91 | String origName = name;
92 | if (!overwrite) {
93 | Map created = create(name);
94 | for (Entry entry : created.entrySet()) {
95 | count = entry.getKey();
96 | name = entry.getValue();
97 | }
98 | }
99 |
100 | SpigboardEntry entry = new SpigboardEntry(key, this, value, origName, count);
101 | entry.update(name);
102 | entries.put(key, entry);
103 | return entry;
104 | }
105 |
106 | public void remove(String key) {
107 | remove(getEntry(key));
108 | }
109 |
110 | public void remove(SpigboardEntry entry) {
111 | if (entry.getSpigboard() != this) {
112 | throw new IllegalArgumentException("Supplied entry does not belong to this Spigboard");
113 | }
114 |
115 | String key = entries.inverse().get(entry);
116 | if (key != null) {
117 | entries.remove(key);
118 | }
119 |
120 | entry.remove();
121 | }
122 |
123 | private Map create(String name) {
124 | // Bukkit.getLogger().info("Name: '" + name + "' (" + name.length() + ") contains? " + contains(name));
125 | int count = 0;
126 | while (contains(name)) {
127 | name = ChatColor.RESET + name;
128 | count++;
129 | // Bukkit.getLogger().info("Name: '" + name + "' (" + name.length() + ") contains? " + contains(name) + " (" + ++count + ")");
130 | }
131 |
132 | if (name.length() > 48) {
133 | name = name.substring(0, 47);
134 | // Bukkit.getLogger().info("Name: '" + name + "' (" + name.length() + ") contains? " + contains(name) + " (trim)");
135 | }
136 |
137 | if (contains(name)) {
138 | throw new IllegalArgumentException("Could not find a suitable replacement name for '" + name + "'");
139 | }
140 |
141 | Map created = new HashMap<>();
142 | created.put(count, name);
143 | return created;
144 | }
145 |
146 | public int getTeamId() {
147 | return teamId++;
148 | }
149 |
150 | public SpigboardEntry getEntryByName(String name) {
151 | for (SpigboardEntry entry : entries.values()) {
152 | if (entry.getName().equals(name)) {
153 | return entry;
154 | }
155 | }
156 |
157 | return null;
158 | }
159 |
160 | public boolean contains(String name) {
161 | for (SpigboardEntry entry : entries.values()) {
162 | if (entry.getName().equals(name)) {
163 | return true;
164 | }
165 | }
166 |
167 | return false;
168 | }
169 |
170 | public void add(Player player) {
171 | player.setScoreboard(scoreboard);
172 | }
173 |
174 | }
175 |
--------------------------------------------------------------------------------
/src/main/java/me/hfox/spigboard/SpigboardEntry.java:
--------------------------------------------------------------------------------
1 | package me.hfox.spigboard;
2 |
3 | import com.google.common.base.Splitter;
4 | import org.bukkit.Bukkit;
5 | import org.bukkit.ChatColor;
6 | import org.bukkit.scoreboard.Score;
7 | import org.bukkit.scoreboard.Team;
8 |
9 | import java.util.Iterator;
10 |
11 | public class SpigboardEntry {
12 |
13 | private String key;
14 | private Spigboard spigboard;
15 | private String name;
16 | private Team team;
17 | private Score score;
18 | private int value;
19 |
20 | private String origName;
21 | private int count;
22 |
23 | public SpigboardEntry(String key, Spigboard spigboard, int value) {
24 | this.key = key;
25 | this.spigboard = spigboard;
26 | this.value = value;
27 | this.count = 0;
28 | }
29 |
30 | public SpigboardEntry(String key, Spigboard spigboard, int value, String origName, int count) {
31 | this.key = key;
32 | this.spigboard = spigboard;
33 | this.value = value;
34 | this.origName = origName;
35 | this.count = count;
36 | }
37 |
38 | public String getKey() {
39 | return key;
40 | }
41 |
42 | public Spigboard getSpigboard() {
43 | return spigboard;
44 | }
45 |
46 | public String getName() {
47 | return name;
48 | }
49 |
50 | public Team getTeam() {
51 | return team;
52 | }
53 |
54 | public Score getScore() {
55 | return score;
56 | }
57 |
58 | public int getValue() {
59 | return score != null ? (value = score.getScore()) : value;
60 | }
61 |
62 | public void setValue(int value) {
63 | if (!score.isScoreSet()) {
64 | score.setScore(-1);
65 | }
66 |
67 | score.setScore(value);
68 | }
69 |
70 | public void update(String newName) {
71 | int value = getValue();
72 | if (origName != null && newName.equals(origName)) {
73 | // String oldName = newName;
74 | for (int i = 0; i < count; i++) {
75 | newName = ChatColor.RESET + newName;
76 | }
77 |
78 | // Bukkit.getLogger().info("Changed '" + oldName + "' (" + oldName.length() + ") into '" + newName + "' (" + newName.length() + ")");
79 | } else if (newName.equals(name)) {
80 | // Bukkit.getLogger().info("Not updating '" + newName + "' because it matches previous name");
81 | return;
82 | }
83 |
84 | create(newName);
85 | setValue(value);
86 | }
87 |
88 | void remove() {
89 | if (score != null) {
90 | score.getScoreboard().resetScores(score.getEntry());
91 | }
92 |
93 | if (team != null) {
94 | team.unregister();
95 | }
96 | }
97 |
98 | private void create(String name) {
99 | this.name = name;
100 | remove();
101 |
102 | if (name.length() <= 16) {
103 | int value = getValue();
104 | score = spigboard.getObjective().getScore(name);
105 | score.setScore(value);
106 | return;
107 | }
108 |
109 | // Credit to RainoBoy97 for this section here.
110 | team = spigboard.getScoreboard().registerNewTeam("spigboard-" + spigboard.getTeamId());
111 | Iterator iterator = Splitter.fixedLength(16).split(name).iterator();
112 | if (name.length() > 16)
113 | team.setPrefix(iterator.next());
114 | String entry = iterator.next();
115 | score = spigboard.getObjective().getScore(entry);
116 | if (name.length() > 32)
117 | team.setSuffix(iterator.next());
118 |
119 | team.addEntry(entry);
120 | }
121 |
122 | }
123 |
--------------------------------------------------------------------------------