Refactor climbplugin

This commit is contained in:
Scott Burns
2019-05-16 00:43:22 +02:00
parent 15c22ccf0c
commit d39d103bc2
2 changed files with 66 additions and 36 deletions

View File

@@ -1,5 +1,7 @@
package net.runelite.client.plugins.climbupclimbdown; package net.runelite.client.plugins.climbupclimbdown;
import javax.inject.Inject;
import javax.inject.Singleton;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -12,18 +14,16 @@ import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginType; import net.runelite.client.plugins.PluginType;
import javax.inject.Inject;
import javax.inject.Singleton;
@PluginDescriptor( @PluginDescriptor(
name = "Climb Up Climb Down", name = "Climb Up Climb Down",
description = "Hold Shift to Climb up, Ctrl to Climb down", description = "Hold Shift to Climb up, Ctrl to Climb down",
tags = {"climb", "stairs", "ladder", "swap", "key", "input"}, tags = {"climb", "stairs", "ladder", "swap", "key", "input"},
type = PluginType.UTILITY type = PluginType.UTILITY
) )
@Slf4j @Slf4j
@Singleton @Singleton
public class ClimbPlugin extends Plugin { public class ClimbPlugin extends Plugin
{
@Inject @Inject
Client client; Client client;
@@ -37,32 +37,45 @@ public class ClimbPlugin extends Plugin {
@Getter @Getter
@Setter @Setter
private boolean isHoldingShift; private boolean isHoldingShift;
@Getter @Getter
@Setter @Setter
private boolean isHoldingCtrl; private boolean isHoldingCtrl;
@Override @Override
protected void startUp() throws Exception { protected void startUp() throws Exception
{
enableCustomization(); enableCustomization();
} }
@Override @Override
protected void shutDown() throws Exception { protected void shutDown() throws Exception
{
disableCustomization(); disableCustomization();
} }
@Subscribe @Subscribe
public void onMenuEntryAdded(MenuEntryAdded menuEntryAdded) { public void onMenuEntryAdded(MenuEntryAdded menuEntryAdded)
try { {
if (menuEntryAdded.getOption().equalsIgnoreCase("climb")) { try
if (isHoldingCtrl ^ isHoldingShift) { {
if (menuEntryAdded.getOption().equalsIgnoreCase("climb"))
{
if (isHoldingCtrl ^ isHoldingShift)
{
if (isHoldingShift) if (isHoldingShift)
{
stripExceptFor("climb-up"); stripExceptFor("climb-up");
}
if (isHoldingCtrl) if (isHoldingCtrl)
{
stripExceptFor("climb-down"); stripExceptFor("climb-down");
}
} }
} }
} catch (Exception e) { }
catch (Exception e)
{
log.error("Uh oh!", e); log.error("Uh oh!", e);
} }
} }
@@ -78,16 +91,21 @@ public class ClimbPlugin extends Plugin {
keyManager.unregisterKeyListener(inputListener); keyManager.unregisterKeyListener(inputListener);
} }
private void stripExceptFor(String option) { private void stripExceptFor(String option)
{
MenuEntry[] newEntries = new MenuEntry[1]; MenuEntry[] newEntries = new MenuEntry[1];
for (MenuEntry entry : client.getMenuEntries()) for (MenuEntry entry : client.getMenuEntries())
{
if (entry.getOption().equalsIgnoreCase(option)) if (entry.getOption().equalsIgnoreCase(option))
{
newEntries[0] = entry; newEntries[0] = entry;
}
}
if (newEntries[0] != null) if (newEntries[0] != null)
{
client.setMenuEntries(newEntries); client.setMenuEntries(newEntries);
}
} }
} }

View File

@@ -1,48 +1,60 @@
package net.runelite.client.plugins.climbupclimbdown; package net.runelite.client.plugins.climbupclimbdown;
import java.awt.event.KeyEvent;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.client.input.KeyListener; import net.runelite.client.input.KeyListener;
import javax.inject.Inject;
import java.awt.event.KeyEvent;
@Slf4j @Slf4j
public class ShiftCtrlInputListener implements KeyListener { public class ShiftCtrlInputListener implements KeyListener
{
@Inject
Client client;
@Inject @Inject
ClimbPlugin plugin; ClimbPlugin plugin;
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e)
{
} }
@Override @Override
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e)
switch (e.getKeyCode()) { {
switch (e.getKeyCode())
{
case KeyEvent.VK_SHIFT: case KeyEvent.VK_SHIFT:
if (plugin.isHoldingShift()) return; if (plugin.isHoldingShift())
{
return;
}
plugin.setHoldingShift(true); plugin.setHoldingShift(true);
break; break;
case KeyEvent.VK_CONTROL: case KeyEvent.VK_CONTROL:
if (plugin.isHoldingCtrl()) return; if (plugin.isHoldingCtrl())
{
return;
}
plugin.setHoldingCtrl(true); plugin.setHoldingCtrl(true);
break; break;
} }
} }
@Override @Override
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e)
switch (e.getKeyCode()) { {
switch (e.getKeyCode())
{
case KeyEvent.VK_SHIFT: case KeyEvent.VK_SHIFT:
if (!plugin.isHoldingShift()) return; if (!plugin.isHoldingShift())
{
return;
}
plugin.setHoldingShift(false); plugin.setHoldingShift(false);
break; break;
case KeyEvent.VK_CONTROL: case KeyEvent.VK_CONTROL:
if (!plugin.isHoldingCtrl()) return; if (!plugin.isHoldingCtrl())
{
return;
}
plugin.setHoldingCtrl(false); plugin.setHoldingCtrl(false);
break; break;
} }