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

implement fertilizers

This commit is contained in:
XiaoMoMi
2024-09-01 17:04:04 +08:00
parent 37688940ca
commit 11a6e803d4
7 changed files with 49 additions and 13 deletions

View File

@@ -19,7 +19,9 @@ package net.momirealms.customcrops.api.core;
import com.flowpowered.nbt.CompoundMap;
import com.flowpowered.nbt.Tag;
import com.flowpowered.nbt.TagType;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
@@ -78,25 +80,36 @@ public class SynchronizedCompoundMap {
@Override
public String toString() {
return compoundMapToString(compoundMap);
return compoundMapToString("BlockData", compoundMap);
}
private String compoundMapToString(CompoundMap compoundMap) {
private String compoundMapToString(String key, CompoundMap compoundMap) {
StringJoiner joiner = new StringJoiner(", ");
for (Map.Entry<String, Tag<?>> entry : compoundMap.entrySet()) {
Tag<?> tag = entry.getValue();
String tagValue;
switch (tag.getType()) {
case TAG_STRING, TAG_BYTE, TAG_DOUBLE, TAG_FLOAT, TAG_INT, TAG_INT_ARRAY, TAG_LONG, TAG_SHORT, TAG_SHORT_ARRAY, TAG_LONG_ARRAY, TAG_BYTE_ARRAY,
TAG_LIST ->
case TAG_STRING, TAG_BYTE, TAG_DOUBLE, TAG_FLOAT, TAG_INT, TAG_INT_ARRAY, TAG_LONG, TAG_SHORT, TAG_SHORT_ARRAY, TAG_LONG_ARRAY, TAG_BYTE_ARRAY ->
tagValue = tag.getValue().toString();
case TAG_COMPOUND -> tagValue = compoundMapToString((CompoundMap) tag.getValue());
case TAG_LIST -> {
List<Tag<?>> list = (List<Tag<?>>) tag.getValue();
StringJoiner listJoiner = new StringJoiner(", ");
for (Tag<?> tag2 : list) {
if (tag2.getType() == TagType.TAG_COMPOUND) {
listJoiner.add(compoundMapToString(tag2.getName(), (CompoundMap) tag2.getValue()));
} else {
listJoiner.add(tag2.getValue().toString());
}
}
tagValue = tag.getName() + "[" + listJoiner + "]";
}
case TAG_COMPOUND -> tagValue = compoundMapToString(tag.getName(), (CompoundMap) tag.getValue());
default -> {
continue;
}
}
joiner.add("\"" + entry.getKey() + "\":\"" + tagValue + "\"");
}
return "BlockData{" + joiner + "}";
return key + "{" + joiner + "}";
}
}

View File

@@ -437,7 +437,7 @@ public class PotBlock extends AbstractCustomCropsBlock {
public Fertilizer[] fertilizers(CustomCropsBlockState state) {
Tag<?> fertilizerTag = state.get("fertilizers");
if (fertilizerTag == null) return new Fertilizer[0];
List<CompoundTag> tags = ((ListTag<CompoundTag>) fertilizerTag.getValue()).getValue();
List<CompoundTag> tags = ((List<CompoundTag>) fertilizerTag.getValue());
Fertilizer[] fertilizers = new Fertilizer[tags.size()];
for (int i = 0; i < tags.size(); i++) {
CompoundTag tag = tags.get(i);
@@ -480,10 +480,10 @@ public class PotBlock extends AbstractCustomCropsBlock {
public boolean addFertilizer(CustomCropsBlockState state, Fertilizer fertilizer) {
Tag<?> fertilizerTag = state.get("fertilizers");
if (fertilizerTag == null) {
fertilizerTag = new ListTag<CompoundTag>("", TagType.TAG_COMPOUND, new ArrayList<>());
fertilizerTag = new ListTag<CompoundTag>("fertilizers", TagType.TAG_COMPOUND, new ArrayList<>());
state.set("fertilizers", fertilizerTag);
}
List<CompoundTag> tags = ((ListTag<CompoundTag>) fertilizerTag.getValue()).getValue();
List<CompoundTag> tags = ((List<CompoundTag>) fertilizerTag.getValue());
for (CompoundTag tag : tags) {
CompoundMap map = tag.getValue();
Fertilizer applied = tagToFertilizer(map);
@@ -510,7 +510,7 @@ public class PotBlock extends AbstractCustomCropsBlock {
if (fertilizerTag == null) {
return false;
}
List<CompoundTag> tags = ((ListTag<CompoundTag>) fertilizerTag.getValue()).getValue();
List<CompoundTag> tags = ((List<CompoundTag>) fertilizerTag.getValue());
if (tags.isEmpty()) {
return false;
}

View File

@@ -71,6 +71,8 @@ public interface PotConfig {
Action<Player>[] fullWaterActions();
Action<Player>[] maxFertilizerActions();
static Builder builder() {
return new PotConfigImpl.BuilderImpl();
}
@@ -113,6 +115,8 @@ public interface PotConfig {
Builder fullWaterActions(Action<Player>[] fullWaterActions);
Builder maxFertilizerActions(Action<Player>[] maxFertilizerActions);
Builder basicAppearance(Pair<String, String> basicAppearance);
Builder potAppearanceMap(HashMap<FertilizerType, Pair<String, String>> potAppearanceMap);

View File

@@ -54,6 +54,7 @@ public class PotConfigImpl implements PotConfig {
private final Action<Player>[] breakActions;
private final Action<Player>[] addWaterActions;
private final Action<Player>[] fullWaterActions;
private final Action<Player>[] maxFertilizerActions;
public PotConfigImpl(
String id,
@@ -74,7 +75,8 @@ public class PotConfigImpl implements PotConfig {
Action<Player>[] placeActions,
Action<Player>[] breakActions,
Action<Player>[] addWaterActions,
Action<Player>[] fullWaterActions
Action<Player>[] fullWaterActions,
Action<Player>[] maxFertilizerActions
) {
this.id = id;
this.basicAppearance = basicAppearance;
@@ -95,6 +97,7 @@ public class PotConfigImpl implements PotConfig {
this.breakActions = breakActions;
this.addWaterActions = addWaterActions;
this.fullWaterActions = fullWaterActions;
this.maxFertilizerActions = maxFertilizerActions;
this.blocks.add(basicAppearance.left());
this.blocks.add(basicAppearance.right());
this.wetBlocks.add(basicAppearance.right());
@@ -211,6 +214,11 @@ public class PotConfigImpl implements PotConfig {
return fullWaterActions;
}
@Override
public Action<Player>[] maxFertilizerActions() {
return maxFertilizerActions;
}
public static class BuilderImpl implements Builder {
private String id;
@@ -233,10 +241,11 @@ public class PotConfigImpl implements PotConfig {
private Action<Player>[] breakActions;
private Action<Player>[] addWaterActions;
private Action<Player>[] fullWaterActions;
private Action<Player>[] maxFertilizerActions;
@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);
return new PotConfigImpl(id, basicAppearance, potAppearanceMap, storage, isRainDropAccepted, isNearbyWaterAccepted, wateringMethods, waterBar, maxFertilizers, placeRequirements, breakRequirements, useRequirements, tickActions, reachLimitActions, interactActions, placeActions, breakActions, addWaterActions, fullWaterActions, maxFertilizerActions);
}
@Override
@@ -341,6 +350,12 @@ public class PotConfigImpl implements PotConfig {
return this;
}
@Override
public Builder maxFertilizerActions(Action<Player>[] maxFertilizerActions) {
this.maxFertilizerActions = maxFertilizerActions;
return this;
}
@Override
public Builder basicAppearance(Pair<String, String> basicAppearance) {
this.basicAppearance = basicAppearance;

View File

@@ -53,7 +53,7 @@ public class FertilizerItem extends AbstractCustomCropsItem {
@Override
public InteractionResult interactAt(WrappedInteractEvent event) {
FertilizerConfig fertilizerConfig = Registries.FERTILIZER.get(event.itemID());
FertilizerConfig fertilizerConfig = Registries.ITEM_TO_FERTILIZER.get(event.itemID());
if (fertilizerConfig == null) {
return InteractionResult.COMPLETE;
}
@@ -114,6 +114,7 @@ public class FertilizerItem extends AbstractCustomCropsItem {
.build();
CustomCropsBlockState potState = potBlock.fixOrGetState(world, Pos3.from(targetLocation), potConfig, event.relatedID());
if (!potBlock.canApplyFertilizer(potState,fertilizer)) {
ActionManager.trigger(context, potConfig.maxFertilizerActions());
return InteractionResult.COMPLETE;
}
// trigger event