From b781e6f3124f3724e83fe22b83ff94c678a841fd Mon Sep 17 00:00:00 2001 From: XiaoMoMi <70987828+Xiao-MoMi@users.noreply.github.com> Date: Fri, 4 Oct 2024 21:14:24 +0800 Subject: [PATCH] add new method --- .../customcrops/api/BukkitCustomCropsAPI.java | 35 +++++++++++++++++++ .../customcrops/api/CustomCropsAPI.java | 8 +++++ 2 files changed, 43 insertions(+) diff --git a/api/src/main/java/net/momirealms/customcrops/api/BukkitCustomCropsAPI.java b/api/src/main/java/net/momirealms/customcrops/api/BukkitCustomCropsAPI.java index b78eea6..efef79f 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/BukkitCustomCropsAPI.java +++ b/api/src/main/java/net/momirealms/customcrops/api/BukkitCustomCropsAPI.java @@ -17,6 +17,8 @@ package net.momirealms.customcrops.api; +import net.momirealms.customcrops.api.action.ActionManager; +import net.momirealms.customcrops.api.context.Context; import net.momirealms.customcrops.api.core.BuiltInBlockMechanics; import net.momirealms.customcrops.api.core.ExistenceForm; import net.momirealms.customcrops.api.core.FurnitureRotation; @@ -37,6 +39,7 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.Nullable; +import java.util.Objects; import java.util.Optional; import static java.util.Objects.requireNonNull; @@ -71,6 +74,38 @@ public class BukkitCustomCropsAPI implements CustomCropsAPI { return plugin.getWorldManager().getWorld(world).orElse(null); } + @Override + public void addPointToCrop(Location location, int point) { + plugin.getWorldManager().getWorld(location.getWorld()).ifPresent(world -> { + Pos3 pos3 = Pos3.from(location); + world.getBlockState(pos3).ifPresent(state -> { + if (state.type() instanceof CropBlock cropBlock) { + CropConfig cropConfig = cropBlock.config(state); + if (cropConfig == null) return; + int currentPoints = cropBlock.point(state); + int afterPoints = Math.min(currentPoints + point, cropConfig.maxPoints()); + if (afterPoints == currentPoints) return; + cropBlock.point(state, afterPoints); + CropStageConfig currentStage = cropConfig.stageWithModelByPoint(currentPoints); + CropStageConfig nextStage = cropConfig.stageWithModelByPoint(afterPoints); + if (currentStage == nextStage) return; + FurnitureRotation rotation = plugin.getItemManager().remove(location, ExistenceForm.ANY); + if (rotation == FurnitureRotation.NONE && cropConfig.rotation()) { + rotation = FurnitureRotation.random(); + } + plugin.getItemManager().place(location, nextStage.existenceForm(), Objects.requireNonNull(nextStage.stageID()), rotation); + Context context = Context.block(state, location); + for (int i = currentPoints + 1; i <= afterPoints; i++) { + CropStageConfig stage = cropConfig.stageByPoint(i); + if (stage != null) { + ActionManager.trigger(context, stage.growActions()); + } + } + } + }); + }); + } + @Override public boolean placeCrop(Location location, String id, int point) { CropConfig cropConfig = Registries.CROP.get(id); diff --git a/api/src/main/java/net/momirealms/customcrops/api/CustomCropsAPI.java b/api/src/main/java/net/momirealms/customcrops/api/CustomCropsAPI.java index 5fa40fb..9d4df58 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/CustomCropsAPI.java +++ b/api/src/main/java/net/momirealms/customcrops/api/CustomCropsAPI.java @@ -52,6 +52,14 @@ public interface CustomCropsAPI { */ @Nullable CustomCropsWorld getCustomCropsWorld(World world); + /** + * Adds point to a crop at certain location + * + * @param location location + * @param point point to add + */ + void addPointToCrop(Location location, int point); + /** * Places a crop regardless of planting conditions such as pots. *