Skip to content

Commit

Permalink
add config options
Browse files Browse the repository at this point in the history
  • Loading branch information
SchnTgaiSpock committed Oct 29, 2023
1 parent 7633619 commit 84e5f52
Show file tree
Hide file tree
Showing 13 changed files with 280 additions and 163 deletions.
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ A Slimefun addon that adds even more foodstuffs to the game. Best used along wit

This addon automatically integrates with DynaTech for crop automation. Both DynaTech and ExoticGarden integrations can be manually disabled in `config.yml`

## Gameplay
## Links

For more gameplay details, please see the Wiki at <https://schn.pages.dev/gastronomicon>
- Wiki: <https://schn.pages.dev/gastronomicon>
- Download: <https://thebusybiscuit.github.io/builds/SchnTgaiSpock/Gastronomicon/master/>

## Requirements

Expand All @@ -21,19 +22,6 @@ For more gameplay details, please see the Wiki at <https://schn.pages.dev/gastro

API documentation can be found on the wiki at <https://schn.pages.dev/gastronomicon/custom-food>

## Changelog

### Version 1.0.5

- Fixed 1.18 incompatibilities
- Fixed Banana Saplings giving the wrong type of leaf in DynaTech Growth Chambers
- Moved wiki links to <https://schn.pages.dev/gastronomicon>

### Version 0.1.0

- First Beta release
- First API release

## Credits

Some head textures were taken and/or modified from [minecraft-heads.com](https://minecraft-heads.com/)
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.schntgaispock.gastronomicon</groupId>
<artifactId>Gastronomicon</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down Expand Up @@ -95,7 +95,7 @@
<dependency>
<groupId>com.github.Slimefun</groupId>
<artifactId>Slimefun4</artifactId>
<version>RC-33</version>
<version>RC-35</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.schntgaispock.gastronomicon.api.loot;

import java.util.List;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class EmptyItemLootTable extends LootTable<ItemStack> {

protected EmptyItemLootTable() {
super(List.of(), 0, new int[0], new int[0]);
}

@Override
public boolean isEmpty() {
return true;
}

@Override
public int size() {
return 0;
}

@Override
public ItemStack generate() {
return new ItemStack(Material.AIR);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.schntgaispock.gastronomicon.api.loot;

import org.bukkit.inventory.ItemStack;

import io.github.schntgaispock.gastronomicon.api.loot.LootTable.LootTableBuilder;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;

public class ItemLootTableBuilder extends LootTableBuilder<ItemStack> {


@SafeVarargs
public final ItemLootTableBuilder addItems(int weight, ItemStack... drops) {
for (final ItemStack drop : drops) {
final SlimefunItem sfItem = SlimefunItem.getByItem(drop);
if (sfItem != null && sfItem.isDisabled()) {
continue;
}

weightedDrops.put(drop, weight);
totalWeight += weight;
}
return this;
}

@SafeVarargs
public final ItemLootTableBuilder addItems(ItemStack... drops) {
return addItems(1, drops);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class LootTable<T> {
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public static class LootTableBuilder<T> {

private int totalWeight = 0;
private final LinkedHashMap<T, Integer> weightedDrops = new LinkedHashMap<>();
protected int totalWeight = 0;
protected final LinkedHashMap<T, Integer> weightedDrops = new LinkedHashMap<>();

@SafeVarargs
public final LootTableBuilder<T> add(int weight, T... drops) {
for (T drop : drops) {
for (final T drop : drops) {
weightedDrops.put(drop, weight);
totalWeight += weight;
}
Expand Down Expand Up @@ -92,7 +92,15 @@ public static <T, U> LootTableBuilder<T> builder(Class<T> type) {
return new LootTableBuilder<T>();
}

public static <T, U> ItemLootTableBuilder builder() {
return new ItemLootTableBuilder();
}

public T generate() {
if (drops.size() == 0) {
return null;
}

final int roll = ThreadLocalRandom.current().nextInt(drops.size());
if (ThreadLocalRandom.current().nextDouble() * totalWeight < prob[roll]) {
return drops.get(roll);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.schntgaispock.gastronomicon.core.listeners;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;
Expand All @@ -22,7 +24,9 @@
import io.github.schntgaispock.gastronomicon.Gastronomicon;
import io.github.schntgaispock.gastronomicon.api.loot.LootTable;
import io.github.schntgaispock.gastronomicon.core.Climate;
import io.github.schntgaispock.gastronomicon.core.slimefun.GastroStacks;
import io.github.schntgaispock.gastronomicon.util.NumberUtil;
import io.github.schntgaispock.gastronomicon.util.StringUtil;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.Interaction;

Expand All @@ -32,8 +36,9 @@ public class WildHarvestListener implements Listener {
private static final Map<Material, LootTable<ItemStack>> dropsByBlock = new HashMap<>();
private static final Map<EntityType, LootTable<ItemStack>> dropsByMob = new HashMap<>();

private static double BLOCK_BREAK_DROP_CHANCE = 0.15;
private static double MOB_KILL_DROP_CHANCE = 0.35;
private static final List<String> worldsDisabledIn = new ArrayList<>();
private static final Map<Material, Double> blockDropChanceCache = new HashMap<>();
private static final Map<EntityType, Double> mobDropChanceCache = new HashMap<>();

public static void registerBlockDrops(Material material, LootTable<ItemStack> table, Climate climate) {
Map<Material, LootTable<ItemStack>> climateDrops = dropsByClimateByBlock.get(climate);
Expand All @@ -52,25 +57,53 @@ public static void registerMobDrops(EntityType entityType, LootTable<ItemStack>
dropsByMob.put(entityType, table);
}

private double getDropChance(Material mat) {
if (blockDropChanceCache.containsKey(mat)) {
return blockDropChanceCache.get(mat);
}

System.out.println("drops.block-break-chances." + StringUtil.kebabCase(mat.name()));
final double chance = Gastronomicon.getInstance()
.getConfig()
.getDouble("drops.block-break-chances." + StringUtil.kebabCase(mat.name()), 0.15);
System.out.println(chance);
blockDropChanceCache.put(mat, chance);
return chance;
}

private double getDropChance(EntityType entity) {
if (mobDropChanceCache.containsKey(entity)) {
return mobDropChanceCache.get(entity);
}

final double chance = Gastronomicon.getInstance()
.getConfig()
.getDouble("drops.mob-kill-chances." + StringUtil.kebabCase(entity.name()), 0.35);
mobDropChanceCache.put(entity, chance);
return chance;
}

@Nullable
@ParametersAreNonnullByDefault
public static LootTable<ItemStack> getDrops(Material dropFrom, Climate climate) {
if (dropsByClimateByBlock.containsKey(climate) && dropsByClimateByBlock.get(climate).containsKey(dropFrom)) {
return dropsByClimateByBlock.get(climate).get(dropFrom);
} else if (dropsByBlock.containsKey(dropFrom)) {
return dropsByBlock.get(dropFrom);
} else {
return null;
return dropsByBlock.getOrDefault(dropFrom, null);
}
}

@Nullable
public static LootTable<ItemStack> getDrops(@Nonnull EntityType dropFrom) {
return dropsByMob.containsKey(dropFrom) ? dropsByMob.get(dropFrom) : null;
return dropsByMob.getOrDefault(dropFrom, null);
}

@EventHandler
public void onBlockBreak(BlockBreakEvent e) {
if (worldsDisabledIn.contains(e.getBlock().getWorld().getName())) {
return;
}

final Block b = e.getBlock();
if (b == null)
return;
Expand All @@ -85,30 +118,158 @@ public void onBlockBreak(BlockBreakEvent e) {
final ItemStack weapon = e.getPlayer().getInventory().getItemInMainHand();
final int fortune = weapon == null ? 0
: weapon.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS);
if (NumberUtil.flip(Math.min(drops.size() * 0.03, BLOCK_BREAK_DROP_CHANCE) * (1 + fortune * 0.5))) {
// Slightly lower the drop chance in items with few drops
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drops.generate().clone());
if (NumberUtil.flip(getDropChance(b.getType()) * (1 + fortune * 0.5))) {
final ItemStack drop = drops.generate();
if (drop == null) {
return;
}
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(), drop.clone());
}
}

@EventHandler
public void onMobKill(EntityDeathEvent e) {
if (worldsDisabledIn.contains(e.getEntity().getWorld().getName())) {
return;
}

final LootTable<ItemStack> drops = getDrops(e.getEntityType());
if (drops == null)
return;

final Player killer = e.getEntity().getKiller();
final int looting = (killer == null || killer.getInventory().getItemInMainHand() == null) ? 0
: killer.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_MOBS);
if (NumberUtil.flip(MOB_KILL_DROP_CHANCE * (1 + looting * 0.5))) {
e.getDrops().add(drops.generate().clone());
if (NumberUtil.flip(getDropChance(e.getEntityType()) * (1 + looting * 0.5))) {
final ItemStack drop = drops.generate();
if (drop == null) {
return;
}
e.getDrops().add(drop.clone());
}
}

public static void setup() {
Bukkit.getPluginManager().registerEvents(new WildHarvestListener(), Gastronomicon.getInstance());
BLOCK_BREAK_DROP_CHANCE = Gastronomicon.getInstance().getConfig().getDouble("drops.block-break-chance");
MOB_KILL_DROP_CHANCE = Gastronomicon.getInstance().getConfig().getDouble("drops.mob-kill-chance");

worldsDisabledIn.addAll(Gastronomicon.getInstance().getConfig().getStringList("drops.disabled-in"));

final LootTable<ItemStack> DRY_CLIMATE_GRASS_DROPS = LootTable.builder()
.addItems(GastroStacks.CASSAVA,
GastroStacks.LENTILS,
GastroStacks.CUMIN_SEEDS,
GastroStacks.HONEYDEW_MELON_SEEDS,
GastroStacks.SORGHUM_SEEDS)
.build();
final LootTable<ItemStack> TEMPERATE_CLIMATE_GRASS_DROPS = LootTable.builder()
.addItems(6,
GastroStacks.RICE,
GastroStacks.OATS,
GastroStacks.SOYBEANS,
GastroStacks.BARLEY_SEEDS,
GastroStacks.RYE_SEEDS,
GastroStacks.SORGHUM_SEEDS)
.addItems(4,
GastroStacks.TURNIP_SEEDS,
GastroStacks.RED_BEANS,
GastroStacks.CANTALOUPE_SEEDS,
GastroStacks.HONEYDEW_MELON_SEEDS,
GastroStacks.SESAME_SEEDS,
GastroStacks.PEANUTS,
GastroStacks.BEANS,
GastroStacks.PEAS)
.addItems(3,
GastroStacks.BOK_CHOY_SEEDS,
GastroStacks.CUCUMBER_SEEDS,
GastroStacks.BASIL_SEEDS,
GastroStacks.SPINACH_SEEDS,
GastroStacks.MINT_SEEDS,
GastroStacks.CHILI_PEPPER_SEEDS,
GastroStacks.PARSLEY_SEEDS,
GastroStacks.CASSAVA,
GastroStacks.LENTILS,
GastroStacks.ASPARAGUS_SEEDS,
GastroStacks.GREEN_ONION_SEEDS,
GastroStacks.CAULIFLOWER_SEEDS,
GastroStacks.AVOCADO_PIT,
GastroStacks.TURMERIC,
GastroStacks.CUMIN_SEEDS,
GastroStacks.VANILLA_PLANT)
.addItems(2,
GastroStacks.LYCHEE_SAPLING,
GastroStacks.BANANA_SAPLING)
.build();
final LootTable<ItemStack> COLD_CLIMATE_GRASS_DROPS = LootTable.builder()
.addItems(6,
GastroStacks.QUINOA,
GastroStacks.OATS,
GastroStacks.RYE_SEEDS)
.addItems(4,
GastroStacks.TURNIP_SEEDS,
GastroStacks.SQUASH_SEEDS,
GastroStacks.PEAS)
.addItems(3,
GastroStacks.CELERY,
GastroStacks.BROCCOLI_SEEDS,
GastroStacks.BRUSSLES_SPROUTS)
.addItems(2,
GastroStacks.LYCHEE_SAPLING)
.build();
final LootTable<ItemStack> SNOWY_CLIMATE_GRASS_DROPS = LootTable.builder()
.addItems(GastroStacks.RYE_SEEDS)
.build();
final LootTable<ItemStack> FERN_DROPS = LootTable.builder()
.addItems(GastroStacks.FIDDLEHEADS)
.build();
final LootTable<ItemStack> DIRT_DROPS = LootTable.builder()
.addItems(GastroStacks.ENOKI_MUSHROOMS,
GastroStacks.KING_OYSTER_MUSHROOM,
GastroStacks.BUTTON_MUSHROOM)
.build();
final LootTable<ItemStack> PODZOL_DROPS = LootTable.builder()
.addItems(4,
GastroStacks.ENOKI_MUSHROOMS,
GastroStacks.KING_OYSTER_MUSHROOM,
GastroStacks.BUTTON_MUSHROOM)
.addItems(GastroStacks.TRUFFLE)
.build();
final LootTable<ItemStack> SEAGRASS_DROPS = LootTable.builder()
.addItems(GastroStacks.CLAM)
.build();

final LootTable<ItemStack> SQUID_DROPS = LootTable.builder()
.addItems(GastroStacks.RAW_SQUID)
.build();
final LootTable<ItemStack> GUARDIAN_DROPS = LootTable.builder()
.addItems(GastroStacks.GUARDIAN_FIN)
.build();
final LootTable<ItemStack> GOAT_DROPS = LootTable.builder()
.addItems(GastroStacks.RAW_CHEVON)
.build();
final LootTable<ItemStack> SALMON_DROPS = LootTable.builder()
.addItems(GastroStacks.SALMON_ROE)
.build();

WildHarvestListener.registerBlockDrops(Material.GRASS, DRY_CLIMATE_GRASS_DROPS, Climate.DRY);
WildHarvestListener.registerBlockDrops(Material.TALL_GRASS, DRY_CLIMATE_GRASS_DROPS, Climate.DRY);
WildHarvestListener.registerBlockDrops(Material.GRASS, TEMPERATE_CLIMATE_GRASS_DROPS, Climate.TEMPERATE);
WildHarvestListener.registerBlockDrops(Material.TALL_GRASS, TEMPERATE_CLIMATE_GRASS_DROPS, Climate.TEMPERATE);
WildHarvestListener.registerBlockDrops(Material.GRASS, COLD_CLIMATE_GRASS_DROPS, Climate.COLD);
WildHarvestListener.registerBlockDrops(Material.TALL_GRASS, COLD_CLIMATE_GRASS_DROPS, Climate.COLD);
WildHarvestListener.registerBlockDrops(Material.GRASS, SNOWY_CLIMATE_GRASS_DROPS, Climate.SNOWY);
WildHarvestListener.registerBlockDrops(Material.TALL_GRASS, SNOWY_CLIMATE_GRASS_DROPS, Climate.SNOWY);
WildHarvestListener.registerBlockDrops(Material.FERN, FERN_DROPS);
WildHarvestListener.registerBlockDrops(Material.LARGE_FERN, FERN_DROPS);
WildHarvestListener.registerBlockDrops(Material.PODZOL, PODZOL_DROPS);
WildHarvestListener.registerBlockDrops(Material.DIRT, DIRT_DROPS);
WildHarvestListener.registerBlockDrops(Material.GRASS_BLOCK, DIRT_DROPS);
WildHarvestListener.registerBlockDrops(Material.ROOTED_DIRT, DIRT_DROPS);
WildHarvestListener.registerBlockDrops(Material.MYCELIUM, DIRT_DROPS);
WildHarvestListener.registerBlockDrops(Material.SEAGRASS, SEAGRASS_DROPS);
WildHarvestListener.registerMobDrops(EntityType.SQUID, SQUID_DROPS);
WildHarvestListener.registerMobDrops(EntityType.GUARDIAN, GUARDIAN_DROPS);
WildHarvestListener.registerMobDrops(EntityType.GOAT, GOAT_DROPS);
WildHarvestListener.registerMobDrops(EntityType.SALMON, SALMON_DROPS);
}

}
Loading

0 comments on commit 84e5f52

Please sign in to comment.