Skip to content

Commit

Permalink
Apply to suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Punikekk committed Oct 16, 2024
1 parent 62456e0 commit 3c1f98a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public NpcInfo getOrCreateNpcInfo(String name) {
if (info == null) {
info = new NpcInfo();

info.name = name;
info.radius = 560;
info.mapList.add(mapId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ private Config loadConfig(Path configFile, Path backupFile) {
// Didn't have any previous config, save this new one:
if (!failedConfig) saveConfig(config, configFile, backupFile);
}
// set name of npc on each config load
config.LOOT.NPC_INFOS.forEach((k, v) -> v.name = k);

return config;
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/github/manolo8/darkbot/config/NpcInfo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.github.manolo8.darkbot.config;

import com.github.manolo8.darkbot.Main;
import com.github.manolo8.darkbot.gui.utils.Strings;
import eu.darkbot.api.config.annotations.Configuration;
import eu.darkbot.api.config.annotations.Option;
import eu.darkbot.api.config.types.NpcFlag;
import eu.darkbot.api.game.items.ItemCategory;
import eu.darkbot.api.game.items.SelectableItem;
import eu.darkbot.api.managers.HeroItemsAPI;
import eu.darkbot.util.StringUtils;

import java.util.Collections;
import java.util.HashSet;
Expand All @@ -30,6 +30,7 @@ public class NpcInfo implements eu.darkbot.api.config.types.NpcInfo {
public Character attackFormation;
public ExtraNpcInfo extra = new ExtraNpcInfo();

public transient String name;
public transient String fuzzyName;
public transient int npcId;
public @Option.Ignore Set<Integer> mapList = new HashSet<>();
Expand Down Expand Up @@ -135,6 +136,16 @@ public void addMapId(int mapId) {
mapList.add(mapId);
}

@Override
public String getName() {
return name;
}

@Override
public String getFuzzyName() {
return fuzzyName == null ? fuzzyName = StringUtils.fuzzyNpcName(getName()) : fuzzyName;
}

// Keep a cache of the last searched id.
// If called repeatedly in a loop for the same flag, it avoids allocations for the string concat of class + name.
private static String lastSearchedId = "";
Expand All @@ -154,10 +165,6 @@ public static String getId(Enum<?> flag) {
return lastSearchedId = className.concat(name);
}

public String getFuzzyName(String originalName) {
return fuzzyName == null ? fuzzyName = Strings.fuzzyMatcher(originalName) : fuzzyName;
}

public static class ExtraNpcInfo {
private Set<String> flags = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.manolo8.darkbot.core.manager;

import com.github.manolo8.darkbot.Main;
import com.github.manolo8.darkbot.config.NpcExtra;
import com.github.manolo8.darkbot.config.NpcInfo;
import com.github.manolo8.darkbot.config.types.suppliers.PetGearSupplier;
import com.github.manolo8.darkbot.core.api.Capability;
Expand All @@ -16,7 +15,6 @@
import com.github.manolo8.darkbot.extensions.features.Feature;
import com.github.manolo8.darkbot.extensions.features.handlers.PetGearSelectorHandler;
import com.github.manolo8.darkbot.gui.utils.Strings;
import eu.darkbot.api.config.ConfigSetting;
import eu.darkbot.api.extensions.selectors.GearSelector;
import eu.darkbot.api.game.entities.Entity;
import eu.darkbot.api.game.enums.PetGear;
Expand All @@ -26,7 +24,6 @@
import eu.darkbot.api.game.other.Location;
import eu.darkbot.api.game.other.LocationInfo;
import eu.darkbot.api.game.other.Point;
import eu.darkbot.api.managers.ConfigAPI;
import eu.darkbot.api.managers.EventBrokerAPI;
import eu.darkbot.api.managers.PetAPI;
import eu.darkbot.api.utils.Inject;
Expand All @@ -35,18 +32,15 @@
import eu.darkbot.util.Timer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -73,11 +67,11 @@ public class PetManager extends Gui implements PetAPI {
private Ship target;
private boolean enabled = false;

private final FlashList<Gear> gearList = FlashList.ofArray(Gear::new);
private final FlashList<Gear> gearList = FlashList.ofArray(() -> new Gear(false));
private final List<PetGear> newGears = new ArrayList<>();

private final FlashListLong locatorWrapper = FlashListLong.ofArray();
private final FlashList<Gear> locatorList = FlashList.ofArray(Gear::new).noAuto();
private final FlashList<Gear> locatorList = FlashList.ofArray(() -> new Gear(true)).noAuto();

private final List<Integer> petBuffsIds = new ArrayList<>();

Expand Down Expand Up @@ -311,11 +305,7 @@ private void updateSelectedLocatorNpc() {
petPing.playerInfo.username = "Unknown";
petPing.npcId = -1;
} else {
selectedNpc = main.config.LOOT.NPC_INFOS.entrySet().stream()
.filter(e -> e.getValue().getFuzzyName(e.getKey()).equals(subModule.fuzzyName))
.map(Map.Entry::getValue)
.findFirst().orElse(null);

selectedNpc = subModule.npcInfo;
petPing.playerInfo.username = subModule.name;
petPing.npcId = subModule.id;
}
Expand Down Expand Up @@ -451,10 +441,8 @@ public Integer getGearOverride() {
public @NotNull Collection<? extends NpcInfo> getLocatorNpcs() {
if (locatorList.isEmpty()) return Collections.emptyList();

return main.config.LOOT.NPC_INFOS.entrySet().stream()
.filter(entry -> locatorList.stream()
.anyMatch(gear -> entry.getValue().getFuzzyName(entry.getKey()).equals(gear.fuzzyName)))
.map(Map.Entry::getValue)
return locatorList.stream()
.map(gear -> gear.npcInfo)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -692,12 +680,19 @@ public Optional<Location> getLocatorNpcLoc() {
return Optional.empty();
}

public static class Gear extends Reporting implements Point, LocatorPick {
public class Gear extends Reporting implements Point, LocatorPick {
public int id, parentId;
public long check;
public String name, fuzzyName;

private NpcInfo npcInfo;

private final SpriteObject sprite = new SpriteObject();
private final boolean submodule;

public Gear(boolean submodule) {
this.submodule = submodule;
}

@Override
public boolean updateAndReport() {
Expand All @@ -715,6 +710,13 @@ public boolean updateAndReport() {
this.name = name;
this.fuzzyName = Strings.fuzzyMatcher(name);
this.check = readLong(208, 152, 16);

if (submodule) {
this.npcInfo = main.config.LOOT.NPC_INFOS.values().stream()
.filter(npcInfo -> npcInfo.getFuzzyName().equals(fuzzyName))
.findAny().orElse(null);
}

return true;
}

Expand All @@ -737,6 +739,11 @@ public void setModule(long gearsSprite) {
152, Main.API.readLong(gearsSprite, 176));
}

@Override
public eu.darkbot.api.config.types.@Nullable NpcInfo getNpcInfo() {
return npcInfo;
}

@Override
public String getName() {
return name;
Expand All @@ -760,14 +767,8 @@ public PetStat getStat(Stat stat) {

@Feature(name = "Default Gear Supplier", description = "Sets the fallback pet gear")
public static class DefaultGearSupplier implements GearSelector, eu.darkbot.api.extensions.selectors.PetGearSupplier {

private final ConfigSetting<Map<String, NpcInfo>> npcInfos;
private PetManager pet;

public DefaultGearSupplier(ConfigAPI config) {
npcInfos = config.requireConfig("loot.npc_infos");
}

@Inject
public void setPetManager(PetManager pet) {
this.pet = pet;
Expand All @@ -788,18 +789,5 @@ public PetGear get() {
return pet.isEnabled();
}

@Override
public @Nullable LocatorPick getNpcLocatorPick(@UnmodifiableView Collection<? extends LocatorPick> picks) {
return npcInfos.getValue().entrySet()
.stream() // should filter kill-only npcs?
.filter(e -> e.getValue().extra.has(NpcExtra.PET_LOCATOR))
.sorted(Comparator.comparingInt(e -> e.getValue().priority))
.map(entry -> picks.stream()
.filter(gear -> entry.getValue().getFuzzyName(entry.getKey()).equals(gear.getFuzzyName()))
.findAny().orElse(null))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
}
}
}
23 changes: 3 additions & 20 deletions src/main/java/com/github/manolo8/darkbot/gui/utils/Strings.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.github.manolo8.darkbot.gui.utils;

import eu.darkbot.util.StringUtils;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DecimalFormat;
import java.util.Locale;
import java.util.regex.Pattern;

public class Strings {
public static final DecimalFormat ONE_PLACE_FORMAT = new DecimalFormat("0.0");

private static final Pattern NON_CHARACTER_REPLACEMENT = Pattern.compile("[^A-Za-z0-9]");
private static final Pattern MIMESIS_REPLACEMENT = Pattern.compile("m[i1]m[e3][s5][i1][s5]");

private static final Pattern SIMPLIFY_NAME_MATCHES = Pattern.compile("^[^\\d]+\\d{1,3}$");
private static final Pattern SIMPLIFY_NAME_REPLACEMENT = Pattern.compile("\\d{1,3}$");

Expand All @@ -37,22 +35,7 @@ public static String simplifyName(String name) {
}

public static String fuzzyMatcher(String string) {
// xX[ Chaos Protegit ]Xx or -x-[ Synk ]-x-, make sure to remove the "x"
if ((string.startsWith("xX[") && string.endsWith("]Xx"))
|| (string.startsWith("-x-") && string.endsWith("-x-"))) {
string = string.substring(3, string.length() - 3);
}
// Special case, keep upper & lower case
if (!string.contains("StreuneR")) {
string = string.toLowerCase(Locale.ROOT);
}
// Fix up referee bot
string = string.replace("referee binary bot", "referee bot");
// Fix up mimesis
string = MIMESIS_REPLACEMENT.matcher(string).replaceAll("mimesis");

// Keep only alphanumerical chars
return NON_CHARACTER_REPLACEMENT.matcher(string).replaceAll("");
return StringUtils.fuzzyNpcName(string);
}

public static boolean isEmpty(String s) {
Expand Down

0 comments on commit 3c1f98a

Please sign in to comment.