Skip to content

Commit

Permalink
add: Jerry's Perkpocalypse candidate perks
Browse files Browse the repository at this point in the history
and some code cleanups
  • Loading branch information
Fix3dll committed Nov 27, 2024
1 parent dc99cdc commit 3fdf3aa
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import codes.biscuit.skyblockaddons.config.PersistentValuesManager;
import codes.biscuit.skyblockaddons.core.Feature;
import codes.biscuit.skyblockaddons.mixins.hooks.FontRendererHook;
import codes.biscuit.skyblockaddons.utils.data.skyblockdata.MayorJerryData;
import codes.biscuit.skyblockaddons.utils.gson.RarityAdapter;
import codes.biscuit.skyblockaddons.utils.gson.UuidAdapter;
import codes.biscuit.skyblockaddons.utils.data.skyblockdata.ElectionData;
Expand Down Expand Up @@ -115,12 +116,14 @@ public class SkyblockAddons {
private final InventoryUtils inventoryUtils;
private final Utils utils;
private final Updater updater;
@Setter private OnlineData onlineData;
private final DiscordRPCManager discordRPCManager;
private final Scheduler scheduler;
private final DungeonManager dungeonManager;
private final SkillXpManager skillXpManager;

@Setter private OnlineData onlineData;
@Setter private ElectionData electionData;
@Setter private MayorJerryData mayorJerryData;

private boolean usingLabymod;
private boolean usingOofModv1;
Expand All @@ -144,6 +147,7 @@ public SkyblockAddons() {
dungeonManager = new DungeonManager();
discordRPCManager = new DiscordRPCManager();
skillXpManager = new SkillXpManager();
electionData = new ElectionData();
}

@Mod.EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,7 @@ public void onChatReceived(ClientChatReceivedEvent e) {
// The player has 10 minutes to kill the animal
CooldownManager.put("TREVOR_THE_TRAPPER_HUNT", 600000);
// The player has cooldown before they can receive another animal after killing the current one
if (main.getUtils().getMayor().equals("Finnegan")
|| main.getUtils().getMinisterAndPerk().getKey().equals("Finnegan")
|| main.getUtils().getJerryMayor().equals("Finnegan")) {
if (main.getElectionData().isCandidateActive("Finnegan")) {
CooldownManager.put("TREVOR_THE_TRAPPER_RETURN", 16000);
} else {
CooldownManager.put("TREVOR_THE_TRAPPER_RETURN", 21000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import codes.biscuit.skyblockaddons.utils.ColorCode;
import codes.biscuit.skyblockaddons.utils.DrawUtils;
import codes.biscuit.skyblockaddons.utils.objects.Pair;
import codes.biscuit.skyblockaddons.utils.data.skyblockdata.ElectionData;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -37,8 +36,7 @@ public IslandButton(IslandWarpGui.Island island) {
for (IslandWarpGui.Marker marker : IslandWarpGui.Marker.values()) {
if (marker.getIsland() == island) {
if (marker == IslandWarpGui.Marker.CARNIVAL) {
ElectionData electionData = SkyblockAddons.getInstance().getElectionData();
if (electionData != null && !electionData.isPerkActive("Chivalrous Carnival")) {
if (!SkyblockAddons.getInstance().getElectionData().isPerkActive("Chivalrous Carnival")) {
continue;
}
}
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/codes/biscuit/skyblockaddons/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import codes.biscuit.skyblockaddons.core.SkyblockDate;
import codes.biscuit.skyblockaddons.events.SkyblockJoinedEvent;
import codes.biscuit.skyblockaddons.events.SkyblockLeftEvent;
import codes.biscuit.skyblockaddons.utils.objects.Pair;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -169,25 +168,6 @@ public class Utils {
*/
private String mayor = "Fix3dll";

/**
* The current minister name
* <br>
* <i>We don't leave it blank in case the Minister is not found</i>
*/
private Pair<String, String> ministerAndPerk = new Pair<>("Fix3dll", "Spaghetti code");

/**
* The current Jerry's Perkpocalypse mayor
* <br>
* <i>We don't leave it blank in case the Mayor is not found</i>
*/
private String jerryMayor = "Fix3dll";

/**
* The current Jerry's Perkpocalypse mayor update timestamp
*/
private long jerryMayorUpdateTime = 0L;

/**
* Dungeon floor information from the scoreboard
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,48 @@
import codes.biscuit.skyblockaddons.utils.data.DataFetchCallback;
import codes.biscuit.skyblockaddons.utils.data.JSONResponseHandler;
import codes.biscuit.skyblockaddons.utils.data.RemoteFileRequest;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import codes.biscuit.skyblockaddons.utils.data.skyblockdata.MayorJerryData;
import net.minecraft.util.EnumChatFormatting;
import org.apache.logging.log4j.Logger;

import java.net.URI;
import java.util.Optional;

public class JerryMayorRequest extends RemoteFileRequest<JsonObject> {
public class JerryMayorRequest extends RemoteFileRequest<MayorJerryData> {
private static final Logger LOGGER = SkyblockAddons.getLogger();

public JerryMayorRequest() {
super(
"https://api.skytils.gg/api/mayor/jerry"
, new JSONResponseHandler<>(JsonObject.class),
, new JSONResponseHandler<>(MayorJerryData.class),
new JerryMayorCallback("https://api.skytils.gg/api/mayor/jerry")
, false
, true
);
}

public static class JerryMayorCallback extends DataFetchCallback<JsonObject> {
public static class JerryMayorCallback extends DataFetchCallback<MayorJerryData> {

public JerryMayorCallback(String path) {
super(LOGGER, URI.create(path));
}

@Override
public void completed(JsonObject result) {
public void completed(MayorJerryData result) {
super.completed(result);
SkyblockAddons main = SkyblockAddons.getInstance();
MayorJerryData oldData = main.getMayorJerryData();
main.setMayorJerryData(result);

String jerryMayorName = Optional.ofNullable(result)
.map(r -> r.get("mayor")).map(JsonElement::getAsJsonObject)
.map(jsonObject -> jsonObject.get("name")).map(JsonElement::getAsString)
.map(MayorJerryData::getMayor)
.map(MayorJerryData.Mayor::getName)
.orElse(null);
String oldJerryMayor = main.getUtils().getJerryMayor();

main.getUtils().setJerryMayor(jerryMayorName == null ? "Fix3dll" : jerryMayorName);

if (jerryMayorName != null) {
LOGGER.info("Jerry's Perkpocalypse mayor switched to {}", jerryMayorName);
main.getUtils().setJerryMayorUpdateTime(result.get("nextSwitch").getAsLong());

if (main.getUtils().isOnSkyblock() && !oldJerryMayor.equals(jerryMayorName)) {
if (main.getUtils().isOnSkyblock() && !jerryMayorName.equals(oldData.getMayor().getName())) {
main.getUtils().sendMessage(
EnumChatFormatting.GREEN + "Jerry's Perkpocalypse mayor switched to " + jerryMayorName
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import codes.biscuit.skyblockaddons.utils.data.DataUtils;
import codes.biscuit.skyblockaddons.utils.data.JSONResponseHandler;
import codes.biscuit.skyblockaddons.utils.data.RemoteFileRequest;
import codes.biscuit.skyblockaddons.utils.objects.Pair;
import codes.biscuit.skyblockaddons.utils.data.skyblockdata.MayorJerryData;
import codes.biscuit.skyblockaddons.utils.data.skyblockdata.ElectionData;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -63,13 +63,6 @@ public void completed(ElectionData result) {
// If initial request or request completed with expected result
if (newMayorName.isEmpty() || newMayorName.equals(mayorName)) {
main.getUtils().setMayor(mayorName == null ? "Fix3dll" : mayorName);

ElectionData.Mayor.Minister minister = result.getMayor().getMinister();
if (minister != null && minister.getPerk() != null) {
main.getUtils().setMinisterAndPerk(
new Pair<>(minister.getName(), minister.getPerk().getName())
);
}
}

// Jerry's Perkpocalypse mayor updater
Expand Down Expand Up @@ -108,7 +101,8 @@ private ScheduledTask scheduleUpdateTask(String expectedMayorName) {

private ScheduledTask scheduleJerryMayorTask() {
return main.getScheduler().scheduleAsyncTask(scheduledTask -> {
if (System.currentTimeMillis() > main.getUtils().getJerryMayorUpdateTime()) {
MayorJerryData mayorJerryData = main.getMayorJerryData();
if (mayorJerryData == null || System.currentTimeMillis() > mayorJerryData.getNextSwitch()) {
DataUtils.loadOnlineData(new JerryMayorRequest());
}
}, 0, 60 * 20);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,158 @@
package codes.biscuit.skyblockaddons.utils.data.skyblockdata;

import codes.biscuit.skyblockaddons.SkyblockAddons;
import com.google.gson.annotations.SerializedName;
import lombok.Getter;
import org.apache.logging.log4j.Logger;

import java.util.Collections;
import java.util.List;

@Getter
public class ElectionData {
private boolean success;
private static final Logger LOGGER = SkyblockAddons.getLogger();

@SerializedName("success")
private boolean success = false;
@SerializedName("lastUpdated")
private long lastUpdated;
@SerializedName("mayor")
private Mayor mayor;
@SerializedName("current")
private Current current;

/**
* Checks if the given perk is active in any Mayor, Minister or Jerry's Perkpocalypse
* @param perkName case-insensitive perk name
* @return true if perk is active
*/
public boolean isPerkActive(String perkName) {
for (Mayor.Perk perk : this.mayor.perks) {
if (perk.name.equals(perkName)) {
// illegal argument
if (perkName == null || perkName.isEmpty()) {
LOGGER.error("\"perkName\" cannot be null or empty!");
return false;
} else if (!success) {
return false;
}

if (mayor != null) {
for (Mayor.Perk perk : this.mayor.perks) {
if (perkName.equalsIgnoreCase(perk.name)) {
return true;
}
}

// If a Special Mayor is elected, then no Minister is selected.
if (mayor.minister != null && perkName.equalsIgnoreCase(mayor.minister.name)) {
return true;
}
}

MayorJerryData mayorJerryData = SkyblockAddons.getInstance().getMayorJerryData();
if (mayorJerryData != null && mayorJerryData.hasMayorAndActive()) {
return mayorJerryData.getMayor().getPerks().stream().anyMatch(
perk -> perk.getName().equalsIgnoreCase(perkName)
);
}

return false;
}

/**
* Checks if the candidate is active in any Mayor, Minister or Jerry's Perkpocalypse
* @param candidateName case-insensitive candidateName name
* @return true if candidate is active
*/
public boolean isCandidateActive(String candidateName) {
// illegal argument
if (candidateName == null || candidateName.isEmpty()) {
LOGGER.error("\"candidateName\" cannot be null or empty!");
return false;
} else if (!success) {
return false;
}

if (this.mayor != null) {
if (candidateName.equalsIgnoreCase(mayor.name)) {
return true;
} else if (mayor.minister != null && candidateName.equalsIgnoreCase(mayor.minister.name)) {
return true;
}
}

return mayor.minister != null && mayor.minister.perk.name.equals(perkName);
MayorJerryData mayorJerryData = SkyblockAddons.getInstance().getMayorJerryData();
return mayorJerryData != null && mayorJerryData.hasMayorAndActive()
&& candidateName.equalsIgnoreCase(mayorJerryData.getMayor().getName());
}

@Getter
public static class Mayor {
@SerializedName("key")
private String key;
@SerializedName("name")
private String name;
private List<Perk> perks;
@SerializedName("perks")
private List<Perk> perks = Collections.emptyList();
@SerializedName("minister")
private Minister minister;
@SerializedName("election")
private Election election;

@Getter
public static class Perk {
@SerializedName("name")
private String name;
@SerializedName("description")
private String description;
}

@Getter
public static class Minister {
@SerializedName("key")
private String key;
@SerializedName("name")
private String name;
@SerializedName("perk")
private Perk perk;
}

@Getter
public static class Election {
@SerializedName("year")
private int year;
@SerializedName("candidates")
private List<Candidate> candidates;

@Getter
public static class Candidate {
@SerializedName("key")
private String key;
@SerializedName("name")
private String name;
private List<Perk> perks;
@SerializedName("perks")
private List<Perk> perks = Collections.emptyList();
@SerializedName("votes")
private int votes;
}
}
}

@Getter
public static class Current {
@SerializedName("year")
private int year;
private List<Candidate> candidates;
@SerializedName("candidates")
private List<Candidate> candidates = Collections.emptyList();

@Getter
public static class Candidate {
@SerializedName("key")
private String key;
@SerializedName("name")
private String name;
private List<Mayor.Perk> perks;
@SerializedName("perks")
private List<Mayor.Perk> perks = Collections.emptyList();
@SerializedName("votes")
private int votes;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package codes.biscuit.skyblockaddons.utils.data.skyblockdata;

import com.google.gson.annotations.SerializedName;
import lombok.Getter;

import java.util.Collections;
import java.util.List;

@Getter
public class MayorJerryData {

@SerializedName("nextSwitch")
private Long nextSwitch;
@SerializedName("mayor")
private Mayor mayor;

public boolean hasMayorAndActive() {
return mayor != null && nextSwitch >= System.currentTimeMillis();
}

@Getter
public static class Mayor {
@SerializedName("name")
private String name = "Fix3dll";
@SerializedName("perks")
private List<Perk> perks = Collections.emptyList();
}

@Getter
public static class Perk {
@SerializedName("name")
private String name;
@SerializedName("description")
private String description;

}
}

0 comments on commit 3fdf3aa

Please sign in to comment.