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

API improvement

This commit is contained in:
Xiao-MoMi
2022-10-19 14:32:54 +08:00
parent cd5bf4e2f7
commit c9ce385cdd
21 changed files with 98 additions and 58 deletions

View File

@@ -4,7 +4,7 @@ plugins {
}
group = 'net.momirealms'
version = '2.0-r9'
version = '2.0.1'
repositories {
mavenCentral()

View File

@@ -17,7 +17,7 @@
package net.momirealms.customcrops.api.crop;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.objects.GiganticCrop;
import net.momirealms.customcrops.objects.OtherLoot;
import net.momirealms.customcrops.objects.QualityLoot;

View File

@@ -22,19 +22,22 @@ import net.momirealms.customcrops.api.crop.Crop;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SeedPlantEvent extends PlayerEvent implements Cancellable {
public class SeedPlantEvent extends Event implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private final Location seedLoc;
private final Crop crop;
private final Player player;
public SeedPlantEvent(@NotNull Player who, Location seedLoc, Crop crop) {
super(who);
public SeedPlantEvent(@Nullable Player who, Location seedLoc, Crop crop) {
this.player = who;
this.cancelled = false;
this.seedLoc = seedLoc;
this.crop = crop;
@@ -62,4 +65,12 @@ public class SeedPlantEvent extends PlayerEvent implements Cancellable {
public Crop getCrop() {
return crop;
}
/**
* return null if this is not triggered by a player
*/
@Nullable
public Player getPlayer() {
return player;
}
}

View File

@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customcrops.integrations.season;
package net.momirealms.customcrops.api.utils;
public enum CCSeason {

View File

@@ -17,8 +17,10 @@
package net.momirealms.customcrops.api.utils;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.crop.Crop;
import net.momirealms.customcrops.config.CropConfig;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;
public class CropUtils {
@@ -32,4 +34,14 @@ public class CropUtils {
public static Crop getCrop(String crop) {
return CropConfig.CROPS.get(crop);
}
/**
* whether planting succeeds
* @param location location
* @param crop crop
* @return
*/
public static boolean plantCrop(Location location, String crop) {
return CustomCrops.plugin.getCropManager().getHandler().plantSeed(location, crop, null, null);
}
}

View File

@@ -18,7 +18,7 @@
package net.momirealms.customcrops.api.utils;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.config.MessageConfig;
import org.bukkit.World;
import org.jetbrains.annotations.NotNull;
@@ -50,4 +50,14 @@ public class SeasonUtils {
public static void unloadSeason(World world) {
CustomCrops.plugin.getCropManager().getSeasonAPI().unloadWorld(world);
}
public static String getSeasonText(CCSeason season) {
return switch (season) {
case SPRING -> MessageConfig.spring;
case SUMMER -> MessageConfig.summer;
case AUTUMN -> MessageConfig.autumn;
case WINTER -> MessageConfig.winter;
default -> throw new IllegalStateException("Unexpected value: " + season);
};
}
}

View File

@@ -22,7 +22,7 @@ import net.momirealms.customcrops.commands.AbstractSubCommand;
import net.momirealms.customcrops.commands.SubCommand;
import net.momirealms.customcrops.config.MessageConfig;
import net.momirealms.customcrops.config.SeasonConfig;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.utils.AdventureUtil;
import org.bukkit.Bukkit;
import org.bukkit.World;

View File

@@ -18,7 +18,7 @@
package net.momirealms.customcrops.config;
import net.momirealms.customcrops.api.crop.Crop;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.objects.CCCrop;
import net.momirealms.customcrops.objects.GiganticCrop;
import net.momirealms.customcrops.objects.OtherLoot;

View File

@@ -24,8 +24,9 @@ import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.crop.Crop;
import net.momirealms.customcrops.api.event.*;
import net.momirealms.customcrops.api.utils.SeasonUtils;
import net.momirealms.customcrops.config.*;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.managers.CropManager;
import net.momirealms.customcrops.managers.CustomWorld;
import net.momirealms.customcrops.managers.listener.InteractListener;
@@ -535,53 +536,54 @@ public abstract class HandlerP extends Function {
}
}
protected void plantSeed(Location seedLoc, String cropName, Player player, ItemStack itemInHand, boolean isOraxen, boolean isWire) {
public boolean plantSeed(Location seedLoc, String cropName, @Nullable Player player, @Nullable ItemStack itemInHand) {
Crop crop = CropConfig.CROPS.get(cropName);
if (crop == null) return;
if (crop == null) return false;
CustomWorld customWorld = cropManager.getCustomWorld(seedLoc.getWorld());
if (customWorld == null) return;
if (customWorld == null) return false;
if (!isOraxen && FurnitureUtil.hasFurniture(seedLoc.clone().add(0.5,0.5,0.5))) return;
if (isOraxen && FurnitureUtil.hasFurniture(seedLoc.clone().add(0.5,0.03125,0.5))) return;
if (seedLoc.getBlock().getType() != Material.AIR) return;
PlantingCondition plantingCondition = new PlantingCondition(seedLoc, player);
if (!MainConfig.OraxenHook && FurnitureUtil.hasFurniture(seedLoc.clone().add(0.5,0.5,0.5))) return false;
if (MainConfig.OraxenHook && FurnitureUtil.hasFurniture(seedLoc.clone().add(0.5,0.03125,0.5))) return false;
if (seedLoc.getBlock().getType() != Material.AIR) return false;
CCSeason[] seasons = crop.getSeasons();
if (SeasonConfig.enable && seasons != null) {
if (cropManager.isWrongSeason(seedLoc, seasons)) {
if (MainConfig.notifyInWrongSeason) AdventureUtil.playerMessage(player, MessageConfig.prefix + MessageConfig.wrongSeason);
if (MainConfig.preventInWrongSeason) return;
if (MainConfig.notifyInWrongSeason) AdventureUtil.playerMessage(player, MessageConfig.prefix + MessageConfig.wrongSeason.replace("{season}", SeasonUtils.getSeasonText(SeasonUtils.getSeason(seedLoc.getWorld()))));
if (MainConfig.preventInWrongSeason) return false;
}
}
if (player != null) {
PlantingCondition plantingCondition = new PlantingCondition(seedLoc, player);
if (crop.getRequirements() != null) {
for (RequirementInterface requirement : crop.getRequirements()) {
if (!requirement.isConditionMet(plantingCondition)) {
return;
return false;
}
}
}
}
if (MainConfig.limitation) {
if (isWire && LimitationUtil.reachWireLimit(seedLoc)) {
AdventureUtil.playerMessage(player, MessageConfig.prefix + MessageConfig.limitWire.replace("{max}", String.valueOf(MainConfig.wireAmount)));
return;
if (MainConfig.cropMode && LimitationUtil.reachWireLimit(seedLoc)) {
if (player != null) AdventureUtil.playerMessage(player, MessageConfig.prefix + MessageConfig.limitWire.replace("{max}", String.valueOf(MainConfig.wireAmount)));
return false;
}
if (!isWire && LimitationUtil.reachFrameLimit(seedLoc)) {
AdventureUtil.playerMessage(player, MessageConfig.prefix + MessageConfig.limitFrame.replace("{max}", String.valueOf(MainConfig.frameAmount)));
return;
if (!MainConfig.cropMode && LimitationUtil.reachFrameLimit(seedLoc)) {
if (player != null) AdventureUtil.playerMessage(player, MessageConfig.prefix + MessageConfig.limitFrame.replace("{max}", String.valueOf(MainConfig.frameAmount)));
return false;
}
}
SeedPlantEvent seedPlantEvent = new SeedPlantEvent(player, seedLoc, crop);
Bukkit.getPluginManager().callEvent(seedPlantEvent);
if (seedPlantEvent.isCancelled()) {
return;
return false;
}
if (SoundConfig.plantSeed.isEnable()) {
if (SoundConfig.plantSeed.isEnable() && player != null) {
AdventureUtil.playerSound(
player,
SoundConfig.plantSeed.getSource(),
@@ -590,13 +592,14 @@ public abstract class HandlerP extends Function {
);
}
if (player.getGameMode() != GameMode.CREATIVE) itemInHand.setAmount(itemInHand.getAmount() - 1);
if (isWire) customInterface.placeWire(seedLoc, cropName + "_stage_1");
if (itemInHand != null && player != null && player.getGameMode() != GameMode.CREATIVE) itemInHand.setAmount(itemInHand.getAmount() - 1);
if (MainConfig.cropMode) customInterface.placeWire(seedLoc, cropName + "_stage_1");
else {
ItemFrame itemFrame = customInterface.placeFurniture(seedLoc, cropName + "_stage_1");
if (itemFrame == null) return;
if (itemFrame == null) return false;
itemFrame.setRotation(FurnitureUtil.getRandomRotation());
}
customWorld.addCrop(seedLoc, cropName);
return true;
}
}

View File

@@ -188,13 +188,13 @@ public class ItemsAdderFrameHandler extends ItemsAdderHandler {
String namespacedID = customStack.getNamespacedID();
if (namespacedID.endsWith("_seeds")) {
String cropName = customStack.getId().substring(0, customStack.getId().length() - 6);
plantSeed(seedLoc, cropName, player, itemInHand, false, false);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}
else if (MainConfig.enableConvert) {
String cropName = MainConfig.vanilla2Crops.get(itemInHand.getType());
if (cropName == null) return;
plantSeed(seedLoc, cropName, player, itemInHand, false, false);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}
}

View File

@@ -163,13 +163,13 @@ public class ItemsAdderWireHandler extends ItemsAdderHandler {
String namespacedID = customStack.getNamespacedID();
if (namespacedID.endsWith("_seeds")) {
String cropName = customStack.getId().substring(0, customStack.getId().length() - 6);
plantSeed(seedLoc, cropName, player, itemInHand, false, true);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}
else if (MainConfig.enableConvert) {
String cropName = MainConfig.vanilla2Crops.get(itemInHand.getType());
if (cropName == null) return;
plantSeed(seedLoc, cropName, player, itemInHand, false, true);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}
}

View File

@@ -142,13 +142,13 @@ public class OraxenFrameHandler extends OraxenHandler {
if (id != null) {
if (id.endsWith("_seeds")) {
String cropName = id.substring(0, id.length() - 6);
plantSeed(seedLoc, cropName, player, itemInHand, true, false);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}
else if (MainConfig.enableConvert) {
String cropName = MainConfig.vanilla2Crops.get(itemInHand.getType());
if (cropName == null) return;
plantSeed(seedLoc, cropName, player, itemInHand, true, false);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}
}

View File

@@ -184,13 +184,13 @@ public class OraxenWireHandler extends OraxenHandler{
if (id != null) {
if (id.endsWith("_seeds")) {
String cropName = id.substring(0, id.length() - 6);
plantSeed(seedLoc, cropName, player, itemInHand, true, true);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}
else if (MainConfig.enableConvert) {
String cropName = MainConfig.vanilla2Crops.get(itemInHand.getType());
if (cropName == null) return;
plantSeed(seedLoc, cropName, player, itemInHand, true, true);
plantSeed(seedLoc, cropName, player, itemInHand);
}
}

View File

@@ -22,7 +22,7 @@ import net.momirealms.customcrops.api.utils.SeasonUtils;
import net.momirealms.customcrops.config.MainConfig;
import net.momirealms.customcrops.config.MessageConfig;
import net.momirealms.customcrops.config.SeasonConfig;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
@@ -97,12 +97,6 @@ public class SeasonPapi extends PlaceholderExpansion {
private String getSeasonText(World world) {
CCSeason season = SeasonUtils.getSeason(world);
return switch (season) {
case SPRING -> MessageConfig.spring;
case SUMMER -> MessageConfig.summer;
case AUTUMN -> MessageConfig.autumn;
case WINTER -> MessageConfig.winter;
default -> throw new IllegalStateException("Unexpected value: " + season);
};
return SeasonUtils.getSeasonText(season);
}
}

View File

@@ -18,6 +18,7 @@
package net.momirealms.customcrops.integrations.season;
import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.config.MainConfig;
import net.momirealms.customcrops.config.SeasonConfig;
import net.momirealms.customcrops.objects.Function;

View File

@@ -18,6 +18,7 @@
package net.momirealms.customcrops.integrations.season;
import me.casperge.realisticseasons.api.SeasonsAPI;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.config.MainConfig;
import net.momirealms.customcrops.objects.Function;
import org.bukkit.World;
@@ -28,6 +29,10 @@ public class RealisticSeasonsHook extends Function implements SeasonInterface {
private SeasonsAPI api;
public RealisticSeasonsHook() {
load();
}
@Override
public void load() {
super.load();

View File

@@ -17,6 +17,7 @@
package net.momirealms.customcrops.integrations.season;
import net.momirealms.customcrops.api.utils.CCSeason;
import org.bukkit.World;
public interface SeasonInterface {

View File

@@ -29,7 +29,7 @@ import net.momirealms.customcrops.integrations.customplugin.CustomInterface;
import net.momirealms.customcrops.integrations.customplugin.HandlerP;
import net.momirealms.customcrops.integrations.customplugin.itemsadder.*;
import net.momirealms.customcrops.integrations.customplugin.oraxen.*;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.integrations.season.InternalSeason;
import net.momirealms.customcrops.integrations.season.RealisticSeasonsHook;
import net.momirealms.customcrops.integrations.season.SeasonInterface;
@@ -57,7 +57,6 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
public class CropManager extends Function {
@@ -465,4 +464,8 @@ public class CropManager extends Function {
public ArmorStandUtil getArmorStandUtil() {
return armorStandUtil;
}
public HandlerP getHandler() {
return handler;
}
}

View File

@@ -22,7 +22,7 @@ import net.momirealms.customcrops.CustomCrops;
import net.momirealms.customcrops.api.event.CustomWorldEvent;
import net.momirealms.customcrops.api.utils.SeasonUtils;
import net.momirealms.customcrops.config.*;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.objects.SimpleLocation;
import net.momirealms.customcrops.objects.Sprinkler;
import net.momirealms.customcrops.objects.WorldState;
@@ -59,7 +59,6 @@ public class CustomWorld {
private final BukkitScheduler bukkitScheduler;
private final HashSet<SimpleLocation> plantedToday;
private final CropModeInterface cropMode;
private YamlConfiguration cropData;
public CustomWorld(World world, CropManager cropManager) {
@@ -332,6 +331,7 @@ public class CustomWorld {
}
public void growWire(int cropTime, int sprinklerTime, int dryTime, boolean compensation, boolean force) {
if (cropData == null) return;
Random randomGenerator = new Random();
if (force) {

View File

@@ -18,7 +18,7 @@
package net.momirealms.customcrops.objects;
import net.momirealms.customcrops.api.crop.Crop;
import net.momirealms.customcrops.integrations.season.CCSeason;
import net.momirealms.customcrops.api.utils.CCSeason;
import net.momirealms.customcrops.objects.actions.ActionInterface;
import net.momirealms.customcrops.objects.requirements.RequirementInterface;

View File

@@ -17,7 +17,7 @@ messages:
grow-simulation: '<white>Growing simulation has started'
set-season: '<white>Successfully set {world}''s season to {season}!'
before-plant: '<white>This fertilizer must be used before planting!'
wrong-season: '<white>Current season is not suitable for planting this seed'
wrong-season: '<white>Current season is not suitable for planting this seed. Suitable season: {season}'
no-season: 'No season'
season-disabled: 'Season is disabled'