Tags Helper improvements
This commit is contained in:
parent
4b78008e1d
commit
75d3347a45
2 changed files with 44 additions and 16 deletions
|
@ -1,24 +1,35 @@
|
||||||
package ru.betterend.mixin.common;
|
package ru.betterend.mixin.common;
|
||||||
|
|
||||||
import java.util.Map;
|
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.Mixin;
|
||||||
|
import org.spongepowered.asm.mixin.Shadow;
|
||||||
import org.spongepowered.asm.mixin.injection.At;
|
import org.spongepowered.asm.mixin.injection.At;
|
||||||
import org.spongepowered.asm.mixin.injection.Inject;
|
import org.spongepowered.asm.mixin.injection.Inject;
|
||||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||||
|
|
||||||
|
import net.minecraft.resource.ResourceManager;
|
||||||
import net.minecraft.tag.Tag;
|
import net.minecraft.tag.Tag;
|
||||||
import net.minecraft.tag.TagGroup;
|
|
||||||
import net.minecraft.tag.TagGroupLoader;
|
import net.minecraft.tag.TagGroupLoader;
|
||||||
import net.minecraft.util.Identifier;
|
import net.minecraft.util.Identifier;
|
||||||
|
|
||||||
import ru.betterend.util.TagHelper;
|
import ru.betterend.util.TagHelper;
|
||||||
|
|
||||||
@Mixin(TagGroupLoader.class)
|
@Mixin(TagGroupLoader.class)
|
||||||
public class TagGroupLoaderMixin {
|
public class TagGroupLoaderMixin {
|
||||||
@Inject(method = "applyReload", at = @At(value = "HEAD"))
|
|
||||||
private void onReload(Map<Identifier, Tag.Builder> tags, CallbackInfoReturnable<TagGroup<?>> info) {
|
@Shadow
|
||||||
tags.forEach((id, builder) -> {
|
private String entryType;
|
||||||
TagHelper.apply(id, builder);
|
|
||||||
});
|
@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;
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,14 +14,15 @@ import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.registry.Registry;
|
import net.minecraft.util.registry.Registry;
|
||||||
|
|
||||||
public class TagHelper {
|
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) {
|
public static void addTag(Tag.Identified<Block> tag, Block... blocks) {
|
||||||
Identifier tagID = tag.getId();
|
Identifier tagID = tag.getId();
|
||||||
Set<Identifier> set = TAGS.get(tagID);
|
Set<Identifier> set = TAGS_BLOCK.get(tagID);
|
||||||
if (set == null) {
|
if (set == null) {
|
||||||
set = Sets.newHashSet();
|
set = Sets.newHashSet();
|
||||||
TAGS.put(tagID, set);
|
TAGS_BLOCK.put(tagID, set);
|
||||||
}
|
}
|
||||||
for (Block block: blocks) {
|
for (Block block: blocks) {
|
||||||
Identifier id = Registry.BLOCK.getId(block);
|
Identifier id = Registry.BLOCK.getId(block);
|
||||||
|
@ -33,10 +34,10 @@ public class TagHelper {
|
||||||
|
|
||||||
public static void addTag(Tag.Identified<Item> tag, ItemConvertible... items) {
|
public static void addTag(Tag.Identified<Item> tag, ItemConvertible... items) {
|
||||||
Identifier tagID = tag.getId();
|
Identifier tagID = tag.getId();
|
||||||
Set<Identifier> set = TAGS.get(tagID);
|
Set<Identifier> set = TAGS_ITEM.get(tagID);
|
||||||
if (set == null) {
|
if (set == null) {
|
||||||
set = Sets.newHashSet();
|
set = Sets.newHashSet();
|
||||||
TAGS.put(tagID, set);
|
TAGS_ITEM.put(tagID, set);
|
||||||
}
|
}
|
||||||
for (ItemConvertible item: items) {
|
for (ItemConvertible item: items) {
|
||||||
Identifier id = Registry.ITEM.getId(item.asItem());
|
Identifier id = Registry.ITEM.getId(item.asItem());
|
||||||
|
@ -60,11 +61,27 @@ public class TagHelper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void apply(Identifier id, Tag.Builder builder) {
|
private static Tag.Builder apply(String entry, Set<Identifier> ids, Tag.Builder builder) {
|
||||||
Set<Identifier> values = TAGS.get(id);
|
ids.forEach((value) -> {
|
||||||
if (values != null) {
|
builder.add(value, "Better End Code");
|
||||||
values.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()));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue