13
0
geforkt von Mirrors/Paper

Add more advancement API

== AT ==
public net.minecraft.advancements.Advancement decorateName(Lnet/minecraft/advancements/DisplayInfo;)Lnet/minecraft/network/chat/Component;

Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
Dieser Commit ist enthalten in:
syldium 2021-07-09 18:50:40 +02:00
Ursprung b2715aeeed
Commit 864494345a
6 geänderte Dateien mit 152 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,10 @@
--- a/net/minecraft/advancements/DisplayInfo.java
+++ b/net/minecraft/advancements/DisplayInfo.java
@@ -37,6 +37,7 @@
private final boolean hidden;
private float x;
private float y;
+ public final io.papermc.paper.advancement.AdvancementDisplay paper = new io.papermc.paper.advancement.PaperAdvancementDisplay(this); // Paper - Add more advancement API
public DisplayInfo(
ItemStack icon,

Datei anzeigen

@ -0,0 +1,69 @@
package io.papermc.paper.advancement;
import io.papermc.paper.adventure.PaperAdventure;
import net.kyori.adventure.text.Component;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementType;
import net.minecraft.advancements.DisplayInfo;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public record PaperAdvancementDisplay(DisplayInfo handle) implements AdvancementDisplay {
@Override
public @NotNull Frame frame() {
return asPaperFrame(this.handle.getType());
}
@Override
public @NotNull Component title() {
return PaperAdventure.asAdventure(this.handle.getTitle());
}
@Override
public @NotNull Component description() {
return PaperAdventure.asAdventure(this.handle.getDescription());
}
@Override
public @NotNull ItemStack icon() {
return CraftItemStack.asBukkitCopy(this.handle.getIcon());
}
@Override
public boolean doesShowToast() {
return this.handle.shouldShowToast();
}
@Override
public boolean doesAnnounceToChat() {
return this.handle.shouldAnnounceChat();
}
@Override
public boolean isHidden() {
return this.handle.isHidden();
}
@Override
public @Nullable NamespacedKey backgroundPath() {
return this.handle.getBackground().map(CraftNamespacedKey::fromMinecraft).orElse(null);
}
@Override
public @NotNull Component displayName() {
return PaperAdventure.asAdventure(Advancement.decorateName(java.util.Objects.requireNonNull(this.handle, "cannot build display name for null handle, invalid state")));
}
public static @NotNull Frame asPaperFrame(final @NotNull AdvancementType frameType) {
return switch (frameType) {
case TASK -> Frame.TASK;
case CHALLENGE -> Frame.CHALLENGE;
case GOAL -> Frame.GOAL;
};
}
}

Datei anzeigen

@ -35,12 +35,47 @@ public class CraftAdvancement implements org.bukkit.advancement.Advancement {
return new CraftAdvancementRequirements(this.handle.value().requirements());
}
// Paper start - Add more advancement API
@Override
public AdvancementDisplay getDisplay() {
if (this.handle.value().display().isEmpty()) {
return null;
}
return new CraftAdvancementDisplay(this.handle.value().display().get());
public io.papermc.paper.advancement.AdvancementDisplay getDisplay() {
return this.handle.value().display().map(d -> d.paper).orElse(null);
}
@Deprecated
@io.papermc.paper.annotation.DoNotUse
public AdvancementDisplay getDisplay0() { // May be called by plugins via Commodore
return this.handle.value().display().map(CraftAdvancementDisplay::new).orElse(null);
}
@Override
public net.kyori.adventure.text.Component displayName() {
return io.papermc.paper.adventure.PaperAdventure.asAdventure(net.minecraft.advancements.Advancement.name(this.handle));
}
@Override
public org.bukkit.advancement.Advancement getParent() {
return this.handle.value().parent()
.map(net.minecraft.server.MinecraftServer.getServer().getAdvancements()::get)
.map(AdvancementHolder::toBukkit)
.orElse(null);
}
@Override
public Collection<org.bukkit.advancement.Advancement> getChildren() {
final com.google.common.collect.ImmutableList.Builder<org.bukkit.advancement.Advancement> children = com.google.common.collect.ImmutableList.<org.bukkit.advancement.Advancement>builder();
final net.minecraft.advancements.AdvancementNode advancementNode = net.minecraft.server.MinecraftServer.getServer().getAdvancements().tree().get(this.handle);
if (advancementNode != null) {
for (final net.minecraft.advancements.AdvancementNode child : advancementNode.children()) {
children.add(child.holder().toBukkit());
}
}
return children.build();
}
@Override
public org.bukkit.advancement.Advancement getRoot() {
final net.minecraft.advancements.AdvancementNode advancementNode = net.minecraft.server.MinecraftServer.getServer().getAdvancements().tree().get(this.handle);
return java.util.Objects.requireNonNull(advancementNode, "could not find internal advancement node for advancement " + this.handle.id()).root().holder().toBukkit();
}
// Paper end - Add more advancement API
}

Datei anzeigen

@ -6,6 +6,7 @@ import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.inventory.ItemStack;
@Deprecated // Paper
public class CraftAdvancementDisplay implements org.bukkit.advancement.AdvancementDisplay {
private final DisplayInfo handle;

Datei anzeigen

@ -458,6 +458,11 @@ public class Commodore {
super.visitMethodInsn(opcode, owner, name, "()Lcom/destroystokyo/paper/profile/PlayerProfile;", itf);
return;
}
if (owner.equals("org/bukkit/advancement/Advancement") && name.equals("getDisplay") && desc.endsWith(")Lorg/bukkit/advancement/AdvancementDisplay;")) {
super.visitTypeInsn(Opcodes.CHECKCAST, runtimeCbPkgPrefix() + "advancement/CraftAdvancement");
super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, runtimeCbPkgPrefix() + "advancement/CraftAdvancement", "getDisplay0", desc, false);
return;
}
// Paper end
if (modern) {

Datei anzeigen

@ -0,0 +1,26 @@
package io.papermc.paper.advancement;
import io.papermc.paper.adventure.PaperAdventure;
import net.kyori.adventure.text.format.TextColor;
import net.minecraft.advancements.AdvancementType;
import net.minecraft.network.chat.contents.TranslatableContents;
import org.bukkit.support.environment.Normal;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Normal
public class AdvancementFrameTest {
@Test
public void test() {
for (final AdvancementType advancementType : AdvancementType.values()) {
final TextColor expectedColor = PaperAdventure.asAdventure(advancementType.getChatColor());
final String expectedTranslationKey = ((TranslatableContents) advancementType.getDisplayName().getContents()).getKey();
final var frame = PaperAdvancementDisplay.asPaperFrame(advancementType);
assertEquals(expectedTranslationKey, frame.translationKey(), "The translation keys should be the same");
assertEquals(expectedColor, frame.color(), "The frame colors should be the same");
assertEquals(advancementType.getSerializedName(), AdvancementDisplay.Frame.NAMES.key(frame));
}
}
}