Fix tag related issues when connecting to a vanilla server.

This commit is contained in:
LexManos committed 2020-06-29 12:42:32 -07:00
1 parent 3b64ea6305
commit 80d2730fec
13 files changed
+207 -29

No files matched your search

@@ -19,11 +19,18 @@
package net.minecraftforge.common;
import java.util.Arrays;
import java.util.stream.Collectors;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.ITag;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.registry.Registry;
import net.minecraftforge.common.util.ForgeNetworkTagManager;
import net.minecraftforge.registries.IForgeRegistryEntry;
public class Tags
{
@@ -37,10 +44,10 @@ public class Tags
public static final ITag.INamedTag<Block> DIRT = tag("dirt");
public static final ITag.INamedTag<Block> END_STONES = tag("end_stones");
public static final ITag.INamedTag<Block> FENCE_GATES = tag("fence_gates");
public static final ITag.INamedTag<Block> FENCE_GATES_WOODEN = tag("fence_gates/wooden");
public static final ITag.INamedTag<Block> FENCE_GATES_WOODEN = defaulted("fence_gates/wooden", net.minecraft.block.Blocks.OAK_FENCE_GATE, net.minecraft.block.Blocks.BIRCH_FENCE_GATE, net.minecraft.block.Blocks.SPRUCE_FENCE_GATE, net.minecraft.block.Blocks.JUNGLE_FENCE_GATE, net.minecraft.block.Blocks.DARK_OAK_FENCE_GATE, net.minecraft.block.Blocks.ACACIA_FENCE_GATE);
public static final ITag.INamedTag<Block> FENCES = tag("fences");
public static final ITag.INamedTag<Block> FENCES_NETHER_BRICK = tag("fences/nether_brick");
public static final ITag.INamedTag<Block> FENCES_WOODEN = tag("fences/wooden");
public static final ITag.INamedTag<Block> FENCES_WOODEN = defaulted("fences/wooden", net.minecraft.block.Blocks.OAK_FENCE, net.minecraft.block.Blocks.BIRCH_FENCE, net.minecraft.block.Blocks.SPRUCE_FENCE, net.minecraft.block.Blocks.JUNGLE_FENCE, net.minecraft.block.Blocks.DARK_OAK_FENCE, net.minecraft.block.Blocks.ACACIA_FENCE);
public static final ITag.INamedTag<Block> GLASS = tag("glass");
public static final ITag.INamedTag<Block> GLASS_BLACK = tag("glass/black");
@@ -115,6 +122,12 @@ public class Tags
{
return BlockTags.makeWrapperTag("forge:" + name);
}
@SuppressWarnings("deprecation")
private static ITag.INamedTag<Block> defaulted(String name, Block... values)
{
return ForgeNetworkTagManager.defaulted(Registry.field_239711_l_, tag(name), Arrays.asList(values).stream().map(e -> e.getRegistryName()).collect(Collectors.toSet()));
}
}
public static class Items
@@ -162,10 +175,10 @@ public class Tags
public static final ITag.INamedTag<Item> ENDER_PEARLS = tag("ender_pearls");
public static final ITag.INamedTag<Item> FEATHERS = tag("feathers");
public static final ITag.INamedTag<Item> FENCE_GATES = tag("fence_gates");
public static final ITag.INamedTag<Item> FENCE_GATES_WOODEN = tag("fence_gates/wooden");
public static final ITag.INamedTag<Item> FENCE_GATES_WOODEN = defaulted("fence_gates/wooden", Blocks.FENCE_GATES_WOODEN);
public static final ITag.INamedTag<Item> FENCES = tag("fences");
public static final ITag.INamedTag<Item> FENCES_NETHER_BRICK = tag("fences/nether_brick");
public static final ITag.INamedTag<Item> FENCES_WOODEN = tag("fences/wooden");
public static final ITag.INamedTag<Item> FENCES_WOODEN = defaulted("fences/wooden", Blocks.FENCES_WOODEN);
public static final ITag.INamedTag<Item> GEMS = tag("gems");
public static final ITag.INamedTag<Item> GEMS_DIAMOND = tag("gems/diamond");
public static final ITag.INamedTag<Item> GEMS_EMERALD = tag("gems/emerald");
@@ -271,5 +284,11 @@ public class Tags
{
return ItemTags.makeWrapperTag("forge:" + name);
}
@SuppressWarnings("deprecation")
private static ITag.INamedTag<Item> defaulted(String name, ITag.INamedTag<Block> parent)
{
return ForgeNetworkTagManager.defaulted(Registry.field_239714_o_, tag(name), RegistryKey.func_240903_a_(Registry.field_239711_l_, parent.func_230234_a_()));
}
}
}
@@ -22,7 +22,6 @@ package net.minecraftforge.common.data;
import net.minecraft.data.DataGenerator;
import net.minecraft.item.DyeColor;
import net.minecraft.tags.ITag;
import net.minecraft.tags.Tag;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.Tags;
import net.minecraftforge.registries.ForgeRegistries;
@@ -47,6 +46,7 @@ public class ForgeBlockTagsProvider extends BlockTagsProvider
super(gen);
}
@SuppressWarnings("unchecked")
@Override
public void registerTags()
{
@@ -47,6 +47,7 @@ public class ForgeItemTagsProvider extends ItemTagsProvider
super(gen, blockTagProvider);
}
@SuppressWarnings("unchecked")
@Override
public void registerTags()
{
@@ -35,6 +35,7 @@ public interface IForgeTagBuilder<T>
return (TagsProvider.Builder<T>) this;
}
@SuppressWarnings("unchecked")
default TagsProvider.Builder<T> addTags(ITag.INamedTag<T>... values) {
TagsProvider.Builder<T> builder = getBuilder();
for (ITag.INamedTag<T> value : values) {
@@ -57,6 +58,7 @@ public interface IForgeTagBuilder<T>
return addOptional(Arrays.asList(locations));
}
@SuppressWarnings("deprecation")
default TagsProvider.Builder<T> addOptional(final Collection<ResourceLocation> locations)
{
return getBuilder().add(ForgeHooks.makeOptionalTag(true, locations));
@@ -67,6 +69,7 @@ public interface IForgeTagBuilder<T>
return addOptionalTag(Arrays.asList(locations));
}
@SuppressWarnings("deprecation")
default TagsProvider.Builder<T> addOptionalTag(final Collection<ResourceLocation> locations)
{
return getBuilder().add(ForgeHooks.makeOptionalTag(false, locations));
@@ -0,0 +1,128 @@
/*
* 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.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import com.google.common.collect.ImmutableSet;
import net.minecraft.tags.ITag;
import net.minecraft.tags.NetworkTagCollection;
import net.minecraft.tags.NetworkTagManager;
import net.minecraft.tags.Tag;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
@Deprecated //For Forge's internal use only.
public class ForgeNetworkTagManager extends NetworkTagManager
{
private static Map<RegistryKey<?>, Set<ResourceLocation>> defaults = new HashMap<>();
/**
* Returns a new instance of this tag manager, again, only exposed for Forge's Hook use.
*/
public static NetworkTagManager create()
{
return new ForgeNetworkTagManager();
}
/**
* This will provide default tags if the server side does not send us one.
* Note: If they DO send us a empty one, this DOES NOT override that.
* This is 100% intended to be only used by Forge tto make sure our tags behave correctly on vanilla servers for the edge cases where we use tags for in-game loogic.
* Notably: Furnace burnables.
*/
public static <T> ITag.INamedTag<T> defaulted(RegistryKey<Registry<T>> type, ITag.INamedTag<T> tag, Set<ResourceLocation> values)
{
defaults.put(RegistryKey.func_240903_a_(type, tag.func_230234_a_()), ImmutableSet.copyOf(values));
return tag;
}
/**
* Copies the resource key from the parent tag to a new one.
* This simplifies the item variants of block tags.
* WILL explode if the parent hasn't been defaulted.
*/
public static <T> ITag.INamedTag<T> defaulted(RegistryKey<Registry<T>> type, ITag.INamedTag<T> tag, RegistryKey<?> parent)
{
RegistryKey<T> regKey = RegistryKey.func_240903_a_(type, tag.func_230234_a_());
Set<ResourceLocation> other = defaults.get(parent);
if (other == null)
throw new IllegalStateException("Attempted to set nested default without setting parent: " + regKey + " -> " + parent);
defaults.put(regKey, other);
return tag;
}
private ForgeNetworkTagManager()
{
this.blocks = new ForgeNetworkTagCollection<>(Registry.BLOCK, "tags/blocks", "block");
this.items = new ForgeNetworkTagCollection<>(Registry.ITEM, "tags/items", "item");
this.fluids = new ForgeNetworkTagCollection<>(Registry.FLUID, "tags/fluids", "fluid");
this.entityTypes = new ForgeNetworkTagCollection<>(Registry.ENTITY_TYPE, "tags/entity_types", "entity_type");
}
private static class ForgeNetworkTagCollection<T> extends NetworkTagCollection<T>
{
private final ITag<T> EMPTY = Tag.func_241284_a_();
private final Registry<T> registry;
private final Map<ResourceLocation, ITag<T>> cache = new HashMap<>();
public ForgeNetworkTagCollection(Registry<T> registry, String path, String name)
{
super(registry, path, name);
this.registry = registry;
}
@Override
@Nullable
public ITag<T> get(ResourceLocation key)
{
ITag<T> ret = super.get(key);
if (ret == null)
{
ret = cache.get(key);
if (ret == null) {
RegistryKey<T> regKey = RegistryKey.func_240903_a_(this.registry.getRegistryKey(), key);
Set<ResourceLocation> defaults = ForgeNetworkTagManager.defaults.get(regKey);
if (defaults == null)
{
ret = EMPTY;
}
else
{
Set<T> values = defaults.stream().map(getEntryLookup()).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
ret = Tag.func_241286_a_(values);
}
this.cache.put(key, ret);
}
}
return ret;
}
}
}