From 44d3e9c20abfd417734610f30c71d27ebbe5b5b4 Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Thu, 1 Oct 2026 17:27:52 +0200 Subject: [PATCH 1/2] fix: require open sky when planting food crops --- README.md | 2 + .../java/net/tfminecraft/cooking/Cooking.java | 2 + .../crops/CropCustomCropsListener.java | 10 ++ .../cooking/crops/CropPlantingListener.java | 22 ++++ .../cooking/crops/CropPlantingRule.java | 40 +++++++ .../cooking/crops/CropPlantingTest.java | 101 ++++++++++++++++++ 6 files changed, 177 insertions(+) create mode 100644 src/main/java/net/tfminecraft/cooking/crops/CropPlantingListener.java create mode 100644 src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java create mode 100644 src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java diff --git a/README.md b/README.md index dcd570d..165005f 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ Cooking makes food an interactive part of everyday life. Players grow and gather ## A complete food journey +Food crops must be planted outdoors with an open column of sky above them. Cave ceilings, building roofs (including glass), and other overhead blocks prevent vanilla and CustomCrops planting, including automatic replanting. Nether wart, mushrooms, and the CustomCrops yeast crop can still be planted indoors. + 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..250de81 --- /dev/null +++ b/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java @@ -0,0 +1,40 @@ +package net.tfminecraft.cooking.crops; + +import java.util.Set; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; + +/** Food crops need an open column above them, even under transparent roofs. */ +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 CropPlantingRule() {} + + public static boolean requiresOpenSky(Material material) { + return OUTDOOR_CROPS.contains(material); + } + + public static boolean customRequiresOpenSky(String cropId) { + // Yeast is the fungal crop in the CustomCrops catalog. + return !"yeast".equalsIgnoreCase(cropId); + } + + 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++) { + if (!world.getBlockAt(location.getBlockX(), y, location.getBlockZ()).getType().isAir()) { + return false; + } + } + return 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..cec540e --- /dev/null +++ b/src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java @@ -0,0 +1,101 @@ +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.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() { + 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", "GLASS", "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.GLASS, 319); + assertFalse(CropPlantingRule.hasOpenSky(location)); + } + + @Test + void customCropsCancelUnderGlassButYeastIsExempt() { + roof(Material.GLASS, 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); + } +} From 777be74af15c3849a3e5ab38c3225fa357bb8153 Mon Sep 17 00:00:00 2001 From: XxFran10xX <318299142+XxFran10xX@users.noreply.github.com> Date: Thu, 1 Oct 2026 17:37:23 +0200 Subject: [PATCH 2/2] feat: allow greenhouses and configure crop planting restrictions --- README.md | 4 +- .../cooking/crops/CropPlantingRule.java | 47 +++++++++++++++++-- .../cooking/crops/CropsLoader.java | 2 + src/main/resources/crops.yml | 9 ++++ .../cooking/crops/CropPlantingTest.java | 46 ++++++++++++++++-- 5 files changed, 98 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 165005f..471ab7e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,9 @@ Cooking makes food an interactive part of everyday life. Players grow and gather ## A complete food journey -Food crops must be planted outdoors with an open column of sky above them. Cave ceilings, building roofs (including glass), and other overhead blocks prevent vanilla and CustomCrops planting, including automatic replanting. Nether wart, mushrooms, and the CustomCrops yeast crop can still be planted indoors. +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. diff --git a/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java b/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java index 250de81..40e5ca1 100644 --- a/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java +++ b/src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java @@ -1,12 +1,15 @@ 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 above them, even under transparent roofs. */ +/** 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, @@ -14,15 +17,47 @@ public final class CropPlantingRule { 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 OUTDOOR_CROPS.contains(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) { - // Yeast is the fungal crop in the CustomCrops catalog. - return !"yeast".equalsIgnoreCase(cropId); + return enabled && (cropId == null || !exemptCustom.contains(cropId.toLowerCase(Locale.ROOT))); } public static boolean hasOpenSky(Location location) { @@ -31,7 +66,9 @@ public static boolean hasOpenSky(Location location) { return false; } for (int y = location.getBlockY() + 1; y < world.getMaxHeight(); y++) { - if (!world.getBlockAt(location.getBlockX(), y, location.getBlockZ()).getType().isAir()) { + 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; } } 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 index cec540e..d445b8a 100644 --- a/src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java +++ b/src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java @@ -11,6 +11,8 @@ 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; @@ -24,6 +26,7 @@ class CropPlantingTest { @BeforeEach void setup() { + CropPlantingRule.configure(null); world = mock(World.class); location = new Location(world, 4, 64, 8); when(world.getMaxHeight()).thenReturn(320); @@ -39,7 +42,7 @@ void outdoorsWorksAtNightAndBelowSeaLevel() { } @ParameterizedTest - @EnumSource(value = Material.class, names = {"STONE", "OAK_PLANKS", "GLASS", "OAK_SLAB", "OAK_LEAVES"}) + @EnumSource(value = Material.class, names = {"STONE", "OAK_PLANKS", "OAK_SLAB", "OAK_LEAVES"}) void caveAndHouseRoofsCancelVanillaPlanting(Material roof) { roof(roof, 90); Block crop = mock(Block.class); @@ -58,13 +61,13 @@ void caveAndHouseRoofsCancelVanillaPlanting(Material roof) { void checksRoofAtTopOfWorldButIgnoresCropItself() { roof(Material.WHEAT, 64); assertTrue(CropPlantingRule.hasOpenSky(location)); - roof(Material.GLASS, 319); + roof(Material.STONE, 319); assertFalse(CropPlantingRule.hasOpenSky(location)); } @Test - void customCropsCancelUnderGlassButYeastIsExempt() { - roof(Material.GLASS, 66); + void customCropsCancelUnderStoneButYeastIsExempt() { + roof(Material.STONE, 66); CropPlantEvent event = mock(CropPlantEvent.class); CropConfig config = mock(CropConfig.class); when(config.id()).thenReturn("tomato"); @@ -98,4 +101,39 @@ private void roof(Material material, int y) { 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")); + } }