33import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
44import lombok .Getter ;
55import net .swofty .commons .skyblock .item .ItemType ;
6+ import net .swofty .commons .skyblock .item .Rarity ;
67import net .swofty .commons .skyblock .item .UnderstandableSkyBlockItem ;
78import net .swofty .commons .protocol .Serializer ;
89import net .swofty .commons .protocol .serializers .UnderstandableSkyBlockItemSerializer ;
910import net .swofty .type .skyblockgeneric .data .SkyBlockDatapoint ;
1011import net .swofty .type .skyblockgeneric .entity .PetEntityImpl ;
1112import net .swofty .type .skyblockgeneric .item .SkyBlockItem ;
13+ import net .swofty .type .skyblockgeneric .item .components .PetComponent ;
1214import net .swofty .type .skyblockgeneric .item .components .SkullHeadComponent ;
15+ import net .swofty .type .skyblockgeneric .item .handlers .pet .PetHandler ;
16+ import net .swofty .type .skyblockgeneric .item .handlers .pet .abstr .AbilityRuntime ;
17+ import net .swofty .type .skyblockgeneric .item .handlers .pet .abstr .PetAbility ;
18+ import net .swofty .type .skyblockgeneric .item .handlers .pet .abstr .PetEvent ;
1319import net .swofty .type .skyblockgeneric .user .SkyBlockPlayer ;
1420import org .jetbrains .annotations .Nullable ;
1521import org .json .JSONObject ;
1622
1723import java .util .HashMap ;
24+ import java .util .List ;
25+ import java .util .Map ;
1826
1927public class DatapointPetData extends SkyBlockDatapoint <DatapointPetData .UserPetData > {
2028 private static final Serializer serializer = new Serializer <UserPetData >() {
@@ -64,6 +72,8 @@ public DatapointPetData(String key) {
6472 public static class UserPetData {
6573 private HashMap <SkyBlockItem , Boolean > petsMap ;
6674 private PetEntityImpl enabledPetEntityImpl = null ;
75+ private transient List <PetAbility > cachedAbilities ;
76+ private final transient Map <PetAbility , AbilityRuntime > abilityRuntimes = new HashMap <>();
6777
6878 public UserPetData () {
6979 this .petsMap = new HashMap <>();
@@ -79,13 +89,15 @@ public void addPet(SkyBlockItem pet) {
7989
8090 public void setEnabled (ItemType type , boolean enabled ) {
8191 // Set all previous true pets to false
82- petsMap .keySet ().stream (). filter ( pet -> pet . getAttributeHandler (). getPotentialType () == type ). forEach (pet -> petsMap .put (pet , false ));
92+ petsMap .keySet ().forEach (pet -> petsMap .put (pet , false ));
8393
8494 // Set the new pet to the new state
8595 petsMap .keySet ().stream ().filter (pet -> pet .getAttributeHandler ().getPotentialType () == type ).findFirst ().ifPresent (pet -> petsMap .put (pet , enabled ));
8696
8797 if (enabledPetEntityImpl != null )
8898 enabledPetEntityImpl .remove ();
99+
100+ refreshCachedAbilities ();
89101 }
90102
91103 public void updatePetEntityImpl (SkyBlockPlayer player ) {
@@ -108,12 +120,45 @@ public void isEnabled(ItemType type) {
108120
109121 public void deselectCurrent () {
110122 petsMap .keySet ().forEach (pet -> petsMap .put (pet , false ));
123+ this .cachedAbilities = null ;
111124 }
112125
113126 public @ Nullable SkyBlockItem getEnabledPet () {
114127 return petsMap .keySet ().stream ().filter (petsMap ::get ).findFirst ().orElse (null );
115128 }
116129
130+ public List <PetAbility > getCachedAbilities (SkyBlockItem pet ) {
131+ if (cachedAbilities == null ) {
132+ PetComponent component = pet .getComponent (PetComponent .class );
133+ cachedAbilities = PetHandler .valueOf (component .getHandlerId ().toUpperCase ()).getAbilities (pet );
134+ }
135+ return cachedAbilities ;
136+ }
137+
138+ public AbilityRuntime getAbilityRuntime (PetAbility ability ) {
139+ return abilityRuntimes .computeIfAbsent (ability , _ -> new AbilityRuntime ());
140+ }
141+
142+ public <E extends PetEvent > E dispatch (E event ) {
143+ SkyBlockItem pet = getEnabledPet ();
144+ if (pet == null ) return event ;
145+ for (PetAbility ability : getCachedAbilities (pet )) {
146+ ability .onEvent (event );
147+ }
148+ return event ;
149+ }
150+
151+ // TODO: need to be called by Tier Boost
152+ public void refreshCachedAbilities () {
153+ SkyBlockItem activePet = getEnabledPet ();
154+ if (activePet != null ) {
155+ PetComponent component = activePet .getComponent (PetComponent .class );
156+ this .cachedAbilities = PetHandler .valueOf (component .getHandlerId ().toUpperCase ()).getAbilities (activePet );
157+ } else {
158+ this .cachedAbilities = null ;
159+ }
160+ }
161+
117162 public @ Nullable SkyBlockItem getPet (ItemType type ) {
118163 return petsMap .keySet ().stream ().filter (pet -> pet .getAttributeHandler ().getPotentialType () == type ).findFirst ().orElse (null );
119164 }
0 commit comments