Skip to content

Commit

Permalink
Add some methods for easier working with BiomesIslandDataObject
Browse files Browse the repository at this point in the history
  • Loading branch information
BONNe committed Jan 17, 2022
1 parent ca228b6 commit c070b91
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
Expand Down Expand Up @@ -144,9 +145,9 @@ public void setPurchasedBiomes(Set<String> purchasedBiomes)
*
* @return the biome change counter
*/
public Map<String, Integer> getBiomeChangeCounter()
public Map<String, AtomicInteger> getBiomeChangeCounter()
{
return biomeChangeCounter;
return this.biomeChangeCounter;
}


Expand All @@ -155,12 +156,87 @@ public Map<String, Integer> getBiomeChangeCounter()
*
* @param biomeChangeCounter the biome change counter
*/
public void setBiomeChangeCounter(Map<String, Integer> biomeChangeCounter)
public void setBiomeChangeCounter(Map<String, AtomicInteger> biomeChangeCounter)
{
this.biomeChangeCounter = biomeChangeCounter;
}


// ---------------------------------------------------------------------
// Section: Useful methods
// ---------------------------------------------------------------------


/**
* Gets biome change counter.
*
* @param biomeObjectId the biome object id
* @return the biome change counter
*/
public int getBiomeChangeCounter(String biomeObjectId)
{
return this.biomeChangeCounter.getOrDefault(biomeObjectId, dummy).get();
}


/**
* Increase biome change counter.
*
* @param biomeObjectId the biome object id
*/
public void increaseBiomeChangeCounter(String biomeObjectId)
{
this.biomeChangeCounter.computeIfAbsent(biomeObjectId,
id -> new AtomicInteger(0)).incrementAndGet();
}


/**
* Is unlocked biome.
*
* @param biomeObjectId the biome object id
* @return the boolean
*/
public boolean isUnlocked(String biomeObjectId)
{
return this.unlockedBiomes.contains(biomeObjectId);
}


/**
* Is purchased biome.
*
* @param biomeObjectId the biome object id
* @return the boolean
*/
public boolean isPurchased(String biomeObjectId)
{
return this.purchasedBiomes.contains(biomeObjectId);
}


/**
* Unlock biome.
*
* @param biomeObjectId the biome object id
*/
public void unlockBiome(String biomeObjectId)
{
this.unlockedBiomes.add(biomeObjectId);
}


/**
* Purchase biome.
*
* @param biomeObjectId the biome object id
*/
public void purchaseBiome(String biomeObjectId)
{
this.purchasedBiomes.add(biomeObjectId);
}


/**
* This method clears island data for this island.
*/
Expand All @@ -178,6 +254,11 @@ public void clear()
// ---------------------------------------------------------------------


/**
* The constant dummy for non-existing values.
*/
private static final AtomicInteger dummy = new AtomicInteger(0);

/**
* Unique ID of the island.
*/
Expand Down Expand Up @@ -214,5 +295,5 @@ public void clear()
* Stores map that links biome with how many times it is updated.
*/
@Expose
private Map<String, Integer> biomeChangeCounter = new HashMap<>();
private Map<String, AtomicInteger> biomeChangeCounter = new ConcurrentHashMap<>();
}
8 changes: 4 additions & 4 deletions src/main/java/world/bentobox/biomes/panels/CommonPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ private String generateChangePrice(BiomesObject biome, @Nullable BiomesIslandDat
if (target != null &&
BiomesObject.CostMode.PER_USAGE.equals(biome.getCostMode()) &&
biome.getCostIncrement() > 0 &&
target.getBiomeChangeCounter().containsKey(biome.getUniqueId()))
target.getBiomeChangeCounter(biome.getUniqueId()) > 0)
{
int counter = target.getBiomeChangeCounter().get(biome.getUniqueId());
int counter = target.getBiomeChangeCounter(biome.getUniqueId());

cost = cost + biome.getCostIncrement() * cost * counter;
}
Expand Down Expand Up @@ -382,9 +382,9 @@ private String generateChangeItemPrice(BiomesObject biome, @Nullable BiomesIslan
if (target != null &&
BiomesObject.CostMode.PER_USAGE.equals(biome.getCostMode()) &&
biome.getCostIncrement() > 0 &&
target.getBiomeChangeCounter().containsKey(biome.getUniqueId()))
target.getBiomeChangeCounter(biome.getUniqueId()) > 0)
{
increment = biome.getCostIncrement() * target.getBiomeChangeCounter().get(biome.getUniqueId());
increment = biome.getCostIncrement() * target.getBiomeChangeCounter(biome.getUniqueId());
}
else
{
Expand Down

0 comments on commit c070b91

Please sign in to comment.