Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-89 Replace old take/give keys with health change set to 0 #96

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class ConfigMigrationService extends PlainMigrationService {
protected boolean performMigrations(PropertyReader reader, ConfigurationData configData) {
return migrateCraftingRecipeToCrafting(reader, configData) |
migrateEliminationModeToCustomActions(reader, configData) |
migratesHealthChangeToHealthChangeSection(reader, configData) ||
migrateHealthChangeToHealthChangeSection(reader, configData) |
removeTakeGiveHeartsFromPlayers(reader, configData) ||
hasDeprecatedKeys(reader);
}

Expand Down Expand Up @@ -141,7 +142,7 @@ private static boolean migrateEliminationModeToCustomActions(PropertyReader read
*
* @return the state whether migration is required
*/
private static boolean migratesHealthChangeToHealthChangeSection(PropertyReader reader, ConfigurationData configData) {
private static boolean migrateHealthChangeToHealthChangeSection(PropertyReader reader, ConfigurationData configData) {
String oldKey = "baseSettings.healthChange";
PropertyValue<Integer> healthProperty = new IntegerProperty(oldKey, 0).determineValue(reader);
if (!healthProperty.isValidInResource()) {
Expand All @@ -156,12 +157,47 @@ private static boolean migratesHealthChangeToHealthChangeSection(PropertyReader
return MIGRATION_REQUIRED;
}

/**
* Removes the old take/give hearts from a victim/killer and
* replaces it with their healthChange set to 0.
*
* @since 1.6
*
* @param reader the config reader
* @param configData the config inmemory
*
* @return the state whether migration is required
*/
private static boolean removeTakeGiveHeartsFromPlayers(PropertyReader reader, ConfigurationData configData) {
String oldVictimKey = "baseSettings.takeHealthFromVictim";
String oldKillerKey = "baseSettings.giveHealthToKiller";

PropertyValue<Boolean> oldVictimProperty = new BooleanProperty(oldVictimKey, true).determineValue(reader);
PropertyValue<Boolean> oldKillerProperty = new BooleanProperty(oldKillerKey, true).determineValue(reader);

if (!oldVictimProperty.isValidInResource() && !oldKillerProperty.isValidInResource()) {
return NO_MIGRATION_NEEDED;
}

if (oldVictimProperty.getValue()) {
configData.setValue(HealthChangeConfig.VICTIM, 0);
}

if (oldKillerProperty.getValue()) {
configData.setValue(HealthChangeConfig.KILLER, 0);
}

return MIGRATION_REQUIRED;
}

private static boolean hasDeprecatedKeys(PropertyReader reader) {
List<String> deprecatedKeys = new ArrayList<String>() {
{
this.add("baseSettings.heartItem.craftingRecipe");
this.add("baseSettings.eliminationMode");
this.add("baseSettings.healthChange");
this.add("baseSettings.takeHealthFromVictim");
this.add("baseSettings.giveHealthToKiller");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@
*/
public class BaseConfig implements SettingsHolder {

@Comment("If health points should be decreased from a player who is killed.")
public static final Property<Boolean> TAKE_HEALTH_FROM_VICTIM = new BooleanProperty(
"baseSettings.takeHealthFromVictim",
true
);

@Comment("If health points should be increased for a player who killed somebody.")
public static final Property<Boolean> GIVE_HEALTH_TO_KILLER = new BooleanProperty(
"baseSettings.giveHealthToKiller",
true
);

@Comment("Amount of maximum health points that will be given to the new player that did not play before.")
public static final Property<Integer> DEFAULT_HEALTH = new IntegerProperty(
"baseSettings.defaultHealth",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class HealthChangeConfig implements SettingsHolder {
public void registerComments(CommentsConfiguration config) {
config.setComment(
"baseSettings.healthChange",
"Amount of health points revoked when a player was killed or awarded when a kill was scored."
"Amount of health points revoked when a player was killed or awarded when a kill was scored.",
"If you want to disable the life steal system for a killer or a victim just set their health change to 0."
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ public void changePlayerHearts(PlayerDeathEvent event) {

EntityDamageEvent lastDamageCause = victim.getLastDamageCause();

boolean giveHealthToKiller = this.config.getProperty(BaseConfig.GIVE_HEALTH_TO_KILLER);
boolean killByPlayerOnly = this.config.getProperty(BaseConfig.KILL_BY_PLAYER_ONLY);

if (killByPlayerOnly && killer == null) {
return;
}

if (giveHealthToKiller && killer != null) {
int killerHealthChange = this.config.getProperty(HealthChangeConfig.KILLER);
if (killerHealthChange > 0 && killer != null) {
if (this.config.getProperty(BaseConfig.IGNORE_SAME_IP)) {
if (victim.getAddress().getAddress().equals(killer.getAddress().getAddress())) {
return;
Expand All @@ -113,7 +113,7 @@ public void changePlayerHearts(PlayerDeathEvent event) {
this.stealCooldowns.put(killer, new SimpleImmutableEntry<>(victim, Instant.now()));
}

double killerNewHealth = this.adapter.getMaxHealth(killer) + this.config.getProperty(HealthChangeConfig.KILLER);
double killerNewHealth = this.adapter.getMaxHealth(killer) + killerHealthChange;
if (killerNewHealth <= this.config.getProperty(BaseConfig.MAXIMUM_HEALTH)) {
this.adapter.setMaxHealth(killer, killerNewHealth);
} else {
Expand All @@ -126,14 +126,17 @@ public void changePlayerHearts(PlayerDeathEvent event) {
}
}

boolean takeHealthFromVictim = this.config.getProperty(BaseConfig.TAKE_HEALTH_FROM_VICTIM);
int victimHealthChange = this.config.getProperty(HealthChangeConfig.VICTIM);
if (victimHealthChange <= 0) {
return;
}

double victimMaxHealth = this.adapter.getMaxHealth(victim);
double victimNewHealth = victimMaxHealth - this.config.getProperty(HealthChangeConfig.VICTIM);
double victimNewHealth = victimMaxHealth - victimHealthChange;

int minimumHealth = this.config.getProperty(BaseConfig.MINIMUM_HEALTH);

if (takeHealthFromVictim && victimNewHealth >= minimumHealth) {
if (victimNewHealth >= minimumHealth) {
this.adapter.setMaxHealth(victim, victimNewHealth);
}

Expand Down