Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/tfminecraft/cooking/Cooking.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,6 +32,15 @@ public final class CropCustomCropsListener implements Listener {

private final Map<String, PendingHarvest> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/net/tfminecraft/cooking/crops/CropPlantingRule.java
Original file line number Diff line number Diff line change
@@ -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<Material> 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;
}
}
101 changes: 101 additions & 0 deletions src/test/java/net/tfminecraft/cooking/crops/CropPlantingTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}