Skip to content

Commit 0fcc325

Browse files
Merge pull request #731 from ArikSquad/feat/mayors
SkyBlock Mayors
2 parents 3cf942f + 5353092 commit 0fcc325

198 files changed

Lines changed: 5585 additions & 1760 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

commons/src/main/java/net/swofty/commons/ServiceType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public enum ServiceType {
1111
ORCHESTRATOR,
1212
FRIEND,
1313
PUNISHMENT,
14+
ELECTION,
15+
;
1416
}

commons/src/main/java/net/swofty/commons/StringUtility.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ public static String getAsRomanNumeral(int num) {
245245
return roman.toString();
246246
}
247247

248+
/**
249+
* Capitalizes the first letter of the input string and lowercases the rest.
250+
* @param input The string to capitalize.
251+
* @return The input string with the first letter capitalized and the rest lowercased. If the input is null or empty, it returns the input as is.
252+
*/
253+
public static String capitalize(String input) {
254+
if (input == null || input.isEmpty()) {
255+
return input;
256+
}
257+
return input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
258+
}
259+
248260
public static String getTextFromComponent(Component component) {
249261
if (component == null)
250262
throw new IllegalArgumentException("Component cannot be null");
@@ -342,6 +354,49 @@ public static List<String> splitByWordAndLength(String string, int splitLength)
342354
return result;
343355
}
344356

357+
public static List<String> splitByWordAndLengthKeepLegacyColor(String string, int splitLength) {
358+
List<String> result = new ArrayList<>();
359+
String lastColorCode = "";
360+
361+
for (String line : string.split("\n", -1)) {
362+
if (line.isEmpty()) {
363+
result.add("");
364+
continue;
365+
}
366+
367+
StringBuilder currentLine = new StringBuilder(lastColorCode);
368+
369+
for (String word : line.split(" ")) {
370+
if (word.isEmpty()) continue;
371+
372+
int extraSpace = currentLine.length() == lastColorCode.length() ? 0 : 1;
373+
374+
if (currentLine.length() + extraSpace + word.length() > splitLength) {
375+
if (currentLine.length() > lastColorCode.length()) {
376+
result.add(currentLine.toString());
377+
currentLine = new StringBuilder(lastColorCode);
378+
}
379+
} else if (extraSpace == 1) {
380+
currentLine.append(' ');
381+
}
382+
383+
currentLine.append(word);
384+
385+
// this wont work with bold/italic and those but if you need those don't use this method
386+
int colorIndex = word.lastIndexOf('§');
387+
if (colorIndex != -1 && colorIndex < word.length() - 1) {
388+
lastColorCode = "§" + word.charAt(colorIndex + 1);
389+
}
390+
}
391+
392+
if (currentLine.length() > lastColorCode.length()) {
393+
result.add(currentLine.toString());
394+
}
395+
}
396+
397+
return result;
398+
}
399+
345400
public static List<String> splitByNewLine(String string) {
346401
return new ArrayList<>(Arrays.asList(string.split("\n", -1)));
347402
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package net.swofty.commons.protocol.objects.election;
2+
3+
import net.swofty.commons.protocol.ProtocolObject;
4+
import net.swofty.commons.protocol.Serializer;
5+
import org.json.JSONObject;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.UUID;
10+
11+
public class CastVoteProtocolObject
12+
extends ProtocolObject<CastVoteProtocolObject.CastVoteMessage,
13+
CastVoteProtocolObject.CastVoteResponse> {
14+
15+
@Override
16+
public Serializer<CastVoteMessage> getSerializer() {
17+
return new Serializer<>() {
18+
@Override
19+
public String serialize(CastVoteMessage value) {
20+
JSONObject json = new JSONObject();
21+
json.put("accountId", value.accountId().toString());
22+
json.put("candidateName", value.candidateName());
23+
return json.toString();
24+
}
25+
26+
@Override
27+
public CastVoteMessage deserialize(String json) {
28+
JSONObject obj = new JSONObject(json);
29+
return new CastVoteMessage(
30+
UUID.fromString(obj.getString("accountId")),
31+
obj.getString("candidateName")
32+
);
33+
}
34+
35+
@Override
36+
public CastVoteMessage clone(CastVoteMessage value) {
37+
return value;
38+
}
39+
};
40+
}
41+
42+
@Override
43+
public Serializer<CastVoteResponse> getReturnSerializer() {
44+
return new Serializer<>() {
45+
@Override
46+
public String serialize(CastVoteResponse value) {
47+
JSONObject json = new JSONObject();
48+
json.put("success", value.success());
49+
json.put("tallies", value.tallies() == null ? null : new JSONObject(value.tallies()));
50+
return json.toString();
51+
}
52+
53+
@Override
54+
public CastVoteResponse deserialize(String json) {
55+
JSONObject obj = new JSONObject(json);
56+
Map<String, Long> tallies = null;
57+
if (!obj.isNull("tallies")) {
58+
tallies = new HashMap<>();
59+
JSONObject talliesObject = obj.getJSONObject("tallies");
60+
for (String key : talliesObject.keySet()) {
61+
tallies.put(key, talliesObject.getLong(key));
62+
}
63+
}
64+
return new CastVoteResponse(
65+
obj.getBoolean("success"),
66+
tallies
67+
);
68+
}
69+
70+
@Override
71+
public CastVoteResponse clone(CastVoteResponse value) {
72+
return value;
73+
}
74+
};
75+
}
76+
77+
public record CastVoteMessage(UUID accountId, String candidateName) {}
78+
79+
public record CastVoteResponse(boolean success, Map<String, Long> tallies) {
80+
}
81+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package net.swofty.commons.protocol.objects.election;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.reflect.TypeToken;
5+
import net.swofty.commons.protocol.ProtocolObject;
6+
import net.swofty.commons.protocol.Serializer;
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
10+
import java.util.List;
11+
12+
public class GetCandidatesProtocolObject
13+
extends ProtocolObject<GetCandidatesProtocolObject.GetCandidatesMessage,
14+
GetCandidatesProtocolObject.GetCandidatesResponse> {
15+
16+
@Override
17+
public Serializer<GetCandidatesMessage> getSerializer() {
18+
return new Serializer<>() {
19+
@Override
20+
public String serialize(GetCandidatesMessage value) {
21+
return "";
22+
}
23+
24+
@Override
25+
public GetCandidatesMessage deserialize(String json) {
26+
return new GetCandidatesMessage();
27+
}
28+
29+
@Override
30+
public GetCandidatesMessage clone(GetCandidatesMessage value) {
31+
return value;
32+
}
33+
};
34+
}
35+
36+
@Override
37+
public Serializer<GetCandidatesResponse> getReturnSerializer() {
38+
return new Serializer<>() {
39+
private final Gson gson = new Gson();
40+
41+
@Override
42+
public String serialize(GetCandidatesResponse value) {
43+
JSONObject json = new JSONObject();
44+
json.put("electionOpen", value.electionOpen());
45+
json.put("candidates", value.candidates() == null ? null : new JSONArray(gson.toJson(value.candidates())));
46+
return json.toString();
47+
}
48+
49+
@Override
50+
public GetCandidatesResponse deserialize(String json) {
51+
JSONObject obj = new JSONObject(json);
52+
boolean open = obj.getBoolean("electionOpen");
53+
List<CandidateInfo> candidates = List.of();
54+
if (!obj.isNull("candidates")) {
55+
candidates = gson.fromJson(
56+
obj.getJSONArray("candidates").toString(),
57+
new TypeToken<List<CandidateInfo>>() {}.getType()
58+
);
59+
}
60+
return new GetCandidatesResponse(open, candidates);
61+
}
62+
63+
@Override
64+
public GetCandidatesResponse clone(GetCandidatesResponse value) {
65+
return value;
66+
}
67+
};
68+
}
69+
70+
public record GetCandidatesMessage() {}
71+
72+
public record GetCandidatesResponse(
73+
boolean electionOpen,
74+
List<CandidateInfo> candidates
75+
) {}
76+
77+
public record CandidateInfo(
78+
String mayorName,
79+
List<String> activePerks,
80+
long votes,
81+
double votePercentage
82+
) {}
83+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package net.swofty.commons.protocol.objects.election;
2+
3+
import net.swofty.commons.protocol.ProtocolObject;
4+
import net.swofty.commons.protocol.Serializer;
5+
import org.json.JSONObject;
6+
7+
public class GetElectionDataProtocolObject
8+
extends ProtocolObject<GetElectionDataProtocolObject.GetElectionDataMessage,
9+
GetElectionDataProtocolObject.GetElectionDataResponse> {
10+
11+
@Override
12+
public Serializer<GetElectionDataMessage> getSerializer() {
13+
return new Serializer<>() {
14+
@Override
15+
public String serialize(GetElectionDataMessage value) {
16+
return "";
17+
}
18+
19+
@Override
20+
public GetElectionDataMessage deserialize(String json) {
21+
return new GetElectionDataMessage();
22+
}
23+
24+
@Override
25+
public GetElectionDataMessage clone(GetElectionDataMessage value) {
26+
return value;
27+
}
28+
};
29+
}
30+
31+
@Override
32+
public Serializer<GetElectionDataResponse> getReturnSerializer() {
33+
return new Serializer<>() {
34+
@Override
35+
public String serialize(GetElectionDataResponse value) {
36+
JSONObject json = new JSONObject();
37+
json.put("found", value.found());
38+
json.put("data", value.serializedData());
39+
return json.toString();
40+
}
41+
42+
@Override
43+
public GetElectionDataResponse deserialize(String json) {
44+
JSONObject obj = new JSONObject(json);
45+
return new GetElectionDataResponse(
46+
obj.getBoolean("found"),
47+
obj.optString("data", null)
48+
);
49+
}
50+
51+
@Override
52+
public GetElectionDataResponse clone(GetElectionDataResponse value) {
53+
return value;
54+
}
55+
};
56+
}
57+
58+
public record GetElectionDataMessage() {}
59+
60+
public record GetElectionDataResponse(
61+
boolean found,
62+
String serializedData
63+
) {}
64+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.swofty.commons.protocol.objects.election;
2+
3+
import net.swofty.commons.protocol.ProtocolObject;
4+
import net.swofty.commons.protocol.Serializer;
5+
import org.json.JSONObject;
6+
7+
import java.util.UUID;
8+
9+
public class GetPlayerVoteProtocolObject
10+
extends ProtocolObject<GetPlayerVoteProtocolObject.GetPlayerVoteMessage,
11+
GetPlayerVoteProtocolObject.GetPlayerVoteResponse> {
12+
13+
@Override
14+
public Serializer<GetPlayerVoteMessage> getSerializer() {
15+
return new Serializer<>() {
16+
@Override
17+
public String serialize(GetPlayerVoteMessage value) {
18+
JSONObject json = new JSONObject();
19+
json.put("accountId", value.accountId().toString());
20+
return json.toString();
21+
}
22+
23+
@Override
24+
public GetPlayerVoteMessage deserialize(String json) {
25+
JSONObject obj = new JSONObject(json);
26+
return new GetPlayerVoteMessage(UUID.fromString(obj.getString("accountId")));
27+
}
28+
29+
@Override
30+
public GetPlayerVoteMessage clone(GetPlayerVoteMessage value) {
31+
return value;
32+
}
33+
};
34+
}
35+
36+
@Override
37+
public Serializer<GetPlayerVoteResponse> getReturnSerializer() {
38+
return new Serializer<>() {
39+
@Override
40+
public String serialize(GetPlayerVoteResponse value) {
41+
JSONObject json = new JSONObject();
42+
json.put("candidateName", value.candidateName());
43+
return json.toString();
44+
}
45+
46+
@Override
47+
public GetPlayerVoteResponse deserialize(String json) {
48+
JSONObject obj = new JSONObject(json);
49+
return new GetPlayerVoteResponse(obj.optString("candidateName", null));
50+
}
51+
52+
@Override
53+
public GetPlayerVoteResponse clone(GetPlayerVoteResponse value) {
54+
return value;
55+
}
56+
};
57+
}
58+
59+
public record GetPlayerVoteMessage(UUID accountId) {}
60+
61+
public record GetPlayerVoteResponse(String candidateName) {}
62+
}

0 commit comments

Comments
 (0)