Skip to content

Commit 113f94b

Browse files
committed
Add a delay to game closing
Fixes #2
1 parent e885c53 commit 113f94b

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/main/java/io/github/haykam821/sculkprison/game/SculkPrisonConfig.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@
33
import com.mojang.serialization.Codec;
44
import com.mojang.serialization.codecs.RecordCodecBuilder;
55

6+
import net.minecraft.SharedConstants;
7+
import net.minecraft.util.math.intprovider.ConstantIntProvider;
8+
import net.minecraft.util.math.intprovider.IntProvider;
69
import xyz.nucleoid.plasmid.game.common.config.PlayerConfig;
710

811
public class SculkPrisonConfig {
912
public static final Codec<SculkPrisonConfig> CODEC = RecordCodecBuilder.create(instance -> {
1013
return instance.group(
1114
PlayerConfig.CODEC.fieldOf("players").forGetter(SculkPrisonConfig::getPlayerConfig),
1215
Codec.INT.optionalFieldOf("lock_time", 20 * 30).forGetter(SculkPrisonConfig::getLockTime),
13-
Codec.INT.optionalFieldOf("survive_time", 20 * 60 * 5).forGetter(SculkPrisonConfig::getSurviveTime)
16+
Codec.INT.optionalFieldOf("survive_time", 20 * 60 * 5).forGetter(SculkPrisonConfig::getSurviveTime),
17+
IntProvider.NON_NEGATIVE_CODEC.optionalFieldOf("ticks_until_close", ConstantIntProvider.create(SharedConstants.TICKS_PER_SECOND * 5)).forGetter(SculkPrisonConfig::getTicksUntilClose)
1418
).apply(instance, SculkPrisonConfig::new);
1519
});
1620

1721
private final PlayerConfig playerConfig;
1822
private final int lockTime;
1923
private final int surviveTime;
24+
private final IntProvider ticksUntilClose;
2025

21-
public SculkPrisonConfig(PlayerConfig playerConfig, int lockTime, int surviveTime) {
26+
public SculkPrisonConfig(PlayerConfig playerConfig, int lockTime, int surviveTime, IntProvider ticksUntilClose) {
2227
this.playerConfig = playerConfig;
2328
this.lockTime = lockTime;
2429
this.surviveTime = surviveTime;
30+
this.ticksUntilClose = ticksUntilClose;
2531
}
2632

2733
public PlayerConfig getPlayerConfig() {
@@ -35,4 +41,8 @@ public int getLockTime() {
3541
public int getSurviveTime() {
3642
return this.surviveTime;
3743
}
44+
45+
public IntProvider getTicksUntilClose() {
46+
return this.ticksUntilClose;
47+
}
3848
}

src/main/java/io/github/haykam821/sculkprison/game/phase/SculkPrisonActivePhase.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class SculkPrisonActivePhase implements PlayerAttackEntityEvent, GameActi
5050

5151
private int lockTime;
5252
private int surviveTime;
53+
private int ticksUntilClose = -1;
5354

5455
public SculkPrisonActivePhase(GameSpace gameSpace, ServerWorld world, SculkPrisonMap map, SculkPrisonConfig config, List<ServerPlayerEntity> players, GlobalWidgets widgets) {
5556
this.world = world;
@@ -104,7 +105,7 @@ public static void open(GameSpace gameSpace, ServerWorld world, SculkPrisonMap m
104105
// Listeners
105106
@Override
106107
public ActionResult onAttackEntity(ServerPlayerEntity attacker, Hand hand, Entity attacked, EntityHitResult hitResult) {
107-
if (attacker.equals(this.warden) && attacked instanceof ServerPlayerEntity) {
108+
if (!this.isGameEnding() && attacker.equals(this.warden) && attacked instanceof ServerPlayerEntity) {
108109
this.eliminate((ServerPlayerEntity) attacked, Text.translatable("text.sculkprison.eliminated.warden", attacked.getDisplayName(), attacker.getDisplayName()), true);
109110
}
110111
return ActionResult.FAIL;
@@ -125,6 +126,15 @@ public void onEnable() {
125126

126127
@Override
127128
public void onTick() {
129+
// Decrease ticks until game end to zero
130+
if (this.isGameEnding()) {
131+
if (this.ticksUntilClose == 0) {
132+
this.gameSpace.close(GameCloseReason.FINISHED);
133+
}
134+
this.ticksUntilClose -= 1;
135+
return;
136+
}
137+
128138
this.lockTime -= 1;
129139
if (this.lockTime < 0) {
130140
this.surviveTime -= 1;
@@ -165,7 +175,6 @@ public ActionResult onDeath(ServerPlayerEntity player, DamageSource source) {
165175
@Override
166176
public void onRemovePlayer(ServerPlayerEntity player) {
167177
this.eliminate(player, true);
168-
this.players.remove(player);
169178
}
170179

171180
// Utilities
@@ -189,6 +198,8 @@ private void setSpectator(ServerPlayerEntity player) {
189198
* @param remove whether to remove the player from {@link SculkPrisonActivePhase#players}
190199
*/
191200
private void eliminate(ServerPlayerEntity player, Text message, boolean remove) {
201+
if (this.isGameEnding()) return;
202+
192203
this.gameSpace.getPlayers().sendMessage(message);
193204

194205
if (remove) {
@@ -214,12 +225,16 @@ private void eliminate(ServerPlayerEntity player, boolean remove) {
214225
*/
215226
private void endWithWinner(WinTeam team) {
216227
this.gameSpace.getPlayers().sendMessage(team.getWinMessage());
217-
this.gameSpace.close(GameCloseReason.FINISHED);
228+
this.endGame();
218229
}
219230

220231
private void endWithNoWinners() {
221232
this.gameSpace.getPlayers().sendMessage(Text.translatable("text.sculkprison.no_winners").formatted(Formatting.RED));
222-
this.gameSpace.close(GameCloseReason.FINISHED);
233+
this.endGame();
234+
}
235+
236+
private void endGame() {
237+
this.ticksUntilClose = this.config.getTicksUntilClose().get(this.world.getRandom());
223238
}
224239

225240
/**
@@ -245,6 +260,10 @@ public int getLockTime() {
245260
return this.lockTime;
246261
}
247262

263+
private boolean isGameEnding() {
264+
return this.ticksUntilClose >= 0;
265+
}
266+
248267
/**
249268
* Spawns a given player within the map.
250269
* @param warden whether to use the {@linkplain SculkPrisonMap#WARDEN_SPAWN warden spawn} instead of the {@linkplain SculkPrisonMap#SPAWN default spawn}

0 commit comments

Comments
 (0)