|
6 | 6 | package meteordevelopment.meteorclient.gui.screens.settings; |
7 | 7 |
|
8 | 8 | import meteordevelopment.meteorclient.gui.GuiTheme; |
9 | | -import meteordevelopment.meteorclient.gui.WindowScreen; |
10 | 9 | import meteordevelopment.meteorclient.gui.renderer.GuiRenderer; |
11 | | -import meteordevelopment.meteorclient.gui.widgets.containers.WTable; |
12 | | -import meteordevelopment.meteorclient.gui.widgets.input.WTextBox; |
| 10 | +import meteordevelopment.meteorclient.gui.screens.settings.base.CollectionMapSettingScreen; |
| 11 | +import meteordevelopment.meteorclient.gui.widgets.WWidget; |
13 | 12 | import meteordevelopment.meteorclient.gui.widgets.pressable.WButton; |
14 | 13 | import meteordevelopment.meteorclient.settings.BlockDataSetting; |
15 | 14 | import meteordevelopment.meteorclient.settings.IBlockData; |
|
18 | 17 | import meteordevelopment.meteorclient.utils.misc.ISerializable; |
19 | 18 | import meteordevelopment.meteorclient.utils.misc.Names; |
20 | 19 | import net.minecraft.block.Block; |
| 20 | +import net.minecraft.block.Blocks; |
| 21 | +import net.minecraft.client.gui.DrawContext; |
21 | 22 | import net.minecraft.registry.Registries; |
22 | | -import org.apache.commons.lang3.StringUtils; |
23 | | - |
24 | | -import java.util.ArrayList; |
25 | | -import java.util.List; |
| 23 | +import org.jetbrains.annotations.Nullable; |
26 | 24 |
|
27 | 25 | import static meteordevelopment.meteorclient.MeteorClient.mc; |
28 | 26 |
|
29 | | -public class BlockDataSettingScreen extends WindowScreen { |
30 | | - private static final List<Block> BLOCKS = new ArrayList<>(100); |
31 | | - |
32 | | - private final BlockDataSetting<?> setting; |
33 | | - |
34 | | - private WTable table; |
35 | | - private String filterText = ""; |
| 27 | +public class BlockDataSettingScreen<T extends ICopyable<T> & ISerializable<T> & IChangeable & IBlockData<T>> extends CollectionMapSettingScreen<Block, T> { |
| 28 | + private final BlockDataSetting<T> setting; |
| 29 | + private boolean invalidate; |
36 | 30 |
|
37 | | - public BlockDataSettingScreen(GuiTheme theme, BlockDataSetting<?> setting) { |
38 | | - super(theme, "Configure Blocks"); |
| 31 | + public BlockDataSettingScreen(GuiTheme theme, BlockDataSetting<T> setting) { |
| 32 | + super(theme, "Configure Blocks", setting, setting.get(), Registries.BLOCK); |
39 | 33 |
|
40 | 34 | this.setting = setting; |
41 | 35 | } |
42 | 36 |
|
43 | 37 | @Override |
44 | | - public void initWidgets() { |
45 | | - WTextBox filter = add(theme.textBox("")).minWidth(400).expandX().widget(); |
46 | | - filter.setFocused(true); |
47 | | - filter.action = () -> { |
48 | | - filterText = filter.get().trim(); |
49 | | - |
50 | | - table.clear(); |
51 | | - initTable(); |
52 | | - }; |
53 | | - |
54 | | - table = add(theme.table()).expandX().widget(); |
55 | | - |
56 | | - initTable(); |
| 38 | + protected boolean includeValue(Block value) { |
| 39 | + return value != Blocks.AIR; |
57 | 40 | } |
58 | 41 |
|
59 | | - @SuppressWarnings("unchecked") |
60 | | - public <T extends ICopyable<T> & ISerializable<T> & IChangeable & IBlockData<T>> void initTable() { |
61 | | - for (Block block : Registries.BLOCK) { |
62 | | - T blockData = (T) setting.get().get(block); |
63 | | - |
64 | | - if (blockData != null && blockData.isChanged()) BLOCKS.addFirst(block); |
65 | | - else BLOCKS.add(block); |
66 | | - } |
67 | | - |
68 | | - for (Block block : BLOCKS) { |
69 | | - String name = Names.get(block); |
70 | | - if (!StringUtils.containsIgnoreCase(name, filterText)) continue; |
71 | | - |
72 | | - T blockData = (T) setting.get().get(block); |
73 | | - |
74 | | - table.add(theme.itemWithLabel(block.asItem().getDefaultStack(), Names.get(block))).expandCellX(); |
75 | | - table.add(theme.label((blockData != null && blockData.isChanged()) ? "*" : " ")); |
76 | | - |
77 | | - WButton edit = table.add(theme.button(GuiRenderer.EDIT)).widget(); |
78 | | - edit.action = () -> { |
79 | | - T data = blockData; |
80 | | - if (data == null) data = (T) setting.defaultData.get().copy(); |
81 | | - |
82 | | - mc.setScreen(data.createScreen(theme, block, (BlockDataSetting<T>) setting)); |
83 | | - }; |
84 | | - |
85 | | - WButton reset = table.add(theme.button(GuiRenderer.RESET)).widget(); |
86 | | - reset.action = () -> { |
87 | | - setting.get().remove(block); |
88 | | - setting.onChanged(); |
| 42 | + @Override |
| 43 | + protected WWidget getValueWidget(Block block) { |
| 44 | + return theme.itemWithLabel(block.asItem().getDefaultStack(), Names.get(block)); |
| 45 | + } |
89 | 46 |
|
90 | | - if (blockData != null && blockData.isChanged()) { |
91 | | - table.clear(); |
92 | | - initTable(); |
93 | | - } |
94 | | - }; |
| 47 | + @Override |
| 48 | + protected WWidget getDataWidget(Block block, @Nullable T blockData) { |
| 49 | + WButton edit = theme.button(GuiRenderer.EDIT); |
| 50 | + edit.action = () -> { |
| 51 | + T data = blockData; |
| 52 | + if (data == null) data = setting.defaultData.get().copy(); |
| 53 | + |
| 54 | + mc.setScreen(data.createScreen(theme, block, setting)); |
| 55 | + invalidate = true; |
| 56 | + }; |
| 57 | + return edit; |
| 58 | + } |
95 | 59 |
|
96 | | - table.row(); |
| 60 | + @Override |
| 61 | + protected void onRenderBefore(DrawContext drawContext, float delta) { |
| 62 | + if (invalidate) { |
| 63 | + this.invalidateTable(); |
| 64 | + invalidate = false; |
97 | 65 | } |
| 66 | + } |
98 | 67 |
|
99 | | - BLOCKS.clear(); |
| 68 | + @Override |
| 69 | + protected String[] getValueNames(Block block) { |
| 70 | + return new String[]{ |
| 71 | + Names.get(block), |
| 72 | + Registries.BLOCK.getId(block).toString() |
| 73 | + }; |
100 | 74 | } |
101 | 75 | } |
0 commit comments