cooking plugin: add wine ferment timer

Co-authored-by: LucasChilders <lucas1757@gmail.com>
This commit is contained in:
Adam
2019-05-07 09:51:43 -04:00
committed by Adam
parent e40b1a7b10
commit e6113dc82a
5 changed files with 224 additions and 8 deletions

View File

@@ -42,4 +42,15 @@ public interface CookingConfig extends Config
{
return 5;
}
@ConfigItem(
position = 2,
keyName = "fermentTimer",
name = "Show wine fermenting timer",
description = "Conifgures if the timer before wines are fermented is shown."
)
default boolean fermentTimer()
{
return true;
}
}

View File

@@ -50,18 +50,26 @@ import net.runelite.client.ui.overlay.OverlayManager;
@PluginDependency(XpTrackerPlugin.class)
public class CookingPlugin extends Plugin
{
private static final String WINE_MESSAGE = "You squeeze the grapes into the jug";
@Inject
private CookingConfig config;
@Inject
private CookingOverlay cookingOverlay;
@Inject
private FermentTimerOverlay fermentTimerOverlay;
@Inject
private OverlayManager overlayManager;
@Getter(AccessLevel.PACKAGE)
private CookingSession cookingSession;
@Getter(AccessLevel.PACKAGE)
private FermentTimerSession fermentTimerSession;
@Provides
CookingConfig getConfig(ConfigManager configManager)
{
@@ -72,30 +80,47 @@ public class CookingPlugin extends Plugin
protected void startUp() throws Exception
{
cookingSession = null;
fermentTimerSession = null;
overlayManager.add(cookingOverlay);
overlayManager.add(fermentTimerOverlay);
}
@Override
protected void shutDown() throws Exception
{
overlayManager.remove(fermentTimerOverlay);
overlayManager.remove(cookingOverlay);
fermentTimerSession = null;
cookingSession = null;
}
@Subscribe
public void onGameTick(GameTick gameTick)
{
if (cookingSession == null || config.statTimeout() == 0)
if (config.statTimeout() == 0)
{
return;
}
Duration statTimeout = Duration.ofMinutes(config.statTimeout());
Duration sinceCut = Duration.between(cookingSession.getLastCookingAction(), Instant.now());
if (sinceCut.compareTo(statTimeout) >= 0)
if (cookingSession != null)
{
cookingSession = null;
Duration statTimeout = Duration.ofMinutes(config.statTimeout());
Duration sinceCut = Duration.between(cookingSession.getLastCookingAction(), Instant.now());
if (sinceCut.compareTo(statTimeout) >= 0)
{
cookingSession = null;
}
}
if (fermentTimerSession != null)
{
Duration statTimeout = Duration.ofMinutes(config.statTimeout());
Duration sinceCut = Duration.between(fermentTimerSession.getLastWineMakingAction(), Instant.now());
if (sinceCut.compareTo(statTimeout) >= 0)
{
fermentTimerSession = null;
}
}
}
@@ -109,12 +134,22 @@ public class CookingPlugin extends Plugin
final String message = event.getMessage();
if (message.startsWith(WINE_MESSAGE) && config.fermentTimer())
{
if (fermentTimerSession == null)
{
fermentTimerSession = new FermentTimerSession();
}
fermentTimerSession.updateLastWineMakingAction();
}
if (message.startsWith("You successfully cook")
|| message.startsWith("You successfully bake")
|| message.startsWith("You manage to cook")
|| message.startsWith("You roast a")
|| message.startsWith("You cook")
|| message.startsWith("You squeeze the grapes into the jug"))
|| message.startsWith(WINE_MESSAGE))
{
if (cookingSession == null)
{

View File

@@ -0,0 +1,101 @@
/*
* Copyright (c) 2019, Lucas C <lucas1757@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.cooking;
import com.google.inject.Inject;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.time.Duration;
import java.time.Instant;
import lombok.extern.slf4j.Slf4j;
import static net.runelite.api.AnimationID.COOKING_WINE;
import net.runelite.api.Client;
import static net.runelite.api.MenuAction.RUNELITE_OVERLAY_CONFIG;
import net.runelite.client.ui.overlay.Overlay;
import static net.runelite.client.ui.overlay.OverlayManager.OPTION_CONFIGURE;
import net.runelite.client.ui.overlay.OverlayMenuEntry;
import net.runelite.client.ui.overlay.OverlayPosition;
import net.runelite.client.ui.overlay.components.LineComponent;
import net.runelite.client.ui.overlay.components.PanelComponent;
import net.runelite.client.ui.overlay.components.TitleComponent;
@Slf4j
class FermentTimerOverlay extends Overlay
{
private static final int INITIAL_TIME = 12;
private final Client client;
private final CookingPlugin plugin;
private final PanelComponent panelComponent = new PanelComponent();
@Inject
private FermentTimerOverlay(final Client client, final CookingPlugin plugin)
{
super(plugin);
setPosition(OverlayPosition.TOP_LEFT);
this.client = client;
this.plugin = plugin;
getMenuEntries().add(new OverlayMenuEntry(RUNELITE_OVERLAY_CONFIG, OPTION_CONFIGURE, "Fermenting Timer overlay"));
}
@Override
public Dimension render(Graphics2D graphics)
{
FermentTimerSession session = plugin.getFermentTimerSession();
if (session == null)
{
return null;
}
panelComponent.getChildren().clear();
if (isMakingWine() || Duration.between(session.getLastWineMakingAction(), Instant.now()).getSeconds() < INITIAL_TIME)
{
panelComponent.getChildren().add(TitleComponent.builder()
.text("Making Wine")
.color(Color.GREEN)
.build());
panelComponent.getChildren().add(LineComponent.builder()
.left("Ferments in: ")
.right(String.valueOf(INITIAL_TIME - Duration.between(session.getLastWineMakingAction(), Instant.now()).getSeconds()))
.build());
}
else
{
panelComponent.getChildren().add(TitleComponent.builder()
.text("Wine Fermented")
.color(Color.ORANGE)
.build());
}
return panelComponent.render(graphics);
}
private boolean isMakingWine()
{
return (client.getLocalPlayer().getAnimation() == COOKING_WINE);
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2019, Lucas C <lucas1757@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.cooking;
import java.time.Instant;
import lombok.AccessLevel;
import lombok.Getter;
class FermentTimerSession
{
@Getter(AccessLevel.PACKAGE)
private Instant lastWineMakingAction;
void updateLastWineMakingAction()
{
this.lastWineMakingAction = Instant.now();
}
}

View File

@@ -33,6 +33,9 @@ import net.runelite.api.events.ChatMessage;
import net.runelite.client.ui.overlay.OverlayManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -64,6 +67,10 @@ public class CookingPluginTest
@Bind
CookingOverlay cookingOverlay;
@Mock
@Bind
FermentTimerOverlay fermentTimerOverlay;
@Mock
@Bind
OverlayManager overlayManager;
@@ -83,8 +90,30 @@ public class CookingPluginTest
cookingPlugin.onChatMessage(chatMessage);
}
CookingSession cookingSession = cookingPlugin.getSession();
CookingSession cookingSession = cookingPlugin.getCookingSession();
assertNotNull(cookingSession);
assertEquals(COOKING_MESSAGES.length, cookingSession.getCookAmount());
}
@Test
public void testFermentTimerOnChatMessage()
{
when(config.fermentTimer()).thenReturn(true);
ChatMessage chatMessage = new ChatMessage(null, ChatMessageType.SPAM, "", COOKING_MESSAGES[6], "", 0);
cookingPlugin.onChatMessage(chatMessage);
FermentTimerSession fermentTimerSession = cookingPlugin.getFermentTimerSession();
assertNotNull(fermentTimerSession);
}
@Test
public void testFermentTimerOnChatMessage_pluginDisabled()
{
when(config.fermentTimer()).thenReturn(false);
ChatMessage chatMessage = new ChatMessage(null, ChatMessageType.SPAM, "", COOKING_MESSAGES[6], "", 0);
cookingPlugin.onChatMessage(chatMessage);
FermentTimerSession fermentTimerSession = cookingPlugin.getFermentTimerSession();
assertNull(fermentTimerSession);
}
}