Skip to content

Commit

Permalink
Added config option for fish weight
Browse files Browse the repository at this point in the history
Added addtional fish prefixes
Fixed config not saving when changing values in the in-game config
Other small fixes
  • Loading branch information
GirafiStudios committed May 3, 2018
1 parent ddf412d commit f72a21d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Aquaculture {

@EventHandler
public void preInit(FMLPreInitializationEvent event) {
new Config().init(event.getSuggestedConfigurationFile());
Config.init(event.getSuggestedConfigurationFile());
tab = new AquacultureTab("Aquaculture");

new AquacultureItems().register();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public GuiScreen createConfigGui(GuiScreen parentScreen) {
private static List<IConfigElement> getConfigElements() {
List<IConfigElement> list = new ArrayList<>();

List<IConfigElement> basic = new ConfigElement(Config.config.getCategory(Config.categoryBasic)).getChildElements();
List<IConfigElement> fishRarity = new ConfigElement(Config.config.getCategory(Config.categoryFishRarity)).getChildElements();
List<IConfigElement> junkRarity = new ConfigElement(Config.config.getCategory(Config.categoryJunkRarity)).getChildElements();

list.add(new DummyConfigElement.DummyCategoryElement("Fish rarity", String.valueOf(new ResourceLocation(Aquaculture.MOD_ID, "config.category.mobDrops")), fishRarity));
list.add(new DummyConfigElement.DummyCategoryElement("Junk rarity", String.valueOf(new ResourceLocation(Aquaculture.MOD_ID, "config.category.modSupport")), junkRarity));
list.add(new DummyConfigElement.DummyCategoryElement("Basic options", String.valueOf(new ResourceLocation(Aquaculture.MOD_ID, "config.category.basic")), basic));
list.add(new DummyConfigElement.DummyCategoryElement("Fish rarity", String.valueOf(new ResourceLocation(Aquaculture.MOD_ID, "config.category.fishRarity")), fishRarity));
list.add(new DummyConfigElement.DummyCategoryElement("Junk rarity", String.valueOf(new ResourceLocation(Aquaculture.MOD_ID, "config.category.junkRarity")), junkRarity));

return list;
}
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/com/teammetallurgy/aquaculture/handlers/Config.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.teammetallurgy.aquaculture.handlers;

import com.teammetallurgy.aquaculture.Aquaculture;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map.Entry;

@Mod.EventBusSubscriber
public class Config {
public static Configuration config;
public static final String categoryBasic = "BASIC_OPTIONS";
public static boolean assignRandomWeight;

public static final String categoryFishRarity = "FISH_RARITY";
public static HashMap<String, Integer> fishRarity = new HashMap<>();
Expand Down Expand Up @@ -86,9 +93,15 @@ public class Config {

}

public void init(File file) {
config = new Configuration(file);
config.load();
public static void init(File file) {
if (config == null) {
config = new Configuration(file);
loadConfiguration();
}
}

private static void loadConfiguration() {
assignRandomWeight = config.get(categoryBasic, "Enable fish weight", true).getBoolean();

for (Entry<String, Integer> entry : fishRarity.entrySet()) {
int rarity = entry.getValue();
Expand All @@ -108,4 +121,11 @@ public void init(File file) {
config.save();
}
}

@SubscribeEvent
public static void onConfigurationChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event) {
if (event.getModID().equalsIgnoreCase(Aquaculture.MOD_ID)) {
loadConfiguration();
}
}
}
20 changes: 9 additions & 11 deletions src/main/java/com/teammetallurgy/aquaculture/items/ItemFish.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ItemFish() {
}

public void addFish(String name, int filletAmount, int minWeight, int maxWeight, BiomeType biome, int rarity) {
addFish(name, filletAmount, minWeight, maxWeight, new BiomeType[] { biome }, rarity);
addFish(name, filletAmount, minWeight, maxWeight, new BiomeType[]{biome}, rarity);
}

public void addFish(String name, int filletAmount, int minWeight, int maxWeight, BiomeType[] biomes, int rarity) {
Expand Down Expand Up @@ -84,13 +84,11 @@ public void assignRandomWeight(@Nonnull ItemStack stack, int heavyLineLvl) {
if (f.maxWeight == 1 && f.minWeight == 1)
return;

Random rand = new Random();

float min = f.minWeight;

min += (f.maxWeight - min) * (0.1 * heavyLineLvl);

float weight = rand.nextFloat() * ((f.maxWeight * 1.1f) - min) + min;
float weight = new Random().nextFloat() * (f.maxWeight - min) + min;

if (!stack.hasTagCompound()) {
stack.setTagCompound(new NBTTagCompound());
Expand All @@ -99,14 +97,15 @@ public void assignRandomWeight(@Nonnull ItemStack stack, int heavyLineLvl) {
Preconditions.checkNotNull(stack.getTagCompound(), "tagCompound");
stack.getTagCompound().setFloat("Weight", weight);

if (weight <= f.maxWeight / 10.0) {
if (weight <= f.maxWeight * 0.10F) {
stack.getTagCompound().setString("Prefix", "Juvenile");
}

if (weight > f.maxWeight) {
} else if (weight > f.maxWeight * 0.10F && weight <= f.maxWeight * 0.20F) {
stack.getTagCompound().setString("Prefix", "Small");
} else if (weight >= f.maxWeight * 0.80F && weight < f.maxWeight * 0.90F) {
stack.getTagCompound().setString("Prefix", "Large");
} else if (weight >= f.maxWeight * 0.90F) {
stack.getTagCompound().setString("Prefix", "Massive");
}

}

@Nonnull
Expand All @@ -129,7 +128,6 @@ public String getUnlocalizedName(@Nonnull ItemStack stack) {
}

@Override
@SideOnly(Side.CLIENT)
public void getSubItems(@Nonnull CreativeTabs tab, @Nonnull NonNullList<ItemStack> subItems) {
if (this.isInCreativeTab(tab)) {
for (int j = 0; j < fish.size(); ++j) {
Expand All @@ -151,4 +149,4 @@ public Fish(String name, int amount, int min, int max) {
minWeight = min;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import javax.annotation.Nonnull;
import java.util.ArrayList;
Expand Down Expand Up @@ -45,7 +43,6 @@ public String getUnlocalizedName(@Nonnull ItemStack stack) {
}

@Override
@SideOnly(Side.CLIENT)
public void getSubItems(@Nonnull CreativeTabs tab, @Nonnull NonNullList<ItemStack> items) {
if (this.isInCreativeTab(tab)) {
for (SubItem subItem : subItems) {
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/com/teammetallurgy/aquaculture/loot/FishLoot.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.teammetallurgy.aquaculture.loot;

import com.teammetallurgy.aquaculture.handlers.Config;
import com.teammetallurgy.aquaculture.items.AquacultureItems;
import net.minecraft.item.ItemStack;

Expand Down Expand Up @@ -33,7 +34,6 @@ public void addFish(ItemStack fish, BiomeType biome, int rarity) {
if (!fishBiomeMap.containsKey(biome)) {
fishBiomeMap.put(biome, new WeightedLootSet());
}

fishBiomeMap.get(biome).addLoot(fish, rarity, 1, 1);
}

Expand All @@ -53,7 +53,6 @@ public void addJunkLoot(ItemStack fish, BiomeType biome, int rarity) {
if (!junkBiomeMap.containsKey(biome)) {
junkBiomeMap.put(biome, new WeightedLootSet());
}

junkBiomeMap.get(biome).addLoot(fish, rarity, 1, 1);
}

Expand All @@ -62,11 +61,11 @@ public ItemStack getRandomFish(int biomeID) {
BiomeType biome = BiomeType.getBiomeType(biomeID);

ItemStack fishStack;
if (biome != null && fishBiomeMap.containsKey(biome))
if (biome != null && fishBiomeMap.containsKey(biome)) {
fishStack = fishBiomeMap.get(biome).getRandomLoot();
else
} else {
fishStack = fishBiomeMap.get(BiomeType.freshwater).getRandomLoot();

}
return fishStack;
}

Expand All @@ -75,23 +74,25 @@ public ItemStack getRandomFish(int biomeID, int heavyLineLvl) {
BiomeType biome = BiomeType.getBiomeType(biomeID);

ItemStack fishStack;
if (biome != null && fishBiomeMap.containsKey(biome))
if (biome != null && fishBiomeMap.containsKey(biome)) {
fishStack = fishBiomeMap.get(biome).getRandomLoot();
else
} else {
fishStack = fishBiomeMap.get(BiomeType.freshwater).getRandomLoot();

AquacultureItems.fish.assignRandomWeight(fishStack, heavyLineLvl);

}
if (Config.assignRandomWeight) {
AquacultureItems.fish.assignRandomWeight(fishStack, heavyLineLvl);
}
return fishStack;
}

@Nonnull
public ItemStack getRandomJunk(int biomeID) {
BiomeType biome = BiomeType.getBiomeType(biomeID);

if (biome != null && junkBiomeMap.containsKey(biome))
if (biome != null && junkBiomeMap.containsKey(biome)) {
return junkBiomeMap.get(biome).getRandomLoot();
else
} else {
return junkBiomeMap.get(BiomeType.freshwater).getRandomLoot();
}
}
}
}

0 comments on commit f72a21d

Please sign in to comment.