pre修改:添加对修改值事件的泛型支持&API方法的扩充(支持修改maxKeepTick)

This commit is contained in:
叁玖领域 2025-10-22 12:54:46 +08:00
parent 44bc1ad92a
commit 7da5fc6c8d
8 changed files with 211 additions and 130 deletions

1
.gitignore vendored
View File

@ -48,3 +48,4 @@ build
/RenderDoc_1.40_64/ /RenderDoc_1.40_64/
/.run/ /.run/
/.idea/ /.idea/
/mcmodsrepo/

View File

@ -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. # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3 mod_license=GPLv3
# The mod version. See https://semver.org/ # The mod version. See https://semver.org/
mod_version=0.0.0.6-pre4 mod_version=0.0.0.6-pre5
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # 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. # This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html # See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View File

@ -79,7 +79,7 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
/** /**
* The type Add leash. * The type Add leash.
*/ */
// ADD LEASH // ADD LEASH
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Cancelable @Cancelable
public static class AddLeash extends SuperLeadRopeEvent { public static class AddLeash extends SuperLeadRopeEvent {
@ -165,7 +165,7 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
/** /**
* The type Remove leash. * The type Remove leash.
*/ */
// REMOVE LEASH // REMOVE LEASH
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Cancelable @Cancelable
public static class RemoveLeash extends SuperLeadRopeEvent { public static class RemoveLeash extends SuperLeadRopeEvent {
@ -210,7 +210,7 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
/** /**
* The type Transfer leash. * The type Transfer leash.
*/ */
// TRANSFORM LEASH // TRANSFORM LEASH
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Cancelable @Cancelable
public static class TransferLeash extends SuperLeadRopeEvent { public static class TransferLeash extends SuperLeadRopeEvent {
@ -268,57 +268,44 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
/** /**
* The type Modify value. * The type Modify value.
*/ */
// MODIFY LEASH MAX_LEASH_LENGTH / ELASTIC_DISTANCE_SCALE // MODIFY LEASH MAX_LEASH_LENGTH / ELASTIC_DISTANCE_SCALE
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Cancelable @Cancelable
public static class ModifyValue extends SuperLeadRopeEvent { public static class ModifyValue<T> extends SuperLeadRopeEvent {
@Nullable @Nullable
private final LeashHolder holder; private final LeashHolder holder;
@Nullable @Nullable
private final Double oldValue; private final T oldValue;
@Nullable @Nullable
private Double newValue; private T newValue;
private final Type type; private final Type type;
private final Scope scope; private final Scope scope;
/**
* The enum Type.
*/
public enum Type { public enum Type {
/** MAX_DISTANCE(Double.class),
* Max distance type. ELASTIC_DISTANCE_SCALE(Double.class),
*/ MAX_KEEP_LEASH_TICKS(Integer.class),
MAX_DISTANCE, CUSTOM_DATA(String.class); // 支持更多类型
/**
* Elastic distance scale type.
*/
ELASTIC_DISTANCE_SCALE,
}
/** private final Class<?> valueType;
* The enum Scope.
*/ Type(Class<?> valueType) {
this.valueType = valueType;
}
public Class<?> getValueType() {
return valueType;
}
}
public enum Scope { public enum Scope {
/** INSTANCE,
* Static scope. STATIC
*/
STATIC,
/**
* Instance scope.
*/
INSTANCE
} }
/** // 构造方法 - UUID holder
* Instantiates a new Modify value. public ModifyValue(Entity leashedEntity, UUID holderUUID,
* @Nullable T oldValue, @Nullable T newValue,
* @param leashedEntity the leashed entity Type type) {
* @param holderUUID the holder uuid
* @param oldValue the old value
* @param newValue the new value
* @param type the type
*/
public ModifyValue(Entity leashedEntity, UUID holderUUID, @Nullable Double oldValue, @Nullable Double newValue, Type type) {
super(leashedEntity); super(leashedEntity);
this.holder = new LeashHolder(holderUUID); this.holder = new LeashHolder(holderUUID);
this.oldValue = oldValue; this.oldValue = oldValue;
@ -327,16 +314,10 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
this.scope = Scope.INSTANCE; this.scope = Scope.INSTANCE;
} }
/** // 构造方法 - BlockPos holder
* Instantiates a new Modify value. public ModifyValue(Entity leashedEntity, BlockPos knotBlockpos,
* @Nullable T oldValue, @Nullable T newValue,
* @param leashedEntity the leashed entity Type type) {
* @param knotBlockpos the knot blockpos
* @param oldValue the old value
* @param newValue the new value
* @param type the type
*/
public ModifyValue(Entity leashedEntity, BlockPos knotBlockpos, @Nullable Double oldValue, @Nullable Double newValue, Type type) {
super(leashedEntity); super(leashedEntity);
this.holder = new LeashHolder(knotBlockpos); this.holder = new LeashHolder(knotBlockpos);
this.oldValue = oldValue; this.oldValue = oldValue;
@ -345,15 +326,10 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
this.scope = Scope.INSTANCE; this.scope = Scope.INSTANCE;
} }
/** // 构造方法 - 静态作用域
* Instantiates a new Modify value. public ModifyValue(Entity leashedEntity,
* @Nullable T oldValue, @Nullable T newValue,
* @param leashedEntity the leashed entity Type type) {
* @param oldValue the old value
* @param newValue the new value
* @param type the type
*/
public ModifyValue(Entity leashedEntity, @Nullable Double oldValue, @Nullable Double newValue, Type type) {
super(leashedEntity); super(leashedEntity);
this.holder = null; this.holder = null;
this.oldValue = oldValue; this.oldValue = oldValue;
@ -362,66 +338,49 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
this.scope = Scope.STATIC; this.scope = Scope.STATIC;
} }
/**
* Gets holder. // 类型安全的获取方法
* @SuppressWarnings("unchecked")
* @return the holder public <R> R getOldValueAs(Class<R> clazz) {
*/ if (clazz.isInstance(oldValue)) {
public @Nullable LeashHolder getHolder() { return (R) oldValue;
return holder; }
return null;
} }
/** @SuppressWarnings("unchecked")
* Gets old value. public <R> R getNewValueAs(Class<R> clazz) {
* if (clazz.isInstance(newValue)) {
* @return the old value return (R) newValue;
*/ }
public @Nullable Double getOldValue() { return null;
return oldValue;
} }
/** @SuppressWarnings("unchecked")
* Gets new value. public T getOldValue() {
* return (T) getOldValueAs(type.valueType);
* @return the new value }
*/ @SuppressWarnings("unchecked")
public @Nullable Double getNewValue() { public T getNewValue() {
return newValue; return (T) getNewValueAs(type.valueType);
} }
/**
* Gets type.
*
* @return the type
*/
public Type getType() {
return type;
}
/** public void setNewValue(@Nullable T newValue) {
* Gets scope. if (newValue != null && !type.valueType.isInstance(newValue)) {
* throw new IllegalArgumentException(
* @return the scope "Expected value of type " + type.valueType + ", but got " + newValue.getClass());
*/ }
public Scope getScope() {
return scope;
}
/**
* Sets new value.
*
* @param newValue the new value
*/
public void setNewValue(@Nullable Double newValue) {
markModified(); markModified();
this.newValue = newValue; this.newValue = newValue;
} }
} }
/** /**
* The type Has focus. * The type Has focus.
*/ */
// HAS FOCUS // HAS FOCUS
@SuppressWarnings("unused") @SuppressWarnings("unused")
@Cancelable @Cancelable
public static class hasFocus extends SuperLeadRopeEvent { public static class hasFocus extends SuperLeadRopeEvent {
@ -497,7 +456,7 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
/** /**
* The type Keep not break tick. * The type Keep not break tick.
*/ */
// KEEP NOT BREAK TICK // KEEP NOT BREAK TICK
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static class keepNotBreakTick extends SuperLeadRopeEvent { public static class keepNotBreakTick extends SuperLeadRopeEvent {
private final int remainedTicks; private final int remainedTicks;
@ -558,7 +517,7 @@ public abstract class SuperLeadRopeEvent extends Event implements IModBusEvent {
/** /**
* The type Teleport with holder. * The type Teleport with holder.
*/ */
// TELEPORT // TELEPORT
@Cancelable @Cancelable
@SuppressWarnings("unused") @SuppressWarnings("unused")
public static class teleportWithHolder extends SuperLeadRopeEvent { public static class teleportWithHolder extends SuperLeadRopeEvent {

View File

@ -506,6 +506,58 @@ public interface ILeashData extends INBTSerializable<CompoundTag> {
*/ */
boolean transferLeash(BlockPos knotPos, Entity newHolder, String reserved); boolean transferLeash(BlockPos knotPos, Entity newHolder, String reserved);
/**
* Sets max keep ticks.
*
* @param holder the holder
* @param maxKeepTicks the max keep ticks
* @return the max keep ticks
*/
boolean setMaxKeepTicks(Entity holder, int maxKeepTicks);
/**
* Sets max keep ticks.
*
* @param holderUUID the holder uuid
* @param maxKeepTicks the max keep ticks
* @return the max keep ticks
*/
boolean setMaxKeepTicks(UUID holderUUID, int maxKeepTicks);
/**
* Sets max keep ticks.
*
* @param knotPos the knot pos
* @param maxKeepTicks the max keep ticks
* @return the max keep ticks
*/
boolean setMaxKeepTicks(BlockPos knotPos, int maxKeepTicks);
/**
* Gets max keep ticks.
*
* @param holder the holder
* @return the max keep ticks
*/
int getMaxKeepTicks(Entity holder);
/**
* Gets max keep ticks.
*
* @param holderUUID the holder uuid
* @return the max keep ticks
*/
int getMaxKeepTicks(UUID holderUUID);
/**
* Gets max keep ticks.
*
* @param knotPos the knot pos
* @return the max keep ticks
*/
int getMaxKeepTicks(BlockPos knotPos);
/** /**
* Has leash boolean. * Has leash boolean.
* *

View File

@ -171,7 +171,8 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public void setStaticMaxDistance(@Nullable Double distance) { public void setStaticMaxDistance(@Nullable Double distance) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(distance)) return; if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(distance)) return;
if (MinecraftForge.EVENT_BUS.post(new SuperLeadRopeEvent.ModifyValue(this.entity, getStaticMaxDistance(), distance, SuperLeadRopeEvent.ModifyValue.Type.MAX_DISTANCE))) return; SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, getStaticMaxDistance(), distance, SuperLeadRopeEvent.ModifyValue.Type.MAX_DISTANCE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return;
staticMaxDistance = distance; staticMaxDistance = distance;
} }
@ -212,7 +213,8 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public void setStaticElasticDistanceScale(@Nullable Double scale) { public void setStaticElasticDistanceScale(@Nullable Double scale) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return;
if (MinecraftForge.EVENT_BUS.post(new SuperLeadRopeEvent.ModifyValue(this.entity, getStaticElasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE))) return; SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, getStaticElasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return;
staticElasticDistanceScale = scale; staticElasticDistanceScale = scale;
} }
@ -399,7 +401,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setMaxDistance(UUID holderUUID, @Nullable Double newMaxDistance) { public boolean setMaxDistance(UUID holderUUID, @Nullable Double newMaxDistance) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; 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); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo( return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
@ -422,7 +424,7 @@ public class LeashDataImpl implements ILeashData {
@SuppressWarnings("OptionalGetWithoutIsPresent") @SuppressWarnings("OptionalGetWithoutIsPresent")
public void setMaxDistanceInner(UUID holderUUID, @Nullable Double newMaxDistance) { public void setMaxDistanceInner(UUID holderUUID, @Nullable Double newMaxDistance) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return; if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, holderUUID, leashHolders.get(holderUUID).maxDistance(), newMaxDistance, SuperLeadRopeEvent.ModifyValue.Type.MAX_DISTANCE); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return;
updateLeashInfoInner(leashHolders, holderUUID, old -> new LeashInfo( updateLeashInfoInner(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
@ -440,7 +442,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setMaxDistance(UUID holderUUID, @Nullable Double newMaxDistance, int newMaxKeepLeashTicks) { public boolean setMaxDistance(UUID holderUUID, @Nullable Double newMaxDistance, int newMaxKeepLeashTicks) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; 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); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo( return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
@ -458,7 +460,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setMaxDistance(UUID holderUUID, @Nullable Double newMaxDistance, int maxKeepTicks, String reserved) { public boolean setMaxDistance(UUID holderUUID, @Nullable Double newMaxDistance, int maxKeepTicks, String reserved) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; 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); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo( return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
@ -476,7 +478,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setMaxDistance(BlockPos knotPos, @Nullable Double newMaxDistance) { public boolean setMaxDistance(BlockPos knotPos, @Nullable Double newMaxDistance) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; 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); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo( return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -499,7 +501,7 @@ public class LeashDataImpl implements ILeashData {
@SuppressWarnings("OptionalGetWithoutIsPresent") @SuppressWarnings("OptionalGetWithoutIsPresent")
public void setMaxDistanceInner(BlockPos knotPos, @Nullable Double newMaxDistance) { public void setMaxDistanceInner(BlockPos knotPos, @Nullable Double newMaxDistance) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return; if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, knotPos, leashKnots.get(knotPos).maxDistance(), newMaxDistance, SuperLeadRopeEvent.ModifyValue.Type.MAX_DISTANCE); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return;
updateLeashInfoInner(leashKnots, knotPos, old -> new LeashInfo( updateLeashInfoInner(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -517,7 +519,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setMaxDistance(BlockPos knotPos, @Nullable Double newMaxDistance, int newMaxKeepLeashTicks) { public boolean setMaxDistance(BlockPos knotPos, @Nullable Double newMaxDistance, int newMaxKeepLeashTicks) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; 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); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo( return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -535,7 +537,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setMaxDistance(BlockPos knotPos, @Nullable Double newMaxDistance, int maxKeepTicks, String reserved) { public boolean setMaxDistance(BlockPos knotPos, @Nullable Double newMaxDistance, int maxKeepTicks, String reserved) {
if (!LeashConfigManager.MAX_DISTANCE_CHECK.test(newMaxDistance)) return false; 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); SuperLeadRopeEvent.ModifyValue<Double> 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; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.MAX_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo( return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -561,7 +563,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setElasticDistanceScale(UUID holderUUID, @Nullable Double scale) { public boolean setElasticDistanceScale(UUID holderUUID, @Nullable Double scale) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE); SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo( return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
@ -584,14 +586,15 @@ public class LeashDataImpl implements ILeashData {
@SuppressWarnings("OptionalGetWithoutIsPresent") @SuppressWarnings("OptionalGetWithoutIsPresent")
public void setElasticDistanceScaleInner(UUID holderUUID, @Nullable Double scale) { public void setElasticDistanceScaleInner(UUID holderUUID, @Nullable Double scale) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return;
if (MinecraftForge.EVENT_BUS.post(new SuperLeadRopeEvent.ModifyValue(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE))) return; SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return;
updateLeashInfoInner(leashHolders, holderUUID, old -> new LeashInfo( updateLeashInfoInner(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
old.holderIdOpt().get(), old.holderIdOpt().get(),
old.marks(), old.marks(),
old.reserved(), old.reserved(),
old.maxDistance(), old.maxDistance(),
scale, event.getNewValue(),
old.keepLeashTicks(), old.keepLeashTicks(),
old.maxKeepLeashTicks() old.maxKeepLeashTicks()
)); ));
@ -601,7 +604,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setElasticDistanceScale(BlockPos knotPos, @Nullable Double scale) { public boolean setElasticDistanceScale(BlockPos knotPos, @Nullable Double scale) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE); SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo( return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -624,7 +627,7 @@ public class LeashDataImpl implements ILeashData {
@SuppressWarnings("OptionalGetWithoutIsPresent") @SuppressWarnings("OptionalGetWithoutIsPresent")
public void setElasticDistanceScaleInner(BlockPos knotPos, @Nullable Double scale) { public void setElasticDistanceScaleInner(BlockPos knotPos, @Nullable Double scale) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE); SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return;
updateLeashInfoInner(leashKnots, knotPos, old -> new LeashInfo( updateLeashInfoInner(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -657,7 +660,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setElasticDistanceScale(UUID holderUUID, @Nullable Double scale, int newMaxKeepLeashTicks) { public boolean setElasticDistanceScale(UUID holderUUID, @Nullable Double scale, int newMaxKeepLeashTicks) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE); SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo( return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
@ -675,7 +678,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setElasticDistanceScale(UUID holderUUID, @Nullable Double scale, int maxKeepTicks, String reserved) { public boolean setElasticDistanceScale(UUID holderUUID, @Nullable Double scale, int maxKeepTicks, String reserved) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE); SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, holderUUID, leashHolders.get(holderUUID).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo( return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(), old.holderUUIDOpt().get(),
@ -693,7 +696,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setElasticDistanceScale(BlockPos knotPos, @Nullable Double scale, int newMaxKeepLeashTicks) { public boolean setElasticDistanceScale(BlockPos knotPos, @Nullable Double scale, int newMaxKeepLeashTicks) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE); SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo( return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -711,7 +714,7 @@ public class LeashDataImpl implements ILeashData {
@Override @Override
public boolean setElasticDistanceScale(BlockPos knotPos, @Nullable Double scale, int newMaxKeepLeashTicks, String reserved) { public boolean setElasticDistanceScale(BlockPos knotPos, @Nullable Double scale, int newMaxKeepLeashTicks, String reserved) {
if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false; if (!LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(scale)) return false;
SuperLeadRopeEvent.ModifyValue event = new SuperLeadRopeEvent.ModifyValue(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE); SuperLeadRopeEvent.ModifyValue<Double> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, knotPos, leashKnots.get(knotPos).elasticDistanceScale(), scale, SuperLeadRopeEvent.ModifyValue.Type.ELASTIC_DISTANCE_SCALE);
if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false; if (MinecraftForge.EVENT_BUS.post(event) || event.isModified() && !(LeashConfigManager.ELASTIC_DISTANCE_CHECK.test(event.getNewValue()))) return false;
return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo( return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo(
old.blockPosOpt().get(), old.blockPosOpt().get(),
@ -1184,6 +1187,70 @@ public class LeashDataImpl implements ILeashData {
return true; return true;
} }
@Override
public boolean setMaxKeepTicks(Entity holder, int maxKeepTicks) {
return holder instanceof SuperLeashKnotEntity superLeashKnotEntity ?
setMaxKeepTicks(superLeashKnotEntity.getPos(), maxKeepTicks) :
setMaxKeepTicks(holder.getUUID(), maxKeepTicks);
}
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Override
public boolean setMaxKeepTicks(UUID holderUUID, int maxKeepTicks) {
if (maxKeepTicks < 0) return false;
LeashInfo info = leashHolders.get(holderUUID);
if (info == null) return false;
SuperLeadRopeEvent.ModifyValue<Integer> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, holderUUID, info.keepLeashTicks(), maxKeepTicks, SuperLeadRopeEvent.ModifyValue.Type.MAX_KEEP_LEASH_TICKS);
if (MinecraftForge.EVENT_BUS.post(event) && (event.getNewValue() == null || event.getNewValue() < 0)) return false;
return updateLeashInfo(leashHolders, holderUUID, old -> new LeashInfo(
old.holderUUIDOpt().get(),
old.holderIdOpt().get(),
old.marks(),
old.reserved(),
old.maxDistance(),
old.elasticDistanceScale(),
event.getNewValue(),
event.getNewValue()
));
}
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Override
public boolean setMaxKeepTicks(BlockPos knotPos, int maxKeepTicks) {
if (maxKeepTicks < 0) return false;
LeashInfo info = leashKnots.get(knotPos);
if (info == null) return false;
SuperLeadRopeEvent.ModifyValue<Integer> event = new SuperLeadRopeEvent.ModifyValue<>(this.entity, knotPos, info.keepLeashTicks(), maxKeepTicks, SuperLeadRopeEvent.ModifyValue.Type.MAX_KEEP_LEASH_TICKS);
if (MinecraftForge.EVENT_BUS.post(event) && (event.getNewValue() == null || event.getNewValue() < 0)) return false;
return updateLeashInfo(leashKnots, knotPos, old -> new LeashInfo(
old.holderUUIDOpt().get(),
old.holderIdOpt().get(),
old.marks(),
old.reserved(),
old.maxDistance(),
old.elasticDistanceScale(),
event.getNewValue(),
event.getNewValue()
));
}
@Override
public int getMaxKeepTicks(Entity holder) {
return holder instanceof SuperLeashKnotEntity superLeashKnotEntity ?
getMaxKeepTicks(superLeashKnotEntity.getPos()) :
getMaxKeepTicks(holder.getUUID());
}
@Override
public int getMaxKeepTicks(UUID holderUUID) {
return Optional.ofNullable(leashHolders.get(holderUUID)).map(LeashInfo::maxKeepLeashTicks).orElse(-1);
}
@Override
public int getMaxKeepTicks(BlockPos knotPos) {
return Optional.ofNullable(leashKnots.get(knotPos)).map(LeashInfo::maxKeepLeashTicks).orElse(-1);
}
@Override @Override
public boolean hasLeash() { public boolean hasLeash() {
return !leashKnots.isEmpty() || !leashHolders.isEmpty(); return !leashKnots.isEmpty() || !leashHolders.isEmpty();

View File

@ -517,7 +517,9 @@ public class LeashDataCommand {
Entity holder = EntityArgument.getEntity(context, "holder"); Entity holder = EntityArgument.getEntity(context, "holder");
CommandSourceStack source = context.getSource(); CommandSourceStack source = context.getSource();
int successCount = 0; 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) { for (Entity target : targets) {
boolean success = LeashDataInnerAPI.LeashOperations.detach(target, holder); boolean success = LeashDataInnerAPI.LeashOperations.detach(target, holder);

View File

@ -150,7 +150,7 @@ public class MotionCommand {
LiteralArgumentBuilder<CommandSourceStack> Motion = $$motionRoot.requires(cs -> cs.hasPermission(2)) LiteralArgumentBuilder<CommandSourceStack> Motion = $$motionRoot.requires(cs -> cs.hasPermission(2))
.then(Commands.argument("targets", EntityArgument.entities()) .then(Commands.argument("targets", EntityArgument.entities())
.then(Commands.literal("addApplyEntity") .then(Commands.literal("add")
.then(Commands.argument("vecX", DoubleArgumentType.doubleArg()) .then(Commands.argument("vecX", DoubleArgumentType.doubleArg())
.then(Commands.argument("vecY", DoubleArgumentType.doubleArg()) .then(Commands.argument("vecY", DoubleArgumentType.doubleArg())
.then(Commands.argument("vecZ", DoubleArgumentType.doubleArg()) .then(Commands.argument("vecZ", DoubleArgumentType.doubleArg())
@ -159,7 +159,7 @@ public class MotionCommand {
) )
) )
) )
.then(Commands.literal("setApplyEntity") .then(Commands.literal("set")
.then(Commands.argument("vecX", DoubleArgumentType.doubleArg()) .then(Commands.argument("vecX", DoubleArgumentType.doubleArg())
.then(Commands.argument("vecY", DoubleArgumentType.doubleArg()) .then(Commands.argument("vecY", DoubleArgumentType.doubleArg())
.then(Commands.argument("vecZ", DoubleArgumentType.doubleArg()) .then(Commands.argument("vecZ", DoubleArgumentType.doubleArg())

View File

@ -432,7 +432,7 @@ public enum SLPLangKeyValue {
* The Message leashdata add success. * The Message leashdata add success.
*/ */
MESSAGE_LEASHDATA_ADD_SUCCESS( MESSAGE_LEASHDATA_ADD_SUCCESS(
"command.leashdata.addApplyEntity.success", ModPartEnum.COMMAND, "command.leashdata.add_apply_entity.success", ModPartEnum.COMMAND,
"§bAdded leash successfully. §a%s §7→ §e%s", "§bAdded leash successfully. §a%s §7→ §e%s",
"§b添加拴绳成功. §a%s §7→ §e%s", "§b添加拴绳成功. §a%s §7→ §e%s",
"§b添加拴繩成功. §a%s §7→ §e%s", "§b添加拴繩成功. §a%s §7→ §e%s",