Skip to content

Commit

Permalink
[CI Skip] Merge pull request #15 from Sefiraat/feature/new_config
Browse files Browse the repository at this point in the history
Config dynamic updating, config options
  • Loading branch information
Sefiraat authored Jun 16, 2022
2 parents a47271d + 8a501e1 commit eec8950
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 125 deletions.
111 changes: 0 additions & 111 deletions dependency-reduced-pom.xml

This file was deleted.

6 changes: 2 additions & 4 deletions src/main/java/dev/sefiraat/netheopoiesis/Netheopoiesis.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public void onEnable() {
getLogger().info("########################################");

saveDefaultConfig();
this.configManager = new ConfigManager();
tryUpdate();

this.configManager = new ConfigManager();
this.supportedPluginManager = new SupportedPluginManager();
this.listenerManager = new ListenerManager();
this.runnableManager = new RunnableManager();
Expand All @@ -73,9 +73,7 @@ public void onDisable() {
}

public void tryUpdate() {
if (getConfig().getBoolean("auto-update")
&& getDescription().getVersion().startsWith("DEV")
) {
if (configManager.isAutoUpdate() && getDescription().getVersion().startsWith("DEV")) {
String updateLocation = MessageFormat.format("{0}/{1}/{2}", this.username, this.repo, this.branch);
GitHubBuildsUpdater updater = new GitHubBuildsUpdater(this, getFile(), updateLocation);
updater.start();
Expand Down
64 changes: 55 additions & 9 deletions src/main/java/dev/sefiraat/netheopoiesis/api/mobs/MobCap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.sefiraat.netheopoiesis.api.mobs;

import dev.sefiraat.netheopoiesis.Netheopoiesis;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity;
import org.jetbrains.annotations.Unmodifiable;
Expand All @@ -12,15 +13,50 @@

public class MobCap {

public static final MobCap WATER_AMBIENT = new MobCap(MobCapType.WATER_AMBIENT, 15);
public static final MobCap WATER_ANIMAL = new MobCap(MobCapType.WATER_ANIMAL, 5);
public static final MobCap WATER_HOSTILE = new MobCap(MobCapType.WATER_HOSTILE, 10);
public static final MobCap LAND_AMBIENT = new MobCap(MobCapType.WATER_AMBIENT, 5);
public static final MobCap LAND_ANIMAL = new MobCap(MobCapType.LAND_ANIMAL, 10);
public static final MobCap LAND_HOSTILE = new MobCap(MobCapType.LAND_HOSTILE, 10);
public static final MobCap VILLAGER = new MobCap(MobCapType.VILLAGER, 5);
public static final MobCap PIGLIN_TRADER = new MobCap(MobCapType.PIGLIN_TRADER, 1);
public static final MobCap WANDERING_TRADER = new MobCap(MobCapType.WANDERING_TRADER, 1);
public static final MobCap WATER_AMBIENT = new MobCap(
MobCapType.WATER_AMBIENT,
Netheopoiesis.getConfigManager().getPlayerMobCapWaterAmbient()
);

public static final MobCap WATER_ANIMAL = new MobCap(
MobCapType.WATER_ANIMAL,
Netheopoiesis.getConfigManager().getPlayerMobCapWaterAnimal()
);

public static final MobCap WATER_HOSTILE = new MobCap(
MobCapType.WATER_HOSTILE,
Netheopoiesis.getConfigManager().getPlayerMobCapWaterHostile()
);

public static final MobCap LAND_AMBIENT = new MobCap(
MobCapType.WATER_AMBIENT,
Netheopoiesis.getConfigManager().getPlayerMobCapLandAmbient()
);

public static final MobCap LAND_ANIMAL = new MobCap(
MobCapType.LAND_ANIMAL,
Netheopoiesis.getConfigManager().getPlayerMobCapLandAnimal()
);

public static final MobCap LAND_HOSTILE = new MobCap(
MobCapType.LAND_HOSTILE,
Netheopoiesis.getConfigManager().getPlayerMobCapLandHostile()
);

public static final MobCap VILLAGER = new MobCap(
MobCapType.VILLAGER,
Netheopoiesis.getConfigManager().getPlayerMobCapVillager()
);

public static final MobCap PIGLIN_TRADER = new MobCap(
MobCapType.PIGLIN_TRADER,
Netheopoiesis.getConfigManager().getPlayerMobCapPiglinTrader()
);

public static final MobCap WANDERING_TRADER = new MobCap(
MobCapType.WANDERING_TRADER,
Netheopoiesis.getConfigManager().getPlayerMobCapWanderingTrader()
);

private final int amountPerPlayer;
@Nonnull
Expand All @@ -35,6 +71,7 @@ public MobCap(@Nonnull MobCapType type, int maxPerPlayer) {

/**
* Gets the number of mobs currently held in this cap
*
* @return The number of mobs currently held in this cap
*/
public int count() {
Expand All @@ -43,6 +80,7 @@ public int count() {

/**
* Checks if there is space in this cap for another mob
*
* @return true if there is space
*/
public boolean hasSpace() {
Expand All @@ -51,6 +89,7 @@ public boolean hasSpace() {

/**
* Checks if a given {@link UUID} is contained in this cap
*
* @param mobUuid The {@link UUID} for the mob being checked
* @return true if contained in the cap
*/
Expand All @@ -61,6 +100,7 @@ public boolean contains(@Nonnull UUID mobUuid) {
/**
* Adds a new mob to this cap. Does not check for space and can be forced if required.
* Check hasSpace() first!
*
* @param mobUuid The {@link UUID} of the mob to add
*/
public void addMob(@Nonnull UUID mobUuid) {
Expand All @@ -69,6 +109,7 @@ public void addMob(@Nonnull UUID mobUuid) {

/**
* Removes a mob from this cap if possible
*
* @param mobUuid The {@link UUID} of the mob to remove
*/
public void removeMob(@Nonnull UUID mobUuid) {
Expand All @@ -77,6 +118,7 @@ public void removeMob(@Nonnull UUID mobUuid) {

/**
* Kills a mob whilst also removing them from the cap
*
* @param mobUuid The {@link UUID} of the mob to kill/remove
*/
public void killMob(@Nonnull UUID mobUuid) {
Expand All @@ -102,6 +144,7 @@ public void killAllMobs() {

/**
* Gets the maximum number of spawns, per player, for this cap.
*
* @return The max number of mobs
*/
public int getMaxAmountPerPlayer() {
Expand All @@ -110,6 +153,7 @@ public int getMaxAmountPerPlayer() {

/**
* Gets the maximum number of spawns for this cap (includes the multiplier per-player)
*
* @return The max number of mobs
*/
public int getMaxAmount() {
Expand All @@ -118,6 +162,7 @@ public int getMaxAmount() {

/**
* The {@link MobCapType} for this cap
*
* @return The {@link MobCapType} for this cap
*/
@Nonnull
Expand All @@ -127,6 +172,7 @@ public MobCapType getType() {

/**
* Gets an immutable list of all mobs contained within this cap
*
* @return An immutable list of all mobs contained within this cap
*/
@Nonnull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package dev.sefiraat.netheopoiesis.managers;

import dev.sefiraat.netheopoiesis.Netheopoiesis;
import dev.sefiraat.netheopoiesis.api.items.NetherSeed;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
* This class is used to create and manage/save custom configuration files
Expand All @@ -17,9 +25,48 @@ public class ConfigManager {
private final FileConfiguration discoveries;

public ConfigManager() {
setupDefaultConfig();
this.discoveries = getConfig("discoveries.yml");
}

private void setupDefaultConfig() {
final Netheopoiesis plugin = Netheopoiesis.getInstance();
final InputStream inputStream = plugin.getResource("config.yml");
final File existingFile = new File(plugin.getDataFolder(), "config.yml");

if (inputStream == null) {
// Not sure how? Regardless cannot copy over new keys
return;
}

final Reader reader = new InputStreamReader(inputStream);
final FileConfiguration resourceConfig = YamlConfiguration.loadConfiguration(reader);
final FileConfiguration existingConfig = YamlConfiguration.loadConfiguration(existingFile);

for (String key : resourceConfig.getKeys(false)) {
checkKey(existingConfig, resourceConfig, key);
}

try {
existingConfig.save(existingFile);
} catch (IOException e) {
e.printStackTrace();
}
}

@ParametersAreNonnullByDefault
private void checkKey(FileConfiguration existingConfig, FileConfiguration resourceConfig, String key) {
final Object currentValue = existingConfig.get(key);
final Object newValue = resourceConfig.get(key);
if (newValue instanceof ConfigurationSection section) {
for (String sectionKey : section.getKeys(false)) {
checkKey(existingConfig, resourceConfig, key + "." + sectionKey);
}
} else if (currentValue == null) {
existingConfig.set(key, newValue);
}
}

@Nonnull
@SuppressWarnings("ResultOfMethodCallIgnored")
private FileConfiguration getConfig(@Nonnull String fileName) {
Expand Down Expand Up @@ -54,4 +101,48 @@ private void saveDiscoveries() {
public FileConfiguration getDiscoveries() {
return discoveries;
}

public boolean isAutoUpdate() {
return Netheopoiesis.getInstance().getConfig().getBoolean("auto-update");
}

public boolean isDebugMessages() {
return Netheopoiesis.getInstance().getConfig().getBoolean("debug-messages");
}

public int getPlayerMobCapWaterAmbient() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-water-ambient");
}

public int getPlayerMobCapWaterAnimal() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-water-animal");
}

public int getPlayerMobCapWaterHostile() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-water-hostile");
}

public int getPlayerMobCapLandAmbient() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-land-ambient");
}

public int getPlayerMobCapLandAnimal() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-land-animal");
}

public int getPlayerMobCapLandHostile() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-land-hostile");
}

public int getPlayerMobCapVillager() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-villager");
}

public int getPlayerMobCapPiglinTrader() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-piglin-trader");
}

public int getPlayerMobCapWanderingTrader() {
return Netheopoiesis.getInstance().getConfig().getInt("spawning.player-cap-wandering-trader");
}
}
Loading

0 comments on commit eec8950

Please sign in to comment.