Skip to content

Commit 9e34efb

Browse files
committed
Add a delay to game closing
Fixes #3
1 parent a21e7ac commit 9e34efb

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

src/main/java/io/github/haykam821/blocklobbers/game/BlockLobbersConfig.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,34 @@
44
import com.mojang.serialization.MapCodec;
55
import com.mojang.serialization.codecs.RecordCodecBuilder;
66

7+
import net.minecraft.SharedConstants;
78
import net.minecraft.util.Identifier;
9+
import net.minecraft.util.math.intprovider.ConstantIntProvider;
10+
import net.minecraft.util.math.intprovider.IntProvider;
811
import xyz.nucleoid.plasmid.api.game.common.config.WaitingLobbyConfig;
912

1013
public class BlockLobbersConfig {
14+
private static final IntProvider DEFAULT_TICKS_UNTIL_CLOSE = ConstantIntProvider.create(SharedConstants.TICKS_PER_SECOND * 5);
15+
1116
public static final MapCodec<BlockLobbersConfig> CODEC = RecordCodecBuilder.mapCodec(instance -> {
1217
return instance.group(
1318
Identifier.CODEC.fieldOf("map").forGetter(BlockLobbersConfig::getMap),
1419
WaitingLobbyConfig.CODEC.fieldOf("players").forGetter(BlockLobbersConfig::getPlayerConfig),
15-
Codec.BOOL.optionalFieldOf("consume_lobbables", true).forGetter(BlockLobbersConfig::shouldConsumeLobbables)
20+
Codec.BOOL.optionalFieldOf("consume_lobbables", true).forGetter(BlockLobbersConfig::shouldConsumeLobbables),
21+
IntProvider.NON_NEGATIVE_CODEC.optionalFieldOf("ticks_until_close", DEFAULT_TICKS_UNTIL_CLOSE).forGetter(BlockLobbersConfig::getTicksUntilClose)
1622
).apply(instance, BlockLobbersConfig::new);
1723
});
1824

1925
private final Identifier map;
2026
private final WaitingLobbyConfig playerConfig;
2127
private final boolean consumeLobbables;
28+
private final IntProvider ticksUntilClose;
2229

23-
public BlockLobbersConfig(Identifier map, WaitingLobbyConfig playerConfig, boolean consumeLobbables) {
30+
public BlockLobbersConfig(Identifier map, WaitingLobbyConfig playerConfig, boolean consumeLobbables, IntProvider ticksUntilClose) {
2431
this.map = map;
2532
this.playerConfig = playerConfig;
2633
this.consumeLobbables = consumeLobbables;
34+
this.ticksUntilClose = ticksUntilClose;
2735
}
2836

2937
public Identifier getMap() {
@@ -37,4 +45,8 @@ public WaitingLobbyConfig getPlayerConfig() {
3745
public boolean shouldConsumeLobbables() {
3846
return this.consumeLobbables;
3947
}
48+
49+
public IntProvider getTicksUntilClose() {
50+
return this.ticksUntilClose;
51+
}
4052
}

src/main/java/io/github/haykam821/blocklobbers/game/phase/BlockLobbersActivePhase.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class BlockLobbersActivePhase implements BlockBreakEvent, GameActivityEve
4141
private final Set<PlayerEntry> players;
4242
private final WinManager winManager;
4343

44-
private boolean closing;
44+
private int ticksUntilClose = -1;
4545

4646
public BlockLobbersActivePhase(GameSpace gameSpace, ServerWorld world, BlockLobbersMap map, BlockLobbersConfig config) {
4747
this.gameSpace = gameSpace;
@@ -106,6 +106,17 @@ public void onEnable() {
106106

107107
@Override
108108
public void onTick() {
109+
// Decrease ticks until game end to zero
110+
if (this.isGameEnding()) {
111+
if (this.ticksUntilClose == 0) {
112+
this.gameSpace.close(GameCloseReason.FINISHED);
113+
} else {
114+
this.ticksUntilClose -= 1;
115+
}
116+
117+
return;
118+
}
119+
109120
Iterator<PlayerEntry> playerIterator = this.players.iterator();
110121
while (playerIterator.hasNext()) {
111122
PlayerEntry entry = playerIterator.next();
@@ -116,8 +127,7 @@ public void onTick() {
116127

117128
// Attempt to determine a winner
118129
if (this.winManager.checkForWinner()) {
119-
this.closing = true;
120-
gameSpace.close(GameCloseReason.FINISHED);
130+
this.endGame();
121131
}
122132
}
123133

@@ -207,7 +217,7 @@ private void setSpectator(ServerPlayerEntity player) {
207217
}
208218

209219
private boolean eliminate(PlayerEntry entry, String suffix, boolean remove) {
210-
if (this.closing) return false;
220+
if (this.isGameEnding()) return false;
211221

212222
Text message = entry.getEliminatedText(suffix);
213223
for (ServerPlayerEntity player : this.gameSpace.getPlayers()) {
@@ -225,4 +235,12 @@ private boolean eliminate(PlayerEntry entry, String suffix, boolean remove) {
225235
public boolean eliminate(PlayerEntry entry, boolean remove) {
226236
return this.eliminate(entry, "", remove);
227237
}
238+
239+
private void endGame() {
240+
this.ticksUntilClose = this.config.getTicksUntilClose().get(this.world.getRandom());
241+
}
242+
243+
private boolean isGameEnding() {
244+
return this.ticksUntilClose >= 0;
245+
}
228246
}

0 commit comments

Comments
 (0)