Tags Helper improvements

This commit is contained in:
Aleksey 2020-11-13 17:45:42 +03:00
parent 4b78008e1d
commit 75d3347a45
2 changed files with 44 additions and 16 deletions

View file

@ -1,24 +1,35 @@
package ru.betterend.mixin.common;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.resource.ResourceManager;
import net.minecraft.tag.Tag;
import net.minecraft.tag.TagGroup;
import net.minecraft.tag.TagGroupLoader;
import net.minecraft.util.Identifier;
import ru.betterend.util.TagHelper;
@Mixin(TagGroupLoader.class)
public class TagGroupLoaderMixin {
@Inject(method = "applyReload", at = @At(value = "HEAD"))
private void onReload(Map<Identifier, Tag.Builder> tags, CallbackInfoReturnable<TagGroup<?>> info) {
tags.forEach((id, builder) -> {
TagHelper.apply(id, builder);
});
@Shadow
private String entryType;
@Inject(method = "prepareReload", at = @At("RETURN"), cancellable = true)
public void prepareReload(ResourceManager manager, Executor prepareExecutor, CallbackInfoReturnable<CompletableFuture<Map<Identifier, Tag.Builder>>> info) {
CompletableFuture<Map<Identifier, Tag.Builder>> future = info.getReturnValue();
info.setReturnValue(CompletableFuture.supplyAsync(() -> {
Map<Identifier, Tag.Builder> map = future.join();
TagHelper.apply(entryType, map);
return map;
}));
}
}

View file

@ -14,14 +14,15 @@ import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
public class TagHelper {
private static final Map<Identifier, Set<Identifier>> TAGS = Maps.newHashMap();
private static final Map<Identifier, Set<Identifier>> TAGS_BLOCK = Maps.newHashMap();
private static final Map<Identifier, Set<Identifier>> TAGS_ITEM = Maps.newHashMap();
public static void addTag(Tag.Identified<Block> tag, Block... blocks) {
Identifier tagID = tag.getId();
Set<Identifier> set = TAGS.get(tagID);
Set<Identifier> set = TAGS_BLOCK.get(tagID);
if (set == null) {
set = Sets.newHashSet();
TAGS.put(tagID, set);
TAGS_BLOCK.put(tagID, set);
}
for (Block block: blocks) {
Identifier id = Registry.BLOCK.getId(block);
@ -33,10 +34,10 @@ public class TagHelper {
public static void addTag(Tag.Identified<Item> tag, ItemConvertible... items) {
Identifier tagID = tag.getId();
Set<Identifier> set = TAGS.get(tagID);
Set<Identifier> set = TAGS_ITEM.get(tagID);
if (set == null) {
set = Sets.newHashSet();
TAGS.put(tagID, set);
TAGS_ITEM.put(tagID, set);
}
for (ItemConvertible item: items) {
Identifier id = Registry.ITEM.getId(item.asItem());
@ -60,11 +61,27 @@ public class TagHelper {
}
}
public static void apply(Identifier id, Tag.Builder builder) {
Set<Identifier> values = TAGS.get(id);
if (values != null) {
values.forEach((value) -> {
builder.add(value, "Better End Code");
private static Tag.Builder apply(String entry, Set<Identifier> ids, Tag.Builder builder) {
ids.forEach((value) -> {
builder.add(value, "Better End Code");
});
return builder;
}
public static void apply(String entry, Map<Identifier, Tag.Builder> tagsMap) {
Map<Identifier, Set<Identifier>> endTags = null;
if (entry.equals("block")) {
endTags = TAGS_BLOCK;
} else if (entry.equals("item")) {
endTags = TAGS_ITEM;
}
if (endTags != null) {
endTags.forEach((id, ids) -> {
if (tagsMap.containsKey(id)) {
apply(entry, ids, tagsMap.get(id));
} else {
tagsMap.put(id, apply(entry, ids, Tag.Builder.create()));
}
});
}
}