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

Provide an option to use vanilla mechanic

This commit is contained in:
XiaoMoMi
2024-09-05 01:13:54 +08:00
parent db89cd25e2
commit 1a6a59a616
8 changed files with 82 additions and 51 deletions

View File

@@ -115,7 +115,8 @@ public abstract class AbstractCustomEventListener implements Listener {
this.itemManager.handlePlayerInteractBlock(
event.getPlayer(),
block,
block.getType().name(), event.getBlockFace(),
block.getBlockData().getAsString(),
event.getBlockFace(),
event.getHand(),
event.getItem(),
event
@@ -130,8 +131,10 @@ public abstract class AbstractCustomEventListener implements Listener {
}
this.itemManager.handlePlayerInteractFurniture(
event.getPlayer(),
event.getRightClicked().getLocation(), type.name(),
event.getHand(), event.getPlayer().getInventory().getItem(event.getHand()),
event.getRightClicked().getLocation(),
type.name(),
event.getHand(),
event.getPlayer().getInventory().getItem(event.getHand()),
event
);
}
@@ -145,7 +148,7 @@ public abstract class AbstractCustomEventListener implements Listener {
this.itemManager.handlePlayerPlace(
event.getPlayer(),
block.getLocation(),
block.getType().name(),
block.getBlockData().getAsString(),
event.getHand(),
event.getItemInHand(),
event
@@ -160,7 +163,9 @@ public abstract class AbstractCustomEventListener implements Listener {
}
this.itemManager.handlePlayerBreak(
event.getPlayer(),
block.getLocation(), event.getPlayer().getInventory().getItemInMainHand(), block.getType().name(),
block.getLocation(),
event.getPlayer().getInventory().getItemInMainHand(),
block.getBlockData().getAsString(),
event
);
}
@@ -216,7 +221,7 @@ public abstract class AbstractCustomEventListener implements Listener {
event.setCancelled(true);
return;
}
this.itemManager.handlePhysicsBreak(block.getLocation(), "FARMLAND", event);
this.itemManager.handlePhysicsBreak(block.getLocation(), block.getBlockData().getAsString(), event);
}
}
}
@@ -229,7 +234,7 @@ public abstract class AbstractCustomEventListener implements Listener {
event.setCancelled(true);
return;
}
this.itemManager.handleEntityTrample(event.getEntity(), block.getLocation(), "FARMLAND", event);
this.itemManager.handleEntityTrample(event.getEntity(), block.getLocation(), block.getBlockData().getAsString(), event);
}
}

View File

@@ -18,6 +18,7 @@
package net.momirealms.customcrops.api.core.block;
import com.flowpowered.nbt.*;
import com.flowpowered.nbt.Tag;
import net.momirealms.customcrops.api.misc.NamedTextColor;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.action.ActionManager;
@@ -42,10 +43,7 @@ import net.momirealms.customcrops.api.util.EventUtils;
import net.momirealms.customcrops.api.util.LocationUtils;
import net.momirealms.customcrops.api.util.PlayerUtils;
import net.momirealms.customcrops.api.util.StringUtils;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
@@ -349,6 +347,9 @@ public class PotBlock extends AbstractCustomCropsBlock {
}
}
// work as vanilla farmland
if (config.vanillaFarmland()) return;
boolean hasNaturalWater = false;
boolean waterChanged = false;
@@ -613,20 +614,9 @@ public class PotBlock extends AbstractCustomCropsBlock {
}
public void updateBlockAppearance(Location location, PotConfig config, boolean hasWater, @Nullable Fertilizer fertilizer) {
if (config.vanillaFarmland()) return;
String appearance = config.getPotAppearance(hasWater, fertilizer == null ? null : fertilizer.type());
if (StringUtils.isCapitalLetter(appearance)) {
Block block = location.getBlock();
Material type = Material.valueOf(appearance);
if (type == Material.FARMLAND) {
Farmland data = ((Farmland) Material.FARMLAND.createBlockData());
data.setMoisture(hasWater ? 7 : 0);
block.setBlockData(data, false);
} else {
block.setType(type, false);
}
} else {
BukkitCustomCropsPlugin.getInstance().getItemManager().place(location, ExistenceForm.BLOCK, appearance, FurnitureRotation.NONE);
}
BukkitCustomCropsPlugin.getInstance().getItemManager().placeBlock(location, appearance);
}
private Fertilizer tagToFertilizer(CompoundMap tag) {

View File

@@ -302,18 +302,20 @@ public class SprinklerBlock extends AbstractCustomCropsBlock {
CustomCropsBlockState anotherState = optionalState.get();
if (anotherState.type() instanceof PotBlock potBlock) {
PotConfig potConfig = potBlock.config(anotherState);
if (config.potWhitelist().contains(potConfig.id())) {
if (potBlock.addWater(anotherState, potConfig, config.wateringAmount())) {
BukkitCustomCropsPlugin.getInstance().getScheduler().sync().run(
() -> potBlock.updateBlockAppearance(
pos3.toLocation(world.bukkitWorld()),
potConfig,
true,
potBlock.fertilizers(anotherState)
),
bukkitWorld,
pos3.chunkX(), pos3.chunkZ()
);
if (!potConfig.vanillaFarmland()) {
if (config.potWhitelist().contains(potConfig.id())) {
if (potBlock.addWater(anotherState, potConfig, config.wateringAmount())) {
BukkitCustomCropsPlugin.getInstance().getScheduler().sync().run(
() -> potBlock.updateBlockAppearance(
pos3.toLocation(world.bukkitWorld()),
potConfig,
true,
potBlock.fertilizers(anotherState)
),
bukkitWorld,
pos3.chunkX(), pos3.chunkZ()
);
}
}
}
}

View File

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

View File

@@ -62,6 +62,13 @@ public interface PotConfig {
*/
boolean isNearbyWaterAccepted();
/**
* Checks if the pot works as a vanilla farmland
*
* @return True if disabled
*/
boolean vanillaFarmland();
/**
* Gets the methods available for watering the pot.
*
@@ -221,6 +228,14 @@ public interface PotConfig {
*/
Builder storage(int storage);
/**
* Sets if the pot works as a vanilla farmland
*
* @param vanillaFarmland if works as a vanilla farmland
* @return The current instance of the Builder.
*/
Builder vanillaFarmland(boolean vanillaFarmland);
/**
* Sets whether the pot can accept rain as a source of water.
*

View File

@@ -24,7 +24,6 @@ import net.momirealms.customcrops.api.core.world.CustomCropsBlockState;
import net.momirealms.customcrops.api.misc.water.WaterBar;
import net.momirealms.customcrops.api.misc.water.WateringMethod;
import net.momirealms.customcrops.api.requirement.Requirement;
import net.momirealms.customcrops.api.util.StringUtils;
import net.momirealms.customcrops.common.util.Pair;
import org.bukkit.entity.Player;
@@ -35,6 +34,7 @@ import java.util.Set;
public class PotConfigImpl implements PotConfig {
private final String id;
private final boolean vanillaFarmland;
private final Pair<String, String> basicAppearance;
private final HashMap<FertilizerType, Pair<String, String>> potAppearanceMap;
private final Set<String> blocks = new HashSet<>();
@@ -59,6 +59,7 @@ public class PotConfigImpl implements PotConfig {
public PotConfigImpl(
String id,
boolean vanillaFarmland,
Pair<String, String> basicAppearance,
HashMap<FertilizerType, Pair<String, String>> potAppearanceMap,
int storage,
@@ -80,6 +81,7 @@ public class PotConfigImpl implements PotConfig {
Action<Player>[] maxFertilizerActions
) {
this.id = id;
this.vanillaFarmland = vanillaFarmland;
this.basicAppearance = basicAppearance;
this.potAppearanceMap = potAppearanceMap;
this.storage = storage;
@@ -101,19 +103,18 @@ public class PotConfigImpl implements PotConfig {
this.maxFertilizerActions = maxFertilizerActions;
this.blocks.add(basicAppearance.left());
this.blocks.add(basicAppearance.right());
addToWet(basicAppearance.right());
this.wetBlocks.add(basicAppearance.right());
for (Pair<String, String> pair : potAppearanceMap.values()) {
this.blocks.add(pair.left());
this.blocks.add(pair.right());
addToWet(pair.right());
this.wetBlocks.add(pair.right());
}
}
private void addToWet(String id) {
if (StringUtils.isCapitalLetter(id)) {
return;
if (vanillaFarmland) {
this.blocks.clear();
for (int i = 0; i <= 7; i++) {
this.blocks.add("minecraft:farmland[moisture=" + i +"]");
}
}
this.wetBlocks.add(id);
}
@Override
@@ -123,21 +124,28 @@ public class PotConfigImpl implements PotConfig {
@Override
public int storage() {
if (vanillaFarmland()) return 0;
return storage;
}
@Override
public boolean isRainDropAccepted() {
return isRainDropAccepted;
return isRainDropAccepted && !vanillaFarmland();
}
@Override
public boolean isNearbyWaterAccepted() {
return isNearbyWaterAccepted;
return isNearbyWaterAccepted && !vanillaFarmland();
}
@Override
public boolean vanillaFarmland() {
return vanillaFarmland;
}
@Override
public WateringMethod[] wateringMethods() {
if (vanillaFarmland()) return new WateringMethod[0];
return wateringMethods;
}
@@ -148,6 +156,7 @@ public class PotConfigImpl implements PotConfig {
@Override
public boolean isWet(String blockID) {
if (vanillaFarmland()) return false;
return wetBlocks.contains(blockID);
}
@@ -158,6 +167,7 @@ public class PotConfigImpl implements PotConfig {
@Override
public int maxFertilizers() {
if (vanillaFarmland) return 0;
return maxFertilizers;
}
@@ -230,7 +240,7 @@ public class PotConfigImpl implements PotConfig {
public static class BuilderImpl implements Builder {
private String id;
private ExistenceForm existenceForm;
private boolean vanillaFarmland;
private Pair<String, String> basicAppearance;
private HashMap<FertilizerType, Pair<String, String>> potAppearanceMap;
private int storage;
@@ -253,7 +263,7 @@ public class PotConfigImpl implements PotConfig {
@Override
public PotConfig build() {
return new PotConfigImpl(id, 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);
}
@Override
@@ -268,6 +278,12 @@ public class PotConfigImpl implements PotConfig {
return this;
}
@Override
public Builder vanillaFarmland(boolean vanillaFarmland) {
this.vanillaFarmland = vanillaFarmland;
return this;
}
@Override
public Builder isRainDropAccepted(boolean isRainDropAccepted) {
this.isRainDropAccepted = isRainDropAccepted;

View File

@@ -115,6 +115,7 @@ public class ConfigType {
PotConfig config = PotConfig.builder()
.id(id)
.vanillaFarmland(section.getBoolean("vanilla-farmland", false))
.storage(section.getInt("storage", 5))
.isRainDropAccepted(section.getBoolean("absorb-rainwater", false))
.isNearbyWaterAccepted(section.getBoolean("absorb-nearby-water", false))

View File

@@ -201,8 +201,8 @@ public class BukkitItemManager extends AbstractItemManager {
@Override
public void placeBlock(@NotNull Location location, @NotNull String id) {
if (StringUtils.isCapitalLetter(id)) {
location.getWorld().getBlockAt(location).setType(Material.valueOf(id), false);
if (id.startsWith("minecraft:")) {
location.getWorld().getBlockAt(location).setBlockData(Bukkit.createBlockData(id), false);
} else {
this.provider.placeCustomBlock(location, id);
}