@@ -14,6 +14,8 @@ import dev.slne.surf.surfapi.core.api.util.mutableObject2ObjectMapOf
1414import net.kyori.adventure.text.Component
1515import java.util.*
1616
17+ private const val CRITERION_ID = " surfapi_toast"
18+
1719class ToastImpl (
1820 override val icon : ItemType ,
1921 override val text : Component ,
@@ -23,65 +25,67 @@ class ToastImpl(
2325 val user =
2426 PacketEvents .getAPI().playerManager.getUser(surfCoreApi.getPlayer(player) ? : return )
2527 ? : return
26- val stack =
27- ItemStack .builder().type(icon).build()
28- val criterionName = " surfapi_toast"
29- val criterion: List <String > = Collections .singletonList(criterionName)
3028 val resourceLocation = ResourceLocation .minecraft(" surfapi_toast_${UUID .randomUUID()} " )
31- val advancement = Advancement (
32- null ,
33- AdvancementDisplay (
34- text,
35- buildText {
36- error(" test" )
37- },
38- stack,
39- style.toType(),
40- null ,
41- true ,
42- false ,
43- 0.0f ,
44- 0.0f
45- ),
46- criterion,
47- Collections .singletonList(criterion),
48- false
49- )
50-
51- val showPacket = WrapperPlayServerUpdateAdvancements (
52- false ,
53- Collections .singletonList(
54- AdvancementHolder (
55- resourceLocation,
56- advancement
57- )
58- ),
59- Collections .emptySet(),
60- Collections .singletonMap(
61- resourceLocation, AdvancementProgress (
62- mutableObject2ObjectMapOf(
63- criterionName to AdvancementProgress .CriterionProgress (System .currentTimeMillis())
64- )
65- )
66- ),
67- true
68- )
69-
70- val hidePacket = WrapperPlayServerUpdateAdvancements (
71- false ,
72- Collections .emptyList(),
73- Collections .singleton(resourceLocation),
74- Collections .emptyMap(),
75- false
76- )
7729
78- user.sendPacket(showPacket )
79- user.sendPacket(hidePacket )
30+ user.sendPacket(buildShowPacket(resourceLocation, this ) )
31+ user.sendPacket(buildHidePacket(resourceLocation) )
8032 }
8133}
8234
8335private fun ToastStyle.toType () = when (this ) {
8436 ToastStyle .TASK -> AdvancementType .TASK
8537 ToastStyle .GOAL -> AdvancementType .GOAL
8638 ToastStyle .CHALLENGE -> AdvancementType .CHALLENGE
87- }
39+ }
40+
41+ private fun createFakeAdvancement (toast : Toast ): Advancement {
42+ val criterion: List <String > = Collections .singletonList(CRITERION_ID )
43+ return Advancement (
44+ null ,
45+ AdvancementDisplay (
46+ toast.text,
47+ buildText {
48+ error(" test" )
49+ },
50+ ItemStack .builder().type(toast.icon).build(),
51+ toast.style.toType(),
52+ null ,
53+ true ,
54+ false ,
55+ 0.0f ,
56+ 0.0f
57+ ),
58+ criterion,
59+ Collections .singletonList(criterion),
60+ false
61+ )
62+ }
63+
64+ private fun buildShowPacket (resourceLocation : ResourceLocation , toast : Toast ) =
65+ WrapperPlayServerUpdateAdvancements (
66+ false ,
67+ Collections .singletonList(
68+ AdvancementHolder (
69+ resourceLocation,
70+ createFakeAdvancement(toast)
71+ )
72+ ),
73+ Collections .emptySet(),
74+ Collections .singletonMap(
75+ resourceLocation, AdvancementProgress (
76+ mutableObject2ObjectMapOf(
77+ CRITERION_ID to AdvancementProgress .CriterionProgress (System .currentTimeMillis())
78+ )
79+ )
80+ ),
81+ true
82+ )
83+
84+ private fun buildHidePacket (resourceLocation : ResourceLocation ) =
85+ WrapperPlayServerUpdateAdvancements (
86+ false ,
87+ Collections .emptyList(),
88+ Collections .singleton(resourceLocation),
89+ Collections .emptyMap(),
90+ false
91+ )
0 commit comments