Port to 26.1

This commit is contained in:
skippyall
2026-04-29 17:20:13 +02:00
parent f5202a4264
commit e117139a63
100 changed files with 609 additions and 550 deletions
@@ -4,11 +4,12 @@ import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.skippyall.minions.registration.MinionRegistries;
import java.util.List;
import java.util.function.Function;
import net.minecraft.core.RegistryAccess;
import net.minecraft.server.dialog.body.DialogBody;
import java.util.List;
import java.util.function.Function;
public interface DocsEntry {
Codec<DocsEntry> CODEC = MinionRegistries.DOCS_ENTRY_TYPES.byNameCodec().dispatch(DocsEntry::getCodec, Function.identity());
@@ -7,7 +7,7 @@ import net.fabricmc.fabric.api.resource.SimpleResourceReloadListener;
import net.minecraft.core.Holder;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.resources.Identifier;
import net.minecraft.server.dialog.ActionButton;
import net.minecraft.server.dialog.CommonButtonData;
import net.minecraft.server.dialog.CommonDialogData;
@@ -30,15 +30,15 @@ import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<ResourceLocation, DocsEntry>, DocsTree>> {
private static Map<ResourceLocation, DocsEntry> docs;
public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<Identifier, DocsEntry>, DocsTree>> {
private static Map<Identifier, DocsEntry> docs;
private static DocsTree tree;
public static DocsTree getTree() {
return tree;
}
public static void showDocsEntry(ServerPlayer player, ResourceLocation id) {
public static void showDocsEntry(ServerPlayer player, Identifier id) {
DocsEntry entry = getDocsEntry(id);
if(entry == null) {
return;
@@ -48,11 +48,11 @@ public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<Resou
if(tree != null) {
DocsTree.DocElement element = tree.getElement(id);
if (element.previous() != null) {
ResourceLocation previousId = element.previous().getId();
Identifier previousId = element.previous().getId();
buttons.add(getDialogButton(Component.literal("<- ").append(Component.translatable(getDocsEntry(previousId).getMetadata().titleKey())), previousId.toString()));
}
if (element.next() != null) {
ResourceLocation nextId = element.next().getId();
Identifier nextId = element.next().getId();
buttons.add(getDialogButton(Component.translatable(getDocsEntry(nextId).getMetadata().titleKey()).append(Component.literal(" ->")), nextId.toString()));
}
}
@@ -87,21 +87,21 @@ public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<Resou
);
}
public static DocsEntry getDocsEntry(ResourceLocation id) {
public static DocsEntry getDocsEntry(Identifier id) {
return docs.get(id);
}
public static Collection<ResourceLocation> getDocsEntryIds() {
public static Collection<Identifier> getDocsEntryIds() {
return docs.keySet();
}
@Override
public CompletableFuture<Tuple<Map<ResourceLocation, DocsEntry>, DocsTree>> load(ResourceManager resourceManager, Executor executor) {
public CompletableFuture<Tuple<Map<Identifier, DocsEntry>, DocsTree>> load(ResourceManager resourceManager, Executor executor) {
return CompletableFuture.supplyAsync(() -> {
Map<ResourceLocation, Resource> resources = resourceManager.listResources("docs", id -> id.getNamespace().equals(Minions.MOD_ID) && id.getPath().endsWith(".json"));
Map<Identifier, Resource> resources = resourceManager.listResources("docs", id -> id.getNamespace().equals(Minions.MOD_ID) && id.getPath().endsWith(".json"));
final DocsTree.BranchElement[] root = {null};
Map<ResourceLocation, DocsEntry> docsEntries = new HashMap<>();
Map<Identifier, DocsEntry> docsEntries = new HashMap<>();
resources.forEach((id, resource) -> {
try(Reader reader = resource.openAsReader()) {
if(id.getPath().equals("docs/tree.json")) {
@@ -109,7 +109,7 @@ public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<Resou
.ifSuccess(entry -> root[0] = entry.getFirst())
.ifError(error -> Minions.LOGGER.warn("Could not parse docs tree {}: {}", id, error.message()));
} else {
ResourceLocation docId = ResourceLocation.fromNamespaceAndPath(id.getNamespace(), id.getPath().substring("docs/".length(), id.getPath().length() - ".json".length()));
Identifier docId = Identifier.fromNamespaceAndPath(id.getNamespace(), id.getPath().substring("docs/".length(), id.getPath().length() - ".json".length()));
DocsEntry.CODEC.decode(JsonOps.INSTANCE, StrictJsonParser.parse(reader))
.ifSuccess(entry -> docsEntries.put(docId, entry.getFirst()))
.ifError(error -> Minions.LOGGER.warn("Could not parse docs entry {}: {}", id, error.message()));
@@ -128,7 +128,7 @@ public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<Resou
}
@Override
public CompletableFuture<Void> apply(Tuple<Map<ResourceLocation, DocsEntry>, DocsTree> o, ResourceManager resourceManager, Executor executor) {
public CompletableFuture<Void> apply(Tuple<Map<Identifier, DocsEntry>, DocsTree> o, ResourceManager resourceManager, Executor executor) {
return CompletableFuture.supplyAsync(() -> {
docs = o.getA();
tree = o.getB();
@@ -137,7 +137,7 @@ public class DocsManager implements SimpleResourceReloadListener<Tuple<Map<Resou
}
@Override
public ResourceLocation getFabricId() {
return ResourceLocation.fromNamespaceAndPath(Minions.MOD_ID, "docs");
public Identifier getFabricId() {
return Identifier.fromNamespaceAndPath(Minions.MOD_ID, "docs");
}
}
@@ -2,15 +2,16 @@ package io.github.skippyall.minions.docs;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import net.minecraft.resources.Identifier;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import net.minecraft.resources.ResourceLocation;
public class DocsTree {
private final BranchElement root;
private final Map<ResourceLocation, DocElement> entries = new HashMap<>();
private final Map<Identifier, DocElement> entries = new HashMap<>();
public DocsTree(BranchElement root) {
this.root = root;
@@ -29,7 +30,7 @@ public class DocsTree {
return root;
}
public DocElement getElement(ResourceLocation id) {
public DocElement getElement(Identifier id) {
return entries.get(id);
}
@@ -54,15 +55,15 @@ public class DocsTree {
}
public static final class DocElement extends Element {
public static final Codec<DocElement> CODEC = ResourceLocation.CODEC.xmap(DocElement::new, DocElement::getId);
public static final Codec<DocElement> CODEC = Identifier.CODEC.xmap(DocElement::new, DocElement::getId);
private final ResourceLocation id;
private final Identifier id;
public DocElement(ResourceLocation element) {
public DocElement(Identifier element) {
this.id = element;
}
public ResourceLocation getId() {
public Identifier getId() {
return id;
}
}
@@ -4,26 +4,27 @@ import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import io.github.skippyall.minions.gui.GuiDisplay;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.dialog.body.DialogBody;
import net.minecraft.server.dialog.body.ItemBody;
import net.minecraft.server.dialog.body.PlainMessage;
import net.minecraft.world.item.Item;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public record ReferenceEntry(Metadata metadata, ResourceKey<?> object, Component shortDescription, Component longDescription) implements DocsEntry {
private static final Codec<ResourceKey<?>> REGISTRY_KEY_CODEC = RecordCodecBuilder.create(instance ->
instance.group(
ResourceLocation.CODEC.fieldOf("registry").forGetter(ResourceKey::registry),
ResourceLocation.CODEC.fieldOf("value").forGetter(ResourceKey::location)
Identifier.CODEC.fieldOf("registry").forGetter(ResourceKey::registry),
Identifier.CODEC.fieldOf("value").forGetter(ResourceKey::identifier)
).apply(instance, (registry, value) -> ResourceKey.create(ResourceKey.createRegistryKey(registry), value))
);
@@ -46,9 +47,9 @@ public record ReferenceEntry(Metadata metadata, ResourceKey<?> object, Component
GuiDisplay display = getObjectDisplay(manager);
if(display != null) {
bodyElements.add(new ItemBody(display.createItemStack(), Optional.empty(), false, false, 16, 16));
bodyElements.add(new ItemBody(display.createItemStackTemplate(), Optional.empty(), false, false, 16, 16));
}
bodyElements.add(new PlainMessage(Component.translatable(object.location().toLanguageKey(object.registry().getPath())), 200));
bodyElements.add(new PlainMessage(Component.translatable(object.identifier().toLanguageKey(object.registry().getPath())), 200));
bodyElements.add(new PlainMessage(longDescription, 200));
return bodyElements;
@@ -59,15 +60,15 @@ public record ReferenceEntry(Metadata metadata, ResourceKey<?> object, Component
if(object.isFor(Registries.ITEM) || object.isFor(Registries.BLOCK)) {
Item item;
if(object.isFor(Registries.ITEM)) {
item = BuiltInRegistries.ITEM.getValue(object.location());
item = BuiltInRegistries.ITEM.getValue(object.identifier());
} else {
item = BuiltInRegistries.BLOCK.getValue(object.location()).asItem();
item = BuiltInRegistries.BLOCK.getValue(object.identifier()).asItem();
}
if(item != null) {
display = new GuiDisplay.ItemBased(item);
}
} else {
ResourceLocation displayId = object.location().withPrefix(object.registry().getPath() + "/");
Identifier displayId = object.identifier().withPrefix(object.registry().getPath() + "/");
display = GuiDisplay.getGuiDisplay(displayId, manager);
}
return display;