Skip to content

Commit

Permalink
Refactor Vote and FastForward
Browse files Browse the repository at this point in the history
  • Loading branch information
mattboy9921 committed Aug 13, 2023
1 parent 8fa5d47 commit bd4d2b2
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 509 deletions.
664 changes: 180 additions & 484 deletions src/main/java/net/mattlabs/skipnight/Vote.java

Large diffs are not rendered by default.

52 changes: 48 additions & 4 deletions src/main/java/net/mattlabs/skipnight/Voter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@

public class Voter {

private UUID uuid;
private enum Status {
ACTIVE,
BED,
IDLE,
AWAY;
}

private final UUID uuid;
private int vote;

private Status status;

Voter(UUID uuid) {
this.uuid = uuid;
vote = 0;
status = null;
}

UUID getUuid() {
return uuid;
}

int getVote() {
Expand All @@ -24,10 +38,40 @@ void voteNo() {
vote = -1;
}

int resetVote() {
int vote = this.vote;
void resetVote() {
this.vote = 0;
return vote;
}

boolean isActive() {
return status == Status.ACTIVE;
}

boolean isBed() {
return status == Status.BED;
}

boolean isIdle() {
return status == Status.IDLE;
}

boolean isAway() {
return status == Status.AWAY;
}

void setActive() {
status = Status.ACTIVE;
}

void setBed() {
status = Status.BED;
}

void setIdle() {
status = Status.IDLE;
}

void setAway() {
status = Status.AWAY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onYes(CommandSender sender) {
}
else {
Player player = (Player) sender;
this.vote.addYes(player.getUniqueId(), VoteType.DAY);
this.vote.addYes(player, VoteType.DAY);
}
}

Expand All @@ -52,7 +52,7 @@ public void onNo(CommandSender sender) {
}
else {
Player player = (Player) sender;
this.vote.addNo(player.getUniqueId(), VoteType.DAY);
this.vote.addNo(player, VoteType.DAY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void onYes(CommandSender sender) {
}
else {
Player player = (Player) sender;
this.vote.addYes(player.getUniqueId(), VoteType.NIGHT);
this.vote.addYes(player, VoteType.NIGHT);
}
}

Expand All @@ -52,7 +52,7 @@ public void onNo(CommandSender sender) {
}
else {
Player player = (Player) sender;
this.vote.addNo(player.getUniqueId(), VoteType.NIGHT);
this.vote.addNo(player, VoteType.NIGHT);
}
}
}
21 changes: 4 additions & 17 deletions src/main/java/net/mattlabs/skipnight/util/FastForward.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,19 @@ public class FastForward implements Runnable {
private World world;
private Plugin plugin;
private VoteType voteType;
private boolean storm;

public FastForward(World world, Plugin plugin, VoteType voteType) {
this.world = world;
this.plugin = plugin;
this.voteType = voteType;
storm = voteType == VoteType.NIGHT && (world.getTime() > 23900 || world.getTime() < 12516) && world.hasStorm();
}

@Override
public void run() {
long totalTime = voteType == VoteType.DAY ? 12541 - world.getTime() : 24000 - world.getTime();
world.setTime(world.getTime() + 80);

// Fast-forward day to night with storm
if (voteType == VoteType.NIGHT && (world.getTime() > 23900 || world.getTime() < 12516) && storm) {
world.setStorm(true);
world.setWeatherDuration(1000);
plugin.getServer().getScheduler().runTaskLater(plugin, this, 1);
}
// Disable storm once it is night
if (voteType == VoteType.NIGHT && (world.getTime() > 12516 && world.getTime() < 23900) && storm) {
world.setStorm(false);
storm = false;
}

if (voteType == VoteType.NIGHT && (world.getTime() > 12516 && world.getTime() < 23900)) plugin.getServer().getScheduler().runTaskLater(plugin, this, 1);
if (voteType == VoteType.DAY && (world.getTime() > 23900 || world.getTime() < 12516)) plugin.getServer().getScheduler().runTaskLater(plugin, this, 1);
totalTime -= 80;
if (totalTime < 80 && (voteType == VoteType.NIGHT && world.hasStorm())) world.setStorm(false);
if (totalTime > 0) plugin.getServer().getScheduler().runTaskLater(plugin, this, 1);
}
}
1 change: 1 addition & 0 deletions src/test/java/net/mattlabs/skipnight/NightVoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public void voteBedEnterDuringVote() {
// Second player places bed and sleeps
Block bed = player2.simulateBlockPlace(Material.RED_BED, player2.getLocation()).getBlockPlaced();
Future<?> future = server.getScheduler().executeAsyncEvent(new PlayerBedEnterEvent(player2, bed, PlayerBedEnterEvent.BedEnterResult.OK));
player2.setSleeping(true);

// Wait for event to execute
while (!future.isDone()) server.getScheduler().performOneTick();
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/net/mattlabs/skipnight/VoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ public void voteCooldown() {
plain.serialize(player1.nextComponentMessage())
);

// Wait out cooldown
server.getScheduler().performTicks(100 * 20);

// First player starts vote again
server.execute("skip" + voteType, player1).assertSucceeded();

Assertions.assertEquals(
plain.serialize(plugin.getMessages().duringVote().voteStarted(player1.getName(), voteType)),
plain.serialize(player1.nextComponentMessage())
);

// Make sure time does not fast-forward
Assertions.assertTrue(world.getTime() > startTime || world.getTime() < endTime);
}
Expand Down

0 comments on commit bd4d2b2

Please sign in to comment.