mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-20 12:20:11 +00:00
Add support for custom tag types (#7289)
This commit is contained in:
1 parent
43391c009c
commit
45f8dc5245
27 files changed
+1059
-24
No files matched your search
@@ -198,6 +198,8 @@ public class ModLoader
|
||||
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("Early mod loading complete"));
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,14 @@
|
||||
|
||||
package net.minecraftforge.fml.network;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Maps;
|
||||
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;
|
||||
@@ -27,23 +34,36 @@ 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;
|
||||
import net.minecraft.tags.ITagCollectionSupplier;
|
||||
import net.minecraft.tags.TagRegistryManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
import net.minecraft.util.math.vector.Vector3d;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.biome.FuzzedBiomeMagnifier;
|
||||
import net.minecraftforge.common.ForgeTagHandler;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.TagsUpdatedEvent;
|
||||
import net.minecraftforge.fml.LogicalSidedProvider;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
import net.minecraftforge.registries.IForgeRegistry;
|
||||
import net.minecraftforge.registries.IForgeRegistryEntry;
|
||||
import net.minecraftforge.registries.RegistryManager;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class FMLPlayMessages
|
||||
{
|
||||
@@ -308,4 +328,144 @@ public class FMLPlayMessages
|
||||
return additionalData;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SyncCustomTagTypes
|
||||
{
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private final Map<ResourceLocation, ITagCollection<?>> customTagTypeCollections;
|
||||
|
||||
SyncCustomTagTypes(Map<ResourceLocation, ITagCollection<?>> customTagTypeCollections)
|
||||
{
|
||||
this.customTagTypeCollections = customTagTypeCollections;
|
||||
}
|
||||
|
||||
public Map<ResourceLocation, ITagCollection<?>> getCustomTagTypes()
|
||||
{
|
||||
return customTagTypeCollections;
|
||||
}
|
||||
|
||||
public static void encode(SyncCustomTagTypes msg, PacketBuffer buf)
|
||||
{
|
||||
buf.writeVarInt(msg.customTagTypeCollections.size());
|
||||
msg.customTagTypeCollections.forEach((registryName, modded) -> forgeTagCollectionWrite(buf, registryName, modded.func_241833_a()));
|
||||
}
|
||||
|
||||
private static <T> void forgeTagCollectionWrite(PacketBuffer buf, ResourceLocation registryName, Map<ResourceLocation, ITag<T>> tags)
|
||||
{
|
||||
buf.writeResourceLocation(registryName);
|
||||
buf.writeVarInt(tags.size());
|
||||
tags.forEach((name, tag) -> {
|
||||
buf.writeResourceLocation(name);
|
||||
List<T> elements = tag.func_230236_b_();
|
||||
buf.writeVarInt(elements.size());
|
||||
for (T element : elements)
|
||||
{
|
||||
buf.writeResourceLocation(((IForgeRegistryEntry<?>) element).getRegistryName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static SyncCustomTagTypes decode(PacketBuffer buf)
|
||||
{
|
||||
ImmutableMap.Builder<ResourceLocation, ITagCollection<?>> builder = ImmutableMap.builder();
|
||||
int size = buf.readVarInt();
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
ResourceLocation regName = buf.readResourceLocation();
|
||||
IForgeRegistry<?> registry = RegistryManager.ACTIVE.getRegistry(regName);
|
||||
if (registry != null)
|
||||
{
|
||||
builder.put(regName, readTagCollection(buf, registry));
|
||||
}
|
||||
}
|
||||
return new SyncCustomTagTypes(builder.build());
|
||||
}
|
||||
|
||||
private static <T extends IForgeRegistryEntry<T>> ITagCollection<T> readTagCollection(PacketBuffer buf, IForgeRegistry<T> registry)
|
||||
{
|
||||
Map<ResourceLocation, ITag<T>> tags = Maps.newHashMap();
|
||||
int totalTags = buf.readVarInt();
|
||||
for (int i = 0; i < totalTags; i++)
|
||||
{
|
||||
ImmutableSet.Builder<T> elementBuilder = ImmutableSet.builder();
|
||||
ResourceLocation name = buf.readResourceLocation();
|
||||
int totalElements = buf.readVarInt();
|
||||
for (int j = 0; j < totalElements; j++)
|
||||
{
|
||||
T element = registry.getValue(buf.readResourceLocation());
|
||||
if (element != null)
|
||||
{
|
||||
elementBuilder.add(element);
|
||||
}
|
||||
}
|
||||
tags.put(name, ITag.func_232946_a_(elementBuilder.build()));
|
||||
}
|
||||
return ITagCollection.func_242202_a(tags);
|
||||
}
|
||||
|
||||
public static void handle(SyncCustomTagTypes msg, Supplier<NetworkEvent.Context> ctx)
|
||||
{
|
||||
ctx.get().enqueueWork(() -> {
|
||||
if (Minecraft.getInstance().world != null)
|
||||
{
|
||||
ITagCollectionSupplier tagCollectionSupplier = Minecraft.getInstance().world.getTags();
|
||||
//Validate that all the tags exist using the tag type collections from the packet
|
||||
// We mimic vanilla in that we validate before updating the actual stored tags so that it can gracefully fallback
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
if (missingTags.isEmpty())
|
||||
{
|
||||
//If we have no missing tags, update the custom tag types
|
||||
ForgeTagHandler.updateCustomTagTypes(msg);
|
||||
if (!ctx.get().getNetworkManager().isLocalChannel())
|
||||
{
|
||||
//And if everything hasn't already been set due to being in single player
|
||||
// Fetch and update the custom tag types. We skip vanilla tag types as they have already been fetched
|
||||
// And fire an event that the custom tag types have been updated
|
||||
TagRegistryManager.fetchCustomTagTypes(tagCollectionSupplier);
|
||||
MinecraftForge.EVENT_BUS.post(new TagsUpdatedEvent.CustomTagTypes(tagCollectionSupplier));
|
||||
}
|
||||
} else {
|
||||
LOGGER.warn("Incomplete server tags, disconnecting. Missing: {}", missingTags);
|
||||
ctx.get().getNetworkManager().closeChannel(new TranslationTextComponent("multiplayer.disconnect.missing_tags"));
|
||||
}
|
||||
}
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
package net.minecraftforge.fml.network;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -26,6 +27,8 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import net.minecraft.tags.ITagCollection;
|
||||
import net.minecraft.tags.ITagCollectionSupplier;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraftforge.fml.common.thread.EffectiveSide;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -208,4 +211,31 @@ public class NetworkHooks
|
||||
player.openContainer.addListener(player);
|
||||
MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, c));
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the custom tag types attached to a {@link ITagCollectionSupplier} to all connected players.
|
||||
* @param tagCollectionSupplier The tag collection supplier containing the custom tags
|
||||
*/
|
||||
public static void syncCustomTagTypes(ITagCollectionSupplier tagCollectionSupplier)
|
||||
{
|
||||
Map<ResourceLocation, ITagCollection<?>> customTagTypes = tagCollectionSupplier.getCustomTagTypes();
|
||||
if (!customTagTypes.isEmpty())
|
||||
{
|
||||
FMLNetworkConstants.playChannel.send(PacketDistributor.ALL.noArg(), new FMLPlayMessages.SyncCustomTagTypes(customTagTypes));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the custom tag types attached to a {@link ITagCollectionSupplier} to the given player.
|
||||
* @param player The player to sync the custom tags to.
|
||||
* @param tagCollectionSupplier The tag collection supplier containing the custom tags
|
||||
*/
|
||||
public static void syncCustomTagTypes(ServerPlayerEntity player, ITagCollectionSupplier tagCollectionSupplier)
|
||||
{
|
||||
Map<ResourceLocation, ITagCollection<?>> customTagTypes = tagCollectionSupplier.getCustomTagTypes();
|
||||
if (!customTagTypes.isEmpty())
|
||||
{
|
||||
FMLNetworkConstants.playChannel.sendTo(new FMLPlayMessages.SyncCustomTagTypes(customTagTypes), player.connection.getNetworkManager(), NetworkDirection.PLAY_TO_CLIENT);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,12 @@ class NetworkInitialization {
|
||||
// .consumer(FMLPlayMessages.DimensionInfoMessage::handle)
|
||||
// .add();
|
||||
|
||||
playChannel.messageBuilder(FMLPlayMessages.SyncCustomTagTypes.class, 3).
|
||||
decoder(FMLPlayMessages.SyncCustomTagTypes::decode).
|
||||
encoder(FMLPlayMessages.SyncCustomTagTypes::encode).
|
||||
consumer(FMLPlayMessages.SyncCustomTagTypes::handle).
|
||||
add();
|
||||
|
||||
return playChannel;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user