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

Added more vanilla blocks support

This commit is contained in:
XiaoMoMi
2024-09-07 18:22:27 +08:00
parent eb3cb7b0d6
commit 558ced3517
9 changed files with 58 additions and 22 deletions

View File

@@ -359,7 +359,7 @@ public class PotBlock extends AbstractCustomCropsBlock {
future.thenAcceptAsync((run) -> { future.thenAcceptAsync((run) -> {
if (!run) return; if (!run) return;
// work as vanilla farmland // work as vanilla farmland
if (config.vanillaFarmland()) return; if (config.disablePluginMechanism()) return;
boolean hasNaturalWater = false; boolean hasNaturalWater = false;
boolean waterChanged = false; boolean waterChanged = false;
@@ -620,7 +620,7 @@ public class PotBlock extends AbstractCustomCropsBlock {
} }
public void updateBlockAppearance(Location location, PotConfig config, boolean hasWater, @Nullable Fertilizer fertilizer) { public void updateBlockAppearance(Location location, PotConfig config, boolean hasWater, @Nullable Fertilizer fertilizer) {
if (config.vanillaFarmland()) return; if (config.disablePluginMechanism()) return;
String appearance = config.getPotAppearance(hasWater, fertilizer == null ? null : fertilizer.type()); String appearance = config.getPotAppearance(hasWater, fertilizer == null ? null : fertilizer.type());
BukkitCustomCropsPlugin.getInstance().getItemManager().placeBlock(location, appearance); BukkitCustomCropsPlugin.getInstance().getItemManager().placeBlock(location, appearance);
} }

View File

@@ -49,7 +49,6 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class SprinklerBlock extends AbstractCustomCropsBlock { public class SprinklerBlock extends AbstractCustomCropsBlock {
@@ -295,7 +294,7 @@ public class SprinklerBlock extends AbstractCustomCropsBlock {
CustomCropsBlockState anotherState = optionalState.get(); CustomCropsBlockState anotherState = optionalState.get();
if (anotherState.type() instanceof PotBlock potBlock) { if (anotherState.type() instanceof PotBlock potBlock) {
PotConfig potConfig = potBlock.config(anotherState); PotConfig potConfig = potBlock.config(anotherState);
if (!potConfig.vanillaFarmland()) { if (!potConfig.disablePluginMechanism()) {
if (config.potWhitelist().contains(potConfig.id())) { if (config.potWhitelist().contains(potConfig.id())) {
if (potBlock.addWater(anotherState, potConfig, config.wateringAmount())) { if (potBlock.addWater(anotherState, potConfig, config.wateringAmount())) {
BukkitCustomCropsPlugin.getInstance().getScheduler().sync().run( BukkitCustomCropsPlugin.getInstance().getScheduler().sync().run(

View File

@@ -82,6 +82,9 @@ public class FertilizerItem extends AbstractCustomCropsItem {
// if the clicked block is a pot // if the clicked block is a pot
PotConfig potConfig = Registries.ITEM_TO_POT.get(targetBlockID); PotConfig potConfig = Registries.ITEM_TO_POT.get(targetBlockID);
if (potConfig != null) { if (potConfig != null) {
if (potConfig.disablePluginMechanism()) {
return InteractionResult.COMPLETE;
}
// check pot whitelist // check pot whitelist
if (!fertilizerConfig.whitelistPots().contains(potConfig.id())) { if (!fertilizerConfig.whitelistPots().contains(potConfig.id())) {
ActionManager.trigger(context, fertilizerConfig.wrongPotActions()); ActionManager.trigger(context, fertilizerConfig.wrongPotActions());
@@ -115,7 +118,7 @@ public class FertilizerItem extends AbstractCustomCropsItem {
.id(fertilizerConfig.id()) .id(fertilizerConfig.id())
.build(); .build();
CustomCropsBlockState potState = potBlock.fixOrGetState(world, Pos3.from(targetLocation), potConfig, event.relatedID()); CustomCropsBlockState potState = potBlock.fixOrGetState(world, Pos3.from(targetLocation), potConfig, event.relatedID());
if (!potBlock.canApplyFertilizer(potState,fertilizer)) { if (!potBlock.canApplyFertilizer(potState, fertilizer)) {
ActionManager.trigger(context, potConfig.maxFertilizerActions()); ActionManager.trigger(context, potConfig.maxFertilizerActions());
return InteractionResult.COMPLETE; return InteractionResult.COMPLETE;
} }

View File

@@ -286,7 +286,7 @@ public class WateringCanItem extends AbstractCustomCropsItem {
PotConfig potConfig = Registries.ITEM_TO_POT.get(targetBlockID); PotConfig potConfig = Registries.ITEM_TO_POT.get(targetBlockID);
if (potConfig != null) { if (potConfig != null) {
if (potConfig.vanillaFarmland()) if (potConfig.disablePluginMechanism())
return InteractionResult.COMPLETE; return InteractionResult.COMPLETE;
// need to click the upper face // need to click the upper face
if (blockFace != BlockFace.UP) if (blockFace != BlockFace.UP)

View File

@@ -27,6 +27,7 @@ import net.momirealms.customcrops.common.util.Pair;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
@@ -67,7 +68,7 @@ public interface PotConfig {
* *
* @return True if disabled * @return True if disabled
*/ */
boolean vanillaFarmland(); boolean disablePluginMechanism();
/** /**
* Gets the methods available for watering the pot. * Gets the methods available for watering the pot.
@@ -236,6 +237,14 @@ public interface PotConfig {
*/ */
Builder vanillaFarmland(boolean vanillaFarmland); Builder vanillaFarmland(boolean vanillaFarmland);
/**
* Mark this pot as a vanilla one and disable plugin mechanisms
*
* @param vanillaPots pots
* @return The current instance of the Builder.
*/
Builder vanillaPots(List<String> vanillaPots);
/** /**
* Sets whether the pot can accept rain as a source of water. * Sets whether the pot can accept rain as a source of water.
* *

View File

@@ -24,16 +24,17 @@ import net.momirealms.customcrops.api.misc.water.WaterBar;
import net.momirealms.customcrops.api.misc.water.WateringMethod; import net.momirealms.customcrops.api.misc.water.WateringMethod;
import net.momirealms.customcrops.api.requirement.Requirement; import net.momirealms.customcrops.api.requirement.Requirement;
import net.momirealms.customcrops.common.util.Pair; import net.momirealms.customcrops.common.util.Pair;
import org.bukkit.Material;
import org.bukkit.block.data.type.Farmland;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Set;
public class PotConfigImpl implements PotConfig { public class PotConfigImpl implements PotConfig {
private final String id; private final String id;
private final boolean vanillaFarmland; private final boolean vanillaFarmland;
private final boolean disablePluginSystem;
private final Pair<String, String> basicAppearance; private final Pair<String, String> basicAppearance;
private final HashMap<FertilizerType, Pair<String, String>> potAppearanceMap; private final HashMap<FertilizerType, Pair<String, String>> potAppearanceMap;
private final Set<String> blocks = new HashSet<>(); private final Set<String> blocks = new HashSet<>();
@@ -77,7 +78,8 @@ public class PotConfigImpl implements PotConfig {
Action<Player>[] breakActions, Action<Player>[] breakActions,
Action<Player>[] addWaterActions, Action<Player>[] addWaterActions,
Action<Player>[] fullWaterActions, Action<Player>[] fullWaterActions,
Action<Player>[] maxFertilizerActions Action<Player>[] maxFertilizerActions,
List<String> vanillaPots
) { ) {
this.id = id; this.id = id;
this.vanillaFarmland = vanillaFarmland; this.vanillaFarmland = vanillaFarmland;
@@ -109,10 +111,19 @@ public class PotConfigImpl implements PotConfig {
this.wetBlocks.add(pair.right()); this.wetBlocks.add(pair.right());
} }
if (vanillaFarmland) { if (vanillaFarmland) {
disablePluginSystem = true;
this.blocks.clear(); this.blocks.clear();
for (int i = 0; i <= 7; i++) { for (int i = 0; i <= 7; i++) {
this.blocks.add("minecraft:farmland[moisture=" + i +"]"); Farmland data = (Farmland) Material.FARMLAND.createBlockData();
data.setMoisture(i);
this.blocks.add(data.getAsString());
} }
} else if (vanillaPots != null && !vanillaPots.isEmpty()) {
disablePluginSystem = true;
this.blocks.clear();
this.blocks.addAll(vanillaPots);
} else {
disablePluginSystem = false;
} }
} }
@@ -123,28 +134,28 @@ public class PotConfigImpl implements PotConfig {
@Override @Override
public int storage() { public int storage() {
if (vanillaFarmland()) return 0; if (disablePluginMechanism()) return 0;
return storage; return storage;
} }
@Override @Override
public boolean isRainDropAccepted() { public boolean isRainDropAccepted() {
return isRainDropAccepted && !vanillaFarmland(); return isRainDropAccepted && !disablePluginMechanism();
} }
@Override @Override
public boolean isNearbyWaterAccepted() { public boolean isNearbyWaterAccepted() {
return isNearbyWaterAccepted && !vanillaFarmland(); return isNearbyWaterAccepted && !disablePluginMechanism();
} }
@Override @Override
public boolean vanillaFarmland() { public boolean disablePluginMechanism() {
return vanillaFarmland; return disablePluginSystem;
} }
@Override @Override
public WateringMethod[] wateringMethods() { public WateringMethod[] wateringMethods() {
if (vanillaFarmland()) return new WateringMethod[0]; if (disablePluginMechanism()) return new WateringMethod[0];
return wateringMethods; return wateringMethods;
} }
@@ -155,18 +166,19 @@ public class PotConfigImpl implements PotConfig {
@Override @Override
public boolean isWet(String blockID) { public boolean isWet(String blockID) {
if (vanillaFarmland()) return false; if (disablePluginMechanism()) return false;
return wetBlocks.contains(blockID); return wetBlocks.contains(blockID);
} }
@Override @Override
public WaterBar waterBar() { public WaterBar waterBar() {
if (disablePluginMechanism()) return null;
return waterBar; return waterBar;
} }
@Override @Override
public int maxFertilizers() { public int maxFertilizers() {
if (vanillaFarmland) return 0; if (disablePluginMechanism()) return 0;
return maxFertilizers; return maxFertilizers;
} }
@@ -259,10 +271,11 @@ public class PotConfigImpl implements PotConfig {
private Action<Player>[] addWaterActions; private Action<Player>[] addWaterActions;
private Action<Player>[] fullWaterActions; private Action<Player>[] fullWaterActions;
private Action<Player>[] maxFertilizerActions; private Action<Player>[] maxFertilizerActions;
private List<String> vanillaPots = new ArrayList<>();
@Override @Override
public PotConfig build() { public PotConfig build() {
return new PotConfigImpl(id, vanillaFarmland, basicAppearance, potAppearanceMap, storage, isRainDropAccepted, isNearbyWaterAccepted, wateringMethods, waterBar, maxFertilizers, placeRequirements, breakRequirements, useRequirements, tickActions, reachLimitActions, interactActions, placeActions, breakActions, addWaterActions, fullWaterActions, maxFertilizerActions); return new PotConfigImpl(id, vanillaFarmland, basicAppearance, potAppearanceMap, storage, isRainDropAccepted, isNearbyWaterAccepted, wateringMethods, waterBar, maxFertilizers, placeRequirements, breakRequirements, useRequirements, tickActions, reachLimitActions, interactActions, placeActions, breakActions, addWaterActions, fullWaterActions, maxFertilizerActions, vanillaPots);
} }
@Override @Override
@@ -283,6 +296,12 @@ public class PotConfigImpl implements PotConfig {
return this; return this;
} }
@Override
public Builder vanillaPots(List<String> vanillaPots) {
this.vanillaPots = vanillaPots;
return this;
}
@Override @Override
public Builder isRainDropAccepted(boolean isRainDropAccepted) { public Builder isRainDropAccepted(boolean isRainDropAccepted) {
this.isRainDropAccepted = isRainDropAccepted; this.isRainDropAccepted = isRainDropAccepted;

View File

@@ -38,6 +38,9 @@ public class ListUtils {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static List<String> toList(final Object obj) { public static List<String> toList(final Object obj) {
if (obj == null) {
return List.of();
}
if (obj instanceof String s) { if (obj instanceof String s) {
return List.of(s); return List.of(s);
} else if (obj instanceof List<?> list) { } else if (obj instanceof List<?> list) {

View File

@@ -65,8 +65,9 @@ public class DebugDataCommand extends BukkitCommandFeature<CommandSender> {
.sendMessage(AdventureHelper.miniMessage("<red>CustomCrops Data not found")); .sendMessage(AdventureHelper.miniMessage("<red>CustomCrops Data not found"));
} }
}); });
String bData = block.getBlockData().getAsString();
BukkitCustomCropsPlugin.getInstance().getSenderFactory().wrap(player) BukkitCustomCropsPlugin.getInstance().getSenderFactory().wrap(player)
.sendMessage(AdventureHelper.miniMessage("<green>Vanilla crop data: " + block.getBlockData().getAsString())); .sendMessage(AdventureHelper.miniMessage("<green>Vanilla crop data: <hover:show_text:'<yellow>Copy'><click:copy_to_clipboard:'"+bData+"'>" + bData + "</click>"));
}); });
} }

View File

@@ -36,6 +36,7 @@ import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
import net.momirealms.customcrops.api.misc.value.TextValue; import net.momirealms.customcrops.api.misc.value.TextValue;
import net.momirealms.customcrops.api.misc.water.WaterBar; import net.momirealms.customcrops.api.misc.water.WaterBar;
import net.momirealms.customcrops.api.requirement.RequirementManager; import net.momirealms.customcrops.api.requirement.RequirementManager;
import net.momirealms.customcrops.common.util.ListUtils;
import net.momirealms.customcrops.common.util.Pair; import net.momirealms.customcrops.common.util.Pair;
import net.momirealms.customcrops.common.util.TriFunction; import net.momirealms.customcrops.common.util.TriFunction;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -116,6 +117,7 @@ public class ConfigType {
PotConfig config = PotConfig.builder() PotConfig config = PotConfig.builder()
.id(id) .id(id)
.vanillaFarmland(section.getBoolean("vanilla-farmland", false)) .vanillaFarmland(section.getBoolean("vanilla-farmland", false))
.vanillaPots(ListUtils.toList(section.get("vanilla-blocks")))
.storage(section.getInt("storage", 5)) .storage(section.getInt("storage", 5))
.isRainDropAccepted(section.getBoolean("absorb-rainwater", false)) .isRainDropAccepted(section.getBoolean("absorb-rainwater", false))
.isNearbyWaterAccepted(section.getBoolean("absorb-nearby-water", false)) .isNearbyWaterAccepted(section.getBoolean("absorb-nearby-water", false))