mirror of
https://github.com/Xiao-MoMi/Custom-Crops.git
synced 2025-12-24 09:29:19 +00:00
add new method
This commit is contained in:
@@ -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<CustomCropsBlockState> 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);
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user