ItemChargePlugin extends Plugin { @Subscribe public void onChatMessage(ChatMessage event) { if (event.getType() == ChatMessageType.GAMEMESSAGE || event.getType() == ChatMessageType.SPAM) { String message = event.getMessage(); Matcher dodgyCheckMatcher = DODGY_CHECK_PATTERN.matcher(message); Matcher dodgyProtectMatcher = DODGY_PROTECT_PATTERN.matcher(message); Matcher dodgyBreakMatcher = DODGY_BREAK_PATTERN.matcher(message); Matcher bindingNecklaceCheckMatcher = BINDING_CHECK_PATTERN.matcher(message); Matcher bindingNecklaceUsedMatcher = BINDING_USED_PATTERN.matcher(message); Matcher ringOfForgingCheckMatcher = RING_OF_FORGING_CHECK_PATTERN.matcher(message); Matcher amuletOfChemistryCheckMatcher = AMULET_OF_CHEMISTRY_CHECK_PATTERN.matcher(message); Matcher amuletOfChemistryUsedMatcher = AMULET_OF_CHEMISTRY_USED_PATTERN.matcher(message); Matcher amuletOfChemistryBreakMatcher = AMULET_OF_CHEMISTRY_BREAK_PATTERN.matcher(message); Matcher amuletOfBountyCheckMatcher = AMULET_OF_BOUNTY_CHECK_PATTERN.matcher(message); Matcher amuletOfBountyUsedMatcher = AMULET_OF_BOUNTY_USED_PATTERN.matcher(message); if (config.recoilNotification() && message.contains(RING_OF_RECOIL_BREAK_MESSAGE)) { notifier.notify("Your Ring of Recoil has shattered"); } else if (dodgyBreakMatcher.find()) { if (config.dodgyNotification()) { notifier.notify("Your dodgy necklace has crumbled to dust."); } updateDodgyNecklaceCharges(MAX_DODGY_CHARGES); } else if (dodgyCheckMatcher.find()) { updateDodgyNecklaceCharges(Integer.parseInt(dodgyCheckMatcher.group(1))); } else if (dodgyProtectMatcher.find()) { updateDodgyNecklaceCharges(Integer.parseInt(dodgyProtectMatcher.group(1))); } else if (amuletOfChemistryCheckMatcher.find()) { updateAmuletOfChemistryCharges(Integer.parseInt(amuletOfChemistryCheckMatcher.group(1))); } else if (amuletOfChemistryUsedMatcher.find()) { final String match = amuletOfChemistryUsedMatcher.group(1); int charges = 1; if (!match.equals("one")) { charges = Integer.parseInt(match); } updateAmuletOfChemistryCharges(charges); } else if (amuletOfChemistryBreakMatcher.find()) { updateAmuletOfChemistryCharges(MAX_AMULET_OF_CHEMISTRY_CHARGES); } else if (amuletOfBountyCheckMatcher.find()) { updateAmuletOfBountyCharges(Integer.parseInt(amuletOfBountyCheckMatcher.group(1))); } else if (amuletOfBountyUsedMatcher.find()) { updateAmuletOfBountyCharges(Integer.parseInt(amuletOfBountyUsedMatcher.group(1))); } else if (message.equals(AMULET_OF_BOUNTY_BREAK_TEXT)) { updateAmuletOfBountyCharges(MAX_AMULET_OF_BOUNTY_CHARGES); } else if (message.contains(BINDING_BREAK_TEXT)) { if (config.bindingNotification()) { notifier.notify(BINDING_BREAK_TEXT); } updateBindingNecklaceCharges(MAX_BINDING_CHARGES + 1); } else if (bindingNecklaceUsedMatcher.find()) { updateBindingNecklaceCharges(config.bindingNecklace() - 1); } else if (bindingNecklaceCheckMatcher.find()) { final String match = bindingNecklaceCheckMatcher.group(1); int charges = 1; if (!match.equals("one")) { charges = Integer.parseInt(match); } updateBindingNecklaceCharges(charges); } else if (ringOfForgingCheckMatcher.find()) { final String match = ringOfForgingCheckMatcher.group(1); int charges = 1; if (!match.equals("one")) { charges = Integer.parseInt(match); } updateRingOfForgingCharges(charges); } else if (message.equals(RING_OF_FORGING_USED_TEXT)) { final ItemContainer equipment = client.getItemContainer(InventoryID.EQUIPMENT); if (equipment == null) { return; } if (equipment.contains(ItemID.RING_OF_FORGING)) { int charges = Ints.constrainToRange(config.ringOfForging() - 1, 0, MAX_RING_OF_FORGING_CHARGES); updateRingOfForgingCharges(charges); } } else if (message.equals(RING_OF_FORGING_BREAK_TEXT)) { if (config.ringOfForgingNotification()) { notifier.notify("Your ring of forging has melted."); } updateRingOfForgingCharges(MAX_RING_OF_FORGING_CHARGES); } } }  @Subscribe void onConfigChanged(ConfigChanged event); @Subscribe void onChatMessage(ChatMessage event); @Subscribe void onItemContainerChanged(ItemContainerChanged event); @Subscribe void onWidgetLoaded(WidgetLoaded widgetLoaded);  }
@Test public void testOnChatMessage() { ChatMessage chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", CHECK, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).dodgyNecklace(eq(10)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", PROTECT, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).dodgyNecklace(eq(9)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", PROTECT_1, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).dodgyNecklace(eq(1)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", BREAK, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).dodgyNecklace(eq(10)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", CHECK_RING_OF_FORGING_ONE, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).ringOfForging(eq(1)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", CHECK_RING_OF_FORGING_FULL, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).ringOfForging(eq(140)); reset(config); when(config.ringOfForging()).thenReturn(90); ItemContainer equipmentItemContainer = mock(ItemContainer.class); when(client.getItemContainer(eq(InventoryID.EQUIPMENT))).thenReturn(equipmentItemContainer); when(equipmentItemContainer.contains(ItemID.RING_OF_FORGING)).thenReturn(true); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", USED_RING_OF_FORGING, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).ringOfForging(eq(89)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", BREAK_RING_OF_FORGING, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).ringOfForging(eq(140)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", CHECK_AMULET_OF_CHEMISTRY, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(5)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", CHECK_AMULET_OF_CHEMISTRY_1, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(1)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", USED_AMULET_OF_CHEMISTRY, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(4)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", USED_AMULET_OF_CHEMISTRY_3_DOSES, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(2)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", USED_AMULET_OF_CHEMISTRY_2_DOSES, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(1)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", BREAK_AMULET_OF_CHEMISTRY, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(5)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", BREAK_AMULET_OF_CHEMISTRY_3_DOSES, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(5)); reset(config); chatMessage = new ChatMessage(null, ChatMessageType.GAMEMESSAGE, "", BREAK_AMULET_OF_CHEMISTRY_2_DOSES, "", 0); itemChargePlugin.onChatMessage(chatMessage); verify(config).amuletOfChemistry(eq(5)); reset(config); }