diff --git a/build.gradle b/build.gradle index b6628a2..a15c191 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,3 @@ - -// For those who want the bleeding edge buildscript { repositories { jcenter() @@ -9,29 +7,40 @@ buildscript { } } dependencies { + classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4' classpath 'net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT' } } -apply plugin: 'net.minecraftforge.gradle.forge' -apply plugin: 'java' +plugins { + id "com.github.johnrengelman.shadow" version "1.2.3" +} -sourceCompatibility = 1.8 -targetCompatibility = 1.8 +apply plugin: 'net.minecraftforge.gradle.forge' -version = "1.0" +compileJava { + sourceCompatibility = '1.8' + targetCompatibility = '1.8' +} +version = "1.0.0" group= "io.github.symt" archivesBaseName = "ZealotCounter" - - minecraft { version = "1.8.9-11.15.1.2318-1.8.9" runDir = "run" mappings = "stable_20" } -processResources -{ +repositories { + jcenter() +} + + +dependencies { + compile group: 'org.json', name: 'json', version: '20190722' +} + +processResources { inputs.property "version", project.version inputs.property "mcversion", project.minecraft.version from(sourceSets.main.resources.srcDirs) { @@ -42,3 +51,12 @@ processResources exclude 'mcmod.info' } } + +shadowJar { + classifier = "" + + relocate "org.json", "io.github.symt.repack.org.json" +} + +reobf { shadowJar { mappingType = "SEARGE" } } +tasks.reobfShadowJar.mustRunAfter shadowJar \ No newline at end of file diff --git a/out/production/zealot-counter.main/io/github/symt/EventHandler.class b/out/production/zealot-counter.main/io/github/symt/EventHandler.class index 88fc91b..e965667 100644 Binary files a/out/production/zealot-counter.main/io/github/symt/EventHandler.class and b/out/production/zealot-counter.main/io/github/symt/EventHandler.class differ diff --git a/out/production/zealot-counter.main/io/github/symt/ZealotCounter.class b/out/production/zealot-counter.main/io/github/symt/ZealotCounter.class index 5119bc9..eb7716d 100644 Binary files a/out/production/zealot-counter.main/io/github/symt/ZealotCounter.class and b/out/production/zealot-counter.main/io/github/symt/ZealotCounter.class differ diff --git a/out/production/zealot-counter.main/io/github/symt/ZealotCounterCommand.class b/out/production/zealot-counter.main/io/github/symt/ZealotCounterCommand.class index fc221a6..3bda454 100644 Binary files a/out/production/zealot-counter.main/io/github/symt/ZealotCounterCommand.class and b/out/production/zealot-counter.main/io/github/symt/ZealotCounterCommand.class differ diff --git a/src/main/java/io/github/symt/EventHandler.java b/src/main/java/io/github/symt/EventHandler.java index 61f2348..715e4cb 100644 --- a/src/main/java/io/github/symt/EventHandler.java +++ b/src/main/java/io/github/symt/EventHandler.java @@ -1,9 +1,17 @@ package io.github.symt; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; import java.text.DecimalFormat; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.entity.monster.EntityEnderman; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.ChatComponentTranslation; +import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.MovingObjectPosition.MovingObjectType; import net.minecraftforge.client.event.ClientChatReceivedEvent; @@ -13,6 +21,7 @@ import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.network.FMLNetworkEvent; +import org.json.JSONObject; public class EventHandler { @@ -20,28 +29,33 @@ public class EventHandler { private FontRenderer renderer = Minecraft.getMinecraft().fontRendererObj; private int attackedEntity = -1; private int prevEntity = -1; + private long lastHit = 0; @SubscribeEvent(priority = EventPriority.HIGH) public void onMobDeath(LivingDeathEvent event) { + System.out.println(event.entity.getCustomNameTag()); MovingObjectPosition objectMouseOver = Minecraft.getMinecraft().objectMouseOver; if (((objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.ENTITY && objectMouseOver.entityHit.getEntityId() == event.entity.getEntityId()) || attackedEntity == event.entity.getEntityId()) && (event.entity.getName().substring(2).equals("Enderman") || event.entity instanceof EntityEnderman) - && prevEntity != event.entity.getEntityId()) { + && prevEntity != event.entity.getEntityId() + && System.currentTimeMillis() - lastHit < 150 + && ZealotCounter.dragonsNest) { prevEntity = event.entity.getEntityId(); ZealotCounter.zealotCount++; + ZealotCounter.sinceLastEye++; } } @SubscribeEvent(priority = EventPriority.HIGH) public void onAttack(AttackEntityEvent event) { - if (Minecraft.getMinecraft().thePlayer.worldObj.isRemote - && event.entity.getEntityId() == Minecraft.getMinecraft().thePlayer.getEntityId() && + if (event.entity.getEntityId() == Minecraft.getMinecraft().thePlayer.getEntityId() && (event.target.getName().substring(2).equals("Enderman") - || event.target instanceof EntityEnderman)) { + || event.target instanceof EntityEnderman) && ZealotCounter.dragonsNest) { attackedEntity = event.target.getEntityId(); + lastHit = System.currentTimeMillis(); } } @@ -49,6 +63,7 @@ public void onAttack(AttackEntityEvent event) { public void onChatMessageReceived(ClientChatReceivedEvent e) { if (e.message.getUnformattedText().equals("A special Zealot has spawned nearby!")) { ZealotCounter.summoningEyes++; + ZealotCounter.sinceLastEye = 0; } } @@ -57,8 +72,63 @@ public void onPlayerJoinEvent(FMLNetworkEvent.ClientConnectedToServerEvent event if (firstJoin) { ZealotCounter.loggedIn = true; firstJoin = false; - } + Minecraft.getMinecraft().addScheduledTask(() -> { + new Thread(() -> { + try { + URL url = new URL("https://api.github.com/repos/symt/zealot-counter/releases/latest"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("User-Agent", "Mozilla/5.0"); + int responseCode = con.getResponseCode(); + + if (responseCode == 200) { + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + JSONObject json = new JSONObject(response.toString()); + String latest = ((String) json.get("tag_name")); + String[] latestTag = latest.split("\\."); + String current = ZealotCounter.VERSION; + String[] currentTag = current.split("\\."); + System.out.println(latestTag.length + " " + currentTag.length); + if (latestTag.length == 3 && currentTag.length == 3) { + for (int i = 0; i < latestTag.length; i++) { + if (latestTag[i].compareTo(currentTag[i]) != 0) { + Minecraft.getMinecraft().thePlayer + .addChatMessage(new ChatComponentTranslation("", new Object[0])); + if (latestTag[i].compareTo(currentTag[i]) <= -1) { + Minecraft.getMinecraft().thePlayer + .addChatMessage(new ChatComponentText(EnumChatFormatting.GREEN + + "You are currently on a pre-release build of ZealotCounter. Please report any bugs that you may come across")); + } else if (latestTag[i].compareTo(currentTag[i]) >= 1) { + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText( + EnumChatFormatting.GREEN + "You are currently on version " + + EnumChatFormatting.DARK_GREEN + current + EnumChatFormatting.GREEN + + " and the latest version is " + EnumChatFormatting.DARK_GREEN + + latest + EnumChatFormatting.GREEN + + ". Please update to the latest version of ZealotCounter.")); + } + Minecraft.getMinecraft().thePlayer + .addChatMessage(new ChatComponentTranslation("", new Object[0])); + break; + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + }).start(); + }); + } } @SubscribeEvent @@ -76,18 +146,27 @@ public void renderGameOverlayEvent(RenderGameOverlayEvent event) { .format(ZealotCounter.zealotCount / (ZealotCounter.summoningEyes * 1.0d))); String zealot = "Zealots: " + ZealotCounter.zealotCount; String eye = "Eyes: " + ZealotCounter.summoningEyes; + String lastEye = "Zealots since last eye: " + ZealotCounter.sinceLastEye; + String longest = + (lastEye.length() > zealot.length() && lastEye.length() > zealotEye.length()) ? lastEye + : (zealot.length() > zealotEye.length()) ? zealot : zealotEye; if (ZealotCounter.align.equals("right")) { renderer.drawString(zealot, ZealotCounter.guiLocation[0] + - renderer.getStringWidth(zealotEye) - + renderer.getStringWidth(longest) - renderer.getStringWidth(zealot), ZealotCounter.guiLocation[1], ZealotCounter.color, true); - renderer.drawString(eye, - ZealotCounter.guiLocation[0] + - renderer.getStringWidth(zealotEye) - + renderer.drawString(eye, ZealotCounter.guiLocation[0] + + renderer.getStringWidth(longest) - renderer.getStringWidth(eye), ZealotCounter.guiLocation[1] + renderer.FONT_HEIGHT, ZealotCounter.color, true); - renderer.drawString(zealotEye, ZealotCounter.guiLocation[0], + renderer.drawString(zealotEye, ZealotCounter.guiLocation[0] + + renderer.getStringWidth(longest) - + renderer.getStringWidth(zealotEye), ZealotCounter.guiLocation[1] + renderer.FONT_HEIGHT * 2, ZealotCounter.color, true); + renderer.drawString(lastEye, ZealotCounter.guiLocation[0] + + renderer.getStringWidth(longest) - + renderer.getStringWidth(lastEye), + ZealotCounter.guiLocation[1] + renderer.FONT_HEIGHT * 3, ZealotCounter.color, true); } else { renderer.drawString(zealot, ZealotCounter.guiLocation[0], ZealotCounter.guiLocation[1], ZealotCounter.color, true); @@ -96,6 +175,8 @@ public void renderGameOverlayEvent(RenderGameOverlayEvent event) { ZealotCounter.guiLocation[1] + renderer.FONT_HEIGHT, ZealotCounter.color, true); renderer.drawString(zealotEye, ZealotCounter.guiLocation[0], ZealotCounter.guiLocation[1] + renderer.FONT_HEIGHT * 2, ZealotCounter.color, true); + renderer.drawString(lastEye, ZealotCounter.guiLocation[0], + ZealotCounter.guiLocation[1] + renderer.FONT_HEIGHT * 3, ZealotCounter.color, true); } } } diff --git a/src/main/java/io/github/symt/ZealotCounter.java b/src/main/java/io/github/symt/ZealotCounter.java index 650b1dd..9b199a6 100644 --- a/src/main/java/io/github/symt/ZealotCounter.java +++ b/src/main/java/io/github/symt/ZealotCounter.java @@ -1,14 +1,24 @@ package io.github.symt; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import net.minecraft.client.Minecraft; -import net.minecraft.util.MovingObjectPosition; +import net.minecraft.scoreboard.Score; +import net.minecraft.scoreboard.ScoreObjective; +import net.minecraft.scoreboard.ScorePlayerTeam; +import net.minecraft.scoreboard.Scoreboard; +import net.minecraft.util.StringUtils; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Mod; @@ -19,31 +29,49 @@ public class ZealotCounter { static final String MODID = "ZealotCounter"; - static final String VERSION = "1.0"; + static final String VERSION = "1.0.0"; private static final String ZEALOT_PATH = "zealotcounter.dat"; static boolean loggedIn = false; + static boolean dragonsNest = false; static int color = 0x55FFFF; static String align = "left"; static int zealotCount = 0; static int summoningEyes = 0; + static int sinceLastEye = 0; static int[] guiLocation = new int[2]; + private static void scheduleNestCheck() { + Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> { + List scoreboard = getSidebarLines(); + boolean found = false; + for (String s : scoreboard) { + if (StringUtils.stripControlCodes(s).contains("Nest")) { + found = true; + break; + } + } + dragonsNest = found; + }, 0, 5, TimeUnit.SECONDS); + } + private static void scheduleFileSave() { Executors.newSingleThreadScheduledExecutor() .scheduleAtFixedRate(() -> { if (loggedIn) { - saveZealotInfo(zealotCount, summoningEyes); + saveZealotInfo(zealotCount, summoningEyes, sinceLastEye); } }, 0, 2, TimeUnit.MINUTES); } - static void saveZealotInfo(int zealots, int eyes) { + static void saveZealotInfo(int zealots, int eyes, int last) { new Thread(() -> { File zealot_file = new File(ZEALOT_PATH); try { FileWriter fw = new FileWriter(zealot_file, false); - fw.write(zealots + "," + eyes + "," + guiLocation[0] + "," + guiLocation[1] + "," + Integer - .toHexString(color) + "," + align); + fw.write( + zealots + "," + eyes + "," + last + "," + guiLocation[0] + "," + guiLocation[1] + "," + + Integer + .toHexString(color) + "," + align); fw.close(); } catch (IOException e) { e.printStackTrace(); @@ -74,6 +102,39 @@ static boolean isInteger(String s, int radix) { return true; } + private static List getSidebarLines() { + List lines = new ArrayList<>(); + Scoreboard scoreboard = Minecraft.getMinecraft().theWorld.getScoreboard(); + if (scoreboard == null) { + return lines; + } + + ScoreObjective objective = scoreboard.getObjectiveInDisplaySlot(1); + + if (objective == null) { + return lines; + } + + Collection scores = scoreboard.getSortedScores(objective); + List list = Lists.newArrayList(scores.stream() + .filter(input -> input != null && input.getPlayerName() != null && !input.getPlayerName() + .startsWith("#")) + .collect(Collectors.toList())); + + if (list.size() > 15) { + scores = Lists.newArrayList(Iterables.skip(list, scores.size() - 15)); + } else { + scores = list; + } + + for (Score score : scores) { + ScorePlayerTeam team = scoreboard.getPlayersTeam(score.getPlayerName()); + lines.add(ScorePlayerTeam.formatPlayerName(team, score.getPlayerName())); + } + + return lines; + } + @EventHandler public void init(FMLInitializationEvent event) { ClientCommandHandler.instance.registerCommand(new ZealotCounterCommand()); @@ -81,22 +142,24 @@ public void init(FMLInitializationEvent event) { if (new File(ZEALOT_PATH).isFile()) { try { String[] input = new BufferedReader(new FileReader(ZEALOT_PATH)).readLine().split(","); - if (input.length == 6 && isInteger(input[0]) && isInteger(input[1]) && isInteger(input[2]) - && isInteger(input[3]) && isInteger(input[4], 16)) { + if (input.length == 7 && isInteger(input[0]) && isInteger(input[1]) && isInteger(input[2]) + && isInteger(input[3]) && isInteger(input[4]) && isInteger(input[5], 16)) { zealotCount = Integer.parseInt(input[0]); summoningEyes = Integer.parseInt(input[1]); - guiLocation = new int[]{Integer.parseInt(input[2]), Integer.parseInt(input[3])}; - color = Integer.parseInt(input[4], 16); - align = input[5]; + sinceLastEye = Integer.parseInt(input[2]); + guiLocation = new int[]{Integer.parseInt(input[3]), Integer.parseInt(input[4])}; + color = Integer.parseInt(input[5], 16); + align = input[6]; } else { - saveZealotInfo(0, 0); + saveZealotInfo(0, 0, 0); } } catch (IOException e) { e.printStackTrace(); } } else { - saveZealotInfo(0, 0); + saveZealotInfo(0, 0, 0); } scheduleFileSave(); + scheduleNestCheck(); } } diff --git a/src/main/java/io/github/symt/ZealotCounterCommand.java b/src/main/java/io/github/symt/ZealotCounterCommand.java index 7b5ebf3..fa77cbc 100644 --- a/src/main/java/io/github/symt/ZealotCounterCommand.java +++ b/src/main/java/io/github/symt/ZealotCounterCommand.java @@ -25,7 +25,8 @@ public void processCommand(ICommandSender ics, String[] args) { if (args.length == 3 && args[0].equalsIgnoreCase("location") && ZealotCounter .isInteger(args[1]) && ZealotCounter.isInteger(args[2])) { ZealotCounter.guiLocation = new int[]{Integer.parseInt(args[1]), Integer.parseInt(args[2])}; - ZealotCounter.saveZealotInfo(ZealotCounter.zealotCount, ZealotCounter.summoningEyes); + ZealotCounter.saveZealotInfo(ZealotCounter.zealotCount, ZealotCounter.summoningEyes, + ZealotCounter.sinceLastEye); } else if (args.length == 2 && args[0].equalsIgnoreCase("align")) { ZealotCounter.align = (args[1].equalsIgnoreCase("right")) ? "right" : "left"; } else if (args.length == 2 && args[0].equalsIgnoreCase("color") && ZealotCounter @@ -34,11 +35,12 @@ public void processCommand(ICommandSender ics, String[] args) { } else if (args.length == 1 && args[0].equalsIgnoreCase("reset")) { ZealotCounter.summoningEyes = 0; ZealotCounter.zealotCount = 0; - ZealotCounter.saveZealotInfo(0, 0); + ZealotCounter.saveZealotInfo(0, 0, 0); player.addChatMessage( new ChatComponentText(EnumChatFormatting.GREEN + "ZealotCounter has been reset")); } else if (args.length == 1 && args[0].equalsIgnoreCase("save")) { - ZealotCounter.saveZealotInfo(ZealotCounter.zealotCount, ZealotCounter.summoningEyes); + ZealotCounter.saveZealotInfo(ZealotCounter.zealotCount, ZealotCounter.summoningEyes, + ZealotCounter.sinceLastEye); player.addChatMessage( new ChatComponentText(EnumChatFormatting.GREEN + "ZealotCounter has been saved")); } else {