From 08aae0f1e1052f1233b28d4f2dd2ce950b000c7b Mon Sep 17 00:00:00 2001 From: 3944Realms Date: Thu, 23 Oct 2025 01:06:37 +0800 Subject: [PATCH] =?UTF-8?q?pre=E4=BF=AE=E6=94=B9=EF=BC=9A=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3BUG=EF=BC=8C=E8=BF=9B=E4=B8=80=E6=AD=A5=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../api/event/SuperLeadRopeEvent.java | 176 +++- .../api/type/capabilty/ILeashData.java | 32 + .../api/type/capabilty/LeashInfo.java | 32 +- .../config/LeashCommonConfig.java | 7 +- .../config/LeashConfigManager.java | 45 +- .../capability/impi/LeashDataImpl.java | 83 +- .../content/command/Command.java | 26 + .../content/command/LeashDataCommand.java | 789 +++++++++++++----- .../content/command/LeashStateCommand.java | 2 +- .../util/capability/LeashDataInnerAPI.java | 213 +++-- 11 files changed, 1090 insertions(+), 317 deletions(-) diff --git a/gradle.properties b/gradle.properties index dd273b3..3d7e638 100644 --- a/gradle.properties +++ b/gradle.properties @@ -59,7 +59,7 @@ mod_name=Super Lead Rope # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=0.0.0.6-pre5 +mod_version=0.0.0.6-pre7 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/top/r3944realms/superleadrope/api/event/SuperLeadRopeEvent.java b/src/main/java/top/r3944realms/superleadrope/api/event/SuperLeadRopeEvent.java index 5253af7..1e96884 100644 --- a/src/main/java/top/r3944realms/superleadrope/api/event/SuperLeadRopeEvent.java +++ b/src/main/java/top/r3944realms/superleadrope/api/event/SuperLeadRopeEvent.java @@ -88,15 +88,17 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { private Double maxLeashDistance; @Nullable private Double elasticDistanceScale; + private int maxKeepLeashTicks; /** * Instantiates a new Add leash. * - * @param leashedEntity the leashed entity - * @param holderEntity the holder entity + * @param leashedEntity the leashed entity + * @param holderEntity the holder entity + * @param maxKeepLeashTicks the max keep leash ticks */ - public AddLeash(Entity leashedEntity, Entity holderEntity) { - this(leashedEntity, holderEntity, null, null); + public AddLeash(Entity leashedEntity, Entity holderEntity, int maxKeepLeashTicks) { + this(leashedEntity, holderEntity, null, null, maxKeepLeashTicks); } /** @@ -106,12 +108,14 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { * @param holderEntity the holder entity * @param maxLeashDistance the max leash distance * @param elasticDistanceScale the elastic distance scale + * @param maxKeepLeashTicks the max keep leash ticks */ - public AddLeash(Entity leashedEntity, Entity holderEntity, @Nullable Double maxLeashDistance, @Nullable Double elasticDistanceScale) { + public AddLeash(Entity leashedEntity, Entity holderEntity, @Nullable Double maxLeashDistance, @Nullable Double elasticDistanceScale, int maxKeepLeashTicks) { super(leashedEntity); this.holderEntity = holderEntity; this.maxLeashDistance = maxLeashDistance; this.elasticDistanceScale = elasticDistanceScale; + this.maxKeepLeashTicks = maxKeepLeashTicks; } /** @@ -134,6 +138,26 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { this.maxLeashDistance = maxLeashDistance; } + /** + * Sets max keep leash ticks. + * + * @param maxKeepLeashTicks the max keep leash ticks + */ + public void setMaxKeepLeashTicks(int maxKeepLeashTicks) { + if (maxKeepLeashTicks < 0) return; + markModified(); + this.maxKeepLeashTicks = maxKeepLeashTicks; + } + + /** + * Gets max keep leash ticks. + * + * @return the max keep leash ticks + */ + public int getMaxKeepLeashTicks() { + return maxKeepLeashTicks; + } + /** * Gets holder entity. * @@ -216,34 +240,38 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { public static class TransferLeash extends SuperLeadRopeEvent { private final LeashHolder oldLeashHolder; private final Entity newLeashHolder; + private int maxKeepLeashTicks; /** * Instantiates a new Transfer leash. * - * @param leashedEntity the leashed entity - * @param holderEntity the holder entity - * @param newLeashHolder the new leash holder + * @param leashedEntity the leashed entity + * @param holderEntity the holder entity + * @param newLeashHolder the new leash holder + * @param maxKeepLeashTicks the max keep leash ticks */ - public TransferLeash(Entity leashedEntity, UUID holderEntity, Entity newLeashHolder) { - this(leashedEntity, holderEntity, null, false , newLeashHolder); + public TransferLeash(Entity leashedEntity, UUID holderEntity, Entity newLeashHolder, int maxKeepLeashTicks) { + this(leashedEntity, holderEntity, null, false , newLeashHolder, maxKeepLeashTicks); } /** * Instantiates a new Transfer leash. * - * @param leashedEntity the leashed entity - * @param holderKnot the holder knot - * @param newLeashHolder the new leash holder + * @param leashedEntity the leashed entity + * @param holderKnot the holder knot + * @param newLeashHolder the new leash holder + * @param maxKeepLeashTicks the max keep leash ticks */ - public TransferLeash(Entity leashedEntity, BlockPos holderKnot, Entity newLeashHolder) { - this(leashedEntity, null, holderKnot, true, newLeashHolder); + public TransferLeash(Entity leashedEntity, BlockPos holderKnot, Entity newLeashHolder, int maxKeepLeashTicks) { + this(leashedEntity, null, holderKnot, true, newLeashHolder, maxKeepLeashTicks); } - private TransferLeash(Entity leashedEntity, @Nullable UUID holderEntity, @Nullable BlockPos holderPos, boolean isSuperLeadRopeKnot, Entity newLeashHolder) { + private TransferLeash(Entity leashedEntity, @Nullable UUID holderEntity, @Nullable BlockPos holderPos, boolean isSuperLeadRopeKnot, Entity newLeashHolder, int maxKeepLeashTicks) { super(leashedEntity); if (isSuperLeadRopeKnot) { oldLeashHolder = new LeashHolder(holderPos); } else oldLeashHolder = new LeashHolder(holderEntity); this.newLeashHolder = newLeashHolder; + this.maxKeepLeashTicks = maxKeepLeashTicks; } /** @@ -263,10 +291,32 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { public LeashHolder getOldLeashHolder() { return oldLeashHolder; } + + /** + * Sets max keep leash ticks. + * + * @param maxKeepLeashTicks the max keep leash ticks + */ + public void setMaxKeepLeashTicks(int maxKeepLeashTicks) { + if(maxKeepLeashTicks < 0) return; + markModified(); + this.maxKeepLeashTicks = maxKeepLeashTicks; + } + + /** + * Gets max keep leash ticks. + * + * @return the max keep leash ticks + */ + public int getMaxKeepLeashTicks() { + return maxKeepLeashTicks; + } } /** * The type Modify value. + * + * @param the type parameter */ // MODIFY LEASH MAX_LEASH_LENGTH / ELASTIC_DISTANCE_SCALE @SuppressWarnings("unused") @@ -281,10 +331,25 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { private final Type type; private final Scope scope; + /** + * The enum Type. + */ public enum Type { + /** + * Max distance type. + */ MAX_DISTANCE(Double.class), + /** + * Elastic distance scale type. + */ ELASTIC_DISTANCE_SCALE(Double.class), + /** + * Max keep leash ticks type. + */ MAX_KEEP_LEASH_TICKS(Integer.class), + /** + * Custom data type. + */ CUSTOM_DATA(String.class); // 支持更多类型 private final Class valueType; @@ -293,16 +358,40 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { this.valueType = valueType; } + /** + * Gets value type. + * + * @return the value type + */ public Class getValueType() { return valueType; } } + + /** + * The enum Scope. + */ public enum Scope { + /** + * Instance scope. + */ INSTANCE, + /** + * Static scope. + */ STATIC } - // 构造方法 - UUID holder + /** + * Instantiates a new Modify value. + * + * @param leashedEntity the leashed entity + * @param holderUUID the holder uuid + * @param oldValue the old value + * @param newValue the new value + * @param type the type + */ +// 构造方法 - UUID holder public ModifyValue(Entity leashedEntity, UUID holderUUID, @Nullable T oldValue, @Nullable T newValue, Type type) { @@ -314,7 +403,16 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { this.scope = Scope.INSTANCE; } - // 构造方法 - BlockPos holder + /** + * Instantiates a new Modify value. + * + * @param leashedEntity the leashed entity + * @param knotBlockpos the knot blockpos + * @param oldValue the old value + * @param newValue the new value + * @param type the type + */ +// 构造方法 - BlockPos holder public ModifyValue(Entity leashedEntity, BlockPos knotBlockpos, @Nullable T oldValue, @Nullable T newValue, Type type) { @@ -326,7 +424,15 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { this.scope = Scope.INSTANCE; } - // 构造方法 - 静态作用域 + /** + * Instantiates a new Modify value. + * + * @param leashedEntity the leashed entity + * @param oldValue the old value + * @param newValue the new value + * @param type the type + */ +// 构造方法 - 静态作用域 public ModifyValue(Entity leashedEntity, @Nullable T oldValue, @Nullable T newValue, Type type) { @@ -339,7 +445,14 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { } - // 类型安全的获取方法 + /** + * Gets old value as. + * + * @param the type parameter + * @param clazz the clazz + * @return the old value as + */ +// 类型安全的获取方法 @SuppressWarnings("unchecked") public R getOldValueAs(Class clazz) { if (clazz.isInstance(oldValue)) { @@ -348,6 +461,13 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { return null; } + /** + * Gets new value as. + * + * @param the type parameter + * @param clazz the clazz + * @return the new value as + */ @SuppressWarnings("unchecked") public R getNewValueAs(Class clazz) { if (clazz.isInstance(newValue)) { @@ -356,16 +476,32 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent { return null; } + /** + * Gets old value. + * + * @return the old value + */ @SuppressWarnings("unchecked") public T getOldValue() { return (T) getOldValueAs(type.valueType); } + + /** + * Gets new value. + * + * @return the new value + */ @SuppressWarnings("unchecked") public T getNewValue() { return (T) getNewValueAs(type.valueType); } + /** + * Sets new value. + * + * @param newValue the new value + */ public void setNewValue(@Nullable T newValue) { if (newValue != null && !type.valueType.isInstance(newValue)) { throw new IllegalArgumentException( diff --git a/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/ILeashData.java b/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/ILeashData.java index 721fa20..889697c 100644 --- a/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/ILeashData.java +++ b/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/ILeashData.java @@ -267,6 +267,16 @@ public interface ILeashData extends INBTSerializable { * ---------------------- */ boolean setMaxDistance(Entity holder, Double distance); + /** + * Sets max distance. + * + * @param holder the holder + * @param distance the distance + * @param reserved the reserved + * @return the max distance + */ + boolean setMaxDistance(Entity holder, Double distance, String reserved); + /** * Sets max distance. * @@ -297,6 +307,17 @@ public interface ILeashData extends INBTSerializable { */ boolean setMaxDistance(UUID holderUUID, Double distance); + /** + * Sets max distance. + * + * @param holderUUID the holder uuid + * @param distance the distance + * @param reserved the reserved + * @return the max distance + */ + boolean setMaxDistance(UUID holderUUID, Double distance, String reserved); + + /** * Sets max distance. * @@ -327,6 +348,16 @@ public interface ILeashData extends INBTSerializable { */ boolean setMaxDistance(BlockPos knotPos, Double distance); + /** + * Sets max distance. + * + * @param knotPos the knot pos + * @param distance the distance + * @param reserved the reserved + * @return the max distance + */ + boolean setMaxDistance(BlockPos knotPos, Double distance, String reserved); + /** * Sets max distance. * @@ -357,6 +388,7 @@ public interface ILeashData extends INBTSerializable { */ boolean setElasticDistanceScale(Entity holder, Double scale); + /** * Sets elastic distance scale. * diff --git a/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/LeashInfo.java b/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/LeashInfo.java index fdd4755..38354ac 100644 --- a/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/LeashInfo.java +++ b/src/main/java/top/r3944realms/superleadrope/api/type/capabilty/LeashInfo.java @@ -232,7 +232,37 @@ public record LeashInfo( !isKnot ? Optional.of(entity.getUUID()) : Optional.empty(), Optional.of(entity.getId()), marks, newReserved, maxDistance, elasticDistanceScale, - keepLeashTicks, maxKeepLeashTicks + maxKeepLeashTicks, maxKeepLeashTicks + ); + } + + /** + * Transfer holder leash info. + * + * @param entity the entity + * @param maxKeepLeashTicks the max keep leash ticks + * @return the leash info + */ + public LeashInfo transferHolder(Entity entity, int maxKeepLeashTicks) { + return transferHolder(entity, maxKeepLeashTicks, reserved); + } + + /** + * Transfer holder leash info. + * + * @param entity the entity + * @param maxKeepLeashTicks the max keep leash ticks + * @param newReserved the new reserved + * @return the leash info + */ + public LeashInfo transferHolder(Entity entity, int maxKeepLeashTicks, String newReserved) { + boolean isKnot = SuperLeadRopeApi.isSuperLeadKnot(entity); + return new LeashInfo( + isKnot ? Optional.of(SuperLeadRopeApi.getSuperLeadKnotPos(entity)) : Optional.empty(), + !isKnot ? Optional.of(entity.getUUID()) : Optional.empty(), + Optional.of(entity.getId()), + marks, newReserved, maxDistance, elasticDistanceScale, + maxKeepLeashTicks, maxKeepLeashTicks ); } diff --git a/src/main/java/top/r3944realms/superleadrope/config/LeashCommonConfig.java b/src/main/java/top/r3944realms/superleadrope/config/LeashCommonConfig.java index 383dd0c..0c8ef1e 100644 --- a/src/main/java/top/r3944realms/superleadrope/config/LeashCommonConfig.java +++ b/src/main/java/top/r3944realms/superleadrope/config/LeashCommonConfig.java @@ -1,6 +1,7 @@ package top.r3944realms.superleadrope.config; import net.minecraftforge.common.ForgeConfigSpec; +import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.regex.Matcher; @@ -120,7 +121,7 @@ public class LeashCommonConfig { * * @param builder the builder */ - public Common(ForgeConfigSpec.Builder builder) { + public Common(ForgeConfigSpec.@NotNull Builder builder) { // ===== Command ===== builder.push("Command"); enableSLPModCommandPrefix = builder @@ -153,11 +154,11 @@ public class LeashCommonConfig { builder.push("LeashSettings"); maxLeashLength = builder .comment("Maximum leash distance (in blocks) for any entity") - .defineInRange("maxLeashLength", 6.0, 1.0, 256.0); + .defineInRange("maxLeashLength", 6.0, LeashConfigManager.MAX_DISTANCE_MIN_VALUE, LeashConfigManager.MAX_DISTANCE_MAX_VALUE); elasticDistanceScale = builder .comment("Default elastic distance for the Super Lead rope") - .defineInRange("elasticDistanceScale", 1.0, 0.2, 4.0); + .defineInRange("elasticDistanceScale", 1.0, LeashConfigManager.ELASTIC_DISTANCE_MIN_VALUE, LeashConfigManager.ELASTIC_DISTANCE_MAX_VALUE); extremeSnapFactor = builder .comment("Leash break factor = maxDistance * factor") diff --git a/src/main/java/top/r3944realms/superleadrope/config/LeashConfigManager.java b/src/main/java/top/r3944realms/superleadrope/config/LeashConfigManager.java index 6baa9aa..129814e 100644 --- a/src/main/java/top/r3944realms/superleadrope/config/LeashConfigManager.java +++ b/src/main/java/top/r3944realms/superleadrope/config/LeashConfigManager.java @@ -21,6 +21,8 @@ import net.minecraft.tags.TagKey; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.phys.Vec3; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Unmodifiable; import top.r3944realms.superleadrope.SuperLeadRope; import java.util.*; @@ -33,16 +35,33 @@ import static top.r3944realms.superleadrope.config.LeashCommonConfig.Common.OFFS /** * The type Leash config manager. */ +@SuppressWarnings("unused") public class LeashConfigManager { + // ========== 最值检测 ========== /** * The constant MAX_DISTANCE_CHECK. */ - // ========== 最值检测 ========== - public static final Predicate MAX_DISTANCE_CHECK = distance -> distance == null || (distance >= 1.0 && distance <= 256.0); + public static final Double MAX_DISTANCE_MAX_VALUE = 256.0; + /** + * The constant MAX_DISTANCE_MIN_VALUE. + */ + public static final Double MAX_DISTANCE_MIN_VALUE = 1.0; + /** + * The constant MAX_DISTANCE_CHECK. + */ + public static final Predicate MAX_DISTANCE_CHECK = distance -> distance == null || (distance >= MAX_DISTANCE_MIN_VALUE && distance <= MAX_DISTANCE_MAX_VALUE); /** * The constant ELASTIC_DISTANCE_CHECK. */ - public static final Predicate ELASTIC_DISTANCE_CHECK = distance -> distance == null || (distance >= 0.2 && distance <= 4.0); + public static final Double ELASTIC_DISTANCE_MAX_VALUE = 4.0; + /** + * The constant ELASTIC_DISTANCE_MIN_VALUE. + */ + public static final Double ELASTIC_DISTANCE_MIN_VALUE = 0.2; + /** + * The constant ELASTIC_DISTANCE_CHECK. + */ + public static final Predicate ELASTIC_DISTANCE_CHECK = distance -> distance == null || (distance >= ELASTIC_DISTANCE_MIN_VALUE && distance <= ELASTIC_DISTANCE_MAX_VALUE); // ========== 偏移映射 ========== private final Map entityHolderMap = new ConcurrentHashMap<>(); private final Map tagHolderMap = new ConcurrentHashMap<>(); @@ -77,7 +96,7 @@ public class LeashConfigManager { } // ================== 偏移解析 ================== - private Map> parseOffsetList(List offsetConfigs) { + private @NotNull @Unmodifiable Map> parseOffsetList(@NotNull List offsetConfigs) { Map entityMap = new HashMap<>(); Map tagMap = new HashMap<>(); Map modMap = new HashMap<>(); @@ -131,7 +150,7 @@ public class LeashConfigManager { // ================== 获取偏移 ================== private double[] getOffset(String entityId, String modId, List tags, - Map entityMap, + @NotNull Map entityMap, Map tagMap, Map modMap) { @@ -148,7 +167,7 @@ public class LeashConfigManager { * @return the default entity offset */ @SuppressWarnings({"DuplicatedCode", "deprecation"}) - public Vec3 getDefaultEntityOffset(EntityType type) { + public Vec3 getDefaultEntityOffset(@NotNull EntityType type) { String entityId = type.builtInRegistryHolder().key().location().toString(); String modId = entityId.split(":")[0]; List tags = new ArrayList<>(); @@ -165,7 +184,7 @@ public class LeashConfigManager { * @return the default holder offset */ @SuppressWarnings({"DuplicatedCode", "deprecation"}) - public Vec3 getDefaultHolderOffset(EntityType type) { + public Vec3 getDefaultHolderOffset(@NotNull EntityType type) { String entityId = type.builtInRegistryHolder().key().location().toString(); String modId = entityId.split(":")[0]; List tags = new ArrayList<>(); @@ -181,7 +200,7 @@ public class LeashConfigManager { * @param entity the entity * @return the default entity offset */ - public Vec3 getDefaultEntityOffset(Entity entity) { return getDefaultEntityOffset(entity.getType()); } + public Vec3 getDefaultEntityOffset(@NotNull Entity entity) { return getDefaultEntityOffset(entity.getType()); } /** * Gets default holder offset. @@ -189,7 +208,7 @@ public class LeashConfigManager { * @param entity the entity * @return the default holder offset */ - public Vec3 getDefaultHolderOffset(Entity entity) { return getDefaultHolderOffset(entity.getType()); } + public Vec3 getDefaultHolderOffset(@NotNull Entity entity) { return getDefaultHolderOffset(entity.getType()); } /** * Gets teleport whitelist. @@ -206,7 +225,7 @@ public class LeashConfigManager { * @return the boolean */ @SuppressWarnings({"DuplicatedCode", "deprecation"}) - public boolean isEntityTeleportAllowed(EntityType type) { + public boolean isEntityTeleportAllowed(@NotNull EntityType type) { String entityId = type.builtInRegistryHolder().key().location().toString(); String modId = entityId.split(":")[0]; @@ -231,7 +250,7 @@ public class LeashConfigManager { * @param entity the entity * @return the boolean */ - public boolean isEntityTeleportAllowed(Entity entity) { return isEntityTeleportAllowed(entity.getType()); } + public boolean isEntityTeleportAllowed(@NotNull Entity entity) { return isEntityTeleportAllowed(entity.getType()); } /** * Gets command prefix. @@ -401,7 +420,7 @@ public class LeashConfigManager { * * @param manager the manager */ - public static void loading(LeashConfigManager manager) { + public static void loading(@NotNull LeashConfigManager manager) { manager.reloadAll(); } @@ -410,7 +429,7 @@ public class LeashConfigManager { * * @param manager the manager */ - public static void reloading(LeashConfigManager manager) { + public static void reloading(@NotNull LeashConfigManager manager) { manager.reloadAll(); } diff --git a/src/main/java/top/r3944realms/superleadrope/content/capability/impi/LeashDataImpl.java b/src/main/java/top/r3944realms/superleadrope/content/capability/impi/LeashDataImpl.java index d4ae7c9..aea492a 100644 --- a/src/main/java/top/r3944realms/superleadrope/content/capability/impi/LeashDataImpl.java +++ b/src/main/java/top/r3944realms/superleadrope/content/capability/impi/LeashDataImpl.java @@ -295,7 +295,7 @@ public class LeashDataImpl implements ILeashData { return false; } if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(maxDistance) || !LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(elasticDistanceScale)) return false; - SuperLeadRopeEvent.AddLeash event = new SuperLeadRopeEvent.AddLeash(holder, this.entity, maxDistance, elasticDistanceScale); + SuperLeadRopeEvent.AddLeash event = new SuperLeadRopeEvent.AddLeash(this.entity, holder, maxDistance, elasticDistanceScale, maxKeepLeashTicks); //再次检查 if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getMaxLeashDistance()) && LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getElasticDistanceScale()))) return false; if (!canBeLeashed()) { @@ -311,7 +311,7 @@ public class LeashDataImpl implements ILeashData { reserved, event.getMaxLeashDistance(), event.getElasticDistanceScale(), - maxKeepLeashTicks, + event.getMaxKeepLeashTicks(), maxKeepLeashTicks ); @@ -383,6 +383,13 @@ public class LeashDataImpl implements ILeashData { setMaxDistance(holder.getUUID(), newMaxDistance); } + @Override + public boolean setMaxDistance(Entity holder, Double distance, String reserved) { + return holder instanceof SuperLeashKnotEntity superLeashKnotEntity ? + setMaxDistance(superLeashKnotEntity.getPos(), distance, reserved) : + setMaxDistance(holder.getUUID(),distance , reserved); + } + @Override public boolean setMaxDistance(Entity holder, @Nullable Double newMaxDistance, int newMaxKeepLeashTicks) { return holder instanceof SuperLeashKnotEntity superLeashKnotEntity ? @@ -415,6 +422,24 @@ public class LeashDataImpl implements ILeashData { )); } + @SuppressWarnings("OptionalGetWithoutIsPresent") + @Override + public boolean setMaxDistance(UUID holderUUID, Double newMaxDistance, String reserved) { + if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; + SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, holderUUID, leashHolders.get(holderUUID).maxDistance(), newMaxDistance, SuperLeadRopeEvent.ModifyValue.Type.MAX_DISTANCE); + if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false; + return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo( + old.holderUUIDOpt().get(), + old.holderIdOpt().get(), + old.marks(), + reserved, + event.getNewValue(), + old.elasticDistanceScale(), + old.keepLeashTicks(), + old.maxKeepLeashTicks() + )); + } + /** * Sets max distance inner. * @@ -492,6 +517,24 @@ public class LeashDataImpl implements ILeashData { )); } + @SuppressWarnings("OptionalGetWithoutIsPresent") + @Override + public boolean setMaxDistance(BlockPos knotPos, Double newMaxDistance, String reserved) { + if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; + SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, knotPos, leashKnots.get(knotPos).maxDistance(), newMaxDistance, SuperLeadRopeEvent.ModifyValue.Type.MAX_DISTANCE); + if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false; + return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo( + old.blockPosOpt().get(), + old.holderIdOpt().get(), + old.marks(), + reserved, + event.getNewValue(), + old.elasticDistanceScale(), + old.keepLeashTicks(), + old.maxKeepLeashTicks() + )); + } + /** * Sets max distance inner. * @@ -1122,14 +1165,17 @@ public class LeashDataImpl implements ILeashData { // 将拴绳持有者转移到新实体(非拴绳结 -> 任意) @Override public boolean transferLeash(UUID oldHolderUUID, Entity newHolder) { - if(MinecraftForge.EVENT_BUS.post(new SuperLeadRopeEvent.TransferLeash(this.entity, oldHolderUUID, newHolder))) return false; + LeashInfo preInfo = leashHolders.get(oldHolderUUID); + if(preInfo == null) return false; + SuperLeadRopeEvent.TransferLeash event = new SuperLeadRopeEvent.TransferLeash(this.entity, oldHolderUUID, newHolder, preInfo.maxKeepLeashTicks()); + if(MinecraftForge.EVENT_BUS.post(event)) return false; LeashInfo info = leashHolders.remove(oldHolderUUID); if (info == null || newHolder == null) return false; if(newHolder instanceof SuperLeashKnotEntity superLeashKnotEntity) { - LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity); + LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity, event.getMaxKeepLeashTicks()); leashKnots.put(superLeashKnotEntity.getPos(), leashInfo); } else { - LeashInfo leashInfo = info.transferHolder(newHolder); + LeashInfo leashInfo = info.transferHolder(newHolder, event.getMaxKeepLeashTicks()); leashHolders.put(newHolder.getUUID(), leashInfo); } LeashStateInnerAPI.Operations.transfer(entity, oldHolderUUID, newHolder); @@ -1138,14 +1184,17 @@ public class LeashDataImpl implements ILeashData { } @Override public boolean transferLeash(UUID oldHolderUUID, Entity newHolder, String reserved) { - if(MinecraftForge.EVENT_BUS.post(new SuperLeadRopeEvent.TransferLeash(this.entity, oldHolderUUID, newHolder))) return false; + LeashInfo preInfo = leashHolders.get(oldHolderUUID); + if(preInfo == null) return false; + SuperLeadRopeEvent.TransferLeash event = new SuperLeadRopeEvent.TransferLeash(this.entity, oldHolderUUID, newHolder, preInfo.maxKeepLeashTicks()); + if(MinecraftForge.EVENT_BUS.post(event)) return false; LeashInfo info = leashHolders.remove(oldHolderUUID); if (info == null || newHolder == null) return false; if(newHolder instanceof SuperLeashKnotEntity superLeashKnotEntity) { - LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity, reserved); + LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity, event.getMaxKeepLeashTicks(), reserved); leashKnots.put(superLeashKnotEntity.getPos(), leashInfo); } else { - LeashInfo leashInfo = info.transferHolder(newHolder, reserved); + LeashInfo leashInfo = info.transferHolder(newHolder, event.getMaxKeepLeashTicks(), reserved); leashHolders.put(newHolder.getUUID(), leashInfo); } LeashStateInnerAPI.Operations.transfer(entity, oldHolderUUID, newHolder); @@ -1155,14 +1204,17 @@ public class LeashDataImpl implements ILeashData { @Override public boolean transferLeash(BlockPos knotPos, Entity newHolder) { - if(MinecraftForge.EVENT_BUS.post(new SuperLeadRopeEvent.TransferLeash(this.entity, knotPos, newHolder))) return false; + LeashInfo preInfo = leashKnots.get(knotPos); + if(preInfo == null) return false; + SuperLeadRopeEvent.TransferLeash event = new SuperLeadRopeEvent.TransferLeash(this.entity, knotPos, newHolder, preInfo.maxKeepLeashTicks()); + if (MinecraftForge.EVENT_BUS.post(event)) return false; LeashInfo info = leashKnots.remove(knotPos); if (info == null || newHolder == null) return false; if(newHolder instanceof SuperLeashKnotEntity superLeashKnotEntity) { - LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity); + LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity, event.getMaxKeepLeashTicks()); leashKnots.put(superLeashKnotEntity.getPos(), leashInfo); } else { - LeashInfo leashInfo = info.transferHolder(newHolder); + LeashInfo leashInfo = info.transferHolder(newHolder, event.getMaxKeepLeashTicks()); leashHolders.put(newHolder.getUUID(), leashInfo); } LeashStateInnerAPI.Operations.transfer(entity, knotPos, newHolder); @@ -1172,14 +1224,17 @@ public class LeashDataImpl implements ILeashData { @Override public boolean transferLeash(BlockPos knotPos, Entity newHolder, String reserved) { - if(MinecraftForge.EVENT_BUS.post(new SuperLeadRopeEvent.TransferLeash(this.entity, knotPos, newHolder))) return false; + LeashInfo preInfo = leashKnots.get(knotPos); + if(preInfo == null) return false; + SuperLeadRopeEvent.TransferLeash event = new SuperLeadRopeEvent.TransferLeash(this.entity, knotPos, newHolder, preInfo.maxKeepLeashTicks()); + if(MinecraftForge.EVENT_BUS.post(event)) return false; LeashInfo info = leashKnots.remove(knotPos); if (info == null || newHolder == null) return false; if(newHolder instanceof SuperLeashKnotEntity superLeashKnotEntity) { - LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity, reserved); + LeashInfo leashInfo = info.transferHolder(superLeashKnotEntity, event.getMaxKeepLeashTicks(), reserved); leashKnots.put(superLeashKnotEntity.getPos(), leashInfo); } else { - LeashInfo leashInfo = info.transferHolder(newHolder, reserved); + LeashInfo leashInfo = info.transferHolder(newHolder, event.getMaxKeepLeashTicks(), reserved); leashHolders.put(newHolder.getUUID(), leashInfo); } LeashStateInnerAPI.Operations.transfer(entity, knotPos, newHolder); diff --git a/src/main/java/top/r3944realms/superleadrope/content/command/Command.java b/src/main/java/top/r3944realms/superleadrope/content/command/Command.java index f5b8433..5b690f5 100644 --- a/src/main/java/top/r3944realms/superleadrope/content/command/Command.java +++ b/src/main/java/top/r3944realms/superleadrope/content/command/Command.java @@ -18,8 +18,13 @@ package top.r3944realms.superleadrope.content.command; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraft.world.entity.Entity; import org.jetbrains.annotations.Nullable; import top.r3944realms.superleadrope.CommonEventHandler; +import top.r3944realms.superleadrope.SuperLeadRope; +import top.r3944realms.superleadrope.content.entity.SuperLeashKnotEntity; import java.util.List; @@ -35,6 +40,27 @@ public class Command { * The constant SHOULD_USE_PREFIX. */ public static boolean SHOULD_USE_PREFIX = CommonEventHandler.leashConfigManager.isCommandPrefixEnabled(); + /** + * The constant BASE_. + */ + public static final String BASE_ = SuperLeadRope.MOD_ID + ".command."; + /** + * The constant ABBREVIATION. + */ + public static final String ABBREVIATION = BASE_ + "abbreviation"; + public static final String BLOCK_POS = BASE_ + ".block_pos"; + + public static final int MAX_SHOW_NUMBER = 4; + public static Component getSLPName(Entity entity) { + if (entity instanceof SuperLeashKnotEntity superLeashKnot) { + BlockPos pos = superLeashKnot.getPos(); + return Component.translatable(BLOCK_POS, pos.getX(), pos.getY(), pos.getZ()); + } + return entity.getName(); + } + public static Component getSLPName(BlockPos pos) { + return Component.translatable(BLOCK_POS, pos.getX(), pos.getY(), pos.getZ()); + } /** * Gets liter argument builder of css. diff --git a/src/main/java/top/r3944realms/superleadrope/content/command/LeashDataCommand.java b/src/main/java/top/r3944realms/superleadrope/content/command/LeashDataCommand.java index da16ff8..54e5830 100644 --- a/src/main/java/top/r3944realms/superleadrope/content/command/LeashDataCommand.java +++ b/src/main/java/top/r3944realms/superleadrope/content/command/LeashDataCommand.java @@ -30,12 +30,13 @@ import net.minecraft.commands.arguments.coordinates.BlockPosArgument; import net.minecraft.commands.arguments.selector.EntitySelector; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.Entity; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import top.r3944realms.superleadrope.CommonEventHandler; -import top.r3944realms.superleadrope.SuperLeadRope; import top.r3944realms.superleadrope.api.type.capabilty.LeashInfo; +import top.r3944realms.superleadrope.config.LeashConfigManager; import top.r3944realms.superleadrope.content.entity.SuperLeashKnotEntity; import top.r3944realms.superleadrope.content.gamerule.server.CreateSuperLeashKnotEntityIfAbsent; import top.r3944realms.superleadrope.core.register.SLPGameruleRegistry; @@ -55,7 +56,7 @@ public class LeashDataCommand { /** * The constant SLP_LEASH_MESSAGE_. */ - public static final String SLP_LEASH_MESSAGE_ = SuperLeadRope.MOD_ID + ".command.leash.message."; + public static final String SLP_LEASH_MESSAGE_ = Command.BASE_ + "leash.message."; /** * The constant LEASH_DATA_GET_. */ @@ -104,25 +105,40 @@ public class LeashDataCommand { LiteralArgumentBuilder literalArgumentBuilder = Commands.literal(PREFIX); LiteralArgumentBuilder $$leashDataRoot = getLiterArgumentBuilderOfCSS("leashdata", !SHOULD_USE_PREFIX, nodeList); RequiredArgumentBuilder $$$add$holder = Commands.argument("holder", EntityArgument.entity()) - .executes(LeashDataCommand::addLeash) - .then(Commands.argument("maxDistance", DoubleArgumentType.doubleArg(1.0, 256.0)) + .executes(context -> addLeash(context, + EntityArgument.getEntity(context, "holder") + ) + ) + .then(Commands.argument("maxDistance", DoubleArgumentType.doubleArg(LeashConfigManager.MAX_DISTANCE_MIN_VALUE, LeashConfigManager.MAX_DISTANCE_MAX_VALUE)) .executes(context -> addLeash(context, - DoubleArgumentType.getDouble(context, "maxDistance"))) - .then(Commands.argument("elasticDistanceScale", DoubleArgumentType.doubleArg(1.0, 128.0)) + EntityArgument.getEntity(context, "holder"), + DoubleArgumentType.getDouble(context, "maxDistance") + ) + ) + .then(Commands.argument("elasticDistanceScale", DoubleArgumentType.doubleArg(LeashConfigManager.ELASTIC_DISTANCE_MIN_VALUE, LeashConfigManager.ELASTIC_DISTANCE_MAX_VALUE)) .executes(context -> addLeash(context, + EntityArgument.getEntity(context, "holder"), DoubleArgumentType.getDouble(context, "maxDistance"), - DoubleArgumentType.getDouble(context, "elasticDistanceScale"))) + DoubleArgumentType.getDouble(context, "elasticDistanceScale") + ) + ) .then(Commands.argument("keepTicks", IntegerArgumentType.integer(0)) .executes(context -> addLeash(context, + EntityArgument.getEntity(context, "holder"), DoubleArgumentType.getDouble(context, "maxDistance"), DoubleArgumentType.getDouble(context, "elasticDistanceScale"), - IntegerArgumentType.getInteger(context, "keepTicks"))) + IntegerArgumentType.getInteger(context, "keepTicks") + ) + ) .then(Commands.argument("reserved", StringArgumentType.string()) .executes(context -> addLeash(context, - DoubleArgumentType.getDouble(context, "maxDistance"), - DoubleArgumentType.getDouble(context, "elasticDistanceScale"), - IntegerArgumentType.getInteger(context, "keepTicks"), - StringArgumentType.getString(context, "reserved"))) + EntityArgument.getEntity(context, "holder"), + DoubleArgumentType.getDouble(context, "maxDistance"), + DoubleArgumentType.getDouble(context, "elasticDistanceScale"), + IntegerArgumentType.getInteger(context, "keepTicks"), + StringArgumentType.getString(context, "reserved") + ) + ) ) ) ) @@ -130,48 +146,64 @@ public class LeashDataCommand { LiteralArgumentBuilder $$$add$pos = Commands.literal("block") .then(Commands.argument("pos", BlockPosArgument.blockPos()) .executes(LeashDataCommand::addBlockLeash) - .then(Commands.argument("maxDistance", DoubleArgumentType.doubleArg(1.0, 256.0)) + .then(Commands.argument("maxDistance", DoubleArgumentType.doubleArg(LeashConfigManager.MAX_DISTANCE_MIN_VALUE, LeashConfigManager.MAX_DISTANCE_MAX_VALUE)) .executes(context -> addBlockLeash(context, - DoubleArgumentType.getDouble(context, "maxDistance"))) - .then(Commands.argument("elasticDistanceScale", DoubleArgumentType.doubleArg(1.0, 128.0)) + DoubleArgumentType.getDouble(context, "maxDistance") + ) + ) + .then(Commands.argument("elasticDistanceScale", DoubleArgumentType.doubleArg(LeashConfigManager.ELASTIC_DISTANCE_MIN_VALUE, LeashConfigManager.ELASTIC_DISTANCE_MAX_VALUE)) .executes(context -> addBlockLeash(context, DoubleArgumentType.getDouble(context, "maxDistance"), - DoubleArgumentType.getDouble(context, "elasticDistanceScale"), 0, "")) + DoubleArgumentType.getDouble(context, "elasticDistanceScale") + ) + ) .then(Commands.argument("keepTicks", IntegerArgumentType.integer(0)) .executes(context -> addBlockLeash(context, DoubleArgumentType.getDouble(context, "maxDistance"), DoubleArgumentType.getDouble(context, "elasticDistanceScale"), - IntegerArgumentType.getInteger(context, "keepTicks"))) + IntegerArgumentType.getInteger(context, "keepTicks") + ) + ) .then(Commands.argument("reserved", StringArgumentType.string()) .executes(context -> addBlockLeash(context, DoubleArgumentType.getDouble(context, "maxDistance"), DoubleArgumentType.getDouble(context, "elasticDistanceScale"), IntegerArgumentType.getInteger(context, "keepTicks"), - StringArgumentType.getString(context, "reserved"))) + StringArgumentType.getString(context, "reserved") + ) + ) ) ) ) ) ); - LiteralArgumentBuilder $$$add = Commands.literal("addApplyEntity") - .then(Commands.argument("target", EntityArgument.entities()) + LiteralArgumentBuilder $$$add = Commands.literal("add") + .then(Commands.argument("targets", EntityArgument.entities()) // 实体拴绳 .then($$$add$holder) // 方块拴绳 .then($$$add$pos) ); - LiteralArgumentBuilder $$$remove = Commands.literal("removeApplyEntity") - .then(Commands.argument("target", EntityArgument.entities()) + LiteralArgumentBuilder $$$remove = Commands.literal("remove") + .then(Commands.argument("targets", EntityArgument.entities()) // 移除特定实体拴绳 .then(Commands.argument("holder", EntityArgument.entity()) - .executes(LeashDataCommand::removeLeash) + .executes(context -> removeLeash( + context, + EntityArgument.getEntity(context, "holder") + ) + ) ) // 移除方块拴绳 .then(Commands.literal("block") .then(Commands.argument("pos", BlockPosArgument.blockPos()) - .executes(LeashDataCommand::removeBlockLeash) + .executes(context -> removeBlockLeash( + context, + BlockPosArgument.getBlockPos(context, "pos") + ) + ) ) ) @@ -187,14 +219,23 @@ public class LeashDataCommand { ) ); LiteralArgumentBuilder $$$transfer = Commands.literal("transfer") - .then(Commands.argument("target", EntityArgument.entities()) + .then(Commands.argument("targets", EntityArgument.entities()) // 实体到实体转移 .then(Commands.argument("from", EntityArgument.entity()) .then(Commands.argument("to", EntityArgument.entity()) - .executes(LeashDataCommand::transferLeash) + .executes(context -> transferLeash( + context, + EntityArgument.getEntity(context, "from"), + EntityArgument.getEntity(context, "to") + ) + ) .then(Commands.argument("reserved", StringArgumentType.string()) .executes(context -> transferLeash(context, - StringArgumentType.getString(context, "reserved"))) + EntityArgument.getEntity(context, "from"), + EntityArgument.getEntity(context, "to"), + StringArgumentType.getString(context, "reserved") + ) + ) ) ) ) @@ -215,31 +256,54 @@ public class LeashDataCommand { RequiredArgumentBuilder $$$set$holder = Commands.argument("holder", EntityArgument.entity()) // 设置最大距离 .then(Commands.literal("maxDistance") - .then(Commands.argument("distance", DoubleArgumentType.doubleArg(1.0, 256.0)) - .executes(LeashDataCommand::setMaxDistance) + .executes(LeashDataCommand::setMaxDistance) + .then(Commands.argument("distance", DoubleArgumentType.doubleArg(LeashConfigManager.MAX_DISTANCE_MIN_VALUE, LeashConfigManager.MAX_DISTANCE_MAX_VALUE)) + .executes(context -> setMaxDistance( + context, + DoubleArgumentType.getDouble(context,"distance") + ) + ) .then(Commands.argument("keepTicks", IntegerArgumentType.integer(0)) .executes(context -> setMaxDistance(context, - IntegerArgumentType.getInteger(context, "keepTicks"))) + DoubleArgumentType.getDouble(context,"distance"), + IntegerArgumentType.getInteger(context, "keepTicks") + ) + ) .then(Commands.argument("reserved", StringArgumentType.string()) .executes(context -> setMaxDistance(context, + DoubleArgumentType.getDouble(context,"distance"), IntegerArgumentType.getInteger(context, "keepTicks"), - StringArgumentType.getString(context, "reserved"))) + StringArgumentType.getString(context, "reserved") + ) + ) ) ) ) ) - // 设置弹性距离 + // 设置弹性距离比例 .then(Commands.literal("elasticDistanceScale") - .then(Commands.argument("distance", DoubleArgumentType.doubleArg(1.0, 128.0)) - .executes(context -> setElasticDistance(context, 0, "")) + .executes(LeashDataCommand::setElasticDistanceScale) + .then(Commands.argument("scale", DoubleArgumentType.doubleArg(LeashConfigManager.ELASTIC_DISTANCE_MIN_VALUE, LeashConfigManager.ELASTIC_DISTANCE_MAX_VALUE)) + .executes(context -> setElasticDistanceScale( + context, + DoubleArgumentType.getDouble(context,"scale") + ) + ) .then(Commands.argument("keepTicks", IntegerArgumentType.integer(0)) - .executes(context -> setElasticDistance(context, - IntegerArgumentType.getInteger(context, "keepTicks"), "")) + .executes(context -> setElasticDistanceScale( + context, + DoubleArgumentType.getDouble(context,"scale"), + IntegerArgumentType.getInteger(context, "keepTicks") + ) + ) .then(Commands.argument("reserved", StringArgumentType.string()) - .executes(context -> setElasticDistance(context, + .executes(context -> setElasticDistanceScale(context, + DoubleArgumentType.getDouble(context,"scale"), IntegerArgumentType.getInteger(context, "keepTicks"), - StringArgumentType.getString(context, "reserved"))) + StringArgumentType.getString(context, "reserved") + ) + ) ) ) ) @@ -248,38 +312,60 @@ public class LeashDataCommand { .then(Commands.argument("pos", BlockPosArgument.blockPos()) // 设置最大距离 .then(Commands.literal("maxDistance") - .then(Commands.argument("distance", DoubleArgumentType.doubleArg(1.0, 256.0)) - .executes(LeashDataCommand::setBlockMaxDistance) + .executes(LeashDataCommand::setBlockMaxDistance) + .then(Commands.argument("distance", DoubleArgumentType.doubleArg(LeashConfigManager.MAX_DISTANCE_MIN_VALUE, LeashConfigManager.MAX_DISTANCE_MAX_VALUE)) + .executes(context -> setBlockMaxDistance( + context, + DoubleArgumentType.getDouble(context, "distance") + ) + ) .then(Commands.argument("keepTicks", IntegerArgumentType.integer(0)) .executes(context -> setBlockMaxDistance(context, - IntegerArgumentType.getInteger(context, "keepTicks"))) + DoubleArgumentType.getDouble(context, "distance"), + IntegerArgumentType.getInteger(context, "keepTicks") + ) + ) .then(Commands.argument("reserved", StringArgumentType.string()) .executes(context -> setBlockMaxDistance(context, + DoubleArgumentType.getDouble(context, "distance"), IntegerArgumentType.getInteger(context, "keepTicks"), - StringArgumentType.getString(context, "reserved"))) + StringArgumentType.getString(context, "reserved") + ) + ) ) ) ) ) - // 设置弹性距离 + // 设置弹性距离比例 .then(Commands.literal("elasticDistanceScale") - .then(Commands.argument("distance", DoubleArgumentType.doubleArg(1.0, 128.0)) - .executes(LeashDataCommand::setBlockElasticDistance) + .executes(LeashDataCommand::setBlockElasticDistanceScale) + .then(Commands.argument("scale", DoubleArgumentType.doubleArg(LeashConfigManager.ELASTIC_DISTANCE_MIN_VALUE, LeashConfigManager.ELASTIC_DISTANCE_MAX_VALUE)) + .executes(context -> setBlockElasticDistanceScale( + context, + DoubleArgumentType.getDouble(context, "scale") + ) + ) .then(Commands.argument("keepTicks", IntegerArgumentType.integer(0)) - .executes(context -> setBlockElasticDistance(context, - IntegerArgumentType.getInteger(context, "keepTicks"))) + .executes(context -> setBlockElasticDistanceScale(context, + DoubleArgumentType.getDouble(context, "scale"), + IntegerArgumentType.getInteger(context, "keepTicks") + ) + ) .then(Commands.argument("reserved", StringArgumentType.string()) - .executes(context -> setBlockElasticDistance(context, + .executes(context -> setBlockElasticDistanceScale(context, + DoubleArgumentType.getDouble(context, "scale"), IntegerArgumentType.getInteger(context, "keepTicks"), - StringArgumentType.getString(context, "reserved"))) + StringArgumentType.getString(context, "reserved") + ) + ) ) ) ) ) ); - LiteralArgumentBuilder $$$set = Commands.literal("setApplyEntity") - .then(Commands.argument("target", EntityArgument.entities()) + LiteralArgumentBuilder $$$set = Commands.literal("set") + .then(Commands.argument("targets", EntityArgument.entities()) // 实体拴绳设置 .then($$$set$holder) @@ -287,12 +373,14 @@ public class LeashDataCommand { .then($$$set$pos) ); LiteralArgumentBuilder $$$applayForces = Commands.literal("applyForces") - .then(Commands.argument("target", EntityArgument.entities()) + .then(Commands.argument("targets", EntityArgument.entities()) .executes(LeashDataCommand::applyForces) ); LiteralArgumentBuilder $$$get = Commands.literal("get") - .then(Commands.argument("target", EntityArgument.entities()) - .executes(LeashDataCommand::getLeashData) + .then(Commands.literal("data") + .then(Commands.argument("targets", EntityArgument.entities()).executes(LeashDataCommand::getLeashData))) + .then(Commands.literal("info") + .then(Commands.argument("target", EntityArgument.entity()).executes(LeashDataCommand::getLeashInfo)) ); $$leashDataRoot .requires(source -> source.hasPermission(2)) // 需要OP权限 @@ -327,19 +415,25 @@ public class LeashDataCommand { /** * The constant SET_MAX_DISTANCE. */ - public static final String SET_MAX_DISTANCE = SLP_LEASH_MESSAGE_ + "set_apply_entity.max_distance"; + public static final String SET_MAX_DISTANCE_ = SLP_LEASH_MESSAGE_ + "set.max_distance."; + private static int setMaxDistance(CommandContext context) throws CommandSyntaxException { - return setMaxDistance(context, CommonEventHandler.leashConfigManager.getMaxLeashLength(), ""); + return setMaxDistance(context, -1/* -1 -> null*/); } private static int setMaxDistance(CommandContext context, double maxDistance) throws CommandSyntaxException { - return setMaxDistance(context, maxDistance, ""); + return setMaxDistance(context, maxDistance, 0); + } private static int setMaxDistance(CommandContext context, double maxDistance, int keepTicks) throws CommandSyntaxException { + return setMaxDistance(context, maxDistance, keepTicks, null); } - private static int setMaxDistance(CommandContext context, double maxDistance, String reserved) throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); + + private static int setMaxDistance(CommandContext context, double maxDistance, int keepTicks,@Nullable String reserved) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); Entity holder = EntityArgument.getEntity(context, "holder"); for (Entity target : targets) { - + boolean isSuccessful = LeashDataInnerAPI.PropertyOperations.setMaxDistance(holder, target, maxDistance== -1 ? null : maxDistance, keepTicks, reserved); } + // Successfully adjusted leash to the max distance from %s[if more than 4 items, display in abbreviated form] to %s[BlockPos] + // [if failed then add ", but failed to adjust from %s[if more than 4 items, display in abbreviated form] to %s", else "."] return -1; } @@ -348,6 +442,12 @@ public class LeashDataCommand { */ public static final String REMOVE_ALL_BLOCK_LEASHES = SLP_LEASH_MESSAGE_ + "remove_apply_entity.all_block_leashes"; private static int removeAllBlockLeashes(CommandContext context) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + for (Entity target : targets) { + LeashDataInnerAPI.LeashOperations.detachAllKnots(target); + } + // Successfully adjusted leash to the max distance from %s[if more than 4 items, display in abbreviated form] to %s[BlockPos] + // [if failed then add ", but failed to adjust from %s[if more than 4 items, display in abbreviated form] to %s", else "."] return -1; } @@ -356,6 +456,10 @@ public class LeashDataCommand { */ public static final String REMOVE_ALL_HOLDER_LEASHES = SLP_LEASH_MESSAGE_ + "remove_apply_entity.all_holder_leashes"; private static int removeAllHolderLeashes(CommandContext context) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + for (Entity target : targets) { + LeashDataInnerAPI.LeashOperations.detachAllHolders(target); + } return -1; } @@ -367,20 +471,36 @@ public class LeashDataCommand { return transferFromBlock(context, ""); } private static int transferFromBlock(CommandContext context, String reserved) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + Entity from = EntityArgument.getEntity(context, "from"); + Entity to = EntityArgument.getEntity(context, "to"); + for (Entity target : targets) { + boolean isSuccessful = LeashDataInnerAPI.TransferOperations.transfer(target, from, to, reserved); + } return -1; } /** - * The constant SET_ELASTIC_DISTANCE. + * The constant SET_ELASTIC_DISTANCE_SCALE. */ - public static final String SET_ELASTIC_DISTANCE = SLP_LEASH_MESSAGE_ + "set_apply_entity.elastic_distance"; - private static int setElasticDistance(CommandContext context) throws CommandSyntaxException { - return setElasticDistance(context, 0 ,""); + public static final String SET_ELASTIC_DISTANCE_SCALE = SLP_LEASH_MESSAGE_ + "set_apply_entity.elastic_distance_scale"; + private static int setElasticDistanceScale(CommandContext context) throws CommandSyntaxException { + return setElasticDistanceScale(context, -1); } - private static int setElasticDistance(CommandContext context, int keepTicks) throws CommandSyntaxException { - return setElasticDistance(context, keepTicks ,""); + private static int setElasticDistanceScale(CommandContext context, double maxDistance) throws CommandSyntaxException { + return setElasticDistanceScale(context, maxDistance ,0); } - private static int setElasticDistance(CommandContext context, int keepTicks, String reserved) throws CommandSyntaxException { + private static int setElasticDistanceScale(CommandContext context, double maxDistance, int keepTicks) throws CommandSyntaxException { + return setElasticDistanceScale(context, maxDistance, keepTicks,null); + } + private static int setElasticDistanceScale(CommandContext context, double maxDistance, int keepTicks, String reserved) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + Entity holder = EntityArgument.getEntity(context, "holder"); + for (Entity target : targets) { + boolean isSuccessful = LeashDataInnerAPI.PropertyOperations.setMaxDistance(holder, target, maxDistance == -1 ? null : maxDistance, keepTicks, reserved); + } + // Successfully adjusted leash to the elastic distance scale from %s[if more than 4 items, display in abbreviated form] to %s[Entity] + // [if failed then add ", but failed to adjust from %s[if more than 4 items, display in abbreviated form] to %s", else "."] return -1; } @@ -389,39 +509,57 @@ public class LeashDataCommand { */ public static final String SET_BLOCK_MAX_DISTANCE = SLP_LEASH_MESSAGE_ + "set_apply_entity.block_max_distance"; private static int setBlockMaxDistance(CommandContext context) throws CommandSyntaxException { - return setBlockMaxDistance(context, 0 ,""); + return setBlockMaxDistance(context, -1); } - private static int setBlockMaxDistance(CommandContext context, int keepTicks) throws CommandSyntaxException { - return setBlockMaxDistance(context, keepTicks ,""); + private static int setBlockMaxDistance(CommandContext context, double maxDistance) throws CommandSyntaxException { + return setBlockMaxDistance(context, maxDistance, 0); } - private static int setBlockMaxDistance(CommandContext context, int keepTicks, String reserved) throws CommandSyntaxException { + private static int setBlockMaxDistance(CommandContext context, double maxDistance, int keepTicks) throws CommandSyntaxException { + return setBlockMaxDistance(context, maxDistance, keepTicks, null); + } + private static int setBlockMaxDistance(CommandContext context, double maxDistance, int keepTicks, String reserved) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + Entity holder = EntityArgument.getEntity(context, "holder"); + for (Entity target : targets) { + boolean isSuccessful = LeashDataInnerAPI.PropertyOperations.setMaxDistance(holder, target, maxDistance == -1 ? null : maxDistance, keepTicks, reserved); + } + // Successfully adjusted leash to the max distance from %s[if more than 4 items, display in abbreviated form] to %s[Entity] + // [if failed then add ", but failed to adjust from %s[if more than 4 items, display in abbreviated form] to %s", else "."] return -1; } /** - * The constant SET_BLOCK_ELASTIC_DISTANCE. + * The constant SET_BLOCK_ELASTIC_DISTANCE_SCALE. */ - public static final String SET_BLOCK_ELASTIC_DISTANCE = SLP_LEASH_MESSAGE_ + "set_apply_entity.block_elastic_distance"; - private static int setBlockElasticDistance(CommandContext context) throws CommandSyntaxException { - return setBlockElasticDistance(context, 0 ,""); + public static final String SET_BLOCK_ELASTIC_DISTANCE_SCALE = SLP_LEASH_MESSAGE_ + "set_apply_entity.block_elastic_distance_scale"; + private static int setBlockElasticDistanceScale(CommandContext context) throws CommandSyntaxException { + return setBlockElasticDistanceScale(context, -1); } - private static int setBlockElasticDistance(CommandContext context, int keepTicks) throws CommandSyntaxException { - return setBlockElasticDistance(context, keepTicks ,""); + private static int setBlockElasticDistanceScale(CommandContext context, double scale) throws CommandSyntaxException { + return setBlockElasticDistanceScale(context, scale ,0); } - private static int setBlockElasticDistance(CommandContext context, int keepTicks, String reserved) throws CommandSyntaxException { + private static int setBlockElasticDistanceScale(CommandContext context, double scale, int keepTicks) throws CommandSyntaxException { + return setBlockElasticDistanceScale(context, scale ,keepTicks, null); + } + private static int setBlockElasticDistanceScale(CommandContext context, double scale, int keepTicks,@Nullable String reserved) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + Entity holder = EntityArgument.getEntity(context, "holder"); + for (Entity target : targets) { + + boolean isSuccessful = LeashDataInnerAPI.PropertyOperations.setElasticDistanceScale(holder, target, scale == -1 ? null : scale, keepTicks, reserved); + } + // Successfully adjusted leash to the elastic distance scale from %s[if more than 4 items, display in abbreviated form] to %s[BlockPos] + // [if failed then add ", but failed to adjust from %s[if more than 4 items, display in abbreviated form] to %s", else "."] return -1; } // ==================== 命令执行方法 ==================== - private static int getLeashData(CommandContext context) throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); + Collection targets = EntityArgument.getEntities(context, "targets"); CommandSourceStack source = context.getSource(); for (Entity target : targets) { Collection leashes = LeashDataInnerAPI.QueryOperations.getAllLeashes(target); - // +++ - source.sendSuccess(() -> Component.literal("=== Leash Data for " + target.getName().getString() + " ==="), false); source.sendSuccess(() -> Component.literal("Total leashes: " + leashes.size()), false); // TODO:翻译支持 HoverTip实现部分信息简化显示 @@ -438,57 +576,156 @@ public class LeashDataCommand { source.sendSuccess(() -> Component.literal(info.toString()), false); } + // LeashData: [Entity]{Holder:{[Entity]}, BlockPos{[BlockPos]}} ... } return targets.size(); } - private static int addLeash(CommandContext context) throws CommandSyntaxException { - return addLeash(context, CommonEventHandler.leashConfigManager.getMaxLeashLength(), CommonEventHandler.leashConfigManager.getElasticDistanceScale(), 0, ""); - } - - private static int addLeash(CommandContext context, - double maxDistance) throws CommandSyntaxException { - return addLeash(context, maxDistance, CommonEventHandler.leashConfigManager.getElasticDistanceScale(), 0, ""); - } - - private static int addLeash(CommandContext context, - double maxDistance, double elasticDistance) throws CommandSyntaxException { - return addLeash(context, maxDistance, elasticDistance, 0, ""); - } - private static int addLeash(CommandContext context, - double maxDistance, double elasticDistance, int keepTicks) throws CommandSyntaxException { - return addLeash(context, maxDistance, elasticDistance, keepTicks, ""); - } - private static int addLeash(CommandContext context, - double maxDistance, double elasticDistance, int keepTicks, String reserved) - throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); - Entity holder = EntityArgument.getEntity(context, "holder"); + private static int getLeashInfo(CommandContext context) throws CommandSyntaxException { + Entity target = EntityArgument.getEntity(context, "target"); CommandSourceStack source = context.getSource(); - List successful = new ArrayList<>(), failed = new ArrayList<>(); - for (Entity target : targets) { - if(LeashDataInnerAPI.LeashOperations.attach(target, holder, maxDistance, elasticDistance, keepTicks, reserved)) { - successful.add(target); - } else failed.add(target); + + + Collection leashes = LeashDataInnerAPI.QueryOperations.getAllLeashes(target); + // +++ + + source.sendSuccess(() -> Component.literal("=== Leash Info from " + target.getName().getString() + " ==="), false); + source.sendSuccess(() -> Component.literal("Total leashes: " + leashes.size()), false); + // TODO:翻译支持 HoverTip实现部分信息简化显示 + for (LeashInfo leash : leashes) { + StringBuilder info = new StringBuilder(); + leash.blockPosOpt().ifPresent(pos -> info.append("Block: ").append(pos.toShortString()).append(" ")); + leash.holderUUIDOpt().ifPresent(uuid -> info.append("UUID: ").append(uuid).append(" ")); + info.append("Max: ").append(leash.maxDistance()).append(" "); + info.append("Elastic: ").append(leash.elasticDistanceScale()).append(" "); + info.append("Keep: ").append(leash.keepLeashTicks()).append("/").append(leash.maxKeepLeashTicks()); + if (!leash.reserved().isEmpty()) { + info.append(" Reserved: ").append(leash.reserved()); + } + + source.sendSuccess(() -> Component.literal(info.toString()), false); } + // LeashInfo: [Entity]{LeashInfo: {B/U, M, E, K, R} ..,} + + + return 0; + } + private static int addLeash(CommandContext context, Entity holder) throws CommandSyntaxException { + return addLeash(context, holder,-1); + } + private static int addLeash(CommandContext context, Entity holder, + double maxDistance) throws CommandSyntaxException { + return addLeash(context, holder, maxDistance, -1); + } + + private static int addLeash(CommandContext context, Entity holder, + double maxDistance, double elasticDistance) throws CommandSyntaxException { + return addLeash(context, holder, maxDistance, elasticDistance, 0); + } + private static int addLeash(CommandContext context, Entity holder, + double maxDistance, double elasticDistance, int keepTicks) throws CommandSyntaxException { + return addLeash(context, holder, maxDistance, elasticDistance, keepTicks, "");//add 默认保留"",其余方法缺省为null + } + + /** + * The constant ADD_HOLDER_LEASHES_. + */ + public static final String ADD_HOLDER_LEASHES_ = SLP_LEASH_MESSAGE_ + "add_holder."; + /** + * The constant ADD_HOLDER_LEASHES_SUC. + */ + public static final String ADD_HOLDER_LEASHES_SUC = ADD_HOLDER_LEASHES_ + "suc"; + /** + * The constant ADD_HOLDER_LEASHES_SUC_FAIL. + */ + public static final String ADD_HOLDER_LEASHES_SUC_FAIL = ADD_HOLDER_LEASHES_ + "suc_fail"; + /** + * The constant ADD_HOLDER_LEASHES_FAIL. + */ + public static final String ADD_HOLDER_LEASHES_FAIL = ADD_HOLDER_LEASHES_ + "fail"; + private static int addLeash(CommandContext context, Entity holder, + double maxDistance, double elasticDistanceScale, int keepTicks, String reserved) + throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + + CommandSourceStack source = context.getSource(); + int success = -1, failure = -1; + Entity[] sucEntities = new Entity[MAX_SHOW_NUMBER], failEntities = new Entity[MAX_SHOW_NUMBER]; + for (Entity target : targets) { + boolean isSuccessful = LeashDataInnerAPI.LeashOperations.attach(target, holder, maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved); + if (isSuccessful) { + if (++success <= MAX_SHOW_NUMBER - 1) { + sucEntities[success] = target; + } + } else { + failure++; + if (++failure <= MAX_SHOW_NUMBER - 1) { + failEntities[failure] = target; + } + } + } + MutableComponent failureEntitiesComponent = Component.empty(), successEntitiesComponent = Component.empty(); + generateEntityComponent(sucEntities, success, successEntitiesComponent); + generateEntityComponent(failEntities, failure, failureEntitiesComponent); + MutableComponent send = Component.empty(); + if (success >= 0) { + // Successfully attached >leash<[Basic Info] from %s[if more than 4 items, display in abbreviated form] to %s[Entity] + send.append(Component.translatable(ADD_HOLDER_LEASHES_SUC, showLeashInfo(maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved), successEntitiesComponent, Command.getSLPName(holder))); + if (failure >= 0) { + // , but failed to attached >leash<[Basic Info] from %s to %s [Leashed: [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(ADD_HOLDER_LEASHES_SUC_FAIL, showLeashInfo(maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved), failureEntitiesComponent, Command.getSLPName(holder))); + } //. + else send.append("."); + } else { + // Failed to attached >leash<[Basic Info] from %s to %s [Leashed: [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(ADD_HOLDER_LEASHES_FAIL, showLeashInfo(maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved), failureEntitiesComponent, Command.getSLPName(holder))); + } + source.sendSuccess(() -> send, true); + return 0; + // + // [if failed then add ", but failed to attached leash from %s[if more than 4 items, display in abbreviated form] to %s", else "."] // todo: source.sendSuccess(() -> Component.translatable(/*成功{},失败{}*/), true); - return successful.size(); } private static int addBlockLeash(CommandContext context) throws CommandSyntaxException { - return addBlockLeash(context, CommonEventHandler.leashConfigManager.getMaxLeashLength(), CommonEventHandler.leashConfigManager.getElasticDistanceScale(), 0, ""); + return addBlockLeash(context, -1); } private static int addBlockLeash(CommandContext context, double maxDistance) throws CommandSyntaxException { - return addBlockLeash(context, maxDistance, CommonEventHandler.leashConfigManager.getElasticDistanceScale(), 0, ""); + return addBlockLeash(context, maxDistance, -1); } private static int addBlockLeash(CommandContext context, - double maxDistance, double elasticDistance, int keepTicks) throws CommandSyntaxException { - return addBlockLeash(context, maxDistance, elasticDistance, keepTicks, ""); + double maxDistance, double elasticDistanceScale) throws CommandSyntaxException { + return addBlockLeash(context, maxDistance, elasticDistanceScale, 0); } private static int addBlockLeash(CommandContext context, - double maxDistance, double elasticDistance, int keepTicks, String reserved) + double maxDistance, double elasticDistanceScale, int keepTicks) throws CommandSyntaxException { + return addBlockLeash(context, maxDistance, elasticDistanceScale, keepTicks, "");//add 默认保留"",其余方法缺省为null + } + + /** + * The constant ADD_BLOCK_LEASHES_. + */ + public static final String ADD_BLOCK_LEASHES_ = SLP_LEASH_MESSAGE_ + "add_block."; + /** + * The constant ADD_BLOCK_LEASHES_SUC. + */ + public static final String ADD_BLOCK_LEASHES_SUC = ADD_BLOCK_LEASHES_ + "suc"; + /** + * The constant ADD_BLOCK_LEASHES_SUC_FAIL. + */ + public static final String ADD_BLOCK_LEASHES_SUC_FAIL = ADD_BLOCK_LEASHES_ + "suc_fail"; + /** + * The constant ADD_BLOCK_LEASHES_FAIL. + */ + public static final String ADD_BLOCK_LEASHES_FAIL = ADD_BLOCK_LEASHES_ + "fail"; + /** + * The constant ADD_BLOCK_LEASHES_FAIL_NO_KNOT_FOUND. + */ + public static final String ADD_BLOCK_LEASHES_FAIL_NO_KNOT_FOUND = ADD_BLOCK_LEASHES_ + "fail.no_knot_found"; + private static int addBlockLeash(CommandContext context, + double maxDistance, double elasticDistanceScale, int keepTicks, String reserved) throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); + Collection targets = EntityArgument.getEntities(context, "targets"); BlockPos pos = BlockPosArgument.getBlockPos(context, "pos"); CommandSourceStack source = context.getSource(); ServerLevel level = source.getLevel(); @@ -499,114 +736,282 @@ public class LeashDataCommand { else return Optional.empty(); }).orElse(null); if (knotEntity == null) { + // Failed to attach leash to %s because there is not existed knot in pos."] + source.sendFailure(Component.translatable(ADD_BLOCK_LEASHES_FAIL_NO_KNOT_FOUND, Command.getSLPName(pos))); // todo: source.sendFailure(Component.translatable(/*失败,目标上无拴绳结*/)); return -1; } - List successful = new ArrayList<>(), failed = new ArrayList<>(); + int success = -1, failure = -1; + Entity[] sucEntities = new Entity[MAX_SHOW_NUMBER], failEntities = new Entity[MAX_SHOW_NUMBER]; for (Entity target : targets) { - if(LeashDataInnerAPI.LeashOperations.attach(target, knotEntity, maxDistance, elasticDistance, keepTicks, reserved)) { - successful.add(target); - } else failed.add(target); + boolean isSuccessful = LeashDataInnerAPI.LeashOperations.attach(target, knotEntity, maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved); + if (isSuccessful) { + if (++success <= MAX_SHOW_NUMBER - 1) { + sucEntities[success] = target; + } + } else { + failure++; + if (++failure <= MAX_SHOW_NUMBER - 1) { + failEntities[failure] = target; + } + } } + MutableComponent failureEntitiesComponent = Component.empty(), successEntitiesComponent = Component.empty(); + generateEntityComponent(sucEntities, success, successEntitiesComponent); + generateEntityComponent(failEntities, failure, failureEntitiesComponent); + MutableComponent send = Component.empty(); + if (success >= 0) { + // Successfully attached >leash<[Basic Info] from %s[if more than 4 items, display in abbreviated form] to %s[BlockPos] + send.append(Component.translatable(ADD_BLOCK_LEASHES_SUC, showLeashInfo(maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved), successEntitiesComponent, Command.getSLPName(pos))); + if (failure >= 0) { + // , but failed to attached >leash<[Basic Info] from %s to %s [Leashed: [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(ADD_BLOCK_LEASHES_SUC_FAIL, showLeashInfo(maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved), failureEntitiesComponent, Command.getSLPName(pos))); + } //. + else send.append("."); + } else { + // Failed to attached >leash<[Basic Info] from %s to %s [Leashed: [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(ADD_BLOCK_LEASHES_FAIL, showLeashInfo(maxDistance == -1 ? null : maxDistance, elasticDistanceScale == -1 ? null : elasticDistanceScale, keepTicks, reserved), failureEntitiesComponent, Command.getSLPName(pos))); + } + source.sendSuccess(() -> send, true); + return 0; + // + // [if failed then add ", but failed to attach leash from %s[if more than 4 items, display in abbreviated form] to %s", else "."] // todo: source.sendSuccess(() -> Component.translatable(/*成功{},失败{}*/), true); - return successful.size(); } - private static int removeLeash(CommandContext context) throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); - Entity holder = EntityArgument.getEntity(context, "holder"); - CommandSourceStack source = context.getSource(); - int successCount = 0; - /* - Removed leash from %s[more than 4只显示前4个] held by %s[如果有失败则加上, But no leash found form %s on %s[more than 4 只显示前4个]] - */ - for (Entity target : targets) { - boolean success = LeashDataInnerAPI.LeashOperations.detach(target, holder); + /** + * The constant LEASH_INFO. + */ + public static final String LEASH_INFO = SLP_LEASH_MESSAGE_ + "leash.info"; + /** + * The constant DEFAULT. + */ + public static final String DEFAULT = SLP_LEASH_MESSAGE_ + "default"; + private static Component showLeashInfo(@Nullable Double maxDistance, @Nullable Double elasticDistanceScale, int keepTicks, String reserved) { + return Component.translatable(LEASH_INFO, maxDistance == null ? DEFAULT : maxDistance , elasticDistanceScale == null ? DEFAULT : elasticDistanceScale, keepTicks, reserved); + } - if (success) { - successCount++; - source.sendSuccess(() -> Component.literal("Removed leash from " + target.getName().getString() + - " held by " + holder.getName().getString()), false); + /** + * The constant REMOVE_HOLDER_LEASHES_. + */ + public static final String REMOVE_HOLDER_LEASHES_ = SLP_LEASH_MESSAGE_ + "remove_holder."; + /** + * The constant REMOVE_HOLDER_LEASHES_SUC. + */ + public static final String REMOVE_HOLDER_LEASHES_SUC = REMOVE_HOLDER_LEASHES_ + "suc"; + /** + * The constant REMOVE_HOLDER_LEASHES_SUC_FAIL. + */ + public static final String REMOVE_HOLDER_LEASHES_SUC_FAIL = REMOVE_HOLDER_LEASHES_ + "suc_fail"; + /** + * The constant REMOVE_HOLDER_LEASHES_FAIL. + */ + public static final String REMOVE_HOLDER_LEASHES_FAIL = REMOVE_HOLDER_LEASHES_ + "fail"; + private static int removeLeash(CommandContext context, Entity holder) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + CommandSourceStack source = context.getSource(); + int success = -1, failure = -1; + Entity[] sucEntities = new Entity[MAX_SHOW_NUMBER], failEntities = new Entity[MAX_SHOW_NUMBER]; + for (Entity target : targets) { + boolean isSuccessful = LeashDataInnerAPI.LeashOperations.detach(target, holder); + if (isSuccessful) { + if (++success <= MAX_SHOW_NUMBER - 1) { + sucEntities[success] = target; + } } else { - source.sendFailure(Component.literal("No leash found for " + holder.getName().getString() + - " on " + target.getName().getString())); + failure++; + if (++failure <= MAX_SHOW_NUMBER - 1) { + failEntities[failure] = target; + } } } - - return successCount; + MutableComponent failureEntitiesComponent = Component.empty(), successEntitiesComponent = Component.empty(); + generateEntityComponent(sucEntities, success, successEntitiesComponent); + generateEntityComponent(failEntities, failure, failureEntitiesComponent); + MutableComponent send = Component.empty(); + if (success >= 0) { + // Successfully detached leash from %s[if more than 4 items, display in abbreviated form] to %s[Entity] + send.append(Component.translatable(REMOVE_HOLDER_LEASHES_SUC, successEntitiesComponent, Command.getSLPName(holder))); + if (failure >= 0) { + // , but failed to detach leash from %s[if more than 4 items, display in abbreviated form] to %s + send.append(Component.translatable(REMOVE_HOLDER_LEASHES_SUC_FAIL, failureEntitiesComponent, Command.getSLPName(holder))); + } // . + else send.append("."); + } else { + // Failed to detach leash from %s to %s [Leashed: [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(REMOVE_HOLDER_LEASHES_FAIL, failureEntitiesComponent, Command.getSLPName(holder))); + } + source.sendSuccess(() -> send, true); + return 0; } - private static int removeBlockLeash(CommandContext context) throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); - BlockPos pos = BlockPosArgument.getBlockPos(context, "pos"); + /** + * The constant REMOVE_BLOCK_LEASHES_. + */ + public static final String REMOVE_BLOCK_LEASHES_ = SLP_LEASH_MESSAGE_ + "remove_knot."; + /** + * The constant REMOVE_BLOCK_LEASHES_SUC. + */ + public static final String REMOVE_BLOCK_LEASHES_SUC = REMOVE_BLOCK_LEASHES_ + "suc"; + /** + * The constant REMOVE_BLOCK_LEASHES_SUC_FAIL. + */ + public static final String REMOVE_BLOCK_LEASHES_SUC_FAIL = REMOVE_BLOCK_LEASHES_ + "suc_fail"; + /** + * The constant REMOVE_BLOCK_LEASHES_FAIL. + */ + public static final String REMOVE_BLOCK_LEASHES_FAIL = REMOVE_BLOCK_LEASHES_ + "fail"; + private static int removeBlockLeash(CommandContext context, BlockPos pos) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); CommandSourceStack source = context.getSource(); - int successCount = 0; - + int success = -1, failure = -1; + Entity[] sucEntities = new Entity[MAX_SHOW_NUMBER], failEntities = new Entity[MAX_SHOW_NUMBER]; for (Entity target : targets) { - boolean success = LeashDataInnerAPI.LeashOperations.detach(target, pos); - - if (success) { - successCount++; - source.sendSuccess(() -> Component.literal("Removed block leash from " + target.getName().getString() + - " at " + pos.toShortString()), false); + boolean isSuccessful = LeashDataInnerAPI.LeashOperations.detach(target, pos); + if (isSuccessful) { + if (++success <= MAX_SHOW_NUMBER - 1) { + sucEntities[success] = target; + } } else { - source.sendFailure(Component.literal("No block leash found at " + pos.toShortString() + - " on " + target.getName().getString())); + failure++; + if (++failure <= MAX_SHOW_NUMBER - 1) { + failEntities[failure] = target; + } } } - - return successCount; + MutableComponent failureEntitiesComponent = Component.empty(), successEntitiesComponent = Component.empty(); + generateEntityComponent(sucEntities, success, successEntitiesComponent); + generateEntityComponent(failEntities, failure, failureEntitiesComponent); + MutableComponent send = Component.empty(); + if (success >= 0) { + // Successfully detached leash from %s[if more than 4 items, display in abbreviated form] to %s[BlockPos] + send.append(Component.translatable(REMOVE_BLOCK_LEASHES_SUC, successEntitiesComponent, Command.getSLPName(pos))); + if (failure >= 0) { + // , but failed to detach leash from %s[if more than 4 items, display in abbreviated form] to %s + send.append(Component.translatable(REMOVE_BLOCK_LEASHES_SUC_FAIL, failureEntitiesComponent, Command.getSLPName(pos))); + } //. + else send.append("."); + } else { + // Failed to detach leash from %s to %s [Leashed: [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(REMOVE_BLOCK_LEASHES_FAIL, failureEntitiesComponent, Command.getSLPName(pos))); + } + source.sendSuccess(() -> send, true); + return 0; } + /** + * The constant REMOVE_ALL_LEASHES. + */ + public static final String REMOVE_ALL_LEASHES = SLP_LEASH_MESSAGE_ + "remove_all_leashes"; private static int removeAllLeashes(CommandContext context) throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); + Collection targets = EntityArgument.getEntities(context, "targets"); CommandSourceStack source = context.getSource(); - + int success = -1; + Entity[] sucEntities = new Entity[MAX_SHOW_NUMBER]; for (Entity target : targets) { LeashDataInnerAPI.LeashOperations.detachAll(target); - source.sendSuccess(() -> Component.literal("Removed all leashes from " + target.getName().getString()), false); - } - - return targets.size(); - } - - private static int transferLeash(CommandContext context) throws CommandSyntaxException { - return transferLeash(context, ""); - } - private static int transferLeash(CommandContext context, String reserved) - throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); - Entity from = EntityArgument.getEntity(context, "from"); - Entity to = EntityArgument.getEntity(context, "to"); - CommandSourceStack source = context.getSource(); - int successCount = 0; - - for (Entity target : targets) { - boolean success = reserved.isEmpty() ? - LeashDataInnerAPI.TransferOperations.transfer(target, from, to) : - LeashDataInnerAPI.TransferOperations.transfer(target, from, to, reserved); - - if (success) { - successCount++; - source.sendSuccess(() -> Component.literal("Transferred leash from " + from.getName().getString() + - " to " + to.getName().getString() + " for " + target.getName().getString()), false); - } else { - source.sendFailure(Component.literal("Failed to transfer leash for " + target.getName().getString())); + if (++success <= MAX_SHOW_NUMBER - 1) { + sucEntities[success] = target; } } - - return successCount; + MutableComponent successEntitiesComponent = Component.empty(), send = Component.empty(); + generateEntityComponent(sucEntities, success, successEntitiesComponent); + // Successfully detached all leash from %s[if more than 4 items, display in abbreviated form] + send.append(Component.translatable(REMOVE_ALL_LEASHES, successEntitiesComponent)); + source.sendSuccess(() -> send, true); + return 0; } - private static int applyForces(CommandContext context) throws CommandSyntaxException { - Collection targets = EntityArgument.getEntities(context, "target"); - CommandSourceStack source = context.getSource(); + private static int transferLeash(CommandContext context, Entity from, Entity to) throws CommandSyntaxException { + return transferLeash(context, from, to,null); + } + /** + * The constant TRANSFER_LEASH_. + */ + public static final String TRANSFER_LEASH_ = SLP_LEASH_MESSAGE_ + "transfer_leash."; + /** + * The constant TRANSFER_LEASH_FAIL. + */ + public static final String TRANSFER_LEASH_FAIL = TRANSFER_LEASH_ + "fail"; + /** + * The constant TRANSFER_LEASH_SUC. + */ + public static final String TRANSFER_LEASH_SUC = TRANSFER_LEASH_ + "suc"; + /** + * The constant TRANSFER_LEASH_SUC_FAIL. + */ + public static final String TRANSFER_LEASH_SUC_FAIL = TRANSFER_LEASH_ + "suc_fail"; + private static int transferLeash(CommandContext context, Entity from, Entity to, @Nullable String reserved) + throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + CommandSourceStack source = context.getSource(); + int success = -1, failure = -1; + Entity[] sucEntities = new Entity[MAX_SHOW_NUMBER], failEntities = new Entity[MAX_SHOW_NUMBER]; + for (Entity target : targets) { + boolean isSuccessful = LeashDataInnerAPI.TransferOperations.transfer(target, from, to, reserved); + if (isSuccessful) { + if (++success <= MAX_SHOW_NUMBER - 1) { + sucEntities[success] = target; + } + } else { + failure++; + if (++failure <= MAX_SHOW_NUMBER - 1) { + failEntities[failure] = target; + } + } + } + MutableComponent failureEntitiesComponent = Component.empty(), successEntitiesComponent = Component.empty(); + generateEntityComponent(sucEntities, success, successEntitiesComponent); + generateEntityComponent(failEntities, failure, failureEntitiesComponent); + MutableComponent send = Component.empty(); + if (success >= 0) { + // Successfully transferred leash from %s[BlockPos/Entity] to %s[BlockPos/Entity] [Leashed ((>1)Entities/Entity): [if more than 4 items, display in abbreviated form]] + send.append(Component.translatable(TRANSFER_LEASH_SUC, Command.getSLPName(from), Command.getSLPName(to), successEntitiesComponent)); + if (failure >= 0) { + // , but failed to transfer leash from %s to %s [Leashed ((>1)Entities/Entity): [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(TRANSFER_LEASH_SUC_FAIL, Command.getSLPName(from), Command.getSLPName(to), failureEntitiesComponent)); + } //. + else send.append("."); + } else { + // Failed to transfer leash from %s to %s [Leashed ((>1)Entities/Entity): [if more than 4 items, display in abbreviated form]]. + send.append(Component.translatable(TRANSFER_LEASH_FAIL, Command.getSLPName(from), Command.getSLPName(to), failureEntitiesComponent)); + } + source.sendSuccess(() -> send, true); + return 0; + } + + /** + * The constant APPLY_FORCE. + */ + public static final String APPLY_FORCE = SLP_LEASH_MESSAGE_ + "apply_forces"; + private static int applyForces(CommandContext context) throws CommandSyntaxException { + Collection targets = EntityArgument.getEntities(context, "targets"); + CommandSourceStack source = context.getSource(); + int successful = -1; + Entity[] entities = new Entity[MAX_SHOW_NUMBER]; for (Entity target : targets) { LeashDataInnerAPI.PhysicsOperations.applyForces(target); - source.sendSuccess(() -> Component.literal("Applied leash forces to " + target.getName().getString()), false); + if (++successful <= MAX_SHOW_NUMBER - 1) { + entities[successful] = target; + } } + MutableComponent entitiesComponent = Component.empty(); + generateEntityComponent(entities, successful, entitiesComponent); + // Successfully applied force on %s[if more than 4 items, display in abbreviated form] + source.sendSuccess(() -> Component.translatable(APPLY_FORCE, entitiesComponent), true); + return 0; + } - return targets.size(); + private static void generateEntityComponent(Entity @NotNull [] entities, int successful, MutableComponent entitiesComponent) { + for (int i = 0; i < entities.length; i++) { + entitiesComponent.append(entities[i].getName()); + if (i < entities.length - 1) { + entitiesComponent.append(", "); + } + } + if (successful > MAX_SHOW_NUMBER - 1) { + entitiesComponent.append(Component.translatable(ABBREVIATION)); + } } } \ No newline at end of file diff --git a/src/main/java/top/r3944realms/superleadrope/content/command/LeashStateCommand.java b/src/main/java/top/r3944realms/superleadrope/content/command/LeashStateCommand.java index c0c900d..1c14f2a 100644 --- a/src/main/java/top/r3944realms/superleadrope/content/command/LeashStateCommand.java +++ b/src/main/java/top/r3944realms/superleadrope/content/command/LeashStateCommand.java @@ -27,7 +27,7 @@ public class LeashStateCommand { * * @param dispatcher the dispatcher */ -// 获取State + // 获取State // 设置State // Holder> // 设置对应目标的 拴绳偏移 diff --git a/src/main/java/top/r3944realms/superleadrope/util/capability/LeashDataInnerAPI.java b/src/main/java/top/r3944realms/superleadrope/util/capability/LeashDataInnerAPI.java index 99ca692..7b07bcb 100644 --- a/src/main/java/top/r3944realms/superleadrope/util/capability/LeashDataInnerAPI.java +++ b/src/main/java/top/r3944realms/superleadrope/util/capability/LeashDataInnerAPI.java @@ -20,6 +20,7 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import top.r3944realms.superleadrope.api.SLPCapability; import top.r3944realms.superleadrope.api.type.capabilty.ILeashData; import top.r3944realms.superleadrope.api.type.capabilty.LeashInfo; @@ -83,7 +84,7 @@ public final class LeashDataInnerAPI { * @param maxDistance the max distance * @return the boolean */ - public static boolean attach(Entity entity, Entity holder, double maxDistance) { + public static boolean attach(Entity entity, Entity holder, @Nullable Double maxDistance) { return getLeashData(entity).map(data -> data.addLeash(holder, maxDistance)).orElse(false); } @@ -93,12 +94,12 @@ public final class LeashDataInnerAPI { * @param entity the entity * @param holder the holder * @param maxDistance the max distance - * @param elasticDistance the elastic distance + * @param elasticdDistanceScale the elastic distance * @param maxKeepTicks the max keep ticks * @return the boolean */ - public static boolean attach(Entity entity, Entity holder, double maxDistance, double elasticDistance, int maxKeepTicks) { - return getLeashData(entity).map(data -> data.addLeash(holder, maxDistance, elasticDistance, maxKeepTicks)).orElse(false); + public static boolean attach(Entity entity, Entity holder, @Nullable Double maxDistance, @Nullable Double elasticdDistanceScale, int maxKeepTicks) { + return getLeashData(entity).map(data -> data.addLeash(holder, maxDistance, elasticdDistanceScale, maxKeepTicks)).orElse(false); } /** @@ -110,7 +111,7 @@ public final class LeashDataInnerAPI { * @param reserved the reserved * @return the boolean */ - public static boolean attach(Entity entity, Entity holder, double maxDistance, String reserved) { + public static boolean attach(Entity entity, Entity holder, @Nullable Double maxDistance, String reserved) { return getLeashData(entity).map(data -> data.addLeash(holder, maxDistance, reserved)).orElse(false); } @@ -120,13 +121,13 @@ public final class LeashDataInnerAPI { * @param entity the entity * @param holder the holder * @param maxDistance the max distance - * @param elasticDistance the elastic distance + * @param elasticDistanceScale the elastic distance * @param maxKeepTicks the max keep ticks * @param reserved the reserved * @return the boolean */ - public static boolean attach(Entity entity, Entity holder, double maxDistance, double elasticDistance, int maxKeepTicks, String reserved) { - return getLeashData(entity).map(data -> data.addLeash(holder, maxDistance, elasticDistance, maxKeepTicks, reserved)).orElse(false); + public static boolean attach(Entity entity, Entity holder, @Nullable Double maxDistance, @Nullable Double elasticDistanceScale, int maxKeepTicks, String reserved) { + return getLeashData(entity).map(data -> data.addLeash(holder, maxDistance, elasticDistanceScale, maxKeepTicks, reserved)).orElse(false); } /** @@ -239,10 +240,26 @@ public final class LeashDataInnerAPI { * @param distance the distance * @return the max distance */ -// ---------------------- 设置最大距离 ---------------------- - public static boolean setMaxDistance(Entity entity, Entity holder, double distance) { + // ---------------------- 设置最大距离 ---------------------- + public static boolean setMaxDistance(Entity entity, Entity holder, @Nullable Double distance) { return getLeashData(entity).map(data -> data.setMaxDistance(holder, distance)).orElse(false); } + /** + * Sets max distance. + * + * @param entity the entity + * @param holder the holder + * @param distance the distance + * @return the max distance + */ + public static boolean setMaxDistance(Entity entity, Entity holder, @Nullable Double distance, @Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.setMaxDistance(holder, distance, reserved) : + data.setMaxDistance(holder, distance)) + .orElse(false); + } /** * Sets max distance. @@ -253,7 +270,7 @@ public final class LeashDataInnerAPI { * @param maxKeepTicks the max keep ticks * @return the max distance */ - public static boolean setMaxDistance(Entity entity, Entity holder, double distance, int maxKeepTicks) { + public static boolean setMaxDistance(Entity entity, Entity holder, @Nullable Double distance, int maxKeepTicks) { return getLeashData(entity).map(data -> data.setMaxDistance(holder, distance, maxKeepTicks)).orElse(false); } @@ -267,8 +284,12 @@ public final class LeashDataInnerAPI { * @param reserved the reserved * @return the max distance */ - public static boolean setMaxDistance(Entity entity, Entity holder, double distance, int maxKeepTicks, String reserved) { - return getLeashData(entity).map(data -> data.setMaxDistance(holder, distance, maxKeepTicks, reserved)).orElse(false); + public static boolean setMaxDistance(Entity entity, Entity holder, @Nullable Double distance, int maxKeepTicks, @Nullable String reserved) { + return getLeashData(entity) + .map(data -> reserved != null ? + data.setMaxDistance(holder, distance, maxKeepTicks, reserved) : + data.setMaxDistance(holder, distance, maxKeepTicks)) + .orElse(false); } /** @@ -279,7 +300,7 @@ public final class LeashDataInnerAPI { * @param distance the distance * @return the max distance */ - public static boolean setMaxDistance(Entity entity, UUID holderUUID, double distance) { + public static boolean setMaxDistance(Entity entity, UUID holderUUID, @Nullable Double distance) { return getLeashData(entity).map(data -> data.setMaxDistance(holderUUID, distance)).orElse(false); } @@ -292,7 +313,7 @@ public final class LeashDataInnerAPI { * @param maxKeepTicks the max keep ticks * @return the max distance */ - public static boolean setMaxDistance(Entity entity, UUID holderUUID, double distance, int maxKeepTicks) { + public static boolean setMaxDistance(Entity entity, UUID holderUUID, @Nullable Double distance, int maxKeepTicks) { return getLeashData(entity).map(data -> data.setMaxDistance(holderUUID, distance, maxKeepTicks)).orElse(false); } @@ -306,8 +327,14 @@ public final class LeashDataInnerAPI { * @param reserved the reserved * @return the max distance */ - public static boolean setMaxDistance(Entity entity, UUID holderUUID, double distance, int maxKeepTicks, String reserved) { - return getLeashData(entity).map(data -> data.setMaxDistance(holderUUID, distance, maxKeepTicks, reserved)).orElse(false); + public static boolean setMaxDistance(Entity entity, UUID holderUUID, @Nullable Double distance, int maxKeepTicks,@Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.setMaxDistance(holderUUID, distance, maxKeepTicks, reserved) : + data.setMaxDistance(holderUUID, distance, maxKeepTicks) + ) + .orElse(false); } /** @@ -318,7 +345,7 @@ public final class LeashDataInnerAPI { * @param distance the distance * @return the max distance */ - public static boolean setMaxDistance(Entity entity, BlockPos knotPos, double distance) { + public static boolean setMaxDistance(Entity entity, BlockPos knotPos, @Nullable Double distance) { return getLeashData(entity).map(data -> data.setMaxDistance(knotPos, distance)).orElse(false); } @@ -331,7 +358,7 @@ public final class LeashDataInnerAPI { * @param maxKeepTicks the max keep ticks * @return the max distance */ - public static boolean setMaxDistance(Entity entity, BlockPos knotPos, double distance, int maxKeepTicks) { + public static boolean setMaxDistance(Entity entity, BlockPos knotPos, @Nullable Double distance, int maxKeepTicks) { return getLeashData(entity).map(data -> data.setMaxDistance(knotPos, distance, maxKeepTicks)).orElse(false); } @@ -345,126 +372,150 @@ public final class LeashDataInnerAPI { * @param reserved the reserved * @return the max distance */ - public static boolean setMaxDistance(Entity entity, BlockPos knotPos, double distance, int maxKeepTicks, String reserved) { - return getLeashData(entity).map(data -> data.setMaxDistance(knotPos, distance, maxKeepTicks, reserved)).orElse(false); + public static boolean setMaxDistance(Entity entity, BlockPos knotPos, @Nullable Double distance, int maxKeepTicks,@Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.setMaxDistance(knotPos, distance, maxKeepTicks, reserved) : + data.setMaxDistance(knotPos, distance, maxKeepTicks) + ) + .orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param holder the holder - * @param distance the distance - * @return the elastic distance + * @param scale the scale + * @return the elastic scale */ // ---------------------- 设置弹性距离 ---------------------- - public static boolean setElasticDistance(Entity entity, Entity holder, double distance) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(holder, distance)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, Entity holder, @Nullable Double scale) { + return getLeashData(entity).map(data -> data.setElasticDistanceScale(holder, scale)).orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param holder the holder - * @param distance the distance + * @param scale the scale * @param maxKeepTicks the max keep ticks - * @return the elastic distance + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, Entity holder, double distance, int maxKeepTicks) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(holder, distance, maxKeepTicks)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, Entity holder, @Nullable Double scale, int maxKeepTicks) { + return getLeashData(entity).map(data -> data.setElasticDistanceScale(holder, scale, maxKeepTicks)).orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param holder the holder - * @param distance the distance + * @param scale the scale * @param maxKeepTicks the max keep ticks * @param reserved the reserved - * @return the elastic distance + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, Entity holder, double distance, int maxKeepTicks, String reserved) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(holder, distance, maxKeepTicks, reserved)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, Entity holder, @Nullable Double scale, int maxKeepTicks, @Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.setElasticDistanceScale(holder, scale, maxKeepTicks, reserved) : + data.setElasticDistanceScale(holder, scale, maxKeepTicks) + ) + .orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param holderUUID the holder uuid - * @param distance the distance - * @return the elastic distance + * @param scale the scale + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, UUID holderUUID, double distance) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(holderUUID, distance)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, UUID holderUUID, @Nullable Double scale) { + return getLeashData(entity).map(data -> data.setElasticDistanceScale(holderUUID, scale)).orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param holderUUID the holder uuid - * @param distance the distance + * @param scale the scale * @param maxKeepTicks the max keep ticks - * @return the elastic distance + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, UUID holderUUID, double distance, int maxKeepTicks) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(holderUUID, distance, maxKeepTicks)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, UUID holderUUID, @Nullable Double scale, int maxKeepTicks) { + return getLeashData(entity).map(data -> data.setElasticDistanceScale(holderUUID, scale, maxKeepTicks)).orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param holderUUID the holder uuid - * @param distance the distance + * @param scale the scale * @param maxKeepTicks the max keep ticks * @param reserved the reserved - * @return the elastic distance + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, UUID holderUUID, double distance, int maxKeepTicks, String reserved) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(holderUUID, distance, maxKeepTicks, reserved)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, UUID holderUUID, @Nullable Double scale, int maxKeepTicks, @Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.setElasticDistanceScale(holderUUID, scale, maxKeepTicks, reserved) : + data.setElasticDistanceScale(holderUUID, scale, maxKeepTicks) + ) + .orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param knotPos the knot pos - * @param distance the distance - * @return the elastic distance + * @param scale the scale + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, BlockPos knotPos, double distance) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(knotPos, distance)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, BlockPos knotPos, @Nullable Double scale) { + return getLeashData(entity).map(data -> data.setElasticDistanceScale(knotPos, scale)).orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param knotPos the knot pos - * @param distance the distance + * @param scale the scale * @param maxKeepTicks the max keep ticks - * @return the elastic distance + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, BlockPos knotPos, double distance, int maxKeepTicks) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(knotPos, distance, maxKeepTicks)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, BlockPos knotPos, @Nullable Double scale, int maxKeepTicks) { + return getLeashData(entity).map(data -> data.setElasticDistanceScale(knotPos, scale, maxKeepTicks)).orElse(false); } /** - * Sets elastic distance. + * Sets elastic scale. * * @param entity the entity * @param knotPos the knot pos - * @param distance the distance + * @param scale the scale * @param maxKeepTicks the max keep ticks * @param reserved the reserved - * @return the elastic distance + * @return the elastic scale */ - public static boolean setElasticDistance(Entity entity, BlockPos knotPos, double distance, int maxKeepTicks, String reserved) { - return getLeashData(entity).map(data -> data.setElasticDistanceScale(knotPos, distance, maxKeepTicks, reserved)).orElse(false); + public static boolean setElasticDistanceScale(Entity entity, BlockPos knotPos, @Nullable Double scale, int maxKeepTicks, @Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.setElasticDistanceScale(knotPos, scale, maxKeepTicks, reserved) : + data.setElasticDistanceScale(knotPos, scale, maxKeepTicks) + ) + .orElse(false); } } @@ -515,8 +566,14 @@ public final class LeashDataInnerAPI { * @param reserved the reserved * @return the boolean */ - public static boolean transfer(Entity entity, Entity holder, Entity newHolder, String reserved) { - return getLeashData(entity).map(data -> data.transferLeash(holder, newHolder, reserved)).orElse(false); + public static boolean transfer(Entity entity, Entity holder, Entity newHolder, @Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.transferLeash(holder, newHolder, reserved) : + data.transferLeash(holder, newHolder) + ) + .orElse(false); } /** @@ -540,8 +597,14 @@ public final class LeashDataInnerAPI { * @param reserved the reserved * @return the boolean */ - public static boolean transfer(Entity entity, UUID holderUUID, Entity newHolder, String reserved) { - return getLeashData(entity).map(data -> data.transferLeash(holderUUID, newHolder, reserved)).orElse(false); + public static boolean transfer(Entity entity, UUID holderUUID, Entity newHolder, @Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.transferLeash(holderUUID, newHolder, reserved) : + data.transferLeash(holderUUID, newHolder) + ) + .orElse(false); } /** @@ -565,8 +628,14 @@ public final class LeashDataInnerAPI { * @param reserved the reserved * @return the boolean */ - public static boolean transfer(Entity entity, BlockPos knotPos, Entity newHolder, String reserved) { - return getLeashData(entity).map(data -> data.transferLeash(knotPos, newHolder, reserved)).orElse(false); + public static boolean transfer(Entity entity, BlockPos knotPos, Entity newHolder, @Nullable String reserved) { + return getLeashData(entity) + .map( + data -> reserved != null ? + data.transferLeash(knotPos, newHolder, reserved) : + data.transferLeash(knotPos, newHolder) + ) + .orElse(false); } }