Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofpu committed Sep 22, 2021
2 parents 3eb8ac5 + fc6b1f1 commit 2a01f5b
Show file tree
Hide file tree
Showing 14 changed files with 306 additions and 206 deletions.
1 change: 1 addition & 0 deletions spigot/src/main/java/me/tofpu/speedbridge/SpeedBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void onEnable() {
@Override
public void onDisable() {
// saving phase
Game.EXECUTOR.shutdown();
game().dataManager().shutdown();

// cancelling the leaderboard task
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.tofpu.speedbridge.game.process;

import me.tofpu.speedbridge.game.process.item.JoinItemProcessor;
import me.tofpu.speedbridge.game.process.processors.JoinProcessor;
import me.tofpu.speedbridge.game.process.processors.SpectatorProcessor;
import me.tofpu.speedbridge.game.process.item.SpectatorItemProcessor;
import me.tofpu.speedbridge.game.process.processors.TimerUpdateProcessor;
import me.tofpu.speedbridge.game.process.type.GameItemProcessor;
import me.tofpu.speedbridge.game.process.type.GameProcessor;

public class Process {
public static final GameProcessor GAME_JOIN = new JoinProcessor();
public static final GameProcessor GAME_SPECTATOR = new SpectatorProcessor();
public static final GameProcessor GAME_UPDATE = new TimerUpdateProcessor();

public static final GameItemProcessor ITEM_JOIN = new JoinItemProcessor();
public static final GameItemProcessor ITEM_SPECTATOR =
new SpectatorItemProcessor();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.tofpu.speedbridge.game.processor;
package me.tofpu.speedbridge.game.process;

public enum ProcessType {
PROCESS, REVERSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package me.tofpu.speedbridge.game.process.item;

import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.data.file.path.Path;
import me.tofpu.speedbridge.game.process.ProcessType;
import me.tofpu.speedbridge.game.process.type.GameItemProcessor;
import me.tofpu.speedbridge.util.XMaterial;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.Optional;

public class JoinItemProcessor extends GameItemProcessor {
@Override
public void process(final User user, final Player player,
final ProcessType type) {
final Inventory inventory = player.getInventory();
// clearing this user inventory
inventory.clear();

// if the process type is not process, we're done here
if (type != ProcessType.PROCESS) return;

// trying to get the material that matches the server version
final Optional<XMaterial> material = XMaterial.matchXMaterial(Path.SETTINGS_BLOCK.getValue());

// if the material is present
if (material.isPresent()){
// parsing the material chosen
inventory.addItem(new ItemStack(material.get().parseMaterial(), 64));
} else {
// default material
inventory.addItem(new ItemStack(XMaterial.WHITE_WOOL.parseMaterial(), 64));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package me.tofpu.speedbridge.game.process.item;

import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.game.process.ProcessType;
import me.tofpu.speedbridge.game.process.type.GameItemProcessor;
import me.tofpu.speedbridge.util.Util;
import me.tofpu.speedbridge.util.XMaterial;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class SpectatorItemProcessor extends GameItemProcessor {
@Override
public void process(final User user, final Player player,
final ProcessType type) {
final Inventory inventory = player.getInventory();
// clearing this user inventory
inventory.clear();

// if the process type is not process, we're done here
if (type != ProcessType.PROCESS) return;

final ItemStack itemStack = XMaterial.RED_DYE.parseItem();
final ItemMeta meta = itemStack.getItemMeta();
meta.setDisplayName(Util.colorize("&cLeave"));
itemStack.setItemMeta(meta);

inventory.setItem(0, itemStack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package me.tofpu.speedbridge.game.process.processors;

import me.tofpu.speedbridge.api.island.Island;
import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.game.process.ProcessType;
import me.tofpu.speedbridge.game.process.Process;
import me.tofpu.speedbridge.game.process.type.GameProcessor;
import me.tofpu.speedbridge.game.service.GameServiceImpl;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;

public class JoinProcessor extends GameProcessor {
@Override
public void process(final GameServiceImpl gameService,
final Island island, final User user,
final Player player,
final ProcessType type) {

switch (type) {
case PROCESS:
// setting the user properties island slot
// to this island's slot for tracking purposes
user.properties().islandSlot(island.slot());

// setting the island takenBy to this user
// for availability reasons
island.takenBy(user);

// teleporting player to the island's location
// MULTI-WORLD PATCH
player.teleport(island.location());

// removing their effects
player.getActivePotionEffects().clear();

// setting their gamemode to survival
player.setGameMode(GameMode.SURVIVAL);
// resetting their health back to the max health attribute
player.setHealth(player.getMaxHealth());
// resetting their food levels back to full
player.setFoodLevel(20);

// storing an instance of user & the island that
// they're in for the runnable to keep track of them
gameService.gameChecker().put(user, island);

// if the runnable is paused
if (gameService.runnable().isPaused()) {
// since the runnable is paused
// we'll start it
gameService.runnable().resume();
}
break;
case REVERSE:
// resetting the island for the next player
gameService.resetIsland(user.properties().islandSlot());
// resetting the user's properties slot to null
// since they're not playing anymore
user.properties().islandSlot(null);

// removing them from our tracker
gameService.gameTimer().remove(player.getUniqueId());
gameService.gameChecker().remove(user);

// if the gameChecker is empty
if (gameService.gameChecker().isEmpty()) {
// since no one is playing right now
// we'll stop the runnable to save up resources
gameService.runnable().pause();
}

// teleporting the player to the lobby location
player.teleport(gameService.lobbyService().getLobbyLocation());
break;
}
// processing the items to this user
Process.ITEM_JOIN.process(user, player, type);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package me.tofpu.speedbridge.game.processor.game;
package me.tofpu.speedbridge.game.process.processors;

import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.data.file.path.Path;
import me.tofpu.speedbridge.game.processor.ProcessType;
import me.tofpu.speedbridge.game.processor.type.GameProcessor;
import me.tofpu.speedbridge.game.processor.Processor;
import me.tofpu.speedbridge.game.process.ProcessType;
import me.tofpu.speedbridge.game.process.type.GameProcessor;
import me.tofpu.speedbridge.game.process.Process;
import me.tofpu.speedbridge.game.service.GameServiceImpl;
import me.tofpu.speedbridge.util.Util;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -95,6 +95,6 @@ public void process(final GameServiceImpl gameService,
}

// item processor
Processor.ITEM_SPECTATOR.process(spectator, spectatorPlayer, type);
Process.ITEM_SPECTATOR.process(spectator, spectatorPlayer, type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package me.tofpu.speedbridge.game.process.processors;

import me.tofpu.speedbridge.api.island.Island;
import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.api.user.UserProperties;
import me.tofpu.speedbridge.api.user.timer.Timer;
import me.tofpu.speedbridge.data.file.path.Path;
import me.tofpu.speedbridge.game.process.ProcessType;
import me.tofpu.speedbridge.game.process.type.GameProcessor;
import me.tofpu.speedbridge.game.service.GameServiceImpl;
import me.tofpu.speedbridge.util.Util;
import org.bukkit.entity.Player;

public class TimerUpdateProcessor extends GameProcessor {
@Override
public void process(final GameServiceImpl gameService, final User user, final Player player, final ProcessType type) {
// if the process type is not process, no need to continue
if (type != ProcessType.PROCESS) return;

// getting an instance of timer that were
// associated with this users unique id
final Timer gameTimer = gameService.gameTimer().get(user.uniqueId());
// temporally instance of user's properties for ease of use
final UserProperties properties = user.properties();
// temporally instance of user's timer for ease of use
final Timer lowestTimer = properties.timer();

// resetting the island state
gameService.reset(user);

// ending the timer with the current system millis second
gameTimer.end(System.currentTimeMillis());

// messaging this player that they scored
Util.message(player, Path.MESSAGES_SCORED, new String[]{"%scored%"}, gameTimer.result() + "");

// notifying the spectators
gameService.messageSpectator(user,
Util.WordReplacer.replace(Path.MESSAGES_SPECTATOR_SCORED.getValue(), new String[]{"%player%", "%scored%"}, player.getName(), gameTimer.result() + ""), false);

// checking if the player has a personal best record
// and if the timer was higher than the player's best record
if (lowestTimer != null && lowestTimer.result() <= gameTimer.result()) {
// since the timer was higher than the player's best record
// sending a message saying they've not beaten their best score

// messaging the player
Util.message(player, Path.MESSAGES_NOT_BEATEN, new String[]{"%score%"}, lowestTimer.result() + "");
} else {
// if the player has personal best score
if (lowestTimer != null) {
// since they do, we are calculating the player's best record
// subtracting with the current timer and sending it to them
final String result = String.format("%.03f", lowestTimer.result() - gameTimer.result());

// messaging this player that they have beaten their score
Util.message(player, Path.MESSAGES_BEATEN_SCORE, new String[]{"%calu_score%"},
result);

// notifying the target & spectators
gameService.messageSpectator(user,
Util.WordReplacer.replace(Path.MESSAGES_SPECTATOR_BEATEN_SCORE.getValue(), new String[]{"%player%", "%calu_score%"}, player.getName(), result), true);
}

// replacing the old record with the player's current record
properties.timer(gameTimer);

// manually triggering the leaderboard
gameService.leaderboardService().check(user, null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.tofpu.speedbridge.game.process.type;

import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.game.process.ProcessType;
import org.bukkit.entity.Player;

public abstract class GameItemProcessor {
public void process(final User user,
final Player player,
final ProcessType type) {}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package me.tofpu.speedbridge.game.processor.type;
package me.tofpu.speedbridge.game.process.type;

import me.tofpu.speedbridge.api.island.Island;
import me.tofpu.speedbridge.api.user.User;
import me.tofpu.speedbridge.game.processor.ProcessType;
import me.tofpu.speedbridge.game.process.ProcessType;
import me.tofpu.speedbridge.game.service.GameServiceImpl;
import org.bukkit.Location;
import org.bukkit.entity.Player;

public abstract class GameProcessor {
public void process(final User user, final ProcessType type) {
process(user, user.player(), type);
}

public void process(final User user, final Player player,
public void process(final GameServiceImpl gameService,
final User user,
final Player player,
final ProcessType type) {}

public void process(final GameServiceImpl gameService, final User user,
final Player player, Island island,
final ProcessType type) {}
public void process(final GameServiceImpl gameService,
final Island island, final User user,
final Player player,
final ProcessType type) {}

public void process(final GameServiceImpl gameService,
final Location location,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2a01f5b

Please sign in to comment.