mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 03:49:14 +00:00
Add data generators for sounds.json (#6982)
This commit is contained in:
12 files changed
+1127
-6
No files matched your search
@@ -31,6 +31,8 @@ import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
|
||||
import net.minecraft.data.IDataProvider;
|
||||
import net.minecraft.client.resources.ResourceIndex;
|
||||
import net.minecraft.client.resources.VirtualAssetsPack;
|
||||
import net.minecraft.resources.FilePack;
|
||||
import net.minecraft.resources.FolderPack;
|
||||
import net.minecraft.resources.IResource;
|
||||
@@ -46,6 +48,8 @@ import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
|
||||
import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo;
|
||||
import net.minecraftforge.fml.packs.ModFileResourcePack;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Enables data providers to check if other data files currently exist. The
|
||||
* instance provided in the {@link GatherDataEvent} utilizes the standard
|
||||
@@ -105,11 +109,22 @@ public class ExistingFileHelper {
|
||||
* @param existingMods
|
||||
* @param enable
|
||||
*/
|
||||
@Deprecated // TODO: Remove in 1.17
|
||||
public ExistingFileHelper(Collection<Path> existingPacks, Set<String> existingMods, boolean enable) {
|
||||
this(existingPacks, existingMods, enable, null, null);
|
||||
}
|
||||
|
||||
public ExistingFileHelper(Collection<Path> existingPacks, final Set<String> existingMods, boolean enable, @Nullable final String assetIndex, @Nullable final File assetsDir) {
|
||||
this.clientResources = new SimpleReloadableResourceManager(ResourcePackType.CLIENT_RESOURCES);
|
||||
this.serverData = new SimpleReloadableResourceManager(ResourcePackType.SERVER_DATA);
|
||||
|
||||
this.clientResources.add(new VanillaPack("minecraft", "realms"));
|
||||
if (assetIndex != null && assetsDir != null)
|
||||
{
|
||||
this.clientResources.add(new VirtualAssetsPack(new ResourceIndex(assetsDir, assetIndex)));
|
||||
}
|
||||
this.serverData.add(new VanillaPack("minecraft"));
|
||||
|
||||
for (Path existing : existingPacks) {
|
||||
File file = existing.toFile();
|
||||
IResourcePack pack = file.isDirectory() ? new FolderPack(file) : new FilePack(file);
|
||||
|
||||
@@ -0,0 +1,422 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2020.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.data;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Contains all the data to completely define a sound event.
|
||||
*/
|
||||
public final class SoundDefinition
|
||||
{
|
||||
/**
|
||||
* Identifies a specific sound that has to be played in a sound event, along with
|
||||
* all the necessary parameters.
|
||||
*
|
||||
* <p>If any of the optional parameters (i.e. the ones that aren't required to
|
||||
* obtain an instance of this class) are unset, their default values will be
|
||||
* used instead. The list of defaults is available in the text that follows:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>Volume: 1.0F</li>
|
||||
* <li>Pitch: 1.0F</li>
|
||||
* <li>Weight: 1</li>
|
||||
* <li>Stream: false</li>
|
||||
* <li>Attenuation Distance: 16</li>
|
||||
* <li>Preload: false</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static final class Sound
|
||||
{
|
||||
private static final SoundType DEFAULT_TYPE = SoundType.SOUND;
|
||||
private static final float DEFAULT_VOLUME = 1.0F;
|
||||
private static final float DEFAULT_PITCH = 1.0F;
|
||||
private static final int DEFAULT_WEIGHT = 1;
|
||||
private static final boolean DEFAULT_STREAM = false;
|
||||
private static final int DEFAULT_ATTENUATION_DISTANCE = 16;
|
||||
private static final boolean DEFAULT_PRELOAD = false;
|
||||
|
||||
private final ResourceLocation name;
|
||||
private final SoundType type;
|
||||
|
||||
// Optional parameters, available in specs
|
||||
private float volume = DEFAULT_VOLUME;
|
||||
private float pitch = DEFAULT_PITCH;
|
||||
private int weight = DEFAULT_WEIGHT;
|
||||
private boolean stream = DEFAULT_STREAM;
|
||||
private int attenuationDistance = DEFAULT_ATTENUATION_DISTANCE;
|
||||
private boolean preload = DEFAULT_PRELOAD;
|
||||
|
||||
private Sound(final ResourceLocation name, final SoundType type)
|
||||
{
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sound with the given name and type.
|
||||
*
|
||||
* @param name The name of the sound to create.
|
||||
* @param type The type of sound to create.
|
||||
*/
|
||||
public static Sound sound(final ResourceLocation name, final SoundType type)
|
||||
{
|
||||
return new Sound(name, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the volume of this specific sound.
|
||||
*
|
||||
* <p>The volume of a sound represents how <strong>loud</strong> the sound is when played.</p>
|
||||
*
|
||||
* @param volume The volume to set. It must be higher than 0.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound volume(final double volume)
|
||||
{
|
||||
return this.volume((float) volume);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the volume of this specific sound.
|
||||
*
|
||||
* <p>The volume of a sound represents how <strong>loud</strong> the sound is when played.</p>
|
||||
*
|
||||
* @param volume The volume to set. It must be higher than 0.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound volume(final float volume)
|
||||
{
|
||||
// Wiki specifies that volume cannot be higher than 1.0F, but I don't see any checks in vanilla nor in the
|
||||
// original specs (https://www.reddit.com/r/Minecraft/comments/1ont25/snapshot_13w42a_has_been_released/cctryvp/)
|
||||
// Set at own risk
|
||||
if (volume <= 0.0F) {
|
||||
throw new IllegalArgumentException("Volume must be positive for sound " + this.name + ", but instead got " + volume);
|
||||
}
|
||||
this.volume = volume;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pitch of this specific sound.
|
||||
*
|
||||
* <p>The pitch of a sound represents how <strong>high</strong> or <strong>low</strong> the sound is
|
||||
* when played.</p>
|
||||
*
|
||||
* @param pitch The pitch to set. It must be higher than 0.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound pitch(final double pitch)
|
||||
{
|
||||
return this.pitch((float) pitch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pitch of this specific sound.
|
||||
*
|
||||
* <p>The pitch of a sound represents how <strong>high</strong> or <strong>low</strong> the sound is
|
||||
* when played.</p>
|
||||
*
|
||||
* @param pitch The pitch to set. It must be higher than 0.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound pitch(final float pitch)
|
||||
{
|
||||
if (pitch <= 0.0F)
|
||||
{
|
||||
throw new IllegalArgumentException("Pitch must be positive for sound " + this.name + ", but instead got " + pitch);
|
||||
}
|
||||
this.pitch = pitch;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the weight of this specific sound.
|
||||
*
|
||||
* <p>The weight represents how likely it is for this sound to be played when the respective
|
||||
* event is triggered. This value is ignored when there is only one sound per event.</p>
|
||||
*
|
||||
* @param weight The weight to set. It must be higher than 0.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound weight(final int weight)
|
||||
{
|
||||
if (weight <= 0)
|
||||
{
|
||||
throw new IllegalArgumentException("Weight has to be a positive number in sound " + this.name + ", but instead got " + weight);
|
||||
}
|
||||
this.weight = weight;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets this sound to a streamed sound.
|
||||
*
|
||||
* <p>In this context, streaming refers to reading the file on disk as needed instead of
|
||||
* loading the whole set in memory. This is useful in case of longer sounds, like records
|
||||
* and music (usually more than a minute).</p>
|
||||
*
|
||||
* <p>This is equivalent to a call to {@link #stream(boolean)} with a value of true.</p>
|
||||
*
|
||||
* @return This sound for chaining
|
||||
*/
|
||||
public Sound stream()
|
||||
{
|
||||
return this.stream(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether this sound should be streamed or not.
|
||||
*
|
||||
* <p>In this context, streaming refers to reading the file on disk as needed instead of
|
||||
* loading the whole set in memory. This is useful in case of longer sounds, like records
|
||||
* and music (usually more than a minute).</p>
|
||||
*
|
||||
* @param stream Whether the sound should be streamed or not.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound stream(final boolean stream)
|
||||
{
|
||||
this.stream = stream;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the attenuation distance of the sound.
|
||||
*
|
||||
* <p>This represents how far this sound will be heard, in blocks. While the specs don't
|
||||
* require so, it is suggested to keep this value positive.</p>
|
||||
*
|
||||
* @param attenuationDistance The attenuation distance to set.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound attenuationDistance(final int attenuationDistance)
|
||||
{
|
||||
this.attenuationDistance = attenuationDistance;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks this sound as needing to be preloaded.
|
||||
*
|
||||
* <p>A preloaded sound identifies a sound that is loaded in memory as soon as the resource
|
||||
* pack is loaded, without having to wait for the sound to be ready to stream. It is suggested
|
||||
* to keep this to {@code false}, unless you are using it for a highly recurring sound (e.g.
|
||||
* underwater ambient sounds).</p>
|
||||
*
|
||||
* <p>This is equivalent to a call to {@link #preload(boolean)} with a value of true.</p>
|
||||
*
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound preload()
|
||||
{
|
||||
return this.preload(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether this sound should be preloaded or not.
|
||||
*
|
||||
* <p>A preloaded sound identifies a sound that is loaded in memory as soon as the resource
|
||||
* pack is loaded, without having to wait for the sound to be ready to stream. It is suggested
|
||||
* to keep this to {@code false}, unless you are using it for a highly recurring sound (e.g.
|
||||
* underwater ambient sounds).</p>
|
||||
*
|
||||
* @param preload Whether the sound should be preloaded or not.
|
||||
* @return This sound for chaining.
|
||||
*/
|
||||
public Sound preload(final boolean preload)
|
||||
{
|
||||
this.preload = preload;
|
||||
return this;
|
||||
}
|
||||
|
||||
ResourceLocation name()
|
||||
{
|
||||
return this.name;
|
||||
}
|
||||
|
||||
SoundType type()
|
||||
{
|
||||
return this.type;
|
||||
}
|
||||
|
||||
JsonElement serialize()
|
||||
{
|
||||
if (this.canBeInShortForm())
|
||||
{
|
||||
return new JsonPrimitive(this.stripMcPrefix(this.name));
|
||||
}
|
||||
|
||||
final JsonObject object = new JsonObject();
|
||||
object.addProperty("name", this.stripMcPrefix(this.name));
|
||||
if (this.type != DEFAULT_TYPE) object.addProperty("type", this.type.jsonString);
|
||||
if (this.volume != DEFAULT_VOLUME) object.addProperty("volume", this.volume);
|
||||
if (this.pitch != DEFAULT_PITCH) object.addProperty("pitch", this.pitch);
|
||||
if (this.weight != DEFAULT_WEIGHT) object.addProperty("weight", this.weight);
|
||||
if (this.stream != DEFAULT_STREAM) object.addProperty("stream", this.stream);
|
||||
if (this.preload != DEFAULT_PRELOAD) object.addProperty("preload", this.preload);
|
||||
if (this.attenuationDistance != DEFAULT_ATTENUATION_DISTANCE) object.addProperty("attenuation_distance", this.attenuationDistance);
|
||||
return object;
|
||||
}
|
||||
|
||||
private boolean canBeInShortForm()
|
||||
{
|
||||
return this.type == DEFAULT_TYPE &&
|
||||
this.volume == DEFAULT_VOLUME &&
|
||||
this.pitch == DEFAULT_PITCH &&
|
||||
this.weight == DEFAULT_WEIGHT &&
|
||||
this.stream == DEFAULT_STREAM &&
|
||||
this.attenuationDistance == DEFAULT_ATTENUATION_DISTANCE &&
|
||||
this.preload == DEFAULT_PRELOAD;
|
||||
}
|
||||
|
||||
private String stripMcPrefix(final ResourceLocation name)
|
||||
{
|
||||
return "minecraft".equals(name.getNamespace()) ? name.getPath() : name.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the type of sound that the {@link Sound} object represents.
|
||||
*/
|
||||
public enum SoundType
|
||||
{
|
||||
/**
|
||||
* Identifies a "normal" sound.
|
||||
*
|
||||
* <p>In a "normal" sound, the {@code name} is considered a file name, which
|
||||
* the game attempts to load from the currently loaded resource packs.</p>
|
||||
*/
|
||||
SOUND("sound"),
|
||||
/**
|
||||
* Identifies a "redirect" sound.
|
||||
*
|
||||
* <p>In a "redirect" sound, the {@code name} of the sound is treated as the
|
||||
* name of another {@code SoundEvent}, which will be queried. Processing is
|
||||
* then deferred to that specific sound event, recursively as needed.</p>
|
||||
*/
|
||||
EVENT("event");
|
||||
|
||||
private final String jsonString;
|
||||
|
||||
SoundType(final String jsonString)
|
||||
{
|
||||
this.jsonString = jsonString;
|
||||
}
|
||||
}
|
||||
|
||||
private final List<Sound> sounds = new ArrayList<>();
|
||||
private boolean replace = false;
|
||||
private String subtitle = null;
|
||||
|
||||
private SoundDefinition() {}
|
||||
|
||||
/**
|
||||
* Creates a new {@link SoundDefinition}, which will host a set of
|
||||
* {@link Sound}s and the necessary parameters.
|
||||
*/
|
||||
public static SoundDefinition definition()
|
||||
{
|
||||
return new SoundDefinition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether this definition should replace any other definition for the
|
||||
* same sound event previously applied, rather than overwriting it.
|
||||
*
|
||||
* @param replace Whether this definition replaces or not.
|
||||
* @return This definition for chaining.
|
||||
*/
|
||||
public SoundDefinition replace(final boolean replace)
|
||||
{
|
||||
this.replace = replace;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the language key for the subtitle that will be displayed whenever this
|
||||
* sound is being played.
|
||||
*
|
||||
* <p>The subtitle is optional and the game will skip displaying it if it
|
||||
* isn't present.</p>
|
||||
*
|
||||
* @param subtitle The subtitle to display, or null to disable.
|
||||
* @return This definition for chaining.
|
||||
*/
|
||||
public SoundDefinition subtitle(@Nullable final String subtitle)
|
||||
{
|
||||
this.subtitle = subtitle;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given sound to this sound definition.
|
||||
*
|
||||
* @param sound The sound to add.
|
||||
* @return This definition for chaining.
|
||||
*/
|
||||
public SoundDefinition with(final Sound sound)
|
||||
{
|
||||
this.sounds.add(sound);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given sounds to this sound definition.
|
||||
*
|
||||
* @param sounds The sounds to add.
|
||||
* @return This definition for chaining.
|
||||
*/
|
||||
public SoundDefinition with(final Sound... sounds)
|
||||
{
|
||||
this.sounds.addAll(Arrays.asList(sounds));
|
||||
return this;
|
||||
}
|
||||
|
||||
List<Sound> soundList()
|
||||
{
|
||||
return this.sounds;
|
||||
}
|
||||
|
||||
JsonObject serialize()
|
||||
{
|
||||
if (this.sounds.isEmpty())
|
||||
{
|
||||
throw new IllegalStateException("Unable to serialize a sound definition that has no sounds!");
|
||||
}
|
||||
|
||||
final JsonObject object = new JsonObject();
|
||||
if (this.replace) object.addProperty("replace", true);
|
||||
if (this.subtitle != null) object.addProperty("subtitle", this.subtitle);
|
||||
final JsonArray sounds = new JsonArray();
|
||||
this.sounds.stream().map(Sound::serialize).forEach(sounds::add);
|
||||
object.add("sounds", sounds);
|
||||
return object;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2020.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.data;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.data.DirectoryCache;
|
||||
import net.minecraft.data.IDataProvider;
|
||||
import net.minecraft.resources.ResourcePackType;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Data provider for the {@code sounds.json} file, which identifies sound definitions
|
||||
* for the various sound events in Minecraft.
|
||||
*/
|
||||
public abstract class SoundDefinitionsProvider implements IDataProvider
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
|
||||
private final DataGenerator generator;
|
||||
private final String modId;
|
||||
private final ExistingFileHelper helper;
|
||||
|
||||
private final Map<String, SoundDefinition> sounds = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* Creates a new instance of this data provider.
|
||||
*
|
||||
* @param generator The data generator instance provided by the event you are initializing this provider in.
|
||||
* @param modId The mod ID of the current mod.
|
||||
* @param helper The existing file helper provided by the event you are initializing this provider in.
|
||||
*/
|
||||
protected SoundDefinitionsProvider(final DataGenerator generator, final String modId, final ExistingFileHelper helper)
|
||||
{
|
||||
this.generator = generator;
|
||||
this.modId = modId;
|
||||
this.helper = helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the sound definitions that should be generated via one of the {@code add} methods.
|
||||
*/
|
||||
public abstract void registerSounds();
|
||||
|
||||
@Override
|
||||
public void run(DirectoryCache cache) throws IOException
|
||||
{
|
||||
this.sounds.clear();
|
||||
this.registerSounds();
|
||||
this.validate();
|
||||
if (!this.sounds.isEmpty())
|
||||
{
|
||||
this.save(cache, this.generator.getOutputFolder().resolve("assets/" + this.modId + "/sounds.json"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return "Sound Definitions";
|
||||
}
|
||||
|
||||
// Quick helpers
|
||||
/**
|
||||
* Creates a new {@link SoundDefinition}, which will host a set of
|
||||
* {@link SoundDefinition.Sound}s and the necessary parameters.
|
||||
*/
|
||||
protected static SoundDefinition definition()
|
||||
{
|
||||
return SoundDefinition.definition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sound with the given name and type.
|
||||
*
|
||||
* @param name The name of the sound to create.
|
||||
* @param type The type of sound to create.
|
||||
*/
|
||||
protected static SoundDefinition.Sound sound(final ResourceLocation name, final SoundDefinition.SoundType type)
|
||||
{
|
||||
return SoundDefinition.Sound.sound(name, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sound with the given name and {@link SoundDefinition.SoundType#SOUND} as
|
||||
* sound type.
|
||||
*
|
||||
* @param name The name of the sound to create.
|
||||
*/
|
||||
protected static SoundDefinition.Sound sound(final ResourceLocation name)
|
||||
{
|
||||
return sound(name, SoundDefinition.SoundType.SOUND);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sound with the given name and type.
|
||||
*
|
||||
* @param name The name of the sound to create.
|
||||
* @param type The type of sound to create.
|
||||
*/
|
||||
protected static SoundDefinition.Sound sound(final String name, final SoundDefinition.SoundType type)
|
||||
{
|
||||
return sound(new ResourceLocation(name), type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sound with the given name and {@link SoundDefinition.SoundType#SOUND} as
|
||||
* sound type.
|
||||
*
|
||||
* @param name The name of the sound to create.
|
||||
*/
|
||||
protected static SoundDefinition.Sound sound(final String name)
|
||||
{
|
||||
return sound(new ResourceLocation(name));
|
||||
}
|
||||
|
||||
// Addition methods
|
||||
/**
|
||||
* Adds the entry name associated with the supplied {@link SoundEvent} with the given
|
||||
* {@link SoundDefinition} to the list.
|
||||
*
|
||||
* <p>This method should be preferred when dealing with a {@code RegistryObject} or
|
||||
* {@code RegistryDelegate}.</p>
|
||||
*
|
||||
* @param soundEvent A {@code Supplier} for the given {@link SoundEvent}.
|
||||
* @param definition A {@link SoundDefinition} that defines the given sound.
|
||||
*/
|
||||
protected void add(final Supplier<SoundEvent> soundEvent, final SoundDefinition definition)
|
||||
{
|
||||
this.add(soundEvent.get(), definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the entry name associated with the given {@link SoundEvent} with the
|
||||
* {@link SoundDefinition} to the list.
|
||||
*
|
||||
* <p>This method should be preferred when a {@code SoundEvent} is already
|
||||
* available in the method context. If you already have a {@code Supplier} for
|
||||
* it, refer to {@link #add(Supplier, SoundDefinition)}.</p>
|
||||
*
|
||||
* @param soundEvent A {@link SoundEvent}.
|
||||
* @param definition The {@link SoundDefinition} that defines the given event.
|
||||
*/
|
||||
protected void add(final SoundEvent soundEvent, final SoundDefinition definition)
|
||||
{
|
||||
this.add(soundEvent.getLocation(), definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the {@link SoundEvent} referenced by the given {@link ResourceLocation} with the
|
||||
* {@link SoundDefinition} to the list.
|
||||
*
|
||||
* @param soundEvent The {@link ResourceLocation} that identifies the event.
|
||||
* @param definition The {@link SoundDefinition} that defines the given event.
|
||||
*/
|
||||
protected void add(final ResourceLocation soundEvent, final SoundDefinition definition)
|
||||
{
|
||||
this.addSounds(soundEvent.getPath(), definition);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the {@link SoundEvent} with the specified name along with its {@link SoundDefinition}
|
||||
* to the list.
|
||||
*
|
||||
* <p>The given sound event must NOT contain the namespace the name is a part of, since
|
||||
* the sound definition specification doesn't allow sounds to be defined outside the
|
||||
* namespace they're in. For this reason, any namespace will automatically be stripped
|
||||
* from the name.</p>
|
||||
*
|
||||
* @param soundEvent The name of the {@link SoundEvent}.
|
||||
* @param definition The {@link SoundDefinition} that defines the given event.
|
||||
*/
|
||||
protected void add(final String soundEvent, final SoundDefinition definition)
|
||||
{
|
||||
this.add(new ResourceLocation(soundEvent), definition);
|
||||
}
|
||||
|
||||
private void addSounds(final String soundEvent, final SoundDefinition definition)
|
||||
{
|
||||
if (this.sounds.put(soundEvent, definition) != null)
|
||||
{
|
||||
throw new IllegalStateException("Sound event '" + this.modId + ":" + soundEvent + "' already exists");
|
||||
}
|
||||
}
|
||||
|
||||
// Internal handling stuff
|
||||
private void validate()
|
||||
{
|
||||
final List<String> notValid = this.sounds.entrySet().stream()
|
||||
.filter(it -> !this.validate(it.getKey(), it.getValue()))
|
||||
.map(Map.Entry::getKey)
|
||||
.map(it -> this.modId + ":" + it)
|
||||
.collect(Collectors.toList());
|
||||
if (!notValid.isEmpty())
|
||||
{
|
||||
throw new IllegalStateException("Found invalid sound events: " + notValid);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean validate(final String name, final SoundDefinition def)
|
||||
{
|
||||
return def.soundList().stream().allMatch(it -> this.validate(name,it));
|
||||
}
|
||||
|
||||
private boolean validate(final String name, final SoundDefinition.Sound sound)
|
||||
{
|
||||
switch (sound.type())
|
||||
{
|
||||
case SOUND: return this.validateSound(name, sound.name());
|
||||
case EVENT: return this.validateEvent(name, sound.name());
|
||||
}
|
||||
// Differently from all the other errors, this is not a 'missing sound' but rather something completely different
|
||||
// that has broken the invariants of this sound definitions provider. In fact, a sound may only be either of
|
||||
// SOUND or EVENT type. Any other values is somebody messing with the internals, reflectively adding something
|
||||
// to an enum or passing `null` to a parameter annotated with `@Nonnull`.
|
||||
throw new IllegalArgumentException("The given sound '" + sound.name() + "' does not have a valid type: expected either SOUND or EVENT, but found " + sound.type());
|
||||
}
|
||||
|
||||
private boolean validateSound(final String soundName, final ResourceLocation name)
|
||||
{
|
||||
final boolean valid = this.helper.exists(name, ResourcePackType.CLIENT_RESOURCES, ".ogg", "sounds");
|
||||
if (!valid)
|
||||
{
|
||||
final String path = name.getNamespace() + ":sounds/" + name.getPath() + ".ogg";
|
||||
LOGGER.warn("Unable to find corresponding OGG file '{}' for sound event '{}'", path, soundName);
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
|
||||
private boolean validateEvent(final String soundName, final ResourceLocation name)
|
||||
{
|
||||
final boolean valid = this.sounds.containsKey(soundName) || ForgeRegistries.SOUND_EVENTS.containsKey(name);
|
||||
if (!valid)
|
||||
{
|
||||
LOGGER.warn("Unable to find event '{}' referenced from '{}'", name, soundName);
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
|
||||
private void save(final DirectoryCache cache, final Path targetFile) throws IOException
|
||||
{
|
||||
IDataProvider.save(GSON, cache, this.mapToJson(this.sounds), targetFile);
|
||||
}
|
||||
|
||||
private JsonObject mapToJson(final Map<String, SoundDefinition> map)
|
||||
{
|
||||
final JsonObject obj = new JsonObject();
|
||||
// namespaces are ignored when serializing
|
||||
map.forEach((k, v) -> obj.add(k, v.serialize()));
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2020.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package net.minecraftforge.common.data;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
@@ -26,6 +26,7 @@ import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
@@ -45,7 +46,12 @@ public class DatagenModLoader {
|
||||
begin(mods, path, inputs, existingPacks, Collections.emptySet(), serverGenerators, clientGenerators, devToolGenerators, reportsGenerator, structureValidator, flat);
|
||||
}
|
||||
|
||||
@Deprecated //TODO: Remove in 1.17
|
||||
public static void begin(final Set<String> mods, final Path path, final Collection<Path> inputs, Collection<Path> existingPacks, Set<String> existingMods, final boolean serverGenerators, final boolean clientGenerators, final boolean devToolGenerators, final boolean reportsGenerator, final boolean structureValidator, final boolean flat) {
|
||||
begin(mods, path, inputs, existingPacks, existingMods, serverGenerators, clientGenerators, devToolGenerators, reportsGenerator, structureValidator, flat, null, null);
|
||||
}
|
||||
|
||||
public static void begin(final Set<String> mods, final Path path, final Collection<Path> inputs, Collection<Path> existingPacks, Set<String> existingMods, final boolean serverGenerators, final boolean clientGenerators, final boolean devToolGenerators, final boolean reportsGenerator, final boolean structureValidator, final boolean flat, final String assetIndex, final File assetsDir) {
|
||||
if (mods.contains("minecraft") && mods.size() == 1) return;
|
||||
LOGGER.info("Initializing Data Gatherer for mods {}", mods);
|
||||
runningDataGen = true;
|
||||
@@ -56,7 +62,7 @@ public class DatagenModLoader {
|
||||
//If we aren't generating data for forge, automatically add forge as an existing so mods can access forge's data
|
||||
existingMods.add("forge");
|
||||
}
|
||||
existingFileHelper = new ExistingFileHelper(existingPacks, existingMods, structureValidator);
|
||||
existingFileHelper = new ExistingFileHelper(existingPacks, existingMods, structureValidator, assetIndex, assetsDir);
|
||||
ModLoader.get().runEventGenerator(mc->new GatherDataEvent(mc, dataGeneratorConfig.makeGenerator(p->dataGeneratorConfig.isFlat() ? p : p.resolve(mc.getModId()), dataGeneratorConfig.getMods().contains(mc.getModId())), dataGeneratorConfig, existingFileHelper));
|
||||
dataGeneratorConfig.runAll();
|
||||
}
|
||||
|
||||
@@ -45,7 +45,13 @@ import net.minecraftforge.versions.forge.ForgeVersion;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
Reference in new issue
Block a user