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( this.itemManager.handlePlayerInteractBlock(
event.getPlayer(), event.getPlayer(),
block, block,
block.getType().name(), event.getBlockFace(), block.getBlockData().getAsString(),
event.getBlockFace(),
event.getHand(), event.getHand(),
event.getItem(), event.getItem(),
event event
@@ -130,8 +131,10 @@ public abstract class AbstractCustomEventListener implements Listener {
} }
this.itemManager.handlePlayerInteractFurniture( this.itemManager.handlePlayerInteractFurniture(
event.getPlayer(), event.getPlayer(),
event.getRightClicked().getLocation(), type.name(), event.getRightClicked().getLocation(),
event.getHand(), event.getPlayer().getInventory().getItem(event.getHand()), type.name(),
event.getHand(),
event.getPlayer().getInventory().getItem(event.getHand()),
event event
); );
} }
@@ -145,7 +148,7 @@ public abstract class AbstractCustomEventListener implements Listener {
this.itemManager.handlePlayerPlace( this.itemManager.handlePlayerPlace(
event.getPlayer(), event.getPlayer(),
block.getLocation(), block.getLocation(),
block.getType().name(), block.getBlockData().getAsString(),
event.getHand(), event.getHand(),
event.getItemInHand(), event.getItemInHand(),
event event
@@ -160,7 +163,9 @@ public abstract class AbstractCustomEventListener implements Listener {
} }
this.itemManager.handlePlayerBreak( this.itemManager.handlePlayerBreak(
event.getPlayer(), event.getPlayer(),
block.getLocation(), event.getPlayer().getInventory().getItemInMainHand(), block.getType().name(), block.getLocation(),
event.getPlayer().getInventory().getItemInMainHand(),
block.getBlockData().getAsString(),
event event
); );
} }
@@ -216,7 +221,7 @@ public abstract class AbstractCustomEventListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
return; 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); event.setCancelled(true);
return; 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; package net.momirealms.customcrops.api.core.block;
import com.flowpowered.nbt.*; import com.flowpowered.nbt.*;
import com.flowpowered.nbt.Tag;
import net.momirealms.customcrops.api.misc.NamedTextColor; import net.momirealms.customcrops.api.misc.NamedTextColor;
import net.momirealms.customcrops.api.BukkitCustomCropsPlugin; import net.momirealms.customcrops.api.BukkitCustomCropsPlugin;
import net.momirealms.customcrops.api.action.ActionManager; 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.LocationUtils;
import net.momirealms.customcrops.api.util.PlayerUtils; import net.momirealms.customcrops.api.util.PlayerUtils;
import net.momirealms.customcrops.api.util.StringUtils; import net.momirealms.customcrops.api.util.StringUtils;
import org.bukkit.GameMode; import org.bukkit.*;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData; import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged; 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 hasNaturalWater = false;
boolean waterChanged = 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) { 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()); String appearance = config.getPotAppearance(hasWater, fertilizer == null ? null : fertilizer.type());
if (StringUtils.isCapitalLetter(appearance)) { BukkitCustomCropsPlugin.getInstance().getItemManager().placeBlock(location, 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);
}
} }
private Fertilizer tagToFertilizer(CompoundMap tag) { private Fertilizer tagToFertilizer(CompoundMap tag) {

View File

@@ -302,18 +302,20 @@ 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 (config.potWhitelist().contains(potConfig.id())) { if (!potConfig.vanillaFarmland()) {
if (potBlock.addWater(anotherState, potConfig, config.wateringAmount())) { if (config.potWhitelist().contains(potConfig.id())) {
BukkitCustomCropsPlugin.getInstance().getScheduler().sync().run( if (potBlock.addWater(anotherState, potConfig, config.wateringAmount())) {
() -> potBlock.updateBlockAppearance( BukkitCustomCropsPlugin.getInstance().getScheduler().sync().run(
pos3.toLocation(world.bukkitWorld()), () -> potBlock.updateBlockAppearance(
potConfig, pos3.toLocation(world.bukkitWorld()),
true, potConfig,
potBlock.fertilizers(anotherState) true,
), potBlock.fertilizers(anotherState)
bukkitWorld, ),
pos3.chunkX(), pos3.chunkZ() bukkitWorld,
); pos3.chunkX(), pos3.chunkZ()
);
}
} }
} }
} }

View File

@@ -254,6 +254,8 @@ 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())
return InteractionResult.COMPLETE;
// need to click the upper face // need to click the upper face
if (blockFace != BlockFace.UP) if (blockFace != BlockFace.UP)
return InteractionResult.PASS; return InteractionResult.PASS;

View File

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

View File

@@ -115,6 +115,7 @@ public class ConfigType {
PotConfig config = PotConfig.builder() PotConfig config = PotConfig.builder()
.id(id) .id(id)
.vanillaFarmland(section.getBoolean("vanilla-farmland", false))
.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))

View File

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