From a0b710e4d78d4660dd226cd9558b5e92d13ae748 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 28 Nov 2017 00:41:25 -0500 Subject: [PATCH 01/22] Add resource manager for data packs Addd a ResourcePath for easier path validation Move package to resources instead of pack Vanilla datapacks can be disabled Move package to resource (remove plural) Add builders Complete* javadocs Add functionality for dynamic resources More work on resources. Resource creation is more foolproof Added ResourceId annotation for resource injection Various other additions and fixes Use a packid instead of a name. Lift more methods into ResourceProvider and PackProvider Add some QoL methods into ResourcePath (lifted from java.nio.Path) Add a walkResources method Add ResourceReloadEvent.Post Add a resourcetype to be used later --- build.gradle | 1 + src/main/java/org/spongepowered/api/Game.java | 11 + .../java/org/spongepowered/api/Sponge.java | 7 + .../org/spongepowered/api/asset/Asset.java | 1 + .../org/spongepowered/api/asset/AssetId.java | 1 + .../spongepowered/api/asset/AssetManager.java | 9 + .../api/event/resource/ResourceEvent.java | 39 ++++ .../event/resource/ResourceReloadEvent.java | 51 +++++ .../api/event/resource/package-info.java | 25 +++ .../api/plugin/PluginContainer.java | 15 +- .../org/spongepowered/api/resource/Pack.java | 136 ++++++++++++ .../api/resource/PackProvider.java | 75 +++++++ .../spongepowered/api/resource/Resource.java | 159 ++++++++++++++ .../api/resource/ResourceData.java | 65 ++++++ .../api/resource/ResourceId.java | 62 ++++++ .../api/resource/ResourceManager.java | 73 +++++++ .../api/resource/ResourcePath.java | 202 ++++++++++++++++++ .../api/resource/ResourceProvider.java | 101 +++++++++ .../api/resource/ResourceType.java | 8 + .../api/resource/ResourceTypes.java | 15 ++ .../api/resource/package-info.java | 25 +++ 21 files changed, 1080 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java create mode 100644 src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java create mode 100644 src/main/java/org/spongepowered/api/event/resource/package-info.java create mode 100644 src/main/java/org/spongepowered/api/resource/Pack.java create mode 100644 src/main/java/org/spongepowered/api/resource/PackProvider.java create mode 100644 src/main/java/org/spongepowered/api/resource/Resource.java create mode 100644 src/main/java/org/spongepowered/api/resource/ResourceData.java create mode 100644 src/main/java/org/spongepowered/api/resource/ResourceId.java create mode 100644 src/main/java/org/spongepowered/api/resource/ResourceManager.java create mode 100644 src/main/java/org/spongepowered/api/resource/ResourcePath.java create mode 100644 src/main/java/org/spongepowered/api/resource/ResourceProvider.java create mode 100644 src/main/java/org/spongepowered/api/resource/ResourceType.java create mode 100644 src/main/java/org/spongepowered/api/resource/ResourceTypes.java create mode 100644 src/main/java/org/spongepowered/api/resource/package-info.java diff --git a/build.gradle b/build.gradle index 6237ae2095e..aee52d8d07d 100644 --- a/build.gradle +++ b/build.gradle @@ -175,6 +175,7 @@ sortClassFields { add 'main', 'org.spongepowered.api.item.inventory.equipment.EquipmentTypes' add 'main', 'org.spongepowered.api.item.inventory.query.QueryOperationTypes' add 'main', 'org.spongepowered.api.item.potion.PotionTypes' + add 'main', 'org.spongepowered.api.resource.ResourceTypes' add 'main', 'org.spongepowered.api.scoreboard.CollisionRules' add 'main', 'org.spongepowered.api.scoreboard.Visibilities' add 'main', 'org.spongepowered.api.scoreboard.criteria.Criteria' diff --git a/src/main/java/org/spongepowered/api/Game.java b/src/main/java/org/spongepowered/api/Game.java index 23ab90b6ee6..5183c5b98c8 100644 --- a/src/main/java/org/spongepowered/api/Game.java +++ b/src/main/java/org/spongepowered/api/Game.java @@ -35,6 +35,7 @@ import org.spongepowered.api.event.CauseStackManager; import org.spongepowered.api.event.EventManager; import org.spongepowered.api.network.ChannelRegistrar; +import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.api.plugin.PluginManager; import org.spongepowered.api.scheduler.Scheduler; import org.spongepowered.api.service.ServiceManager; @@ -168,10 +169,20 @@ default EventManager getEventManager() { * * @return The asset manager */ + @Deprecated default AssetManager getAssetManager() { return Sponge.getAssetManager(); } + /** + * Gets the {@link ResourceManager}. + * + * @return The resource manager + */ + default ResourceManager getResourceManager() { + return Sponge.getResourceManager(); + } + /** * Gets the {@link ConfigManager} used to load and manage configuration files * for plugins. diff --git a/src/main/java/org/spongepowered/api/Sponge.java b/src/main/java/org/spongepowered/api/Sponge.java index f32e59182bf..1ea7fc7638c 100644 --- a/src/main/java/org/spongepowered/api/Sponge.java +++ b/src/main/java/org/spongepowered/api/Sponge.java @@ -37,6 +37,7 @@ import org.spongepowered.api.event.CauseStackManager; import org.spongepowered.api.event.EventManager; import org.spongepowered.api.network.ChannelRegistrar; +import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.api.plugin.PluginManager; import org.spongepowered.api.scheduler.Scheduler; import org.spongepowered.api.service.ServiceManager; @@ -59,6 +60,7 @@ public final class Sponge { @Inject private static PluginManager pluginManager; @Inject private static EventManager eventManager; @Inject private static AssetManager assetManager; + @Inject private static ResourceManager resourceManager; @Inject private static ConfigManager configManager; @Inject private static ServiceManager serviceManager; @Inject private static ChannelRegistrar channelRegistrar; @@ -145,10 +147,15 @@ public static EventManager getEventManager() { * * @return The asset manager instance */ + @Deprecated public static AssetManager getAssetManager() { return check(assetManager); } + public static ResourceManager getResourceManager() { + return check(resourceManager); + } + /** * Gets the {@link ConfigManager} used to load and manage configuration files * for plugins. diff --git a/src/main/java/org/spongepowered/api/asset/Asset.java b/src/main/java/org/spongepowered/api/asset/Asset.java index 16598490a44..0c6f52b3a7a 100644 --- a/src/main/java/org/spongepowered/api/asset/Asset.java +++ b/src/main/java/org/spongepowered/api/asset/Asset.java @@ -42,6 +42,7 @@ /** * Represents an {@link Asset} within Sponge that belongs to a {@link Plugin}. */ +@Deprecated public interface Asset { /** diff --git a/src/main/java/org/spongepowered/api/asset/AssetId.java b/src/main/java/org/spongepowered/api/asset/AssetId.java index 94511e1bdfd..919c936feaf 100644 --- a/src/main/java/org/spongepowered/api/asset/AssetId.java +++ b/src/main/java/org/spongepowered/api/asset/AssetId.java @@ -34,6 +34,7 @@ /** * Provides an injection for {@link Asset}s in plugins. */ +@Deprecated @BindingAnnotation @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.PARAMETER}) diff --git a/src/main/java/org/spongepowered/api/asset/AssetManager.java b/src/main/java/org/spongepowered/api/asset/AssetManager.java index 96de1fdbe09..4380f991dd3 100644 --- a/src/main/java/org/spongepowered/api/asset/AssetManager.java +++ b/src/main/java/org/spongepowered/api/asset/AssetManager.java @@ -24,6 +24,7 @@ */ package org.spongepowered.api.asset; +import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; @@ -33,7 +34,15 @@ * The AssetManager offers a convenient way to easily retrieve resources from * Sponge {@link Plugin}s. The asset manager will attempt to find the * asset of the specified name at: assets/<plugin_id> + * + * @deprecated The asset manager was unable to provide assets which are not on + * the classpath. Additionally, it was limited to providing the URL to an + * asset, which might not have been useful in some situations. + * + *

Use the more powerful {@link ResourceManager} instead. It allows you to + * add resources from local files as well as on-the-fly generation.

*/ +@Deprecated public interface AssetManager { /** diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java new file mode 100644 index 00000000000..75d055d79ac --- /dev/null +++ b/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java @@ -0,0 +1,39 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.event.resource; + +import org.spongepowered.api.event.Event; +import org.spongepowered.api.resource.ResourceManager; + +public interface ResourceEvent extends Event { + + /** + * Gets the {@link ResourceManager} which caused this event. + * + * @return The resource manager + */ + ResourceManager getResourceManager(); + +} diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java new file mode 100644 index 00000000000..b247500a648 --- /dev/null +++ b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java @@ -0,0 +1,51 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.event.resource; + +import org.spongepowered.api.resource.Resource; +import org.spongepowered.api.resource.ResourceManager; + +/** + * Base interface for resource reloading events. + */ +public interface ResourceReloadEvent extends ResourceEvent { + + /** + * Called before the {@link ResourceManager} is reloaded. At this point, + * the {@link ResourceManager#getActivePacks() active packs} can be added + * or removed from. + */ + interface Pre extends ResourceReloadEvent { + + } + + /** + * Called after the {@link ResourceManager} is reloaded. When a + * {@link Resource} is reloaded, this event should be used to obtain the new + * file. + */ + interface Post extends ResourceReloadEvent { + } +} diff --git a/src/main/java/org/spongepowered/api/event/resource/package-info.java b/src/main/java/org/spongepowered/api/event/resource/package-info.java new file mode 100644 index 00000000000..88e5eabc8a6 --- /dev/null +++ b/src/main/java/org/spongepowered/api/event/resource/package-info.java @@ -0,0 +1,25 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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. + */ +@org.spongepowered.api.util.annotation.NonnullByDefault package org.spongepowered.api.event.resource; diff --git a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java index 775d211f964..659c33ed03f 100644 --- a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java +++ b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java @@ -32,6 +32,8 @@ import org.spongepowered.api.Sponge; import org.spongepowered.api.asset.Asset; import org.spongepowered.api.asset.AssetManager; +import org.spongepowered.api.resource.Pack; +import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.plugin.meta.PluginDependency; import java.nio.file.Path; @@ -132,15 +134,26 @@ default Optional getDependency(String id) { * @param name Name of asset * @return Asset if present, empty otherwise */ + @Deprecated default Optional getAsset(String name) { return Sponge.getAssetManager().getAsset(this, name); } + /** + * Retrieves the {@link Pack} owned by this plugin from the + * {@link ResourceManager}. + * + * @return The plugin's resource pack. + */ + default Pack getPack() { + return Sponge.getResourceManager().getPack(this); + } + /** * Returns the source the plugin was loaded from. * * @return The source the plugin was loaded from or {@link Optional#empty()} - * if unknown + * if unknown */ default Optional getSource() { return Optional.empty(); diff --git a/src/main/java/org/spongepowered/api/resource/Pack.java b/src/main/java/org/spongepowered/api/resource/Pack.java new file mode 100644 index 00000000000..ffae2113b1c --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/Pack.java @@ -0,0 +1,136 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import org.spongepowered.api.Sponge; +import org.spongepowered.api.data.DataView; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.util.ResettableBuilder; + +import java.util.Collection; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; + +/** + * A pack can contain several {@link Resource Resources}. + */ +public interface Pack extends ResourceProvider { + + /** + * Creates a new {@link Builder} instance. + * + * @return A new builder + */ + static Builder builder() { + return Sponge.getRegistry().createBuilder(Builder.class); + } + + /** + * Gets the name of this pack which is displayed to the user. + * + * @return The name + */ + Text getName(); + + /** + * Gets the metadata of this pack. The {@link DataView} represented is of + * the pack.json file in the pack root. If the pack does not contain a + * pack.json, {@link Optional#empty()} is returned. + * + * @return The metadata if it exists + */ + Optional getMetadata(); + + /** + * Gets all the resources loaded from this pack. Depending on the pack + * implementation, the contents of the collection may not reflect what the + * pack contains. + * + *

If the pack is lazy-initialized, it is possible for the collection to + * be empty.

+ * + * @return List of loaded resources + */ + Collection getAllResources(); + + /** + * A builder for a {@link Pack}. + */ + interface Builder extends ResettableBuilder { + + /** + * Sets the name of this pack as it will appear in chat. + * + * @param name The name + * @return This builder + */ + Builder name(Text name); + + /** + * Sets the metadata for this pack. + * + * @param metadata The metadata + * @return This builder + */ + Builder metadata(DataView metadata); + + /** + * Adds a static {@link Resource} resource to the {@link Pack}. + * + * @param path The path + * @param resource The data for the resource + * @return This builder + */ + Builder resource(ResourcePath path, ResourceData resource); + + /** + * Specifies static {@link Resource}s to include in the {@link Pack}. + * Content is loaded along-with the pack. The resources themselves + * cannot be changed, but the contents are able to be reloaded. + * + * @param resources The resources + * @return This builder + */ + Builder resources(Map resources); + + /** + * Provides a {@link Resource} list to include in the pack. The + * resources are loaded when the pack is loaded. Resources can be added + * or removed as needed. + * + * @param resources The resources to load + * @return This builder + */ + Builder resources(Supplier> resources); + + /** + * Creates a new instance of {@link Pack}. + * + * @return A new pack + */ + Pack build(); + } +} diff --git a/src/main/java/org/spongepowered/api/resource/PackProvider.java b/src/main/java/org/spongepowered/api/resource/PackProvider.java new file mode 100644 index 00000000000..da3b080532e --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/PackProvider.java @@ -0,0 +1,75 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import org.spongepowered.api.plugin.PluginContainer; + +import java.util.Map; +import java.util.Optional; + +public interface PackProvider { + + /** + * Returns a map of pack ids to available packs. An available pack could be + * active or not. The key in the returned map should correspond it its id. + * + * @return A collection of available packs. + */ + Map getPacks(); + + /** + * Gets the {@link Pack} defined from {@link PluginContainer#getSource()}. + * The name of the pack will contain the plugin id. + * + * @param plugin The plugin instance or container. + * @return The pack + * @throws IllegalArgumentException if the object is not a plugin + */ + Pack getPack(Object plugin); + + /** + * Gets the pack by its name. + * + * @param name The pack's name. + * @return The pack or empty if it doesn't exist + */ + Optional getPack(String name); + + /** + * Registers a pack to be usable. + * + * @param packId The id of the pack + * @param pack The pack to register + */ + void registerPack(String packId, Pack pack); + + /** + * Unregisters a pack by its name. + * + * @param packId The id of the pack + * @return The pack which was unregistered or empty if it wasn't registered + */ + Optional unregisterPack(String packId); +} diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java new file mode 100644 index 00000000000..6b1978a901a --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -0,0 +1,159 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import com.google.common.io.ByteStreams; +import com.google.common.io.CharStreams; +import com.google.common.io.MoreFiles; +import org.spongepowered.api.data.DataView; +import org.spongepowered.api.data.persistence.DataFormat; +import org.spongepowered.api.data.persistence.InvalidDataFormatException; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.util.stream.Stream; +import javax.annotation.WillNotClose; + +/** + * A resource can represent any kind of loaded data. It can be a file on the + * filesystem, a network location, or even generated at runtime. Use + * {@link #openStream()} to load the data held by a resource. + */ +public interface Resource extends ResourceData { + + /** + * Gets the path of this resource. + * + * @return The path + */ + ResourcePath getResourcePath(); + + /** + * Gets the {@link Pack} which owns this resource. The pack is set + * automatically when the resource is added to a pack. + * + * @return The parent pack. + */ + Pack getPack(); + + ResourceType getType(); + + /** + * Gets a reader for this resource using the given {@link Charset}. + * + * @param charset The charset + * @return A new reader + * @throws IOException if an error occurs + */ + default Reader getReader(Charset charset) throws IOException { + return new InputStreamReader(openStream(), charset); + } + + /** + * Reads this resource into a string using the given charset. + * + * @param charset The charset + * @return The string contents + * @throws IOException if an error occurs + */ + default String readString(Charset charset) throws IOException { + try (Reader r = getReader(charset)) { + return CharStreams.toString(r); + } + } + + /** + * Reads a list of string lines from this resource using the given charset. + * + * @param charset The charset + * @return The list of strings + * @throws IOException if an error occurs + */ + default Stream readLines(Charset charset) throws IOException { + try (BufferedReader r = new BufferedReader(getReader(charset))) { + return r.lines(); + } + } + + /** + * Reads the bytes from this resource and returns them in an array. + * + * @return The bytes + * @throws IOException if an error occurs + */ + default byte[] readBytes() throws IOException { + try (InputStream in = openStream()) { + return ByteStreams.toByteArray(in); + } + } + + /** + * Reads this resource into a {@link DataView}. + * + * @param format The data format to use + * @return The dataview + * @throws IOException if an error occurs + */ + default DataView readDataView(DataFormat format) throws IOException { + try (InputStream in = openStream()) { + return format.readFrom(in); + } catch (InvalidDataFormatException e) { + throw new IOException(e); + } + } + + /** + * Copies this resource to a given {@link Path}. + * + * @param path The file to write to + * @param options The options to use + * @throws IOException if an error occurs + */ + default void copyTo(Path path, OpenOption... options) throws IOException { + try (InputStream in = openStream()) { + MoreFiles.asByteSink(path, options).writeFrom(in); + } + } + + /** + * Copies this resource to a given {@link OutputStream}. + * + * @param out The output stream to write + * @throws IOException if an error occurs + */ + default void copyTo(@WillNotClose OutputStream out) throws IOException { + try (InputStream in = openStream()) { + ByteStreams.copy(in, out); + } + } + +} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceData.java b/src/main/java/org/spongepowered/api/resource/ResourceData.java new file mode 100644 index 00000000000..e8788bb2148 --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ResourceData.java @@ -0,0 +1,65 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import org.spongepowered.api.data.DataView; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; + +/** + * Provides a new {@link InputStream} for a {@link Resource}. + */ +@FunctionalInterface +public interface ResourceData { + + /** + * Returns a new {@link InputStream} of this resource. A new input stream + * should be created each time this method is called. + * + * @return A new input stream + * @throws IOException if an error occurs + */ + InputStream openStream() throws IOException; + + /** + * Gets the metadata for this resource. + * + *

The metadata file has the same name as this resource, but has + * {@code .mcmeta} appended to the end.

+ * + *

For example: the metadata for the resource + * {@code minecraft:textures/blocks/water_flow.png} would be located at + * {@code minecraft:textures/blocks/water_flow.png.mcmeta}

+ * + * @return The metadata or {@link Optional#empty() empty} if it doesn't exist. + * @throws IOException if the metadata file is corrupt or cannot be read. + * @see Minecraft Wiki/Resource Packs + */ + default Optional getMetadata() throws IOException { + return Optional.empty(); + } +} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceId.java b/src/main/java/org/spongepowered/api/resource/ResourceId.java new file mode 100644 index 00000000000..b18be7dbdb6 --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ResourceId.java @@ -0,0 +1,62 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation for injecting resources into a plugin instance. + * + * The type this should be used on is {@code Optional}. When applied + * to a method, the {@link Resource} will be automatically updated when the + * {@link ResourceManager} is reloaded. + * + * @see ResourceManager#getResource(ResourcePath) + */ +@BindingAnnotation +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.PARAMETER, ElementType.FIELD}) +public @interface ResourceId { + + /** + * The namespace of the {@link Resource}. If not provided, it will default + * to the owner's plugin id. + * + * @see ResourcePath#getNamespace() + */ + String namespace() default ""; + + /** + * The path to the {@link Resource}. + * + * @see ResourcePath#getPath() + */ + String value(); +} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceManager.java b/src/main/java/org/spongepowered/api/resource/ResourceManager.java new file mode 100644 index 00000000000..f14a5b5a7ea --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ResourceManager.java @@ -0,0 +1,73 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * The resource manager is in charge of loading {@link Resource Resources} and + * {@link Pack Data Packs}. On the client, there can also be resource packs. + * + * Packs are stacked on top of each other, so they will override and replace + * resources in packs which are a lower priority. + */ +public interface ResourceManager extends ResourceProvider, PackProvider { + + /** + * Returns a list of currently active packs. The returned list is mutable, + * but changes will not be applied. To apply the changes, use + * {@link #reload(List)}. + * + * @return The list of active packs. + */ + List getActivePacks(); + + /** + * Gets the resources at the given path from all active packs + * + * @param path The path of the resource. + * @return A collection of resources + */ + Collection getResources(ResourcePath path); + + /** + * Schedules a reload of resources from active packs. + * + * @return A future for the task + */ + CompletableFuture reload(); + + /** + * Schedules a reload of resources, using the packs from {@code packs} + * + * @param packs The packs to reload with + * @return A future for the task + */ + CompletableFuture reload(List packs); + + ResourceType getResourceType(); +} diff --git a/src/main/java/org/spongepowered/api/resource/ResourcePath.java b/src/main/java/org/spongepowered/api/resource/ResourcePath.java new file mode 100644 index 00000000000..b247065d1a4 --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ResourcePath.java @@ -0,0 +1,202 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import org.spongepowered.api.Sponge; +import org.spongepowered.api.util.ResettableBuilder; + +import java.util.List; +import java.util.Map; + +/** + *

A resource path should contain a namespace. If one is not provided, + * {@code minecraft} will be used instead. The namespace and path must not + * contain any special characters or uppercase letters. + * + *

In the pack, the path will point to a resource. The resource should + * be located roughly at {@code data/namespace/path}

+ * + *

A resource path should be usable in a {@link Map}, so implementations + * should override {@link #hashCode()} and {@link #equals(Object)}.

+ */ +public interface ResourcePath extends Comparable { + + /** + * Creates a new {@link Builder} to build a {@link ResourcePath}. + * + * @return The new builder + */ + static Builder builder() { + return Sponge.getRegistry().createBuilder(Builder.class); + } + + /** + * Creates a new {@link ResourcePath} using the given namespace and path. + * + * @param namespace The namespace to use + * @param path The path to use + * @return A new ResourcePath + */ + static ResourcePath of(String namespace, String path) throws IllegalArgumentException { + return builder().namespace(namespace).path(path).build(); + } + + /** + * Parses a new {@link ResourcePath} from the given string representation. + * The representation should include both the namespace and path, separated + * by a colon (:). Path items should be separated by a forward slash (/). + * If the namespace is excluded, the default of minecraft is used instead. + * + *

e.g. {@code lagmeter:functions/lm.json} points to the + * functions/lm.json file in the lagmeter namespace.

+ * + * @param path The path + * @return A new ResourcePath + */ + static ResourcePath parse(String path) throws IllegalArgumentException { + return builder().parse(path).build(); + } + + /** + * Gets the namespace portion of this resource path. + * + * @return The namespace + */ + String getNamespace(); + + /** + * Gets the path portion of this resource path + * + * @return The path + */ + String getPath(); + + /** + * Gets the root path from this path. This is the same object as returned + * by the following code. + *
+     * List parents = getParents();
+     * if (parents.isEmpty()) {
+     *     return this;
+     * }
+     * return parents.get(parents.size() - 1);
+     * 
+ * + * @return The root path + */ + ResourcePath getRoot(); + + /** + * Gets a list of parents for this {@link ResourcePath}. Order of parents + * will be closest first, with the root being the last element. If the + * returned list is empty, this path is the root path. + * + * @return The list of parents + */ + List getParents(); + + /** + * Similar to {@link #getParents()}, but only returns the name portion of + * the paths. + * + * @return The list of names in the path. + */ + List getNames(); + + /** + * Gets the file name portion of this {@link ResourcePath}. + * + * @return The file name of the path + */ + String getName(); + + /** + * Resolves a new {@link ResourcePath} with this one as the parent. + * + * @param name The file name of the new path + * @return A new path + */ + ResourcePath resolve(String name); + + /** + * Resolves a new {@link ResourcePath} with the parent of this one as the + * parent. + * + * @param name The file name of the new path + * @return A new path + */ + ResourcePath resolveSibling(String name); + + /** + * Represents a builder to create {@link ResourcePath} instances. + */ + interface Builder extends ResettableBuilder { + + /** + * Sets the namespace of the {@link ResourcePath}. + * + * @param namespace The namespace + * @return This builder + * @throws IllegalArgumentException if the namespace contains illegal characters + */ + Builder namespace(String namespace) throws IllegalArgumentException; + + /** + * Sets the namespace of the {@link ResourcePath} using the given + * plugin's id. + * + * @param plugin The owning plugin + * @return This builder + */ + Builder plugin(Object plugin); + + /** + * Sets the path of the {@link ResourcePath}. + * + * @param path The path + * @return This builder + */ + Builder path(String path) throws IllegalArgumentException; + + /** + * Parses a string which represents a {@link ResourcePath}. This + * completes the builder. + * + * @param path The string representation of a resource path + * @return This builder + * @see ResourcePath#parse(String) + */ + Builder parse(String path) throws IllegalArgumentException; + + /** + * Builds an instance of a {@link ResourcePath}. + * + * @return A new instance of a {@link ResourcePath} + * @throws IllegalStateException if a path was not provided + */ + ResourcePath build() throws IllegalStateException; + + } +} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceProvider.java b/src/main/java/org/spongepowered/api/resource/ResourceProvider.java new file mode 100644 index 00000000000..b25fa169c40 --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ResourceProvider.java @@ -0,0 +1,101 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import com.google.common.collect.Streams; + +import java.util.Collection; +import java.util.Optional; +import java.util.stream.Stream; + +public interface ResourceProvider { + + /** + * Gets a loaded resource at the given path, or {@link Optional#empty()} + * if it does not exist. + * + * @param path The path to the resource + * @return The resource + */ + Optional getResource(ResourcePath path); + + /** + * Returns all of the resources which exist. + * + * @return All of the resources + */ + Collection getResources(); + + /** + * Walks though the loaded {@link ResourcePath paths}, which are children + * of the given path. The paths are traversed depth-first. Children will + * include both directories and files. + * + * @param path The start path + * @return A stream of resource paths + */ + Stream walk(ResourcePath path); + + /** + * Walks though the loaded {@link ResourcePath paths}, which are children + * of the given path. The paths are traversed depth-first. Children will + * include both directories and files. + * + *

The {@code maxDepth} parameter is used to limit the number of + * directories that will be traversed.

+ * + * @param path The start path + * @param maxDepth The max number of directories to traverse + * @return A stream of resource paths + */ + Stream walk(ResourcePath path, int maxDepth); + + /** + * Walks though loaded {@link Resource resources}, which are children of + * the given path. The paths are traversed depth-first. + * + * @param path The start path + * @return A stream of resources + */ + default Stream walkResources(ResourcePath path) { + return walk(path).map(this::getResource).flatMap(Streams::stream); + } + + /** + * Walks though loaded {@link Resource resources}, which are children of + * the given path. The paths are traversed depth-first. + * + *

The {@code maxDepth} parameter is used to limit the number of + * directories that will be traversed.

+ * + * @param path The start path + * @param maxDepth The max number of directories to traverse + * @return A stream of resources + */ + default Stream walkResources(ResourcePath path, int maxDepth) { + return walk(path, maxDepth).map(this::getResource).flatMap(Streams::stream); + } + +} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceType.java b/src/main/java/org/spongepowered/api/resource/ResourceType.java new file mode 100644 index 00000000000..a550438880f --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ResourceType.java @@ -0,0 +1,8 @@ +package org.spongepowered.api.resource; + +import org.spongepowered.api.CatalogType; +import org.spongepowered.api.util.annotation.CatalogedBy; + +@CatalogedBy(ResourceTypes.class) +public interface ResourceType extends CatalogType { +} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceTypes.java b/src/main/java/org/spongepowered/api/resource/ResourceTypes.java new file mode 100644 index 00000000000..02e1c114ddd --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ResourceTypes.java @@ -0,0 +1,15 @@ +package org.spongepowered.api.resource; + +import org.spongepowered.api.util.generator.dummy.DummyObjectProvider; + +// TODO I don't like this +public class ResourceTypes { + + // SORTFIELDS:ON + + public static final ResourceType ASSETS = DummyObjectProvider.createFor(ResourceType.class, "ASSETS"); + + public static final ResourceType DATA = DummyObjectProvider.createFor(ResourceType.class, "DATA"); + + // SORTFIELDS:OFF +} diff --git a/src/main/java/org/spongepowered/api/resource/package-info.java b/src/main/java/org/spongepowered/api/resource/package-info.java new file mode 100644 index 00000000000..1dcdcd693bf --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/package-info.java @@ -0,0 +1,25 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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. + */ +@org.spongepowered.api.util.annotation.NonnullByDefault package org.spongepowered.api.resource; From 8e0fc0e1bdca990172eb429ed3db93c7a96b9ece Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Mon, 8 Jan 2018 01:08:12 -0500 Subject: [PATCH 02/22] Resource.getReader now returns a BufferedReader --- src/main/java/org/spongepowered/api/resource/Resource.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index 6b1978a901a..67174737caa 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -74,8 +74,8 @@ public interface Resource extends ResourceData { * @return A new reader * @throws IOException if an error occurs */ - default Reader getReader(Charset charset) throws IOException { - return new InputStreamReader(openStream(), charset); + default BufferedReader getReader(Charset charset) throws IOException { + return new BufferedReader(new InputStreamReader(openStream(), charset)); } /** @@ -99,7 +99,7 @@ default String readString(Charset charset) throws IOException { * @throws IOException if an error occurs */ default Stream readLines(Charset charset) throws IOException { - try (BufferedReader r = new BufferedReader(getReader(charset))) { + try (BufferedReader r = getReader(charset)) { return r.lines(); } } @@ -155,5 +155,4 @@ default void copyTo(@WillNotClose OutputStream out) throws IOException { ByteStreams.copy(in, out); } } - } From cb2f43172ba4f77ea16a848224a0eb5a90f3213e Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Mon, 8 Jan 2018 19:51:33 -0500 Subject: [PATCH 03/22] ResourcePath is iterable on it's parents --- .../api/resource/ResourcePath.java | 41 ++++++++++--------- .../api/resource/ResourceType.java | 24 +++++++++++ .../api/resource/ResourceTypes.java | 24 +++++++++++ 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/ResourcePath.java b/src/main/java/org/spongepowered/api/resource/ResourcePath.java index b247065d1a4..fe637a5e902 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourcePath.java +++ b/src/main/java/org/spongepowered/api/resource/ResourcePath.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.Map; +import java.util.stream.Stream; /** *

A resource path should contain a namespace. If one is not provided, @@ -41,7 +42,7 @@ *

A resource path should be usable in a {@link Map}, so implementations * should override {@link #hashCode()} and {@link #equals(Object)}.

*/ -public interface ResourcePath extends Comparable { +public interface ResourcePath extends Comparable, Iterable { /** * Creates a new {@link Builder} to build a {@link ResourcePath}. @@ -94,36 +95,38 @@ static ResourcePath parse(String path) throws IllegalArgumentException { String getPath(); /** - * Gets the root path from this path. This is the same object as returned - * by the following code. - *
-     * List parents = getParents();
-     * if (parents.isEmpty()) {
-     *     return this;
-     * }
-     * return parents.get(parents.size() - 1);
-     * 
+ * Gets the root path from this path. * * @return The root path */ ResourcePath getRoot(); /** - * Gets a list of parents for this {@link ResourcePath}. Order of parents - * will be closest first, with the root being the last element. If the - * returned list is empty, this path is the root path. + * Gets the immediate parent for this {@link ResourcePath}. If this path is + * the root path, itself is returned. * - * @return The list of parents + * @return The parent path + * @see #getRoot() */ - List getParents(); + ResourcePath getParent(); /** - * Similar to {@link #getParents()}, but only returns the name portion of - * the paths. + * Gets a list of paths for this {@link ResourcePath}. Order of parents + * will be closest first, with the first element being this element and the + * root being the last element. If the returned list is empty, this path is + * the root path. * - * @return The list of names in the path. + * @return The list of paths */ - List getNames(); + List getParts(); + + /** + * Creates a new stream of all the paths in this {@link ResourcePath}. + * + * @return A new stream + * @see #getParts() + */ + Stream stream(); /** * Gets the file name portion of this {@link ResourcePath}. diff --git a/src/main/java/org/spongepowered/api/resource/ResourceType.java b/src/main/java/org/spongepowered/api/resource/ResourceType.java index a550438880f..94a94daacd3 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceType.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceType.java @@ -1,3 +1,27 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; import org.spongepowered.api.CatalogType; diff --git a/src/main/java/org/spongepowered/api/resource/ResourceTypes.java b/src/main/java/org/spongepowered/api/resource/ResourceTypes.java index 02e1c114ddd..d75848a149a 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceTypes.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceTypes.java @@ -1,3 +1,27 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; import org.spongepowered.api.util.generator.dummy.DummyObjectProvider; From 2efe859f45a4562999a5734ec72b22f20ff20ad4 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Mon, 8 Jan 2018 20:08:15 -0500 Subject: [PATCH 04/22] Fixup some javadoc --- .../java/org/spongepowered/api/resource/ResourcePath.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/ResourcePath.java b/src/main/java/org/spongepowered/api/resource/ResourcePath.java index fe637a5e902..575279ce25b 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourcePath.java +++ b/src/main/java/org/spongepowered/api/resource/ResourcePath.java @@ -111,10 +111,9 @@ static ResourcePath parse(String path) throws IllegalArgumentException { ResourcePath getParent(); /** - * Gets a list of paths for this {@link ResourcePath}. Order of parents - * will be closest first, with the first element being this element and the - * root being the last element. If the returned list is empty, this path is - * the root path. + * Gets a list of paths for this {@link ResourcePath}. Order of parts will + * be closest first, with the first element being this element and the root + * being the last element. * * @return The list of paths */ From 84a1b558e7d52aa2633ca9e5f7ff0a63e00775cd Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 9 Jan 2018 21:58:11 -0500 Subject: [PATCH 05/22] Add some filename related methods --- .../api/resource/ResourcePath.java | 74 ++++++++++++++++--- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/ResourcePath.java b/src/main/java/org/spongepowered/api/resource/ResourcePath.java index 575279ce25b..f36556cdf50 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourcePath.java +++ b/src/main/java/org/spongepowered/api/resource/ResourcePath.java @@ -27,8 +27,10 @@ import org.spongepowered.api.Sponge; import org.spongepowered.api.util.ResettableBuilder; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Stream; /** @@ -69,9 +71,9 @@ static ResourcePath of(String namespace, String path) throws IllegalArgumentExce * The representation should include both the namespace and path, separated * by a colon (:). Path items should be separated by a forward slash (/). * If the namespace is excluded, the default of minecraft is used instead. - * - *

e.g. {@code lagmeter:functions/lm.json} points to the - * functions/lm.json file in the lagmeter namespace.

+ *

+ *

e.g. {@code foo:bar/custom.json} points to the + * {@code bar/custom.json} file in the {@code foo} namespace.

* * @param path The path * @return A new ResourcePath @@ -111,28 +113,59 @@ static ResourcePath parse(String path) throws IllegalArgumentException { ResourcePath getParent(); /** - * Gets a list of paths for this {@link ResourcePath}. Order of parts will - * be closest first, with the first element being this element and the root - * being the last element. + * Gets a list of names which make up this {@link ResourcePath}. Order of + * names will be root first, with the last name being this path's + * filename. * * @return The list of paths */ - List getParts(); + List getNames(); /** - * Creates a new stream of all the paths in this {@link ResourcePath}. + * Tests if this {@link ResourcePath} is a direct child of the given other path. * - * @return A new stream - * @see #getParts() + * @param parent The other resource path to check against + * @return True if this is a child to other */ - Stream stream(); + boolean isParent(ResourcePath parent); /** * Gets the file name portion of this {@link ResourcePath}. * * @return The file name of the path + * @see #getBaseName() + * @see #getExtension() + */ + String getFileName(); + + /** + * Gets the base name of this path without the extension + * + * @return The base name + * @see #getFileName() + */ + String getBaseName(); + + /** + * Gets the extension of this path, which is the portion which is after the + * last dot if it has one. + * + *

If the path does not have an extension, {@link Optional#empty() empty} + * is returned.

+ * + * @return The extension of empty if not present + * @see #hasExtension(String...) + */ + Optional getExtension(); + + /** + * Tests if this path has one of the given extensions. Case is insensitive. + * + * @param ext The extensions to test + * @return True if the extension matches one of the ones provided + * @see #getExtension() */ - String getName(); + boolean hasExtension(String... ext); /** * Resolves a new {@link ResourcePath} with this one as the parent. @@ -151,6 +184,23 @@ static ResourcePath parse(String path) throws IllegalArgumentException { */ ResourcePath resolveSibling(String name); + /** + * Creates a new stream of paths leading up to this path, starting with the + * root. + * + * @return A new stream + */ + Stream stream(); + + /** + * Returns an iterator of the paths leading up to this path, starting with + * the root. + * + * @return The iterator + */ + @Override + Iterator iterator(); + /** * Represents a builder to create {@link ResourcePath} instances. */ From aa40cf5b446ffb67807604468ba109add9bcd684 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 9 Jan 2018 22:07:20 -0500 Subject: [PATCH 06/22] Add some methods to ResourceReloadEvent.Pre for manipulating the packs to load --- .../event/resource/ResourceReloadEvent.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java index b247500a648..ebc65804ecf 100644 --- a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java +++ b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java @@ -24,9 +24,13 @@ */ package org.spongepowered.api.event.resource; +import com.google.common.collect.ImmutableList; +import org.spongepowered.api.resource.Pack; import org.spongepowered.api.resource.Resource; import org.spongepowered.api.resource.ResourceManager; +import java.util.List; + /** * Base interface for resource reloading events. */ @@ -39,6 +43,27 @@ public interface ResourceReloadEvent extends ResourceEvent { */ interface Pre extends ResourceReloadEvent { + /** + * Gets an immutable list of the original packs to reload. + * + * @return The packs to reload + */ + ImmutableList getOriginalPacksToReload(); + + /** + * Gets a mutable list of the packs to reload. This list can be changed + * or may be set via {@link #setPacksToReload(List)}. + * + * @return The packs to reload + */ + List getPacksToReload(); + + /** + * Sets the packs to reload. + * + * @param packs The packs + */ + void setPacksToReload(List packs); } /** From 425b94e7f1a902a2f19cc544ef3efbee2ea773f1 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 9 Jan 2018 22:07:20 -0500 Subject: [PATCH 07/22] Add a method in Pack.Builder for providing from a file --- .../java/org/spongepowered/api/resource/Pack.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/org/spongepowered/api/resource/Pack.java b/src/main/java/org/spongepowered/api/resource/Pack.java index ffae2113b1c..dcb49153ba7 100644 --- a/src/main/java/org/spongepowered/api/resource/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/Pack.java @@ -29,6 +29,7 @@ import org.spongepowered.api.text.Text; import org.spongepowered.api.util.ResettableBuilder; +import java.nio.file.Path; import java.util.Collection; import java.util.Map; import java.util.Optional; @@ -126,6 +127,17 @@ interface Builder extends ResettableBuilder { */ Builder resources(Supplier> resources); + /** + * Uses the given path to provide the resources, as well as the + * metadata and name of pack if not yet provided. + * + *

The path can be either a directory or a zip file.

+ * + * @param path The path to a folder or zip + * @return This builder + */ + Builder from(Path path); + /** * Creates a new instance of {@link Pack}. * From 12cc2f687dc07f1840e86c85820a725eaa93776e Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sun, 11 Nov 2018 02:30:14 -0500 Subject: [PATCH 08/22] Rework the resource system to be more inline with vanilla. I'm still working on figuring out how ResourcePackList works. --- src/main/java/org/spongepowered/api/Game.java | 10 -- .../java/org/spongepowered/api/Server.java | 10 ++ .../java/org/spongepowered/api/Sponge.java | 6 -- .../spongepowered/api/asset/AssetManager.java | 2 +- .../event/resource/ResourceReloadEvent.java | 19 ++-- .../api/plugin/PluginContainer.java | 6 +- .../org/spongepowered/api/resource/Pack.java | 50 ++++++--- .../resource/ReloadableResourceManager.java | 55 ++++++++++ .../spongepowered/api/resource/Resource.java | 20 ++-- .../api/resource/ResourceData.java | 7 +- .../api/resource/ResourceManager.java | 56 +++++----- .../api/resource/ResourcePath.java | 9 -- .../api/resource/ResourceProvider.java | 101 ------------------ .../api/resource/ResourceType.java | 11 ++ .../api/resource/ResourceTypes.java | 1 - 15 files changed, 169 insertions(+), 194 deletions(-) create mode 100644 src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java delete mode 100644 src/main/java/org/spongepowered/api/resource/ResourceProvider.java diff --git a/src/main/java/org/spongepowered/api/Game.java b/src/main/java/org/spongepowered/api/Game.java index 5183c5b98c8..4579b744e67 100644 --- a/src/main/java/org/spongepowered/api/Game.java +++ b/src/main/java/org/spongepowered/api/Game.java @@ -35,7 +35,6 @@ import org.spongepowered.api.event.CauseStackManager; import org.spongepowered.api.event.EventManager; import org.spongepowered.api.network.ChannelRegistrar; -import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.api.plugin.PluginManager; import org.spongepowered.api.scheduler.Scheduler; import org.spongepowered.api.service.ServiceManager; @@ -174,15 +173,6 @@ default AssetManager getAssetManager() { return Sponge.getAssetManager(); } - /** - * Gets the {@link ResourceManager}. - * - * @return The resource manager - */ - default ResourceManager getResourceManager() { - return Sponge.getResourceManager(); - } - /** * Gets the {@link ConfigManager} used to load and manage configuration files * for plugins. diff --git a/src/main/java/org/spongepowered/api/Server.java b/src/main/java/org/spongepowered/api/Server.java index ca3dd73d384..434a8d5012a 100644 --- a/src/main/java/org/spongepowered/api/Server.java +++ b/src/main/java/org/spongepowered/api/Server.java @@ -27,6 +27,7 @@ import org.spongepowered.api.command.source.CommandSource; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.profile.GameProfileManager; +import org.spongepowered.api.resource.ReloadableResourceManager; import org.spongepowered.api.resourcepack.ResourcePack; import org.spongepowered.api.scoreboard.Scoreboard; import org.spongepowered.api.text.Text; @@ -102,6 +103,15 @@ public interface Server extends Engine, CommandSource { */ Optional getServerScoreboard(); + /** + * Gets the {@link ReloadableResourceManager} for the server instance. As of + * Minecraft 1.13 there is only one instance of the resource manager per + * server instance. It is not per-world. + * + * @return The resource manager + */ + ReloadableResourceManager getResourceManager(); + /** * Returns information about the chunk layout used by this server * implementation. diff --git a/src/main/java/org/spongepowered/api/Sponge.java b/src/main/java/org/spongepowered/api/Sponge.java index 1ea7fc7638c..2a7562363a9 100644 --- a/src/main/java/org/spongepowered/api/Sponge.java +++ b/src/main/java/org/spongepowered/api/Sponge.java @@ -37,7 +37,6 @@ import org.spongepowered.api.event.CauseStackManager; import org.spongepowered.api.event.EventManager; import org.spongepowered.api.network.ChannelRegistrar; -import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.api.plugin.PluginManager; import org.spongepowered.api.scheduler.Scheduler; import org.spongepowered.api.service.ServiceManager; @@ -60,7 +59,6 @@ public final class Sponge { @Inject private static PluginManager pluginManager; @Inject private static EventManager eventManager; @Inject private static AssetManager assetManager; - @Inject private static ResourceManager resourceManager; @Inject private static ConfigManager configManager; @Inject private static ServiceManager serviceManager; @Inject private static ChannelRegistrar channelRegistrar; @@ -152,10 +150,6 @@ public static AssetManager getAssetManager() { return check(assetManager); } - public static ResourceManager getResourceManager() { - return check(resourceManager); - } - /** * Gets the {@link ConfigManager} used to load and manage configuration files * for plugins. diff --git a/src/main/java/org/spongepowered/api/asset/AssetManager.java b/src/main/java/org/spongepowered/api/asset/AssetManager.java index 4380f991dd3..d0dfb8dc0f3 100644 --- a/src/main/java/org/spongepowered/api/asset/AssetManager.java +++ b/src/main/java/org/spongepowered/api/asset/AssetManager.java @@ -24,9 +24,9 @@ */ package org.spongepowered.api.asset; -import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.api.plugin.Plugin; import org.spongepowered.api.plugin.PluginContainer; +import org.spongepowered.api.resource.ResourceManager; import java.util.Optional; diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java index ebc65804ecf..975e3884711 100644 --- a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java +++ b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java @@ -24,21 +24,26 @@ */ package org.spongepowered.api.event.resource; -import com.google.common.collect.ImmutableList; +import java.util.List; + +import org.spongepowered.api.event.Cancellable; import org.spongepowered.api.resource.Pack; import org.spongepowered.api.resource.Resource; -import org.spongepowered.api.resource.ResourceManager; +import org.spongepowered.api.resource.ReloadableResourceManager; -import java.util.List; +import com.google.common.collect.ImmutableList; /** * Base interface for resource reloading events. */ -public interface ResourceReloadEvent extends ResourceEvent { +public interface ResourceReloadEvent extends ResourceEvent, Cancellable{ + + @Override + ReloadableResourceManager getResourceManager(); /** - * Called before the {@link ResourceManager} is reloaded. At this point, - * the {@link ResourceManager#getActivePacks() active packs} can be added + * Called before the {@link ReloadableResourceManager} is reloaded. At this point, + * the {@link ReloadableResourceManager#getActivePacks() active packs} can be added * or removed from. */ interface Pre extends ResourceReloadEvent { @@ -67,7 +72,7 @@ interface Pre extends ResourceReloadEvent { } /** - * Called after the {@link ResourceManager} is reloaded. When a + * Called after the {@link ReloadableResourceManager} is reloaded. When a * {@link Resource} is reloaded, this event should be used to obtain the new * file. */ diff --git a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java index 659c33ed03f..4d365fd38ad 100644 --- a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java +++ b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java @@ -33,7 +33,7 @@ import org.spongepowered.api.asset.Asset; import org.spongepowered.api.asset.AssetManager; import org.spongepowered.api.resource.Pack; -import org.spongepowered.api.resource.ResourceManager; +import org.spongepowered.api.resource.ReloadableResourceManager; import org.spongepowered.plugin.meta.PluginDependency; import java.nio.file.Path; @@ -141,12 +141,12 @@ default Optional getAsset(String name) { /** * Retrieves the {@link Pack} owned by this plugin from the - * {@link ResourceManager}. + * {@link ReloadableResourceManager}. * * @return The plugin's resource pack. */ default Pack getPack() { - return Sponge.getResourceManager().getPack(this); + return Sponge.getServer().getResourceManager().getPack(this); } /** diff --git a/src/main/java/org/spongepowered/api/resource/Pack.java b/src/main/java/org/spongepowered/api/resource/Pack.java index dcb49153ba7..5158b05c89b 100644 --- a/src/main/java/org/spongepowered/api/resource/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/Pack.java @@ -29,16 +29,19 @@ import org.spongepowered.api.text.Text; import org.spongepowered.api.util.ResettableBuilder; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.Path; import java.util.Collection; import java.util.Map; import java.util.Optional; +import java.util.function.Predicate; import java.util.function.Supplier; /** * A pack can contain several {@link Resource Resources}. */ -public interface Pack extends ResourceProvider { +public interface Pack { /** * Creates a new {@link Builder} instance. @@ -50,11 +53,35 @@ static Builder builder() { } /** - * Gets the name of this pack which is displayed to the user. + * Opens a new {@link InputStream} to the specified resource. * - * @return The name + * @param type The resource type + * @param path The path of the resource to open + * @return The input stream + * @throws IOException If the resource does not exist or another IOException occurs. */ - Text getName(); + InputStream openStream(ResourceType type, ResourcePath path) throws IOException; + + /** + * Recursively gets all the resources loaded from this pack. All namespaces + * are considered. + * + * @param type The resource type + * @param path The resource path + * @param filter The file name filter + * @return Collection of resources + * @see ResourceManager#getResources(String, Predicate) + */ + Collection getResources(ResourceType type, String path, Predicate filter); + + /** + * Checks if a resource exists in this pack. + * + * @param type The resource type + * @param path THe path of the resource + * @return True if the resource exists, false otherwise + */ + boolean exists(ResourceType type, ResourcePath path); /** * Gets the metadata of this pack. The {@link DataView} represented is of @@ -66,16 +93,11 @@ static Builder builder() { Optional getMetadata(); /** - * Gets all the resources loaded from this pack. Depending on the pack - * implementation, the contents of the collection may not reflect what the - * pack contains. - * - *

If the pack is lazy-initialized, it is possible for the collection to - * be empty.

+ * Gets the name of this pack which is displayed to the user. * - * @return List of loaded resources + * @return The name */ - Collection getAllResources(); + Text getName(); /** * A builder for a {@link Pack}. @@ -115,7 +137,9 @@ interface Builder extends ResettableBuilder { * @param resources The resources * @return This builder */ - Builder resources(Map resources); + default Builder resources(Map resources) { + return resources(() -> resources); + } /** * Provides a {@link Resource} list to include in the pack. The diff --git a/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java b/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java new file mode 100644 index 00000000000..42339db9e24 --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java @@ -0,0 +1,55 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * The resource manager is in charge of loading {@link Resource Resources} and + * {@link Pack Data Packs}. On the client, there can also be resource packs. + * + * Packs are stacked on top of each other, so they will override and replace + * resources in packs which are a lower priority. + */ +public interface ReloadableResourceManager extends ResourceManager, PackProvider { + + /** + * Schedules a reload of resources from active packs. + * + * @return A future for the task + */ + CompletableFuture reload(); + + /** + * Schedules a reload of resources, using the packs from {@code packs} + * + * @param packs The packs to reload with + * @return A future for the task + */ + CompletableFuture reload(List packs); + + ResourceType getResourceType(); +} diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index 67174737caa..948e16f25ec 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -32,6 +32,7 @@ import org.spongepowered.api.data.persistence.InvalidDataFormatException; import java.io.BufferedReader; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -46,9 +47,9 @@ /** * A resource can represent any kind of loaded data. It can be a file on the * filesystem, a network location, or even generated at runtime. Use - * {@link #openStream()} to load the data held by a resource. + * {@link #getInputStream()} to load the data held by a resource. */ -public interface Resource extends ResourceData { +public interface Resource extends ResourceData, Closeable { /** * Gets the path of this resource. @@ -64,18 +65,15 @@ public interface Resource extends ResourceData { * @return The parent pack. */ Pack getPack(); - - ResourceType getType(); /** * Gets a reader for this resource using the given {@link Charset}. * * @param charset The charset * @return A new reader - * @throws IOException if an error occurs */ - default BufferedReader getReader(Charset charset) throws IOException { - return new BufferedReader(new InputStreamReader(openStream(), charset)); + default BufferedReader getReader(Charset charset) { + return new BufferedReader(new InputStreamReader(getInputStream(), charset)); } /** @@ -111,7 +109,7 @@ default Stream readLines(Charset charset) throws IOException { * @throws IOException if an error occurs */ default byte[] readBytes() throws IOException { - try (InputStream in = openStream()) { + try (InputStream in = getInputStream()) { return ByteStreams.toByteArray(in); } } @@ -124,7 +122,7 @@ default byte[] readBytes() throws IOException { * @throws IOException if an error occurs */ default DataView readDataView(DataFormat format) throws IOException { - try (InputStream in = openStream()) { + try (InputStream in = getInputStream()) { return format.readFrom(in); } catch (InvalidDataFormatException e) { throw new IOException(e); @@ -139,7 +137,7 @@ default DataView readDataView(DataFormat format) throws IOException { * @throws IOException if an error occurs */ default void copyTo(Path path, OpenOption... options) throws IOException { - try (InputStream in = openStream()) { + try (InputStream in = getInputStream()) { MoreFiles.asByteSink(path, options).writeFrom(in); } } @@ -151,7 +149,7 @@ default void copyTo(Path path, OpenOption... options) throws IOException { * @throws IOException if an error occurs */ default void copyTo(@WillNotClose OutputStream out) throws IOException { - try (InputStream in = openStream()) { + try (InputStream in = getInputStream()) { ByteStreams.copy(in, out); } } diff --git a/src/main/java/org/spongepowered/api/resource/ResourceData.java b/src/main/java/org/spongepowered/api/resource/ResourceData.java index e8788bb2148..9409ec8751f 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceData.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceData.java @@ -26,7 +26,6 @@ import org.spongepowered.api.data.DataView; -import java.io.IOException; import java.io.InputStream; import java.util.Optional; @@ -41,9 +40,8 @@ public interface ResourceData { * should be created each time this method is called. * * @return A new input stream - * @throws IOException if an error occurs */ - InputStream openStream() throws IOException; + InputStream getInputStream(); /** * Gets the metadata for this resource. @@ -56,10 +54,9 @@ public interface ResourceData { * {@code minecraft:textures/blocks/water_flow.png.mcmeta}

* * @return The metadata or {@link Optional#empty() empty} if it doesn't exist. - * @throws IOException if the metadata file is corrupt or cannot be read. * @see
Minecraft Wiki/Resource Packs */ - default Optional getMetadata() throws IOException { + default Optional getMetadata() { return Optional.empty(); } } diff --git a/src/main/java/org/spongepowered/api/resource/ResourceManager.java b/src/main/java/org/spongepowered/api/resource/ResourceManager.java index f14a5b5a7ea..48fe7c4c592 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceManager.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceManager.java @@ -26,48 +26,50 @@ import java.util.Collection; import java.util.List; -import java.util.concurrent.CompletableFuture; +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; -/** - * The resource manager is in charge of loading {@link Resource Resources} and - * {@link Pack Data Packs}. On the client, there can also be resource packs. - * - * Packs are stacked on top of each other, so they will override and replace - * resources in packs which are a lower priority. - */ -public interface ResourceManager extends ResourceProvider, PackProvider { +public interface ResourceManager { /** - * Returns a list of currently active packs. The returned list is mutable, - * but changes will not be applied. To apply the changes, use - * {@link #reload(List)}. - * - * @return The list of active packs. + * Get the namespaces + * @return */ - List getActivePacks(); + Set getNamespaces(); /** - * Gets the resources at the given path from all active packs + * Gets a loaded resource at the given path, or {@link Optional#empty()} + * if it does not exist. * - * @param path The path of the resource. - * @return A collection of resources + * @param path The path to the resource + * @return The resource */ - Collection getResources(ResourcePath path); + Optional getResource(ResourcePath path); /** - * Schedules a reload of resources from active packs. + * Returns all of the resources which exist in each {@link Pack}. * - * @return A future for the task + * @return All of the resources */ - CompletableFuture reload(); + List getResources(ResourcePath path); /** - * Schedules a reload of resources, using the packs from {@code packs} + * Returns all {@link ResourcePath paths} within a directory from all the + * resource domains. + * + *

This can be used for resources which can have multiple locations. For + * example, {@code functions} and {@code loot_tables}.

+ * + *

For example

+ *
+     * manager.getResources("loot_tables", name -> name.endsWith(".json"));
+     * 
* - * @param packs The packs to reload with - * @return A future for the task + * @param path The path to search in + * @param filter The file name filter + * @return A collection of resource paths */ - CompletableFuture reload(List packs); + Collection getResources(String path, Predicate filter); - ResourceType getResourceType(); } diff --git a/src/main/java/org/spongepowered/api/resource/ResourcePath.java b/src/main/java/org/spongepowered/api/resource/ResourcePath.java index f36556cdf50..0c9a113f181 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourcePath.java +++ b/src/main/java/org/spongepowered/api/resource/ResourcePath.java @@ -31,7 +31,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Stream; /** *

A resource path should contain a namespace. If one is not provided, @@ -184,14 +183,6 @@ static ResourcePath parse(String path) throws IllegalArgumentException { */ ResourcePath resolveSibling(String name); - /** - * Creates a new stream of paths leading up to this path, starting with the - * root. - * - * @return A new stream - */ - Stream stream(); - /** * Returns an iterator of the paths leading up to this path, starting with * the root. diff --git a/src/main/java/org/spongepowered/api/resource/ResourceProvider.java b/src/main/java/org/spongepowered/api/resource/ResourceProvider.java deleted file mode 100644 index b25fa169c40..00000000000 --- a/src/main/java/org/spongepowered/api/resource/ResourceProvider.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.resource; - -import com.google.common.collect.Streams; - -import java.util.Collection; -import java.util.Optional; -import java.util.stream.Stream; - -public interface ResourceProvider { - - /** - * Gets a loaded resource at the given path, or {@link Optional#empty()} - * if it does not exist. - * - * @param path The path to the resource - * @return The resource - */ - Optional getResource(ResourcePath path); - - /** - * Returns all of the resources which exist. - * - * @return All of the resources - */ - Collection getResources(); - - /** - * Walks though the loaded {@link ResourcePath paths}, which are children - * of the given path. The paths are traversed depth-first. Children will - * include both directories and files. - * - * @param path The start path - * @return A stream of resource paths - */ - Stream walk(ResourcePath path); - - /** - * Walks though the loaded {@link ResourcePath paths}, which are children - * of the given path. The paths are traversed depth-first. Children will - * include both directories and files. - * - *

The {@code maxDepth} parameter is used to limit the number of - * directories that will be traversed.

- * - * @param path The start path - * @param maxDepth The max number of directories to traverse - * @return A stream of resource paths - */ - Stream walk(ResourcePath path, int maxDepth); - - /** - * Walks though loaded {@link Resource resources}, which are children of - * the given path. The paths are traversed depth-first. - * - * @param path The start path - * @return A stream of resources - */ - default Stream walkResources(ResourcePath path) { - return walk(path).map(this::getResource).flatMap(Streams::stream); - } - - /** - * Walks though loaded {@link Resource resources}, which are children of - * the given path. The paths are traversed depth-first. - * - *

The {@code maxDepth} parameter is used to limit the number of - * directories that will be traversed.

- * - * @param path The start path - * @param maxDepth The max number of directories to traverse - * @return A stream of resources - */ - default Stream walkResources(ResourcePath path, int maxDepth) { - return walk(path, maxDepth).map(this::getResource).flatMap(Streams::stream); - } - -} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceType.java b/src/main/java/org/spongepowered/api/resource/ResourceType.java index 94a94daacd3..a3a1e2528dd 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceType.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceType.java @@ -27,6 +27,17 @@ import org.spongepowered.api.CatalogType; import org.spongepowered.api.util.annotation.CatalogedBy; +/** + * Represents the root directory for different resource types. Current + * values in vanilla is assets and data. + */ @CatalogedBy(ResourceTypes.class) public interface ResourceType extends CatalogType { + + /** + * Gets the name of the root directory path for this resource type. + * + * @return The root name + */ + String getRootName(); } diff --git a/src/main/java/org/spongepowered/api/resource/ResourceTypes.java b/src/main/java/org/spongepowered/api/resource/ResourceTypes.java index d75848a149a..a99a7b30963 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceTypes.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceTypes.java @@ -26,7 +26,6 @@ import org.spongepowered.api.util.generator.dummy.DummyObjectProvider; -// TODO I don't like this public class ResourceTypes { // SORTFIELDS:ON From abd3024e042cebd783bd3dbb5fdd9d359c35a99e Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sun, 11 Nov 2018 15:47:19 -0500 Subject: [PATCH 09/22] Add a PackRepository and PackInfo. The PackInfo is used to get the Pack, but the PackInfo cannot be gotten back from the pack. Temp remove ResourceId until I can figure out a good way to use it. Remove the try-with-resources from Resource. It should be done from the caller. --- .../java/org/spongepowered/api/Server.java | 7 ++ .../api/plugin/PluginContainer.java | 12 +-- .../org/spongepowered/api/resource/Pack.java | 90 +------------------ .../spongepowered/api/resource/PackInfo.java | 89 ++++++++++++++++++ ...{PackProvider.java => PackRepository.java} | 54 ++++++----- .../resource/ReloadableResourceManager.java | 27 +----- .../spongepowered/api/resource/Resource.java | 64 ++++++++----- .../api/resource/ResourceData.java | 62 ------------- .../api/resource/ResourceId.java | 62 ------------- .../api/resource/ResourceManager.java | 6 +- 10 files changed, 189 insertions(+), 284 deletions(-) create mode 100644 src/main/java/org/spongepowered/api/resource/PackInfo.java rename src/main/java/org/spongepowered/api/resource/{PackProvider.java => PackRepository.java} (65%) delete mode 100644 src/main/java/org/spongepowered/api/resource/ResourceData.java delete mode 100644 src/main/java/org/spongepowered/api/resource/ResourceId.java diff --git a/src/main/java/org/spongepowered/api/Server.java b/src/main/java/org/spongepowered/api/Server.java index 434a8d5012a..ed403ded73c 100644 --- a/src/main/java/org/spongepowered/api/Server.java +++ b/src/main/java/org/spongepowered/api/Server.java @@ -27,6 +27,7 @@ import org.spongepowered.api.command.source.CommandSource; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.profile.GameProfileManager; +import org.spongepowered.api.resource.PackRepository; import org.spongepowered.api.resource.ReloadableResourceManager; import org.spongepowered.api.resourcepack.ResourcePack; import org.spongepowered.api.scoreboard.Scoreboard; @@ -112,6 +113,12 @@ public interface Server extends Engine, CommandSource { */ ReloadableResourceManager getResourceManager(); + /** + * Gets the {@link PackRepository} for the game, which discovers and + * activates resource and data packs. + */ + PackRepository getPackRepository(); + /** * Returns information about the chunk layout used by this server * implementation. diff --git a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java index 4d365fd38ad..ea18eae285b 100644 --- a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java +++ b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java @@ -32,8 +32,8 @@ import org.spongepowered.api.Sponge; import org.spongepowered.api.asset.Asset; import org.spongepowered.api.asset.AssetManager; -import org.spongepowered.api.resource.Pack; -import org.spongepowered.api.resource.ReloadableResourceManager; +import org.spongepowered.api.resource.PackInfo; +import org.spongepowered.api.resource.PackRepository; import org.spongepowered.plugin.meta.PluginDependency; import java.nio.file.Path; @@ -140,13 +140,13 @@ default Optional getAsset(String name) { } /** - * Retrieves the {@link Pack} owned by this plugin from the - * {@link ReloadableResourceManager}. + * Retrieves the {@link PackInfo} owned by this plugin from the + * {@link PackRepository}. * * @return The plugin's resource pack. */ - default Pack getPack() { - return Sponge.getServer().getResourceManager().getPack(this); + default PackInfo getPack() { + return Sponge.getServer().getPackRepository().getPack(this); } /** diff --git a/src/main/java/org/spongepowered/api/resource/Pack.java b/src/main/java/org/spongepowered/api/resource/Pack.java index 5158b05c89b..a237fbac9c3 100644 --- a/src/main/java/org/spongepowered/api/resource/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/Pack.java @@ -24,34 +24,20 @@ */ package org.spongepowered.api.resource; -import org.spongepowered.api.Sponge; import org.spongepowered.api.data.DataView; -import org.spongepowered.api.text.Text; -import org.spongepowered.api.util.ResettableBuilder; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Path; import java.util.Collection; -import java.util.Map; import java.util.Optional; import java.util.function.Predicate; -import java.util.function.Supplier; /** - * A pack can contain several {@link Resource Resources}. + * A pack can contain several {@link Resource Resources}. Each pack is + * independently loaded with a configured priority. */ public interface Pack { - /** - * Creates a new {@link Builder} instance. - * - * @return A new builder - */ - static Builder builder() { - return Sponge.getRegistry().createBuilder(Builder.class); - } - /** * Opens a new {@link InputStream} to the specified resource. * @@ -97,76 +83,6 @@ static Builder builder() { * * @return The name */ - Text getName(); - - /** - * A builder for a {@link Pack}. - */ - interface Builder extends ResettableBuilder { - - /** - * Sets the name of this pack as it will appear in chat. - * - * @param name The name - * @return This builder - */ - Builder name(Text name); - - /** - * Sets the metadata for this pack. - * - * @param metadata The metadata - * @return This builder - */ - Builder metadata(DataView metadata); - - /** - * Adds a static {@link Resource} resource to the {@link Pack}. - * - * @param path The path - * @param resource The data for the resource - * @return This builder - */ - Builder resource(ResourcePath path, ResourceData resource); - - /** - * Specifies static {@link Resource}s to include in the {@link Pack}. - * Content is loaded along-with the pack. The resources themselves - * cannot be changed, but the contents are able to be reloaded. - * - * @param resources The resources - * @return This builder - */ - default Builder resources(Map resources) { - return resources(() -> resources); - } - - /** - * Provides a {@link Resource} list to include in the pack. The - * resources are loaded when the pack is loaded. Resources can be added - * or removed as needed. - * - * @param resources The resources to load - * @return This builder - */ - Builder resources(Supplier> resources); - - /** - * Uses the given path to provide the resources, as well as the - * metadata and name of pack if not yet provided. - * - *

The path can be either a directory or a zip file.

- * - * @param path The path to a folder or zip - * @return This builder - */ - Builder from(Path path); + String getName(); - /** - * Creates a new instance of {@link Pack}. - * - * @return A new pack - */ - Pack build(); - } } diff --git a/src/main/java/org/spongepowered/api/resource/PackInfo.java b/src/main/java/org/spongepowered/api/resource/PackInfo.java new file mode 100644 index 00000000000..24630b14c04 --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/PackInfo.java @@ -0,0 +1,89 @@ +/* + * This file is part of SpongeAPI, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * 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.spongepowered.api.resource; + +import org.spongepowered.api.text.Text; + +/** + * Basic info about a {@link Pack} + */ +public interface PackInfo { + + /** + * Gets the name of the {@link Pack} used to identify it in the + * {@link PackRepository}. + * + * @return The pack name + */ + String getName(); + + /** + * Gets the display name of the {@link Pack} which is displayed to the user. + * + * @return The display name + */ + Text getDisplayName(); + + /** + * Gets the description of the {@link Pack} which is displayed as the hover + * text to the user. + * + * @return The description + */ + Text getDescription(); + + /** + * Formats the text of the pack info to display to the user. + * + * @param active Whether this pack is active or not + * @return The formatted text + */ + Text formatName(boolean active); + + /** + * Gets the {@link Pack} associated with this pack info. + * + * @return + */ + Pack getPack(); + + /** + * Gets whether or not this pack is required to be active. If it is + * required to be active, it cannot be disabled and will be reactivated + * when reloaded. + * + * @return True if required, false otherwise + */ + boolean isRequired(); + + /** + * Gets whether or not this pack was loaded from a remote location. For + * example, if this is a server-defined client resource pack. + * + * @return Whether this pack is remote + */ + boolean isRemote(); + +} diff --git a/src/main/java/org/spongepowered/api/resource/PackProvider.java b/src/main/java/org/spongepowered/api/resource/PackRepository.java similarity index 65% rename from src/main/java/org/spongepowered/api/resource/PackProvider.java rename to src/main/java/org/spongepowered/api/resource/PackRepository.java index da3b080532e..5ec057df787 100644 --- a/src/main/java/org/spongepowered/api/resource/PackProvider.java +++ b/src/main/java/org/spongepowered/api/resource/PackRepository.java @@ -26,18 +26,43 @@ import org.spongepowered.api.plugin.PluginContainer; -import java.util.Map; +import java.util.Collection; import java.util.Optional; -public interface PackProvider { +public interface PackRepository { /** - * Returns a map of pack ids to available packs. An available pack could be - * active or not. The key in the returned map should correspond it its id. + * Sets the active packs to be used when reloading resources. Calls to this + * should be followed by {@link ReloadableResourceManager#reload()}. + * + * @param packs The active packs + * @see ReloadableResourceManager#reload() + */ + void setActivePacks(Collection packs); + + /** + * Returns a collection of all available packs. An available pack could be + * enabled or not. * * @return A collection of available packs. */ - Map getPacks(); + Collection getAllPacks(); + + /** + * Returns a collection of the disabled packs. Disabled packs are present, + * but not used in the {@link ResourceManager}. Resources can still be + * accessed using {@link PackInfo#getPack()}. + * + * @return The disabled packs + */ + Collection getDisabledPacks(); + + /** + * Returns a collection of enabled packs. + * + * @return The enabled packs + */ + Collection getEnabledPacks(); /** * Gets the {@link Pack} defined from {@link PluginContainer#getSource()}. @@ -47,7 +72,7 @@ public interface PackProvider { * @return The pack * @throws IllegalArgumentException if the object is not a plugin */ - Pack getPack(Object plugin); + PackInfo getPack(Object plugin); /** * Gets the pack by its name. @@ -55,21 +80,6 @@ public interface PackProvider { * @param name The pack's name. * @return The pack or empty if it doesn't exist */ - Optional getPack(String name); + Optional getPack(String name); - /** - * Registers a pack to be usable. - * - * @param packId The id of the pack - * @param pack The pack to register - */ - void registerPack(String packId, Pack pack); - - /** - * Unregisters a pack by its name. - * - * @param packId The id of the pack - * @return The pack which was unregistered or empty if it wasn't registered - */ - Optional unregisterPack(String packId); } diff --git a/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java b/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java index 42339db9e24..4f52d6465fb 100644 --- a/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java +++ b/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java @@ -24,32 +24,11 @@ */ package org.spongepowered.api.resource; -import java.util.List; -import java.util.concurrent.CompletableFuture; - -/** - * The resource manager is in charge of loading {@link Resource Resources} and - * {@link Pack Data Packs}. On the client, there can also be resource packs. - * - * Packs are stacked on top of each other, so they will override and replace - * resources in packs which are a lower priority. - */ -public interface ReloadableResourceManager extends ResourceManager, PackProvider { - - /** - * Schedules a reload of resources from active packs. - * - * @return A future for the task - */ - CompletableFuture reload(); +public interface ReloadableResourceManager extends ResourceManager { /** - * Schedules a reload of resources, using the packs from {@code packs} - * - * @param packs The packs to reload with - * @return A future for the task + * Reloads the resources from active packs. */ - CompletableFuture reload(List packs); + void reload(); - ResourceType getResourceType(); } diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index 948e16f25ec..fb8faeea12a 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -37,10 +37,10 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.Reader; import java.nio.charset.Charset; import java.nio.file.OpenOption; import java.nio.file.Path; +import java.util.Optional; import java.util.stream.Stream; import javax.annotation.WillNotClose; @@ -48,8 +48,41 @@ * A resource can represent any kind of loaded data. It can be a file on the * filesystem, a network location, or even generated at runtime. Use * {@link #getInputStream()} to load the data held by a resource. + * + *

Resource extends Closeable, so don't forget to close your streams either + * with a try-with-resources or a close() inside a try-finally block. e.g.

+ *
+ *     try (Resource res = resourceManager.getResource(path)) {
+ *         logger.info(res.readString(DefaultCharsets.UTF_8));
+ *     } catch (IOException e) {
+ *         e.printStackTrace();
+ *     }
+ * 
*/ -public interface Resource extends ResourceData, Closeable { +public interface Resource extends Closeable { + + /** + * Returns a new {@link InputStream} of this resource. A new input stream + * should be created each time this method is called. + * + * @return A new input stream + */ + InputStream getInputStream(); + + /** + * Gets the metadata for this resource. + * + *

The metadata file has the same name as this resource, but has + * {@code .mcmeta} appended to the end.

+ * + *

For example: the metadata for the resource + * {@code minecraft:textures/blocks/water_flow.png} would be located at + * {@code minecraft:textures/blocks/water_flow.png.mcmeta}

+ * + * @return The metadata or {@link Optional#empty() empty} if it doesn't exist. + * @see
Minecraft Wiki/Resource Packs + */ + Optional getMetadata(); /** * Gets the path of this resource. @@ -84,9 +117,7 @@ default BufferedReader getReader(Charset charset) { * @throws IOException if an error occurs */ default String readString(Charset charset) throws IOException { - try (Reader r = getReader(charset)) { - return CharStreams.toString(r); - } + return CharStreams.toString(getReader(charset)); } /** @@ -96,10 +127,8 @@ default String readString(Charset charset) throws IOException { * @return The list of strings * @throws IOException if an error occurs */ - default Stream readLines(Charset charset) throws IOException { - try (BufferedReader r = getReader(charset)) { - return r.lines(); - } + default Stream readLines(Charset charset) { + return getReader(charset).lines(); } /** @@ -109,9 +138,7 @@ default Stream readLines(Charset charset) throws IOException { * @throws IOException if an error occurs */ default byte[] readBytes() throws IOException { - try (InputStream in = getInputStream()) { - return ByteStreams.toByteArray(in); - } + return ByteStreams.toByteArray(getInputStream()); } /** @@ -122,8 +149,8 @@ default byte[] readBytes() throws IOException { * @throws IOException if an error occurs */ default DataView readDataView(DataFormat format) throws IOException { - try (InputStream in = getInputStream()) { - return format.readFrom(in); + try { + return format.readFrom(getInputStream()); } catch (InvalidDataFormatException e) { throw new IOException(e); } @@ -137,9 +164,7 @@ default DataView readDataView(DataFormat format) throws IOException { * @throws IOException if an error occurs */ default void copyTo(Path path, OpenOption... options) throws IOException { - try (InputStream in = getInputStream()) { - MoreFiles.asByteSink(path, options).writeFrom(in); - } + MoreFiles.asByteSink(path, options).writeFrom(getInputStream()); } /** @@ -149,8 +174,7 @@ default void copyTo(Path path, OpenOption... options) throws IOException { * @throws IOException if an error occurs */ default void copyTo(@WillNotClose OutputStream out) throws IOException { - try (InputStream in = getInputStream()) { - ByteStreams.copy(in, out); - } + ByteStreams.copy(getInputStream(), out); } + } diff --git a/src/main/java/org/spongepowered/api/resource/ResourceData.java b/src/main/java/org/spongepowered/api/resource/ResourceData.java deleted file mode 100644 index 9409ec8751f..00000000000 --- a/src/main/java/org/spongepowered/api/resource/ResourceData.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.resource; - -import org.spongepowered.api.data.DataView; - -import java.io.InputStream; -import java.util.Optional; - -/** - * Provides a new {@link InputStream} for a {@link Resource}. - */ -@FunctionalInterface -public interface ResourceData { - - /** - * Returns a new {@link InputStream} of this resource. A new input stream - * should be created each time this method is called. - * - * @return A new input stream - */ - InputStream getInputStream(); - - /** - * Gets the metadata for this resource. - * - *

The metadata file has the same name as this resource, but has - * {@code .mcmeta} appended to the end.

- * - *

For example: the metadata for the resource - * {@code minecraft:textures/blocks/water_flow.png} would be located at - * {@code minecraft:textures/blocks/water_flow.png.mcmeta}

- * - * @return The metadata or {@link Optional#empty() empty} if it doesn't exist. - * @see
Minecraft Wiki/Resource Packs - */ - default Optional getMetadata() { - return Optional.empty(); - } -} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceId.java b/src/main/java/org/spongepowered/api/resource/ResourceId.java deleted file mode 100644 index b18be7dbdb6..00000000000 --- a/src/main/java/org/spongepowered/api/resource/ResourceId.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.resource; - -import com.google.inject.BindingAnnotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Annotation for injecting resources into a plugin instance. - * - * The type this should be used on is {@code Optional}. When applied - * to a method, the {@link Resource} will be automatically updated when the - * {@link ResourceManager} is reloaded. - * - * @see ResourceManager#getResource(ResourcePath) - */ -@BindingAnnotation -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.PARAMETER, ElementType.FIELD}) -public @interface ResourceId { - - /** - * The namespace of the {@link Resource}. If not provided, it will default - * to the owner's plugin id. - * - * @see ResourcePath#getNamespace() - */ - String namespace() default ""; - - /** - * The path to the {@link Resource}. - * - * @see ResourcePath#getPath() - */ - String value(); -} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceManager.java b/src/main/java/org/spongepowered/api/resource/ResourceManager.java index 48fe7c4c592..61a462351cb 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceManager.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceManager.java @@ -24,12 +24,16 @@ */ package org.spongepowered.api.resource; +import java.io.IOException; import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.function.Predicate; +/** + * The resource manager is in charge of loading {@link Resource Resources}. + */ public interface ResourceManager { /** @@ -45,7 +49,7 @@ public interface ResourceManager { * @param path The path to the resource * @return The resource */ - Optional getResource(ResourcePath path); + Resource getResource(ResourcePath path) throws IOException; /** * Returns all of the resources which exist in each {@link Pack}. From 58020b583697a19611007f9b899a531b0240184b Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sun, 11 Nov 2018 23:02:24 -0500 Subject: [PATCH 10/22] Update some javadocs and modify a few methods --- .../event/resource/ResourceReloadEvent.java | 44 ++++++------------- .../spongepowered/api/resource/Resource.java | 8 ++-- .../api/resource/ResourceManager.java | 17 +++++-- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java index 975e3884711..4919a26a15d 100644 --- a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java +++ b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java @@ -24,58 +24,40 @@ */ package org.spongepowered.api.event.resource; -import java.util.List; - import org.spongepowered.api.event.Cancellable; import org.spongepowered.api.resource.Pack; -import org.spongepowered.api.resource.Resource; +import org.spongepowered.api.resource.PackRepository; import org.spongepowered.api.resource.ReloadableResourceManager; - -import com.google.common.collect.ImmutableList; +import org.spongepowered.api.resource.Resource; /** * Base interface for resource reloading events. */ -public interface ResourceReloadEvent extends ResourceEvent, Cancellable{ - - @Override - ReloadableResourceManager getResourceManager(); +public interface ResourceReloadEvent extends ResourceEvent, Cancellable { /** - * Called before the {@link ReloadableResourceManager} is reloaded. At this point, - * the {@link ReloadableResourceManager#getActivePacks() active packs} can be added - * or removed from. + * Called before the {@link ReloadableResourceManager} is reloaded. At this + * point, the {@link PackRepository#getEnabledPacks() enables packs} can be + * added or removed from. */ interface Pre extends ResourceReloadEvent { /** - * Gets an immutable list of the original packs to reload. + * Gets the {@link PackRepository} used to reload the resources. + * {@link Pack packs} and order can be changed using this instance. * - * @return The packs to reload + * @return The pack repository. */ - ImmutableList getOriginalPacksToReload(); + PackRepository getPackRepository(); - /** - * Gets a mutable list of the packs to reload. This list can be changed - * or may be set via {@link #setPacksToReload(List)}. - * - * @return The packs to reload - */ - List getPacksToReload(); - - /** - * Sets the packs to reload. - * - * @param packs The packs - */ - void setPacksToReload(List packs); } /** * Called after the {@link ReloadableResourceManager} is reloaded. When a - * {@link Resource} is reloaded, this event should be used to obtain the new - * file. + * {@link Resource} is reloaded, this event should be used to obtain the + * new file. */ interface Post extends ResourceReloadEvent { + } } diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index fb8faeea12a..56786cd8251 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -89,15 +89,14 @@ public interface Resource extends Closeable { * * @return The path */ - ResourcePath getResourcePath(); + ResourcePath getPath(); /** - * Gets the {@link Pack} which owns this resource. The pack is set - * automatically when the resource is added to a pack. + * Gets the pack name which owns this resource. * * @return The parent pack. */ - Pack getPack(); + String getPack(); /** * Gets a reader for this resource using the given {@link Charset}. @@ -125,7 +124,6 @@ default String readString(Charset charset) throws IOException { * * @param charset The charset * @return The list of strings - * @throws IOException if an error occurs */ default Stream readLines(Charset charset) { return getReader(charset).lines(); diff --git a/src/main/java/org/spongepowered/api/resource/ResourceManager.java b/src/main/java/org/spongepowered/api/resource/ResourceManager.java index 61a462351cb..df4b332f887 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceManager.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceManager.java @@ -32,13 +32,15 @@ import java.util.function.Predicate; /** - * The resource manager is in charge of loading {@link Resource Resources}. + * The resource manager is in charge of loading {@link Resource Resources}. It + * does this using active {@link Pack Packs} from the {@link PackRepository}. */ public interface ResourceManager { /** - * Get the namespaces - * @return + * Get the known namespaces to the resource manager. + * + * @return The known namespaces */ Set getNamespaces(); @@ -76,4 +78,13 @@ public interface ResourceManager { */ Collection getResources(String path, Predicate filter); + /** + * Returns true if the given {@link ResourcePath} exists in the active + * {@link Pack packs}. + * + * @param path The path to check + * @return True if the path exists, false otherwise + */ + boolean exists(ResourcePath path); + } From 0cf1c8398279eb1ad5c213b80d57750f215c46bb Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Mon, 21 Jan 2019 14:35:00 -0500 Subject: [PATCH 11/22] Add resourcetype argument to PluginContainer.getPack() Also some javadocs --- .../api/event/resource/ResourceReloadEvent.java | 2 +- .../java/org/spongepowered/api/plugin/PluginContainer.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java index 4919a26a15d..458bd2549ba 100644 --- a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java +++ b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java @@ -37,7 +37,7 @@ public interface ResourceReloadEvent extends ResourceEvent, Cancellable { /** * Called before the {@link ReloadableResourceManager} is reloaded. At this - * point, the {@link PackRepository#getEnabledPacks() enables packs} can be + * point, the {@link PackRepository#getEnabledPacks() enabled packs} can be * added or removed from. */ interface Pre extends ResourceReloadEvent { diff --git a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java index ea18eae285b..a9cde68a1c7 100644 --- a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java +++ b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java @@ -34,6 +34,7 @@ import org.spongepowered.api.asset.AssetManager; import org.spongepowered.api.resource.PackInfo; import org.spongepowered.api.resource.PackRepository; +import org.spongepowered.api.resource.ResourceType; import org.spongepowered.plugin.meta.PluginDependency; import java.nio.file.Path; @@ -133,6 +134,7 @@ default Optional getDependency(String id) { * * @param name Name of asset * @return Asset if present, empty otherwise + * @deprecated Use {@link #getPack(ResourceType)} instead. */ @Deprecated default Optional getAsset(String name) { @@ -143,9 +145,11 @@ default Optional getAsset(String name) { * Retrieves the {@link PackInfo} owned by this plugin from the * {@link PackRepository}. * + * @param type The type of resource pack + * * @return The plugin's resource pack. */ - default PackInfo getPack() { + default PackInfo getPack(ResourceType type) { return Sponge.getServer().getPackRepository().getPack(this); } From 2a786323aa8ea34bb1dd69e42929db4126247ca4 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Thu, 2 May 2019 23:35:40 -0400 Subject: [PATCH 12/22] Change for 1.14 Adds async reloaders Tweak some method names Remove some less useful methods TODO: Javadocs --- .../java/org/spongepowered/api/Engine.java | 21 +++++- .../java/org/spongepowered/api/Server.java | 17 +---- .../api/event/resource/ResourceEvent.java | 39 ----------- .../event/resource/ResourceReloadEvent.java | 63 ------------------ .../api/plugin/PluginContainer.java | 31 --------- .../api/resource/AsyncReloadListener.java | 14 ++++ .../api/resource/AsyncReloader.java | 34 ++++++++++ .../resource/ReloadableResourceManager.java | 9 +-- .../spongepowered/api/resource/Resource.java | 2 +- .../api/resource/ResourceManager.java | 21 ++++-- .../api/resource/ResourcePath.java | 64 ++++++++++--------- .../api/resource/{ => pack}/Pack.java | 13 ++-- .../api/resource/{ => pack}/PackInfo.java | 32 +++++++--- .../resource/{ => pack}/PackRepository.java | 4 +- .../{ResourceType.java => pack/PackType.java} | 6 +- .../PackTypes.java} | 8 +-- .../pack}/package-info.java | 2 +- 17 files changed, 166 insertions(+), 214 deletions(-) delete mode 100644 src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java delete mode 100644 src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java create mode 100644 src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java create mode 100644 src/main/java/org/spongepowered/api/resource/AsyncReloader.java rename src/main/java/org/spongepowered/api/resource/{ => pack}/Pack.java (85%) rename src/main/java/org/spongepowered/api/resource/{ => pack}/PackInfo.java (85%) rename src/main/java/org/spongepowered/api/resource/{ => pack}/PackRepository.java (94%) rename src/main/java/org/spongepowered/api/resource/{ResourceType.java => pack/PackType.java} (92%) rename src/main/java/org/spongepowered/api/resource/{ResourceTypes.java => pack/PackTypes.java} (83%) rename src/main/java/org/spongepowered/api/{event/resource => resource/pack}/package-info.java (97%) diff --git a/src/main/java/org/spongepowered/api/Engine.java b/src/main/java/org/spongepowered/api/Engine.java index 304243ab838..b6f808732d5 100644 --- a/src/main/java/org/spongepowered/api/Engine.java +++ b/src/main/java/org/spongepowered/api/Engine.java @@ -25,12 +25,16 @@ package org.spongepowered.api; import org.spongepowered.api.client.Client; +import org.spongepowered.api.resource.ResourceManager; +import org.spongepowered.api.resource.pack.PackRepository; import org.spongepowered.api.scheduler.Scheduler; +import java.util.concurrent.Executor; + /** * Shared functionality between {@link Client} and {@link Server} engines. */ -public interface Engine { +public interface Engine extends Executor { /** * Gets the {@link Scheduler} used to schedule sync tasks on this {@link Engine}. @@ -39,6 +43,21 @@ public interface Engine { */ Scheduler getScheduler(); + /** + * Gets the {@link ResourceManager} for the server instance. As of + * Minecraft 1.13 there is only one instance of the resource manager per + * server instance. It is not per-world. + * + * @return The resource manager + */ + ResourceManager getResourceManager(); + + /** + * Gets the {@link PackRepository} for the game, which discovers and + * activates resource and data packs. + */ + PackRepository getPackRepository(); + /** * Checks if the {@link Thread#currentThread() current thread} is the main thread of the engine. * diff --git a/src/main/java/org/spongepowered/api/Server.java b/src/main/java/org/spongepowered/api/Server.java index ed403ded73c..f0e6ffaaf2d 100644 --- a/src/main/java/org/spongepowered/api/Server.java +++ b/src/main/java/org/spongepowered/api/Server.java @@ -27,7 +27,7 @@ import org.spongepowered.api.command.source.CommandSource; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.profile.GameProfileManager; -import org.spongepowered.api.resource.PackRepository; +import org.spongepowered.api.resource.pack.PackRepository; import org.spongepowered.api.resource.ReloadableResourceManager; import org.spongepowered.api.resourcepack.ResourcePack; import org.spongepowered.api.scoreboard.Scoreboard; @@ -104,21 +104,6 @@ public interface Server extends Engine, CommandSource { */ Optional getServerScoreboard(); - /** - * Gets the {@link ReloadableResourceManager} for the server instance. As of - * Minecraft 1.13 there is only one instance of the resource manager per - * server instance. It is not per-world. - * - * @return The resource manager - */ - ReloadableResourceManager getResourceManager(); - - /** - * Gets the {@link PackRepository} for the game, which discovers and - * activates resource and data packs. - */ - PackRepository getPackRepository(); - /** * Returns information about the chunk layout used by this server * implementation. diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java deleted file mode 100644 index 75d055d79ac..00000000000 --- a/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.event.resource; - -import org.spongepowered.api.event.Event; -import org.spongepowered.api.resource.ResourceManager; - -public interface ResourceEvent extends Event { - - /** - * Gets the {@link ResourceManager} which caused this event. - * - * @return The resource manager - */ - ResourceManager getResourceManager(); - -} diff --git a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java b/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java deleted file mode 100644 index 458bd2549ba..00000000000 --- a/src/main/java/org/spongepowered/api/event/resource/ResourceReloadEvent.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.event.resource; - -import org.spongepowered.api.event.Cancellable; -import org.spongepowered.api.resource.Pack; -import org.spongepowered.api.resource.PackRepository; -import org.spongepowered.api.resource.ReloadableResourceManager; -import org.spongepowered.api.resource.Resource; - -/** - * Base interface for resource reloading events. - */ -public interface ResourceReloadEvent extends ResourceEvent, Cancellable { - - /** - * Called before the {@link ReloadableResourceManager} is reloaded. At this - * point, the {@link PackRepository#getEnabledPacks() enabled packs} can be - * added or removed from. - */ - interface Pre extends ResourceReloadEvent { - - /** - * Gets the {@link PackRepository} used to reload the resources. - * {@link Pack packs} and order can be changed using this instance. - * - * @return The pack repository. - */ - PackRepository getPackRepository(); - - } - - /** - * Called after the {@link ReloadableResourceManager} is reloaded. When a - * {@link Resource} is reloaded, this event should be used to obtain the - * new file. - */ - interface Post extends ResourceReloadEvent { - - } -} diff --git a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java index a9cde68a1c7..4774cc019df 100644 --- a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java +++ b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java @@ -29,12 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spongepowered.api.CatalogKey; -import org.spongepowered.api.Sponge; -import org.spongepowered.api.asset.Asset; -import org.spongepowered.api.asset.AssetManager; -import org.spongepowered.api.resource.PackInfo; -import org.spongepowered.api.resource.PackRepository; -import org.spongepowered.api.resource.ResourceType; import org.spongepowered.plugin.meta.PluginDependency; import java.nio.file.Path; @@ -128,31 +122,6 @@ default Optional getDependency(String id) { return Optional.empty(); } - /** - * Retrieves the {@link Asset} of the specified name from the - * {@link AssetManager} for this {@link Plugin}. - * - * @param name Name of asset - * @return Asset if present, empty otherwise - * @deprecated Use {@link #getPack(ResourceType)} instead. - */ - @Deprecated - default Optional getAsset(String name) { - return Sponge.getAssetManager().getAsset(this, name); - } - - /** - * Retrieves the {@link PackInfo} owned by this plugin from the - * {@link PackRepository}. - * - * @param type The type of resource pack - * - * @return The plugin's resource pack. - */ - default PackInfo getPack(ResourceType type) { - return Sponge.getServer().getPackRepository().getPack(this); - } - /** * Returns the source the plugin was loaded from. * diff --git a/src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java b/src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java new file mode 100644 index 00000000000..3a8173f4a58 --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java @@ -0,0 +1,14 @@ +package org.spongepowered.api.resource; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.concurrent.CompletableFuture; + +public interface AsyncReloadListener { + + CompletableFuture listen(Staging stg, AsyncReloader reloader); + + interface Staging { + <@Nullable T> CompletableFuture setup(T instance); + } +} diff --git a/src/main/java/org/spongepowered/api/resource/AsyncReloader.java b/src/main/java/org/spongepowered/api/resource/AsyncReloader.java new file mode 100644 index 00000000000..46b8199cb1d --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/AsyncReloader.java @@ -0,0 +1,34 @@ +package org.spongepowered.api.resource; + +import org.spongepowered.api.Sponge; +import org.spongepowered.api.resource.pack.Pack; +import org.spongepowered.api.util.ResettableBuilder; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; + +public interface AsyncReloader { + + static Builder builder() { + return Sponge.getRegistry().createBuilder(Builder.class); + } + + ResourceManager getResourceManager(); + + Executor getWorker(); + + Executor getEngine(); + + interface Builder extends ResettableBuilder { + + Builder manager(ReloadableResourceManager manager); + + Builder engine(Executor engine); + + Builder packs(Pack... packs); + + Builder action(CompletableFuture action); + + AsyncReloader build(); + } +} diff --git a/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java b/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java index 4f52d6465fb..d2f811ea1e7 100644 --- a/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java +++ b/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java @@ -26,9 +26,10 @@ public interface ReloadableResourceManager extends ResourceManager { - /** - * Reloads the resources from active packs. - */ - void reload(); + default AsyncReloader.Builder reload() { + return AsyncReloader.builder().manager(this); + } + + void addListener(AsyncReloadListener listener); } diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index 56786cd8251..86d5c291644 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -96,7 +96,7 @@ public interface Resource extends Closeable { * * @return The parent pack. */ - String getPack(); + String getPackName(); /** * Gets a reader for this resource using the given {@link Charset}. diff --git a/src/main/java/org/spongepowered/api/resource/ResourceManager.java b/src/main/java/org/spongepowered/api/resource/ResourceManager.java index df4b332f887..c3d25efd65c 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceManager.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceManager.java @@ -24,6 +24,10 @@ */ package org.spongepowered.api.resource; +import org.spongepowered.api.resource.pack.Pack; +import org.spongepowered.api.resource.pack.PackRepository; + +import java.io.FileNotFoundException; import java.io.IOException; import java.util.Collection; import java.util.List; @@ -48,17 +52,26 @@ public interface ResourceManager { * Gets a loaded resource at the given path, or {@link Optional#empty()} * if it does not exist. * + *

In the pack, the path will point to a resource. The resource should + * be located roughly at {@code data/namespace/path}

+ * * @param path The path to the resource * @return The resource + * @throws IOException If there was an error reading the resource + * @throws FileNotFoundException If the resource does not exist. */ - Resource getResource(ResourcePath path) throws IOException; + Resource getResource(ResourcePath path) throws IOException, FileNotFoundException; /** - * Returns all of the resources which exist in each {@link Pack}. + * Returns all of the resources which exist in each {@link Pack}. Remember + * to close all of the resources. * * @return All of the resources + * @see #getResource(ResourcePath) + * @throws IOException If there was an error reading any of the resources + * @throws FileNotFoundException If no resources exist */ - List getResources(ResourcePath path); + List getResources(ResourcePath path) throws IOException, FileNotFoundException; /** * Returns all {@link ResourcePath paths} within a directory from all the @@ -76,7 +89,7 @@ public interface ResourceManager { * @param filter The file name filter * @return A collection of resource paths */ - Collection getResources(String path, Predicate filter); + Collection getPaths(String path, Predicate filter); /** * Returns true if the given {@link ResourcePath} exists in the active diff --git a/src/main/java/org/spongepowered/api/resource/ResourcePath.java b/src/main/java/org/spongepowered/api/resource/ResourcePath.java index 0c9a113f181..2b99759f262 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourcePath.java +++ b/src/main/java/org/spongepowered/api/resource/ResourcePath.java @@ -25,6 +25,8 @@ package org.spongepowered.api.resource; import org.spongepowered.api.Sponge; +import org.spongepowered.api.data.DataContainer; +import org.spongepowered.api.data.DataSerializable; import org.spongepowered.api.util.ResettableBuilder; import java.util.Iterator; @@ -33,17 +35,19 @@ import java.util.Optional; /** - *

A resource path should contain a namespace. If one is not provided, - * {@code minecraft} will be used instead. The namespace and path must not - * contain any special characters or uppercase letters. + *

A resource path should contain a namespace. The namespace and path must + * not contain any special characters or uppercase letters. * - *

In the pack, the path will point to a resource. The resource should - * be located roughly at {@code data/namespace/path}

+ *

A namespace can only contain the following characters: + * "{@code a-z0-9_-}"

+ *

A path can only contain the following characters: + * "{@code a-z0-9_/.-}". Additionally, a path cannot use {@code ..} to go to + * the parent directory. * - *

A resource path should be usable in a {@link Map}, so implementations + * @implNote A resource path should be usable in a {@link Map}, so implementations * should override {@link #hashCode()} and {@link #equals(Object)}.

*/ -public interface ResourcePath extends Comparable, Iterable { +public interface ResourcePath extends Comparable { /** * Creates a new {@link Builder} to build a {@link ResourcePath}. @@ -58,7 +62,7 @@ static Builder builder() { * Creates a new {@link ResourcePath} using the given namespace and path. * * @param namespace The namespace to use - * @param path The path to use + * @param path The path to use * @return A new ResourcePath */ static ResourcePath of(String namespace, String path) throws IllegalArgumentException { @@ -95,38 +99,21 @@ static ResourcePath parse(String path) throws IllegalArgumentException { */ String getPath(); - /** - * Gets the root path from this path. - * - * @return The root path - */ - ResourcePath getRoot(); - /** * Gets the immediate parent for this {@link ResourcePath}. If this path is * the root path, itself is returned. * * @return The parent path - * @see #getRoot() */ ResourcePath getParent(); - /** - * Gets a list of names which make up this {@link ResourcePath}. Order of - * names will be root first, with the last name being this path's - * filename. - * - * @return The list of paths - */ - List getNames(); - /** * Tests if this {@link ResourcePath} is a direct child of the given other path. * * @param parent The other resource path to check against * @return True if this is a child to other */ - boolean isParent(ResourcePath parent); + boolean startsWith(ResourcePath parent); /** * Gets the file name portion of this {@link ResourcePath}. @@ -158,7 +145,7 @@ static ResourcePath parse(String path) throws IllegalArgumentException { Optional getExtension(); /** - * Tests if this path has one of the given extensions. Case is insensitive. + * Tests if this path has one of the given extensions. * * @param ext The extensions to test * @return True if the extension matches one of the ones provided @@ -184,13 +171,28 @@ static ResourcePath parse(String path) throws IllegalArgumentException { ResourcePath resolveSibling(String name); /** - * Returns an iterator of the paths leading up to this path, starting with - * the root. + * Provides a String representation of this object consisting of + * {@code namespace:path}. This is easily reversible via + * {@link #parse(String)}. * - * @return The iterator + * @return A string reprsentation */ @Override - Iterator iterator(); + String toString(); + + /** + * {@inheritDoc} + * + * @implNote Implementation should override this for Map usage. + */ + boolean equals(Object other); + + /** + * {@inheritDoc} + * + * @implNote Implementation should override this for Map usage. + */ + int hashCode(); /** * Represents a builder to create {@link ResourcePath} instances. diff --git a/src/main/java/org/spongepowered/api/resource/Pack.java b/src/main/java/org/spongepowered/api/resource/pack/Pack.java similarity index 85% rename from src/main/java/org/spongepowered/api/resource/Pack.java rename to src/main/java/org/spongepowered/api/resource/pack/Pack.java index a237fbac9c3..52a692de71c 100644 --- a/src/main/java/org/spongepowered/api/resource/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/pack/Pack.java @@ -22,9 +22,12 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.api.resource; +package org.spongepowered.api.resource.pack; import org.spongepowered.api.data.DataView; +import org.spongepowered.api.resource.Resource; +import org.spongepowered.api.resource.ResourceManager; +import org.spongepowered.api.resource.ResourcePath; import java.io.IOException; import java.io.InputStream; @@ -46,7 +49,7 @@ public interface Pack { * @return The input stream * @throws IOException If the resource does not exist or another IOException occurs. */ - InputStream openStream(ResourceType type, ResourcePath path) throws IOException; + InputStream openStream(PackType type, ResourcePath path) throws IOException; /** * Recursively gets all the resources loaded from this pack. All namespaces @@ -56,9 +59,9 @@ public interface Pack { * @param path The resource path * @param filter The file name filter * @return Collection of resources - * @see ResourceManager#getResources(String, Predicate) + * @see ResourceManager#getPaths(String, Predicate) */ - Collection getResources(ResourceType type, String path, Predicate filter); + Collection getPaths(PackType type, String path, Predicate filter); /** * Checks if a resource exists in this pack. @@ -67,7 +70,7 @@ public interface Pack { * @param path THe path of the resource * @return True if the resource exists, false otherwise */ - boolean exists(ResourceType type, ResourcePath path); + boolean exists(PackType type, ResourcePath path); /** * Gets the metadata of this pack. The {@link DataView} represented is of diff --git a/src/main/java/org/spongepowered/api/resource/PackInfo.java b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java similarity index 85% rename from src/main/java/org/spongepowered/api/resource/PackInfo.java rename to src/main/java/org/spongepowered/api/resource/pack/PackInfo.java index 24630b14c04..3a2c2fb83a2 100644 --- a/src/main/java/org/spongepowered/api/resource/PackInfo.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java @@ -22,14 +22,16 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.api.resource; +package org.spongepowered.api.resource.pack; +import org.spongepowered.api.data.DataSerializable; import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.TextRepresentable; /** * Basic info about a {@link Pack} */ -public interface PackInfo { +public interface PackInfo extends DataSerializable, TextRepresentable { /** * Gets the name of the {@link Pack} used to identify it in the @@ -54,14 +56,6 @@ public interface PackInfo { */ Text getDescription(); - /** - * Formats the text of the pack info to display to the user. - * - * @param active Whether this pack is active or not - * @return The formatted text - */ - Text formatName(boolean active); - /** * Gets the {@link Pack} associated with this pack info. * @@ -86,4 +80,22 @@ public interface PackInfo { */ boolean isRemote(); + boolean isLocked(); + + Priority getPriority(); + + Compatibility getCompatability(); + + enum Priority { + FIRST, + LAST + } + + enum Compatibility { + OLD, + NEW, + OK + } + + } diff --git a/src/main/java/org/spongepowered/api/resource/PackRepository.java b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java similarity index 94% rename from src/main/java/org/spongepowered/api/resource/PackRepository.java rename to src/main/java/org/spongepowered/api/resource/pack/PackRepository.java index 5ec057df787..a4f0e381e93 100644 --- a/src/main/java/org/spongepowered/api/resource/PackRepository.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java @@ -22,9 +22,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.api.resource; +package org.spongepowered.api.resource.pack; import org.spongepowered.api.plugin.PluginContainer; +import org.spongepowered.api.resource.ReloadableResourceManager; +import org.spongepowered.api.resource.ResourceManager; import java.util.Collection; import java.util.Optional; diff --git a/src/main/java/org/spongepowered/api/resource/ResourceType.java b/src/main/java/org/spongepowered/api/resource/pack/PackType.java similarity index 92% rename from src/main/java/org/spongepowered/api/resource/ResourceType.java rename to src/main/java/org/spongepowered/api/resource/pack/PackType.java index a3a1e2528dd..29507d5c2c5 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceType.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackType.java @@ -22,7 +22,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.api.resource; +package org.spongepowered.api.resource.pack; import org.spongepowered.api.CatalogType; import org.spongepowered.api.util.annotation.CatalogedBy; @@ -31,8 +31,8 @@ * Represents the root directory for different resource types. Current * values in vanilla is assets and data. */ -@CatalogedBy(ResourceTypes.class) -public interface ResourceType extends CatalogType { +@CatalogedBy(PackTypes.class) +public interface PackType extends CatalogType { /** * Gets the name of the root directory path for this resource type. diff --git a/src/main/java/org/spongepowered/api/resource/ResourceTypes.java b/src/main/java/org/spongepowered/api/resource/pack/PackTypes.java similarity index 83% rename from src/main/java/org/spongepowered/api/resource/ResourceTypes.java rename to src/main/java/org/spongepowered/api/resource/pack/PackTypes.java index a99a7b30963..37f3e4b7316 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceTypes.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackTypes.java @@ -22,17 +22,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.api.resource; +package org.spongepowered.api.resource.pack; import org.spongepowered.api.util.generator.dummy.DummyObjectProvider; -public class ResourceTypes { +public class PackTypes { // SORTFIELDS:ON - public static final ResourceType ASSETS = DummyObjectProvider.createFor(ResourceType.class, "ASSETS"); + public static final PackType ASSETS = DummyObjectProvider.createFor(PackType.class, "ASSETS"); - public static final ResourceType DATA = DummyObjectProvider.createFor(ResourceType.class, "DATA"); + public static final PackType DATA = DummyObjectProvider.createFor(PackType.class, "DATA"); // SORTFIELDS:OFF } diff --git a/src/main/java/org/spongepowered/api/event/resource/package-info.java b/src/main/java/org/spongepowered/api/resource/pack/package-info.java similarity index 97% rename from src/main/java/org/spongepowered/api/event/resource/package-info.java rename to src/main/java/org/spongepowered/api/resource/pack/package-info.java index 88e5eabc8a6..9d7e7ee417a 100644 --- a/src/main/java/org/spongepowered/api/event/resource/package-info.java +++ b/src/main/java/org/spongepowered/api/resource/pack/package-info.java @@ -22,4 +22,4 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -@org.spongepowered.api.util.annotation.NonnullByDefault package org.spongepowered.api.event.resource; +@org.spongepowered.api.util.annotation.NonnullByDefault package org.spongepowered.api.resource.pack; From 30fe7de548f411a3d47a4825f3994fbc9957712a Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Thu, 2 May 2019 23:52:51 -0400 Subject: [PATCH 13/22] Fix the build --- build.gradle | 2 +- .../org/spongepowered/api/resource/Resource.java | 2 +- .../api/resource/SimpleReloadListener.java | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java diff --git a/build.gradle b/build.gradle index aee52d8d07d..eb31c4cc0cd 100644 --- a/build.gradle +++ b/build.gradle @@ -175,7 +175,7 @@ sortClassFields { add 'main', 'org.spongepowered.api.item.inventory.equipment.EquipmentTypes' add 'main', 'org.spongepowered.api.item.inventory.query.QueryOperationTypes' add 'main', 'org.spongepowered.api.item.potion.PotionTypes' - add 'main', 'org.spongepowered.api.resource.ResourceTypes' + add 'main', 'org.spongepowered.api.resource.pack.PackTypes' add 'main', 'org.spongepowered.api.scoreboard.CollisionRules' add 'main', 'org.spongepowered.api.scoreboard.Visibilities' add 'main', 'org.spongepowered.api.scoreboard.criteria.Criteria' diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index 86d5c291644..dd3707e5969 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -55,7 +55,7 @@ * try (Resource res = resourceManager.getResource(path)) { * logger.info(res.readString(DefaultCharsets.UTF_8)); * } catch (IOException e) { - * e.printStackTrace(); + * logger.warn("Failed to load resource: {}", path, e); * } * */ diff --git a/src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java b/src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java new file mode 100644 index 00000000000..ba18c4ee61c --- /dev/null +++ b/src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java @@ -0,0 +1,13 @@ +package org.spongepowered.api.resource; + + +import java.util.concurrent.CompletableFuture; + +public interface SimpleReloadListener extends AsyncReloadListener { + @Override + default CompletableFuture listen(Staging stg, AsyncReloader reloader) { + return stg.setup(null).thenRunAsync(() -> onReload(reloader.getResourceManager()), reloader.getEngine()); + } + + void onReload(ResourceManager manager); +} From 1052aabcd5f7e710fdef1bca1de03e229a0aaad1 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sun, 5 May 2019 17:42:45 -0400 Subject: [PATCH 14/22] Fix some rebase artifacts --- src/main/java/org/spongepowered/api/Server.java | 2 -- .../api/plugin/PluginContainer.java | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/spongepowered/api/Server.java b/src/main/java/org/spongepowered/api/Server.java index f0e6ffaaf2d..ca3dd73d384 100644 --- a/src/main/java/org/spongepowered/api/Server.java +++ b/src/main/java/org/spongepowered/api/Server.java @@ -27,8 +27,6 @@ import org.spongepowered.api.command.source.CommandSource; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.profile.GameProfileManager; -import org.spongepowered.api.resource.pack.PackRepository; -import org.spongepowered.api.resource.ReloadableResourceManager; import org.spongepowered.api.resourcepack.ResourcePack; import org.spongepowered.api.scoreboard.Scoreboard; import org.spongepowered.api.text.Text; diff --git a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java index 4774cc019df..775d211f964 100644 --- a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java +++ b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java @@ -29,6 +29,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spongepowered.api.CatalogKey; +import org.spongepowered.api.Sponge; +import org.spongepowered.api.asset.Asset; +import org.spongepowered.api.asset.AssetManager; import org.spongepowered.plugin.meta.PluginDependency; import java.nio.file.Path; @@ -122,11 +125,22 @@ default Optional getDependency(String id) { return Optional.empty(); } + /** + * Retrieves the {@link Asset} of the specified name from the + * {@link AssetManager} for this {@link Plugin}. + * + * @param name Name of asset + * @return Asset if present, empty otherwise + */ + default Optional getAsset(String name) { + return Sponge.getAssetManager().getAsset(this, name); + } + /** * Returns the source the plugin was loaded from. * * @return The source the plugin was loaded from or {@link Optional#empty()} - * if unknown + * if unknown */ default Optional getSource() { return Optional.empty(); From 84cf0d9aec19651b8fd2e50b48bf681dd15b75e1 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sat, 11 May 2019 21:11:12 -0400 Subject: [PATCH 15/22] AsyncReloader had too much impl. --- .../java/org/spongepowered/api/Engine.java | 14 ++++++-- .../resource/ResourceEvent.java} | 32 +++++++++++++---- .../api/resource/AsyncReloadListener.java | 14 -------- .../api/resource/AsyncReloader.java | 34 ------------------- .../api/resource/ResourceManager.java | 2 +- .../api/resource/ResourcePath.java | 9 +++-- .../api/resource/SimpleReloadListener.java | 13 ------- .../spongepowered/api/resource/pack/Pack.java | 19 +++++++---- .../api/resource/pack/PackInfo.java | 34 +++++-------------- .../api/resource/pack/PackRepository.java | 30 ++++++++-------- 10 files changed, 77 insertions(+), 124 deletions(-) rename src/main/java/org/spongepowered/api/{resource/ReloadableResourceManager.java => event/resource/ResourceEvent.java} (63%) delete mode 100644 src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java delete mode 100644 src/main/java/org/spongepowered/api/resource/AsyncReloader.java delete mode 100644 src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java diff --git a/src/main/java/org/spongepowered/api/Engine.java b/src/main/java/org/spongepowered/api/Engine.java index b6f808732d5..8bb77138369 100644 --- a/src/main/java/org/spongepowered/api/Engine.java +++ b/src/main/java/org/spongepowered/api/Engine.java @@ -29,12 +29,12 @@ import org.spongepowered.api.resource.pack.PackRepository; import org.spongepowered.api.scheduler.Scheduler; -import java.util.concurrent.Executor; +import java.util.concurrent.CompletableFuture; /** * Shared functionality between {@link Client} and {@link Server} engines. */ -public interface Engine extends Executor { +public interface Engine { /** * Gets the {@link Scheduler} used to schedule sync tasks on this {@link Engine}. @@ -46,7 +46,7 @@ public interface Engine extends Executor { /** * Gets the {@link ResourceManager} for the server instance. As of * Minecraft 1.13 there is only one instance of the resource manager per - * server instance. It is not per-world. + * server/client instance. It is not per-world. * * @return The resource manager */ @@ -58,6 +58,14 @@ public interface Engine extends Executor { */ PackRepository getPackRepository(); + /** + * Reloads resources from the {@link ResourceManager} using active packs + * from the {@link PackRepository}. + * + * @return The future when it will finish. + */ + CompletableFuture reloadResources(); + /** * Checks if the {@link Thread#currentThread() current thread} is the main thread of the engine. * diff --git a/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java b/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java similarity index 63% rename from src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java rename to src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java index d2f811ea1e7..21152c2b84d 100644 --- a/src/main/java/org/spongepowered/api/resource/ReloadableResourceManager.java +++ b/src/main/java/org/spongepowered/api/event/resource/ResourceEvent.java @@ -22,14 +22,34 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -package org.spongepowered.api.resource; +package org.spongepowered.api.event.resource; -public interface ReloadableResourceManager extends ResourceManager { +import org.spongepowered.api.Engine; +import org.spongepowered.api.event.Event; +import org.spongepowered.api.resource.ResourceManager; - default AsyncReloader.Builder reload() { - return AsyncReloader.builder().manager(this); - } +/** + * Base class for events related to resources. + */ +public interface ResourceEvent extends Event { + + /** + * Gets the engine of the resources + * @return The engine + */ + Engine getEngine(); - void addListener(AsyncReloadListener listener); + /** + * Gets the {@link ResourceManager}. + * @return The resource manager + */ + ResourceManager getResourceManager(); + /** + * Fired after a {@link ResourceManager} has been reloaded. This can be + * used to reload some systems that are resource based. + */ + interface Reload extends ResourceEvent { + + } } diff --git a/src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java b/src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java deleted file mode 100644 index 3a8173f4a58..00000000000 --- a/src/main/java/org/spongepowered/api/resource/AsyncReloadListener.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.spongepowered.api.resource; - -import org.checkerframework.checker.nullness.qual.Nullable; - -import java.util.concurrent.CompletableFuture; - -public interface AsyncReloadListener { - - CompletableFuture listen(Staging stg, AsyncReloader reloader); - - interface Staging { - <@Nullable T> CompletableFuture setup(T instance); - } -} diff --git a/src/main/java/org/spongepowered/api/resource/AsyncReloader.java b/src/main/java/org/spongepowered/api/resource/AsyncReloader.java deleted file mode 100644 index 46b8199cb1d..00000000000 --- a/src/main/java/org/spongepowered/api/resource/AsyncReloader.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.spongepowered.api.resource; - -import org.spongepowered.api.Sponge; -import org.spongepowered.api.resource.pack.Pack; -import org.spongepowered.api.util.ResettableBuilder; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; - -public interface AsyncReloader { - - static Builder builder() { - return Sponge.getRegistry().createBuilder(Builder.class); - } - - ResourceManager getResourceManager(); - - Executor getWorker(); - - Executor getEngine(); - - interface Builder extends ResettableBuilder { - - Builder manager(ReloadableResourceManager manager); - - Builder engine(Executor engine); - - Builder packs(Pack... packs); - - Builder action(CompletableFuture action); - - AsyncReloader build(); - } -} diff --git a/src/main/java/org/spongepowered/api/resource/ResourceManager.java b/src/main/java/org/spongepowered/api/resource/ResourceManager.java index c3d25efd65c..2a1ab6fef22 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourceManager.java +++ b/src/main/java/org/spongepowered/api/resource/ResourceManager.java @@ -89,7 +89,7 @@ public interface ResourceManager { * @param filter The file name filter * @return A collection of resource paths */ - Collection getPaths(String path, Predicate filter); + Collection getPaths(String path, Predicate filter); /** * Returns true if the given {@link ResourcePath} exists in the active diff --git a/src/main/java/org/spongepowered/api/resource/ResourcePath.java b/src/main/java/org/spongepowered/api/resource/ResourcePath.java index 2b99759f262..d3b01fd6570 100644 --- a/src/main/java/org/spongepowered/api/resource/ResourcePath.java +++ b/src/main/java/org/spongepowered/api/resource/ResourcePath.java @@ -25,12 +25,9 @@ package org.spongepowered.api.resource; import org.spongepowered.api.Sponge; -import org.spongepowered.api.data.DataContainer; -import org.spongepowered.api.data.DataSerializable; +import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.util.ResettableBuilder; -import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.Optional; @@ -185,6 +182,7 @@ static ResourcePath parse(String path) throws IllegalArgumentException { * * @implNote Implementation should override this for Map usage. */ + @Override boolean equals(Object other); /** @@ -192,6 +190,7 @@ static ResourcePath parse(String path) throws IllegalArgumentException { * * @implNote Implementation should override this for Map usage. */ + @Override int hashCode(); /** @@ -215,7 +214,7 @@ interface Builder extends ResettableBuilder { * @param plugin The owning plugin * @return This builder */ - Builder plugin(Object plugin); + Builder namespace(PluginContainer plugin); /** * Sets the path of the {@link ResourcePath}. diff --git a/src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java b/src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java deleted file mode 100644 index ba18c4ee61c..00000000000 --- a/src/main/java/org/spongepowered/api/resource/SimpleReloadListener.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.spongepowered.api.resource; - - -import java.util.concurrent.CompletableFuture; - -public interface SimpleReloadListener extends AsyncReloadListener { - @Override - default CompletableFuture listen(Staging stg, AsyncReloader reloader) { - return stg.setup(null).thenRunAsync(() -> onReload(reloader.getResourceManager()), reloader.getEngine()); - } - - void onReload(ResourceManager manager); -} diff --git a/src/main/java/org/spongepowered/api/resource/pack/Pack.java b/src/main/java/org/spongepowered/api/resource/pack/Pack.java index 52a692de71c..f319614e20f 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/pack/Pack.java @@ -32,7 +32,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Collection; -import java.util.Optional; +import java.util.Set; import java.util.function.Predicate; /** @@ -55,13 +55,13 @@ public interface Pack { * Recursively gets all the resources loaded from this pack. All namespaces * are considered. * - * @param type The resource type - * @param path The resource path + * @param type The resource type + * @param path The resource path * @param filter The file name filter * @return Collection of resources * @see ResourceManager#getPaths(String, Predicate) */ - Collection getPaths(PackType type, String path, Predicate filter); + Collection getPaths(PackType type, String path, int depth, Predicate filter); /** * Checks if a resource exists in this pack. @@ -72,14 +72,21 @@ public interface Pack { */ boolean exists(PackType type, ResourcePath path); + /** + * Gets all the namespaces which exist in this pack. + * + * @return The set of namespaces + */ + Set getNamespaces(); + /** * Gets the metadata of this pack. The {@link DataView} represented is of * the pack.json file in the pack root. If the pack does not contain a - * pack.json, {@link Optional#empty()} is returned. + * pack.json, it is an error. * * @return The metadata if it exists */ - Optional getMetadata(); + DataView getMetadata(); /** * Gets the name of this pack which is displayed to the user. diff --git a/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java index 3a2c2fb83a2..49e209e6e25 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java @@ -42,7 +42,8 @@ public interface PackInfo extends DataSerializable, TextRepresentable { String getName(); /** - * Gets the display name of the {@link Pack} which is displayed to the user. + * Gets the display name of the {@link Pack} which is displayed to the + * user. This is loaded from the pack.mcmeta * * @return The display name */ @@ -50,18 +51,18 @@ public interface PackInfo extends DataSerializable, TextRepresentable { /** * Gets the description of the {@link Pack} which is displayed as the hover - * text to the user. + * text to the user. This is loaded from the pack.mcmeta * * @return The description */ Text getDescription(); /** - * Gets the {@link Pack} associated with this pack info. + * Creates a new instance of {@link Pack} associated with this pack info. * - * @return + * @return The new Pack */ - Pack getPack(); + Pack createPack(); /** * Gets whether or not this pack is required to be active. If it is @@ -72,30 +73,11 @@ public interface PackInfo extends DataSerializable, TextRepresentable { */ boolean isRequired(); - /** - * Gets whether or not this pack was loaded from a remote location. For - * example, if this is a server-defined client resource pack. - * - * @return Whether this pack is remote - */ - boolean isRemote(); - - boolean isLocked(); - - Priority getPriority(); + InsertionPosition getPosition(); - Compatibility getCompatability(); - - enum Priority { + enum InsertionPosition { FIRST, LAST } - enum Compatibility { - OLD, - NEW, - OK - } - - } diff --git a/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java index a4f0e381e93..7ef66d3338e 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java @@ -24,23 +24,28 @@ */ package org.spongepowered.api.resource.pack; +import org.spongepowered.api.Engine; import org.spongepowered.api.plugin.PluginContainer; -import org.spongepowered.api.resource.ReloadableResourceManager; -import org.spongepowered.api.resource.ResourceManager; import java.util.Collection; import java.util.Optional; public interface PackRepository { + /** + * Refreshes all the providers in the repository. + */ + void refresh(); + /** * Sets the active packs to be used when reloading resources. Calls to this - * should be followed by {@link ReloadableResourceManager#reload()}. + * should be followed by {@link Engine#reloadResources()} in order to take + * effect. * * @param packs The active packs - * @see ReloadableResourceManager#reload() + * @see Engine#reloadResources() */ - void setActivePacks(Collection packs); + void setEnabledPacks(Collection packs); /** * Returns a collection of all available packs. An available pack could be @@ -50,14 +55,7 @@ public interface PackRepository { */ Collection getAllPacks(); - /** - * Returns a collection of the disabled packs. Disabled packs are present, - * but not used in the {@link ResourceManager}. Resources can still be - * accessed using {@link PackInfo#getPack()}. - * - * @return The disabled packs - */ - Collection getDisabledPacks(); + Collection getAvailablePacks(); /** * Returns a collection of enabled packs. @@ -67,14 +65,14 @@ public interface PackRepository { Collection getEnabledPacks(); /** - * Gets the {@link Pack} defined from {@link PluginContainer#getSource()}. - * The name of the pack will contain the plugin id. + * Gets the {@link Pack} defined from the plugin contains. The name of the + * pack will contain the plugin id. * * @param plugin The plugin instance or container. * @return The pack * @throws IllegalArgumentException if the object is not a plugin */ - PackInfo getPack(Object plugin); + PackInfo getPack(PluginContainer plugin); /** * Gets the pack by its name. From 309d66ec73603cff4d8e166941937399eb3ad575 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sat, 11 May 2019 21:25:09 -0400 Subject: [PATCH 16/22] Pack should be closeable. --- src/main/java/org/spongepowered/api/resource/pack/Pack.java | 3 ++- .../org/spongepowered/api/resource/pack/PackRepository.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/pack/Pack.java b/src/main/java/org/spongepowered/api/resource/pack/Pack.java index f319614e20f..63ac13bc487 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/pack/Pack.java @@ -29,6 +29,7 @@ import org.spongepowered.api.resource.ResourceManager; import org.spongepowered.api.resource.ResourcePath; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.util.Collection; @@ -39,7 +40,7 @@ * A pack can contain several {@link Resource Resources}. Each pack is * independently loaded with a configured priority. */ -public interface Pack { +public interface Pack extends Closeable { /** * Opens a new {@link InputStream} to the specified resource. diff --git a/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java index 7ef66d3338e..a16d7a31bfd 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java @@ -65,7 +65,7 @@ public interface PackRepository { Collection getEnabledPacks(); /** - * Gets the {@link Pack} defined from the plugin contains. The name of the + * Gets the {@link Pack} defined from the plugin container. The name of the * pack will contain the plugin id. * * @param plugin The plugin instance or container. From d6dc2a03231afec423293b44906c4c47c32f4ba6 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sat, 11 May 2019 21:59:46 -0400 Subject: [PATCH 17/22] Fixes to javadocs and nitpicks --- .../spongepowered/api/resource/pack/Pack.java | 6 +++--- .../api/resource/pack/PackInfo.java | 10 +++++++--- .../api/resource/pack/PackRepository.java | 18 ++++++++++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/pack/Pack.java b/src/main/java/org/spongepowered/api/resource/pack/Pack.java index 63ac13bc487..f94e31b8954 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/pack/Pack.java @@ -62,7 +62,7 @@ public interface Pack extends Closeable { * @return Collection of resources * @see ResourceManager#getPaths(String, Predicate) */ - Collection getPaths(PackType type, String path, int depth, Predicate filter); + Collection getPaths(PackType type, String path, int depth, Predicate filter); /** * Checks if a resource exists in this pack. @@ -82,8 +82,8 @@ public interface Pack extends Closeable { /** * Gets the metadata of this pack. The {@link DataView} represented is of - * the pack.json file in the pack root. If the pack does not contain a - * pack.json, it is an error. + * the pack.mcmeta file in the pack root. If the pack does not contain a + * pack.mcmeta, it is an error. * * @return The metadata if it exists */ diff --git a/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java index 49e209e6e25..07be2b0f896 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java @@ -24,14 +24,13 @@ */ package org.spongepowered.api.resource.pack; -import org.spongepowered.api.data.DataSerializable; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.TextRepresentable; /** - * Basic info about a {@link Pack} + * Basic info about a {@link Pack}. Is also used to create new instances. */ -public interface PackInfo extends DataSerializable, TextRepresentable { +public interface PackInfo extends TextRepresentable { /** * Gets the name of the {@link Pack} used to identify it in the @@ -73,6 +72,11 @@ public interface PackInfo extends DataSerializable, TextRepresentable { */ boolean isRequired(); + /** + * Gets the insertion position of this pack. Should it be added before or + * after existing packs? + * @return + */ InsertionPosition getPosition(); enum InsertionPosition { diff --git a/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java index a16d7a31bfd..0bec390387b 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackRepository.java @@ -28,8 +28,14 @@ import org.spongepowered.api.plugin.PluginContainer; import java.util.Collection; +import java.util.List; import java.util.Optional; +/** + * Manages the {@link PackInfo packs} which are known to the game. Use this + * class to get specific packs, as well as enable and disable packs from + * loading on the next call to {@link Engine#reloadResources()}. + */ public interface PackRepository { /** @@ -45,7 +51,7 @@ public interface PackRepository { * @param packs The active packs * @see Engine#reloadResources() */ - void setEnabledPacks(Collection packs); + void setEnabledPacks(List packs); /** * Returns a collection of all available packs. An available pack could be @@ -55,6 +61,11 @@ public interface PackRepository { */ Collection getAllPacks(); + /** + * Gets the list of packs which are not enabled. + * + * @return The disabled packs + */ Collection getAvailablePacks(); /** @@ -62,15 +73,14 @@ public interface PackRepository { * * @return The enabled packs */ - Collection getEnabledPacks(); + List getEnabledPacks(); /** * Gets the {@link Pack} defined from the plugin container. The name of the * pack will contain the plugin id. * - * @param plugin The plugin instance or container. + * @param plugin The plugin container. * @return The pack - * @throws IllegalArgumentException if the object is not a plugin */ PackInfo getPack(PluginContainer plugin); From a47186a37dd7c912b5aeabc44c47931d5cf3c044 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Sat, 11 May 2019 23:15:01 -0400 Subject: [PATCH 18/22] Change PackTypes.ASSETS to RESOURCES --- .../org/spongepowered/api/resource/pack/PackTypes.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/pack/PackTypes.java b/src/main/java/org/spongepowered/api/resource/pack/PackTypes.java index 37f3e4b7316..78fe2d0227f 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/PackTypes.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackTypes.java @@ -30,9 +30,15 @@ public class PackTypes { // SORTFIELDS:ON - public static final PackType ASSETS = DummyObjectProvider.createFor(PackType.class, "ASSETS"); - + /** + * Resources from the server. The root directory is {@code /data}. + */ public static final PackType DATA = DummyObjectProvider.createFor(PackType.class, "DATA"); + /** + * Resources from the client. The root directory is {@code /assets}. + */ + public static final PackType RESOURCES = DummyObjectProvider.createFor(PackType.class, "RESOURCES"); + // SORTFIELDS:OFF } From b124bb87a12482339be6fdf24c528ff07e52be62 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 14 May 2019 22:17:35 -0400 Subject: [PATCH 19/22] Remove the assets package. --- src/main/java/org/spongepowered/api/Game.java | 11 - .../java/org/spongepowered/api/Sponge.java | 12 - .../org/spongepowered/api/asset/Asset.java | 218 ------------------ .../org/spongepowered/api/asset/AssetId.java | 51 ---- .../spongepowered/api/asset/AssetManager.java | 69 ------ .../spongepowered/api/asset/package-info.java | 26 --- .../api/plugin/PluginContainer.java | 14 -- 7 files changed, 401 deletions(-) delete mode 100644 src/main/java/org/spongepowered/api/asset/Asset.java delete mode 100644 src/main/java/org/spongepowered/api/asset/AssetId.java delete mode 100644 src/main/java/org/spongepowered/api/asset/AssetManager.java delete mode 100644 src/main/java/org/spongepowered/api/asset/package-info.java diff --git a/src/main/java/org/spongepowered/api/Game.java b/src/main/java/org/spongepowered/api/Game.java index 4579b744e67..fa1f92dd4d0 100644 --- a/src/main/java/org/spongepowered/api/Game.java +++ b/src/main/java/org/spongepowered/api/Game.java @@ -24,7 +24,6 @@ */ package org.spongepowered.api; -import org.spongepowered.api.asset.AssetManager; import org.spongepowered.api.client.Client; import org.spongepowered.api.config.ConfigManager; import org.spongepowered.api.data.DataManager; @@ -163,16 +162,6 @@ default EventManager getEventManager() { return Sponge.getEventManager(); } - /** - * Gets the {@link AssetManager}. - * - * @return The asset manager - */ - @Deprecated - default AssetManager getAssetManager() { - return Sponge.getAssetManager(); - } - /** * Gets the {@link ConfigManager} used to load and manage configuration files * for plugins. diff --git a/src/main/java/org/spongepowered/api/Sponge.java b/src/main/java/org/spongepowered/api/Sponge.java index 2a7562363a9..0a1ed94a972 100644 --- a/src/main/java/org/spongepowered/api/Sponge.java +++ b/src/main/java/org/spongepowered/api/Sponge.java @@ -28,7 +28,6 @@ import com.google.inject.Inject; import org.checkerframework.checker.nullness.qual.Nullable; -import org.spongepowered.api.asset.AssetManager; import org.spongepowered.api.command.manager.CommandManager; import org.spongepowered.api.config.ConfigManager; import org.spongepowered.api.data.DataManager; @@ -58,7 +57,6 @@ public final class Sponge { @Inject private static PropertyRegistry propertyRegistry; @Inject private static PluginManager pluginManager; @Inject private static EventManager eventManager; - @Inject private static AssetManager assetManager; @Inject private static ConfigManager configManager; @Inject private static ServiceManager serviceManager; @Inject private static ChannelRegistrar channelRegistrar; @@ -140,16 +138,6 @@ public static EventManager getEventManager() { return check(eventManager); } - /** - * Gets the {@link AssetManager} instance. - * - * @return The asset manager instance - */ - @Deprecated - public static AssetManager getAssetManager() { - return check(assetManager); - } - /** * Gets the {@link ConfigManager} used to load and manage configuration files * for plugins. diff --git a/src/main/java/org/spongepowered/api/asset/Asset.java b/src/main/java/org/spongepowered/api/asset/Asset.java deleted file mode 100644 index 0c6f52b3a7a..00000000000 --- a/src/main/java/org/spongepowered/api/asset/Asset.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.asset; - -import static com.google.common.base.Preconditions.checkNotNull; - -import com.google.common.io.Resources; -import org.spongepowered.api.plugin.Plugin; -import org.spongepowered.api.plugin.PluginContainer; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; - -/** - * Represents an {@link Asset} within Sponge that belongs to a {@link Plugin}. - */ -@Deprecated -public interface Asset { - - /** - * The default {@link Charset} that is used for reading {@link Asset}s. - */ - Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; - - /** - * Returns the original {@link Plugin} owner of this Asset. - * - * @return Original owner of asset - */ - PluginContainer getOwner(); - - /** - * Returns the {@link URL} to this Asset. - * - * @return URL to asset - */ - URL getUrl(); - - /** - * Copies this Asset to the specified 'output' {@link Path}. - * - * @param output Path to copy to - * @throws IOException If any file exception is thrown - */ - default void copyToFile(Path output) throws IOException { - this.copyToFile(output, false); - } - - /** - * Copies this Asset to the specified 'output' {@link Path}. - * - * @param output Path to copy to - * @param overwrite If the file should be overwritten if it exists - * @throws IOException File exception - */ - default void copyToFile(Path output, boolean overwrite) throws IOException { - this.copyToFile(output, overwrite, true); - } - - /** - * Copies this Asset to the specified 'output' {@link Path}. - * - * @param output Path to copy to - * @param overwrite If the file should be overwritten if it exists - * @param onlyIfAbsent If the file should only be copied if absent - * @throws IOException File exception - */ - default void copyToFile(Path output, boolean overwrite, boolean onlyIfAbsent) throws IOException { - checkNotNull(output, "output"); - if (Files.exists(output)) { - if (overwrite) { - Files.delete(output); - } else if (onlyIfAbsent) { - return; - } - } - try (InputStream in = this.getUrl().openStream()) { - Files.copy(in, output); - } - } - - /** - * Copies this Asset to the specified 'outputDirectory' {@link Path}. - * - * @param outputDirectory The directory to copy to - * @throws IOException If any file exception is thrown - */ - default void copyToDirectory(Path outputDirectory) throws IOException { - this.copyToDirectory(outputDirectory, false); - } - - /** - * Copies this Asset to the specified 'outputDirectory' {@link Path}. - * - * @param outputDirectory The directory to copy to - * @param overwrite If the file should be overwritten if it exists - * @throws IOException File exception - */ - default void copyToDirectory(Path outputDirectory, boolean overwrite) throws IOException { - this.copyToDirectory(outputDirectory, overwrite, true); - } - - /** - * Copies this Asset to the specified 'outputDirectory' {@link Path}. - * - * @param outputDirectory The directory to copy to - * @param overwrite If the file should be overwritten if it exists - * @param onlyIfAbsent If the file should only be copied if absent - * @throws IOException File exception - */ - default void copyToDirectory(Path outputDirectory, boolean overwrite, boolean onlyIfAbsent) throws IOException { - checkNotNull(outputDirectory, "outputDirectory"); - Files.createDirectories(outputDirectory); - this.copyToFile(outputDirectory.resolve(this.getFileName()), overwrite, onlyIfAbsent); - } - - /** - * Returns the the last portion of the Asset URL, e.g. the file name. - * - * @return The file name - */ - default String getFileName() { - String path = getUrl().getPath(); - //We don't need to worry about file system specific file separators as we are dealing with a substring of URL - int end = path.lastIndexOf('/'); - if (end < 0) { - return path; - } - - return path.substring(end + 1); - } - - /** - * Reads this Asset in it's entirety as a {@link String} and returns the - * result. - * - * @return String representation of Asset - * @throws IOException If any file exception is thrown - */ - default String readString() throws IOException { - return readString(DEFAULT_CHARSET); - } - - /** - * Reads this Asset in it's entirety as a {@link String} and returns the - * result. - * - * @param charset The charset to read the asset with - * @return String representation of Asset - * @throws IOException If any file exception is thrown - */ - default String readString(Charset charset) throws IOException { - checkNotNull(charset, "charset"); - return Resources.toString(getUrl(), charset); - } - - /** - * Reads all lines from the asset and returns the result. - * - * @return The lines read from the asset - * @throws IOException If any file exception is thrown - */ - default List readLines() throws IOException { - return readLines(DEFAULT_CHARSET); - } - - /** - * Reads all lines from the asset and returns the result. - * - * @param charset The charset to read the asset with - * @return An immutable list of the lines read from the asset - * @throws IOException If any file exception is thrown - */ - default List readLines(Charset charset) throws IOException { - checkNotNull(charset, "charset"); - return Resources.asCharSource(getUrl(), charset).readLines(); - } - - /** - * Reads this Asset in it's entirety as a byte array and returns the - * result. - * - * @return Byte array representation of Asset - * @throws IOException If any file exception is thrown - */ - default byte[] readBytes() throws IOException { - return Resources.toByteArray(getUrl()); - } - -} diff --git a/src/main/java/org/spongepowered/api/asset/AssetId.java b/src/main/java/org/spongepowered/api/asset/AssetId.java deleted file mode 100644 index 919c936feaf..00000000000 --- a/src/main/java/org/spongepowered/api/asset/AssetId.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.asset; - -import com.google.inject.BindingAnnotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Provides an injection for {@link Asset}s in plugins. - */ -@Deprecated -@BindingAnnotation -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.FIELD, ElementType.PARAMETER}) -public @interface AssetId { - - /** - * The path to the {@link Asset} in the asset folder of the plugin. - * - * @return The path to the asset - * @see AssetManager#getAsset(String) - */ - String value(); - -} diff --git a/src/main/java/org/spongepowered/api/asset/AssetManager.java b/src/main/java/org/spongepowered/api/asset/AssetManager.java deleted file mode 100644 index d0dfb8dc0f3..00000000000 --- a/src/main/java/org/spongepowered/api/asset/AssetManager.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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.spongepowered.api.asset; - -import org.spongepowered.api.plugin.Plugin; -import org.spongepowered.api.plugin.PluginContainer; -import org.spongepowered.api.resource.ResourceManager; - -import java.util.Optional; - -/** - * The AssetManager offers a convenient way to easily retrieve resources from - * Sponge {@link Plugin}s. The asset manager will attempt to find the - * asset of the specified name at: assets/<plugin_id> - * - * @deprecated The asset manager was unable to provide assets which are not on - * the classpath. Additionally, it was limited to providing the URL to an - * asset, which might not have been useful in some situations. - * - *

Use the more powerful {@link ResourceManager} instead. It allows you to - * add resources from local files as well as on-the-fly generation.

- */ -@Deprecated -public interface AssetManager { - - /** - * Returns the {@link Asset} of the specified name for the specified - * {@link Plugin} instance. - * - * @param plugin Plugin instance - * @param name Name of resource to retrieve - * @return Asset if present, empty otherwise - */ - Optional getAsset(PluginContainer plugin, String name); - - /** - * Returns the {@link Asset} of the specified name within the domain of the - * implementation. This method will typically call - * {@link #getAsset(PluginContainer, String)} using a dummy - * {@link PluginContainer} for the SpongeAPI implementation. - * - * @param name Name of resource to retrieve - * @return Asset if present, empty otherwise - */ - Optional getAsset(String name); - -} diff --git a/src/main/java/org/spongepowered/api/asset/package-info.java b/src/main/java/org/spongepowered/api/asset/package-info.java deleted file mode 100644 index bbc2a752e3c..00000000000 --- a/src/main/java/org/spongepowered/api/asset/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * This file is part of SpongeAPI, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * 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. - */ -@org.checkerframework.framework.qual.DefaultQualifier(org.checkerframework.checker.nullness.qual.NonNull.class) -package org.spongepowered.api.asset; diff --git a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java index 775d211f964..58d87487be3 100644 --- a/src/main/java/org/spongepowered/api/plugin/PluginContainer.java +++ b/src/main/java/org/spongepowered/api/plugin/PluginContainer.java @@ -29,9 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spongepowered.api.CatalogKey; -import org.spongepowered.api.Sponge; -import org.spongepowered.api.asset.Asset; -import org.spongepowered.api.asset.AssetManager; import org.spongepowered.plugin.meta.PluginDependency; import java.nio.file.Path; @@ -125,17 +122,6 @@ default Optional getDependency(String id) { return Optional.empty(); } - /** - * Retrieves the {@link Asset} of the specified name from the - * {@link AssetManager} for this {@link Plugin}. - * - * @param name Name of asset - * @return Asset if present, empty otherwise - */ - default Optional getAsset(String name) { - return Sponge.getAssetManager().getAsset(this, name); - } - /** * Returns the source the plugin was loaded from. * From f540f2be84b2f6e11f97faf3930470d7d7a60f7f Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 14 May 2019 22:18:04 -0400 Subject: [PATCH 20/22] Fix some nitpicks --- .../java/org/spongepowered/api/resource/pack/Pack.java | 4 ++-- .../org/spongepowered/api/resource/pack/PackInfo.java | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/pack/Pack.java b/src/main/java/org/spongepowered/api/resource/pack/Pack.java index f94e31b8954..467eaa1819a 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/Pack.java +++ b/src/main/java/org/spongepowered/api/resource/pack/Pack.java @@ -83,9 +83,9 @@ public interface Pack extends Closeable { /** * Gets the metadata of this pack. The {@link DataView} represented is of * the pack.mcmeta file in the pack root. If the pack does not contain a - * pack.mcmeta, it is an error. + * pack.mcmeta, an auto-generated view is returned. * - * @return The metadata if it exists + * @return The metadata */ DataView getMetadata(); diff --git a/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java index 07be2b0f896..8ee80fe336f 100644 --- a/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java +++ b/src/main/java/org/spongepowered/api/resource/pack/PackInfo.java @@ -33,20 +33,20 @@ public interface PackInfo extends TextRepresentable { /** - * Gets the name of the {@link Pack} used to identify it in the + * Gets the id of the {@link Pack} used to identify it in the * {@link PackRepository}. * * @return The pack name */ - String getName(); + String getId(); /** - * Gets the display name of the {@link Pack} which is displayed to the - * user. This is loaded from the pack.mcmeta + * Gets the name of the {@link Pack} which is displayed to the + * user. * * @return The display name */ - Text getDisplayName(); + Text getName(); /** * Gets the description of the {@link Pack} which is displayed as the hover From fcfb76c7931dafee07f64f3e9294c4a7a952e014 Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 18 Jun 2019 01:29:14 -0400 Subject: [PATCH 21/22] Remove WillNotClose from Resource copy. --- src/main/java/org/spongepowered/api/resource/Resource.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index dd3707e5969..e92f24f4856 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -42,7 +42,6 @@ import java.nio.file.Path; import java.util.Optional; import java.util.stream.Stream; -import javax.annotation.WillNotClose; /** * A resource can represent any kind of loaded data. It can be a file on the @@ -171,7 +170,7 @@ default void copyTo(Path path, OpenOption... options) throws IOException { * @param out The output stream to write * @throws IOException if an error occurs */ - default void copyTo(@WillNotClose OutputStream out) throws IOException { + default void copyTo(OutputStream out) throws IOException { ByteStreams.copy(getInputStream(), out); } From b195b1d128281d8f751ebea503036cdac89e23ab Mon Sep 17 00:00:00 2001 From: Matthew Messinger Date: Tue, 18 Jun 2019 01:35:25 -0400 Subject: [PATCH 22/22] Change getInputStream javadoc to represent current code. --- src/main/java/org/spongepowered/api/resource/Resource.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/spongepowered/api/resource/Resource.java b/src/main/java/org/spongepowered/api/resource/Resource.java index e92f24f4856..2cbb2483496 100644 --- a/src/main/java/org/spongepowered/api/resource/Resource.java +++ b/src/main/java/org/spongepowered/api/resource/Resource.java @@ -61,8 +61,7 @@ public interface Resource extends Closeable { /** - * Returns a new {@link InputStream} of this resource. A new input stream - * should be created each time this method is called. + * Returns the {@link InputStream} of this resource. * * @return A new input stream */