Skip to content

Commit

Permalink
Removed setter for Collection fields, using reflection now
Browse files Browse the repository at this point in the history
  • Loading branch information
Petersil1998 committed Jun 19, 2023
1 parent c4436c9 commit ffea98d
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static Challenge getChallenge(int id) {
public static List<Challenge> getChallenges() {
return challenges;
}

public static void setChallenges(List<Challenge> challenges) {
Challenges.challenges = challenges;
}
}
4 changes: 0 additions & 4 deletions src/main/java/net/petersil98/thresh/collection/Champions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,4 @@ public static Champion getChampionByName(String name){
public static List<Champion> getChampions() {
return champions;
}

public static void setChampions(List<Champion> champions) {
Champions.champions = champions;
}
}
4 changes: 0 additions & 4 deletions src/main/java/net/petersil98/thresh/collection/Items.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static Item getItem(int id){
public static List<Item> getItems() {
return items;
}

public static void setItems(List<Item> items) {
Items.items = items;
}
}
4 changes: 0 additions & 4 deletions src/main/java/net/petersil98/thresh/collection/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static Map getMap(int id){
public static List<Map> getMaps() {
return maps;
}

public static void setMaps(List<Map> maps) {
Maps.maps = maps;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static QueueType getQueueType(int id){
public static List<QueueType> getQueueTypes() {
return queueTypes;
}

public static void setQueueTypes(List<QueueType> queueTypes) {
QueueTypes.queueTypes = queueTypes;
}
}
4 changes: 0 additions & 4 deletions src/main/java/net/petersil98/thresh/collection/RuneStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static RuneStat getRuneStat(int id){
public static List<RuneStat> getRuneStats() {
return runeStats;
}

public static void setRuneStats(List<RuneStat> runeStats) {
RuneStats.runeStats = runeStats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static RuneStyle getRuneStyle(int id){
public static List<RuneStyle> getRuneStyles() {
return runeStyles;
}

public static void setRuneStyles(List<RuneStyle> runeStyles) {
RuneStyles.runeStyles = runeStyles;
}
}
4 changes: 0 additions & 4 deletions src/main/java/net/petersil98/thresh/collection/Runes.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static Rune getRune(int id){
public static List<Rune> getRunes() {
return runes;
}

public static void setRunes(List<Rune> runes) {
Runes.runes = runes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ public static SummonerSpell getSummonerSpell(int id){
public static List<SummonerSpell> getSummonerSpells() {
return summonerSpells;
}

public static void setSummonerSpells(List<SummonerSpell> summonerSpells) {
SummonerSpells.summonerSpells = summonerSpells;
}
}
33 changes: 24 additions & 9 deletions src/main/java/net/petersil98/thresh/util/LoLLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.petersil98.core.data.Sprite;
import net.petersil98.core.util.Loader;
import net.petersil98.core.util.settings.Settings;
import net.petersil98.thresh.Thresh;
import net.petersil98.thresh.collection.*;
import net.petersil98.thresh.data.Challenge;
import net.petersil98.thresh.data.Item;
Expand All @@ -21,6 +22,7 @@
import org.apache.logging.log4j.core.util.IOUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
Expand Down Expand Up @@ -109,7 +111,7 @@ private void loadRuneStyles(){
runeStyle.get("key").asText())
);
}
RuneStyles.setRuneStyles(runeStyles);
setFieldInCollection(RuneStyles.class, runeStyles);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -134,7 +136,7 @@ private void loadRunes(){
}
}
}
Runes.setRunes(runes);
setFieldInCollection(Runes.class, runes);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -175,7 +177,7 @@ private void loadRuneStats(){
runeStyle.get("longDesc").asText())
);
}
RuneStats.setRuneStats(runeStats);
setFieldInCollection(RuneStats.class, runeStats);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -211,7 +213,7 @@ private void loadMaps(){
sprite)
);
}
Maps.setMaps(maps);
setFieldInCollection(Maps.class, maps);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -229,7 +231,7 @@ private void updateChampionsFile(){
private void loadChampions(){
try {
String content = Files.readString(Paths.get(CHAMPIONS_FILE_PATH));
Champions.setChampions(MAPPER.readValue(content, TypeFactory.defaultInstance().constructCollectionType(List.class, Champion.class)));
setFieldInCollection(Champions.class, MAPPER.readValue(content, TypeFactory.defaultInstance().constructCollectionType(List.class, Champion.class)));
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -250,7 +252,7 @@ private void updateQueueTypesFile(){
private void loadQueueTypes(){
try {
String content = Files.readString(Paths.get(QUEUE_TYPES_FILE_PATH));
QueueTypes.setQueueTypes(MAPPER.readValue(content, TypeFactory.defaultInstance().constructCollectionType(List.class, QueueType.class)));
setFieldInCollection(QueueTypes.class, MAPPER.readValue(content, TypeFactory.defaultInstance().constructCollectionType(List.class, QueueType.class)));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -324,7 +326,7 @@ private void loadItems(){
ITEMS_SPECIAL_RECIPE.put(Integer.parseInt(entry.getKey()), node.get("specialRecipe").asInt());
}
}
Items.setItems(items);
setFieldInCollection(Items.class, items);
Items.getItems().forEach(Item::postInit);
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -385,7 +387,7 @@ private void loadSummonerSpells(){
)
);
}
SummonerSpells.setSummonerSpells(list);
setFieldInCollection(SummonerSpells.class, list);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -406,7 +408,7 @@ private void updateChallengesFile() {
private void loadChallenges(){
try {
String content = Files.readString(Paths.get(CHALLENGES_FILE_PATH));
Challenges.setChallenges(MAPPER.readValue(content, TypeFactory.defaultInstance().constructCollectionType(List.class, Challenge.class)));
setFieldInCollection(Challenges.class, MAPPER.readValue(content, TypeFactory.defaultInstance().constructCollectionType(List.class, Challenge.class)));
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -474,4 +476,17 @@ private void renameFieldInNode(ObjectNode node, String fieldName, String newFiel
node.set(newFieldName, node.get(fieldName));
node.remove(fieldName);
}

private void setFieldInCollection(Class<?> collectionClass, List<?> elements) {
try {
char[] fieldName = collectionClass.getSimpleName().toCharArray();
fieldName[0] += 32;
Field field = collectionClass.getDeclaredField(new String(fieldName));
field.setAccessible(true);
field.set(null, elements);
field.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
Thresh.LOGGER.error("Couldn't set collection Type of class " + collectionClass.getSimpleName(), e);
}
}
}

0 comments on commit ffea98d

Please sign in to comment.