Skip to content

Commit

Permalink
Add stricter SkyBlock menu detection
Browse files Browse the repository at this point in the history
- Detect SkyBlock menus using chest items in addition to name
- Remove Mixin
  • Loading branch information
ILikePlayingGames committed Feb 19, 2024
1 parent f0ad1cf commit f1bfec9
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 368 deletions.
20 changes: 1 addition & 19 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ val baseGroup: String by project
val mcVersion: String by project
val modid: String by project
val version: String by project
val mixinGroup = "$baseGroup.$modid.mixin"

// Toolchains:
java {
Expand All @@ -31,9 +30,6 @@ loom {
launchConfigs {
"client" {
property("devauth.enabled", "true")
property("mixin.debug", "true")
property("asmhelper.verbose", "true")
arg("--tweakClass", "org.spongepowered.asm.launch.MixinTweaker")
}
}
runConfigs {
Expand All @@ -59,10 +55,6 @@ loom {
}
forge {
pack200Provider.set(dev.architectury.pack200.java.Pack200Adapter())
mixinConfig("mixins.$modid.json")
}
mixin {
defaultRefmapName.set("mixins.$modid.refmap.json")
}
}

Expand All @@ -82,11 +74,6 @@ dependencies {
minecraft("com.mojang:minecraft:1.8.9")
mappings("de.oceanlabs.mcp:mcp_stable:22-1.8.9")
forge("net.minecraftforge:forge:1.8.9-11.15.1.2318-1.8.9")

shadowImpl("org.spongepowered:mixin:0.7.11-SNAPSHOT") {
isTransitive = false
}
annotationProcessor("org.spongepowered:mixin:0.8.5-SNAPSHOT")
runtimeOnly("me.djtheredstoner:DevAuth-forge-legacy:1.2.0")
}

Expand All @@ -101,10 +88,6 @@ tasks.withType(Jar::class) {
manifest.attributes.run {
this["FMLCorePluginContainsFMLMod"] = "true"
this["ForceLoadAsMod"] = "true"

// If you don't want mixins, remove these lines
this["TweakClass"] = "org.spongepowered.asm.launch.MixinTweaker"
this["MixinConfigs"] = "mixins.$modid.json"
}
}

Expand All @@ -120,9 +103,8 @@ tasks.processResources {
inputs.property("version", project.version)
inputs.property("mcversion", mcVersion)
inputs.property("modid", modid)
inputs.property("mixinGroup", mixinGroup)

filesMatching(listOf("mcmod.info", "mixins.$modid.json")) {
filesMatching("mcmod.info") {
expand(inputs.properties)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

package ca.tirelesstraveler.fancywarpmenu.data.skyblockconstants;

import ca.tirelesstraveler.fancywarpmenu.data.skyblockconstants.menu.ItemMatchCondition;
import ca.tirelesstraveler.fancywarpmenu.data.skyblockconstants.menu.Menu;
import ca.tirelesstraveler.fancywarpmenu.data.skyblockconstants.menu.matchconditions.MenuMatchCondition;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

Expand All @@ -34,11 +34,10 @@

@SuppressWarnings("unused")
public class SkyBlockConstants {
private SkyBlockConstants() {
}
public static final String WARP_COMMAND_BASE = "/warp";

/** A map with SkyBlock menus as keys and lists of conditions used to identify them as values */
private Map<Menu, List<MenuMatchCondition>> menuMatchingMap;
/** Map of match conditions used to identify SkyBlock menus */
private Map<Menu, List<ItemMatchCondition>> menuMatchingMap;

/** Chat messages sent by the server when a warp attempt succeeds or fails */
private WarpMessages warpMessages;
Expand All @@ -47,10 +46,25 @@ private SkyBlockConstants() {
/** Chat messages are checked to see if they start with this string in order to see if the player joined SkyBlock */
private String skyBlockJoinMessage;

public Map<Menu, List<MenuMatchCondition>> getMenuMatchingMap() {
private SkyBlockConstants() {
}

public Map<Menu, List<ItemMatchCondition>> getMenuMatchingMap() {
return menuMatchingMap;
}

/**
* Returns the inventory slot index of the last {@link ItemMatchCondition} for the given {@link Menu}.
*
* @param menu the {@code Menu} to get the inventory slot index from
* @return the inventory slot index of the last {@code ItemMatchCondition} for the given {@code Menu}
*/
public int getLastMatchConditionInventorySlotIndex(Menu menu) {
List<ItemMatchCondition> matchConditions = menuMatchingMap.get(menu);

return matchConditions.get(matchConditions.size() - 1).getInventorySlotIndex();
}

public WarpMessages getWarpMessages() {
return warpMessages;
}
Expand All @@ -73,11 +87,11 @@ public static void validateSkyBlockConstants(SkyBlockConstants skyBlockConstants
throw new NullPointerException("SkyBlock constants cannot be null");
}

for(Map.Entry<Menu, List<MenuMatchCondition>> menuMatchingMapEntry : skyBlockConstants.menuMatchingMap.entrySet()) {
List<MenuMatchCondition> matchConditions = getMenuMatchConditions(menuMatchingMapEntry);
for(Map.Entry<Menu, List<ItemMatchCondition>> menuMatchingMapEntry : skyBlockConstants.menuMatchingMap.entrySet()) {
List<ItemMatchCondition> matchConditions = getMenuMatchConditions(menuMatchingMapEntry);

for (MenuMatchCondition menuMatchCondition : matchConditions) {
menuMatchCondition.validateCondition();
for (ItemMatchCondition matchCondition : matchConditions) {
matchCondition.validateCondition();
}
}

Expand All @@ -97,8 +111,8 @@ public static void validateSkyBlockConstants(SkyBlockConstants skyBlockConstants
}

@NotNull
private static List<MenuMatchCondition> getMenuMatchConditions(Map.Entry<Menu, List<MenuMatchCondition>> menuMatchingMapEntry) {
List<MenuMatchCondition> matchConditions = menuMatchingMapEntry.getValue();
private static List<ItemMatchCondition> getMenuMatchConditions(Map.Entry<Menu, List<ItemMatchCondition>> menuMatchingMapEntry) {
List<ItemMatchCondition> matchConditions = menuMatchingMapEntry.getValue();

if (matchConditions == null) {
throw new NullPointerException(String.format("Menu %s's menu match conditions list cannot be null",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@
* OR OTHER DEALINGS IN THE SOFTWARE.
*/

package ca.tirelesstraveler.fancywarpmenu.data.skyblockconstants.menu.matchconditions;
package ca.tirelesstraveler.fancywarpmenu.data.skyblockconstants.menu;

import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.StringUtils;
import net.minecraftforge.common.util.Constants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.regex.Pattern;

/**
* A {@link MenuMatchCondition} that checks if a given SkyBlock {@code GuiChest} menu contains an item with the same
* A match condition that checks if a given SkyBlock {@code GuiChest} menu contains an item with the same
* item name, inventory slot index, Minecraft item ID, and SkyBlock item ID as the item specified in this condition.
*/
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal", "unused"})
public class ItemMatchCondition extends MenuMatchCondition {
public class ItemMatchCondition {
public static final Logger logger = LogManager.getLogger();

/** Display name of the {@code ItemStack} (excluding formatting codes) */
private String itemName;
/** The slot index of the slot the item occupies in the {@code IInventory} provided to {@link #inventoryContainsMatchingItem(IInventory)} */
Expand Down Expand Up @@ -100,35 +104,41 @@ public boolean inventoryContainsMatchingItem(IInventory inventory) {

if (itemName != null) {
itemNameMatches = itemStack.hasDisplayName() &&
!StringUtils.stripControlCodes(itemStack.getDisplayName()).equals(itemName);
StringUtils.stripControlCodes(itemStack.getDisplayName()).equals(itemName);
logger.debug("Item name matches: {}", itemNameMatches);

if (!itemNameMatches) return false;
}

if (minecraftItemID != null) {
minecraftItemIDMatches = itemStack.getItem().getRegistryName().equals(minecraftItemID);
logger.debug("Minecraft registry ID matches: {}", minecraftItemIDMatches);

if (!minecraftItemIDMatches) return false;
}

// Following checks require NBT data, fail if NBT data not present
if (!itemStack.hasTagCompound()) return false;

if (skyBlockItemID != null) {
if (!itemStack.hasTagCompound() || !itemStack.getTagCompound().hasKey("ExtraAttributes", Constants.NBT.TAG_COMPOUND)) {
if (!itemStack.getTagCompound().hasKey("ExtraAttributes", Constants.NBT.TAG_COMPOUND)) {
return false;
}

NBTTagCompound extraAttributesTag = itemStack.getSubCompound("ExtraAttributes", false);
skyBlockItemIDMatches = extraAttributesTag.hasKey("id", Constants.NBT.TAG_STRING) &&
extraAttributesTag.getString("id").equals(skyBlockItemID);
logger.debug("SkyBlock Item ID matches: {}", skyBlockItemIDMatches);

if (!skyBlockItemIDMatches) return false;
}

if (loreMatchPattern != null) {
if (!itemStack.hasTagCompound() || !itemStack.getTagCompound().hasKey("display")) {
if (loreMatchPattern != null && loreMatchPattern.pattern() != null) {
if (!itemStack.getTagCompound().hasKey("display", Constants.NBT.TAG_COMPOUND)) {
return false;
}

NBTTagCompound displayTag = itemStack.getTagCompound().getCompoundTag("display");
NBTTagCompound displayTag = itemStack.getSubCompound("display", false);

if (displayTag.hasKey("Lore", Constants.NBT.TAG_LIST)) {
NBTTagList loreTag = displayTag.getTagList("Lore", Constants.NBT.TAG_STRING);
Expand All @@ -139,12 +149,12 @@ public boolean inventoryContainsMatchingItem(IInventory inventory) {
for (int i = 0; i < loreTag.tagCount(); i++) {
loreStringBuilder.append(loreTag.getStringTagAt(i)).append("\n");
}

loreStringBuilder.setLength(loreStringBuilder.length() - 1);
loreStringBuilder.deleteCharAt(loreStringBuilder.length() - 1);

String loreString = loreStringBuilder.toString();

lorePatternMatches = loreMatchPattern.asPredicate().test(loreString);
logger.debug("Lore pattern matches: {}", lorePatternMatches);

return lorePatternMatches;
}
Expand All @@ -157,15 +167,23 @@ public boolean inventoryContainsMatchingItem(IInventory inventory) {
return false;
}

/**
* Verifies that this match condition's properties are valid.
* This is called for conditions that have just been deserialized.
*/
public void validateCondition() {

if (this.inventorySlotIndex <= -1) {
throw new IllegalArgumentException("inventorySlotIndex must be greater than or equal to 0");
}

if (this.itemName == null && this.minecraftItemID == null &&
this.skyBlockItemID == null && this.loreMatchPattern == null) {
throw new IllegalArgumentException("No item name, Minecraft item ID, SkyBlock item ID, or lore criteria specified");
if (this.itemName == null && this.minecraftItemID == null && this.skyBlockItemID == null && this.loreMatchPattern == null) {
throw new IllegalArgumentException("No item name, Minecraft item ID, SkyBlock item ID, or lore criteria specified.");
}

if (this.loreMatchPattern != null && loreMatchPattern.pattern() == null) {
throw new IllegalArgumentException(
String.format("Lore match pattern for item in slot %d lacks a regex string.", inventorySlotIndex));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,21 @@
*/
public enum Menu {
/** Value used when player is not in a menu or in an unknown or irrelevant menu */
NONE,
SKYBLOCK_MENU,
FAST_TRAVEL,
PORHTAL
NONE(""),
SKYBLOCK_MENU("SkyBlock Menu"),
FAST_TRAVEL("Fast Travel"),
PORHTAL("Porhtal");

/**
* Menu name as displayed at the top of the {@code GuiChest}
*/
final String MENU_DISPLAY_NAME;

Menu(String menuDisplayName) {
this.MENU_DISPLAY_NAME = menuDisplayName;
}

public String getMenuDisplayName() {
return MENU_DISPLAY_NAME;
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit f1bfec9

Please sign in to comment.