-
-
Notifications
You must be signed in to change notification settings - Fork 882
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
SchematicsManager: Handling and caching list of known schematics, providing cli suggestions #2212
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f34f74
Implement SchematicsManager to cache list of known schematics
seijikun c009ffb
Add command suggestions for schematic filenames
seijikun 532e6ec
Rename Schematic to SchematicPath and cleanup
seijikun 9334c73
Remove unncessary (un)init log points
seijikun 6dda9a1
Apply suggestions from code review
me4502 e7890b1
Resolve many of octy's review notes
me4502 2283c75
Further review fixes
me4502 b7e095c
Further review improvements
me4502 9b339f3
Fix symlink check
me4502 441e25f
PR notes
me4502 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 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
This file contains 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
88 changes: 88 additions & 0 deletions
88
worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SchematicConverter.java
This file contains 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,88 @@ | ||
/* | ||
* 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.command.argument; | ||
|
||
import com.sk89q.worldedit.WorldEdit; | ||
import com.sk89q.worldedit.internal.annotation.SchematicPath; | ||
import com.sk89q.worldedit.internal.schematic.SchematicsManager; | ||
import com.sk89q.worldedit.util.formatting.text.Component; | ||
import com.sk89q.worldedit.util.formatting.text.TextComponent; | ||
import com.sk89q.worldedit.util.io.file.FilenameException; | ||
import org.enginehub.piston.CommandManager; | ||
import org.enginehub.piston.converter.ArgumentConverter; | ||
import org.enginehub.piston.converter.ConversionResult; | ||
import org.enginehub.piston.converter.FailedConversion; | ||
import org.enginehub.piston.converter.SuccessfulConversion; | ||
import org.enginehub.piston.inject.InjectedValueAccess; | ||
import org.enginehub.piston.inject.Key; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix; | ||
|
||
public class SchematicConverter implements ArgumentConverter<Path> { | ||
|
||
public static void register(WorldEdit worldEdit, CommandManager commandManager) { | ||
commandManager.registerConverter(Key.of(Path.class, SchematicPath.class), new SchematicConverter(worldEdit)); | ||
} | ||
|
||
private final WorldEdit worldEdit; | ||
|
||
private SchematicConverter(WorldEdit worldEdit) { | ||
this.worldEdit = worldEdit; | ||
} | ||
|
||
private final TextComponent choices = TextComponent.of("schematic filename"); | ||
|
||
@Override | ||
public Component describeAcceptableArguments() { | ||
return choices; | ||
} | ||
|
||
@Override | ||
public List<String> getSuggestions(String input, InjectedValueAccess context) { | ||
SchematicsManager schematicsManager = worldEdit.getSchematicsManager(); | ||
Path schematicsRootPath = schematicsManager.getRoot(); | ||
|
||
return limitByPrefix(schematicsManager.getSchematicPaths().stream() | ||
.map(s -> schematicsRootPath.relativize(s).toString()), input); | ||
} | ||
|
||
@Override | ||
public ConversionResult<Path> convert(String s, InjectedValueAccess injectedValueAccess) { | ||
Path schematicsRoot = worldEdit.getSchematicsManager().getRoot(); | ||
// resolve as subpath of schematicsRoot | ||
Path schematicPath = schematicsRoot.resolve(s).toAbsolutePath(); | ||
// then check whether it is still a subpath to rule out "../" | ||
if (!schematicPath.startsWith(schematicsRoot)) { | ||
return FailedConversion.from(new FilenameException(s)); | ||
} | ||
// check whether the file exists | ||
if (Files.exists(schematicPath)) { | ||
// continue as relative path to schematicsRoot | ||
schematicPath = schematicsRoot.relativize(schematicPath); | ||
return SuccessfulConversion.fromSingle(schematicPath); | ||
} else { | ||
return FailedConversion.from(new FilenameException(s)); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
36 changes: 36 additions & 0 deletions
36
worldedit-core/src/main/java/com/sk89q/worldedit/internal/annotation/SchematicPath.java
This file contains 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,36 @@ | ||
/* | ||
* 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.internal.annotation; | ||
|
||
import org.enginehub.piston.inject.InjectAnnotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation to denote an argument as a schematic path. | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
@InjectAnnotation | ||
public @interface SchematicPath { | ||
} |
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.
Why aren't we passing
file
down here? There's no need to do a full rescan when we could just forcibly add the appropriate file entry.