-
-
Notifications
You must be signed in to change notification settings - Fork 883
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
Schematic caching system for suggestions #2542
Open
me4502
wants to merge
10
commits into
master
Choose a base branch
from
feature/schematic-manager
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ec1c7f6
Implement SchematicsManager to cache list of known schematics
seijikun e5f9212
Add command suggestions for schematic filenames
seijikun 0551ff6
Rename Schematic to SchematicPath and cleanup
seijikun efb6304
Remove unncessary (un)init log points
seijikun 64d8e39
Apply suggestions from code review
me4502 4f3ad0c
Resolve many of octy's review notes
me4502 0c7f5f6
Further review fixes
me4502 92a3ad2
Further review improvements
me4502 05b229a
Fix symlink check
me4502 ff6393e
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.
This set of characters doesn't make sense, it's not safe for windows (contains
?
,*
, doesn't ban COM and others), and POSIX will generally take anything. It also excludes extended Unicode characters for some reason, despite those being perfectly fine.Can we just drop checking this aside from "you must have an extension", i.e.
^.+\..+$
?