cooking plugin: add wine ferment timer

This commit is contained in:
Adam
2019-05-30 19:59:12 -04:00
parent 79e67a34bc
commit 59c1139b51
5 changed files with 175 additions and 3 deletions

View File

@@ -26,6 +26,7 @@ package net.runelite.api;
public class GraphicID
{
public static final int WINE_MAKE = 47;
public static final int SPLASH = 85;
public static final int TELEPORT = 111;
public static final int GREY_BUBBLE_TELEPORT = 86;

View File

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

View File

@@ -28,19 +28,27 @@ package net.runelite.client.plugins.cooking;
import com.google.inject.Provides;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import javax.inject.Inject;
import lombok.AccessLevel;
import lombok.Getter;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.GraphicID;
import net.runelite.api.ItemID;
import net.runelite.api.Player;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.GameTick;
import net.runelite.api.events.GraphicChanged;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.game.ItemManager;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDependency;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.xptracker.XpTrackerPlugin;
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
@PluginDescriptor(
name = "Cooking",
@@ -50,6 +58,9 @@ import net.runelite.client.ui.overlay.OverlayManager;
@PluginDependency(XpTrackerPlugin.class)
public class CookingPlugin extends Plugin
{
@Inject
private Client client;
@Inject
private CookingConfig config;
@@ -59,6 +70,12 @@ public class CookingPlugin extends Plugin
@Inject
private OverlayManager overlayManager;
@Inject
private InfoBoxManager infoBoxManager;
@Inject
private ItemManager itemManager;
@Getter(AccessLevel.PACKAGE)
private CookingSession session;
@@ -78,6 +95,7 @@ public class CookingPlugin extends Plugin
@Override
protected void shutDown() throws Exception
{
infoBoxManager.removeIf(FermentTimer.class::isInstance);
overlayManager.remove(overlay);
session = null;
}
@@ -99,6 +117,36 @@ public class CookingPlugin extends Plugin
}
}
@Subscribe
public void onGraphicChanged(GraphicChanged graphicChanged)
{
Player player = client.getLocalPlayer();
if (graphicChanged.getActor() != player)
{
return;
}
if (player.getGraphic() == GraphicID.WINE_MAKE && config.fermentTimer())
{
Optional<FermentTimer> fermentTimerOpt = infoBoxManager.getInfoBoxes().stream()
.filter(FermentTimer.class::isInstance)
.map(FermentTimer.class::cast)
.findAny();
if (fermentTimerOpt.isPresent())
{
FermentTimer fermentTimer = fermentTimerOpt.get();
fermentTimer.reset();
}
else
{
FermentTimer fermentTimer = new FermentTimer(itemManager.getImage(ItemID.JUG_OF_WINE), this);
infoBoxManager.addInfoBox(fermentTimer);
}
}
}
@Subscribe
public void onChatMessage(ChatMessage event)
{
@@ -113,8 +161,7 @@ public class CookingPlugin extends Plugin
|| 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("You cook"))
{
if (session == null)
{

View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* 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.awt.Color;
import java.awt.Image;
import java.time.Duration;
import java.time.Instant;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.ui.overlay.infobox.InfoBox;
final class FermentTimer extends InfoBox
{
private static final Duration FERMENT_TIME = Duration.ofMillis(13_800);
private Instant fermentTime;
FermentTimer(Image image, Plugin plugin)
{
super(image, plugin);
reset();
}
@Override
public String getText()
{
int seconds = timeUntilFerment();
return Integer.toString(seconds);
}
@Override
public Color getTextColor()
{
int seconds = timeUntilFerment();
return seconds <= 3 ? Color.RED : Color.WHITE;
}
@Override
public boolean cull()
{
int seconds = timeUntilFerment();
return seconds <= 0;
}
void reset()
{
fermentTime = Instant.now().plus(FERMENT_TIME);
}
private int timeUntilFerment()
{
return (int) Duration.between(Instant.now(), fermentTime).getSeconds();
}
}

View File

@@ -29,14 +29,24 @@ import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import javax.inject.Inject;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.GraphicID;
import net.runelite.api.Player;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.GraphicChanged;
import net.runelite.client.game.ItemManager;
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.Matchers.any;
import org.mockito.Mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
@@ -49,13 +59,24 @@ public class CookingPluginTest
"You cook the karambwan. It looks delicious.",
"You roast a lobster.",
"You cook a bass.",
"You squeeze the grapes into the jug. The wine begins to ferment.",
"You successfully bake a tasty garden pie."
};
@Inject
CookingPlugin cookingPlugin;
@Mock
@Bind
Client client;
@Mock
@Bind
InfoBoxManager infoBoxManager;
@Mock
@Bind
ItemManager itemManager;
@Mock
@Bind
CookingConfig config;
@@ -87,4 +108,20 @@ public class CookingPluginTest
assertNotNull(cookingSession);
assertEquals(COOKING_MESSAGES.length, cookingSession.getCookAmount());
}
@Test
public void testOnGraphicChanged()
{
Player player = mock(Player.class);
when(player.getGraphic()).thenReturn(GraphicID.WINE_MAKE);
when(config.fermentTimer()).thenReturn(true);
when(client.getLocalPlayer()).thenReturn(player);
GraphicChanged graphicChanged = new GraphicChanged();
graphicChanged.setActor(player);
cookingPlugin.onGraphicChanged(graphicChanged);
verify(infoBoxManager).addInfoBox(any(FermentTimer.class));
}
}