Add validation via resources for tag providers (#7271)

BREAKING CHANGE: Moved ExistingFileHelper to common package
Remove various workarounds from forge tag providers
This commit is contained in:
tterrag authored and GitHub committed 2020-09-02 23:18:22 -04:00
1 parent 5dac065994
commit be5885155c
25 files changed
+191 -121

No files matched your search

@@ -0,0 +1,103 @@
/*
* 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 java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import com.google.common.annotations.VisibleForTesting;
import net.minecraft.resources.FilePack;
import net.minecraft.resources.FolderPack;
import net.minecraft.resources.IResource;
import net.minecraft.resources.IResourceManager;
import net.minecraft.resources.IResourcePack;
import net.minecraft.resources.ResourcePackType;
import net.minecraft.resources.SimpleReloadableResourceManager;
import net.minecraft.resources.VanillaPack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
/**
* Enables data providers to check if other data files currently exist. The
* instance provided in the {@link GatherDataEvent} utilizes the standard
* resources (via {@link VanillaPack}), as well as any extra resource packs
* passed in via the {@code --existing} argument.
*/
public class ExistingFileHelper {
private final SimpleReloadableResourceManager clientResources, serverData;
private final boolean enable;
public ExistingFileHelper(Collection<Path> existingPacks, boolean enable) {
this.clientResources = new SimpleReloadableResourceManager(ResourcePackType.CLIENT_RESOURCES);
this.serverData = new SimpleReloadableResourceManager(ResourcePackType.SERVER_DATA);
this.clientResources.addResourcePack(new VanillaPack("minecraft", "realms"));
this.serverData.addResourcePack(new VanillaPack("minecraft"));
for (Path existing : existingPacks) {
File file = existing.toFile();
IResourcePack pack = file.isDirectory() ? new FolderPack(file) : new FilePack(file);
this.clientResources.addResourcePack(pack);
this.serverData.addResourcePack(pack);
};
this.enable = enable;
}
private IResourceManager getManager(ResourcePackType type) {
return type == ResourcePackType.CLIENT_RESOURCES ? clientResources : serverData;
}
private ResourceLocation getLocation(ResourceLocation base, String suffix, String prefix) {
return new ResourceLocation(base.getNamespace(), prefix + "/" + base.getPath() + suffix);
}
/**
* Check if a given resource exists in the known resource packs.
*
* @param loc the base location of the resource, e.g.
* {@code "minecraft:block/stone"}
* @param type the type of resources to check
* @param pathSuffix a string to append after the path, e.g. {@code ".json"}
* @param pathPrefix a string to append before the path, before a slash, e.g.
* {@code "models"}
* @return {@code true} if the resource exists in any pack, {@code false}
* otherwise
*/
public boolean exists(ResourceLocation loc, ResourcePackType type, String pathSuffix, String pathPrefix) {
if (!enable) {
return true;
}
return getManager(type).hasResource(getLocation(loc, pathSuffix, pathPrefix));
}
@VisibleForTesting
public IResource getResource(ResourceLocation loc, ResourcePackType type, String pathSuffix, String pathPrefix) throws IOException {
return getManager(type).getResource(getLocation(loc, pathSuffix, pathPrefix));
}
/**
* @return {@code true} if validation is enabled, {@code false} otherwise
*/
public boolean isEnabled() {
return enable;
}
}
@@ -32,28 +32,20 @@ import net.minecraft.data.BlockTagsProvider;
import static net.minecraftforge.common.Tags.Blocks.*;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class ForgeBlockTagsProvider extends BlockTagsProvider
{
private Set<ResourceLocation> filter = null;
public ForgeBlockTagsProvider(DataGenerator gen)
public ForgeBlockTagsProvider(DataGenerator gen, ExistingFileHelper existingFileHelper)
{
super(gen);
super(gen, "forge", existingFileHelper);
}
@SuppressWarnings("unchecked")
@Override
public void registerTags()
{
super.registerTags();
filter = this.tagToBuilder.entrySet().stream().map(e -> e.getKey()).collect(Collectors.toSet());
func_240522_a_(CHESTS).addTags(CHESTS_ENDER, CHESTS_TRAPPED, CHESTS_WOODEN);
func_240522_a_(CHESTS_ENDER).func_240534_a_(Blocks.ENDER_CHEST);
func_240522_a_(CHESTS_TRAPPED).func_240534_a_(Blocks.TRAPPED_CHEST);
@@ -131,12 +123,6 @@ public class ForgeBlockTagsProvider extends BlockTagsProvider
}
}
@Override
protected Path makePath(ResourceLocation id)
{
return filter != null && filter.contains(id) ? null : super.makePath(id); //We don't want to save vanilla tags.
}
@Override
public String getName()
{
@@ -19,11 +19,8 @@
package net.minecraftforge.common.data;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import net.minecraft.block.Block;
import net.minecraft.data.BlockTagsProvider;
@@ -39,20 +36,15 @@ import net.minecraftforge.registries.ForgeRegistries;
public class ForgeItemTagsProvider extends ItemTagsProvider
{
private Set<ResourceLocation> filter = null;
public ForgeItemTagsProvider(DataGenerator gen, BlockTagsProvider blockTagProvider)
public ForgeItemTagsProvider(DataGenerator gen, BlockTagsProvider blockTagProvider, ExistingFileHelper existingFileHelper)
{
super(gen, blockTagProvider);
super(gen, blockTagProvider, "forge", existingFileHelper);
}
@SuppressWarnings("unchecked")
@Override
public void registerTags()
{
super.registerTags();
filter = this.tagToBuilder.entrySet().stream().map(e -> e.getKey()).collect(Collectors.toSet());
func_240522_a_(Tags.Items.BONES).func_240534_a_(Items.BONE);
func_240522_a_(Tags.Items.BOOKSHELVES).func_240534_a_(Items.BOOKSHELF);
func_240521_a_(Tags.Blocks.CHESTS, Tags.Items.CHESTS);
@@ -203,12 +195,6 @@ public class ForgeItemTagsProvider extends ItemTagsProvider
}
}
@Override
protected Path makePath(ResourceLocation id)
{
return filter != null && filter.contains(id) ? null : super.makePath(id); //We don't want to save vanilla tags.
}
@Override
public String getName()
{