Skip to content

Commit 1099d54

Browse files
committed
Enable gem extraction of skills
This also adds a 'source' piece of info to the applied skills on items so we can differentiate between gem skills and "base" skills
1 parent 4a255f3 commit 1099d54

3 files changed

Lines changed: 96 additions & 19 deletions

File tree

src/main/java/studio/magemonkey/divinity/hooks/external/FabledHook.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ public void run() {
253253
if (item == null) continue;
254254

255255
AbilityGenerator.updateNamespace(item);
256-
for (Map.Entry<String, Integer> entry : AbilityGenerator.getAbilities(item).entrySet()) {
256+
for (Map.Entry<String, AbilityGenerator.AbilityInfo> entry : AbilityGenerator.getAbilities(item).entrySet()) {
257257
String id = entry.getKey();
258-
int level = entry.getValue();
258+
int level = entry.getValue().getLevel();
259259
if (!skills.containsKey(id) || level > skills.get(id)) {
260260
skills.put(id, level);
261261
}

src/main/java/studio/magemonkey/divinity/modules/list/gems/GemManager.java

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ public ItemStack insertSocket(@NotNull ItemStack item, @NotNull ItemStack src) {
7979
return item;
8080
}
8181

82+
@Override
83+
@NotNull
84+
public List<ItemStack> extractSocket(@NotNull ItemStack target, @NotNull String socketId, int index) {
85+
List<ItemStack> items = super.extractSocket(target, socketId, index);
86+
Gem gem = this.getModuleItem(target);
87+
88+
if (gem == null) return items;
89+
90+
ItemStack result = items.get(0);
91+
int gemLevel = ItemStats.getLevel(target);
92+
gem.removeAbilities(result, gemLevel);
93+
94+
return items;
95+
}
96+
8297
// -------------------------------------------------------------------- //
8398
// CLASSES
8499

@@ -128,38 +143,73 @@ private void loadAbilities(@NotNull ConfigurationSection section, int lvl) {
128143
}
129144
}
130145

131-
public void applyAbilities(ItemStack item, int level) {
146+
private void applyAbilities(ItemStack item, int level) {
132147
List<AbilityGenerator.Ability> abilities =
133148
this.abilitiesByLevel.computeIfAbsent(level, k -> new ArrayList<>());
134149
if (abilities.isEmpty()) return;
135150

136151
// Start off with our old abilities and then apply the new ones on top
137-
Map<String, Integer> itemAbilities = AbilityGenerator.getAbilities(item);
138-
List<AbilityGenerator.Ability> abilitiesList = new ArrayList<>();
152+
Map<String, AbilityGenerator.AbilityInfo> itemAbilities = AbilityGenerator.getAbilities(item);
153+
List<AbilityGenerator.Ability> abilitiesList = new ArrayList<>();
139154
// Only add any abilities that aren't already on the item
140155
for (AbilityGenerator.Ability ability : abilities) {
141156
if (ability == null) continue;
142157

143158
String abilityId = ability.getId();
144159
if (!itemAbilities.containsKey(abilityId)) {
145-
itemAbilities.put(ability.getId(), ability.getRndLevel());
160+
itemAbilities.put(ability.getId(),
161+
new AbilityGenerator.AbilityInfo(ability.getId(), ability.getRndLevel(), "gem"));
146162
abilitiesList.add(ability);
147163
}
148164
}
149165

150166
// Apply the new abilities to the item
151167
int i = 0;
152168
String[] abilityArray = new String[itemAbilities.size()];
153-
for (Map.Entry<String, Integer> entry : itemAbilities.entrySet()) {
154-
abilityArray[i] = entry.getKey() + ":" + entry.getValue();
169+
for (Map.Entry<String, AbilityGenerator.AbilityInfo> entry : itemAbilities.entrySet()) {
170+
abilityArray[i] = entry.getKey() + ":" + entry.getValue() + ":" + entry.getValue().getSource();
171+
i++;
172+
}
173+
DataUT.setData(item, AbilityGenerator.ABILITY_KEY, abilityArray);
174+
updateItemLore(item, itemAbilities, abilitiesList);
175+
}
176+
177+
/**
178+
* Removes the abilities from the item that are associated with the gem at the given level.
179+
*
180+
* @param item The item to remove the abilities from.
181+
* @param level The level of the gem.
182+
*/
183+
private void removeAbilities(ItemStack item, int level) {
184+
List<AbilityGenerator.Ability> abilities = this.abilitiesByLevel.get(level);
185+
if (abilities == null) return;
186+
187+
Map<String, AbilityGenerator.AbilityInfo> itemAbilities = AbilityGenerator.getAbilities(item);
188+
if (itemAbilities.isEmpty()) return;
189+
190+
List<AbilityGenerator.Ability> abilitiesList = new ArrayList<>();
191+
for (AbilityGenerator.Ability ability : abilities) {
192+
if (ability == null) continue;
193+
194+
String abilityId = ability.getId();
195+
if (itemAbilities.containsKey(abilityId) && itemAbilities.get(abilityId).getSource().equals("gem")) {
196+
itemAbilities.remove(abilityId);
197+
}
198+
}
199+
200+
int i = 0;
201+
String[] abilityArray = new String[itemAbilities.size()];
202+
for (Map.Entry<String, AbilityGenerator.AbilityInfo> entry : itemAbilities.entrySet()) {
203+
abilityArray[i] =
204+
entry.getKey() + ":" + entry.getValue().getLevel() + ":" + entry.getValue().getSource();
155205
i++;
156206
}
157207
DataUT.setData(item, AbilityGenerator.ABILITY_KEY, abilityArray);
158208
updateItemLore(item, itemAbilities, abilitiesList);
159209
}
160210

161211
private void updateItemLore(ItemStack item,
162-
Map<String, Integer> itemAbilities,
212+
Map<String, AbilityGenerator.AbilityInfo> itemAbilities,
163213
List<AbilityGenerator.Ability> abilitiesList) {
164214
ItemGeneratorManager.GeneratorItem genItem = Divinity.getInstance().getModuleCache().getTierManager()
165215
.getModuleItem(item);

src/main/java/studio/magemonkey/divinity/modules/list/itemgenerator/generators/AbilityGenerator.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package studio.magemonkey.divinity.modules.list.itemgenerator.generators;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
36
import org.bukkit.NamespacedKey;
47
import org.bukkit.inventory.ItemStack;
58
import org.bukkit.inventory.meta.ItemMeta;
@@ -155,16 +158,16 @@ public void generate(@NotNull ItemStack item, int itemLevel) {
155158
int i = 0;
156159
String[] abilityArray = new String[abilityAdd.size()];
157160
for (Map.Entry<AbilityGenerator.Ability, Integer> entry : abilityAdd.entrySet()) {
158-
abilityArray[i] = entry.getKey().getId() + ':' + entry.getValue();
161+
abilityArray[i] = entry.getKey().getId() + ':' + entry.getValue() + ":item";
159162
i++;
160163
}
161164
DataUT.setData(item, ABILITY_KEY, abilityArray);
162165

163166
updateLore(item);
164167
}
165168

166-
public static Map<String, Integer> getAbilities(ItemStack item) {
167-
Map<String, Integer> map = new HashMap<>();
169+
public static Map<String, AbilityInfo> getAbilities(ItemStack item) {
170+
Map<String, AbilityInfo> map = new HashMap<>();
168171
if (item == null) {
169172
return map;
170173
}
@@ -173,20 +176,31 @@ public static Map<String, Integer> getAbilities(ItemStack item) {
173176
return map;
174177
}
175178
for (String stringAbility : stringAbilities) {
176-
int i = stringAbility.lastIndexOf(':');
177-
int level;
179+
AbilityInfo info = new AbilityInfo();
180+
String[] split = stringAbility.split(":");
181+
if (split.length < 2) {
182+
continue;
183+
}
184+
185+
info.setId(split[0]);
186+
178187
try {
179-
level = Integer.parseInt(stringAbility.substring(i + 1));
188+
info.setLevel(Integer.parseInt(split[1]));
180189
} catch (NumberFormatException e) {
181190
continue;
182191
}
183-
map.put(stringAbility.substring(0, i), level);
192+
193+
if (split.length > 2) {
194+
info.setSource(split[2]);
195+
}
196+
197+
map.put(info.getId(), info);
184198
}
185199
return map;
186200
}
187201

188202
public void updateLore(ItemStack item) {
189-
Map<String, Integer> abilities = getAbilities(item);
203+
Map<String, AbilityInfo> abilities = getAbilities(item);
190204
if (abilities.isEmpty()) return;
191205

192206
List<Ability> abilityList = this.abilities.keySet().stream()
@@ -196,7 +210,7 @@ public void updateLore(ItemStack item) {
196210
updateLore(item, abilities, abilityList);
197211
}
198212

199-
public static void updateLore(ItemStack item, Map<String, Integer> abilities, List<Ability> abilityList) {
213+
public static void updateLore(ItemStack item, Map<String, AbilityInfo> abilities, List<Ability> abilityList) {
200214
if (abilityList.isEmpty()) return;
201215

202216
// At this point, we have a list of Abilities, so we just need to get their lore formats and update the item's lore for them
@@ -234,7 +248,7 @@ public static void updateLore(ItemStack item, Map<String, Integer> abilities, Li
234248
} else lore.remove(pos); // Otherwise, we'll remove it so we can add new lines at the position
235249

236250
for (Ability ability : abilityList) {
237-
int level = abilities.get(ability.getId());
251+
int level = abilities.get(ability.getId()).getLevel();
238252
for (String format : ability.getLoreFormat()) {
239253
String loreLine = format.replace("%level%", String.valueOf(level));
240254
pos = LoreUT.addToLore(lore, pos, loreLine);
@@ -298,4 +312,17 @@ public boolean equals(Object o) {
298312
@Override
299313
public int hashCode() {return Objects.hash(id);}
300314
}
315+
316+
@Data
317+
@NoArgsConstructor
318+
@AllArgsConstructor
319+
public static class AbilityInfo {
320+
private String id;
321+
private int level;
322+
private String source;
323+
324+
public String getSource() {
325+
return Objects.requireNonNullElse(this.source, "item");
326+
}
327+
}
301328
}

0 commit comments

Comments
 (0)