Skip to content

Commit

Permalink
Fixes gamemode setting bug introduced by refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 18, 2019
1 parent 4ec0835 commit f3a3460
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.eclipse.jdt.annotation.NonNull;

import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon;

Expand All @@ -22,7 +24,7 @@
*/
public class Settings
{


/**
* Inits Settings file. Use custom YAML parsing in init.
Expand All @@ -41,62 +43,60 @@ public Settings(StoneGeneratorAddon addon)
if (addon.getConfig().isSet("tiers"))
{
ConfigurationSection section = addon.getConfig().getConfigurationSection("tiers");

addSection(section);
for (String key : Objects.requireNonNull(section).getKeys(false))
{
this.generatorTierMap.put(key, addSection(section, key));
}
}

// Reads GameMode specific generator tiers.
if (addon.getConfig().isSet("gamemodes"))
{
ConfigurationSection section = addon.getConfig().getConfigurationSection("gamemodes");

for (String gameMode : section.getKeys(false))
for (String gameMode : Objects.requireNonNull(section).getKeys(false))
{
ConfigurationSection gameModeSection = section.getConfigurationSection(gameMode);
for (String key : Objects.requireNonNull(gameModeSection).getKeys(false))
{
this.customGeneratorTierMap.computeIfAbsent(gameMode, k -> new HashMap<>()).put(key, addSection(gameModeSection, key));
}

addSection(gameModeSection);
}
}
}

@NonNull
private GeneratorTier addSection(ConfigurationSection section, String key) {
ConfigurationSection tierSection = section.getConfigurationSection(key);
GeneratorTier generatorTier = new GeneratorTier(key);
generatorTier.setName(tierSection.getString("name"));
generatorTier.setMinLevel(tierSection.getInt("min-level"));

private void addSection(ConfigurationSection section) {
for (String key : section.getKeys(false))
{
ConfigurationSection tierSection = section.getConfigurationSection(key);

if (tierSection != null)
TreeMap<Double, Material> blockChances = new TreeMap<>();
if (tierSection.isConfigurationSection("blocks")) {
for (String materialKey : Objects.requireNonNull(tierSection).getConfigurationSection("blocks").getKeys(false))
{
GeneratorTier generatorTier = new GeneratorTier(key);
generatorTier.setName(tierSection.getString("name"));
generatorTier.setMinLevel(tierSection.getInt("min-level"));

TreeMap<Double, Material> blockChances = new TreeMap<>();

for (String materialKey : tierSection.getConfigurationSection("blocks").getKeys(false))
try
{
try
{
Material material = Material.valueOf(materialKey);
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
blockChances.put(lastEntry + tierSection.getDouble("blocks." + materialKey, 0), material);
}
catch (Exception e)
{
addon.logWarning("Unknown material (" + materialKey +
") in config.yml blocks section for tier " + key + ". Skipping...");
}
Material material = Material.valueOf(materialKey);
double lastEntry = blockChances.isEmpty() ? 0D : blockChances.lastKey();
blockChances.put(lastEntry + tierSection.getDouble("blocks." + materialKey, 0), material);
}
catch (Exception e)
{
addon.logWarning("Unknown material (" + materialKey +
") in config.yml blocks section for tier " + key + ". Skipping...");
}

generatorTier.setBlockChanceMap(blockChances);

this.generatorTierMap.put(key, generatorTier);
}

generatorTier.setBlockChanceMap(blockChances);
}

return generatorTier;
}



// ---------------------------------------------------------------------
// Section: Getters
// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package world.bentobox.magiccobblestonegenerator.config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.Collections;

import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon;

/**
* @author tastybento
*
*/
public class SettingsTest {

@Mock
private BentoBox plugin;

@Mock
private StoneGeneratorAddon addon;

private Settings s;
private YamlConfiguration config;

private String skygrid = "SkyGrid";

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
config = new YamlConfiguration();
File file = new File("src/main/resources", "config.yml");
config.load(file);
addon = mock(StoneGeneratorAddon.class);
when(addon.getConfig()).thenReturn(config);
s = new Settings(addon);
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.config.Settings#isOfflineGeneration()}.
*/
@Test
public void testIsOfflineGeneration() {
assertFalse(s.isOfflineGeneration());
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.config.Settings#isOfflineGeneration()}.
*/
@Test
public void testIsOfflineGenerationOnline() {
config.set("offline-generation", true);
s = new Settings(addon);
assertTrue(s.isOfflineGeneration());
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.config.Settings#getDisabledGameModes()}.
*/
@Test
public void testGetDisabledGameModes() {
assertTrue(s.getDisabledGameModes().isEmpty());
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.config.Settings#getDisabledGameModes()}.
*/
@Test
public void testGetDisabledGameModesSomeDisabled() {
config.set("disabled-gamemodes", Collections.singletonList("BSkyBlock"));
s = new Settings(addon);
assertFalse(s.getDisabledGameModes().isEmpty());
assertTrue(s.getDisabledGameModes().contains("BSkyBlock"));
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.config.Settings#getDefaultGeneratorTierMap()}.
*/
@Test
public void testGetDefaultGeneratorTierMap() {
assertEquals(7, s.getDefaultGeneratorTierMap().size());
assertTrue(s.getDefaultGeneratorTierMap().containsKey("default"));
for (int i = 1; i < 7; i++) {
assertTrue(s.getDefaultGeneratorTierMap().containsKey("tier" + String.valueOf(i)));
}
assertEquals("Stone Level", s.getDefaultGeneratorTierMap().get("default").getName());
}

/**
* Test method for {@link world.bentobox.magiccobblestonegenerator.config.Settings#getAddonGeneratorTierMap(java.lang.String)}.
*/
@Test
public void testGetAddonGeneratorTierMap() {
assertEquals(1, s.getAddonGeneratorTierMap(skygrid).size());
assertEquals("Diamond Level", s.getAddonGeneratorTierMap(skygrid).get("default").getName());
}

}

0 comments on commit f3a3460

Please sign in to comment.