diff --git a/src/main/java/codes/cookies/mod/features/dungeons/solver/terminals/MelodyTerminalSolver.java b/src/main/java/codes/cookies/mod/features/dungeons/solver/terminals/MelodyTerminalSolver.java index 0948743..5334a7e 100644 --- a/src/main/java/codes/cookies/mod/features/dungeons/solver/terminals/MelodyTerminalSolver.java +++ b/src/main/java/codes/cookies/mod/features/dungeons/solver/terminals/MelodyTerminalSolver.java @@ -27,125 +27,128 @@ import java.util.ArrayList; public class MelodyTerminalSolver extends TerminalSolver { - private final ItemStack blockedInput = this.doneItem.copy().withItem(Items.BARRIER); - private final boolean[] percentagesAnnounced = new boolean[4]; - private int currentColumn = -1; - private int correctColumn = -1; - private int currentRow = -1; - - public MelodyTerminalSolver() { - InventoryEvents.beforeInit("Click the button on time!", - getFloorPredicate().or(getDebugPredicate()), - this::modify); - } - - private static void melodyNotify(String message) { - if (DungeonConfig.getInstance().terminalFoldable.melodyNotifier.getValue()) { - ChatUtils.sendPartyMessage(message); - } - } - - private void modify(HandledScreen handledScreen) { - ScreenEvents.remove(handledScreen).register((screen) -> { - if (percentagesAnnounced[3]) - melodyNotify("Melody Terminal complete!"); - else - melodyNotify("Melody closed without completion???"); - - CookiesUtils.sendMessage("Melody Terminal reset!"); - percentagesAnnounced[0] = false; - percentagesAnnounced[1] = false; - percentagesAnnounced[2] = false; - percentagesAnnounced[3] = false; - this.clear(); - this.items.clear(); - currentColumn = -1; - correctColumn = -1; - currentRow = -1; - }); - - openNewTerminal(); - - InventoryContentUpdateEvent.register(handledScreen.getScreenHandler(), (slot, stack) -> { - this.update(slot, stack, handledScreen.getScreenHandler()); - }); - } - - @Override - public void openNewTerminal() { - super.openNewTerminal(); - if (DungeonConfig.getInstance().terminalFoldable.preventMissclicks.getValue()) { - this.blockedInput.set(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, Runnables.doNothing()); - } else { - this.blockedInput.remove(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE); - } - melodyNotify("Melody Terminal start!"); - } - - private void update(int slot, ItemStack stack, ScreenHandler handler) { - if (slot > 53) { - return; - } - - this.items.add(stack); - - if (slot == 53) { - stack.set(MiscDataComponentTypes.TERMINAL_SOLVER_TOGGLE, Unit.INSTANCE); - if (this.localToggle) { - stack.set(CookiesDataComponentTypes.OVERRIDE_ITEM, this.toggleOff); - } else { - stack.set(CookiesDataComponentTypes.OVERRIDE_ITEM, this.toggleOn); - } - if (this.localToggle) { - this.setItems(); - } - return; - } - - if (slot % 9 == 0 || slot % 9 == 6 || slot % 9 == 8) { - return; - } - - if (stack.isOf(Items.LIME_STAINED_GLASS_PANE)) { - currentRow = (slot / 9) - 1; - currentColumn = slot % 9; - - if (this.localToggle && currentRow > 0 && !percentagesAnnounced[currentRow]) { - melodyNotify("Melody is " + String.format("%.0f%%", (currentRow / 4f) * 100) + " (" + currentRow + "/4) complete!"); - percentagesAnnounced[currentRow] = true; - } - - var selectedSlot = handler.getSlot(slot + (7 - currentColumn)); - if (!this.localToggle) { - SlotAccessor.setInteractionLocked(selectedSlot, false); - SlotAccessor.setItem(selectedSlot, null); - return; - } - - if (currentColumn == correctColumn) { - SlotAccessor.setInteractionLocked(selectedSlot, false); - SlotAccessor.setItem(selectedSlot, null); - } else { - SlotAccessor.setInteractionLocked(selectedSlot, true); - SlotAccessor.setItem(selectedSlot, this.blockedInput); - } - - return; - } - - if (stack.isOf(Items.MAGENTA_STAINED_GLASS_PANE)) { - correctColumn = slot % 9; - return; - } - - if (stack.isOf(Items.LIME_TERRACOTTA) && this.localToggle) { - if (currentColumn == correctColumn) { - SlotAccessor.setInteractionLocked(handler.getSlot(slot), false); - SlotAccessor.setItem(handler.getSlot(slot), null); - } else { - SlotAccessor.setInteractionLocked(handler.getSlot(slot), true); - SlotAccessor.setItem(handler.getSlot(slot), this.blockedInput); - } - } - } + private final ItemStack blockedInput = this.doneItem.copy().withItem(Items.BARRIER); + private final boolean[] percentagesAnnounced = new boolean[4]; + private int currentColumn = -1; + private int correctColumn = -1; + private int currentRow = -1; + + public MelodyTerminalSolver() { + InventoryEvents.beforeInit("Click the button on time!", + getFloorPredicate().or(getDebugPredicate()), + this::modify); + } + + private static void melodyNotify(String message) { + if (DungeonConfig.getInstance().terminalFoldable.melodyNotifier.getValue()) { + ChatUtils.sendPartyMessage(message); + } + } + + private void modify(HandledScreen handledScreen) { + ScreenEvents.remove(handledScreen).register((screen) -> { + if (percentagesAnnounced[3]) { + melodyNotify("Melody Terminal complete!"); + } else { + melodyNotify("Melody closed without completion???"); + } + + CookiesUtils.sendMessage("Melody Terminal reset!"); + percentagesAnnounced[0] = false; + percentagesAnnounced[1] = false; + percentagesAnnounced[2] = false; + percentagesAnnounced[3] = false; + this.clear(); + this.items.clear(); + currentColumn = -1; + correctColumn = -1; + currentRow = -1; + }); + + openNewTerminal(); + + InventoryContentUpdateEvent.register(handledScreen.getScreenHandler(), (slot, stack) -> { + this.update(slot, stack, handledScreen.getScreenHandler()); + }); + } + + @Override + public void openNewTerminal() { + super.openNewTerminal(); + if (DungeonConfig.getInstance().terminalFoldable.preventMissclicks.getValue()) { + this.blockedInput.set(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, Runnables.doNothing()); + } else { + this.blockedInput.remove(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE); + } + melodyNotify("Melody Terminal start!"); + } + + private void update(int slot, ItemStack stack, ScreenHandler handler) { + if (slot > 53) { + return; + } + + this.items.add(stack); + + if (slot == 53) { + stack.set(MiscDataComponentTypes.TERMINAL_SOLVER_TOGGLE, Unit.INSTANCE); + if (this.localToggle) { + stack.set(CookiesDataComponentTypes.OVERRIDE_ITEM, this.toggleOff); + } else { + stack.set(CookiesDataComponentTypes.OVERRIDE_ITEM, this.toggleOn); + } + if (this.localToggle) { + this.setItems(); + } + return; + } + + if (slot % 9 == 0 || slot % 9 == 6 || slot % 9 == 8) { + return; + } + + if (stack.isOf(Items.LIME_STAINED_GLASS_PANE)) { + currentRow = (slot / 9) - 1; + currentColumn = slot % 9; + + if (this.localToggle && currentRow > 0 && !percentagesAnnounced[currentRow]) { + melodyNotify("Melody is " + String.format("%.0f%%", (currentRow / 4f) * 100) + " (" + currentRow + "/4) complete!"); + percentagesAnnounced[currentRow] = true; + } + + var selectedSlot = handler.getSlot(slot + (7 - currentColumn)); + if (!this.localToggle) { + SlotAccessor.setInteractionLocked(selectedSlot, false); + SlotAccessor.setItem(selectedSlot, null); + return; + } + + if (currentColumn == correctColumn) { + SlotAccessor.setInteractionLocked(selectedSlot, false); + SlotAccessor.setItem(selectedSlot, null); + } else { + if (DungeonConfig.getInstance().terminalFoldable.preventMissclicks.getValue()) { + SlotAccessor.setInteractionLocked(selectedSlot, true); + } + SlotAccessor.setItem(selectedSlot, this.blockedInput); + } + + return; + } + + if (stack.isOf(Items.MAGENTA_STAINED_GLASS_PANE)) { + correctColumn = slot % 9; + return; + } + + if (stack.isOf(Items.LIME_TERRACOTTA) && this.localToggle) { + if (currentColumn == correctColumn) { + SlotAccessor.setInteractionLocked(handler.getSlot(slot), false); + SlotAccessor.setItem(handler.getSlot(slot), null); + } else { + SlotAccessor.setInteractionLocked(handler.getSlot(slot), true); + SlotAccessor.setItem(handler.getSlot(slot), this.blockedInput); + } + } + } } diff --git a/src/main/java/codes/cookies/mod/features/misc/PartyCommandsFeature.java b/src/main/java/codes/cookies/mod/features/misc/PartyCommandsFeature.java index e9478f2..7b04b55 100644 --- a/src/main/java/codes/cookies/mod/features/misc/PartyCommandsFeature.java +++ b/src/main/java/codes/cookies/mod/features/misc/PartyCommandsFeature.java @@ -1,14 +1,11 @@ package codes.cookies.mod.features.misc; -import codes.cookies.mod.config.ConfigManager; import codes.cookies.mod.config.categories.DungeonConfig; import codes.cookies.mod.config.system.Option; import codes.cookies.mod.events.ChatMessageEvents; -import codes.cookies.mod.generated.Regions; import codes.cookies.mod.utils.cookies.CookiesUtils; import codes.cookies.mod.utils.skyblock.ChatUtils; -import codes.cookies.mod.utils.skyblock.LocationUtils; import codes.cookies.mod.utils.skyblock.PartyUtils; import lombok.Getter; @@ -27,179 +24,188 @@ import static java.util.Map.entry; public class PartyCommandsFeature { - public static void register() { - ChatMessageEvents.BEFORE_MODIFY.register(PartyCommandsFeature::handleChat); - } - - private static final Random random = new Random(); - - private static final Map, PartyCommand> Commands = Map.ofEntries( - entry(command -> command.equals("ptme"), new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("ptme"), (command, message) -> { - var sender = getSender(message); - if (sender.equals(MinecraftClient.getInstance().getSession().getUsername())) - return; - CookiesUtils.sendCommand("p transfer " + sender); - })), - entry(command -> command.equals("warp"), new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("warp"), (command, message) -> { - CookiesUtils.sendCommand("p warp"); - })), - entry(command -> { - //only match f[1-7], m[1-7], and t[1-5] - return command.matches("f[1-7]|m[1-7]|t[1-5]"); - }, new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("joinInstance"), (command, message) -> { - StringBuilder joinInstanceCommand = new StringBuilder("joininstance "); - switch (command.charAt(0)) { - case 'f' -> joinInstanceCommand.append("CATACOMBS_FLOOR_"); - case 'm' -> joinInstanceCommand.append("MASTER_CATACOMBS_FLOOR_"); - case 't' -> { - joinInstanceCommand.append("KUUDRA_"); - switch (command.charAt(1)) { - case '1' -> joinInstanceCommand.append("BASIC"); - case '2' -> joinInstanceCommand.append("HOT"); - case '3' -> joinInstanceCommand.append("BURNING"); - case '4' -> joinInstanceCommand.append("FIERY"); - case '5' -> joinInstanceCommand.append("INFERNAL"); - } - CookiesUtils.sendCommand(joinInstanceCommand.toString()); - return; - } - } - - switch (command.charAt(1)) { - case '1' -> joinInstanceCommand.append("ONE"); - case '2' -> joinInstanceCommand.append("TWO"); - case '3' -> joinInstanceCommand.append("THREE"); - case '4' -> joinInstanceCommand.append("FOUR"); - case '5' -> joinInstanceCommand.append("FIVE"); - case '6' -> joinInstanceCommand.append("SIX"); - case '7' -> joinInstanceCommand.append("SEVEN"); - } - - CookiesUtils.sendCommand(joinInstanceCommand.toString()); - })), - entry(command -> command.startsWith("dt"), new DownTimeCommand(() -> PartyCommandsFeature.shouldRunCommand("dt"))), - entry(command -> command.equals("cf"), new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("cf"), (command, message) -> { - if (random.nextBoolean()) { - ChatUtils.sendPartyMessage("HEADS!"); - } else { - ChatUtils.sendPartyMessage("TAILS!"); - } - })) - ); - - private static boolean shouldRunCommand(String partyCommand) { - PartyUtils.request(); - var player = CookiesUtils.getPlayer(); - if (partyCommand.matches("f[1-7]|m[1-7]|t[1-5]")) { - partyCommand = "joininstance"; - } - return Optional.ofNullable(DungeonConfig.getInstance().partyChatCommandsFoldable.partyChatCommands.get(partyCommand)).map(Option::getValue).orElse(true) && player.filter(clientPlayerEntity -> PartyUtils.isInParty() && PartyUtils.getPartyLeader().equals(clientPlayerEntity.getUuid())).isPresent(); - } - - private static void handleChat(Text text, boolean b) { - if (b) return; - var message = text.getString(); - - if (!message.startsWith("§9Party §8> ")) return; - - var commandString = CookiesUtils.stripColor(message.substring(message.indexOf(": !") + 3).trim()); - - var command = Commands.entrySet().stream().filter(commandEntry -> commandEntry.getKey().test(commandString)).findFirst(); - - if (command.isPresent()) { - if (command.get().getValue().getPredicate().get()) { - var action = command.get().getValue().getAction(); - if (action != null) { - action.accept(commandString, message); - } - } - } - } - - private static String getSender(String message) { - var sender = message.substring(message.indexOf("§8> ") + 4, message.indexOf(":")).trim(); - int rank = sender.indexOf("]"); - if (rank != -1) { - sender = sender.substring(rank + 1).trim(); - } - - sender = CookiesUtils.stripColor(sender); - return sender; - } - - - @Getter - public static class PartyCommand { - private final Supplier predicate; - protected BiConsumer action; - - public PartyCommand(Supplier predicate, BiConsumer action) { - this.predicate = predicate; - this.action = action; - } - } - - public static class DownTimeCommand extends PartyCommand { - private final ArrayList downTimesThisInstance = new ArrayList<>(); - - public DownTimeCommand(Supplier predicate) { - super(predicate, null); - ChatMessageEvents.BEFORE_MODIFY.register((text, b) -> { - if (b) return; - var message = CookiesUtils.stripColor(text.getString()); - if (!message.matches(".*> EXTRA STATS <.*")) return; - OnInstanceOver(); - }); - this.action = this::RunAction; - } - - private void RunAction(String command, String message) { - var sender = getSender(message); - - if (downTimesThisInstance.stream().anyMatch(x -> x.Requester.equals(sender))) { - ChatUtils.sendPartyMessage(sender + " has already requested down time."); - return; - } - - var split = command.split(" "); - - switch (split.length) { - case 1 -> downTimesThisInstance.add(new DownTimeAction("Not Specified", sender, null)); - case 2 -> downTimesThisInstance.add(new DownTimeAction(split[1], sender, null)); - case 3 -> downTimesThisInstance.add(new DownTimeAction(split[1], sender, split[2])); - } - - ChatUtils.sendPartyMessage("Down time requested by " + sender); - } - - private void OnInstanceOver() { - downTimesThisInstance.forEach(DownTimeAction::OnInstanceOver); - downTimesThisInstance.clear(); - } - - - public static class DownTimeAction { - private final String Reason; - private final String Requester; - private final String Duration; - - public DownTimeAction(String Reason, String Requester, String Duration) { - this.Reason = Reason; - this.Requester = Requester; - this.Duration = Duration; - } - - public void OnInstanceOver() { - StringBuilder message = new StringBuilder("Down time requested by " + Requester); - if (Duration != null) { - message.append(" for ").append(Duration); - } - if (Reason != null) { - message.append(" because: ").append(Reason); - } - - ChatUtils.sendPartyMessage(message.toString()); - } - } - } + public static void register() { + ChatMessageEvents.BEFORE_MODIFY.register(PartyCommandsFeature::handleChat); + } + + private static final Random random = new Random(); + + private static final Map, PartyCommand> commandMap = Map.ofEntries( + entry(command -> command.equals("ptme"), new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("ptme"), (command, message) -> { + var sender = getSender(message); + if (sender.equals(MinecraftClient.getInstance().getSession().getUsername())) { + return; + } + CookiesUtils.sendCommand("p transfer " + sender); + })), + entry(command -> command.equals("warp"), new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("warp"), (command, message) -> { + CookiesUtils.sendCommand("p warp"); + })), + entry(command -> { + //only match f[1-7], m[1-7], and t[1-5] + return command.matches("f[1-7]|m[1-7]|t[1-5]"); + }, new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("joinInstance"), (command, message) -> { + StringBuilder joinInstanceCommand = new StringBuilder("joininstance "); + switch (command.charAt(0)) { + case 'f' -> joinInstanceCommand.append("CATACOMBS_FLOOR_"); + case 'm' -> joinInstanceCommand.append("MASTER_CATACOMBS_FLOOR_"); + case 't' -> { + joinInstanceCommand.append("KUUDRA_"); + switch (command.charAt(1)) { + case '1' -> joinInstanceCommand.append("BASIC"); + case '2' -> joinInstanceCommand.append("HOT"); + case '3' -> joinInstanceCommand.append("BURNING"); + case '4' -> joinInstanceCommand.append("FIERY"); + case '5' -> joinInstanceCommand.append("INFERNAL"); + } + CookiesUtils.sendCommand(joinInstanceCommand.toString()); + return; + } + } + + switch (command.charAt(1)) { + case '1' -> joinInstanceCommand.append("ONE"); + case '2' -> joinInstanceCommand.append("TWO"); + case '3' -> joinInstanceCommand.append("THREE"); + case '4' -> joinInstanceCommand.append("FOUR"); + case '5' -> joinInstanceCommand.append("FIVE"); + case '6' -> joinInstanceCommand.append("SIX"); + case '7' -> joinInstanceCommand.append("SEVEN"); + } + + CookiesUtils.sendCommand(joinInstanceCommand.toString()); + })), + entry(command -> command.startsWith("dt"), new DownTimeCommand(() -> PartyCommandsFeature.shouldRunCommand("dt"))), + entry(command -> command.equals("cf"), new PartyCommand(() -> PartyCommandsFeature.shouldRunCommand("cf"), (command, message) -> { + if (random.nextBoolean()) { + ChatUtils.sendPartyMessage("HEADS!"); + } else { + ChatUtils.sendPartyMessage("TAILS!"); + } + })) + ); + + private static boolean shouldRunCommand(String partyCommand) { + PartyUtils.request(); + var player = CookiesUtils.getPlayer(); + if (partyCommand.matches("f[1-7]|m[1-7]|t[1-5]")) { + partyCommand = "joininstance"; + } + return Optional.ofNullable(DungeonConfig.getInstance().partyChatCommandsFoldable.partyChatCommands.get(partyCommand)).map(Option::getValue).orElse(true) && player.filter(clientPlayerEntity -> PartyUtils.isInParty() && PartyUtils.getPartyLeader().equals(clientPlayerEntity.getUuid())).isPresent(); + } + + private static void handleChat(Text text, boolean b) { + if (b) { + return; + } + var message = text.getString(); + + if (!message.startsWith("§9Party §8> ")) { + return; + } + + var commandString = CookiesUtils.stripColor(message.substring(message.indexOf(": !") + 3).trim()); + + var command = commandMap.entrySet().stream().filter(commandEntry -> commandEntry.getKey().test(commandString)).findFirst(); + + if (command.isPresent()) { + if (command.get().getValue().getPredicate().get()) { + var action = command.get().getValue().getAction(); + if (action != null) { + action.accept(commandString, message); + } + } + } + } + + private static String getSender(String message) { + var sender = message.substring(message.indexOf("§8> ") + 4, message.indexOf(":")).trim(); + int rank = sender.indexOf("]"); + if (rank != -1) { + sender = sender.substring(rank + 1).trim(); + } + + sender = CookiesUtils.stripColor(sender); + return sender; + } + + + @Getter + public static class PartyCommand { + private final Supplier predicate; + protected BiConsumer action; + + public PartyCommand(Supplier predicate, BiConsumer action) { + this.predicate = predicate; + this.action = action; + } + } + + public static class DownTimeCommand extends PartyCommand { + private final ArrayList downTimesThisInstance = new ArrayList<>(); + + public DownTimeCommand(Supplier predicate) { + super(predicate, null); + ChatMessageEvents.BEFORE_MODIFY.register((text, b) -> { + if (b) { + return; + } + var message = CookiesUtils.stripColor(text.getString()); + if (!message.matches(".*> EXTRA STATS <.*")) { + return; + } + onInstanceOver(); + }); + this.action = this::runAction; + } + + private void runAction(String command, String message) { + var sender = getSender(message); + + if (downTimesThisInstance.stream().anyMatch(x -> x.requester.equals(sender))) { + ChatUtils.sendPartyMessage(sender + " has already requested down time."); + return; + } + + var split = command.split(" "); + + switch (split.length) { + case 1 -> downTimesThisInstance.add(new DownTimeAction("Not Specified", sender, null)); + case 2 -> downTimesThisInstance.add(new DownTimeAction(split[1], sender, null)); + case 3 -> downTimesThisInstance.add(new DownTimeAction(split[1], sender, split[2])); + } + + ChatUtils.sendPartyMessage("Down time requested by " + sender); + } + + private void onInstanceOver() { + downTimesThisInstance.forEach(DownTimeAction::onInstanceOver); + downTimesThisInstance.clear(); + } + + + public static class DownTimeAction { + private final String reason; + private final String requester; + private final String duration; + + public DownTimeAction(String reason, String requester, String duration) { + this.reason = reason; + this.requester = requester; + this.duration = duration; + } + + public void onInstanceOver() { + StringBuilder message = new StringBuilder("Down time requested by " + requester); + if (duration != null) { + message.append(" for ").append(duration); + } + if (reason != null) { + message.append(" because: ").append(reason); + } + + ChatUtils.sendPartyMessage(message.toString()); + } + } + } }