Fix a few minor issues with custom tag types and fix OptionalNamedTags (#7316)

This commit is contained in:
Richard Freimer authored and GitHub committed 2020-09-23 20:12:11 -07:00
1 parent 37be3aff2d
commit af75547d37
18 files changed
+258 -114

No files matched your search

@@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
@@ -121,7 +122,7 @@ public class ForgeTagHandler
* @param <T> Type of the registry
* @return An optional tag
*/
public static <T extends IForgeRegistryEntry<T>> IOptionalNamedTag<T> createOptionalTag(IForgeRegistry<T> registry, ResourceLocation name, @Nullable Supplier<Set<T>> defaults)
public static <T extends IForgeRegistryEntry<T>> IOptionalNamedTag<T> createOptionalTag(IForgeRegistry<T> registry, ResourceLocation name, @Nullable Set<Supplier<T>> defaults)
{
validateRegistrySupportsTags(registry);
if (tagTypesSet)
@@ -180,7 +181,7 @@ public class ForgeTagHandler
* @implNote This method only errors instantly if tag types have already been set, otherwise the error is delayed until after registries finish initializing
* and we can validate if the custom registry really does support custom tags.
*/
public static <T extends IForgeRegistryEntry<T>> IOptionalNamedTag<T> createOptionalTag(ResourceLocation registryName, ResourceLocation name, @Nullable Supplier<Set<T>> defaults)
public static <T extends IForgeRegistryEntry<T>> IOptionalNamedTag<T> createOptionalTag(ResourceLocation registryName, ResourceLocation name, @Nullable Set<Supplier<T>> defaults)
{
if (tagTypesSet)
{
@@ -220,6 +221,10 @@ public class ForgeTagHandler
if (tagTypesSet) throw new RuntimeException("Custom tag types have already been set, this method should only be called by forge, and after registries are initialized");
tagTypesSet = true;
customTagTypeNames = ImmutableSet.copyOf(customTagTypes);
//Add the static references for custom tag types to the proper tag registries
// Note: If this ends up being a hotspot due to lots of mods having lots of statically registered tags
// that get loaded/registered before the new registry event is fired/processed everywhere then this
// potentially should end up being moved into an async processor.
TagRegistry.performDelayedAdd();
}
@@ -248,7 +253,7 @@ public class ForgeTagHandler
*
* @apiNote Internal
*/
public static void resetCachedTagCollections()
public static void resetCachedTagCollections(boolean makeEmpty, boolean withOptional)
{
ImmutableMap.Builder<ResourceLocation, ITagCollection<?>> builder = ImmutableMap.builder();
for (ResourceLocation registryName : customTagTypeNames)
@@ -256,7 +261,17 @@ public class ForgeTagHandler
TagRegistry<?> tagRegistry = TagRegistryManager.get(registryName);
if (tagRegistry != null)
{
builder.put(registryName, ITagCollection.func_242202_a(tagRegistry.func_241288_c_().stream().distinct().collect(Collectors.toMap(INamedTag::func_230234_a_, namedTag -> namedTag))));
if (makeEmpty)
{
if (withOptional)
builder.put(registryName, tagRegistry.reinjectOptionalTags(ITagCollection.func_242202_a(Collections.emptyMap())));
else
builder.put(registryName, ITagCollection.func_242202_a(Collections.emptyMap()));
}
else
{
builder.put(registryName, ITagCollection.func_242202_a(tagRegistry.func_241288_c_().stream().distinct().collect(Collectors.toMap(INamedTag::func_230234_a_, namedTag -> namedTag))));
}
}
}
customTagTypes = builder.build();
@@ -270,7 +285,7 @@ public class ForgeTagHandler
public static ITagCollectionSupplier populateTagCollectionManager(ITagCollection<Block> blockTags, ITagCollection<Item> itemTags, ITagCollection<Fluid> fluidTags, ITagCollection<EntityType<?>> entityTypeTags)
{
//Default the tag collections
resetCachedTagCollections();
resetCachedTagCollections(false, false);
if (!customTagTypes.isEmpty())
{
LOGGER.debug("Populated the TagCollectionManager with {} extra types", customTagTypes.size());
@@ -301,6 +316,7 @@ public class ForgeTagHandler
public static void updateCustomTagTypes(SyncCustomTagTypes packet)
{
customTagTypes = packet.getCustomTagTypes();
reinjectOptionalTagsCustomTypes();
}
/**
@@ -321,6 +337,84 @@ public class ForgeTagHandler
return customResults;
}
/**
* Add all the missing optional tags back into the custom tag types tag collections
*
* @apiNote Internal
*/
public static void reinjectOptionalTagsCustomTypes()
{
ImmutableMap.Builder<ResourceLocation, ITagCollection<?>> builder = ImmutableMap.builder();
for (Entry<ResourceLocation, ITagCollection<?>> entry : customTagTypes.entrySet())
{
ResourceLocation registry = entry.getKey();
TagRegistry<?> tagRegistry = TagRegistryManager.get(registry);
ITagCollection<?> tagCollection = entry.getValue();
builder.put(registry, tagRegistry == null ? tagCollection : tagRegistry.reinjectOptionalTags((ITagCollection) tagCollection));
}
customTagTypes = builder.build();
}
/**
* Gets an {@link ITagCollectionSupplier} with empty custom tag type collections to allow for checking if the client is requiring any tags of custom tag types.
*
* @apiNote Internal: For use with validating missing tags when connecting to a vanilla server
*/
public static ITagCollectionSupplier withNoCustom(ITagCollectionSupplier tagCollectionSupplier)
{
ImmutableMap.Builder<ResourceLocation, ITagCollection<?>> builder = ImmutableMap.builder();
for (ResourceLocation registryName : customTagTypeNames)
{
TagRegistry<?> tagRegistry = TagRegistryManager.get(registryName);
if (tagRegistry != null)
{
builder.put(registryName, ITagCollection.func_242202_a(Collections.emptyMap()));
}
}
return withSpecificCustom(tagCollectionSupplier, builder.build());
}
/**
* Gets an {@link ITagCollectionSupplier} with specific custom tag types for testing if any tags are missing.
*
* @apiNote Internal
*/
public static ITagCollectionSupplier withSpecificCustom(ITagCollectionSupplier tagCollectionSupplier, Map<ResourceLocation, ITagCollection<?>> customTagTypes)
{
return new ITagCollectionSupplier()
{
@Override
public ITagCollection<Block> func_241835_a()
{
return tagCollectionSupplier.func_241835_a();
}
@Override
public ITagCollection<Item> func_241836_b()
{
return tagCollectionSupplier.func_241836_b();
}
@Override
public ITagCollection<Fluid> func_241837_c()
{
return tagCollectionSupplier.func_241837_c();
}
@Override
public ITagCollection<EntityType<?>> func_241838_d()
{
return tagCollectionSupplier.func_241838_d();
}
@Override
public Map<ResourceLocation, ITagCollection<?>> getCustomTagTypes()
{
return customTagTypes;
}
};
}
/**
* Helper storage class for keeping track of various data for all custom tag types in the NetworkTagReader to make the code easier to read.
*
@@ -70,4 +70,10 @@ public abstract class ForgeRegistryTagsProvider<T extends IForgeRegistryEntry<T>
{
return generator.getOutputFolder().resolve("data/" + id.getNamespace() + "/tags/" + folder + "/" + id.getPath() + ".json");
}
@Override
protected String getTagFolder()
{
return folder;
}
}
@@ -195,10 +195,10 @@ public class ModLoader
dispatchAndHandleError(ModLoadingStage.CREATE_REGISTRIES, syncExecutor, parallelExecutor, periodicTask);
ObjectHolderRegistry.findObjectHolders();
CapabilityManager.INSTANCE.injectCapabilities(modList.getAllScanData());
statusConsumer.ifPresent(c->c.accept("Populating registries"));
dispatchAndHandleError(ModLoadingStage.LOAD_REGISTRIES, syncExecutor, parallelExecutor, periodicTask);
statusConsumer.ifPresent(c->c.accept("Adding custom tag types"));
GameData.setCustomTagTypesFromRegistries();
statusConsumer.ifPresent(c->c.accept("Populating registries"));
dispatchAndHandleError(ModLoadingStage.LOAD_REGISTRIES, syncExecutor, parallelExecutor, periodicTask);
statusConsumer.ifPresent(c->c.accept("Early mod loading complete"));
}
@@ -26,7 +26,6 @@ import com.google.common.collect.Multimap;
import io.netty.buffer.Unpooled;
import java.util.List;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.IHasContainer;
import net.minecraft.client.gui.ScreenManager;
@@ -34,10 +33,8 @@ import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.fluid.Fluid;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.item.Item;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tags.ITag;
import net.minecraft.tags.ITagCollection;
@@ -414,39 +411,8 @@ public class FMLPlayMessages
// to the last working set of tags
//Note: We gracefully ignore any tag types the server may have that we don't as they won't be in our tag registry
// so they won't be validated
Multimap<ResourceLocation, ResourceLocation> missingTags = TagRegistryManager.func_242198_b(new ITagCollectionSupplier()
{
@Override
public ITagCollection<Block> func_241835_a()
{
return tagCollectionSupplier.func_241835_a();
}
@Override
public ITagCollection<Item> func_241836_b()
{
return tagCollectionSupplier.func_241836_b();
}
@Override
public ITagCollection<Fluid> func_241837_c()
{
return tagCollectionSupplier.func_241837_c();
}
@Override
public ITagCollection<EntityType<?>> func_241838_d()
{
return tagCollectionSupplier.func_241838_d();
}
@Override
public Map<ResourceLocation, ITagCollection<?>> getCustomTagTypes()
{
//Override and use the tags from the packet to test for validation before we actually set them
return msg.customTagTypeCollections;
}
});
//Override and use the tags from the packet to test for validation before we actually set them
Multimap<ResourceLocation, ResourceLocation> missingTags = TagRegistryManager.func_242198_b(ForgeTagHandler.withSpecificCustom(tagCollectionSupplier, msg.customTagTypeCollections));
if (missingTags.isEmpty())
{
//If we have no missing tags, update the custom tag types
@@ -119,9 +119,14 @@ public class NetworkHooks
FMLNetworkConstants.playChannel.sendTo(new FMLPlayMessages.DimensionInfoMessage(player.dimension), manager, NetworkDirection.PLAY_TO_CLIENT);
}*/
public static void handleClientLoginSuccess(NetworkManager manager) {
public static boolean isVanillaConnection(NetworkManager manager)
{
if (manager == null || manager.channel() == null) throw new NullPointerException("ARGH! Network Manager is null (" + manager != null ? "CHANNEL" : "MANAGER"+")" );
if (getConnectionType(()->manager) == ConnectionType.VANILLA) {
return getConnectionType(() -> manager) == ConnectionType.VANILLA;
}
public static void handleClientLoginSuccess(NetworkManager manager) {
if (isVanillaConnection(manager)) {
LOGGER.info("Connected to a vanilla server. Catching up missing behaviour.");
ConfigTracker.INSTANCE.loadDefaultServerConfigs();
} else {
@@ -179,7 +179,7 @@ public class GameData
makeRegistry(TREE_DECORATOR_TYPES, c(TreeDecoratorType.class)).disableSaving().disableSync().create();
// Dynamic Worldgen
makeRegistry(BIOMES, Biome.class).tagFolder("biomes").create();
makeRegistry(BIOMES, Biome.class).create();
// Custom forge registries
makeRegistry(DATA_SERIALIZERS, DataSerializerEntry.class, 256 /*vanilla space*/, MAX_VARINT).disableSaving().disableOverrides().addCallback(SerializerCallbacks.INSTANCE).create();