Skip to content

Commit

Permalink
Properly handle different number data types in PetConfigEntry Fixes #132
Browse files Browse the repository at this point in the history


Any number from yml can be an int or double, safely convert to the type we need by using the Number object.

Might be able to handle this better? But for now this is the solution
  • Loading branch information
Arnuh committed Aug 22, 2022
1 parent 9d12cb7 commit 9263e52
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

public class Option<T>{

private FileConfiguration config;
private String path;
private final FileConfiguration config;
private final String path;
private T defaultValue;
private String[] comments;
private final String[] comments;

public Option(FileConfiguration configuration, String path, String... comments){
this.config = configuration;
Expand All @@ -38,7 +38,7 @@ public Option(FileConfiguration configuration, String path, String... comments){
for(String comment : comments){
Collections.addAll(commentsList, WordUtils.wrap(comment, 30, "\n", false).split("\n"));
}
this.comments = commentsList.toArray(new String[commentsList.size()]);
this.comments = commentsList.toArray(new String[0]);
}

public Option(FileConfiguration configuration, String path, T defaultValue, String... comments){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@
*/
public class PetConfigEntry<T>{

// private final Class<T> valueType;
private final boolean isNumber;
public final String configKey;
public final Function<IPetType, T> defaultValue;
public final String[] comments;

public PetConfigEntry(String configKey, T defaultValue, String... comments){
this.configKey = configKey;
this.defaultValue = petType->defaultValue;
this.comments = comments;

public PetConfigEntry(Class<T> valueType, String configKey, T defaultValue, String... comments){
this(valueType, configKey, petType->defaultValue, comments);
}

public PetConfigEntry(String configKey, Function<IPetType, T> defaultValue, String... comments){
public PetConfigEntry(Class<T> valueType, String configKey, Function<IPetType, T> defaultValue, String... comments){
// this.valueType = valueType;
this.isNumber = Number.class.isAssignableFrom(valueType);
this.configKey = configKey;
this.defaultValue = defaultValue;
this.comments = comments;
Expand All @@ -54,7 +57,14 @@ public String[] getComments(){
return this.comments;
}

public T get(IPetType type){
return type.getConfigValue(getConfigKey(), getDefaultValue(type));
public T get(IPetType petType){
return petType.getConfigValue(getConfigKey(), getDefaultValue(petType));
}

public Number getNumber(IPetType petType){
if(!isNumber){
throw new IllegalStateException();
}
return (Number) petType.getRawConfigValue(getConfigKey(), getDefaultValue(petType));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ public void set(String path, Object value){
public void set(String path, Object value, String... comments){
for(String comment : comments){
if(!this.config.contains(path)){
this.config.set(manager.getPluginName() + "_COMMENT_" + this.comments, " " + comment);
this.comments++;
this.config.set(manager.getPluginName() + "_COMMENT_" + this.comments++, " " + comment);
}
}
this.config.set(path, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ default boolean isValidData(PetData<?> data){

YAMLConfig getConfig();

Object getRawConfigValue(String variable, Object defaultValue);

<T> T getConfigValue(String variable, T defaultValue);

ConfigurationSection getPetDataSection(PetData<?> data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,15 @@ public YAMLConfig getConfig(){
return EchoPet.getConfig();
}

@Override
public Object getRawConfigValue(String variable, Object defaultValue){
return getConfig().get("pets." + getConfigKeyName() + "." + variable, defaultValue);
}

@Override
@SuppressWarnings("unchecked")
public <T> T getConfigValue(String variable, T defaultValue){
return (T) getConfig().get("pets." + getConfigKeyName() + "." + variable, defaultValue);
return (T) getRawConfigValue(variable, defaultValue);
}

@Override
Expand Down
26 changes: 13 additions & 13 deletions modules/API/src/com/dsh105/echopet/compat/api/entity/pet/IPet.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,42 @@

public interface IPet{

PetConfigEntry<Boolean> RIDING_IGNORE_FALL_DAMAGE = new PetConfigEntry<>("riding.ignoreFallDamage", true);
PetConfigEntry<Boolean> RIDING_IGNORE_FALL_DAMAGE = new PetConfigEntry<>(Boolean.class, "riding.ignoreFallDamage", true);

PetConfigEntry<Double> RIDING_WALK_SPEED = new PetConfigEntry<>("riding.walkSpeed", petType->{
return 0.37D;
PetConfigEntry<Float> RIDING_WALK_SPEED = new PetConfigEntry<Float>(Float.class, "riding.walkSpeed", petType->{
return 0.37f;
/*Double speed = EchoPet.getPlugin().getSpawnUtil().getAttribute(petType, "generic.movement_speed");
if(speed == null){
return 0.37D;
}
return speed;*/
});
PetConfigEntry<Double> RIDING_FLY_SPEED = new PetConfigEntry<>("riding.flySpeed", petType->{
return 0.5D;
PetConfigEntry<Float> RIDING_FLY_SPEED = new PetConfigEntry<Float>(Float.class, "riding.flySpeed", petType->{
return 0.5f;
/*Double speed = EchoPet.getPlugin().getSpawnUtil().getAttribute(petType, "generic.flying_speed");
if(speed == null){
return 0.5D;
}
return speed;*/
});
PetConfigEntry<Double> RIDING_JUMP_HEIGHT = new PetConfigEntry<>("riding.jumpHeight", petType->{
PetConfigEntry<Double> RIDING_JUMP_HEIGHT = new PetConfigEntry<Double>(Double.class, "riding.jumpHeight", petType->{
return 0.6d;
});
PetConfigEntry<Boolean> RIDING_FLY = new PetConfigEntry<>("riding.canFly", petType->{
PetConfigEntry<Boolean> RIDING_FLY = new PetConfigEntry<Boolean>(Boolean.class, "riding.canFly", petType->{
// A better way has to exist
return petType == PetType.ALLAY || petType == PetType.BAT || petType.equals(PetType.BEE) || petType == PetType.BLAZE || petType == PetType.GHAST || petType == PetType.SQUID || petType == PetType.WITHER || petType == PetType.VEX || petType == PetType.PHANTOM;
});
// PetConfigEntry<Boolean> RIDING_HOVER = new PetConfigEntry<>("riding.hover", false);

// Goals might get moved to a higher interface. Some pathing will require "PathfinderMob"
PetConfigEntry<Double> GOAL_WALK_SPEED = new PetConfigEntry<>("goal.walkSpeed", petType->{
PetConfigEntry<Double> GOAL_WALK_SPEED = new PetConfigEntry<Double>(Double.class, "goal.walkSpeed", petType->{
Double speed = EchoPet.getPlugin().getSpawnUtil().getAttribute(petType, "generic.movement_speed");
if(speed == null){
return 0.37D;
}
return speed;
});
PetConfigEntry<Double> GOAL_FLY_SPEED = new PetConfigEntry<>("goal.flySpeed", petType->{
PetConfigEntry<Double> GOAL_FLY_SPEED = new PetConfigEntry<Double>(Double.class, "goal.flySpeed", petType->{
Double speed = EchoPet.getPlugin().getSpawnUtil().getAttribute(petType, "generic.flying_speed");
if(speed == null){
return 0.5D;
Expand All @@ -77,10 +77,10 @@ public interface IPet{
});
/*PetConfigEntry<Double> GOAL_JUMP_HEIGHT = new PetConfigEntry<>("goal.jumpHeight", petType->{
return 0.6d;
});*/ PetConfigEntry<Integer> GOAL_FOLLOW_START_DISTANCE = new PetConfigEntry<>("goal.follow.startDistance", 6);
PetConfigEntry<Integer> GOAL_FOLLOW_STOP_DISTANCE = new PetConfigEntry<>("goal.follow.stopDistance", 2);
PetConfigEntry<Integer> GOAL_FOLLOW_TELEPORT_DISTANCE = new PetConfigEntry<>("goal.follow.teleportDistance", 10);
PetConfigEntry<Double> GOAL_FOLLOW_SPEED_MODIFIER = new PetConfigEntry<>("goal.follow.speedModifier", petType->{
});*/ PetConfigEntry<Integer> GOAL_FOLLOW_START_DISTANCE = new PetConfigEntry<>(Integer.class, "goal.follow.startDistance", 6);
PetConfigEntry<Integer> GOAL_FOLLOW_STOP_DISTANCE = new PetConfigEntry<>(Integer.class, "goal.follow.stopDistance", 2);
PetConfigEntry<Integer> GOAL_FOLLOW_TELEPORT_DISTANCE = new PetConfigEntry<>(Integer.class, "goal.follow.teleportDistance", 10);
PetConfigEntry<Double> GOAL_FOLLOW_SPEED_MODIFIER = new PetConfigEntry<Double>(Double.class, "goal.follow.speedModifier", petType->{
return 1.5d;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public interface IBatPet extends ILivingPet{

PetConfigEntry<Double> GOAL_WANDER_FLY_RANGE = new PetConfigEntry<>("goal.wander.flyRange", 7d);
PetConfigEntry<Double> GOAL_WANDER_FLY_RANGE = new PetConfigEntry<>(Double.class, "goal.wander.flyRange", 7d);

void setWandering(boolean flag);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public interface IFrogPet extends ILivingPet{

PetConfigEntry<Boolean> BRAIN_ENABLED = new PetConfigEntry<>("brain.enabled", false);
PetConfigEntry<Boolean> BRAIN_ENABLED = new PetConfigEntry<>(Boolean.class, "brain.enabled", false);

enum Variant{
Temperate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@

public interface IPhantomPet extends IFlyingPet{

PetConfigEntry<Integer> SWEEP_NEXT_START_DELAY = new PetConfigEntry<>("ai.sweep.nextDelayStart", 10, "In server ticks");
PetConfigEntry<Integer> SWEEP_NEXT_MIN_DELAY = new PetConfigEntry<>("ai.sweep.nextMinDelay", 4, "In seconds");
PetConfigEntry<Integer> SWEEP_NEXT_RAND_DELAY = new PetConfigEntry<>("ai.sweep.nextRandDelay", 2, "In seconds, added to min");
PetConfigEntry<Integer> SWEEP_MIN_HEIGHT_OFFSET = new PetConfigEntry<>("ai.sweep.minHeightOffset", 10);
PetConfigEntry<Integer> SWEEP_RAND_HEIGHT_OFFSET = new PetConfigEntry<>("ai.sweep.randHeightOffset", 2);
PetConfigEntry<Integer> SWEEP_NEXT_START_DELAY = new PetConfigEntry<>(Integer.class, "ai.sweep.nextDelayStart", 10, "In server ticks");
PetConfigEntry<Integer> SWEEP_NEXT_MIN_DELAY = new PetConfigEntry<>(Integer.class, "ai.sweep.nextMinDelay", 4, "In seconds");
PetConfigEntry<Integer> SWEEP_NEXT_RAND_DELAY = new PetConfigEntry<>(Integer.class, "ai.sweep.nextRandDelay", 2, "In seconds, added to min");
PetConfigEntry<Integer> SWEEP_MIN_HEIGHT_OFFSET = new PetConfigEntry<>(Integer.class, "ai.sweep.minHeightOffset", 10);
PetConfigEntry<Integer> SWEEP_RAND_HEIGHT_OFFSET = new PetConfigEntry<>(Integer.class, "ai.sweep.randHeightOffset", 2);

PetConfigEntry<Double> CIRCLE_MIN_DISTANCE = new PetConfigEntry<>("ai.circle.minDistance", 5.0);
PetConfigEntry<Double> CIRCLE_MAX_RAND_DISTANCE = new PetConfigEntry<>("ai.circle.maxRandDistance", 10.0);
PetConfigEntry<Double> CIRCLE_MAX_DISTANCE = new PetConfigEntry<>("ai.circle.maxDistance", 15.0);
PetConfigEntry<Double> CIRCLE_MAX_DISTANCE_RESET = new PetConfigEntry<>("ai.circle.maxDistanceReset", 5.0);
PetConfigEntry<Double> CIRCLE_MIN_HEIGHT = new PetConfigEntry<>("ai.circle.minHeight", 0.0);
PetConfigEntry<Double> CIRCLE_MAX_RAND_HEIGHT = new PetConfigEntry<>("ai.circle.maxRandHeight", 9.0);
PetConfigEntry<Float> CIRCLE_MIN_DISTANCE = new PetConfigEntry<>(Float.class, "ai.circle.minDistance", 5.0f);
PetConfigEntry<Float> CIRCLE_MAX_RAND_DISTANCE = new PetConfigEntry<>(Float.class, "ai.circle.maxRandDistance", 10.0f);
PetConfigEntry<Float> CIRCLE_MAX_DISTANCE = new PetConfigEntry<>(Float.class, "ai.circle.maxDistance", 15.0f);
PetConfigEntry<Float> CIRCLE_MAX_DISTANCE_RESET = new PetConfigEntry<>(Float.class, "ai.circle.maxDistanceReset", 5.0f);
PetConfigEntry<Float> CIRCLE_MIN_HEIGHT = new PetConfigEntry<>(Float.class, "ai.circle.minHeight", 0.0f);
PetConfigEntry<Float> CIRCLE_MAX_RAND_HEIGHT = new PetConfigEntry<>(Float.class, "ai.circle.maxRandHeight", 9.0f);

void setSize(int size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

public interface ITadpolePet extends IWaterAnimalPet{

PetConfigEntry<Boolean> BRAIN_ENABLED = new PetConfigEntry<>("brain.enabled", false);
PetConfigEntry<Boolean> BRAIN_ENABLED = new PetConfigEntry<>(Boolean.class, "brain.enabled", false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public interface IWardenPet extends ILivingPet{

PetConfigEntry<Boolean> BRAIN_ENABLED = new PetConfigEntry<>("brain.enabled", false);
PetConfigEntry<Boolean> BRAIN_ENABLED = new PetConfigEntry<>(Boolean.class, "brain.enabled", false);

enum AngerLevel{
Calm(0),
Expand Down
10 changes: 5 additions & 5 deletions modules/nms/src/com/dsh105/echopet/nms/entity/EntityPet.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ public INMSEntityPetHandle createPetHandle(){

protected void initiateEntityPet(){
IPetType petType = getPet().getPetType();
this.rideSpeed = IPet.RIDING_WALK_SPEED.get(petType).floatValue();
this.rideFlySpeed = IPet.RIDING_FLY_SPEED.get(petType).floatValue();
this.rideJumpHeight = IPet.RIDING_JUMP_HEIGHT.get(petType);
this.rideSpeed = IPet.RIDING_WALK_SPEED.getNumber(petType).floatValue();
this.rideFlySpeed = IPet.RIDING_FLY_SPEED.getNumber(petType).floatValue();
this.rideJumpHeight = IPet.RIDING_JUMP_HEIGHT.getNumber(petType).doubleValue();
AttributeInstance attributeInstance = getAttribute(Attributes.MOVEMENT_SPEED);
if(attributeInstance != null){
attributeInstance.setBaseValue(IPet.GOAL_WALK_SPEED.get(petType));
attributeInstance.setBaseValue(IPet.GOAL_WALK_SPEED.getNumber(petType).doubleValue());
}
attributeInstance = getAttribute(Attributes.FLYING_SPEED);
if(attributeInstance != null){
attributeInstance.setBaseValue(IPet.GOAL_FLY_SPEED.get(petType));
attributeInstance.setBaseValue(IPet.GOAL_FLY_SPEED.getNumber(petType).doubleValue());
}
this.setPathfinding();
this.maxUpStep = getMaxUpStep();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public PetGoalFollowOwner(IEntityPet pet, Mob mob){

double sizeModifier = pet.getSizeCategory().getModifier();
IPetType petType = pet.getPet().getPetType();
double startDistance = IPet.GOAL_FOLLOW_START_DISTANCE.get(petType) * sizeModifier;
double stopDistance = IPet.GOAL_FOLLOW_STOP_DISTANCE.get(petType) * sizeModifier;
double teleportDistance = IPet.GOAL_FOLLOW_TELEPORT_DISTANCE.get(petType) * sizeModifier;
double startDistance = IPet.GOAL_FOLLOW_START_DISTANCE.getNumber(petType).doubleValue() * sizeModifier;
double stopDistance = IPet.GOAL_FOLLOW_STOP_DISTANCE.getNumber(petType).doubleValue() * sizeModifier;
double teleportDistance = IPet.GOAL_FOLLOW_TELEPORT_DISTANCE.getNumber(petType).doubleValue() * sizeModifier;

this.speedModifier = IPet.GOAL_FOLLOW_SPEED_MODIFIER.get(petType);
this.speedModifier = IPet.GOAL_FOLLOW_SPEED_MODIFIER.getNumber(petType).doubleValue();
this.startDistanceSqr = startDistance * startDistance;
this.stopDistanceSqr = stopDistance * stopDistance;
this.teleportDistanceSqr = teleportDistance * teleportDistance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public Entity getEntity(){
protected void initiateEntityPet(){
IPetType petType = getPet().getPetType();
this.canFly = IPet.RIDING_FLY.get(petType);
this.rideSpeed = IPet.RIDING_WALK_SPEED.get(petType).floatValue();
this.rideFlySpeed = IPet.RIDING_FLY_SPEED.get(petType).floatValue();
this.jumpHeight = IPet.RIDING_JUMP_HEIGHT.get(petType);
this.rideSpeed = IPet.RIDING_WALK_SPEED.getNumber(petType).floatValue();
this.rideFlySpeed = IPet.RIDING_FLY_SPEED.getNumber(petType).floatValue();
this.jumpHeight = IPet.RIDING_JUMP_HEIGHT.getNumber(petType).doubleValue();
this.setPathfinding();
getEntity().maxUpStep = getEntityPet().getMaxUpStep();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ protected void initiateEntityPet(){
IPetType petType = getPet().getPetType();
AttributeInstance attributeInstance = getEntity().getAttribute(Attributes.MOVEMENT_SPEED);
if(attributeInstance != null){
attributeInstance.setBaseValue(IPet.GOAL_WALK_SPEED.get(petType));
attributeInstance.setBaseValue(IPet.GOAL_WALK_SPEED.getNumber(petType).doubleValue());
}
attributeInstance = getEntity().getAttribute(Attributes.FLYING_SPEED);
if(attributeInstance != null){
attributeInstance.setBaseValue(IPet.GOAL_FLY_SPEED.get(petType));
attributeInstance.setBaseValue(IPet.GOAL_FLY_SPEED.getNumber(petType).doubleValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public EntityBatPet(Level world){
public EntityBatPet(Level world, IPet pet){
super(EntityType.BAT, world, pet);
double sizeModifier = getSizeCategory().getModifier();
flyRange = (int) Math.max(1, Math.ceil(IBatPet.GOAL_WANDER_FLY_RANGE.get(PetType.BAT) * sizeModifier));
flyRange = (int) Math.max(1, Math.ceil(IBatPet.GOAL_WANDER_FLY_RANGE.getNumber(PetType.BAT).doubleValue() * sizeModifier));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ private class PhantomAttackStrategyGoal extends PetGoal{

PhantomAttackStrategyGoal(){
setFlags(EnumSet.of(PetGoal.Flag.MOVE));
this.sweepTickDelayStart = IPhantomPet.SWEEP_NEXT_START_DELAY.get(PetType.PHANTOM);
this.sweepTickNextMin = IPhantomPet.SWEEP_NEXT_MIN_DELAY.get(PetType.PHANTOM);
this.sweepTickNextRand = IPhantomPet.SWEEP_NEXT_RAND_DELAY.get(PetType.PHANTOM);
this.minHeightOffset = IPhantomPet.SWEEP_MIN_HEIGHT_OFFSET.get(PetType.PHANTOM);
this.randHeightOffset = IPhantomPet.SWEEP_RAND_HEIGHT_OFFSET.get(PetType.PHANTOM);
this.sweepTickDelayStart = IPhantomPet.SWEEP_NEXT_START_DELAY.getNumber(PetType.PHANTOM).intValue();
this.sweepTickNextMin = IPhantomPet.SWEEP_NEXT_MIN_DELAY.getNumber(PetType.PHANTOM).intValue();
this.sweepTickNextRand = IPhantomPet.SWEEP_NEXT_RAND_DELAY.getNumber(PetType.PHANTOM).intValue();
this.minHeightOffset = IPhantomPet.SWEEP_MIN_HEIGHT_OFFSET.getNumber(PetType.PHANTOM).intValue();
this.randHeightOffset = IPhantomPet.SWEEP_RAND_HEIGHT_OFFSET.getNumber(PetType.PHANTOM).intValue();
}

@Override
Expand Down Expand Up @@ -212,12 +212,12 @@ private class PhantomCircleAroundAnchorGoal extends PhantomMoveTargetGoal{

PhantomCircleAroundAnchorGoal(){
super();
this.minDistance = IPhantomPet.CIRCLE_MIN_DISTANCE.get(PetType.PHANTOM).floatValue();
this.maxRandDistance = IPhantomPet.CIRCLE_MAX_RAND_DISTANCE.get(PetType.PHANTOM).floatValue();
this.maxDistance = IPhantomPet.CIRCLE_MAX_DISTANCE.get(PetType.PHANTOM).floatValue();
this.maxDistanceReset = IPhantomPet.CIRCLE_MAX_DISTANCE_RESET.get(PetType.PHANTOM).floatValue();
this.minHeight = IPhantomPet.CIRCLE_MIN_HEIGHT.get(PetType.PHANTOM).floatValue();
this.maxRandHeight = IPhantomPet.CIRCLE_MAX_RAND_HEIGHT.get(PetType.PHANTOM).floatValue();
this.minDistance = IPhantomPet.CIRCLE_MIN_DISTANCE.getNumber(PetType.PHANTOM).floatValue();
this.maxRandDistance = IPhantomPet.CIRCLE_MAX_RAND_DISTANCE.getNumber(PetType.PHANTOM).floatValue();
this.maxDistance = IPhantomPet.CIRCLE_MAX_DISTANCE.getNumber(PetType.PHANTOM).floatValue();
this.maxDistanceReset = IPhantomPet.CIRCLE_MAX_DISTANCE_RESET.getNumber(PetType.PHANTOM).floatValue();
this.minHeight = IPhantomPet.CIRCLE_MIN_HEIGHT.getNumber(PetType.PHANTOM).floatValue();
this.maxRandHeight = IPhantomPet.CIRCLE_MAX_RAND_HEIGHT.getNumber(PetType.PHANTOM).floatValue();
}

@Override
Expand Down

0 comments on commit 9263e52

Please sign in to comment.