Skip to content

Commit

Permalink
fix: don't register/initialize stats until the module is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Sep 27, 2023
1 parent 5464bdd commit dead00f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "me.machinemaker"
version = "0.4.0"
version = "0.4.1-SNAPSHOT"
description = "A replacement for the VanillaTweaks datapack"

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* GNU General Public License v3
*
* PaperTweaks, a performant replacement for the VanillaTweaks datapacks.
*
* Copyright (C) 2023 Machine_Maker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package me.machinemaker.papertweaks.modules.survival.trackrawstats;

import com.google.inject.Inject;
import java.util.Set;
import me.machinemaker.papertweaks.modules.ModuleCommand;
import me.machinemaker.papertweaks.modules.ModuleConfig;
import me.machinemaker.papertweaks.modules.ModuleLifecycle;
import me.machinemaker.papertweaks.modules.ModuleListener;
import me.machinemaker.papertweaks.modules.ModuleRecipe;
import me.machinemaker.papertweaks.utils.boards.Scoreboards;
import org.bukkit.plugin.java.JavaPlugin;

class Lifecycle extends ModuleLifecycle {

@Inject
Lifecycle(final JavaPlugin plugin, final Set<ModuleCommand> commands, final Set<ModuleListener> listeners, final Set<ModuleConfig> configs, final Set<ModuleRecipe<?>> moduleRecipes) {
super(plugin, commands, listeners, configs, moduleRecipes);
}

@Override
public void onEnable() {
RawStats.registerStats(Scoreboards.main());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ public class TrackRawStats extends ModuleBase {

static final Logger LOGGER = LoggerFactory.getModuleLogger(TrackRawStats.class);

TrackRawStats() {
RawStats.registerStats(Scoreboards.main());
}

@Override
protected Class<? extends ModuleLifecycle> lifecycle() {
return ModuleLifecycle.Empty.class;
return Lifecycle.class;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final Component displayName() {
return this.displayName;
}

protected abstract int computeScore(Score score, Player player);
protected abstract int computeScore(Player player);

public final Score getScore(final Scoreboard board, final Player player) {
return this.getObjective(board).getScore(player.getName());
Expand All @@ -60,7 +60,7 @@ public final Objective getObjective(final Scoreboard board) {

public final void updateScore(final Scoreboard board, final Player player) {
final Score score = this.getScore(board, player);
score.setScore(this.computeScore(score, player));
score.setScore(this.computeScore(player));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Score;

final class CombinedStat extends CalculatedStat {

Expand All @@ -38,7 +37,7 @@ private CombinedStat(final String objectiveName, final String displayName, final
}

@Override
protected int computeScore(final Score score, final Player player) {
protected int computeScore(final Player player) {
int value = 0;
for (final ToIntFunction<Player> stat : this.stats) {
value += stat.applyAsInt(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@
import me.machinemaker.papertweaks.modules.ModuleListener;
import me.machinemaker.papertweaks.modules.ModuleRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Scoreboard;

class Lifecycle extends ModuleLifecycle {

private final StatsRunnable runnable;
private final Scoreboard board;

@Inject
Lifecycle(final JavaPlugin plugin, final Set<ModuleCommand> commands, final Set<ModuleListener> listeners, final Set<ModuleConfig> configs, final Set<ModuleRecipe<?>> moduleRecipes, final StatsRunnable runnable) {
Lifecycle(final JavaPlugin plugin, final Set<ModuleCommand> commands, final Set<ModuleListener> listeners, final Set<ModuleConfig> configs, final Set<ModuleRecipe<?>> moduleRecipes, final StatsRunnable runnable, final Scoreboard board) {
super(plugin, commands, listeners, configs, moduleRecipes);
this.runnable = runnable;
this.board = board;
}

@Override
public void onEnable() {
Stats.registerStats(this.board);
this.runnable.runTaskTimer(1L, 5L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.function.IntUnaryOperator;
import org.bukkit.Statistic;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Score;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

final class ScaledStat extends CalculatedStat {
Expand All @@ -38,8 +37,8 @@ final class ScaledStat extends CalculatedStat {
}

@Override
public int computeScore(final Score score, final Player player) {
return this.scaleFunction.applyAsInt(score.getScore());
public int computeScore(final Player player) {
return this.scaleFunction.applyAsInt(player.getStatistic(this.stat));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import java.util.function.IntUnaryOperator;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.scoreboard.Criteria;
import org.bukkit.scoreboard.Scoreboard;

@SuppressWarnings("unused")
final class Stats {

public static final Map<String, CalculatedStat> REGISTRY = new LinkedHashMap<>();
static final Map<String, CalculatedStat> REGISTRY = new LinkedHashMap<>();

public static final CombinedStat ALL_COAL = createCombined("tas_MineCoal", "Mine All Coal").addMined(Material.COAL_ORE, Material.DEEPSLATE_COAL_ORE).build();
public static final CombinedStat ALL_DIAMOND = createCombined("tas_MineDiamond", "Mine All Diamond").addMined(Material.DIAMOND_ORE, Material.DEEPSLATE_DIAMOND_ORE).build();
Expand Down Expand Up @@ -70,4 +73,12 @@ private static ScaledStat createScaled(final Statistic stat, final IntUnaryOpera
private static CombinedStat.Builder createCombined(final String objectiveName, final String displayName) {
return new CombinedStat.Builder(objectiveName, displayName);
}

static void registerStats(final Scoreboard board) {
for (final CalculatedStat stat : REGISTRY.values()) {
if (board.getObjective(stat.objectiveName()) == null) {
board.registerNewObjective(stat.objectiveName(), Criteria.DUMMY, stat.displayName());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,21 @@
package me.machinemaker.papertweaks.modules.survival.trackstats;

import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import me.machinemaker.papertweaks.annotations.ModuleInfo;
import me.machinemaker.papertweaks.modules.ModuleBase;
import me.machinemaker.papertweaks.modules.ModuleCommand;
import me.machinemaker.papertweaks.modules.ModuleLifecycle;
import org.bukkit.Bukkit;
import org.bukkit.scoreboard.Criteria;
import me.machinemaker.papertweaks.utils.boards.Scoreboards;
import org.bukkit.scoreboard.Scoreboard;

@ModuleInfo(name = "TrackStats", configPath = "survival.track-stats", description = "Adds several pre-processed stats")
public class TrackStats extends ModuleBase {

final Scoreboard board = Objects.requireNonNull(Bukkit.getScoreboardManager(), "null ScoreboardManager").getMainScoreboard();

TrackStats() {
for (final CalculatedStat stat : Stats.REGISTRY.values()) {
if (this.board.getObjective(stat.objectiveName()) == null) {
this.board.registerNewObjective(stat.objectiveName(), Criteria.DUMMY, stat.displayName());
}
}
}

@Override
protected void configure() {
super.configure();
this.bind(Scoreboard.class).toInstance(this.board);
this.bind(Scoreboard.class).toInstance(Scoreboards.main());
}

@Override
Expand Down

0 comments on commit dead00f

Please sign in to comment.