Skip to content

Commit

Permalink
Allow configuring the statistic bundle namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Jul 28, 2023
1 parent 9ee7cfb commit 7fa5cde
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.haykam821.downpour.game;

import java.util.Optional;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

Expand All @@ -9,7 +11,9 @@
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.intprovider.ConstantIntProvider;
import net.minecraft.util.math.intprovider.IntProvider;
import xyz.nucleoid.plasmid.game.GameSpace;
import xyz.nucleoid.plasmid.game.common.config.PlayerConfig;
import xyz.nucleoid.plasmid.game.stats.GameStatisticBundle;

public class DownpourConfig {
public static final Codec<DownpourConfig> CODEC = RecordCodecBuilder.create(instance -> {
Expand All @@ -21,7 +25,8 @@ public class DownpourConfig {
SoundEvent.CODEC.optionalFieldOf("unlock_sound", SoundEvents.ENTITY_LIGHTNING_BOLT_THUNDER).forGetter(DownpourConfig::getUnlockSound),
Codec.INT.optionalFieldOf("lock_time", 20 * 7).forGetter(DownpourConfig::getLockTime),
Codec.INT.optionalFieldOf("unlock_time", 20 * 2).forGetter(DownpourConfig::getUnlockTime),
Codec.INT.optionalFieldOf("no_knockback_rounds", 2).forGetter(DownpourConfig::getNoKnockbackRounds)
Codec.INT.optionalFieldOf("no_knockback_rounds", 2).forGetter(DownpourConfig::getNoKnockbackRounds),
Codec.STRING.optionalFieldOf("statistic_bundle_namespace").forGetter(DownpourConfig::getStatisticBundleNamespace)
).apply(instance, DownpourConfig::new);
});

Expand All @@ -33,8 +38,9 @@ public class DownpourConfig {
private final int lockTime;
private final int unlockTime;
private final int noKnockbackRounds;
private final Optional<String> statisticBundleNamespace;

public DownpourConfig(DownpourMapConfig mapConfig, PlayerConfig playerConfig, IntProvider ticksUntilClose, SoundEvent lockSound, SoundEvent unlockSound, int lockTime, int unlockTime, int noKnockbackRounds) {
public DownpourConfig(DownpourMapConfig mapConfig, PlayerConfig playerConfig, IntProvider ticksUntilClose, SoundEvent lockSound, SoundEvent unlockSound, int lockTime, int unlockTime, int noKnockbackRounds, Optional<String> statisticBundleNamespace) {
this.mapConfig = mapConfig;
this.playerConfig = playerConfig;
this.ticksUntilClose = ticksUntilClose;
Expand All @@ -43,6 +49,7 @@ public DownpourConfig(DownpourMapConfig mapConfig, PlayerConfig playerConfig, In
this.lockTime = lockTime;
this.unlockTime = unlockTime;
this.noKnockbackRounds = noKnockbackRounds;
this.statisticBundleNamespace = statisticBundleNamespace;
}

public DownpourMapConfig getMapConfig() {
Expand Down Expand Up @@ -76,4 +83,16 @@ public int getUnlockTime() {
public int getNoKnockbackRounds() {
return this.noKnockbackRounds;
}

public Optional<String> getStatisticBundleNamespace() {
return this.statisticBundleNamespace;
}

public GameStatisticBundle getStatisticBundle(GameSpace gameSpace) {
return this.statisticBundleNamespace
.map(namespace -> {
return gameSpace.getStatistics().bundle(namespace);
})
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public DownpourActivePhase(GameSpace gameSpace, ServerWorld world, DownpourMap m
this.ticksUntilSwitch = this.config.getLockTime();
this.createShelter();

this.statistics = gameSpace.getStatistics().bundle(Main.MOD_ID);
this.statistics = config.getStatisticBundle(gameSpace);
}

public static void setRules(GameActivity activity) {
Expand Down Expand Up @@ -121,7 +121,7 @@ private void enable() {
this.updateRoundsExperienceLevel(player);
player.changeGameMode(GameMode.ADVENTURE);

if (!this.singleplayer) {
if (!this.singleplayer && this.statistics != null) {
this.statistics.forPlayer(player).increment(StatisticKeys.GAMES_PLAYED, 1);
}

Expand Down Expand Up @@ -166,8 +166,10 @@ private void addRounds(int rounds) {
this.updateRoundsExperienceLevel(player);
}

for (PlayerRef player : this.players) {
this.statistics.forPlayer(player).increment(Main.ROUNDS_SURVIVED, rounds);
if (!this.singleplayer && this.statistics != null) {
for (PlayerRef player : this.players) {
this.statistics.forPlayer(player).increment(Main.ROUNDS_SURVIVED, rounds);
}
}
}

Expand Down Expand Up @@ -305,14 +307,14 @@ private boolean eliminate(ServerPlayerEntity eliminatedPlayer, boolean remove) {
}

public void applyPlayerFinishStatistics(ServerPlayerEntity player, StatisticKey<Integer> finishTypeKey) {
if (!this.singleplayer) {
if (!this.singleplayer && this.statistics != null) {
this.statistics.forPlayer(player).increment(finishTypeKey, 1);
this.statistics.forPlayer(player).set(StatisticKeys.LONGEST_TIME, this.ticksElapsed);
}
}

private ActionResult onPlayerAttackEntity(ServerPlayerEntity attacker, Hand hand, Entity attacked, EntityHitResult hitResult) {
if (!this.isGameEnding() && attacker != attacked && this.players.contains(PlayerRef.of(attacker))) {
if (!this.isGameEnding() && attacker != attacked && this.players.contains(PlayerRef.of(attacker)) && !this.singleplayer && this.statistics != null) {
ServerPlayerEntity attackedPlayer = (ServerPlayerEntity) attacked;
if (this.players.contains(PlayerRef.of(attackedPlayer))) {
this.statistics.forPlayer(attacker).increment(Main.PLAYERS_PUNCHED, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"min": 1,
"max": 32,
"threshold": 4
}
},
"statistic_bundle_namespace": "downpour"
}

0 comments on commit 7fa5cde

Please sign in to comment.