Skip to content

Commit b87a49d

Browse files
Pablete1234twizmwazin
authored andcommitted
Add scoreboards
1 parent 941fd4e commit b87a49d

30 files changed

+1691
-24
lines changed

src/main/java/in/twizmwaz/cardinal/module/objective/Objective.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public abstract class Objective {
4949
private final boolean required;
5050
protected final boolean show;
5151

52+
/**
53+
* Gets the objective display name.
54+
* @return The display name.
55+
*/
5256
public abstract UnlocalizedComponent getComponent();
5357

5458
/**
@@ -59,9 +63,9 @@ public abstract class Objective {
5963
public static List<Objective> getObjectives(@NonNull Match match) {
6064
List<Objective> objectives = Lists.newArrayList();
6165

62-
Cardinal.getModule(CoreModule.class).getCores(match).forEach(objectives::add);
63-
Cardinal.getModule(DestroyableModule.class).getDestroyables(match).forEach(objectives::add);
64-
Cardinal.getModule(WoolModule.class).getWools(match).forEach(objectives::add);
66+
objectives.addAll(Cardinal.getModule(WoolModule.class).getWools(match));
67+
objectives.addAll(Cardinal.getModule(CoreModule.class).getCores(match));
68+
objectives.addAll(Cardinal.getModule(DestroyableModule.class).getDestroyables(match));
6569

6670
return objectives;
6771
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2016, Kevin Phoenix
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
package in.twizmwaz.cardinal.module.objective;
27+
28+
import ee.ellytr.chat.component.formattable.UnlocalizedComponent;
29+
import in.twizmwaz.cardinal.module.team.Team;
30+
31+
public interface OwnedObjective {
32+
33+
Team getOwner();
34+
35+
String getPrefix(Team viewer, Team attacker);
36+
37+
UnlocalizedComponent getComponent();
38+
39+
}

src/main/java/in/twizmwaz/cardinal/module/objective/core/Core.java

+47-6
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,27 @@
3939
import in.twizmwaz.cardinal.module.filter.type.TeamFilter;
4040
import in.twizmwaz.cardinal.module.filter.type.modifiers.TransformFilter;
4141
import in.twizmwaz.cardinal.module.objective.Objective;
42+
import in.twizmwaz.cardinal.module.objective.OwnedObjective;
4243
import in.twizmwaz.cardinal.module.objective.ProximityMetric;
4344
import in.twizmwaz.cardinal.module.region.Region;
4445
import in.twizmwaz.cardinal.module.region.type.FiniteBlockRegion;
46+
import in.twizmwaz.cardinal.module.scoreboard.displayables.EntryHolder;
47+
import in.twizmwaz.cardinal.module.scoreboard.displayables.EntryUpdater;
4548
import in.twizmwaz.cardinal.module.team.Team;
49+
import in.twizmwaz.cardinal.util.Characters;
4650
import in.twizmwaz.cardinal.util.MaterialPattern;
4751
import lombok.Data;
4852
import lombok.EqualsAndHashCode;
49-
import lombok.Setter;
53+
import net.md_5.bungee.api.ChatColor;
5054
import org.bukkit.Material;
5155
import org.bukkit.entity.Player;
52-
import org.bukkit.event.Listener;
5356

5457
import java.util.AbstractMap;
5558
import java.util.List;
5659

5760
@Data
5861
@EqualsAndHashCode(callSuper = true)
59-
public class Core extends Objective implements Listener {
62+
public class Core extends Objective implements OwnedObjective, EntryUpdater {
6063

6164
private static final MaterialPattern lavaPattern = new MaterialPattern(
6265
new AbstractMap.SimpleEntry<>(Material.LAVA, MaterialPattern.ANY_DATA_VALUE),
@@ -71,10 +74,11 @@ public class Core extends Objective implements Listener {
7174
private final ProximityMetric proximityMetric;
7275
private final boolean proximityHorizontal;
7376

77+
private final EntryHolder entryHolder = new EntryHolder();
78+
7479
private final List<Player> touchedPlayers = Lists.newArrayList();
80+
private final List<Team> touchedTeams = Lists.newArrayList();
7581

76-
@Setter
77-
private boolean touched;
7882
private boolean complete;
7983

8084
/**
@@ -85,7 +89,7 @@ public class Core extends Objective implements Listener {
8589
* @param region The region that contains this core.
8690
* @param leak The distance required for the lava to be from the core in order to be leaked.
8791
* @param material The material that the core is made out of.
88-
* @param owner The owner that owns this core.
92+
* @param owner The team that owns this core.
8993
* @param modeChanges Determines if this core follows mode changes.
9094
* @param show Determines if this core shows on the scoreboard.
9195
* @param proximityMetric The proximity metric for proximity tracking of this core.
@@ -118,6 +122,43 @@ public Core(Match match, String id, String name, boolean required, Region region
118122
true);
119123
}
120124

125+
public boolean isTouched(Team team) {
126+
return touchedTeams.contains(team);
127+
}
128+
129+
/**
130+
* Sets the core as touched for a certain team. Scoreboard entries will be updated.
131+
* @param team The team that touched the core.
132+
*/
133+
public void setTouched(Team team) {
134+
if (!isTouched(team)) {
135+
touchedTeams.add(team);
136+
entryHolder.updateEntries();
137+
}
138+
}
139+
140+
public void setComplete(boolean complete) {
141+
this.complete = complete;
142+
entryHolder.updateEntries();
143+
}
144+
145+
/**
146+
* Gets the monument prefix for a given viewer team, for a specific attacker
147+
* @param viewer The viewer team, null for observers.
148+
* @param attacker The team attacking the objective. Used to see if the team has a touch or not.
149+
* @return Color and wool state character. Always 3 characters.
150+
*/
151+
@Override
152+
public String getPrefix(Team viewer, Team attacker) {
153+
if (isComplete()) {
154+
return "" + ChatColor.GREEN + Characters.CORE_COMPLETED;
155+
} else if (isTouched(attacker) && (viewer == null || viewer.equals(attacker))) {
156+
return "" + ChatColor.YELLOW + Characters.CORE_TOUCHED;
157+
} else {
158+
return "" + ChatColor.RED + Characters.CORE_INCOMPLETE;
159+
}
160+
}
161+
121162
@Override
122163
public UnlocalizedComponent getComponent() {
123164
return new UnlocalizedComponent(name);

src/main/java/in/twizmwaz/cardinal/module/objective/core/CoreModule.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.bukkit.entity.Player;
5959
import org.bukkit.event.EventHandler;
6060
import org.bukkit.event.EventPriority;
61-
import org.bukkit.event.HandlerList;
6261
import org.bukkit.event.block.BlockBreakEvent;
6362
import org.bukkit.event.block.BlockFromToEvent;
6463
import org.bukkit.event.entity.PlayerDeathEvent;
@@ -191,9 +190,7 @@ public boolean loadMatch(Match match) {
191190

192191
@Override
193192
public void clearMatch(Match match) {
194-
List<Core> cores = this.cores.get(match);
195-
cores.forEach(HandlerList::unregisterAll);
196-
cores.clear();
193+
this.cores.get(match).clear();
197194
this.cores.remove(match);
198195
}
199196

@@ -235,7 +232,7 @@ public void onBlockBreak(BlockBreakEvent event) {
235232
Block block = event.getBlock();
236233
cores.get(match).forEach(core -> {
237234
if (core.getRegion().contains(block.getLocation())) {
238-
core.setTouched(true);
235+
core.setTouched(team);
239236
if (core.isShow() && !core.getTouchedPlayers().contains(player)) {
240237
core.getTouchedPlayers().add(player);
241238
Channels.getTeamChannel(match, team).sendPrefixedMessage(

src/main/java/in/twizmwaz/cardinal/module/objective/destroyable/Destroyable.java

+58-1
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@
4646
import in.twizmwaz.cardinal.module.filter.type.modifiers.AllFilter;
4747
import in.twizmwaz.cardinal.module.filter.type.modifiers.TransformFilter;
4848
import in.twizmwaz.cardinal.module.objective.Objective;
49+
import in.twizmwaz.cardinal.module.objective.OwnedObjective;
4950
import in.twizmwaz.cardinal.module.objective.ProximityMetric;
5051
import in.twizmwaz.cardinal.module.region.Region;
5152
import in.twizmwaz.cardinal.module.region.type.FiniteBlockRegion;
53+
import in.twizmwaz.cardinal.module.scoreboard.displayables.EntryHolder;
54+
import in.twizmwaz.cardinal.module.scoreboard.displayables.EntryUpdater;
5255
import in.twizmwaz.cardinal.module.team.Team;
5356
import in.twizmwaz.cardinal.playercontainer.PlayingPlayerContainer;
5457
import in.twizmwaz.cardinal.util.Channels;
58+
import in.twizmwaz.cardinal.util.Characters;
5559
import in.twizmwaz.cardinal.util.Components;
5660
import in.twizmwaz.cardinal.util.MaterialPattern;
61+
import in.twizmwaz.cardinal.util.Numbers;
5762
import lombok.Getter;
5863
import lombok.NonNull;
5964
import lombok.Setter;
@@ -70,7 +75,7 @@
7075
import java.util.UUID;
7176

7277
@Getter
73-
public class Destroyable extends Objective {
78+
public class Destroyable extends Objective implements OwnedObjective, EntryUpdater {
7479

7580
private final String name;
7681
private final Region region;
@@ -84,7 +89,10 @@ public class Destroyable extends Objective {
8489
private final ProximityMetric proximityMetric;
8590
private final boolean proximityHorizontal;
8691

92+
private final EntryHolder entryHolder = new EntryHolder();
93+
8794
private final List<Player> touchedPlayers = new ArrayList<>();
95+
private final List<Team> touchedTeams = new ArrayList<>();
8896
private final Map<UUID, Integer> playerContributions = new HashMap<>();
8997

9098
@Setter
@@ -141,6 +149,21 @@ public Destroyable(Match match, String id, String name, boolean required, Region
141149
true);
142150
}
143151

152+
public boolean isTouched(Team team) {
153+
return touchedTeams.contains(team);
154+
}
155+
156+
/**
157+
* Sets the destroyable as touched for a certain team. Scoreboard entries will be updated.
158+
* @param team The team that touched the destroyable.
159+
*/
160+
public void setTouched(Team team) {
161+
if (!isTouched(team)) {
162+
touchedTeams.add(team);
163+
entryHolder.updateEntries();
164+
}
165+
}
166+
144167
public boolean isPartOf(@NonNull Block block) {
145168
return region.contains(block.getLocation())
146169
&& materials.contains(block.getType(), (int) block.getState().getMaterialData().getData());
@@ -156,6 +179,7 @@ public void addBrokenPiecesFor(Player player, int contribution) {
156179
PlayingPlayerContainer container = match.getPlayingContainer(player);
157180
if (!isCompleted() && container instanceof Team) {
158181
Team team = (Team) container;
182+
setTouched(team);
159183
if (show && !touchedPlayers.contains(player)) {
160184
touchedPlayers.add(player);
161185
Channels.getTeamChannel(match, team).sendPrefixedMessage(
@@ -187,9 +211,21 @@ public void addBrokenPiecesFor(Player player, int contribution) {
187211
} else {
188212
Bukkit.getPluginManager().callEvent(new ObjectiveTouchEvent(this, player));
189213
}
214+
entryHolder.updateEntries();
190215
}
191216
}
192217

218+
/**
219+
* Gets the completion percentage.
220+
* @return The percentage, always between 0 and 100;
221+
*/
222+
public int getPercent() {
223+
if (isCompleted()) {
224+
return 100;
225+
}
226+
return (int) Numbers.between(Math.floor((double) broken / (total * completion) * 100), 0, 100);
227+
}
228+
193229
private ListComponent getContributionList() {
194230
List<BaseComponent> contributions = new ArrayList<>();
195231
playerContributions.forEach((uuid, amount) -> {
@@ -201,6 +237,27 @@ private ListComponent getContributionList() {
201237
return new ListComponent(contributions);
202238
}
203239

240+
/**
241+
* Gets the monument prefix for a given viewer team, for a specific attacker.
242+
* @param viewer The viewer team, null for observers.
243+
* @param attacker The team attacking the objective. Used to see if the team has a touch or not.
244+
* @return Color and monument state or percentage. Always between 3 and 6 characters (color + "100%").
245+
*/
246+
@Override
247+
public String getPrefix(Team viewer, Team attacker) {
248+
if (isCompleted()) {
249+
return ChatColor.GREEN + getCompletionOrCharacter(viewer, Characters.CORE_COMPLETED);
250+
} else if (isTouched(attacker) && (viewer == null || viewer.equals(attacker))) {
251+
return ChatColor.YELLOW + getCompletionOrCharacter(viewer, Characters.CORE_TOUCHED);
252+
} else {
253+
return ChatColor.RED + getCompletionOrCharacter(viewer, Characters.CORE_INCOMPLETE);
254+
}
255+
}
256+
257+
private String getCompletionOrCharacter(Team viewer, Characters character) {
258+
return viewer == null || isShowProgress() ? getPercent() + "%" : character + "";
259+
}
260+
204261
@Override
205262
public UnlocalizedComponent getComponent() {
206263
return new UnlocalizedComponent(name);

src/main/java/in/twizmwaz/cardinal/module/objective/wool/Wool.java

+33-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
import in.twizmwaz.cardinal.module.objective.Objective;
3232
import in.twizmwaz.cardinal.module.objective.ProximityRule;
3333
import in.twizmwaz.cardinal.module.region.Region;
34+
import in.twizmwaz.cardinal.module.scoreboard.displayables.EntryHolder;
35+
import in.twizmwaz.cardinal.module.scoreboard.displayables.EntryUpdater;
3436
import in.twizmwaz.cardinal.module.team.Team;
37+
import in.twizmwaz.cardinal.util.Characters;
3538
import in.twizmwaz.cardinal.util.Colors;
3639
import in.twizmwaz.cardinal.util.Strings;
3740
import lombok.Data;
@@ -48,7 +51,7 @@
4851

4952
@Data
5053
@EqualsAndHashCode(callSuper = true)
51-
public class Wool extends Objective implements Listener {
54+
public class Wool extends Objective implements Listener, EntryUpdater {
5255

5356
private final Team team;
5457
private final DyeColor color;
@@ -58,6 +61,8 @@ public class Wool extends Objective implements Listener {
5861
private final ProximityRule woolProximityRule;
5962
private final ProximityRule monumentProximityRule;
6063

64+
private final EntryHolder entryHolder = new EntryHolder();
65+
6166
private final List<Player> touchedPlayers = new ArrayList<>();
6267

6368
private boolean touched;
@@ -90,6 +95,23 @@ public Wool(Match match, String id, boolean required, Team team, DyeColor color,
9095
this.monumentProximityRule = monumentProximityRule;
9196
}
9297

98+
/**
99+
* Gets the wool prefix for a given viewer team.
100+
* @param viewer The viewer team, null for observers.
101+
* @return Color and wool state character. Always 3 characters.
102+
*/
103+
public String getPrefix(Team viewer) {
104+
String result = Colors.convertDyeToChatColor(color) + "";
105+
if (isComplete()) {
106+
result += Characters.WOOL_COMPLETED;
107+
} else if (isTouched() && (viewer == null || viewer.equals(team))) {
108+
result += Characters.WOOL_TOUCHED;
109+
} else {
110+
result += Characters.WOOL_INCOMPLETE;
111+
}
112+
return result;
113+
}
114+
93115
@Override
94116
public UnlocalizedComponent getComponent() {
95117
return new UnlocalizedComponentBuilder(
@@ -109,4 +131,14 @@ public void removePlayerTouched(@NonNull Player player) {
109131
touchedPlayers.remove(player);
110132
}
111133

134+
public void setTouched(boolean touched) {
135+
this.touched = touched;
136+
entryHolder.updateEntries();
137+
}
138+
139+
public void setComplete(boolean complete) {
140+
this.complete = complete;
141+
entryHolder.updateEntries();
142+
}
143+
112144
}

0 commit comments

Comments
 (0)