-
Notifications
You must be signed in to change notification settings - Fork 12
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
Minecraft 1.13 - Datapacks #53
Open
Cybermaxke
wants to merge
1
commit into
1.12
Choose a base branch
from
datapacks
base: 1.12
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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[submodule "SpongeAPI"] | ||
path = SpongeAPI | ||
url = https://github.com/SpongePowered/SpongeAPI.git | ||
url = https://github.com/LanternPowered/SpongeAPI.git | ||
ignore = dirty |
Submodule SpongeAPI
updated
18 files
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
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
223 changes: 223 additions & 0 deletions
223
src/main/java/org/lanternpowered/server/resource/FileSystemPack.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,223 @@ | ||
/* | ||
* This file is part of LanternServer, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) LanternPowered <https://www.lanternpowered.org> | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the Software), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.lanternpowered.server.resource; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import org.lanternpowered.server.game.Lantern; | ||
import org.spongepowered.api.data.DataView; | ||
import org.spongepowered.api.data.persistence.DataFormats; | ||
import org.spongepowered.api.plugin.PluginContainer; | ||
import org.spongepowered.api.resource.Resource; | ||
import org.spongepowered.api.resource.ResourcePath; | ||
import org.spongepowered.api.text.Text; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public final class FileSystemPack extends LanternPack { | ||
|
||
private final Path dataRoot; | ||
private final Cache<LanternResourcePath, LanternResource> cache = Caffeine.newBuilder().build(); | ||
|
||
FileSystemPack(Text name, @Nullable DataView metadata, | ||
@Nullable PluginContainer plugin, Path root) { | ||
super(name, metadata, plugin); | ||
// Already go inside the data directory | ||
// data/<namespace>/path/to/file | ||
this.dataRoot = root.resolve("data"); | ||
} | ||
|
||
@Override | ||
void reload() { | ||
this.cache.cleanUp(); | ||
} | ||
|
||
private LanternResource load(LanternResourcePath resourcePath, Path path) { | ||
final Path mcmetaPath = path.resolveSibling(path.getFileName().toString() + ".mcmeta"); | ||
DataView mcmeta = null; | ||
if (Files.exists(mcmetaPath)) { | ||
try (BufferedReader reader = Files.newBufferedReader(mcmetaPath)) { | ||
mcmeta = DataFormats.JSON.readFrom(reader); | ||
} catch (IOException e) { | ||
Lantern.getLogger().warn("Unable to load mcmeta file for: {} (path: {})", resourcePath, path, e); | ||
} | ||
} | ||
return new LanternResource(resourcePath, this, new FileSystemResourceData(path, mcmeta)); | ||
} | ||
|
||
@Override | ||
public Stream<ResourcePath> walkResourcePaths(ResourcePath path, int maxDepth) { | ||
checkNotNull(path, "path"); | ||
final LanternResourcePath path1 = (LanternResourcePath) path; | ||
if (path1.isFile() || maxDepth <= 0) { | ||
return Stream.empty(); | ||
} | ||
Stream<ResourcePath> stream = null; | ||
try { | ||
// If a wildcard is used, there is some special handling required | ||
if (path1.getNamespace().isEmpty()) { | ||
// A list with all the namespace directories | ||
for (Path namespacePath : Files.list(this.dataRoot).collect(Collectors.toList())) { | ||
final String namespace = namespacePath.getFileName().toString(); | ||
// Check if the namespace is valid, don't want to break things | ||
if (LanternResourcePath.NAMESPACE_PATTERN.matcher(namespace).matches()) { | ||
Lantern.getLogger().warn("Found a invalid namespace {} within the {} pack.", namespace, getName().toPlain()); | ||
} else { | ||
final LanternResourcePath newPath = LanternResourcePath.uncheckedOf(namespace, path.getPath()); | ||
final Stream<ResourcePath> extraStream = walkResourcePaths( | ||
newPath, namespacePath.resolve(path.getPath()), maxDepth); | ||
if (extraStream != null) { | ||
stream = stream == null ? extraStream : Stream.concat(stream, extraStream); | ||
} | ||
} | ||
} | ||
} else { | ||
stream = walkResourcePaths(path1, this.dataRoot.resolve(path.getNamespace()).resolve(path.getPath()), maxDepth); | ||
} | ||
} catch (IOException e) { | ||
Lantern.getLogger().error("Failed to walk resource path: {}", path, e); | ||
} | ||
return stream == null ? Stream.empty() : stream; | ||
} | ||
|
||
@Nullable | ||
private Stream<ResourcePath> walkResourcePaths(LanternResourcePath resourcePath, Path start, int maxDepth) { | ||
if (!Files.exists(start)) { | ||
return null; | ||
} | ||
try { | ||
return Files.walk(start, maxDepth).map(path -> { | ||
final Path relPath = path.relativize(start); | ||
// Backslashes are not supported so replace them | ||
final String path1 = relPath.toString().replace('\\', '/'); | ||
// Check if it's a invalid path for a resource and ignore .mcmeta files | ||
if (path1.endsWith(".mcmeta") || | ||
!LanternResourcePath.PATH_PATTERN.matcher(path1).matches()) { | ||
return null; | ||
} | ||
// Create the new path | ||
return LanternResourcePath.uncheckedOf(resourcePath.getNamespace(), | ||
resourcePath.getPath() + '/' + path1); | ||
}); | ||
} catch (IOException e) { | ||
Lantern.getLogger().error("Failed to walk resource path: {}", resourcePath, e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Stream<LanternResource> walkResources(ResourcePath path, int maxDepth) { | ||
checkNotNull(path, "path"); | ||
final LanternResourcePath path1 = (LanternResourcePath) path; | ||
if (path1.isFile() || maxDepth <= 0) { | ||
return Stream.empty(); | ||
} | ||
Stream<LanternResource> stream = null; | ||
try { | ||
// If a wildcard is used, there is some special handling required | ||
if (path1.getNamespace().isEmpty()) { | ||
// A list with all the namespace directories | ||
for (Path namespacePath : Files.list(this.dataRoot).collect(Collectors.toList())) { | ||
final String namespace = namespacePath.getFileName().toString(); | ||
// Check if the namespace is valid, don't want to break things | ||
if (LanternResourcePath.NAMESPACE_PATTERN.matcher(namespace).matches()) { | ||
Lantern.getLogger().warn("Found a invalid namespace {} within the {} pack.", namespace, getName().toPlain()); | ||
} else { | ||
final LanternResourcePath newPath = LanternResourcePath.uncheckedOf(namespace, path.getPath()); | ||
final Stream<LanternResource> extraStream = walkResources( | ||
newPath, namespacePath.resolve(path.getPath()), maxDepth); | ||
if (extraStream != null) { | ||
stream = stream == null ? extraStream : Stream.concat(stream, extraStream); | ||
} | ||
} | ||
} | ||
} else { | ||
stream = walkResources(path1, this.dataRoot.resolve(path.getNamespace()).resolve(path.getPath()), maxDepth); | ||
} | ||
} catch (IOException e) { | ||
Lantern.getLogger().error("Failed to walk resource path: {}", path, e); | ||
} | ||
return stream == null ? Stream.empty() : stream; | ||
} | ||
|
||
@Nullable | ||
private Stream<LanternResource> walkResources(LanternResourcePath resourcePath, Path start, int maxDepth) { | ||
if (!Files.exists(start)) { | ||
return null; | ||
} | ||
try { | ||
return Files.walk(start, maxDepth).map(path -> { | ||
final Path relPath = path.relativize(start); | ||
// Backslashes are not supported so replace them | ||
final String path1 = relPath.toString().replace('\\', '/'); | ||
// Check if it's a invalid path for a resource and ignore .mcmeta files | ||
if (path1.endsWith(".mcmeta") || | ||
!LanternResourcePath.PATH_PATTERN.matcher(path1).matches()) { | ||
return null; | ||
} | ||
final LanternResourcePath newResourcePath = LanternResourcePath.uncheckedOf(resourcePath.getNamespace(), | ||
resourcePath.getPath() + '/' + path1); | ||
// Create the new path | ||
return this.cache.get(newResourcePath, newResourcePath1 -> load(newResourcePath1, path)); | ||
}); | ||
} catch (IOException e) { | ||
Lantern.getLogger().error("Failed to walk resource path: {}", resourcePath, e); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Collection<Resource> getAllResources() { | ||
return walkResources(LanternResourcePath.ALL).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public Optional<Resource> getResource(ResourcePath path) { | ||
checkNotNull(path, "path"); | ||
final LanternResourcePath path1 = (LanternResourcePath) path; | ||
if (path1.isDirectory() || path1.getNamespace().isEmpty()) { | ||
return Optional.empty(); | ||
} | ||
return Optional.ofNullable(this.cache.get(path1, path2 -> { | ||
final Path filePath = this.dataRoot.resolve(path.getNamespace()).resolve(path.getPath()); | ||
if (!Files.exists(filePath)) { | ||
return null; | ||
} | ||
return load(path1, filePath); | ||
})); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/org/lanternpowered/server/resource/FileSystemResourceData.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,67 @@ | ||
/* | ||
* This file is part of LanternServer, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) LanternPowered <https://www.lanternpowered.org> | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the Software), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.lanternpowered.server.resource; | ||
|
||
import com.google.common.base.MoreObjects; | ||
import org.spongepowered.api.data.DataView; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public final class FileSystemResourceData implements IResourceData { | ||
|
||
private final Path path; | ||
@Nullable private final DataView metadata; | ||
|
||
FileSystemResourceData(Path path, @Nullable DataView metadata) { | ||
this.path = path; | ||
this.metadata = metadata; | ||
} | ||
|
||
@Override | ||
public InputStream openStream() throws IOException { | ||
return Files.newInputStream(this.path); | ||
} | ||
|
||
@Override | ||
public Optional<DataView> getMetadata() throws IOException { | ||
return Optional.ofNullable(this.metadata); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("path", this.path) | ||
.add("metadata", this.metadata) | ||
.omitNullValues() | ||
.toString(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/org/lanternpowered/server/resource/IResourceData.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,32 @@ | ||
/* | ||
* This file is part of LanternServer, licensed under the MIT License (MIT). | ||
* | ||
* Copyright (c) LanternPowered <https://www.lanternpowered.org> | ||
* Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
* Copyright (c) contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the Software), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package org.lanternpowered.server.resource; | ||
|
||
import org.spongepowered.api.resource.ResourceData; | ||
|
||
public interface IResourceData extends ResourceData { | ||
|
||
} |
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 will never be empty. If the namespace is omitted from vanilla,
minecraft
is used in its place.e.g.
:.
will becomeminecraft:.
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.
We could modify that behavior? Or use a wildcard like
*
instead.