-
-
Notifications
You must be signed in to change notification settings - Fork 909
Make ^ support nbt (properly) #2734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
QuImUfu
wants to merge
1
commit into
EngineHub:master
Choose a base branch
from
QuImUfu:nbt-aware-partial-patterns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
258 changes: 258 additions & 0 deletions
258
.../com/sk89q/worldedit/extension/factory/parser/pattern/PartiallyApplyingPatternParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
/* | ||
* WorldEdit, a Minecraft world manipulation toolkit | ||
* Copyright (C) sk89q <http://www.sk89q.com> | ||
* Copyright (C) WorldEdit team and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.sk89q.worldedit.extension.factory.parser.pattern; | ||
|
||
import com.sk89q.worldedit.WorldEdit; | ||
import com.sk89q.worldedit.extension.input.InputParseException; | ||
import com.sk89q.worldedit.extension.input.NoMatchException; | ||
import com.sk89q.worldedit.extension.input.ParserContext; | ||
import com.sk89q.worldedit.extent.Extent; | ||
import com.sk89q.worldedit.extent.buffer.ExtentBuffer; | ||
import com.sk89q.worldedit.function.pattern.*; | ||
import com.sk89q.worldedit.internal.registry.InputParser; | ||
import com.sk89q.worldedit.util.formatting.text.TextComponent; | ||
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent; | ||
import com.sk89q.worldedit.world.block.BlockState; | ||
import org.enginehub.linbus.format.snbt.LinStringIO; | ||
import org.enginehub.linbus.stream.exception.NbtParseException; | ||
import org.enginehub.linbus.tree.LinCompoundTag; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
|
||
public class PartiallyApplyingPatternParser extends InputParser<Pattern> { | ||
|
||
boolean compatibilityMode = false; | ||
|
||
public PartiallyApplyingPatternParser(WorldEdit worldEdit) { | ||
super(worldEdit); | ||
} | ||
|
||
protected PartiallyApplyingPatternParser(WorldEdit worldEdit, boolean compatibilityMode) { | ||
super(worldEdit); | ||
this.compatibilityMode = compatibilityMode; | ||
} | ||
|
||
@Override | ||
public Stream<String> getSuggestions(String input, ParserContext context) { | ||
if (input.isEmpty()) { | ||
return Stream.of("^"); | ||
} | ||
if (!input.startsWith("^")) { | ||
return Stream.empty(); | ||
} | ||
input = input.substring(1); | ||
|
||
if (input.isEmpty()) { | ||
//define properties, nbt or a type | ||
return Stream.concat( | ||
Stream.of("^[", "^{", "^{,"), | ||
worldEdit.getBlockFactory().getSuggestions(input, context) | ||
.stream() | ||
.map(s -> "^" + s) | ||
); | ||
} | ||
|
||
PartiallyApplyingComponents components = split(input); | ||
|
||
if (!components.nbt().isEmpty()) { | ||
if (!components.type().isEmpty() && !components.properties().isEmpty()) { | ||
//all of them are defined, we suggest like we would without ^ | ||
return worldEdit.getBlockFactory().getSuggestions(input, context) | ||
.stream() | ||
.map(s -> "^" + s); | ||
} | ||
if (!components.type().isEmpty()) { | ||
//type and nbt. We currently don't support nbt hints, so nothing to suggest | ||
return Stream.empty(); | ||
} | ||
if (!components.properties().isEmpty()) { | ||
//properties and nbt. We can't figure out possible nbt without type | ||
return Stream.empty(); | ||
} | ||
} | ||
|
||
if (!components.properties().isEmpty()) { | ||
if (!components.type().isEmpty()) { | ||
//type and properties are defined, we suggest like we would without ^ | ||
return worldEdit.getBlockFactory().getSuggestions(input, context) | ||
.stream() | ||
.map(s -> "^" + s); | ||
} | ||
return Stream.empty(); // without knowing a type, we can't really suggest states | ||
} | ||
//only type is defined, we suggest like we would without ^ | ||
return worldEdit.getBlockFactory().getSuggestions(input, context) | ||
.stream() | ||
.map(s -> "^" + s); | ||
} | ||
|
||
private @NotNull PartiallyApplyingComponents split(String input) { | ||
String type; | ||
String properties = ""; | ||
//default as delete NBT retains previous behaviour | ||
String nbt = compatibilityMode ? "{=}" : ""; | ||
|
||
int startProperties = input.indexOf('['); | ||
int startNbt = input.indexOf('{'); | ||
|
||
if (startProperties >= 0 && startNbt >= 0) { | ||
//properties and nbt and maybe type | ||
type = input.substring(0, startProperties); | ||
properties = input.substring(startProperties, startNbt); | ||
nbt = input.substring(startNbt); | ||
} else if (startProperties >= 0) { | ||
//properties and maybe type | ||
type = input.substring(0, startProperties); | ||
properties = input.substring(startProperties); | ||
} else if (startNbt >= 0) { | ||
//nbt and maybe type | ||
type = input.substring(0, startNbt); | ||
nbt = input.substring(startNbt); | ||
} else { | ||
type = input; | ||
} | ||
return new PartiallyApplyingComponents(type, properties, nbt); | ||
} | ||
|
||
private record PartiallyApplyingComponents(String type, String properties, String nbt) { | ||
} | ||
|
||
@Override | ||
public Pattern parseFromInput(String input, ParserContext context) throws InputParseException { | ||
if (!input.startsWith("^")) { | ||
return null; | ||
} | ||
Extent extent = context.requireExtent(); | ||
input = input.substring(1); | ||
|
||
if (input.isEmpty()) { | ||
throw new NoMatchException(TranslatableComponent.of("worldedit.error.unknown-block", TextComponent.of(input))); | ||
} | ||
|
||
PartiallyApplyingComponents components = split(input); | ||
|
||
List<ExtendPatternFactory> extendPatternFactories = new ArrayList<>(); | ||
|
||
if (!components.nbt().isEmpty()) { | ||
extendPatternFactories | ||
.add(getNbtApplyingPatternFactory(input, components.nbt())); | ||
} | ||
if (!components.type().isEmpty()) { | ||
extendPatternFactories | ||
.add(getTypeApplyingPatternFactory(context, components.type())); | ||
} | ||
if (!components.properties().isEmpty()) { | ||
extendPatternFactories | ||
.add(getStateApplyingPatternFactory(components)); | ||
} | ||
|
||
if (extendPatternFactories.size() > 1) { | ||
Extent buffer = new ExtentBuffer(extent); | ||
Pattern[] patterns = extendPatternFactories.stream() | ||
.map(factory -> factory.forExtend(buffer)) | ||
.toArray(Pattern[]::new); | ||
return new ExtentBufferedCompositePattern(buffer, patterns); | ||
} | ||
|
||
return extendPatternFactories.getFirst().forExtend(extent); | ||
|
||
} | ||
|
||
private @NotNull ExtendPatternFactory getTypeApplyingPatternFactory(ParserContext context, String type) throws InputParseException { | ||
BlockState blockState = worldEdit.getBlockFactory() | ||
.parseFromInput(type, context).getBlockType().getDefaultState(); | ||
return ext -> new TypeApplyingPattern(ext, blockState); | ||
} | ||
|
||
private static @NotNull ExtendPatternFactory getStateApplyingPatternFactory(PartiallyApplyingComponents components) throws InputParseException { | ||
String properties = components.properties(); | ||
if (!properties.endsWith("]")) { | ||
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.missing-rbracket")); | ||
} | ||
String propertiesWithoutBrackets = properties.substring(1, properties.length() - 1); | ||
final String[] states = propertiesWithoutBrackets.split(","); | ||
Map<String, String> statesToSet = new HashMap<>(); | ||
for (String state : states) { | ||
if (state.isEmpty()) { | ||
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.empty-state")); | ||
} | ||
String[] propVal = state.split("=", 2); | ||
if (propVal.length != 2) { | ||
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.missing-equals-separator")); | ||
} | ||
final String prop = propVal[0]; | ||
if (prop.isEmpty()) { | ||
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.empty-property")); | ||
} | ||
final String value = propVal[1]; | ||
if (value.isEmpty()) { | ||
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.empty-value")); | ||
} | ||
if (statesToSet.put(prop, value) != null) { | ||
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.duplicate-property", TextComponent.of(prop))); | ||
} | ||
} | ||
return ext -> new StateApplyingPattern(ext, statesToSet); | ||
} | ||
|
||
private static @NotNull ExtendPatternFactory getNbtApplyingPatternFactory(String input, String nbt) throws InputParseException { | ||
if (!nbt.endsWith("}")) { | ||
throw new InputParseException(TranslatableComponent.of("worldedit.error.parser.missing-rbrace")); | ||
} | ||
if (nbt.equals("{}")) { | ||
return (ext) -> new NBTApplyingPattern(ext, null); | ||
} | ||
boolean merge = true; | ||
if (nbt.startsWith("{=")) { | ||
merge = false; | ||
nbt = "{" + nbt.substring(2); | ||
} | ||
LinCompoundTag tag; | ||
try { | ||
if (nbt.equals("{}")) { | ||
tag = LinCompoundTag.builder().build(); | ||
} else { | ||
tag = LinStringIO.readFromStringUsing(nbt, LinCompoundTag::readFrom); | ||
} | ||
} catch (NbtParseException e) { | ||
throw new NoMatchException(TranslatableComponent.of( | ||
"worldedit.error.parser.invalid-nbt", | ||
TextComponent.of("^" + input), | ||
TextComponent.of(e.getMessage()) | ||
)); | ||
} | ||
if (merge) { | ||
return (ext) -> new NBTMergingPattern(ext, tag.value()); | ||
} else { | ||
return (ext) -> new NBTApplyingPattern(ext, tag); | ||
} | ||
} | ||
|
||
private interface ExtendPatternFactory { | ||
Pattern forExtend(Extent e); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why you're commenting on style when you're not part of the project. This is fine by our standards. |
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renaming this class would be an API break, so it'd need to retain its original name, or duplicate it and deprecate the old one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the rule for API breaks/changes? Am I allowed to change behaviour in a potentially breaking way (this is) and still maintain the same name/API? Or do I need to provide a fully backwards-compatible implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a deprecated legacy version, that should behave like before. Do I need to do the same for the two Patterns that I changed?
Is there some documentation on what needs to stay API compatible?