diff --git a/README.md b/README.md index dcd570d..471ab7e 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ Cooking makes food an interactive part of everyday life. Players grow and gather ## A complete food journey +Food crops need an open column above them. Cave ceilings and building roofs prevent vanilla and CustomCrops planting, including automatic replanting. Glass greenhouses are allowed by default. Nether wart, mushrooms, and the CustomCrops yeast crop can still be planted indoors. + +The `planting` section of `crops.yml` controls `require-open-sky`, `allow-glass-roofs`, additional `allowed-cover` block materials, and `exempt-vanilla` / `exempt-custom` crop lists. Lists replace their defaults; an empty exemption list requires open sky for those crops too. Reload with `cooking reload`. Existing configs without this section use the defaults above. Adding a roof after planting does not remove existing crops or change their growth rules. + A harvest can become flour, dough, bread, or part of a cooked dish. Milk and meat enter their own preparation chains, and the result can be served through furniture as well as carried as food items. Cooking connects these activities into a shared system for farmers, cooks, and diners. ## Documentation diff --git a/src/main/java/net/tfminecraft/cooking/Cooking.java b/src/main/java/net/tfminecraft/cooking/Cooking.java index bd4e3cf..aa3d627 100644 --- a/src/main/java/net/tfminecraft/cooking/Cooking.java +++ b/src/main/java/net/tfminecraft/cooking/Cooking.java @@ -34,6 +34,7 @@ import net.tfminecraft.cooking.fishing.LegacyFishScan; import net.tfminecraft.cooking.fishing.CustomFishingCatalog; import net.tfminecraft.cooking.crops.CropGrowthListener; +import net.tfminecraft.cooking.crops.CropPlantingListener; import net.tfminecraft.cooking.crops.CropsLoader; import net.tfminecraft.cooking.husbandry.HusbandryLoader; import net.tfminecraft.cooking.husbandry.HusbandryLocator; @@ -237,6 +238,7 @@ public void registerListeners() { getServer().getPluginManager().registerEvents(new FarmHarvestListener(), this); getServer().getPluginManager().registerEvents(new FarmTrampleListener(), this); getServer().getPluginManager().registerEvents(new CropGrowthListener(), this); + getServer().getPluginManager().registerEvents(new CropPlantingListener(), this); CropCustomCropsBridge customCropsBridge = new CropCustomCropsBridge(); getServer().getPluginManager().registerEvents(customCropsBridge, this); CropCustomCropsBridge.tryRegister(this); diff --git a/src/main/java/net/tfminecraft/cooking/crops/CropCustomCropsListener.java b/src/main/java/net/tfminecraft/cooking/crops/CropCustomCropsListener.java index 9b2c55c..c707ef4 100644 --- a/src/main/java/net/tfminecraft/cooking/crops/CropCustomCropsListener.java +++ b/src/main/java/net/tfminecraft/cooking/crops/CropCustomCropsListener.java @@ -21,6 +21,7 @@ import net.momirealms.customcrops.api.core.world.CustomCropsBlockState; import net.momirealms.customcrops.api.event.CropBreakEvent; import net.momirealms.customcrops.api.event.CropInteractEvent; +import net.momirealms.customcrops.api.event.CropPlantEvent; import net.tfminecraft.cooking.Cooking; @@ -31,6 +32,15 @@ public final class CropCustomCropsListener implements Listener { private final Map pending = new ConcurrentHashMap<>(); + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlant(CropPlantEvent event) { + if (CropPlantingRule.customRequiresOpenSky(event.cropConfig().id()) + && !CropPlantingRule.hasOpenSky(event.location())) { + event.setCancelled(true); + event.getPlayer().sendMessage(CropPlantingListener.DENIAL_MESSAGE); + } + } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBreak(CropBreakEvent event) { CropDefinition crop = customCrop(event.cropConfig()); diff --git a/src/main/java/net/tfminecraft/cooking/crops/CropPlantingListener.java b/src/main/java/net/tfminecraft/cooking/crops/CropPlantingListener.java new file mode 100644 index 0000000..b267bf1 --- /dev/null +++ b/src/main/java/net/tfminecraft/cooking/crops/CropPlantingListener.java @@ -0,0 +1,22 @@ +package net.tfminecraft.cooking.crops; + +import org.bukkit.ChatColor; +import org.bukkit.block.Block; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; + +public final class CropPlantingListener implements Listener { + public static final String DENIAL_MESSAGE = ChatColor.RED + "Crops must be planted outdoors with open sky above them."; + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void onPlant(BlockPlaceEvent event) { + Block crop = event.getBlockPlaced(); + if (CropPlantingRule.requiresOpenSky(crop.getType()) + && !CropPlantingRule.hasOpenSky(crop.getLocation())) { + event.setCancelled(true); + event.getPlayer().sendMessage(DENIAL_MESSAGE); + } + } +} diff --git a/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java b/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java new file mode 100644 index 0000000..40e5ca1 --- /dev/null +++ b/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java @@ -0,0 +1,77 @@ +package net.tfminecraft.cooking.crops; + +import java.util.Set; +import java.util.HashSet; +import java.util.Locale; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.configuration.ConfigurationSection; + +/** Food crops need an open column, with configurable greenhouse covers. */ +public final class CropPlantingRule { + private static final Set OUTDOOR_CROPS = Set.of( + Material.WHEAT, Material.POTATOES, Material.CARROTS, Material.BEETROOTS, + Material.MELON_STEM, Material.PUMPKIN_STEM, Material.SUGAR_CANE, + Material.CACTUS, Material.COCOA, Material.SWEET_BERRY_BUSH, + Material.TORCHFLOWER_CROP, Material.PITCHER_CROP); + + private static boolean enabled = true; + private static boolean allowGlass = true; + private static Set allowedCover = Set.of(); + private static Set exemptVanilla = Set.of(Material.NETHER_WART, Material.BROWN_MUSHROOM, Material.RED_MUSHROOM); + private static Set exemptCustom = Set.of("yeast"); + + public static void configure(ConfigurationSection section) { + enabled = section == null || section.getBoolean("require-open-sky", true); + allowGlass = section == null || section.getBoolean("allow-glass-roofs", true); + allowedCover = materials(section, "allowed-cover", Set.of()); + exemptVanilla = materials(section, "exempt-vanilla", Set.of(Material.NETHER_WART, Material.BROWN_MUSHROOM, Material.RED_MUSHROOM)); + exemptCustom = section == null || !section.contains("exempt-custom") ? Set.of("yeast") + : Set.copyOf(section.getStringList("exempt-custom").stream().map(id -> id.toLowerCase(Locale.ROOT)).toList()); + } + + private static Set materials(ConfigurationSection section, String key, Set defaults) { + if (section == null || !section.contains(key)) { + return defaults; + } + Set result = new HashSet<>(); + for (String name : section.getStringList(key)) { + Material material = Material.matchMaterial(name); + if (material != null && material.isBlock()) { + result.add(material); + } else { + org.bukkit.Bukkit.getLogger().warning("[Cooking] Invalid planting material in " + key + ": " + name); + } + } + return Set.copyOf(result); + } + + private CropPlantingRule() {} + + public static boolean requiresOpenSky(Material material) { + return enabled && (OUTDOOR_CROPS.contains(material) + || material == Material.NETHER_WART || material == Material.BROWN_MUSHROOM || material == Material.RED_MUSHROOM) + && !exemptVanilla.contains(material); + } + + public static boolean customRequiresOpenSky(String cropId) { + return enabled && (cropId == null || !exemptCustom.contains(cropId.toLowerCase(Locale.ROOT))); + } + + public static boolean hasOpenSky(Location location) { + World world = location.getWorld(); + if (world == null) { + return false; + } + for (int y = location.getBlockY() + 1; y < world.getMaxHeight(); y++) { + Material cover = world.getBlockAt(location.getBlockX(), y, location.getBlockZ()).getType(); + if (!cover.isAir() && !allowedCover.contains(cover) + && !(allowGlass && (cover.name().endsWith("GLASS") || cover.name().endsWith("GLASS_PANE")))) { + return false; + } + } + return true; + } +} diff --git a/src/main/java/net/tfminecraft/cooking/crops/CropsLoader.java b/src/main/java/net/tfminecraft/cooking/crops/CropsLoader.java index ba6efb2..5b3edd5 100644 --- a/src/main/java/net/tfminecraft/cooking/crops/CropsLoader.java +++ b/src/main/java/net/tfminecraft/cooking/crops/CropsLoader.java @@ -22,9 +22,11 @@ public void load(File file) { } catch (IOException | InvalidConfigurationException ex) { Bukkit.getLogger().warning("[Cooking] Failed to load crops.yml: " + ex.getMessage()); CropsConfig.apply(Map.of(), Map.of(), Map.of(), true); + CropPlantingRule.configure(null); return; } + CropPlantingRule.configure(config.getConfigurationSection("planting")); ConfigurationSection harvest = config.getConfigurationSection("harvest-quality"); boolean growthGate = true; ConfigurationSection growth = config.getConfigurationSection("growth-gate"); diff --git a/src/main/resources/crops.yml b/src/main/resources/crops.yml index 9f294ce..8896654 100644 --- a/src/main/resources/crops.yml +++ b/src/main/resources/crops.yml @@ -3,6 +3,15 @@ # Growth ticks use the same affection: chance = (fertility/100) ^ affection. # Seeds stay untagged IA/vanilla. CustomCrops one-seed drops stay yield/balance. +planting: + require-open-sky: true + # Greenhouses: clear, stained and tinted glass, including panes, are allowed. + allow-glass-roofs: true + # Additional Bukkit block materials allowed anywhere above the crop. + allowed-cover: [] + exempt-vanilla: [NETHER_WART, BROWN_MUSHROOM, RED_MUSHROOM] + exempt-custom: [yeast] + growth-gate: enabled: true diff --git a/src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java b/src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java new file mode 100644 index 0000000..d445b8a --- /dev/null +++ b/src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java @@ -0,0 +1,139 @@ +package net.tfminecraft.cooking.crops; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockPlaceEvent; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.AfterEach; +import org.bukkit.configuration.file.YamlConfiguration; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; + +import net.momirealms.customcrops.api.core.mechanic.crop.CropConfig; +import net.momirealms.customcrops.api.event.CropPlantEvent; + +class CropPlantingTest { + private World world; + private Location location; + + @BeforeEach + void setup() { + CropPlantingRule.configure(null); + world = mock(World.class); + location = new Location(world, 4, 64, 8); + when(world.getMaxHeight()).thenReturn(320); + Block air = mock(Block.class); + when(air.getType()).thenReturn(Material.AIR); + when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(air); + } + + @Test + void outdoorsWorksAtNightAndBelowSeaLevel() { + assertTrue(CropPlantingRule.hasOpenSky(location)); + assertTrue(CropPlantingRule.hasOpenSky(new Location(world, 4, -20, 8))); + } + + @ParameterizedTest + @EnumSource(value = Material.class, names = {"STONE", "OAK_PLANKS", "OAK_SLAB", "OAK_LEAVES"}) + void caveAndHouseRoofsCancelVanillaPlanting(Material roof) { + roof(roof, 90); + Block crop = mock(Block.class); + when(crop.getType()).thenReturn(Material.WHEAT); + when(crop.getLocation()).thenReturn(location); + BlockPlaceEvent event = mock(BlockPlaceEvent.class); + Player player = mock(Player.class); + when(event.getBlockPlaced()).thenReturn(crop); + when(event.getPlayer()).thenReturn(player); + new CropPlantingListener().onPlant(event); + verify(event).setCancelled(true); + verify(player).sendMessage(CropPlantingListener.DENIAL_MESSAGE); + } + + @Test + void checksRoofAtTopOfWorldButIgnoresCropItself() { + roof(Material.WHEAT, 64); + assertTrue(CropPlantingRule.hasOpenSky(location)); + roof(Material.STONE, 319); + assertFalse(CropPlantingRule.hasOpenSky(location)); + } + + @Test + void customCropsCancelUnderStoneButYeastIsExempt() { + roof(Material.STONE, 66); + CropPlantEvent event = mock(CropPlantEvent.class); + CropConfig config = mock(CropConfig.class); + when(config.id()).thenReturn("tomato"); + when(event.cropConfig()).thenReturn(config); + when(event.location()).thenReturn(location); + when(event.getPlayer()).thenReturn(mock(Player.class)); + new CropCustomCropsListener().onPlant(event); + verify(event).setCancelled(true); + + reset(event); + when(config.id()).thenReturn("yeast"); + when(event.cropConfig()).thenReturn(config); + new CropCustomCropsListener().onPlant(event); + verify(event, never()).setCancelled(anyBoolean()); + } + + @Test + void wartMushroomsAndBuildingBlocksAreExempt() { + for (Material material : new Material[] {Material.NETHER_WART, Material.BROWN_MUSHROOM, + Material.RED_MUSHROOM, Material.OAK_PLANKS}) { + assertFalse(CropPlantingRule.requiresOpenSky(material)); + } + assertTrue(CropPlantingRule.requiresOpenSky(Material.MELON_STEM)); + assertTrue(CropPlantingRule.requiresOpenSky(Material.PUMPKIN_STEM)); + assertFalse(CropPlantingRule.customRequiresOpenSky("yeast")); + assertTrue(CropPlantingRule.customRequiresOpenSky("rice")); + } + + private void roof(Material material, int y) { + Block block = mock(Block.class); + when(block.getType()).thenReturn(material); + when(world.getBlockAt(4, y, 8)).thenReturn(block); + } + + @AfterEach + void resetConfig() { + CropPlantingRule.configure(null); + } + + @ParameterizedTest + @EnumSource(value = Material.class, names = {"GLASS", "GLASS_PANE", "RED_STAINED_GLASS", "BLUE_STAINED_GLASS_PANE", "TINTED_GLASS"}) + void greenhousesAllowedUnlessDisabled(Material glass) { + roof(glass, 70); + assertTrue(CropPlantingRule.hasOpenSky(location)); + YamlConfiguration config = new YamlConfiguration(); + config.set("allow-glass-roofs", false); + CropPlantingRule.configure(config); + assertFalse(CropPlantingRule.hasOpenSky(location)); + } + + @Test + void configurableCoverExemptionsAndDisable() { + YamlConfiguration config = new YamlConfiguration(); + config.set("allowed-cover", java.util.List.of("OAK_LEAVES")); + config.set("exempt-vanilla", java.util.List.of("WHEAT")); + config.set("exempt-custom", java.util.List.of("Tomato")); + CropPlantingRule.configure(config); + roof(Material.OAK_LEAVES, 70); + assertTrue(CropPlantingRule.hasOpenSky(location)); + assertFalse(CropPlantingRule.requiresOpenSky(Material.WHEAT)); + assertTrue(CropPlantingRule.requiresOpenSky(Material.NETHER_WART)); + assertFalse(CropPlantingRule.customRequiresOpenSky("tomato")); + assertTrue(CropPlantingRule.customRequiresOpenSky("yeast")); + config.set("require-open-sky", false); + CropPlantingRule.configure(config); + assertFalse(CropPlantingRule.requiresOpenSky(Material.CARROTS)); + assertFalse(CropPlantingRule.customRequiresOpenSky("rice")); + } +}