Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main/java/tomato/backend/data/TomatoData.java
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,53 @@ public boolean isItemPing(String item) {
return false;
}

/**
* Checks if any enchant in the enchant text matches selected enchant pings
*/
public boolean isEnchantPing(String enchantText) {
if (enchantText == null || enchantText.isEmpty()) {
return false;
}

// Load saved enchant ping selections
String saved = PropertiesManager.getProperty("enchantPing.selected");
if (saved == null || saved.trim().isEmpty()) {
return false;
}

Set<Short> selectedEnchants = new HashSet<>();
String[] parts = saved.split(",");
for (String p : parts) {
try {
short v = Short.parseShort(p.trim());
selectedEnchants.add(v);
} catch (NumberFormatException ignored) {}
}

// Check each enchant line against selected enchants
// Format is "EnchantName(ID)" per line
String[] enchantLines = enchantText.split("\n");
for (String enchantLine : enchantLines) {
// Parse enchant ID from the line (format: "EnchantName(ID)")
if (enchantLine.contains("(") && enchantLine.contains(")")) {
try {
int start = enchantLine.lastIndexOf("(") + 1;
int end = enchantLine.lastIndexOf(")");
String idStr = enchantLine.substring(start, end);
short enchantId = Short.parseShort(idStr);
if (selectedEnchants.contains(enchantId)) {
return true;
}
} catch (
NumberFormatException
| IndexOutOfBoundsException ignored
) {}
}
}

return false;
}

/**
* Get a list property from the data storage.
*
Expand Down
163 changes: 104 additions & 59 deletions src/main/java/tomato/gui/maingui/EnchantPingGUI.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package tomato.gui.maingui;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -12,11 +9,15 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import tomato.realmshark.ParseEnchants;
import tomato.realmshark.Sound;
import util.PropertiesManager;

public class EnchantPingGUI extends JPanel {

private static final long serialVersionUID = 1L;

private final JTextField searchField = new JTextField(20);
Expand All @@ -38,8 +39,7 @@ public EnchantPingGUI(List<String> items) {
try {
short v = Short.parseShort(p.trim());
savedSelected.add(v);
} catch (NumberFormatException ignored) {
}
} catch (NumberFormatException ignored) {}
}
}

Expand All @@ -52,7 +52,9 @@ public EnchantPingGUI(List<String> items) {
// Middle: scroll pane with checkbox list (grouped)
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(listPanel);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
);
this.add(scrollPane, BorderLayout.CENTER);

// Bottom: save button + global controls
Expand All @@ -69,39 +71,54 @@ public EnchantPingGUI(List<String> items) {
buildGroupedListFromParseEnchants();

// Search filtering: simple filter that shows matching checkboxes (expands groups with matches)
searchField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
filterList();
}
searchField
.getDocument()
.addDocumentListener(
new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
filterList();
}

@Override
public void removeUpdate(DocumentEvent e) {
filterList();
}
@Override
public void removeUpdate(DocumentEvent e) {
filterList();
}

@Override
public void changedUpdate(DocumentEvent e) {
filterList();
}
});
@Override
public void changedUpdate(DocumentEvent e) {
filterList();
}
}
);

// Save action - persist IDs
saveButton.addActionListener(e -> {
List<String> ids = new ArrayList<>();
for (Map.Entry<Short, JCheckBox> en : checkBoxMap.entrySet()) {
if (en.getValue().isSelected()) ids.add(Short.toString(en.getKey()));
if (en.getValue().isSelected()) ids.add(
Short.toString(en.getKey())
);
}
PropertiesManager.setProperties("enchantPing.selected", String.join(",", ids));
JOptionPane.showMessageDialog(EnchantPingGUI.this,
"Saved " + ids.size() + " items.",
"Save",
JOptionPane.INFORMATION_MESSAGE);
PropertiesManager.setProperties(
"enchantPing.selected",
String.join(",", ids)
);
JOptionPane.showMessageDialog(
EnchantPingGUI.this,
"Saved " + ids.size() + " items.",
"Save",
JOptionPane.INFORMATION_MESSAGE
);
});

// Global select/clear
selectAllBtn.addActionListener(e -> checkBoxMap.values().forEach(cb -> cb.setSelected(true)));
clearAllBtn.addActionListener(e -> checkBoxMap.values().forEach(cb -> cb.setSelected(false)));
selectAllBtn.addActionListener(e ->
checkBoxMap.values().forEach(cb -> cb.setSelected(true))
);
clearAllBtn.addActionListener(e ->
checkBoxMap.values().forEach(cb -> cb.setSelected(false))
);
}

// Build grouped UI using ParseEnchants.ENCHANTS
Expand All @@ -112,22 +129,32 @@ private void buildGroupedListFromParseEnchants() {
// Group enchants by first character (A-Z or Unique for all caps)
Map<String, List<Map.Entry<Short, String>>> groups = new TreeMap<>();

ParseEnchants.ENCHANTS.entrySet().stream()
.sorted(Comparator.comparing(e -> e.getValue().toLowerCase()))
.forEach(entry -> {
String name = entry.getValue();
if (name.equals(name.toUpperCase())) {
// All uppercase names go to "Unique" group
groups.computeIfAbsent("Unique", k -> new ArrayList<>()).add(entry);
return;
}
char c = name.isEmpty() ? '#' : name.charAt(0);
String key = (Character.isLetter(c)) ? String.valueOf(Character.toUpperCase(c)) : "#";
groups.computeIfAbsent(key, k -> new java.util.ArrayList<>()).add(entry);
});
ParseEnchants.ENCHANTS.entrySet()
.stream()
.sorted(Comparator.comparing(e -> e.getValue().toLowerCase()))
.forEach(entry -> {
String name = entry.getValue();
if (name.equals(name.toUpperCase())) {
// All uppercase names go to "Unique" group
groups
.computeIfAbsent("Unique", k -> new ArrayList<>())
.add(entry);
return;
}
char c = name.isEmpty() ? '#' : name.charAt(0);
String key = (Character.isLetter(c))
? String.valueOf(Character.toUpperCase(c))
: "#";
groups
.computeIfAbsent(key, k -> new java.util.ArrayList<>())
.add(entry);
});

// For each group, create a collapsible panel
for (Map.Entry<String, List<Map.Entry<Short, String>>> g : groups.entrySet()) {
for (Map.Entry<
String,
List<Map.Entry<Short, String>>
> g : groups.entrySet()) {
String groupName = g.getKey();
List<Map.Entry<Short, String>> entries = g.getValue();

Expand All @@ -137,12 +164,14 @@ private void buildGroupedListFromParseEnchants() {

// Header with toggle and group controls
JPanel header = new JPanel(new BorderLayout());
// header.setBackground(new Color(0,0,0,0));
// header.setBackground(new Color(0,0,0,0));
JButton toggle = new JButton("▶ " + groupName);
toggle.setFocusPainted(false);
toggle.setBorderPainted(false);
toggle.setContentAreaFilled(false);
JPanel headerRight = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
JPanel headerRight = new JPanel(
new FlowLayout(FlowLayout.RIGHT, 5, 0)
);
headerRight.setOpaque(false);
JButton groupSelect = new JButton("All");
JButton groupClear = new JButton("Clear");
Expand Down Expand Up @@ -171,7 +200,9 @@ private void buildGroupedListFromParseEnchants() {
}

// If any of the group's items were saved as selected, expand this group by default
boolean groupHasSaved = entries.stream().anyMatch(en -> savedSelected.contains(en.getKey()));
boolean groupHasSaved = entries
.stream()
.anyMatch(en -> savedSelected.contains(en.getKey()));
content.setVisible(groupHasSaved ? true : false);
groupContainer.add(content, BorderLayout.CENTER);
listPanel.add(groupContainer);
Expand All @@ -192,16 +223,19 @@ private void buildGroupedListFromParseEnchants() {
if (groupHasSaved) {
toggle.setText("▼ " + groupName);
}
;
// Group select/clear actions
groupSelect.addActionListener(e -> entries.forEach(en -> {
JCheckBox cb = checkBoxMap.get(en.getKey());
if (cb != null) cb.setSelected(true);
}));
groupClear.addActionListener(e -> entries.forEach(en -> {
JCheckBox cb = checkBoxMap.get(en.getKey());
if (cb != null) cb.setSelected(false);
}));
groupSelect.addActionListener(e ->
entries.forEach(en -> {
JCheckBox cb = checkBoxMap.get(en.getKey());
if (cb != null) cb.setSelected(true);
})
);
groupClear.addActionListener(e ->
entries.forEach(en -> {
JCheckBox cb = checkBoxMap.get(en.getKey());
if (cb != null) cb.setSelected(false);
})
);
}

listPanel.revalidate();
Expand Down Expand Up @@ -231,9 +265,15 @@ private void filterList() {
// Alternative: find content by scanning children
if (content == null) {
for (Component c : groupContainer.getComponents()) {
if (c instanceof JPanel && ((JPanel) c).getComponentCount() > 0) {
if (
c instanceof JPanel &&
((JPanel) c).getComponentCount() > 0
) {
JPanel p = (JPanel) c;
if (p.getComponentCount() > 0 && p.getComponent(0) instanceof JCheckBox) {
if (
p.getComponentCount() > 0 &&
p.getComponent(0) instanceof JCheckBox
) {
content = p;
break;
}
Expand All @@ -246,7 +286,8 @@ private void filterList() {
for (Component itemComp : content.getComponents()) {
if (!(itemComp instanceof JCheckBox)) continue;
JCheckBox cb = (JCheckBox) itemComp;
boolean matches = q.isEmpty() || cb.getText().toLowerCase().contains(q);
boolean matches =
q.isEmpty() || cb.getText().toLowerCase().contains(q);
cb.setVisible(matches);
if (matches) groupHasMatch = true;
}
Expand All @@ -260,7 +301,9 @@ private void filterList() {
if (hc instanceof JButton) {
JButton tb = (JButton) hc;
String text = tb.getText();
if (!text.startsWith("▼")) tb.setText("▼ " + text.replaceAll("^[▶▼] ", ""));
if (!text.startsWith("▼")) tb.setText(
"▼ " + text.replaceAll("^[▶▼] ", "")
);
}
}
}
Expand Down Expand Up @@ -296,6 +339,7 @@ public void setItems(List<String> items) {
* Call EnchantPingGUI.open() from other code to show the dialog.
*/
public static void open() {
Sound.custom.play();
open(java.util.Collections.emptyList());
}

Expand All @@ -305,6 +349,7 @@ public static void open() {
* @param items list of strings to populate checkboxes (ignored currently, ParseEnchants used)
*/
public static void open(List<String> items) {
Sound.custom.play();
JFrame parent = tomato.gui.TomatoGUI.getFrame();
EnchantPingGUI panel = new EnchantPingGUI(items);
JDialog dialog = new JDialog(parent, "Enchant Pings", true);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tomato/gui/stats/LootGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ private static int displayBagLootIcons(
Sound.custom.play();
}

// Check for enchant pings
if (data.isEnchantPing(enchantText)) {
Sound.custom.play();
}

if (
enchants != null &&
i < enchants.length &&
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tomato/realmshark/SendLoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ public static void sendLoot(

if (!enchantText.isEmpty()) {
sl = Math.min(4, enchantText.split("\n").length);

// Check for enchant pings
if (data.isEnchantPing(enchantText)) {
Sound.custom.play();
}
}
}

Expand Down