Skip to content

Commit

Permalink
fix bot load error №3
Browse files Browse the repository at this point in the history
  • Loading branch information
IldarW committed Mar 2, 2019
1 parent c097e64 commit 5a25d5d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 45 deletions.
2 changes: 2 additions & 0 deletions main/java/net/ildar/wurm/BotController.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ private synchronized boolean isInstantiated(Class<? extends Bot> botClass) {
return activeBots.stream().anyMatch(bot -> bot.getClass().equals(botClass));
}

//this method is being invoked from com.wurmonline.client.renderer.cell.GroundItemCellRenderable
@SuppressWarnings("WeakerAccess")
public synchronized <T extends Bot> T getInstance(Class<T> botClass) {
T instance = null;
try {
Expand Down
92 changes: 51 additions & 41 deletions main/java/net/ildar/wurm/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,46 @@
import java.util.logging.Logger;

public class Mod implements WurmClientMod, Initable, Configurable, PreInitable {
public static HeadsUpDisplay hud;
public static List<WurmComponent> components;
private final long BLESS_TIMEOUT = 1800000;

private static Logger logger;
private static Map<ConsoleCommand, ConsoleCommandHandler> consoleCommandHandlers;
public static HeadsUpDisplay hud;
private static Mod instance;

private static final long BLESS_TIMEOUT = 1800000;
private static long lastBless = 0L;
private static boolean noBlessings = false;
public List<WurmComponent> components;
private Logger logger;
private Map<ConsoleCommand, ConsoleCommandHandler> consoleCommandHandlers;
private long lastBless = 0L;
private boolean noBlessings = false;

static {
public Mod() {
logger = Logger.getLogger("IldarMod");
consoleCommandHandlers = new HashMap<>();
consoleCommandHandlers.put(ConsoleCommand.sleep, Mod::handleSleepCommand);
consoleCommandHandlers.put(ConsoleCommand.look, Mod::handleLookCommand);
consoleCommandHandlers.put(ConsoleCommand.sleep, this::handleSleepCommand);
consoleCommandHandlers.put(ConsoleCommand.look, this::handleLookCommand);
consoleCommandHandlers.put(ConsoleCommand.combine, input -> handleCombineCommand());
consoleCommandHandlers.put(ConsoleCommand.move, Mod::handleMoveCommand);
consoleCommandHandlers.put(ConsoleCommand.move, this::handleMoveCommand);
consoleCommandHandlers.put(ConsoleCommand.stabilize, input -> Utils.stabilizePlayer());
consoleCommandHandlers.put(ConsoleCommand.bot, BotController.getInstance()::handleInput);
consoleCommandHandlers.put(ConsoleCommand.mts, Mod::handleMtsCommand);
consoleCommandHandlers.put(ConsoleCommand.info, Mod::handleInfoCommand);
consoleCommandHandlers.put(ConsoleCommand.bot, this::handleBotCommand);
consoleCommandHandlers.put(ConsoleCommand.mts, this::handleMtsCommand);
consoleCommandHandlers.put(ConsoleCommand.info, this::handleInfoCommand);
consoleCommandHandlers.put(ConsoleCommand.actionlist, input -> showActionList());
consoleCommandHandlers.put(ConsoleCommand.action, Mod::handleActionCommand);
consoleCommandHandlers.put(ConsoleCommand.action, this::handleActionCommand);
consoleCommandHandlers.put(ConsoleCommand.getid, input -> copyIdToClipboard());
consoleCommandHandlers.put(ConsoleCommand.mtcenter, input -> Utils.moveToCenter());
consoleCommandHandlers.put(ConsoleCommand.mtcorner, input -> Utils.moveToNearestCorner());
consoleCommandHandlers.put(ConsoleCommand.stabilizelook, input -> Utils.stabilizeLook());
Mod.instance = this;
}

public static Mod getInstance() {
return instance;
}

/**
* Handle console commands
*/
@SuppressWarnings("unused")
public static boolean handleInput(final String cmd, final String[] data) {
public boolean handleInput(final String cmd, final String[] data) {
ConsoleCommand consoleCommand = ConsoleCommand.getByName(cmd);
if (consoleCommand == ConsoleCommand.unknown)
return false;
Expand All @@ -81,7 +87,11 @@ public static boolean handleInput(final String cmd, final String[] data) {
return true;
}

private static void printConsoleCommandUsage(ConsoleCommand consoleCommand) {
private void handleBotCommand(String[] input) {
BotController.getInstance().handleInput(input);
}

private void printConsoleCommandUsage(ConsoleCommand consoleCommand) {
if (consoleCommand == ConsoleCommand.look) {
Utils.consolePrint("Usage: " + ConsoleCommand.look.name() + " {" + getCardinalDirectionsList() + "}");
return;
Expand All @@ -93,7 +103,7 @@ private static void printConsoleCommandUsage(ConsoleCommand consoleCommand) {
Utils.consolePrint("Usage: " + consoleCommand.name() + " " + consoleCommand.getUsage());
}

private static void copyIdToClipboard() {
private void copyIdToClipboard() {
int x = hud.getWorld().getClient().getXMouse();
int y = hud.getWorld().getClient().getYMouse();
long[] ids = hud.getCommandTargetsFrom(x, y);
Expand All @@ -111,7 +121,7 @@ private static void copyIdToClipboard() {
}
}

private static void handleInfoCommand(String [] input) {
private void handleInfoCommand(String [] input) {
if (input.length != 1) {
printConsoleCommandUsage(ConsoleCommand.info);
printAvailableConsoleCommands();
Expand All @@ -127,13 +137,13 @@ private static void handleInfoCommand(String [] input) {
Utils.consolePrint(command.description);
}

private static void showActionList() {
private void showActionList() {
for(Action action: Action.values()) {
Utils.consolePrint("\"" + action.abbreviation + "\" is to " + action.name() + " with tool \"" + action.toolName + "\"");
}
}

private static void handleActionCommand(String [] input) {
private void handleActionCommand(String [] input) {
if (input == null || input.length == 0) {
printConsoleCommandUsage(ConsoleCommand.action);
return;
Expand Down Expand Up @@ -171,7 +181,7 @@ private static void handleActionCommand(String [] input) {
}
}

private static void printItemInformation() {
private void printItemInformation() {
WurmComponent inventoryComponent = Utils.getTargetComponent(c -> c instanceof ItemListWindow || c instanceof InventoryWindow);
if (inventoryComponent == null) {
Utils.consolePrint("Didn't find an inventory");
Expand All @@ -194,15 +204,15 @@ private static void printItemInformation() {
printItemInfo(item);
}

private static void printTileInformation() {
private void printTileInformation() {
int checkedtiles[][] = Utils.getAreaCoordinates();
for (int[] checkedtile : checkedtiles) {
Tiles.Tile tileType = hud.getWorld().getNearTerrainBuffer().getTileType(checkedtile[0], checkedtile[1]);
Utils.consolePrint("Tile (" + checkedtile[0] + ", " + checkedtile[1] + ") " + tileType.tilename);
}
}

private static void printPlayerInformation() {
private void printPlayerInformation() {
Utils.consolePrint("Player \"" + hud.getWorld().getPlayer().getPlayerName() + "\"");
Utils.consolePrint("Stamina: " + hud.getWorld().getPlayer().getStamina());
Utils.consolePrint("Damage: " + hud.getWorld().getPlayer().getDamage());
Expand All @@ -213,7 +223,7 @@ private static void printPlayerInformation() {
Utils.consolePrint("Layer: " + hud.getWorld().getPlayerLayer());
}

private static void printItemInfo(InventoryMetaItem item) {
private void printItemInfo(InventoryMetaItem item) {
if (item == null) {
Utils.consolePrint("Null item");
return;
Expand All @@ -230,7 +240,7 @@ private static void printItemInfo(InventoryMetaItem item) {
Utils.consolePrint(" Color override:" + item.isColorOverride() + " Marked for update:" + item.isMarkedForUpdate() + " Unfinished:" + item.isUnfinished());
}

private static void handleMtsCommand(String []input) {
private void handleMtsCommand(String []input) {
if (input.length < 2) {
printConsoleCommandUsage(ConsoleCommand.mts);
return;
Expand All @@ -255,7 +265,7 @@ private static void handleMtsCommand(String []input) {
moveToSacrifice(input[0], favorLevel, coefficient);
}

private static void handleMoveCommand(String []input) {
private void handleMoveCommand(String []input) {
if (input.length == 1) {
try {
float d = Float.parseFloat(input[0]);
Expand All @@ -268,7 +278,7 @@ private static void handleMoveCommand(String []input) {
printConsoleCommandUsage(ConsoleCommand.move);
}

private static void handleCombineCommand() {
private void handleCombineCommand() {
long[] itemsToCombine = hud.getInventoryWindow().getInventoryListComponent().getSelectedCommandTargets();
if (itemsToCombine == null || itemsToCombine.length == 0) {
Utils.consolePrint("No selected items!");
Expand All @@ -277,7 +287,7 @@ private static void handleCombineCommand() {
hud.getWorld().getServerConnection().sendAction(itemsToCombine[0], itemsToCombine, PlayerAction.COMBINE);
}

private static void handleSleepCommand(String [] input) {
private void handleSleepCommand(String [] input) {
if (input.length == 1) {
try {
Thread.sleep(Long.parseLong(input[0]));
Expand All @@ -289,7 +299,7 @@ private static void handleSleepCommand(String [] input) {
} else printConsoleCommandUsage(ConsoleCommand.sleep);
}

private static void handleLookCommand(String []input) {
private void handleLookCommand(String []input) {
if (input.length == 1) {
CardinalDirection direction = CardinalDirection.getByName(input[0]);
if (direction == CardinalDirection.unknown) {
Expand All @@ -305,15 +315,15 @@ private static void handleLookCommand(String []input) {
printConsoleCommandUsage(ConsoleCommand.look);
}

private static String getCardinalDirectionsList() {
private String getCardinalDirectionsList() {
StringBuilder directions = new StringBuilder();
for(CardinalDirection direction : CardinalDirection.values())
directions.append(direction.name()).append("|");
directions.deleteCharAt(directions.length() - 1);
return directions.toString();
}

private static void printAvailableConsoleCommands() {
private void printAvailableConsoleCommands() {
StringBuilder commands = new StringBuilder();
for(ConsoleCommand consoleCommand : consoleCommandHandlers.keySet())
commands.append(consoleCommand.name()).append(", ");
Expand All @@ -323,7 +333,7 @@ private static void printAvailableConsoleCommands() {
}

//move items to altar for a sacrifice
private static void moveToSacrifice(String itemName, float favorLevel, float coefficient) {
private void moveToSacrifice(String itemName, float favorLevel, float coefficient) {
WurmComponent inventoryComponent = Utils.getTargetComponent(c -> c instanceof ItemListWindow || c instanceof InventoryWindow);
if (inventoryComponent == null) {
Utils.consolePrint("Didn't find an inventory under the mouse cursor");
Expand Down Expand Up @@ -394,15 +404,15 @@ public void configure(Properties properties) {
}
String noBlessings = properties.getProperty("NoBlessings");
if (noBlessings != null && noBlessings.equals("true"))
Mod.noBlessings = true;
this.noBlessings = true;
}

@Override
public void preInit() {
try {
final ClassPool classPool = HookManager.getInstance().getClassPool();
final CtClass ctWurmConsole = classPool.getCtClass("com.wurmonline.client.console.WurmConsole");
ctWurmConsole.getMethod("handleDevInput", "(Ljava/lang/String;[Ljava/lang/String;)Z").insertBefore("if (net.ildar.wurm.Mod.handleInput($1,$2)) return true;");
ctWurmConsole.getMethod("handleDevInput", "(Ljava/lang/String;[Ljava/lang/String;)Z").insertBefore("if (net.ildar.wurm.Mod.getInstance().handleInput($1,$2)) return true;");

final CtClass ctSocketConnection = classPool.getCtClass("com.wurmonline.communication.SocketConnection");
ctSocketConnection.getMethod("tickWriting", "(J)Z").insertBefore("net.ildar.wurm.Utils.serverCallLock.lock();");
Expand All @@ -424,17 +434,17 @@ public void preInit() {
CtClass cellRenderableClass = classPool.getCtClass("com.wurmonline.client.renderer.cell.GroundItemCellRenderable");
cellRenderableClass.defrost();
CtMethod cellRenderableInitializeMethod = CtNewMethod.make("public void initialize() {\n" +
" net.ildar.wurm.bot.GroundItemGetterBot gigBot = net.ildar.wurm.BotController.getInstance().getInstance(net.ildar.wurm.bot.GroundItemGetterBot.class);\n" +
" net.ildar.wurm.bot.Bot gigBot = net.ildar.wurm.BotController.getInstance().getInstance(net.ildar.wurm.bot.GroundItemGetterBot.class);\n" +
" if (gigBot != null) {\n" +
" gigBot.processNewItem(this);\n" +
" ((net.ildar.wurm.bot.GroundItemGetterBot)gigBot).processNewItem(this);\n" +
" }\n" +
" super.initialize();\n" +
" };", cellRenderableClass);
cellRenderableClass.addMethod(cellRenderableInitializeMethod);

} catch (Exception e) {
Mod.logger.log(Level.SEVERE, "Error loading mod", e);
Mod.logger.log(Level.SEVERE, e.toString());
logger.log(Level.SEVERE, "Error loading mod", e);
logger.log(Level.SEVERE, e.toString());
}
}

Expand Down Expand Up @@ -510,8 +520,8 @@ public void init() {
logger.info("Loaded");
}
catch (Exception e) {
Mod.logger.log(Level.SEVERE, "Error loading mod", e);
Mod.logger.log(Level.SEVERE, e.toString());
logger.log(Level.SEVERE, "Error loading mod", e);
logger.log(Level.SEVERE, e.toString());
}
}

Expand Down
4 changes: 2 additions & 2 deletions main/java/net/ildar/wurm/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ public static WurmComponent getTargetComponent(Function<WurmComponent, Boolean>
int x = Mod.hud.getWorld().getClient().getXMouse();
int y = Mod.hud.getWorld().getClient().getYMouse();
try {
for (int i = 0; i < Mod.components.size(); i++) {
WurmComponent wurmComponent = Mod.components.get(i);
for (int i = 0; i < Mod.getInstance().components.size(); i++) {
WurmComponent wurmComponent = Mod.getInstance().components.get(i);
if (wurmComponent.contains(x, y)) {
if (filter != null && !filter.apply(wurmComponent))
continue;
Expand Down
2 changes: 1 addition & 1 deletion main/java/net/ildar/wurm/bot/MinerBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void work() throws Exception{
waitOnPause();
if (shardsCombining) {
List<ItemListWindow> piles = new ArrayList<>();
for (WurmComponent wurmComponent : Mod.components)
for (WurmComponent wurmComponent : Mod.getInstance().components)
if (wurmComponent instanceof ItemListWindow
&& !(wurmComponent instanceof InventoryWindow)) {
if (Utils.getRootItem(ReflectionUtil.getPrivateField(wurmComponent,
Expand Down
2 changes: 1 addition & 1 deletion main/java/net/ildar/wurm/bot/PileCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ else if (groundItemData.getName().contains(targetItemName))

}
} catch (ConcurrentModificationException ignored) {}
for(WurmComponent wurmComponent : Mod.components) {
for(WurmComponent wurmComponent : Mod.getInstance().components) {
if (wurmComponent instanceof ItemListWindow) {
InventoryListComponent ilc = ReflectionUtil.getPrivateField(wurmComponent,
ReflectionUtil.getField(wurmComponent.getClass(), "component"));
Expand Down

0 comments on commit 5a25d5d

Please sign in to comment.