Skip to content

Commit 4596512

Browse files
Pablete1234twizmwazin
authored andcommitted
Fix next, setnext, cycle and recycle
1 parent b87a49d commit 4596512

22 files changed

+470
-125
lines changed

src/main/java/in/twizmwaz/cardinal/Cardinal.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import in.twizmwaz.cardinal.command.CommandCardinal;
3434
import in.twizmwaz.cardinal.command.CommandCycle;
3535
import in.twizmwaz.cardinal.command.CommandJoin;
36-
import in.twizmwaz.cardinal.command.CommandSetNext;
36+
import in.twizmwaz.cardinal.command.CommandNext;
3737
import in.twizmwaz.cardinal.command.CommandStart;
3838
import in.twizmwaz.cardinal.command.provider.LoadedMapProvider;
3939
import in.twizmwaz.cardinal.command.provider.TeamProvider;
@@ -154,7 +154,7 @@ private void registerCommands() {
154154
commandRegistry.addClass(CommandCardinal.class);
155155
commandRegistry.addClass(CommandCycle.class);
156156
commandRegistry.addClass(CommandJoin.class);
157-
commandRegistry.addClass(CommandSetNext.class);
157+
commandRegistry.addClass(CommandNext.class);
158158
commandRegistry.addClass(CommandStart.class);
159159
commandRegistry.register();
160160

src/main/java/in/twizmwaz/cardinal/command/CommandCycle.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
import in.twizmwaz.cardinal.match.MatchThread;
3333
import in.twizmwaz.cardinal.module.countdown.CountdownModule;
3434
import in.twizmwaz.cardinal.module.countdown.CycleCountdown;
35-
import org.bukkit.entity.Player;
35+
import in.twizmwaz.cardinal.module.cycle.CycleModule;
36+
import in.twizmwaz.cardinal.module.repository.LoadedMap;
3637

3738
public class CommandCycle {
3839

@@ -43,13 +44,35 @@ public class CommandCycle {
4344
* @param time The time until cycle, defaults to 30.
4445
*/
4546
@Command(aliases = "cycle", description = "Cycles to the next map")
46-
public static void cycle(CommandContext cmd, @Optional Integer time) {
47+
public static void cycle(CommandContext cmd, @Optional Integer time, @Optional LoadedMap map) {
4748
if (time == null) {
4849
time = 30;
4950
}
5051
time *= 20;
51-
MatchThread matchThread = cmd.getSender() instanceof Player
52-
? Cardinal.getMatchThread((Player) cmd.getSender()) : Cardinal.getInstance().getMatchThreads().get(0);
52+
MatchThread matchThread = Cardinal.getMatchThread(cmd.getSender());
53+
if (map != null) {
54+
Cardinal.getModule(CycleModule.class).getNextCycle(Cardinal.getMatchThread(cmd.getSender())).setMap(map);
55+
}
56+
CycleCountdown countdown = Cardinal.getModule(CountdownModule.class).getCycleCountdown(matchThread);
57+
countdown.setTime(time);
58+
countdown.setCancelled(false);
59+
}
60+
61+
/**
62+
* Cycles to the same map.
63+
*
64+
* @param cmd The context of this command.
65+
* @param time The time until cycle, defaults to 30.
66+
*/
67+
@Command(aliases = "recycle", description = "Cycles to the same map")
68+
public static void recycle(CommandContext cmd, @Optional Integer time) {
69+
if (time == null) {
70+
time = 30;
71+
}
72+
time *= 20;
73+
MatchThread matchThread = Cardinal.getMatchThread(cmd.getSender());
74+
Cardinal.getModule(CycleModule.class)
75+
.getNextCycle(Cardinal.getMatchThread(cmd.getSender())).setMap(matchThread.getCurrentMatch().getMap());
5376
CycleCountdown countdown = Cardinal.getModule(CountdownModule.class).getCycleCountdown(matchThread);
5477
countdown.setTime(time);
5578
countdown.setCancelled(false);

src/main/java/in/twizmwaz/cardinal/command/CommandSetNext.java renamed to src/main/java/in/twizmwaz/cardinal/command/CommandNext.java

+19-4
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,40 @@
2525

2626
package in.twizmwaz.cardinal.command;
2727

28+
import ee.ellytr.chat.ChatConstant;
29+
import ee.ellytr.chat.component.builder.LocalizedComponentBuilder;
2830
import ee.ellytr.command.Command;
2931
import ee.ellytr.command.CommandContext;
30-
import ee.ellytr.command.PlayerCommand;
3132
import ee.ellytr.command.argument.MultiArgs;
3233
import in.twizmwaz.cardinal.Cardinal;
34+
import in.twizmwaz.cardinal.component.map.MapComponentBuilder;
3335
import in.twizmwaz.cardinal.module.cycle.CycleModule;
3436
import in.twizmwaz.cardinal.module.repository.LoadedMap;
37+
import in.twizmwaz.cardinal.util.ChatUtil;
38+
import net.md_5.bungee.api.ChatColor;
3539

36-
public class CommandSetNext {
40+
public class CommandNext {
3741

3842
/**
3943
* Sets the next map.
4044
*
4145
* @param cmd The context of this command.
4246
*/
4347
@Command(aliases = {"setnext", "sn"}, description = "Sets the next map.")
44-
@PlayerCommand
4548
public static void setNext(CommandContext cmd, @MultiArgs LoadedMap map) {
46-
Cardinal.getModule(CycleModule.class).getNextCycle().get(Cardinal.getMatchThread(cmd.getSender())).setMap(map);
49+
Cardinal.getModule(CycleModule.class).getNextCycle(Cardinal.getMatchThread(cmd.getSender())).setMap(map);
50+
}
51+
52+
/**
53+
* Sets the next map.
54+
*
55+
* @param cmd The context of this command.
56+
*/
57+
@Command(aliases = {"next"}, description = "Gets the next map.")
58+
public static void getNext(CommandContext cmd) {
59+
LoadedMap map = Cardinal.getModule(CycleModule.class).getNextMap(Cardinal.getMatchThread(cmd.getSender()));
60+
ChatUtil.sendMessage(cmd.getSender(), new LocalizedComponentBuilder(ChatConstant.getConstant("command.next.map"),
61+
new MapComponentBuilder(map).color(ChatColor.GOLD).build()).color(ChatColor.DARK_PURPLE).build());
4762
}
4863

4964
}

src/main/java/in/twizmwaz/cardinal/command/provider/LoadedMapProvider.java

+7-11
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,26 @@
2929
import in.twizmwaz.cardinal.Cardinal;
3030
import in.twizmwaz.cardinal.module.repository.LoadedMap;
3131
import in.twizmwaz.cardinal.module.repository.RepositoryModule;
32+
import in.twizmwaz.cardinal.util.Strings;
3233
import org.bukkit.command.CommandSender;
3334

3435
import java.util.List;
35-
import java.util.Map;
36-
import java.util.Set;
3736
import java.util.stream.Collectors;
3837

3938
public class LoadedMapProvider implements ArgumentProvider<LoadedMap> {
4039

4140
@Override
4241
public LoadedMap getMatch(String input, CommandSender sender) {
43-
Map<String, LoadedMap> maps = Cardinal.getModule(RepositoryModule.class).getLoadedMaps();
44-
Set<String> mapNames = maps.keySet();
45-
for (String mapName : mapNames.stream().filter(m -> m.startsWith(input)).collect(Collectors.toList())) {
46-
return maps.get(mapName);
47-
}
48-
return null;
42+
List<String> mapNames = getSuggestions(input, sender);
43+
return mapNames.size() > 0 ? Cardinal.getModule(RepositoryModule.class).getLoadedMaps().get(mapNames.get(0)) : null;
4944
}
5045

5146
@Override
5247
public List<String> getSuggestions(String input, CommandSender sender) {
53-
;
54-
return Cardinal.getModule(RepositoryModule.class).getLoadedMaps().keySet().stream().filter(
55-
map -> map.toLowerCase().startsWith(input.toLowerCase())).collect(Collectors.toList());
48+
return Cardinal.getModule(RepositoryModule.class).getLoadedMaps().keySet().stream()
49+
.filter(map -> Strings.getSimplifiedName(map).startsWith(Strings.getSimplifiedName(input.toLowerCase())))
50+
.collect(Collectors.toList());
5651

5752
}
53+
5854
}

src/main/java/in/twizmwaz/cardinal/command/provider/TeamProvider.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,27 @@
2525

2626
package in.twizmwaz.cardinal.command.provider;
2727

28-
import com.google.common.collect.Lists;
2928
import ee.ellytr.command.argument.ArgumentProvider;
3029
import in.twizmwaz.cardinal.Cardinal;
3130
import in.twizmwaz.cardinal.module.team.Team;
31+
import in.twizmwaz.cardinal.util.Strings;
3232
import org.bukkit.command.CommandSender;
3333

3434
import java.util.List;
35+
import java.util.stream.Collectors;
3536

3637
public class TeamProvider implements ArgumentProvider<Team> {
3738

3839
@Override
3940
public Team getMatch(String input, CommandSender sender) {
40-
//TODO: Get match from player requesting command match
41-
return Team.getTeamById(Cardinal.getInstance().getMatchThreads().get(0).getCurrentMatch(), input);
41+
return Team.getTeamByName(Cardinal.getMatchThread(sender).getCurrentMatch(), input);
4242
}
4343

4444
@Override
4545
public List<String> getSuggestions(String input, CommandSender sender) {
46-
List<String> suggestions = Lists.newArrayList();
47-
//TODO: Get match from player requesting suggestions
48-
for (Team team : Team.getTeams(Cardinal.getInstance().getMatchThreads().get(0).getCurrentMatch())) {
49-
String id = team.getId();
50-
if (id.toLowerCase().startsWith(input.toLowerCase())) {
51-
suggestions.add(id);
52-
}
53-
}
54-
return suggestions;
46+
return Team.getTeams(Cardinal.getMatchThread(sender).getCurrentMatch())
47+
.stream().filter(team -> Strings.getSimplifiedName(team.getName()).startsWith(Strings.getSimplifiedName(input)))
48+
.map(Team::getName).collect(Collectors.toList());
5549
}
5650

5751
}

src/main/java/in/twizmwaz/cardinal/component/TeamComponentBuilder.java renamed to src/main/java/in/twizmwaz/cardinal/component/BaseComponentBuilder.java

+39-37
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@
2626
package in.twizmwaz.cardinal.component;
2727

2828
import com.google.common.collect.Lists;
29-
import in.twizmwaz.cardinal.module.team.Team;
3029
import net.md_5.bungee.api.ChatColor;
3130
import net.md_5.bungee.api.chat.BaseComponent;
3231
import net.md_5.bungee.api.chat.ClickEvent;
3332
import net.md_5.bungee.api.chat.HoverEvent;
3433

3534
import java.util.List;
3635

37-
public class TeamComponentBuilder {
36+
/**
37+
* This is a base for other component builders to use, it has all the {@link BaseComponent} fields.
38+
*
39+
* @param <B> The builder class, should always be the same as the class.
40+
* @param <C> The component output.
41+
*/
42+
public abstract class BaseComponentBuilder<B extends BaseComponentBuilder, C extends BaseComponent> {
3843

39-
private Team team;
4044
private ChatColor color;
4145
private boolean bold;
4246
private boolean italic;
@@ -46,15 +50,13 @@ public class TeamComponentBuilder {
4650
private ClickEvent clickEvent;
4751
private HoverEvent hoverEvent;
4852
private List<BaseComponent> extra;
49-
private boolean hover;
53+
54+
private B thisObject;
5055

5156
/**
52-
* Creates a builder of {@link TeamComponent} based on specified values.
53-
*
54-
* @param team The team for this component.
57+
* This is a base for other component builders to use, it has all the {@link BaseComponent} fields.
5558
*/
56-
public TeamComponentBuilder(Team team) {
57-
this.team = team;
59+
public BaseComponentBuilder() {
5860
color = null;
5961
bold = false;
6062
italic = false;
@@ -64,66 +66,67 @@ public TeamComponentBuilder(Team team) {
6466
clickEvent = null;
6567
hoverEvent = null;
6668
extra = Lists.newArrayList();
67-
hover = true;
69+
thisObject = getThis();
6870
}
6971

70-
public TeamComponentBuilder color(ChatColor color) {
72+
/**
73+
* Must be implemented by all sub classes, should just be a "return this;".
74+
* @return The builder object.
75+
*/
76+
public abstract B getThis();
77+
78+
79+
public B color(ChatColor color) {
7180
this.color = color;
72-
return this;
81+
return thisObject;
7382
}
7483

75-
public TeamComponentBuilder bold(boolean bold) {
84+
public B bold(boolean bold) {
7685
this.bold = bold;
77-
return this;
86+
return thisObject;
7887
}
7988

80-
public TeamComponentBuilder italic(boolean italic) {
89+
public B italic(boolean italic) {
8190
this.italic = italic;
82-
return this;
91+
return thisObject;
8392
}
8493

85-
public TeamComponentBuilder underlined(boolean underlined) {
94+
public B underlined(boolean underlined) {
8695
this.underlined = underlined;
87-
return this;
96+
return thisObject;
8897
}
8998

90-
public TeamComponentBuilder strikethrough(boolean strikethrough) {
99+
public B strikethrough(boolean strikethrough) {
91100
this.strikethrough = strikethrough;
92-
return this;
101+
return thisObject;
93102
}
94103

95-
public TeamComponentBuilder obfuscated(boolean obfuscated) {
104+
public B obfuscated(boolean obfuscated) {
96105
this.obfuscated = obfuscated;
97-
return this;
106+
return thisObject;
98107
}
99108

100-
public TeamComponentBuilder clickEvent(ClickEvent clickEvent) {
109+
public B clickEvent(ClickEvent clickEvent) {
101110
this.clickEvent = clickEvent;
102-
return this;
111+
return thisObject;
103112
}
104113

105-
public TeamComponentBuilder hoverEvent(HoverEvent hoverEvent) {
114+
public B hoverEvent(HoverEvent hoverEvent) {
106115
this.hoverEvent = hoverEvent;
107-
return this;
116+
return thisObject;
108117
}
109118

110-
public TeamComponentBuilder extra(List<BaseComponent> extra) {
119+
public B extra(List<BaseComponent> extra) {
111120
this.extra = extra;
112-
return this;
113-
}
114-
115-
public TeamComponentBuilder hover(boolean hover) {
116-
this.hover = hover;
117-
return this;
121+
return thisObject;
118122
}
119123

120124
/**
121-
* Builds a {@link TeamComponent} from the specified values.
125+
* Builds a {@link C} from the specified values.
122126
*
123127
* @return The built component.
124128
*/
125-
public TeamComponent build() {
126-
TeamComponent component = new TeamComponent(team);
129+
public C build(C component) {
127130
component.setColor(color);
128131
component.setBold(bold);
129132
component.setItalic(italic);
@@ -133,7 +136,6 @@ public TeamComponent build() {
133136
component.setClickEvent(clickEvent);
134137
component.setHoverEvent(hoverEvent);
135138
component.setExtra(extra);
136-
component.setHover(hover);
137139
return component;
138140
}
139141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.component;
27+
28+
import ee.ellytr.chat.component.LanguageComponent;
29+
import net.md_5.bungee.api.chat.BaseComponent;
30+
31+
public abstract class BaseLanguageComponent<C extends BaseComponent> extends LanguageComponent {
32+
33+
/**
34+
* Adds regular {@link BaseComponent} fields to a duplicate component.
35+
* @param component The BaseComponent to add values to.
36+
* @return The BaseComponent with the values.
37+
*/
38+
public C duplicate(C component) {
39+
component.setColor(getColor());
40+
component.setBold(isBold());
41+
component.setItalic(isItalic());
42+
component.setUnderlined(isUnderlined());
43+
component.setStrikethrough(isStrikethrough());
44+
component.setObfuscated(isObfuscated());
45+
component.setClickEvent(getClickEvent());
46+
component.setHoverEvent(getHoverEvent());
47+
if (getExtra() != null) {
48+
for (BaseComponent extra : getExtra()) {
49+
component.addExtra(extra.duplicate());
50+
}
51+
}
52+
return component;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)