diff options
author | ThatGravyBoat <thatgravyboat@gmail.com> | 2021-07-15 01:36:50 -0230 |
---|---|---|
committer | ThatGravyBoat <thatgravyboat@gmail.com> | 2021-07-15 01:36:50 -0230 |
commit | 718740e85f48b57bf712a62f811cd4d34dd98450 (patch) | |
tree | 8d3c7751cf684fe98e513010233f7b7b46e3e351 /src/main/java/com/thatgravyboat/skyblockhud/tracker | |
parent | 89fa98d85546eb93064ce4a2b66e79341750b577 (diff) | |
download | SkyblockHud-Death-Defied-718740e85f48b57bf712a62f811cd4d34dd98450.tar.gz SkyblockHud-Death-Defied-718740e85f48b57bf712a62f811cd4d34dd98450.tar.bz2 SkyblockHud-Death-Defied-718740e85f48b57bf712a62f811cd4d34dd98450.zip |
Updated important locations
Updated Config System (Thanks Moulberry)
Added Trackers Back with kill trackers
Diffstat (limited to 'src/main/java/com/thatgravyboat/skyblockhud/tracker')
3 files changed, 203 insertions, 157 deletions
diff --git a/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerFileLoader.java b/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerFileLoader.java index 624ca60..804ac60 100644 --- a/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerFileLoader.java +++ b/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerFileLoader.java @@ -4,108 +4,55 @@ import com.google.gson.*; import com.thatgravyboat.skyblockhud.location.Locations; import java.io.*; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; +import java.util.EnumSet; import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.Locale; import net.minecraft.client.Minecraft; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTBase; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import net.minecraft.util.ResourceLocation; public class TrackerFileLoader { private static final Gson gson = new GsonBuilder().create(); - public static ItemStack getDisplayItem(JsonObject jsonObject) { - int meta = jsonObject.get("meta").getAsInt(); - String displayItemId = jsonObject.get("item").getAsString(); - Item item = Item.itemRegistry.getObject(new ResourceLocation(displayItemId)); - ItemStack stack = new ItemStack(item, 0, meta); - if (jsonObject.has("skullData") && displayItemId.equals("minecraft:skull") && meta == 3) { - stack.setTagInfo("SkullOwner", getSkullTag(jsonObject.getAsJsonObject("skullData"))); - } - if (jsonObject.has("enchanted") && jsonObject.get("enchanted").getAsBoolean()) stack.setTagInfo("ench", new NBTTagList()); - return stack; - } - - public static NBTBase getSkullTag(JsonObject skullObject) { - NBTTagCompound skullOwner = new NBTTagCompound(); - NBTTagCompound properties = new NBTTagCompound(); - NBTTagList textures = new NBTTagList(); - NBTTagCompound value = new NBTTagCompound(); - - skullOwner.setString("Id", skullObject.get("id").getAsString()); - - value.setString("Value", skullObject.get("texture").getAsString()); - textures.appendTag(value); - - properties.setTag("textures", textures); - - skullOwner.setTag("Properties", properties); - return skullOwner; - } - private static void loadTrackers(JsonObject object) { for (JsonElement element : object.get("trackers").getAsJsonArray()) { JsonObject tracker = element.getAsJsonObject(); - StringBuilder builder = new StringBuilder(); - tracker.get("location").getAsJsonArray().forEach(loc -> builder.append(loc.getAsString())); - String location = builder.toString(); - - Map<String, ItemStack> stacks = new HashMap<>(); - for (JsonElement drop : tracker.get("drops").getAsJsonArray()) { - JsonObject dropObject = drop.getAsJsonObject(); - - //Display Item Creation - ItemStack stack = getDisplayItem(dropObject.getAsJsonObject("displayItem")); - String itemId = dropObject.get("id").getAsString(); - - stacks.put(itemId, stack); + EnumSet<Locations> locations = EnumSet.noneOf(Locations.class); + tracker.get("location").getAsJsonArray().forEach(l -> { + Locations location = Locations.get(l.getAsString().toUpperCase(Locale.ENGLISH)); + if (location != Locations.DEFAULT){ + locations.add(location); + } + }); + if (tracker.has("drops")) { + for (JsonElement drop : tracker.get("drops").getAsJsonArray()) { + TrackerHandler.trackerObjects.add(new TrackerObject(drop.getAsJsonObject(), locations)); + } } - - String event = tracker.has("event") ? tracker.get("event").getAsString() : null; - - Map<String, Map<String, ItemStack>> events = new HashMap<>(); - events.put(event, stacks); - - if (TrackerHandler.trackers.containsKey(location)) { - TrackerHandler.trackers.get(location).dropTrackers.put(event, stacks); - } else { - TrackerHandler.trackers.putIfAbsent(location, new TrackerHandler.TrackerData(events)); + if (tracker.has("mobs")) { + for (JsonElement mob : tracker.get("mobs").getAsJsonArray()) { + TrackerHandler.trackerObjects.add(new TrackerObject(mob.getAsJsonObject(), locations)); + } } + } - tracker.get("location").getAsJsonArray().forEach(loc -> TrackerHandler.trackerIds.put(Locations.get(loc.getAsString()), location)); + for (TrackerObject trackerObject : TrackerHandler.trackerObjects) { + for (Locations location : trackerObject.getLocations()) { + if (TrackerHandler.trackers.containsKey(location)){ + TrackerHandler.trackers.get(location).put(trackerObject.getInternalId(), trackerObject); + }else { + HashMap<String, TrackerObject> value = new HashMap<>(); + value.put(trackerObject.getInternalId(), trackerObject); + TrackerHandler.trackers.put(location, value); + } + } } - } - private static JsonElement getTrackerFile() { - List<JsonObject> trackerStats = new ArrayList<>(); - TrackerHandler.trackers.forEach( - (locations, trackerData) -> - trackerData.dropTrackers.forEach( - (event, drops) -> { - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("location", locations); - - if (event == null) jsonObject.add("event", new JsonNull()); else jsonObject.addProperty("event", event); - - JsonObject dropsData = new JsonObject(); - drops.forEach((s, stack) -> dropsData.addProperty(s, stack.stackSize)); - jsonObject.add("drops", dropsData); - trackerStats.add(jsonObject); - } - ) - ); - JsonArray stats = new JsonArray(); - trackerStats.forEach(stats::add); - return stats; } public static void loadTrackersFile() { + TrackerHandler.trackers.clear(); + TrackerHandler.trackerObjects.clear(); try { ResourceLocation trackers = new ResourceLocation("skyblockhud:data/trackers.json"); InputStream is = Minecraft.getMinecraft().getResourceManager().getResource(trackers).getInputStream(); @@ -116,6 +63,22 @@ public class TrackerFileLoader { } catch (Exception ignored) {} } + private static JsonElement getTrackerFile() { + JsonArray stats = new JsonArray(); + TrackerHandler.trackerObjects.forEach((trackerObject) ->{ + if (trackerObject.getCount() > 0) { + JsonObject jsonObject = new JsonObject(); + JsonArray locations = new JsonArray(); + trackerObject.getLocations().forEach(l -> locations.add(new JsonPrimitive(l.toString().toUpperCase(Locale.ENGLISH)))); + jsonObject.add("id", new JsonPrimitive(trackerObject.getInternalId())); + jsonObject.add("locations", locations); + jsonObject.add("count", new JsonPrimitive(trackerObject.getCount())); + stats.add(jsonObject); + } + }); + return stats; + } + public static boolean loadTrackerStatsFile(File configDirectory) { File configFile = new File(configDirectory, "sbh-trackers-stats.json"); @@ -127,31 +90,29 @@ public class TrackerFileLoader { try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) { JsonObject json = gson.fromJson(reader, JsonObject.class); if (json.has("trackerStats")) { - json - .getAsJsonArray("trackerStats") - .forEach( - element -> { + json.getAsJsonArray("trackerStats").forEach(element -> { if (element.isJsonObject()) { JsonObject object = element.getAsJsonObject(); - String location = object.get("location").getAsString(); - Map<String, Map<String, ItemStack>> trackers = TrackerHandler.trackers.get(location).dropTrackers; - - JsonElement event = object.get("event"); - String eventString = event == null || event.isJsonNull() ? null : event.getAsString(); - Map<String, ItemStack> drops = trackers.get(eventString); - - if (drops != null) { - for (Map.Entry<String, JsonElement> drop : object.getAsJsonObject("drops").entrySet()) { - if (drops.containsKey(drop.getKey())) { - drops.get(drop.getKey()).stackSize = drop.getValue().getAsInt(); - } - } - drops = TrackerHandler.sortTrackers(drops); - trackers.put(eventString, drops); + JsonArray locations = object.get("locations").getAsJsonArray(); + Locations firstLocation = null; + for (JsonElement location : locations) { + firstLocation = Locations.get(location.getAsString()); + if (!firstLocation.equals(Locations.DEFAULT)) break; + } + + if (firstLocation != null && !firstLocation.equals(Locations.DEFAULT)){ + TrackerHandler.trackers.get(firstLocation).get(object.get("id").getAsString()).setCount(object.get("count").getAsInt()); } } - } + }); + + TrackerHandler.trackers.forEach((location, map) -> { + TrackerHandler.trackers.put(location, + TrackerHandler.sortTrackers(map, + (entry1, entry2) -> Integer.compare(entry2.getValue().getCount(), entry1.getValue().getCount()) + ) ); + }); } } } catch (Exception ignored) {} diff --git a/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerHandler.java b/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerHandler.java index c40924a..9832c4b 100644 --- a/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerHandler.java +++ b/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerHandler.java @@ -2,10 +2,10 @@ package com.thatgravyboat.skyblockhud.tracker; import com.thatgravyboat.skyblockhud.SkyblockHud; import com.thatgravyboat.skyblockhud.Utils; +import com.thatgravyboat.skyblockhud.api.events.SkyBlockEntityKilled; import com.thatgravyboat.skyblockhud.core.config.Position; import com.thatgravyboat.skyblockhud.location.LocationHandler; import com.thatgravyboat.skyblockhud.location.Locations; -import com.thatgravyboat.skyblockhud.seasons.SeasonDateHandler; import java.util.*; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Gui; @@ -16,57 +16,37 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; -public class TrackerHandler { +public class TrackerHandler extends Gui { - public static class TrackerData { + public static Set<TrackerObject> trackerObjects = new HashSet<>(); + public static Map<Locations, Map<String, TrackerObject>> trackers = new HashMap<>(); - public Map<String, Map<String, ItemStack>> dropTrackers; + public static <K,V> Map<K, V> sortTrackers(Map<K, V> map, Comparator<? super Map.Entry<K, V>> comparator) { + List<Map.Entry<K, V>> list = new ArrayList<>(map.entrySet()); + list.sort(comparator); - public TrackerData(Map<String, Map<String, ItemStack>> trackers) { - this.dropTrackers = trackers; - } - - public String getDropId(String event) { - if (event == null || event.isEmpty() || !eventGoing() || !dropTrackers.containsKey(event.toLowerCase().trim())) return null; - return event.toLowerCase().trim(); - } - - private boolean eventGoing() { - return SeasonDateHandler.getCurrentEventTime().trim().toLowerCase().contains("ends in"); - } - } - - public static Map<String, TrackerData> trackers = new HashMap<>(); - public static Map<Locations, String> trackerIds = new HashMap<>(); - - public static Map<String, ItemStack> sortTrackers(Map<String, ItemStack> map) { - List<Map.Entry<String, ItemStack>> list = new ArrayList<>(map.entrySet()); - list.sort((entry1, entry2) -> Integer.compare(entry2.getValue().stackSize, entry1.getValue().stackSize)); - - Map<String, ItemStack> result = new LinkedHashMap<>(); - for (Map.Entry<String, ItemStack> entry : list) { + Map<K, V> result = new LinkedHashMap<>(); + for (Map.Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue()); } return result; } - public static void onItemAdded(String id, int amount, String enchant, int level) { - if (SkyblockHud.hasSkyblockScoreboard() && trackerIds.containsKey(LocationHandler.getCurrentLocation())) { - String trackerId = trackerIds.get(LocationHandler.getCurrentLocation()); - TrackerData tracked = trackers.get(trackerId); - String dropTrackerId = tracked.getDropId(SeasonDateHandler.getCurrentEvent()); - Map<String, ItemStack> tracker = tracked.dropTrackers.get(dropTrackerId); + public static void onItemAdded(String id, int amount, String specialId, int number) { + if (SkyblockHud.hasSkyblockScoreboard() && trackers.containsKey(LocationHandler.getCurrentLocation())) { + Map<String, TrackerObject> trackerMap = trackers.get(LocationHandler.getCurrentLocation()); String dropId = id; - if (enchant != null) { - dropId = enchant.toUpperCase() + ";" + level; + if (specialId != null) { + dropId = specialId.toUpperCase() + ";" + number; } - if (tracker != null && tracker.containsKey(dropId)) { - ItemStack stack = tracker.get(dropId); - stack.stackSize += amount; - tracked.dropTrackers.put(dropTrackerId, sortTrackers(tracker)); + if (trackerMap != null && trackerMap.containsKey(dropId)) { + TrackerObject object = trackerMap.get(dropId); + object.increaseCount(amount); + trackers.put(LocationHandler.getCurrentLocation(), sortTrackers(trackerMap, (entry1, entry2) -> Integer.compare(entry2.getValue().getCount(), entry1.getValue().getCount()))); } + } } @@ -81,36 +61,47 @@ public class TrackerHandler { } @SubscribeEvent + public void onSbEntityDeath(SkyBlockEntityKilled event){ + System.out.println(event.id); + if (SkyblockHud.hasSkyblockScoreboard() && trackers.containsKey(LocationHandler.getCurrentLocation())) { + Map<String, TrackerObject> trackerMap = trackers.get(LocationHandler.getCurrentLocation()); + if (trackerMap.containsKey("ENTITY:"+event.id)){ + TrackerObject object = trackerMap.get("ENTITY:"+event.id); + object.increaseCount(); + trackers.put(LocationHandler.getCurrentLocation(), sortTrackers(trackerMap, (entry1, entry2) -> Integer.compare(entry2.getValue().getCount(), entry1.getValue().getCount()))); + } + } + } + + @SubscribeEvent public void renderOverlay(RenderGameOverlayEvent.Post event) { - if (Utils.overlayShouldRender(event.type, SkyblockHud.hasSkyblockScoreboard(), trackerIds.containsKey(LocationHandler.getCurrentLocation())/*,!SkyblockHud.config.trackers.hideTracker*/)) { - String trackerId = trackerIds.get(LocationHandler.getCurrentLocation()); + if (Utils.overlayShouldRender(event.type, SkyblockHud.hasSkyblockScoreboard(), trackers.containsKey(LocationHandler.getCurrentLocation()),!SkyblockHud.config.trackers.hideTracker)) { + Map<String, TrackerObject> tracker = trackers.get(LocationHandler.getCurrentLocation()); Minecraft mc = Minecraft.getMinecraft(); - TrackerData tracked = trackers.get(trackerId); - Map<String, ItemStack> tracker = tracked.dropTrackers.get(tracked.getDropId(SeasonDateHandler.getCurrentEvent())); if (tracker != null) { - Position pos = null; // SkyblockHud.config.trackers.trackerPosition; - int startPos = pos.getAbsX(event.resolution, (tracker.size() >= 6 ? 120 : tracker.size() * 20)); + Position pos = SkyblockHud.config.trackers.trackerPosition; + int startPos = pos.getAbsX(event.resolution, (tracker.size() >= 6 ? 130 : tracker.size() * 20)); int y = pos.getAbsY(event.resolution, (int) (10 + Math.ceil(tracker.size() / 5d) * 20)); - Gui.drawRect(startPos, y, startPos + 120, y + 10, -1072689136); + Gui.drawRect(startPos, y, startPos + 130, y + 10, -1072689136); mc.fontRendererObj.drawString("Tracker", startPos + 4, y + 1, 0xffffff, false); y += 10; - Gui.drawRect(startPos, y, startPos + (tracker.size() >= 6 ? 120 : tracker.size() * 20), (int) (y + (Math.ceil(tracker.size() / 5d) * 20)), 1610612736); - int x = startPos; - for (ItemStack stack : tracker.values()) { - String s = String.valueOf(stack.stackSize); + Gui.drawRect(startPos, y, startPos + (tracker.size() >= 6 ? 130 : (tracker.size() * 20)+10), (int) (y + (Math.ceil(tracker.size() / 5d) * 20)), 1610612736); + int x = startPos + 5; + for (TrackerObject object : tracker.values()) { + String s = Utils.formattedNumber(object.getCount(), 1000); GlStateManager.disableLighting(); GlStateManager.enableDepth(); - drawItemStack(stack, x, y); + drawItemStack(object.getDisplayStack(), x, y); GlStateManager.disableDepth(); GlStateManager.disableBlend(); - mc.fontRendererObj.drawStringWithShadow(s, (float) (x + 19 - 2 - mc.fontRendererObj.getStringWidth(s)), (float) (y + 9), stack.stackSize < 1 ? 16733525 : 16777215); + mc.fontRendererObj.drawStringWithShadow(s, (float) (x + 19 - 2 - mc.fontRendererObj.getStringWidth(s)), (float) (y + 9), object.getCount() < 1 ? 16733525 : 16777215); GlStateManager.enableBlend(); GlStateManager.enableDepth(); - if ((x - startPos) / 20 == 5) { - x = startPos; + if ((x - startPos + 5) / 20 == 5) { + x = startPos + 5; y += 20; } else { x += 20; diff --git a/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerObject.java b/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerObject.java new file mode 100644 index 0000000..58034d5 --- /dev/null +++ b/src/main/java/com/thatgravyboat/skyblockhud/tracker/TrackerObject.java @@ -0,0 +1,94 @@ +package com.thatgravyboat.skyblockhud.tracker; + +import com.google.gson.JsonObject; +import com.thatgravyboat.skyblockhud.location.Locations; +import java.util.EnumSet; +import java.util.Locale; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.ResourceLocation; + +public class TrackerObject { + + private final ItemStack stack; + private final String internalId; + private final EnumSet<Locations> locations; + private final boolean isEntity; + private int count; + + public TrackerObject(JsonObject jsonObject, EnumSet<Locations> locations){ + this.stack = decodeToItemStack(jsonObject); + this.internalId = jsonObject.get("id").getAsString(); + this.isEntity = jsonObject.get("id").getAsString().contains("entity:"); + this.locations = locations; + } + + public static ItemStack decodeToItemStack(JsonObject jsonObject){ + jsonObject = jsonObject.getAsJsonObject("displayItem"); + int meta = jsonObject.get("meta").getAsInt(); + ResourceLocation itemid = new ResourceLocation(jsonObject.get("item").getAsString()); + ItemStack stack = new ItemStack(Item.itemRegistry.getObject(itemid), 0, meta); + if (jsonObject.has("displayName")) stack.setStackDisplayName(jsonObject.get("displayName").getAsString()); + if (jsonObject.has("skullData") && itemid.getResourcePath().equals("skull") && meta == 3) { + stack.setTagInfo("SkullOwner", getSkullTag(jsonObject.getAsJsonObject("skullData"))); + } + if (jsonObject.has("enchanted") && jsonObject.get("enchanted").getAsBoolean()) { + stack.setTagInfo("ench", new NBTTagList()); + } + return stack; + } + + public static NBTBase getSkullTag(JsonObject skullObject) { + NBTTagCompound skullOwner = new NBTTagCompound(); + NBTTagCompound properties = new NBTTagCompound(); + NBTTagList textures = new NBTTagList(); + NBTTagCompound value = new NBTTagCompound(); + + skullOwner.setString("Id", skullObject.get("id").getAsString()); + + value.setString("Value", skullObject.get("texture").getAsString()); + textures.appendTag(value); + + properties.setTag("textures", textures); + + skullOwner.setTag("Properties", properties); + return skullOwner; + } + + public void increaseCount(int amount) { + count+=amount; + } + + public void increaseCount() { + count++; + } + + public void setCount(int count) { + this.count = count; + } + + public int getCount() { + return count; + } + + public ItemStack getDisplayStack() { + return stack; + } + + public EnumSet<Locations> getLocations() { + return locations; + } + + public String getInternalId() { + return internalId.toUpperCase(Locale.ENGLISH); + } + + public boolean isEntity() { + return isEntity; + } + + +} |