Skip to content

Commit

Permalink
So I don't forget what they do or come from next time I need to chang…
Browse files Browse the repository at this point in the history
…e stuff
  • Loading branch information
Fesaa committed Dec 24, 2023
1 parent 0159282 commit 5d85836
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 10 deletions.
25 changes: 24 additions & 1 deletion core/src/main/java/org/cubepanion/core/events/GameEndEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,62 @@
import net.labymod.api.event.Event;
import org.cubepanion.core.utils.CubeGame;

/**
* Fired when a CubeGame ends, if possible.
* May be fired due to winning, leaving the game, or quiting the server.
*/
public class GameEndEvent implements Event {

private final CubeGame game;

private final long gameStartTime;
private final boolean won;
private final boolean switchedServer;
private final long gameDuration;

public GameEndEvent(CubeGame game, boolean won, boolean switchedServer, long gameStartTime) {
this.game = game;
this.gameStartTime = gameStartTime;
this.won = won;
this.switchedServer = switchedServer;
this.gameDuration = System.currentTimeMillis() - this.gameStartTime;
}

/**
* @return The game that has ended
*/
public CubeGame getGame() {
return game;
}

/**
* @return The unix time when the game started
*/
public long getGameStartTime() {
return gameStartTime;
}

/**
* @return If the player won
*/
public boolean hasWon() {
return won;
}

/**
* Check this before you do anything that should happen on the server in which
* the game was played. (Send chat message, ...)
* @return Whether the player has already switched servers
*/
public boolean hasSwitchedServer() {
return switchedServer;
}

/**
* @return Total play time
*/
public long getGameDuration() {
return System.currentTimeMillis() - gameStartTime;
return gameDuration;
}

}
22 changes: 20 additions & 2 deletions core/src/main/java/org/cubepanion/core/events/GameUpdateEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,52 @@
import net.labymod.api.event.Event;
import org.cubepanion.core.utils.CubeGame;

/**
* Called when the division updates, or the game starts. GameEnds should be listened to with the
* GameEndEvent
*/
public class GameUpdateEvent implements Event {

private final CubeGame origin;

private final CubeGame destination;

private final boolean preLobby;
private final boolean isSwitch;

public GameUpdateEvent(CubeGame origin, CubeGame destination, boolean preLobby) {
public GameUpdateEvent(CubeGame origin, CubeGame destination, boolean preLobby, boolean isSwitch) {
this.origin = origin;
this.destination = destination;
this.preLobby = preLobby;
this.isSwitch = isSwitch;
}

/**
* @return Last division
*/
public CubeGame getOrigin() {
return origin;
}

/**
* @return Current division
*/
public CubeGame getDestination() {
return destination;
}

/**
* @return Game state
*/
public boolean isPreLobby() {
return preLobby;
}

/**
* @return Whether this change is due to a server switch
*/
public boolean isSwitch() {
return origin != destination;
return isSwitch;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import net.labymod.api.event.Event;

/**
* Called when the player dies, or kills someone (the event is stripped from chat)
*/
public class PlayerDeathEvent implements Event {

private final boolean isClientPlayer;
Expand All @@ -16,14 +19,25 @@ public PlayerDeathEvent(boolean isClientPlayer, String killer, String killed) {
this.killed = killed;
}

/**
* @return If the client player died
*/
public boolean isClientPlayer() {
return isClientPlayer;
}

/**
* This may be a non player, if isClientPlayer is True
* Such as; `void`, `leave`, `tnt`, etc.
* @return Who made the kill
*/
public String getKiller() {
return killer;
}

/**
* @return Person who died
*/
public String getKilled() {
return killed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import net.labymod.api.event.Event;

/**
* Called when a player is eliminated. This is only called in EggWars
* PlayerDeathEvent is still called in EggWars (the event is stripped from chat)
*/
public class PlayerEliminationEvent implements Event {

private final boolean isClientPlayer;
Expand All @@ -13,10 +17,16 @@ public PlayerEliminationEvent(boolean isClientPlayer, String name) {
this.name = name;
}

/**
* @return If the client player died
*/
public boolean isClientPlayer() {
return isClientPlayer;
}

/**
* @return Name of the eliminated player
*/
public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import java.util.UUID;
import net.labymod.api.event.Event;

/**
* Called when a player respawn (This is equivalent to a gamemode changes to survival)
* Or the respawn message in case of the client player.
* Look Out! This means that the client player has two RespawnEvents in EggWars per respawn.
*/
public class PlayerRespawnEvent implements Event {

private final boolean isClientPlayer;
Expand All @@ -13,10 +18,16 @@ public PlayerRespawnEvent(boolean isClientPlayer, UUID uuid) {
this.uuid = uuid;
}

/**
* @return If the client player is respawning
*/
public boolean isClientPlayer() {
return this.isClientPlayer;
}

/**
* @return The uuid of the respawning player
*/
public UUID getUUID() {
return this.uuid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import net.labymod.api.event.Event;

/**
* Internal event used for requested things in other classes
*/
public class RequestEvent implements Event {

private final RequestType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public void onChatMessage(ChatReceiveEvent e) {
m.setInPreLobby(false);
m.setGameStartTime(System.currentTimeMillis());

GameUpdateEvent event = new GameUpdateEvent(m.getDivision(), m.getDivision(), false);
GameUpdateEvent event = new GameUpdateEvent(
m.getDivision(),
m.getDivision(),
false,
false);
Laby.fireEvent(event);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void onSessionUpdate(SessionUpdateEvent e) {
private void query() {
ClientPlayer p = Laby.labyAPI().minecraft().getClientPlayer();
if (p == null) {
LOGGER.info(getClass(), "FUCK");
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,22 @@ public void onChatReceiveEvent(ChatReceiveEvent e) {
String userName = SessionTracker.get().username();

if (msg.equals("Congratulations, you win!")) {
Laby.fireEvent(
new GameEndEvent(manager.getDivision(), true, false, manager.getGameStartTime()));
GameEndEvent event = new GameEndEvent(
manager.getDivision(),
true,
false,
manager.getGameStartTime());
Laby.fireEvent(event);
manager.setWon(true);
return;
}

Matcher eliminationMatcher = this.playerElimination.matcher(msg);
if (eliminationMatcher.matches()) {
String eliminatedPlayer = eliminationMatcher.group(1);
Laby.fireEvent(
new PlayerEliminationEvent(userName.equals(eliminatedPlayer), eliminatedPlayer));
boolean isClient = userName.equals(eliminatedPlayer);
PlayerEliminationEvent event = new PlayerEliminationEvent(isClient, eliminatedPlayer);
Laby.fireEvent(event);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ public void setDivision(CubeGame division) {
this.gameStartTime = System.currentTimeMillis();
}

GameUpdateEvent e = new GameUpdateEvent(this.lastDivision, this.division, this.inPreLobby);
GameUpdateEvent e = new GameUpdateEvent(
this.lastDivision,
this.division,
this.inPreLobby,
false);
Laby.fireEvent(e);
}

Expand Down

0 comments on commit 5d85836

Please sign in to comment.