From d2b4867530ff2a194b79ce8eff5aa44e7ef1004d Mon Sep 17 00:00:00 2001 From: Kli Kli Date: Tue, 26 Jul 2022 19:45:37 +0200 Subject: [PATCH] Fix tag matcher Closes #9 --- .../demo/entries/features/multiblock.json | 8 +++ .../modonomicon/datagen/BookGenerator.java | 7 ++- .../multiblock/matcher/TagMatcher.java | 54 ++++++++++++------- .../modonomicon_multiblocks/tag.json | 50 +++++++++++++++++ 4 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 src/main/resources/data/modonomicon/modonomicon_multiblocks/tag.json diff --git a/src/generated/resources/data/modonomicon/modonomicons/demo/entries/features/multiblock.json b/src/generated/resources/data/modonomicon/modonomicons/demo/entries/features/multiblock.json index 05f81cf02..ccb90f2d5 100644 --- a/src/generated/resources/data/modonomicon/modonomicons/demo/entries/features/multiblock.json +++ b/src/generated/resources/data/modonomicon/modonomicons/demo/entries/features/multiblock.json @@ -22,6 +22,14 @@ "multiblock_name": "multiblocks.modonomicon.blockentity", "show_visualize_button": true, "text": "book.modonomicon.demo.features.multiblock.preview.text" + }, + { + "type": "modonomicon:multiblock", + "anchor": "", + "multiblock_id": "modonomicon:tag", + "multiblock_name": "", + "show_visualize_button": true, + "text": "" } ], "x": -8, diff --git a/src/main/java/com/klikli_dev/modonomicon/datagen/BookGenerator.java b/src/main/java/com/klikli_dev/modonomicon/datagen/BookGenerator.java index 6489ba470..3043cecfe 100644 --- a/src/main/java/com/klikli_dev/modonomicon/datagen/BookGenerator.java +++ b/src/main/java/com/klikli_dev/modonomicon/datagen/BookGenerator.java @@ -144,13 +144,18 @@ private BookEntryModel makeMultiblockEntry(BookLangHelper helper, EntryLocationH .withText(helper.pageText()) .build(); + helper.page("preview2"); + var multiblockPreviewPage2 = BookMultiblockPageModel.builder() + .withMultiblockId(this.modLoc("tag")) + .build(); + return BookEntryModel.builder() .withId(this.modLoc("features/multiblock")) .withName(helper.entryName()) .withDescription(helper.entryDescription()) .withIcon("minecraft:furnace") .withLocation(entryHelper.get('m')) - .withPages(multiBlockIntroPage, multiblockPreviewPage) + .withPages(multiBlockIntroPage, multiblockPreviewPage, multiblockPreviewPage2) .build(); } diff --git a/src/main/java/com/klikli_dev/modonomicon/multiblock/matcher/TagMatcher.java b/src/main/java/com/klikli_dev/modonomicon/multiblock/matcher/TagMatcher.java index 3b2260f18..e5de864ca 100644 --- a/src/main/java/com/klikli_dev/modonomicon/multiblock/matcher/TagMatcher.java +++ b/src/main/java/com/klikli_dev/modonomicon/multiblock/matcher/TagMatcher.java @@ -6,6 +6,7 @@ package com.klikli_dev.modonomicon.multiblock.matcher; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import com.google.gson.JsonObject; import com.klikli_dev.modonomicon.Modonomicon; @@ -29,6 +30,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Objects; +import java.util.function.Supplier; /** * Matches against the given tag, and optionally checks for the given BlockState properties. @@ -37,19 +39,19 @@ public class TagMatcher implements StateMatcher { public static final ResourceLocation TYPE = Modonomicon.loc("tag"); private final BlockState displayState; - private final TagKey tag; - private final Map props; + private final Supplier> tag; + private final Supplier> props; private final TriPredicate predicate; - protected TagMatcher(TagKey tag, Map props) { + protected TagMatcher( Supplier> tag, Supplier> props) { this(null, tag, props); } - protected TagMatcher(BlockState displayState, TagKey tag, Map props) { + protected TagMatcher(BlockState displayState, Supplier> tag, Supplier> props) { this.displayState = displayState; this.tag = tag; this.props = props; - this.predicate = (blockGetter, blockPos, blockState) -> blockState.is(this.tag) && this.checkProps(blockState); + this.predicate = (blockGetter, blockPos, blockState) -> blockState.is(this.tag.get()) && this.checkProps(blockState); } public static TagMatcher fromJson(JsonObject json) { @@ -62,20 +64,36 @@ public static TagMatcher fromJson(JsonObject json) { } } - try { + //testing=true enables tag parsing //last param = allowNBT var tagString = GsonHelper.getAsString(json, "tag"); if(!tagString.startsWith("#")) { tagString = "#" + tagString; } - var parserResult = BlockStateParser.parseForTesting(Registry.BLOCK, new StringReader(tagString), true).right().orElseThrow(); - var tag = parserResult.tag().unwrap().left().orElseThrow(); - var props = parserResult.vagueProperties(); - return new TagMatcher(displayState, tag, props); - } catch (CommandSyntaxException e) { - throw new IllegalArgumentException("Failed to parse Tag and BlockState properties from json member \"tag\" for TagMatcher.", e); - } + + + String finalTagString = tagString; + Supplier> tagSupplier = Suppliers.memoize(() -> { + try { + var parserResult = BlockStateParser.parseForTesting(Registry.BLOCK, new StringReader(finalTagString), true).right().orElseThrow(); + return parserResult.tag().unwrap().left().orElseThrow(); + } catch (CommandSyntaxException e) { + throw new IllegalArgumentException("Failed to parse Tag and BlockState properties from json member \"tag\" for TagMatcher.", e); + } + }); + + Supplier> propsSupplier = Suppliers.memoize(() -> { + try { + var parserResult = BlockStateParser.parseForTesting(Registry.BLOCK, new StringReader(finalTagString), true).right().orElseThrow(); + return parserResult.vagueProperties(); + } catch (CommandSyntaxException e) { + throw new IllegalArgumentException("Failed to parse Tag and BlockState properties from json member \"tag\" for TagMatcher.", e); + } + }); + + return new TagMatcher(displayState, tagSupplier, propsSupplier); + } public static TagMatcher fromNetwork(FriendlyByteBuf buffer) { @@ -88,14 +106,14 @@ public static TagMatcher fromNetwork(FriendlyByteBuf buffer) { var tag = TagKey.create(Registry.BLOCK_REGISTRY, buffer.readResourceLocation()); var props = buffer.readMap(FriendlyByteBuf::readUtf, FriendlyByteBuf::readUtf); - return new TagMatcher(displayState, tag, props); + return new TagMatcher(displayState, () -> tag, () -> props); } catch (CommandSyntaxException e) { throw new IllegalArgumentException("Failed to parse TagMatcher from network.", e); } } private boolean checkProps(BlockState state) { - for (Entry entry : this.props.entrySet()) { + for (Entry entry : this.props.get().entrySet()) { Property prop = state.getBlock().getStateDefinition().getProperty(entry.getKey()); if (prop == null) { return false; @@ -123,7 +141,7 @@ public BlockState getDisplayedState(long ticks) { if (this.displayState != null) { return this.displayState; } else { - var all = ImmutableList.copyOf(Registry.BLOCK.getTagOrEmpty(this.tag)); + var all = ImmutableList.copyOf(Registry.BLOCK.getTagOrEmpty(this.tag.get())); if (all.isEmpty()) { return Blocks.BEDROCK.defaultBlockState(); // show something impossible } else { @@ -144,8 +162,8 @@ public void toNetwork(FriendlyByteBuf buffer) { if (this.displayState != null) { buffer.writeUtf(BlockStateParser.serialize(this.displayState)); } - buffer.writeResourceLocation(this.tag.location()); - buffer.writeMap(this.props, FriendlyByteBuf::writeUtf, FriendlyByteBuf::writeUtf); + buffer.writeResourceLocation(this.tag.get().location()); + buffer.writeMap(this.props.get(), FriendlyByteBuf::writeUtf, FriendlyByteBuf::writeUtf); } @Override diff --git a/src/main/resources/data/modonomicon/modonomicon_multiblocks/tag.json b/src/main/resources/data/modonomicon/modonomicon_multiblocks/tag.json new file mode 100644 index 000000000..a8ea48f31 --- /dev/null +++ b/src/main/resources/data/modonomicon/modonomicon_multiblocks/tag.json @@ -0,0 +1,50 @@ +{ + "type": "modonomicon:dense", + "pattern": [ + [ + " WWW ", + "N G S", + "NG0GS", + "N G S", + " EEE " + ], + [ + "DDDDD", + "DDDDD", + "DDDDD", + "DDDDD", + "DDDDD" + ] + ], + "mapping": { + "N": { + "type": "modonomicon:blockstate", + "block": "minecraft:oak_stairs[facing=north]" + }, + "S": { + "type": "modonomicon:blockstate", + "block": "minecraft:oak_stairs[facing=south]" + }, + "W": { + "type": "modonomicon:blockstate", + "block": "minecraft:oak_stairs[facing=west]" + }, + "E": { + "type": "modonomicon:blockstate", + "block": "minecraft:oak_stairs[facing=east]" + }, + "G": { + "type": "modonomicon:tag", + "tag": "minecraft:candles", + "display": "minecraft:white_candle" + }, + "D": { + "type": "modonomicon:display", + "display": "minecraft:stone" + }, + "0": { + "type": "modonomicon:block", + "block": "minecraft:skeleton_skull" + } + } +} \ No newline at end of file