diff options
| author | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-10-18 21:01:11 +1100 |
|---|---|---|
| committer | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-10-18 21:01:11 +1100 |
| commit | a0402708801b525145d01d0f4da17f0ba9d93455 (patch) | |
| tree | ce0b58df707388449558073df3aa9492efaf8978 /src/main/java/io/github/moulberry/notenoughupdates/questing/SBInfo.java | |
| parent | 275fe45caa8eb1048914d864aafae21f3f3a1157 (diff) | |
| download | notenoughupdates-a0402708801b525145d01d0f4da17f0ba9d93455.tar.gz notenoughupdates-a0402708801b525145d01d0f4da17f0ba9d93455.tar.bz2 notenoughupdates-a0402708801b525145d01d0f4da17f0ba9d93455.zip | |
1.4
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/questing/SBInfo.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/questing/SBInfo.java | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/questing/SBInfo.java b/src/main/java/io/github/moulberry/notenoughupdates/questing/SBInfo.java new file mode 100644 index 00000000..b647fb1c --- /dev/null +++ b/src/main/java/io/github/moulberry/notenoughupdates/questing/SBInfo.java @@ -0,0 +1,125 @@ +package io.github.moulberry.notenoughupdates.questing; + +import com.google.gson.JsonObject; +import io.github.moulberry.notenoughupdates.NotEnoughUpdates; +import io.github.moulberry.notenoughupdates.util.Utils; +import net.minecraft.client.Minecraft; +import net.minecraft.scoreboard.Score; +import net.minecraft.scoreboard.ScoreObjective; +import net.minecraft.scoreboard.ScorePlayerTeam; +import net.minecraft.scoreboard.Scoreboard; +import net.minecraftforge.client.event.ClientChatReceivedEvent; +import net.minecraftforge.event.world.WorldEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class SBInfo { + + private static final SBInfo INSTANCE = new SBInfo(); + + private static final Pattern timePattern = Pattern.compile(".+(am|pm)"); + + public String location = ""; + public String date = ""; + public String time = ""; + public String objective = ""; + + public String mode = ""; + + public Date currentTimeDate = null; + + public static SBInfo getInstance() { + return INSTANCE; + } + + private long lastLocRaw = -1; + private JsonObject locraw = null; + + @SubscribeEvent + public void onWorldChange(WorldEvent.Load event) { + lastLocRaw = -1; + locraw = null; + } + + @SubscribeEvent + public void onChatMessage(ClientChatReceivedEvent event) { + if(event.message.getUnformattedText().startsWith("{")) { + try { + JsonObject obj = NotEnoughUpdates.INSTANCE.manager.gson.fromJson(event.message.getUnformattedText(), JsonObject.class); + if(obj.has("server") && obj.has("gametype") && obj.has("mode") && obj.has("map")) { + locraw = obj; + event.setCanceled(true); + } + } catch(Exception e) { + e.printStackTrace(); + } + } + } + + public String getLocation() { + if(locraw == null) { + return null; + } + return locraw.get("mode").getAsString(); + } + + public void tick() { + if(locraw == null && (System.currentTimeMillis() - lastLocRaw) > 20000) { + lastLocRaw = System.currentTimeMillis(); + NotEnoughUpdates.INSTANCE.sendChatMessage("/locraw"); + } + + try { + Scoreboard scoreboard = Minecraft.getMinecraft().thePlayer.getWorldScoreboard(); + + ScoreObjective sidebarObjective = scoreboard.getObjectiveInDisplaySlot(1); //§707/14/20 + + List<Score> scores = new ArrayList<>(); + for(Score score : scoreboard.getSortedScores(sidebarObjective)) { + scores.add(score); + } + List<String> lines = new ArrayList<>(); + for(int i=scores.size()-1; i>=0; i--) { + Score score = scores.get(i); + ScorePlayerTeam scoreplayerteam1 = scoreboard.getPlayersTeam(score.getPlayerName()); + String line = ScorePlayerTeam.formatPlayerName(scoreplayerteam1, score.getPlayerName()); + line = Utils.cleanDuplicateColourCodes(line); + lines.add(line); + } + if(lines.size() >= 5) { + date = Utils.cleanColour(lines.get(1)).trim(); + //§74:40am + Matcher matcher = timePattern.matcher(lines.get(2)); + if(matcher.find()) { + time = Utils.cleanColour(matcher.group()).trim(); + try { + String timeSpace = time.replace("am", " am").replace("pm", " pm"); + SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a"); + currentTimeDate = parseFormat.parse(timeSpace); + } catch (ParseException e) {} + } + location = Utils.cleanColour(lines.get(3)).replaceAll("[^A-Za-z0-9() ]", "").trim(); + } + objective = null; + + boolean objTextLast = false; + for(String line : lines) { + if(objTextLast) { + objective = line; + } + + objTextLast = line.equals("Objective"); + } + } catch(Exception e) { + e.printStackTrace(); + } + } + +} |
