Skip to content

Commit

Permalink
Implement functionality which allows to automatically deactivate olde…
Browse files Browse the repository at this point in the history
…st generator, if activate generator count is reached.
  • Loading branch information
BONNe committed Dec 5, 2020
1 parent b36e55a commit 90c1118
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@


/**
* Settings that implements ConfigObject is powerful and dynamic Config Objects that
* does not need custom parsing. If it is correctly loaded, all its values will be available.
*
* Settings that implements ConfigObject is powerful and dynamic Config Objects that does not need custom parsing. If it
* is correctly loaded, all its values will be available.
* <p>
* Without Getter and Setter this class will not work.
*
* To specify location for config object to be stored, you should use @StoreAt(filename="{config file name}", path="{Path to your addon}")
* To save comments in config file you should use @ConfigComment("{message}") that adds any message you want to be in file.
* <p>
* To specify location for config object to be stored, you should use @StoreAt(filename="{config file name}",
* path="{Path to your addon}") To save comments in config file you should use @ConfigComment("{message}") that adds any
* message you want to be in file.
*/
@StoreAt(filename="config.yml", path="addons/MagicCobblestoneGenerator")
@ConfigComment("MagicCobblestoneGenerator Configuration [version]")
Expand Down Expand Up @@ -437,6 +438,28 @@ public void setClickAction(GuiAction clickAction)
}


/**
* Is overwrite on active boolean.
*
* @return the boolean
*/
public boolean isOverwriteOnActive()
{
return overwriteOnActive;
}


/**
* Sets overwrite on active.
*
* @param overwriteOnActive the overwrite on active
*/
public void setOverwriteOnActive(boolean overwriteOnActive)
{
this.overwriteOnActive = overwriteOnActive;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -495,6 +518,13 @@ public enum GuiAction
@ConfigEntry(path = "default-active-generators")
private int defaultActiveGeneratorCount = 3;

@ConfigComment("")
@ConfigComment("Enabling this functionality will disable one of active generators if active generator limit")
@ConfigComment("is reached when clicking on a new generator.")
@ConfigComment("Useful for situations with one active generator, which will be changed upon activating next one.")
@ConfigEntry(path = "overwrite-on-activate")
private boolean overwriteOnActive = false;

@ConfigComment("")
@ConfigComment("Send a notification message when player unlocks a new generator.")
@ConfigComment("3 messages that will be showed:")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import world.bentobox.bentobox.database.objects.DataObject;
Expand Down Expand Up @@ -323,7 +324,7 @@ public int getActiveGeneratorCount()
* Stores currently active generator names.
*/
@Expose
private Set<String> activeGeneratorList = new HashSet<>();
private Set<String> activeGeneratorList = new LinkedHashSet<>();

/**
* Stores maximum allowed active generator count for island object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,11 +1024,18 @@ public void unlockGenerator(@NotNull GeneratorDataObject dataObject,
* @param user User who deactivates generator.
* @param generatorData Data which will be populated.
* @param generatorTier Generator that will be removed.
* @return {@code true} if deactivation was successful, {@code false} otherwise.
*/
public void deactivateGenerator(@NotNull User user,
public boolean deactivateGenerator(@NotNull User user,
@NotNull GeneratorDataObject generatorData,
@NotNull GeneratorTierObject generatorTier)
{
// If generator is not active, do nothing.
if (!generatorData.getActiveGeneratorList().contains(generatorTier.getUniqueId()))
{
return false;
}

// Call event about generator activation.
GeneratorActivationEvent event = new GeneratorActivationEvent(generatorTier,
user,
Expand All @@ -1045,6 +1052,12 @@ public void deactivateGenerator(@NotNull User user,

// Save object.
this.saveGeneratorData(generatorData);
return true;
}
else
{
// Generator is not deactivated.
return false;
}
}

Expand All @@ -1065,10 +1078,13 @@ public boolean canActivateGenerator(@NotNull User user,
if (generatorData.getActiveGeneratorCount() > 0 &&
generatorData.getActiveGeneratorList().size() >= generatorData.getActiveGeneratorCount())
{
// Too many generators.
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "active-generators-reached"));
return false;
if (!this.addon.getSettings().isOverwriteOnActive())
{
// Too many generators.
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "active-generators-reached"));
return false;
}
}

if (!generatorData.getUnlockedTiers().contains(generatorTier.getUniqueId()))
Expand Down Expand Up @@ -1130,6 +1146,22 @@ public void activateGenerator(@NotNull User user,
@NotNull GeneratorDataObject generatorData,
@NotNull GeneratorTierObject generatorTier)
{
if (this.addon.getSettings().isOverwriteOnActive() &&
generatorData.getActiveGeneratorCount() > 0 &&
generatorData.getActiveGeneratorList().size() >= generatorData.getActiveGeneratorCount())
{
// Try to deactivate first generator, because overwrite is active.
GeneratorTierObject oldGenerator =
this.getGeneratorByID(generatorData.getActiveGeneratorList().iterator().next());

if (!this.deactivateGenerator(user, generatorData, oldGenerator))
{
// If deactivation was not successful, send message about reached limit.
Utils.sendMessage(user,
user.getTranslation(Constants.MESSAGES + "active-generators-reached"));
}
}

// Call event about generator activation.
GeneratorActivationEvent event = new GeneratorActivationEvent(generatorTier,
user,
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ working-range: 0
# Can be changed with a permission `[gamemode].stone-generator.active-generators.[number]`.
default-active-generators: 3
#
# Enabling this functionality will disable one of active generators if active generator limit
# is reached when clicking on a new generator.
# Useful for situations with one active generator, which will be changed upon activating next one.
overwrite-on-activate: false
#
# Send a notification message when player unlocks a new generator.
# 3 messages that will be showed:
# stone-generator.conversations.click-text-to-purchase - if generator is unlocked but is not purchased.
Expand Down

0 comments on commit 90c1118

Please sign in to comment.