diff --git a/build.gradle.kts b/build.gradle.kts index a75e9a3d..2b505fb5 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackrawstats/Lifecycle.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackrawstats/Lifecycle.java new file mode 100644 index 00000000..2cd88353 --- /dev/null +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackrawstats/Lifecycle.java @@ -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 . + */ +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 commands, final Set listeners, final Set configs, final Set> moduleRecipes) { + super(plugin, commands, listeners, configs, moduleRecipes); + } + + @Override + public void onEnable() { + RawStats.registerStats(Scoreboards.main()); + } +} diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackrawstats/TrackRawStats.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackrawstats/TrackRawStats.java index 06f1b7b1..a8c43e21 100644 --- a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackrawstats/TrackRawStats.java +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackrawstats/TrackRawStats.java @@ -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 lifecycle() { - return ModuleLifecycle.Empty.class; + return Lifecycle.class; } @Override diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CalculatedStat.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CalculatedStat.java index 9b0dec49..22e6f9fb 100644 --- a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CalculatedStat.java +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CalculatedStat.java @@ -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()); @@ -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)); } } diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CombinedStat.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CombinedStat.java index d321acf0..cd4eba9e 100644 --- a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CombinedStat.java +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/CombinedStat.java @@ -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 { @@ -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 stat : this.stats) { value += stat.applyAsInt(player); diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Lifecycle.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Lifecycle.java index b4055e62..66858d92 100644 --- a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Lifecycle.java +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Lifecycle.java @@ -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 commands, final Set listeners, final Set configs, final Set> moduleRecipes, final StatsRunnable runnable) { + Lifecycle(final JavaPlugin plugin, final Set commands, final Set listeners, final Set configs, final Set> 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); } diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/ScaledStat.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/ScaledStat.java index 5a20e3d6..ec27922c 100644 --- a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/ScaledStat.java +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/ScaledStat.java @@ -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 { @@ -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 diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Stats.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Stats.java index 10708579..513edd13 100644 --- a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Stats.java +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/Stats.java @@ -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 REGISTRY = new LinkedHashMap<>(); + static final Map 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(); @@ -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()); + } + } + } } diff --git a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/TrackStats.java b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/TrackStats.java index 6f0c23f8..0bf35081 100644 --- a/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/TrackStats.java +++ b/src/main/java/me/machinemaker/papertweaks/modules/survival/trackstats/TrackStats.java @@ -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