Skip to content

Commit 470c122

Browse files
added notification sounds
1 parent 8115aa8 commit 470c122

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

src/main/java/nge/lk/mods/simpletabs/GuiTabEditor.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class GuiTabEditor extends GuiFactory implements Consumer<ButtonElement>
6363
*/
6464
private ButtonElement whitelistButton;
6565

66+
/**
67+
* A button for toggling notification sounds.
68+
*/
69+
private ButtonElement notifyButton;
70+
6671
/**
6772
* The button to save the tab and return to the parent.
6873
*/
@@ -108,7 +113,8 @@ public void accept(final ButtonElement buttonElement) {
108113
if (editingTab != null) {
109114
// Save the changes.
110115
editingTab.updatePattern(patternElement.getTextField().getText(),
111-
!((Boolean) expertModeButton.getMetadata()), ((Boolean) whitelistButton.getMetadata()));
116+
!((Boolean) expertModeButton.getMetadata()), ((Boolean) whitelistButton.getMetadata()),
117+
((Boolean) notifyButton.getMetadata()));
112118
editingTab.setPrefix(prefixElement.getTextField().getText());
113119
} else {
114120
// Create a new tab.
@@ -117,7 +123,8 @@ public void accept(final ButtonElement buttonElement) {
117123
patternElement.getTextField().getText(),
118124
!((Boolean) expertModeButton.getMetadata()),
119125
((Boolean) whitelistButton.getMetadata()),
120-
prefixElement.getTextField().getText()
126+
prefixElement.getTextField().getText(),
127+
((Boolean) notifyButton.getMetadata())
121128
);
122129
}
123130
tabManager.saveState();
@@ -138,6 +145,12 @@ public void accept(final ButtonElement buttonElement) {
138145

139146
// Update visuals to reflect the change.
140147
updateCaptions();
148+
} else if (buttonElement == notifyButton) {
149+
// Toggle the button's state in the metadata.
150+
notifyButton.setMetadata(!((Boolean) notifyButton.getMetadata()));
151+
152+
// Update visuals.
153+
updateCaptions();
141154
}
142155
}
143156

@@ -192,8 +205,12 @@ protected void createGui() {
192205
addBlank(new Positioning().relativeWidth(4));
193206

194207
whitelistButton = addButton(this,
195-
new Positioning().relativeWidth(40).absoluteHeight(20).breakRow());
208+
new Positioning().relativeWidth(19).absoluteHeight(20));
196209
whitelistButton.setMetadata(editingTab == null || editingTab.isWhitelist());
210+
addBlank(new Positioning().relativeWidth(1));
211+
212+
notifyButton = addButton(this, new Positioning().relativeWidth(19).absoluteHeight(20).breakRow());
213+
notifyButton.setMetadata(editingTab != null && editingTab.isNotify());
197214
addBlank(new Positioning().breakRow().absoluteHeight(10));
198215

199216
// At this point, all elements having captions are created.
@@ -211,7 +228,9 @@ protected void createGui() {
211228
0xA0A0A0);
212229
addText(new Positioning().breakRow()).setText("You can edit existing tabs by right clicking them",
213230
0xA0A0A0);
214-
addText(new Positioning()).setText("Don't use expert mode unless you understand regular expressions!",
231+
addText(new Positioning().breakRow()).setText("Don't use expert mode unless you understand regular expressions!",
232+
0xA0A0A0);
233+
addText(new Positioning()).setText("Notifications play a sound when a new message is received in this tab",
215234
0xA0A0A0);
216235

217236
saveButton = addButton(this, new Positioning().alignBottom().relativeWidth(27).absoluteHeight(20));
@@ -244,6 +263,9 @@ private void updateCaptions() {
244263
whitelistButton.getButton().displayString =
245264
((Boolean) whitelistButton.getMetadata()) ? "Whitelist" : "Blacklist";
246265

266+
notifyButton.getButton().displayString =
267+
((Boolean) notifyButton.getMetadata()) ? "Notify: Yes" : "Notify: No";
268+
247269
if ((Boolean) expertModeButton.getMetadata()) {
248270
// Provide expert caption.
249271
patternCaption.setText("Tab Pattern (regular expression)", 0xA0A0A0);

src/main/java/nge/lk/mods/simpletabs/tabs/ChatTab.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import lombok.Setter;
55
import net.minecraft.client.Minecraft;
66
import net.minecraft.client.gui.GuiNewChat;
7+
import net.minecraft.init.SoundEvents;
8+
import net.minecraft.util.SoundCategory;
79
import net.minecraft.util.text.ITextComponent;
810

911
import java.util.regex.Matcher;
@@ -39,6 +41,11 @@ public class ChatTab extends GuiNewChat {
3941
*/
4042
@Getter private boolean whitelist;
4143

44+
/**
45+
* Whether this tab will play sound notifications.
46+
*/
47+
@Getter private boolean notify;
48+
4249
/**
4350
* The prefix for sent messages in this tab.
4451
*/
@@ -50,11 +57,12 @@ public class ChatTab extends GuiNewChat {
5057
* @param mc The minecraft reference.
5158
*/
5259
public ChatTab(final Minecraft mc, final String pattern, final boolean literal, final boolean whitelist,
53-
final String prefix) {
60+
final boolean notify, final String prefix) {
5461
super(mc);
5562
this.pattern = pattern;
5663
this.literal = literal;
5764
this.whitelist = whitelist;
65+
this.notify = notify;
5866
this.prefix = prefix;
5967
filter = Pattern.compile(pattern, literal ? Pattern.LITERAL : 0).matcher("");
6068
}
@@ -65,18 +73,27 @@ public ChatTab(final Minecraft mc, final String pattern, final boolean literal,
6573
* @param pattern The new pattern.
6674
* @param literal Whether the pattern will be escaped.
6775
* @param whitelist Whether the tab implements a whitelist or a blacklist.
76+
* @param notify Whether this tab will play notification sounds.
6877
*/
69-
public void updatePattern(final String pattern, final boolean literal, final boolean whitelist) {
78+
public void updatePattern(final String pattern, final boolean literal, final boolean whitelist,
79+
final boolean notify) {
7080
this.pattern = pattern;
7181
this.literal = literal;
7282
this.whitelist = whitelist;
83+
this.notify = notify;
7384
filter = Pattern.compile(pattern, literal ? Pattern.LITERAL : 0).matcher("");
7485
}
7586

7687
@Override
7788
public void printChatMessageWithOptionalDeletion(final ITextComponent chatComponent, final int chatLineId) {
7889
super.printChatMessageWithOptionalDeletion(chatComponent, chatLineId);
7990
unread = true;
91+
if (notify) {
92+
final float before = Minecraft.getMinecraft().gameSettings.getSoundLevel(SoundCategory.RECORDS);
93+
Minecraft.getMinecraft().gameSettings.setSoundLevel(SoundCategory.RECORDS, 1.0f);
94+
Minecraft.getMinecraft().player.playSound(SoundEvents.BLOCK_NOTE_CHIME, 1.0f, 1.0f);
95+
Minecraft.getMinecraft().gameSettings.setSoundLevel(SoundCategory.RECORDS, before);
96+
}
8097
}
8198

8299
/**
@@ -104,6 +121,7 @@ public void markRead() {
104121
* @return The export string.
105122
*/
106123
public String getExport() {
107-
return pattern + "§" + Boolean.toString(literal) + "§" + prefix + "§" + Boolean.toString(whitelist);
124+
return pattern + "§" + Boolean.toString(literal) + "§" + prefix + "§" + Boolean.toString(whitelist)
125+
+ "§" + Boolean.toString(notify);
108126
}
109127
}

src/main/java/nge/lk/mods/simpletabs/tabs/TabIO.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static Iterator<String> getExportIterator(final Iterable<Map<String, Cha
5353
*/
5454
public void saveState(final Iterable<Map<String, ChatTab>> tabs) {
5555
try {
56-
FileUtil.writeLineStorage(3, saveFile, getExportIterator(tabs));
56+
FileUtil.writeLineStorage(4, saveFile, getExportIterator(tabs));
5757
} catch (final IOException e) {
5858
DebugUtil.recoverableError(e);
5959
}
@@ -92,6 +92,12 @@ public String apply(final Integer version, final String line) {
9292
// Change: Added whitelist flag and tab groups.
9393
newLine += "§true";
9494
newLine = "0§" + newLine;
95+
newVersion++;
96+
}
97+
98+
if (newVersion == 3) { // Converter: v3 -> v4
99+
// Change: Added notify flag.
100+
newLine += "§false";
95101
// newVersion++; // Only need this when converting between more versions.
96102
}
97103

@@ -128,8 +134,9 @@ public void accept(final String line, final Integer lineNo) {
128134
final boolean literal = Boolean.parseBoolean(split[3]);
129135
final String prefix = split[4];
130136
final boolean whitelist = Boolean.parseBoolean(split[5]);
137+
final boolean notify = Boolean.parseBoolean(split[6]);
131138
results.get(results.size() - 1).put(tabName,
132-
new ChatTab(Minecraft.getMinecraft(), pattern, literal, whitelist, prefix));
139+
new ChatTab(Minecraft.getMinecraft(), pattern, literal, whitelist, notify, prefix));
133140
}
134141
}
135142
}

src/main/java/nge/lk/mods/simpletabs/tabs/TabManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ public void printChatMessageWithOptionalDeletion(final ITextComponent chatCompon
116116
* @param prefix The prefix for sent chat messages.
117117
*/
118118
public void createTab(final String title, final String pattern, final boolean literal, final boolean whitelist,
119-
final String prefix) {
120-
tabs.get(activeGroup).put(title, new ChatTab(Minecraft.getMinecraft(), pattern, literal, whitelist, prefix));
119+
final String prefix, final boolean notify) {
120+
tabs.get(activeGroup).put(title, new ChatTab(Minecraft.getMinecraft(), pattern, literal, whitelist, notify,
121+
prefix));
121122
}
122123

123124
/**
@@ -266,7 +267,7 @@ private void loadState() {
266267
*/
267268
private void addDefaultTab() {
268269
tabs.get(activeGroup).put("General", new ChatTab(Minecraft.getMinecraft(), ".*", false,
269-
true, ""));
270+
true, false, ""));
270271
}
271272

272273
/**

0 commit comments

Comments
 (0)