Skip to content

Commit

Permalink
Move to dependency injection and stop allocating tons of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed Feb 14, 2021
1 parent 7955fd1 commit 92218ac
Show file tree
Hide file tree
Showing 76 changed files with 1,388 additions and 1,412 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@

public class ChallengesCreator {

private final VSkyblock plugin = VSkyblock.getInstance();
private final VSkyblock plugin;

private final String ANSI_RED = "\u001B[31m";
private final String ANSI_RESET = "\u001B[0m";
public ChallengesCreator(VSkyblock plugin) {
this.plugin = plugin;
}

public boolean createAllChallenges() {
if (createChallenges(Challenge.Difficulty.EASY)) {
Expand Down Expand Up @@ -91,16 +92,16 @@ public boolean createChallenges(Challenge.Difficulty diff) {
challenge.setRewards(createItems(rewards));
challenge.setRepeatRewards(createItems(repeatRewards));
if (isChallengeValid(challenge)) {
if (!ChallengesHandler.challenges.containsKey(challenge.getMySQLKey())) {
ChallengesHandler.challenges.put(challenge.getMySQLKey(), challenge);
if (!ChallengesManager.challenges.containsKey(challenge.getMySQLKey())) {
ChallengesManager.challenges.put(challenge.getMySQLKey(), challenge);
switch (diff) {
case EASY:
if (slotsEasy.get(challenge.getSlot()) != null) {
while (slotsEasy.get(challenge.getSlot()) != null) {
challenge.setSlot(challenge.getSlot() + 1);
}
}
ChallengesHandler.challengesEasy.put(challengeName, challenge);
ChallengesManager.challengesEasy.put(challengeName, challenge);
slotsEasy.put(challenge.getSlot(), challenge);
if (challenge.getSlot() > highestSlot) {
highestSlot = challenge.getSlot();
Expand All @@ -112,7 +113,7 @@ public boolean createChallenges(Challenge.Difficulty diff) {
challenge.setSlot(challenge.getSlot() + 1);
}
}
ChallengesHandler.challengesMedium.put(challengeName, challenge);
ChallengesManager.challengesMedium.put(challengeName, challenge);
slotsMedium.put(challenge.getSlot(), challenge);
if (challenge.getSlot() > highestSlot) {
highestSlot = challenge.getSlot();
Expand All @@ -124,7 +125,7 @@ public boolean createChallenges(Challenge.Difficulty diff) {
challenge.setSlot(challenge.getSlot() + 1);
}
}
ChallengesHandler.challengesHard.put(challengeName, challenge);
ChallengesManager.challengesHard.put(challengeName, challenge);
slotsHard.put(challenge.getSlot(), challenge);
if (challenge.getSlot() > highestSlot) {
highestSlot = challenge.getSlot();
Expand All @@ -146,13 +147,13 @@ private boolean sortChallenges(Challenge.Difficulty difficulty, int highestSlot)
Map<String, Challenge> challengeHashMap;
switch (difficulty) {
case EASY:
challengeHashMap = ChallengesHandler.challengesEasy;
challengeHashMap = ChallengesManager.challengesEasy;
break;
case MEDIUM:
challengeHashMap = ChallengesHandler.challengesMedium;
challengeHashMap = ChallengesManager.challengesMedium;
break;
case HARD:
challengeHashMap = ChallengesHandler.challengesHard;
challengeHashMap = ChallengesManager.challengesHard;
break;
default:
throw new IllegalStateException("Unexpected value: " + difficulty + " while sorting challenges!");
Expand All @@ -164,15 +165,15 @@ private boolean sortChallenges(Challenge.Difficulty difficulty, int highestSlot)
boolean b;
switch (difficulty) {
case EASY:
ChallengesHandler.sortedChallengesEasy = sortedChallenges;
ChallengesManager.sortedChallengesEasy = sortedChallenges;
b = true;
break;
case MEDIUM:
ChallengesHandler.sortedChallengesMedium = sortedChallenges;
ChallengesManager.sortedChallengesMedium = sortedChallenges;
b = true;
break;
case HARD:
ChallengesHandler.sortedChallengesHard = sortedChallenges;
ChallengesManager.sortedChallengesHard = sortedChallenges;
b = true;
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/

import com.github.Viduality.VSkyblock.Utilitys.ConfigShorts;
import com.github.Viduality.VSkyblock.Utilitys.DatabaseReader;
import com.github.Viduality.VSkyblock.VSkyblock;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand All @@ -35,10 +34,11 @@

import java.util.*;

public class CreateChallengesInventory {
public class ChallengesInventoryCreator {

public static final NamespacedKey CHALLENGE_KEY = new NamespacedKey(VSkyblock.getInstance(), "challengeid");
private DatabaseReader databaseReader = new DatabaseReader();

private final VSkyblock plugin;

public static final String descriptioncolor = ConfigShorts.getChallengesConfig().getString("ItemOverlay.DescriptionColor");
private final String challengeNameColor = ConfigShorts.getChallengesConfig().getString("ItemOverlay.ChallengeNameColor");
Expand All @@ -55,6 +55,10 @@ public class CreateChallengesInventory {
private final String mineasycomp = ConfigShorts.getChallengesConfig().getString("CompletedChallenges.MinEasyCompleted");
private final String minmediumcomp = ConfigShorts.getChallengesConfig().getString("CompletedChallenges.MinMediumCompleted");

public ChallengesInventoryCreator(VSkyblock plugin) {
this.plugin = plugin;
}

/**
* Creates an inventory for up to 18 challenges for the given difficulty.
*
Expand All @@ -63,7 +67,7 @@ public class CreateChallengesInventory {
* @param site The site of the challenges difficulty.
*/
public void createChallenges(Player player, Challenge.Difficulty difficulty, int site) {
databaseReader.getislandidfromplayer(player.getUniqueId(), (islandid) -> databaseReader.getIslandChallenges(islandid, (islandChallenges) -> {
plugin.getDb().getReader().getislandidfromplayer(player.getUniqueId(), (islandid) -> plugin.getDb().getReader().getIslandChallenges(islandid, (islandChallenges) -> {
Inventory cinv = Bukkit.createInventory(null, 27, "Challenges " + ConfigShorts.getChallengesConfig().getString("Difficulty." + getDifficulty(difficulty)));
if (getChallenges(difficulty) != null) {
if (!getChallenges(difficulty).isEmpty()) {
Expand Down Expand Up @@ -174,18 +178,18 @@ private Challenge getChallenge(Challenge.Difficulty difficulty, int slot) {
int slot1 = slot - 1;
switch (difficulty) {
case EASY:
if (ChallengesHandler.sortedChallengesEasy.size() >= slot) {
return ChallengesHandler.sortedChallengesEasy.get(slot1);
if (ChallengesManager.sortedChallengesEasy.size() >= slot) {
return ChallengesManager.sortedChallengesEasy.get(slot1);
}
break;
case MEDIUM:
if (ChallengesHandler.sortedChallengesMedium.size() >= slot) {
return ChallengesHandler.sortedChallengesMedium.get(slot1);
if (ChallengesManager.sortedChallengesMedium.size() >= slot) {
return ChallengesManager.sortedChallengesMedium.get(slot1);
}
break;
case HARD:
if (ChallengesHandler.sortedChallengesHard.size() >= slot) {
return ChallengesHandler.sortedChallengesHard.get(slot1);
if (ChallengesManager.sortedChallengesHard.size() >= slot) {
return ChallengesManager.sortedChallengesHard.get(slot1);
}
break;
}
Expand Down Expand Up @@ -282,9 +286,9 @@ public ItemStack createChallengeItem(Challenge challenge, int challengeCount, bo
*/
private Map<String, Challenge> getChallenges(Challenge.Difficulty difficulty) {
switch (difficulty) {
case EASY: return ChallengesHandler.challengesEasy;
case MEDIUM: return ChallengesHandler.challengesMedium;
case HARD: return ChallengesHandler.challengesHard;
case EASY: return ChallengesManager.challengesEasy;
case MEDIUM: return ChallengesManager.challengesMedium;
case HARD: return ChallengesManager.challengesHard;
default: return null;
}
}
Expand Down Expand Up @@ -312,9 +316,9 @@ private String getDifficulty(Challenge.Difficulty difficulty) {
*/
private int getChallengeListSize(Challenge.Difficulty difficulty) {
switch (difficulty) {
case EASY: return ChallengesHandler.sortedChallengesEasy.size();
case MEDIUM: return ChallengesHandler.sortedChallengesMedium.size();
case HARD: return ChallengesHandler.sortedChallengesHard.size();
case EASY: return ChallengesManager.sortedChallengesEasy.size();
case MEDIUM: return ChallengesManager.sortedChallengesMedium.size();
case HARD: return ChallengesManager.sortedChallengesHard.size();
default: return 0;
}
}
Expand Down
Loading

0 comments on commit 92218ac

Please sign in to comment.