优化DamageModifier
This commit is contained in:
parent
61fe4148d0
commit
d3d843312c
2 changed files with 41 additions and 25 deletions
|
@ -11,13 +11,15 @@ import java.util.function.Function;
|
||||||
|
|
||||||
public class DamageModifier {
|
public class DamageModifier {
|
||||||
|
|
||||||
private final List<DamageModify> list = new ArrayList<>();
|
private final List<DamageModify> immuneList = new ArrayList<>();
|
||||||
|
private final List<DamageModify> reduceList = new ArrayList<>();
|
||||||
|
private final List<DamageModify> multiplyList = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 免疫所有伤害
|
* 免疫所有伤害
|
||||||
*/
|
*/
|
||||||
public DamageModifier immuneTo() {
|
public DamageModifier immuneTo() {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, (TagKey<DamageType>) null));
|
immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ public class DamageModifier {
|
||||||
* @param sourceTagKey 伤害类型
|
* @param sourceTagKey 伤害类型
|
||||||
*/
|
*/
|
||||||
public DamageModifier immuneTo(TagKey<DamageType> sourceTagKey) {
|
public DamageModifier immuneTo(TagKey<DamageType> sourceTagKey) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, sourceTagKey));
|
immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0, sourceTagKey));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +39,7 @@ public class DamageModifier {
|
||||||
* @param sourceKey 伤害类型
|
* @param sourceKey 伤害类型
|
||||||
*/
|
*/
|
||||||
public DamageModifier immuneTo(ResourceKey<DamageType> sourceKey) {
|
public DamageModifier immuneTo(ResourceKey<DamageType> sourceKey) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, sourceKey));
|
immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0, sourceKey));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +49,7 @@ public class DamageModifier {
|
||||||
* @param condition 伤害来源判定条件
|
* @param condition 伤害来源判定条件
|
||||||
*/
|
*/
|
||||||
public DamageModifier immuneTo(Function<DamageSource, Boolean> condition) {
|
public DamageModifier immuneTo(Function<DamageSource, Boolean> condition) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.IMMUNITY, 0, condition));
|
immuneList.add(new DamageModify(DamageModify.ModifyType.IMMUNITY, 0, condition));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +59,7 @@ public class DamageModifier {
|
||||||
* @param value 要减少的数值
|
* @param value 要减少的数值
|
||||||
*/
|
*/
|
||||||
public DamageModifier reduce(float value) {
|
public DamageModifier reduce(float value) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, (TagKey<DamageType>) null));
|
reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +70,7 @@ public class DamageModifier {
|
||||||
* @param sourceTagKey 伤害类型
|
* @param sourceTagKey 伤害类型
|
||||||
*/
|
*/
|
||||||
public DamageModifier reduce(float value, TagKey<DamageType> sourceTagKey) {
|
public DamageModifier reduce(float value, TagKey<DamageType> sourceTagKey) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, sourceTagKey));
|
reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value, sourceTagKey));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +81,7 @@ public class DamageModifier {
|
||||||
* @param sourceKey 伤害类型
|
* @param sourceKey 伤害类型
|
||||||
*/
|
*/
|
||||||
public DamageModifier reduce(float value, ResourceKey<DamageType> sourceKey) {
|
public DamageModifier reduce(float value, ResourceKey<DamageType> sourceKey) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, sourceKey));
|
reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value, sourceKey));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,53 +92,55 @@ public class DamageModifier {
|
||||||
* @param condition 伤害来源判定条件
|
* @param condition 伤害来源判定条件
|
||||||
*/
|
*/
|
||||||
public DamageModifier reduce(float value, Function<DamageSource, Boolean> condition) {
|
public DamageModifier reduce(float value, Function<DamageSource, Boolean> condition) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.REDUCE, value, condition));
|
reduceList.add(new DamageModify(DamageModify.ModifyType.REDUCE, value, condition));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将所有类型的伤害乘以指定数值
|
* 将所有类型的伤害值乘以指定数值
|
||||||
*
|
*
|
||||||
* @param value 要乘以的数值
|
* @param value 要乘以的数值
|
||||||
*/
|
*/
|
||||||
public DamageModifier multiply(float value) {
|
public DamageModifier multiply(float value) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, (TagKey<DamageType>) null));
|
multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将指定类型的伤害乘以指定数值
|
* 将指定类型的伤害值乘以指定数值
|
||||||
*
|
*
|
||||||
* @param value 要乘以的数值
|
* @param value 要乘以的数值
|
||||||
* @param sourceTagKey 伤害类型
|
* @param sourceTagKey 伤害类型
|
||||||
*/
|
*/
|
||||||
public DamageModifier multiply(float value, TagKey<DamageType> sourceTagKey) {
|
public DamageModifier multiply(float value, TagKey<DamageType> sourceTagKey) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, sourceTagKey));
|
multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value, sourceTagKey));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将指定类型的伤害乘以指定数值
|
* 将指定类型的伤害值乘以指定数值
|
||||||
*
|
*
|
||||||
* @param value 要乘以的数值
|
* @param value 要乘以的数值
|
||||||
* @param sourceKey 伤害类型
|
* @param sourceKey 伤害类型
|
||||||
*/
|
*/
|
||||||
public DamageModifier multiply(float value, ResourceKey<DamageType> sourceKey) {
|
public DamageModifier multiply(float value, ResourceKey<DamageType> sourceKey) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, sourceKey));
|
multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value, sourceKey));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将指定类型的伤害乘以指定数值
|
* 将指定类型的伤害值乘以指定数值
|
||||||
*
|
*
|
||||||
* @param value 要乘以的数值
|
* @param value 要乘以的数值
|
||||||
* @param condition 伤害来源判定条件
|
* @param condition 伤害来源判定条件
|
||||||
*/
|
*/
|
||||||
public DamageModifier multiply(float value, Function<DamageSource, Boolean> condition) {
|
public DamageModifier multiply(float value, Function<DamageSource, Boolean> condition) {
|
||||||
list.add(new DamageModify(DamageModify.ReduceType.MULTIPLY, value, condition));
|
multiplyList.add(new DamageModify(DamageModify.ModifyType.MULTIPLY, value, condition));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final List<DamageModify> combinedList = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算减伤后的伤害值
|
* 计算减伤后的伤害值
|
||||||
*
|
*
|
||||||
|
@ -145,7 +149,14 @@ public class DamageModifier {
|
||||||
* @return 减伤后的伤害值
|
* @return 减伤后的伤害值
|
||||||
*/
|
*/
|
||||||
public float compute(DamageSource source, float damage) {
|
public float compute(DamageSource source, float damage) {
|
||||||
for (DamageModify damageModify : list) {
|
if (combinedList.isEmpty()) {
|
||||||
|
// 计算优先级 免疫 > 固定减伤 > 乘
|
||||||
|
combinedList.addAll(immuneList);
|
||||||
|
combinedList.addAll(reduceList);
|
||||||
|
combinedList.addAll(multiplyList);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (DamageModify damageModify : combinedList) {
|
||||||
if (damageModify.match(source)) {
|
if (damageModify.match(source)) {
|
||||||
damage = damageModify.compute(damage);
|
damage = damageModify.compute(damage);
|
||||||
|
|
||||||
|
|
|
@ -8,32 +8,38 @@ import net.minecraft.world.damagesource.DamageType;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class DamageModify {
|
public class DamageModify {
|
||||||
public enum ReduceType {
|
public enum ModifyType {
|
||||||
IMMUNITY, // 完全免疫
|
IMMUNITY, // 完全免疫
|
||||||
REDUCE, // 固定数值减伤
|
REDUCE, // 固定数值减伤
|
||||||
MULTIPLY, // 乘以指定倍数
|
MULTIPLY, // 乘以指定倍数
|
||||||
}
|
}
|
||||||
|
|
||||||
private final float value;
|
private final float value;
|
||||||
private final ReduceType type;
|
private final ModifyType type;
|
||||||
|
|
||||||
private TagKey<DamageType> sourceTagKey = null;
|
private TagKey<DamageType> sourceTagKey = null;
|
||||||
private ResourceKey<DamageType> sourceKey = null;
|
private ResourceKey<DamageType> sourceKey = null;
|
||||||
private Function<DamageSource, Boolean> condition = null;
|
private Function<DamageSource, Boolean> condition = null;
|
||||||
|
|
||||||
public DamageModify(ReduceType type, float value, TagKey<DamageType> sourceTagKey) {
|
public DamageModify(ModifyType type, float value) {
|
||||||
|
this.type = type;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public DamageModify(ModifyType type, float value, TagKey<DamageType> sourceTagKey) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.sourceTagKey = sourceTagKey;
|
this.sourceTagKey = sourceTagKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DamageModify(ReduceType type, float value, ResourceKey<DamageType> sourceKey) {
|
public DamageModify(ModifyType type, float value, ResourceKey<DamageType> sourceKey) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.sourceKey = sourceKey;
|
this.sourceKey = sourceKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DamageModify(ReduceType type, float value, Function<DamageSource, Boolean> condition) {
|
public DamageModify(ModifyType type, float value, Function<DamageSource, Boolean> condition) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.condition = condition;
|
this.condition = condition;
|
||||||
|
@ -52,9 +58,8 @@ public class DamageModify {
|
||||||
return source.is(sourceTagKey);
|
return source.is(sourceTagKey);
|
||||||
} else if (sourceKey != null) {
|
} else if (sourceKey != null) {
|
||||||
return source.is(sourceKey);
|
return source.is(sourceKey);
|
||||||
} else {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue