-
Notifications
You must be signed in to change notification settings - Fork 2
Support for Lightbearer and reorder preaches when new preaches happen #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ private GodbookOverlay(GodbookPlugin plugin) | |
| public Dimension render(Graphics2D graphics) | ||
| { | ||
| String title = "Tick:"; | ||
| HashMap<String, Integer> players = plugin.getPlayers(); | ||
| HashMap<String, Preach> players = plugin.getPlayers(); | ||
|
|
||
| panelComponent.getChildren().clear(); | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not something you changed, but since we're going to update this: |
||
|
|
@@ -44,15 +44,19 @@ public Dimension render(Graphics2D graphics) | |
| return panelComponent.render(graphics); | ||
| } | ||
|
|
||
| private int getMaxWidth(Graphics2D graphics, HashMap<String, Integer> players, String title) | ||
| private int getMaxWidth(Graphics2D graphics, HashMap<String, Preach> players, String title) | ||
| { | ||
| String longestKey = Collections.max(players.keySet(), Comparator.comparingInt(String::length)); | ||
| return graphics.getFontMetrics().stringWidth(longestKey) + graphics.getFontMetrics().stringWidth(title); | ||
| return graphics.getFontMetrics().stringWidth(longestKey) + graphics.getFontMetrics().stringWidth(title) + graphics.getFontMetrics().stringWidth("(157)"); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Format this line so it's not as long |
||
| } | ||
|
|
||
| private void addPlayerToOverlay(String playerName, Integer ticks) | ||
| private void addPlayerToOverlay(String playerName, Preach preach) | ||
| { | ||
| panelComponent.getChildren().add(LineComponent.builder().left(playerName).right(Integer.toString(ticks)).build()); | ||
| panelComponent.getChildren().add(LineComponent.builder() | ||
| .leftColor(preach.preachedWithRing ? Color.YELLOW : Color.WHITE) | ||
| .left(playerName) | ||
| .right(Integer.toString(preach.tick) + (!preach.preachedWithRing && preach.ringTick != 0 ? " (" + Integer.toString(preach.ringTick) + ")" : "")) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract the logic in the right hand side of this out of the builder |
||
| .build()); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,18 @@ | |
| import net.runelite.api.*; | ||
| import net.runelite.api.events.AnimationChanged; | ||
| import net.runelite.api.events.GameTick; | ||
| import net.runelite.api.events.VarbitChanged; | ||
| import net.runelite.api.gameval.VarbitID; | ||
| import net.runelite.client.config.ConfigManager; | ||
| import net.runelite.client.eventbus.Subscribe; | ||
| import net.runelite.client.party.PartyService; | ||
| import net.runelite.client.party.WSClient; | ||
| import net.runelite.client.plugins.Plugin; | ||
| import net.runelite.client.plugins.PluginDescriptor; | ||
| import net.runelite.client.ui.overlay.OverlayManager; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Slf4j | ||
| @PluginDescriptor( | ||
|
|
@@ -24,7 +29,7 @@ | |
| public class GodbookPlugin extends Plugin | ||
| { | ||
| @Getter(AccessLevel.MODULE) | ||
| private LinkedHashMap<String, Integer> players; | ||
| private LinkedHashMap<String, Preach> players; | ||
|
|
||
| @Inject | ||
| private Client client; | ||
|
|
@@ -35,6 +40,12 @@ public class GodbookPlugin extends Plugin | |
| @Inject | ||
| private OverlayManager overlayManager; | ||
|
|
||
| @Inject | ||
| private PartyService partyService; | ||
|
|
||
| @Inject | ||
| private WSClient wsClient; | ||
|
|
||
| @Inject | ||
| private GodbookConfig config; | ||
|
|
||
|
|
@@ -48,24 +59,44 @@ GodbookConfig provideConfig(ConfigManager configManager) | |
| protected void startUp() throws Exception | ||
| { | ||
| players = new LinkedHashMap<>(); | ||
| wsClient.registerMessage(RingChangeMessage.class); | ||
| overlayManager.add(overlay); | ||
| } | ||
|
|
||
| @Override | ||
| protected void shutDown() throws Exception | ||
| { | ||
| overlayManager.remove(overlay); | ||
| wsClient.unregisterMessage(RingChangeMessage.class); | ||
| players.clear(); | ||
| } | ||
|
|
||
| @Subscribe | ||
| public void onAnimationChanged(final AnimationChanged event) | ||
| { | ||
| int animId = event.getActor().getAnimation(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this after creating PreachAnimationID |
||
| if (config.theatreOnly() && !isInTheatreOfBlood()) | ||
| return; | ||
|
|
||
| if (GodbookAnimationID.isGodbookAnimation(event.getActor().getAnimation())) | ||
| players.put(event.getActor().getName(), 0); | ||
| if (GodbookAnimationID.isGodbookAnimation(animId) || isHornAnimation(animId)) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename GodbookAnimationID to PreachAnimationID and add the horn values |
||
| boolean isLightbearerPreach = false; | ||
| String name = event.getActor().getName(); | ||
|
|
||
| if (event.getActor().getName().equalsIgnoreCase(client.getLocalPlayer().getName())) { | ||
| if (config.lightbearerSupport()) { | ||
| // This is preaching, check our equipment. | ||
| try { | ||
| Item item = client.getItemContainer(InventoryID.EQUIPMENT) | ||
| .getItem(EquipmentInventorySlot.RING.getSlotIdx()); | ||
| isLightbearerPreach = item.getId() == ItemID.LIGHTBEARER; | ||
| } catch (NullPointerException _e) { | ||
| isLightbearerPreach = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| doNewPreach(name, isLightbearerPreach, 0); | ||
| } | ||
| } | ||
|
|
||
| @Subscribe | ||
|
|
@@ -74,11 +105,96 @@ public void onGameTick(GameTick event) | |
| if (players.isEmpty()) | ||
| return; | ||
|
|
||
| // Check if the localPlayer equipped a Lightbearer | ||
| if (config.lightbearerSupport() && players.containsKey(client.getLocalPlayer().getName())) { | ||
| Preach localPreach = players.get(client.getLocalPlayer().getName()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| Item item; | ||
| try { | ||
| item = client.getItemContainer(InventoryID.EQUIPMENT).getItem(EquipmentInventorySlot.RING.getSlotIdx()); | ||
| } catch (NullPointerException _e) { | ||
| item = null; | ||
| } | ||
|
Comment on lines
+112
to
+117
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make a function for this, it's done more than once
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also prefer checking for nulls instead of try-catching NPEs
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. InventoryID is also deprecated, use the GameVal form of it |
||
|
|
||
| if (item != null && item.getId() == ItemID.LIGHTBEARER) { | ||
|
|
||
| if (localPreach.ringTick == 0) { | ||
| localPreach.ringTick = findLowestPreach(); | ||
|
|
||
| if (partyService.isInParty()) { | ||
| partyService.send(new RingChangeMessage(client.getLocalPlayer().getName(), 1, localPreach.ringTick)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a new RingChangeMessage (and format it), then call |
||
| } | ||
| } | ||
| } else { | ||
| if (partyService.isInParty() && localPreach.ringTick != 0) { | ||
| partyService.send(new RingChangeMessage(client.getLocalPlayer().getName(), 0, 0)); | ||
| } | ||
|
|
||
| localPreach.ringTick = 0; | ||
| } | ||
| } | ||
|
|
||
| players.entrySet() | ||
| .forEach(i -> i.setValue(i.getValue() + 1)); | ||
| .forEach(i -> i.setValue(i.getValue().advance(config.backwardsCount()))); | ||
|
|
||
| players.entrySet() | ||
| .removeIf(i -> i.getValue() >= config.maxTicks()); | ||
| .removeIf(this::shouldRemove); | ||
| } | ||
|
|
||
| public void doNewPreach(String name, boolean isLightbearerPreach, int offset) { | ||
| players.put(name, | ||
| new Preach(0, isLightbearerPreach, name, startTick() + offset)); | ||
|
|
||
| // Re-order in descending order so that the lowest preach tick always appears | ||
| // at the bottom of the list. | ||
| players = players.entrySet() | ||
| .stream() | ||
| .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) | ||
| .collect(Collectors.toMap( | ||
| Map.Entry::getKey, | ||
| Map.Entry::getValue, | ||
| (oldValue, newValue) -> newValue, LinkedHashMap::new)); | ||
| } | ||
|
|
||
| @Subscribe | ||
| public void onRingChangeMessage(RingChangeMessage e) { | ||
| if (!e.n.equalsIgnoreCase(client.getLocalPlayer().getName())) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer an early return to avoid nesting |
||
| players.entrySet().forEach(p -> { | ||
| if (p.getKey().equalsIgnoreCase(e.n)) { | ||
| Preach remotePreach = p.getValue(); | ||
| remotePreach.ringTick = e.r == 1 ? e.t : 0; | ||
| p.setValue(remotePreach); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private int findLowestPreach() { | ||
| // Find the lowest preach | ||
| final int[] lowestPreach = {Integer.MAX_VALUE}; | ||
| players.entrySet().forEach(i -> { | ||
| if (lowestPreach[0] > i.getValue().tick) { | ||
| lowestPreach[0] = i.getValue().tick; | ||
| } | ||
| }); | ||
|
|
||
| return lowestPreach[0]; | ||
| } | ||
|
Comment on lines
+172
to
+182
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole function looks weird. It should look something like: private int findLowestPreach() {
return players.values().stream()
.mapToInt(preach -> preach.tick)
.min().orElse(-1);
} |
||
|
|
||
| private boolean shouldRemove(Map.Entry<String, Preach> p) { | ||
| if (config.backwardsCount()) { | ||
| return p.getValue().tick < 0; | ||
| } else { | ||
| return p.getValue().tick >= config.maxTicks(); | ||
| } | ||
| } | ||
|
|
||
| private boolean isHornAnimation(int animId) { | ||
| return config.hornAnimId() != 0 && animId == config.hornAnimId(); | ||
| } | ||
|
|
||
| private int startTick() { | ||
| return config.backwardsCount() ? 150 : 0; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 150 should not be hardcoded here, it should be a config option |
||
| } | ||
|
|
||
| private boolean isInTheatreOfBlood() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.infernostats; | ||
|
|
||
| public class Preach implements Comparable<Preach> { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs |
||
| public int tick; | ||
| public String name; | ||
| public boolean preachedWithRing; | ||
| public int ringTick; | ||
|
|
||
| public Preach(int ringTick, boolean preachedWithRing, String name, int tick) { | ||
| this.ringTick = ringTick; | ||
| this.preachedWithRing = preachedWithRing; | ||
| this.name = name; | ||
| this.tick = tick; | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Preach p) { | ||
| return Integer.compare(p.tick, this.tick); | ||
| } | ||
|
|
||
| public Preach advance(boolean isBackwards) { | ||
| this.tick += isBackwards ? -1 : 1; | ||
| return this; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.infernostats; | ||
|
|
||
| import net.runelite.client.party.messages.PartyMessage; | ||
|
|
||
| public class RingChangeMessage extends PartyMessage { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Lombok's |
||
| String n; | ||
| int r; | ||
| int t; | ||
|
|
||
| public RingChangeMessage(String n, int r, int t) { | ||
| this.n = n; | ||
| this.r = r; | ||
| this.t = t; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't PR things that are still a work in progress