-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure material and modifier tags sync over the network
- Loading branch information
1 parent
44027b6
commit 1aaf7aa
Showing
8 changed files
with
122 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/slimeknights/tconstruct/library/utils/GenericTagUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package slimeknights.tconstruct.library.utils; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.ImmutableSet.Builder; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.resources.ResourceKey; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.tags.Tag; | ||
import net.minecraft.tags.TagKey; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
/** Static helpers for generic tag loading */ | ||
public class GenericTagUtil { | ||
private GenericTagUtil() {} | ||
|
||
/** Creates a map of reverse tags for the given map of tags */ | ||
public static <T, I extends ResourceLocation> Map<I,Set<TagKey<T>>> reverseTags(ResourceKey<? extends Registry<T>> registry, Function<T,I> keyMapper, Map<ResourceLocation,Tag<T>> tags) { | ||
Map<I,ImmutableSet.Builder<TagKey<T>>> reverseTags = new HashMap<>(); | ||
Function<I,Builder<TagKey<T>>> makeSet = id -> ImmutableSet.builder(); | ||
for (Entry<ResourceLocation,Tag<T>> entry : tags.entrySet()) { | ||
TagKey<T> key = TagKey.create(registry, entry.getKey()); | ||
for (T value : entry.getValue().getValues()) { | ||
reverseTags.computeIfAbsent(keyMapper.apply(value), makeSet).add(key); | ||
} | ||
} | ||
return reverseTags.entrySet().stream() | ||
.collect(Collectors.toMap(Entry::getKey, entry->entry.getValue().build())); | ||
} | ||
|
||
/** Decodes a map of tags from the packet */ | ||
public static <T> Map<ResourceLocation,Tag<T>> decodeTags(FriendlyByteBuf buf, Function<ResourceLocation,T> valueGetter) { | ||
ImmutableMap.Builder<ResourceLocation,Tag<T>> builder = ImmutableMap.builder(); | ||
int mapSize = buf.readVarInt(); | ||
for (int i = 0; i < mapSize; i++) { | ||
ResourceLocation tagId = buf.readResourceLocation(); | ||
int tagSize = buf.readVarInt(); | ||
ImmutableList.Builder<T> tagBuilder = ImmutableList.builder(); | ||
for (int j = 0; j < tagSize; j++) { | ||
tagBuilder.add(valueGetter.apply(buf.readResourceLocation())); | ||
} | ||
builder.put(tagId, new Tag<>(tagBuilder.build())); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
/** Writes a map of tags to a packet */ | ||
public static <T> void encodeTags(FriendlyByteBuf buf, Function<T,ResourceLocation> keyGetter, Map<ResourceLocation,Tag<T>> tags) { | ||
buf.writeVarInt(tags.size()); | ||
for (Entry<ResourceLocation,Tag<T>> entry : tags.entrySet()) { | ||
buf.writeResourceLocation(entry.getKey()); | ||
List<T> values = entry.getValue().getValues(); | ||
buf.writeVarInt(values.size()); | ||
for (T value : values) { | ||
buf.writeResourceLocation(keyGetter.apply(value)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.