Skip to content

Commit 3cdeb20

Browse files
authored
Tag manager (#771) (#772)
1 parent 36b50c5 commit 3cdeb20

File tree

3 files changed

+102
-20
lines changed

3 files changed

+102
-20
lines changed

src/main/java/com/ldtteam/structurize/client/gui/WindowTagTool.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import com.ldtteam.structurize.Network;
88
import com.ldtteam.structurize.api.util.constant.Constants;
99
import com.ldtteam.structurize.blockentities.interfaces.IBlueprintDataProviderBE;
10+
import com.ldtteam.structurize.blocks.interfaces.IAnchorBlock;
1011
import com.ldtteam.structurize.items.ItemTagTool;
1112
import com.ldtteam.structurize.network.messages.AddRemoveTagMessage;
1213
import com.ldtteam.structurize.network.messages.SetTagInTool;
1314
import com.ldtteam.structurize.util.BlockUtils;
15+
import com.ldtteam.structurize.util.TagManager;
1416
import net.minecraft.client.Minecraft;
1517
import net.minecraft.network.chat.Component;
1618
import net.minecraft.world.item.ItemStack;
@@ -20,20 +22,8 @@
2022

2123
import java.util.*;
2224

23-
import static com.ldtteam.structurize.api.util.constant.Constants.GROUNDLEVEL_TAG;
24-
2525
public class WindowTagTool extends AbstractWindowSkeleton
2626
{
27-
/**
28-
* List of tag options. Mods can just insert on this in mod constructor.
29-
*/
30-
public static List<String> TAG_OPTIONS = new ArrayList<>();
31-
32-
static
33-
{
34-
TAG_OPTIONS.add(GROUNDLEVEL_TAG);
35-
}
36-
3727
private static final String WINDOW_TAG_TOOL = ":gui/windowtagtool.xml";
3828
private static final String INPUT_FIELD = "currentTag";
3929
private static final String LIST_TAG_POS = "tagposlist";
@@ -75,12 +65,16 @@ public class WindowTagTool extends AbstractWindowSkeleton
7565
*/
7666
private List<BlockPos> positionsList = Collections.emptyList();
7767

78-
7968
/**
8069
* The tags list
8170
*/
8271
private ScrollingList tagOptionList;
8372

73+
/**
74+
* Tag options.
75+
*/
76+
private List<String> tagOptions = new ArrayList<>();
77+
8478
/**
8579
* Constructor for the skeleton class of the windows.
8680
*/
@@ -92,13 +86,23 @@ public WindowTagTool(String currentTag, BlockPos anchorPos, final Level world, f
9286
this.anchorPos = anchorPos;
9387
this.stack = stack;
9488

89+
tagOptions.addAll(TagManager.getGlobalTagOptions());
90+
91+
if (anchorPos != null)
92+
{
93+
final BlockEntity blockEntity = world.getBlockEntity(anchorPos);
94+
if (blockEntity instanceof IAnchorBlock anchorBlock)
95+
{
96+
tagOptions.addAll(TagManager.getMatchingTagOptions(anchorBlock));
97+
}
98+
}
9599
registerButton(TAG_SELECT, this::tagOptionSelected);
96100
}
97101

98102
private void tagOptionSelected(final Button button)
99103
{
100104
final int row = tagOptionList.getListElementIndexByPane(button);
101-
this.currentTag = TAG_OPTIONS.get(row);
105+
this.currentTag = tagOptions.get(row);
102106
findPaneOfTypeByID(INPUT_FIELD, TextField.class).setText(currentTag);
103107
}
104108

@@ -238,19 +242,19 @@ public void updateTagOptionList()
238242
@Override
239243
public int getElementCount()
240244
{
241-
return TAG_OPTIONS.size();
245+
return tagOptions.size();
242246
}
243247

244248
@Override
245249
public void updateElement(final int index, final Pane rowPane)
246250
{
247251
final Text tagsText = rowPane.findPaneOfTypeByID(TAG_TEXT, Text.class);
248-
tagsText.setText(Component.literal(TAG_OPTIONS.get(index)));
252+
tagsText.setText(Component.literal(tagOptions.get(index)));
249253
PaneBuilders.tooltipBuilder().hoverPane(tagsText).build()
250-
.setText(Component.translatable("com.ldtteam.tag.tooltip." + TAG_OPTIONS.get(index)));
254+
.setText(Component.translatable("com.ldtteam.tag.tooltip." + tagOptions.get(index)));
251255

252256
final Button button = rowPane.findPaneOfTypeByID(TAG_SELECT, Button.class);
253-
button.setEnabled(!TAG_OPTIONS.get(index).equals(currentTag));
257+
button.setEnabled(!tagOptions.get(index).equals(currentTag));
254258
}
255259
});
256260
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.ldtteam.structurize.util;
2+
3+
import com.ldtteam.structurize.blocks.interfaces.IAnchorBlock;
4+
5+
import java.util.*;
6+
import java.util.function.Predicate;
7+
import java.util.stream.Collectors;
8+
9+
import static com.ldtteam.structurize.api.util.constant.Constants.GROUNDLEVEL_TAG;
10+
import static com.ldtteam.structurize.api.util.constant.Constants.INVISIBLE_TAG;
11+
12+
/**
13+
* Handles tags.
14+
*/
15+
public class TagManager
16+
{
17+
/**
18+
* List of tag options. Mods can just insert on this in mod constructor.
19+
*/
20+
private static Set<String> GLOBAL_TAG_OPTIONS = new HashSet<>();
21+
22+
/**
23+
* Block specific tag options.
24+
*/
25+
private static Map<String, Predicate<IAnchorBlock>> BLOCK_SPECIFIC_TAG_OPTIONS = new HashMap<>();
26+
27+
static
28+
{
29+
GLOBAL_TAG_OPTIONS.add(GROUNDLEVEL_TAG);
30+
GLOBAL_TAG_OPTIONS.add(INVISIBLE_TAG);
31+
}
32+
/**
33+
* Register a new global tag option.
34+
* @param tag the option to register.
35+
*/
36+
public static void registerGlobalTagOption(final String tag)
37+
{
38+
GLOBAL_TAG_OPTIONS.add(tag);
39+
}
40+
41+
/**
42+
* Register a block specific tag option.
43+
* @param tag the tag to register.
44+
* @param predicate the predicate that has to apply.
45+
*/
46+
public static void registerSpecificTagOption(final String tag, final Predicate<IAnchorBlock> predicate)
47+
{
48+
final Predicate<IAnchorBlock> storedPredicate = BLOCK_SPECIFIC_TAG_OPTIONS.getOrDefault(tag, null);
49+
if (storedPredicate == null)
50+
{
51+
BLOCK_SPECIFIC_TAG_OPTIONS.put(tag, predicate);
52+
}
53+
else
54+
{
55+
BLOCK_SPECIFIC_TAG_OPTIONS.put(tag, storedPredicate.or(predicate));
56+
}
57+
}
58+
59+
/**
60+
* Get all matching tag options for a block.
61+
* @param block the block to match.
62+
* @return collection of options.
63+
*/
64+
public static Collection<String> getMatchingTagOptions(final IAnchorBlock block)
65+
{
66+
return BLOCK_SPECIFIC_TAG_OPTIONS.entrySet().stream().filter(entry -> entry.getValue().test(block)).map(Map.Entry::getKey).collect(Collectors.toSet());
67+
}
68+
69+
/**
70+
* Get all global tag options.
71+
* @return the collection of global tag options.
72+
*/
73+
public static Collection<String> getGlobalTagOptions()
74+
{
75+
return GLOBAL_TAG_OPTIONS;
76+
}
77+
}

src/main/resources/assets/structurize/lang/en_us.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@
219219
"structurize.gui.content.entities": "Entities",
220220
"structurize.gui.content.entities_content": "Entity contents",
221221
"com.ldtteam.structurize.gui.taglist.title": "Existing tags in schematic:",
222-
"com.ldtteam.structurize.gui.tagoptions.title": "Tag Suggestions:",
223-
"com.ldtteam.tag.tooltip.groundlevel": "Used by the Buildtool to position the blueprint on the floor"
222+
"com.ldtteam.structurize.gui.tagoptions.title": "Registered Tags:",
223+
"com.ldtteam.tag.tooltip.groundlevel": "Used by the Buildtool to position the blueprint on the floor",
224+
"com.ldtteam.tag.tooltip.invisible": "Hide Blueprint in Buildtool like Integrated Buildings or Mineshafts"
224225

225226
}

0 commit comments

Comments
 (0)