Skip to content

Commit

Permalink
SkyBlock and Menu Check Fixes
Browse files Browse the repository at this point in the history
- Remove chat message SkyBlock check
- Time out SkyBlock scoreboard check after 3 seconds
- Add log warnings for mismatched menu items
- Prevent custom UI from initializing outside SkyBlock
- Hide config button if fast travel menu not detected
- Fix GuiChest flashing before fancy warp menu initializes
  • Loading branch information
ILikePlayingGames committed Feb 27, 2024
1 parent 9394381 commit 1f65072
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

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

import java.util.List;
Expand Down Expand Up @@ -104,10 +103,6 @@ public static void validateSkyBlockConstants(SkyBlockConstants skyBlockConstants
for (WarpCommandVariant warpCommandVariant : skyBlockConstants.warpCommandVariants) {
WarpCommandVariant.validateWarpCommandVariant(warpCommandVariant);
}

if (StringUtils.isEmpty(skyBlockConstants.skyBlockJoinMessage)) {
throw new IllegalArgumentException("SkyBlock join message cannot be null or empty.");
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,26 @@ public boolean inventoryContainsMatchingItem(IInventory inventory) {
boolean lorePatternMatches;

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

if (!itemNameMatches) {
logger.warn("Item name mismatch\nExpected {} ; Found {}",
itemName, itemStackName);
}

if (!itemNameMatches) return false;
}

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

if (!minecraftItemIDMatches) {
logger.warn("Minecraft Item ID mismatch\nExpected {} ; Found {}",
minecraftItemID, itemStackMinecraftItemID);
}

if (!minecraftItemIDMatches) return false;
}
Expand All @@ -126,9 +136,14 @@ public boolean inventoryContainsMatchingItem(IInventory inventory) {
}

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);
String itemStackSkyBlockID = extraAttributesTag.hasKey("id", Constants.NBT.TAG_STRING) ?
extraAttributesTag.getString("id") : null;
skyBlockItemIDMatches = itemStackSkyBlockID != null && itemStackSkyBlockID.equals(skyBlockItemID);

if (!skyBlockItemIDMatches) {
logger.warn("SkyBlock Item ID mismatch\nExpected {} ; Found {}",
skyBlockItemID, itemStackSkyBlockID);
}

if (!skyBlockItemIDMatches) return false;
}
Expand All @@ -154,7 +169,10 @@ public boolean inventoryContainsMatchingItem(IInventory inventory) {
String loreString = loreStringBuilder.toString();

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

if (!lorePatternMatches) {
logger.warn("Lore did not match pattern\nItem lore: {}", loreTag);
}

return lorePatternMatches;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public abstract class GuiChestMenu extends GuiChest {
protected GuiButton selectedButton;

public GuiChestMenu(IInventory playerInventory, IInventory chestInventory, ResourceLocation backgroundTextureLocation) {
this(playerInventory, chestInventory, backgroundTextureLocation, false, false);
this(playerInventory, chestInventory, backgroundTextureLocation, true, true);
}

public GuiChestMenu(IInventory playerInventory, IInventory chestInventory, ResourceLocation backgroundTextureLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class GuiFancyWarp extends GuiChestMenu {
protected Menu menu;
protected Layout layout;
private final InventoryBasic chestInventory;
private GuiButton configButton;
private GuiButtonConfig configButton;
private InventoryChangeListener inventoryListener;
private String warpFailMessage;
private RuntimeException guiInitException;
Expand Down Expand Up @@ -153,8 +153,6 @@ public void initGui() {

buttonList.add(new GuiButtonTimedLabel(0, width / 2 - 100, labelY + ySpacing,
I18n.format("fancywarpmenu.gui.buttons.copyToClipboard")));

setCustomUIState(true, true);
}
}

Expand Down Expand Up @@ -489,13 +487,13 @@ private void onChestItemChange(int triggerCount) {
The item change event is triggered twice for each item, and the item stack is set on the 2nd time it's
triggered. For example, slot 53 is actually set on the 106th time the item change event triggers.
(lastSlotIndexToCheck + 1) since slots are 0-indexed but trigger count starts at 1
*/
if (triggerCount > lastSlotIndexToCheck * 2 && chestInventory.getStackInSlot(lastSlotIndexToCheck) != null) {
if (GameChecks.menuItemsMatch(menu, chestInventory)) {
setCustomUIState(true, true);
}

if (triggerCount > (lastSlotIndexToCheck + 1) * 2 && chestInventory.getStackInSlot(lastSlotIndexToCheck) != null) {
boolean menuItemsMatch = GameChecks.menuItemsMatch(menu, chestInventory);
setCustomUIState(menuItemsMatch, menuItemsMatch);
updateButtonStates();
configButton.setVisible(menuItemsMatch);
chestInventory.removeInventoryChangeListener(inventoryListener);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

package ca.tirelesstraveler.fancywarpmenu.listeners;

import ca.tirelesstraveler.fancywarpmenu.FancyWarpMenu;
import ca.tirelesstraveler.fancywarpmenu.state.GameState;
import io.netty.channel.ChannelHandler;
import net.minecraft.client.Minecraft;
Expand All @@ -42,10 +41,13 @@
@ChannelHandler.Sharable
public class SkyBlockJoinListener {
private static final String SERVER_BRAND_START = "Hypixel BungeeCord";
private static final int SCOREBOARD_CHECK_TIME_OUT = 3000;

private static final Logger logger = LogManager.getLogger();
private boolean serverBrandChecked;
private boolean onHypixel;
private boolean scoreboardChecked;
private long lastWorldSwitchTime;

@SubscribeEvent
public void onClientDisconnect(FMLNetworkEvent.ClientDisconnectionFromServerEvent e) {
Expand All @@ -61,16 +63,17 @@ public void onClientDisconnect(FMLNetworkEvent.ClientDisconnectionFromServerEven
public void onGuiOpen(GuiOpenEvent event) {
// Reset on world switch
if (event.gui instanceof GuiDownloadTerrain) {
lastWorldSwitchTime = Minecraft.getSystemTime();
scoreboardChecked = false;
GameState.setOnSkyBlock(false);
}
}

@SubscribeEvent
public void onChatMessageReceived(ClientChatReceivedEvent event) {
if (!serverBrandChecked || onHypixel) {
if (!serverBrandChecked || onHypixel && !scoreboardChecked) {
// type 0 is a standard chat message
if (event.type == 0 && event.message.getUnformattedText().startsWith(
FancyWarpMenu.getSkyBlockConstants().getSkyBlockJoinMessage())) {
if (event.type == 0) {
EntityPlayerSP thePlayer = Minecraft.getMinecraft().thePlayer;

if (!serverBrandChecked) {
Expand All @@ -82,17 +85,23 @@ public void onChatMessageReceived(ClientChatReceivedEvent event) {
}
}

if (onHypixel) {
if (onHypixel && !scoreboardChecked) {
Scoreboard scoreboard = thePlayer.getWorldScoreboard();
boolean newSkyBlockState = scoreboard != null && scoreboard.getObjective("SBScoreboard") != null;

if (newSkyBlockState && !GameState.isOnSkyBlock()) {
logger.debug("Player joined SkyBlock.");
} else if (!newSkyBlockState && GameState.isOnSkyBlock()) {
logger.debug("Player left SkyBlock.");
if (newSkyBlockState != GameState.isOnSkyBlock()) {
if (newSkyBlockState) {
logger.debug("Player joined SkyBlock.");
} else {
logger.debug("Player left SkyBlock.");
}

GameState.setOnSkyBlock(newSkyBlockState);
}

GameState.setOnSkyBlock(newSkyBlockState);
if (Minecraft.getSystemTime() - lastWorldSwitchTime > SCOREBOARD_CHECK_TIME_OUT) {
scoreboardChecked = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void onGuiOpen(GuiOpenEvent event) {
event.gui = new FancyWarpMenuConfigScreen(null);
FancyWarpMenuState.setOpenConfigMenuRequested(false);
}
} else if (event.gui instanceof GuiChest) {
} else if (GameState.isOnSkyBlock() && event.gui instanceof GuiChest) {
IInventory playerInventory = mc.thePlayer.inventory;
IInventory chestInventory = ((ContainerChest) ((GuiChest) event.gui).inventorySlots).getLowerChestInventory();

Expand All @@ -94,7 +94,7 @@ public void onGuiOpen(GuiOpenEvent event) {

@SubscribeEvent
public void keyTyped(InputEvent.KeyInputEvent event) {
if (Settings.isWarpMenuEnabled() && (GameState.isOnSkyBlock() || Settings.shouldSkipSkyBlockCheck()) &&
if (Settings.isWarpMenuEnabled() && GameState.isOnSkyBlock() &&
FancyWarpMenu.getKeyBindingOpenWarpMenu().isPressed() &&
Minecraft.getSystemTime() - lastWarpMenuHotkeyPress > HOTKEY_PRESS_DELAY) {
lastWarpMenuHotkeyPress = Minecraft.getSystemTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public static void checkSeason() {
/**
* Determines which SkyBlock {@code GuiChest} menu the player is in using the {@link net.minecraft.client.gui.inventory.GuiChest}
* display name. This is used for initial checks when the items haven't loaded in yet.
* The matched menu will be saved to {@code GameState#currentMenu}.
*
* @param chestInventory the inventory of the chest holding the menu
* @return a {@code Menu} value representing the current menu the player has open
Expand All @@ -104,20 +103,19 @@ public static Menu determineOpenMenu(IInventory chestInventory) {
for (Map.Entry<Menu, List<ItemMatchCondition>> menuMatchingEntry :
FancyWarpMenu.getSkyBlockConstants().getMenuMatchingMap().entrySet()) {
if (chestTitle.equals(menuMatchingEntry.getKey().getMenuDisplayName())) {
GameState.setCurrentMenu(menuMatchingEntry.getKey());
return menuMatchingEntry.getKey();
}
}
}

GameState.setCurrentMenu(Menu.NONE);

return Menu.NONE;
}

/**
* Determines if the player is in the given menu by checking whether all the {@link ItemMatchCondition}s for that menu
* match the given inventory. This should be used after the inventory has loaded the slot index returned by
* {@link ca.tirelesstraveler.fancywarpmenu.data.skyblockconstants.SkyBlockConstants#getLastMatchConditionInventorySlotIndex(Menu)}.
* If a match is found, the matched menu is saved using {@link GameState#setCurrentMenu(Menu)}
*
* @param menu the {@code Menu} whose match conditions will be checked
* @param chestInventory the inventory to check against the match conditions
Expand All @@ -131,10 +129,12 @@ public static boolean menuItemsMatch(Menu menu, IInventory chestInventory) {
matchCondition.getInventorySlotIndex(), menu);

if (!matchCondition.inventoryContainsMatchingItem(chestInventory)) {
logger.debug("Item match on slot {} failed.", matchCondition.getInventorySlotIndex());
logger.warn("Item match on slot {} failed.", matchCondition.getInventorySlotIndex());
GameState.setCurrentMenu(Menu.NONE);
return false;
}
logger.debug("Finished item match on slot {} for menu {}.",
matchCondition.getInventorySlotIndex(), menu);
}

GameState.setCurrentMenu(menu);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,5 @@
"command": "garry",
"type": "WARP"
}
],
"skyBlockJoinMessage": "Profile ID:"
]
}

0 comments on commit 1f65072

Please sign in to comment.