9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-19 15:09:25 +00:00

Improve crop tick

This commit is contained in:
XiaoMoMi
2025-02-24 18:16:43 +08:00
parent 08e3ba6f1d
commit b1501bee9f
5 changed files with 83 additions and 6 deletions

View File

@@ -62,7 +62,7 @@ public class CropBlock extends AbstractCustomCropsBlock {
// ignore random tick
if (world.setting().tickCropMode() == 1) return;
if (canTick(state, world.setting().tickCropInterval())) {
tickCrop(state, world, location, offlineTick);
tickCrop(state, world, location, offlineTick, false);
}
}
@@ -71,7 +71,7 @@ public class CropBlock extends AbstractCustomCropsBlock {
// ignore scheduled tick
if (world.setting().tickCropMode() == 2) return;
if (canTick(state, world.setting().tickCropInterval())) {
tickCrop(state, world, location, offlineTick);
tickCrop(state, world, location, offlineTick, true);
}
}
@@ -340,7 +340,7 @@ public class CropBlock extends AbstractCustomCropsBlock {
return state;
}
private void tickCrop(CustomCropsBlockState state, CustomCropsWorld<?> world, Pos3 location, boolean offline) {
private void tickCrop(CustomCropsBlockState state, CustomCropsWorld<?> world, Pos3 location, boolean offline, boolean tickMode) {
CropConfig config = config(state);
BukkitCustomCropsPlugin plugin = BukkitCustomCropsPlugin.getInstance();
if (config == null) {
@@ -349,6 +349,9 @@ public class CropBlock extends AbstractCustomCropsBlock {
return;
}
if (tickMode && config.ignoreRandomTick()) return;
if (!tickMode && config.ignoreScheduledTick()) return;
int previousPoint = point(state);
World bukkitWorld = world.bukkitWorld();
Location bukkitLocation = location.toLocation(bukkitWorld);

View File

@@ -18,6 +18,7 @@
package net.momirealms.customcrops.api.core.mechanic.crop;
import net.momirealms.customcrops.api.action.Action;
import net.momirealms.customcrops.api.core.mechanic.pot.PotConfig;
import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
import net.momirealms.customcrops.api.requirement.Requirement;
import org.bukkit.entity.Player;
@@ -207,6 +208,20 @@ public interface CropConfig {
return new CropConfigImpl.BuilderImpl();
}
/**
* Should the crop ignore scheduled tick?
*
* @return ignore or not
*/
boolean ignoreScheduledTick();
/**
* Should the crop ignore random tick?
*
* @return ignore or not
*/
boolean ignoreRandomTick();
/**
* Builder interface for constructing instances of {@link CropConfig}.
*/
@@ -355,5 +370,21 @@ public interface CropConfig {
* @return The builder instance for chaining.
*/
Builder stages(Collection<CropStageConfig.Builder> stages);
/**
* Sets whether the crop ignores random tick
*
* @param ignoreRandomTick True if ignore random tick
* @return The current instance of the Builder.
*/
Builder ignoreRandomTick(boolean ignoreRandomTick);
/**
* Sets whether the crop ignores scheduled tick
*
* @param ignoreScheduledTick True if ignore scheduled tick
* @return The current instance of the Builder.
*/
Builder ignoreScheduledTick(boolean ignoreScheduledTick);
}
}

View File

@@ -50,6 +50,8 @@ public class CropConfigImpl implements CropConfig {
private final HashMap<String, CropStageConfig> id2Stages = new HashMap<>();
private final Set<String> stageIDs = new HashSet<>();
private final HashMap<Integer, CropStageConfig> cropStageWithModelMap = new HashMap<>();
private final boolean ignoreScheduledTick;
private final boolean ignoreRandomTick;
public CropConfigImpl(
String id,
@@ -69,7 +71,9 @@ public class CropConfigImpl implements CropConfig {
BoneMeal[] boneMeals,
boolean rotation,
Set<String> potWhitelist,
Collection<CropStageConfig.Builder> stageBuilders
Collection<CropStageConfig.Builder> stageBuilders,
boolean ignoreScheduledTick,
boolean ignoreRandomTick
) {
this.id = id;
this.seed = seed;
@@ -88,6 +92,8 @@ public class CropConfigImpl implements CropConfig {
this.boneMeals = boneMeals;
this.rotation = rotation;
this.potWhitelist = potWhitelist;
this.ignoreRandomTick = ignoreRandomTick;
this.ignoreScheduledTick = ignoreScheduledTick;
for (CropStageConfig.Builder builder : stageBuilders) {
CropStageConfig config = builder.crop(this).build();
point2Stages.put(config.point(), config);
@@ -228,8 +234,17 @@ public class CropConfigImpl implements CropConfig {
return navigablePoint2Stages.floorEntry(point);
}
public static class BuilderImpl implements Builder {
@Override
public boolean ignoreScheduledTick() {
return ignoreScheduledTick;
}
@Override
public boolean ignoreRandomTick() {
return ignoreRandomTick;
}
public static class BuilderImpl implements Builder {
private String id;
private String seed;
private ExistenceForm existenceForm;
@@ -249,10 +264,12 @@ public class CropConfigImpl implements CropConfig {
private boolean rotation;
private Set<String> potWhitelist;
private Collection<CropStageConfig.Builder> stages;
private boolean ignoreScheduledTick;
private boolean ignoreRandomTick;
@Override
public CropConfig build() {
return new CropConfigImpl(id, seed, maxPoints, wrongPotActions, interactActions, breakActions, plantActions, reachLimitActions, deathActions, plantRequirements, breakRequirements, interactRequirements, growConditions, deathConditions, boneMeals, rotation, potWhitelist, stages);
return new CropConfigImpl(id, seed, maxPoints, wrongPotActions, interactActions, breakActions, plantActions, reachLimitActions, deathActions, plantRequirements, breakRequirements, interactRequirements, growConditions, deathConditions, boneMeals, rotation, potWhitelist, stages, ignoreScheduledTick, ignoreRandomTick);
}
@Override
@@ -361,5 +378,17 @@ public class CropConfigImpl implements CropConfig {
this.stages = new HashSet<>(stages);
return this;
}
@Override
public Builder ignoreRandomTick(boolean ignoreRandomTick) {
this.ignoreRandomTick = ignoreRandomTick;
return this;
}
@Override
public Builder ignoreScheduledTick(boolean ignoreScheduledTick) {
this.ignoreScheduledTick = ignoreScheduledTick;
return this;
}
}
}

View File

@@ -275,8 +275,20 @@ public interface PotConfig {
*/
Builder isNearbyWaterAccepted(boolean isNearbyWaterAccepted);
/**
* Sets whether the pot ignores random tick
*
* @param ignoreRandomTick True if ignore random tick
* @return The current instance of the Builder.
*/
Builder ignoreRandomTick(boolean ignoreRandomTick);
/**
* Sets whether the pot ignores scheduled tick
*
* @param ignoreScheduledTick True if ignore scheduled tick
* @return The current instance of the Builder.
*/
Builder ignoreScheduledTick(boolean ignoreScheduledTick);
/**

View File

@@ -209,6 +209,8 @@ public class ConfigType {
.seed(section.getString("seed"))
.rotation(section.getBoolean("random-rotation", false))
.maxPoints(section.getInt("max-points", 1))
.ignoreRandomTick(section.getBoolean("ignore-random-tick", false))
.ignoreScheduledTick(section.getBoolean("ignore-scheduled-tick", false))
.potWhitelist(new HashSet<>(section.getStringList("pot-whitelist")))
.wrongPotActions(pam.parseActions(section.getSection("events.wrong_pot")))
.plantActions(pam.parseActions(section.getSection("events.plant")))