Skip to content
Open
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 @@ -2,23 +2,17 @@

import com.google.inject.Inject;
import de.drolpi.gamecore.api.counter.Counter;
import de.drolpi.gamecore.api.counter.HandlerType;
import de.drolpi.gamecore.api.player.GamePlayer;
import de.drolpi.gamecore.api.event.GameJoinEvent;
import de.drolpi.gamecore.api.feature.AbstractFeature;
import de.drolpi.gamecore.api.game.Game;
import de.drolpi.gamecore.api.phase.Phase;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;

import java.util.function.Consumer;

public abstract class AbstractCounterProgressFeature extends AbstractCounterHandlerFeature {

private final Game game;

private boolean countUp;

@Inject
public AbstractCounterProgressFeature(Game game, Phase phase) {
super(phase);
Expand All @@ -28,7 +22,6 @@ public AbstractCounterProgressFeature(Game game, Phase phase) {
@Override
public void enable() {
super.enable();
this.countUp = this.counterFeature.stopCount() > this.counterFeature.startCount();
}

@EventHandler
Expand All @@ -39,7 +32,7 @@ public void handle(GameJoinEvent event) {
return;
}

this.setStart(player, counter);
this.reset(player, counter);
}

@Override
Expand All @@ -57,7 +50,7 @@ protected void tick(Counter counter) {
@Override
protected void cancel(Counter counter) {
for (final GamePlayer player : this.game.allPlayers()) {
this.setStart(player.player(), counter);
this.reset(player.player(), counter);
}
}

Expand All @@ -67,16 +60,20 @@ protected void finish(Counter counter) {
}

protected float progress(Counter counter) {
return counter.currentCount() / (this.startCount(counter) + 0.0F);
return (float) (counter.currentCount() - this.lowerBound(counter)) / (this.upperBound(counter) - this.lowerBound(counter));
}

protected long upperBound(Counter counter) {
return Math.max(counter.startCount(), counter.stopCount());
}

protected long startCount(Counter counter) {
return this.countUp ? counter.stopCount() : counter.startCount();
protected long lowerBound(Counter counter) {
return Math.min(counter.startCount(), counter.stopCount());
}

protected void setStart(Player player, Counter counter) {
this.set(player, (int) this.startCount(counter), this.countUp ? 0 : 1);
protected void reset(Player player, Counter counter) {
this.set(player, counter.startCount(), counter.startCount() > counter.stopCount() ? 1F : 0F);
}

protected abstract void set(Player player, int count, float progress);
protected abstract void set(Player player, long count, float progress);
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,77 @@
package de.drolpi.gamecore.api.feature.def;

import com.google.gson.annotations.Expose;
import com.google.inject.Inject;
import de.drolpi.gamecore.api.counter.Counter;
import de.drolpi.gamecore.api.game.Game;
import de.drolpi.gamecore.api.phase.Phase;
import org.bukkit.boss.BossBar;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.entity.Player;

public class BossBarCounterFeature extends AbstractCounterProgressFeature {

private final BossBarFeature bossBarFeature;

private BossBar bossBar;
@Expose
private final String replaceBossBarId = "counter";
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename?, a little bit long and redundant.

private final Component component;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See field order :)

@Expose
private BossBar.Color color = BossBar.Color.RED;
@Expose
private BossBar.Overlay overlay = BossBar.Overlay.PROGRESS;

@Inject
public BossBarCounterFeature(Game game, Phase phase) {
super(game, phase);
this.bossBarFeature = phase.feature(BossBarFeature.class);
this.component = Component.translatable(phase.key() + "counter_bossbar");
}

@Override
public void enable() {
super.enable();
this.bossBar = this.bossBarFeature.bossBar();
}

@Override
public void disable() {
super.disable();
this.bossBar = null;
protected void start(Counter counter) {
super.start(counter);
if (!this.bossBarFeature.isRegistered(replaceBossBarId)) {
this.bossBarFeature.register(this.replaceBossBarId, this.color, this.overlay);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should register a boss bar during the runtime of a phase. If at all, this should happen in the constructor, because it is called during the configuration of a phase / game or directly after loading the configuration file

}
this.bossBarFeature.setColor(this.replaceBossBarId, this.color);
this.bossBarFeature.setOverlay(this.replaceBossBarId, this.overlay);
}

@Override
protected void start(Counter counter) {
this.bossBar.setVisible(true);
protected void cancel(Counter counter) {
this.resetName();
}

@Override
protected void cancel(Counter counter) {
super.cancel(counter);
this.bossBar.setTitle(this.bossBarFeature.message());
if (!this.bossBarFeature.message().isBlank()) {
return;
}
this.bossBar.setVisible(false);
protected void finish(Counter counter) {
this.resetName();
}

private void resetName() {
this.bossBarFeature.resetName(this.replaceBossBarId);
}

@Override
protected void tick(Counter counter) {
this.set(null, counter.currentCount(), super.progress(counter));
}

@Override
protected void set(Player player, int count, float progress) {
//TODO: Lang
this.bossBar.setTitle("" + count);
this.bossBar.setProgress(progress);
protected void set(Player player, long count, float progress) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The player is not used, is it?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should change the AbstractCounterProgressFeature so that there is no player loop and move it to the LevelCounterFeature, because it is not needed for the BossBarCounterFeature and the method is called unnecessarily often.

if (!this.bossBarFeature.isRegistered(replaceBossBarId)) {
this.bossBarFeature.register(this.replaceBossBarId, this.color, this.overlay);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, here too we should not register during the term of a phase

this.bossBarFeature.setColor(this.replaceBossBarId, this.color);
this.bossBarFeature.setOverlay(this.replaceBossBarId, this.overlay);
}
this.bossBarFeature.setName(this.replaceBossBarId, component);
this.bossBarFeature.setResolvers(this.replaceBossBarId, Placeholder.component("count", Component.text(count)));
this.bossBarFeature.setProgress(this.replaceBossBarId, progress);
}
}
Loading