Skip to content

Commit 5a19bcc

Browse files
committed
Allow removing individual instruments
1 parent 5ca6e63 commit 5a19bcc

3 files changed

Lines changed: 38 additions & 11 deletions

File tree

‎src/main/java/net/raphimc/noteblocktool/elements/table/instrument/InstrumentsTable.java‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,36 @@
2626

2727
public class InstrumentsTable extends JTable {
2828

29+
public static final Object KEEP = new Object() {
30+
@Override
31+
public String toString() {
32+
return "";
33+
}
34+
};
35+
public static final Object REMOVE = new Object() {
36+
@Override
37+
public String toString() {
38+
return "<html><i>Remove";
39+
}
40+
};
41+
2942
public InstrumentsTable(final boolean addEmptyEntry) {
3043
super(new InstrumentsModel("Original", "Replacement"));
3144
this.getTableHeader().setReorderingAllowed(false);
3245

33-
final JComboBox<MinecraftInstrument> instruments = new JComboBox<>();
46+
final JComboBox instruments = new JComboBox<>();
3447
if (addEmptyEntry) {
35-
instruments.addItem(null);
48+
instruments.addItem(KEEP);
49+
instruments.addItem(REMOVE);
3650
}
3751
for (MinecraftInstrument instrument : MinecraftInstrument.values()) {
3852
instruments.addItem(instrument);
3953
}
4054
this.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(instruments));
4155
}
4256

43-
public void addRow(final String original, final MinecraftInstrument instrument) {
44-
((DefaultTableModel) this.getModel()).addRow(new Object[]{original, instrument});
57+
public void addRow(final String original) {
58+
((DefaultTableModel) this.getModel()).addRow(new Object[]{original, KEEP});
4559
}
4660

4761
@Override

‎src/main/java/net/raphimc/noteblocktool/frames/edittabs/CustomInstrumentsTab.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import javax.swing.JPanel;
2929
import java.util.HashMap;
30+
import java.util.HashSet;
3031
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Set;
@@ -48,17 +49,22 @@ protected void initComponents(final JPanel center) {
4849
this.add(new FastScrollPane(this.table));
4950
this.usedInstruments = SongUtil.getUsedNbsCustomInstruments(this.songs.get(0).song());
5051
for (NbsCustomInstrument customInstrument : this.usedInstruments) {
51-
this.table.addRow(customInstrument.getNameOr("No Name") + " (" + customInstrument.getSoundFilePathOr("No Sound File") + ")", null);
52+
this.table.addRow(customInstrument.getNameOr("No Name") + " (" + customInstrument.getSoundFilePathOr("No Sound File") + ")");
5253
}
5354
}
5455

5556
@Override
5657
public void apply(final Song song) {
5758
final Map<NbsCustomInstrument, MinecraftInstrument> replacements = new HashMap<>();
59+
final Set<NbsCustomInstrument> remove = new HashSet<>();
5860
int i = 0;
5961
for (NbsCustomInstrument customInstrument : this.usedInstruments) {
60-
final MinecraftInstrument replacement = (MinecraftInstrument) this.table.getValueAt(i, 1);
61-
replacements.put(customInstrument, replacement);
62+
Object rawReplacement = this.table.getValueAt(i, 1);
63+
if (rawReplacement instanceof MinecraftInstrument replacement) {
64+
replacements.put(customInstrument, replacement);
65+
} else if (rawReplacement == InstrumentsTable.REMOVE) {
66+
remove.add(customInstrument);
67+
}
6268
i++;
6369
}
6470
song.getNotes().forEach(note -> {
@@ -67,6 +73,7 @@ public void apply(final Song song) {
6773
note.setInstrument(replacement);
6874
}
6975
});
76+
song.getNotes().removeIf(note -> remove.contains(note.getInstrument()));
7077
}
7178

7279
}

‎src/main/java/net/raphimc/noteblocktool/frames/edittabs/InstrumentsTab.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.swing.JPanel;
2828
import java.util.EnumSet;
2929
import java.util.HashMap;
30+
import java.util.HashSet;
3031
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Set;
@@ -53,17 +54,22 @@ protected void initComponents(final JPanel center) {
5354
return a;
5455
});
5556
for (MinecraftInstrument instrument : this.usedInstruments) {
56-
this.table.addRow(instrument.name(), instrument);
57+
this.table.addRow(instrument.name());
5758
}
5859
}
5960

6061
@Override
6162
public void apply(final Song song) {
6263
final Map<MinecraftInstrument, MinecraftInstrument> replacements = new HashMap<>();
64+
final Set<MinecraftInstrument> remove = new HashSet<>();
6365
int i = 0;
6466
for (MinecraftInstrument instrument : this.usedInstruments) {
65-
final MinecraftInstrument replacement = (MinecraftInstrument) this.table.getValueAt(i, 1);
66-
replacements.put(instrument, replacement);
67+
Object rawReplacement = this.table.getValueAt(i, 1);
68+
if (rawReplacement instanceof MinecraftInstrument replacement) {
69+
replacements.put(instrument, replacement);
70+
} else if (rawReplacement == InstrumentsTable.REMOVE) {
71+
remove.add(instrument);
72+
}
6773
i++;
6874
}
6975
song.getNotes().forEach(note -> {
@@ -72,7 +78,7 @@ public void apply(final Song song) {
7278
note.setInstrument(replacement);
7379
}
7480
});
75-
song.getNotes().removeIf(note -> replacements.containsKey(note.getInstrument()) && replacements.get(note.getInstrument()) == null);
81+
song.getNotes().removeIf(note -> remove.contains(note.getInstrument()));
7682
}
7783

7884
}

0 commit comments

Comments
 (0)