Skip to content

Commit

Permalink
Fixes #72
Browse files Browse the repository at this point in the history
The issue was with object mapping. I wanted to be smarter then code allowed, and mapped actual generator tier objects inside data object. Off course, when we completely remove and create objects from scratch... it cannot keep this map correct.
  • Loading branch information
BONNe committed Oct 6, 2020
1 parent bd0c790 commit 1982e49
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,112 +67,88 @@ public void onEnable()

// Init new Generator Manager
this.stoneGeneratorManager = new StoneGeneratorManager(this);
this.stoneGeneratorManager.load();

this.getPlugin().getAddonsManager().getGameModeAddons().stream().
filter(gameMode -> !settings.getDisabledGameModes().contains(gameMode.getDescription().getName())).
forEach(gameMode -> {
if (gameMode.getPlayerCommand().isPresent())
{
// Add Placeholders
this.registerPlaceholders(gameMode);

// Add GameMode worlds to Generator.
this.stoneGeneratorManager.addWorld(gameMode.getOverWorld());

if (gameMode.getWorldSettings().isNetherIslands())
{
this.stoneGeneratorManager.addWorld(gameMode.getNetherWorld());
}

if (gameMode.getWorldSettings().isEndIslands())
{
this.stoneGeneratorManager.addWorld(gameMode.getEndWorld());
}

MAGIC_COBBLESTONE_GENERATOR.addGameModeAddon(gameMode);
MAGIC_COBBLESTONE_GENERATOR_PERMISSION.addGameModeAddon(gameMode);

// Hook commands
gameMode.getPlayerCommand().ifPresent(playerCommand ->
new GeneratorPlayerCommand(this, playerCommand));
gameMode.getAdminCommand().ifPresent(adminCommand ->
new GeneratorAdminCommand(this, adminCommand));

// if gamemode does not have any data, load them from template.
if (this.stoneGeneratorManager.getAllGeneratorTiers(gameMode.getOverWorld()).isEmpty())
{
this.stoneGeneratorImportManager.importFile(null, gameMode.getOverWorld());
}

this.hooked = true;
}
});
forEach(this::hookIntoGameMode);

if (this.hooked)
{
this.generator = new MagicGenerator(this);

// Try to find Level addon and if it does not exist, display a warning
this.setupAddon();
}
else
{
this.logError("Magic Cobblestone Generator could not hook into any GameMode so will not do anything!");
this.setState(State.DISABLED);
}
}

Optional<Addon> level = this.getAddonByName("Level");

if (!level.isPresent())
{
this.logWarning("Level add-on not found so Magic Cobblestone Generator, some parts may not work!");
this.levelAddon = null;
}
else
{
this.levelAddon = (Level) level.get();
}
/**
* Sets up everything once the addon is hooked into Game Modes
*/
private void setupAddon()
{
this.generator = new MagicGenerator(this);

// Find upgrades addon
this.findLevel();
this.findVault();

Optional<Addon> upgrades = this.getAddonByName("Upgrades");
// Register the listener.
this.registerListener(new VanillaGeneratorListener(this));
// TODO: fix and implement
//this.registerListener(new MagicGeneratorListener(this));

if (!upgrades.isPresent())
{
// this.logWarning("Upgrades add-on not found so Magic Cobblestone Generator, some parts may not work!");
this.upgradesAddon = null;
}
else
{
this.upgradesAddon = (UpgradesAddon) upgrades.get();
}
this.registerListener(new JoinLeaveListener(this));

// Find vault plugin
// Register Flags
this.registerFlag(MAGIC_COBBLESTONE_GENERATOR);
this.registerFlag(MAGIC_COBBLESTONE_GENERATOR_PERMISSION);

Optional<VaultHook> vault = this.getPlugin().getVault();
// Register Request Handlers
// this.registerRequestHandler(REQUEST_HANDLER);
}

if (!vault.isPresent())
{
this.vaultHook = null;
this.logWarning("Vault plugin not found. Economy will not work!");
}
else
{
this.vaultHook = vault.get();
}

// Register the listener.
this.registerListener(new VanillaGeneratorListener(this));
// TODO: fix and implement
//this.registerListener(new MagicGeneratorListener(this));
/**
* This method hooks this addon into gamemode.
* @param gameMode GameModeAddon where need to be hooked.
*/
private void hookIntoGameMode(GameModeAddon gameMode)
{
// Add Placeholders
this.registerPlaceholders(gameMode);

this.registerListener(new JoinLeaveListener(this));
// Add GameMode worlds to Generator.
this.stoneGeneratorManager.addWorld(gameMode.getOverWorld());

// Register Flags
this.registerFlag(MAGIC_COBBLESTONE_GENERATOR);
this.registerFlag(MAGIC_COBBLESTONE_GENERATOR_PERMISSION);
if (gameMode.getWorldSettings().isNetherIslands())
{
this.stoneGeneratorManager.addWorld(gameMode.getNetherWorld());
}

// Register Request Handlers
// this.registerRequestHandler(REQUEST_HANDLER);
if (gameMode.getWorldSettings().isEndIslands())
{
this.stoneGeneratorManager.addWorld(gameMode.getEndWorld());
}
else

MAGIC_COBBLESTONE_GENERATOR.addGameModeAddon(gameMode);
MAGIC_COBBLESTONE_GENERATOR_PERMISSION.addGameModeAddon(gameMode);

// Hook commands
gameMode.getPlayerCommand().ifPresent(playerCommand ->
new GeneratorPlayerCommand(this, playerCommand));
gameMode.getAdminCommand().ifPresent(adminCommand ->
new GeneratorAdminCommand(this, adminCommand));

// if gamemode does not have any data, load them from template.
if (this.stoneGeneratorManager.getAllGeneratorTiers(gameMode.getOverWorld()).isEmpty())
{
this.logError("Magic Cobblestone Generator could not hook into any GameMode so will not do anything!");
this.setState(State.DISABLED);
this.stoneGeneratorImportManager.importFile(null, gameMode.getOverWorld());
}

this.hooked = true;
}


Expand Down Expand Up @@ -214,8 +190,9 @@ private void registerPlaceholders(GameModeAddon addon)
{
StringBuilder stringBuilder = new StringBuilder();

object.getActiveGeneratorList().forEach(generator ->
stringBuilder.append(generator.getFriendlyName()).append(","));
object.getActiveGeneratorList().stream().
map(this.stoneGeneratorManager::getGeneratorByID).
forEach(generator -> stringBuilder.append(generator.getFriendlyName()).append(","));

if (stringBuilder.length() > 0)
{
Expand All @@ -240,8 +217,10 @@ private void registerPlaceholders(GameModeAddon addon)
{
StringBuilder stringBuilder = new StringBuilder();

object.getUnlockedTiers().stream().sorted().forEach(generator ->
stringBuilder.append(generator.getFriendlyName()).append(","));
object.getUnlockedTiers().stream().
sorted().
map(this.stoneGeneratorManager::getGeneratorByID).
forEach(generator -> stringBuilder.append(generator.getFriendlyName()).append(","));

if (stringBuilder.length() > 0)
{
Expand All @@ -266,8 +245,10 @@ private void registerPlaceholders(GameModeAddon addon)
{
StringBuilder stringBuilder = new StringBuilder();

object.getPurchasedTiers().stream().sorted().forEach(generator ->
stringBuilder.append(generator.getFriendlyName()).append(","));
object.getPurchasedTiers().stream().
sorted().
map(this.stoneGeneratorManager::getGeneratorByID).
forEach(generator -> stringBuilder.append(generator.getFriendlyName()).append(","));

if (stringBuilder.length() > 0)
{
Expand All @@ -284,6 +265,50 @@ private void registerPlaceholders(GameModeAddon addon)
}


/**
* This is silly method that was introduced to reduce main method complexity, and just reports
* if economy is enabled or not.
*/
private void findVault()
{
// Find vault plugin

Optional<VaultHook> vault = this.getPlugin().getVault();

if (!vault.isPresent())
{
this.vaultHook = null;
this.logWarning("Vault plugin not found. Economy will not work!");
}
else
{
this.vaultHook = vault.get();
}
}


/**
* This is silly method that was introduced to reduce main method complexity, and just reports
* if level addon is enabled or not.
*/
private void findLevel()
{
// Try to find Level addon and if it does not exist, display a warning

Optional<Addon> level = this.getAddonByName("Level");

if (!level.isPresent())
{
this.logWarning("Level add-on not found so Magic Cobblestone Generator, some parts may not work!");
this.levelAddon = null;
}
else
{
this.levelAddon = (Level) level.get();
}
}


/**
* Executes code when disabling the addon.
*/
Expand Down Expand Up @@ -421,28 +446,6 @@ public VaultHook getVaultHook()
}


/**
* This method returns the upgradesAddon object.
*
* @return the upgradesAddon object.
*/
public UpgradesAddon getUpgradesAddon()
{
return this.upgradesAddon;
}


/**
* This method returns the upgradesAddon object.
*
* @return the upgradesAddon object exists.
*/
public boolean isUpgradesProvided()
{
return upgradesAddon != null;
}


/**
* This method allows to access static addon instance.
* @return Addon instance.
Expand Down Expand Up @@ -492,11 +495,6 @@ public static StoneGeneratorAddon getInstance()
*/
private Level levelAddon;

/**
* Upgrades addon.
*/
private UpgradesAddon upgradesAddon;


/**
* Static addon isntance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@


import com.google.gson.annotations.Expose;
import com.google.gson.annotations.JsonAdapter;

import java.util.HashSet;
import java.util.Set;

import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
import world.bentobox.magiccobblestonegenerator.database.adapters.GeneratorTierAdapter;


/**
Expand Down Expand Up @@ -64,7 +62,7 @@ public void setUniqueId(String uniqueId)
*
* @return the unlockedTiers (type Set<String>) of this object.
*/
public Set<GeneratorTierObject> getUnlockedTiers()
public Set<String> getUnlockedTiers()
{
return unlockedTiers;
}
Expand All @@ -75,7 +73,7 @@ public Set<GeneratorTierObject> getUnlockedTiers()
* @param unlockedTiers new value for this object.
*
*/
public void setUnlockedTiers(Set<GeneratorTierObject> unlockedTiers)
public void setUnlockedTiers(Set<String> unlockedTiers)
{
this.unlockedTiers = unlockedTiers;
}
Expand All @@ -86,7 +84,7 @@ public void setUnlockedTiers(Set<GeneratorTierObject> unlockedTiers)
*
* @return the activeGeneratorList (type Set<String>) of this object.
*/
public Set<GeneratorTierObject> getActiveGeneratorList()
public Set<String> getActiveGeneratorList()
{
return activeGeneratorList;
}
Expand All @@ -97,7 +95,7 @@ public Set<GeneratorTierObject> getActiveGeneratorList()
* @param activeGeneratorList new value for this object.
*
*/
public void setActiveGeneratorList(Set<GeneratorTierObject> activeGeneratorList)
public void setActiveGeneratorList(Set<String> activeGeneratorList)
{
this.activeGeneratorList = activeGeneratorList;
}
Expand Down Expand Up @@ -128,7 +126,7 @@ public void setMaxGeneratorCount(int maxGeneratorCount)
* This method returns the purchasedTiers value.
* @return the value of purchasedTiers.
*/
public Set<GeneratorTierObject> getPurchasedTiers()
public Set<String> getPurchasedTiers()
{
return purchasedTiers;
}
Expand All @@ -139,7 +137,7 @@ public Set<GeneratorTierObject> getPurchasedTiers()
* @param purchasedTiers the purchasedTiers new value.
*
*/
public void setPurchasedTiers(Set<GeneratorTierObject> purchasedTiers)
public void setPurchasedTiers(Set<String> purchasedTiers)
{
this.purchasedTiers = purchasedTiers;
}
Expand Down Expand Up @@ -201,22 +199,19 @@ public int getWorkingRange()
* Stores a names of unlocked generator tiers.
*/
@Expose
@JsonAdapter(GeneratorTierAdapter.class)
private Set<GeneratorTierObject> unlockedTiers = new HashSet<>();
private Set<String> unlockedTiers = new HashSet<>();

/**
* Stores a names of unlocked purchased tiers.
*/
@Expose
@JsonAdapter(GeneratorTierAdapter.class)
private Set<GeneratorTierObject> purchasedTiers = new HashSet<>();
private Set<String> purchasedTiers = new HashSet<>();

/**
* Stores currently active generator names.
*/
@Expose
@JsonAdapter(GeneratorTierAdapter.class)
private Set<GeneratorTierObject> activeGeneratorList = new HashSet<>();
private Set<String> activeGeneratorList = new HashSet<>();

/**
* Stores amount of maximal active generators at the same time.
Expand Down

0 comments on commit 1982e49

Please sign in to comment.