Skip to content

Commit

Permalink
Merge pull request #9 from NovaUniverse/branch
Browse files Browse the repository at this point in the history
clean up, refactoring, adding double jump item and cooldown
  • Loading branch information
AntonUden authored Sep 26, 2024
2 parents f7d7e7e + 640db2d commit 96558b3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 61 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/bin/
/target/
/.idea
NovaTNTRun.iml
13 changes: 13 additions & 0 deletions NovaTNTRun.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>SPIGOT</platformType>
<platformType>BUKKIT</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
</module>
17 changes: 17 additions & 0 deletions src/net/novauniverse/games/tntrun/NovaTNTRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import net.novauniverse.games.tntrun.game.misc.DoubleJumpCharges;
import net.zeeraa.novacore.spigot.module.modules.cooldown.CooldownManager;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -93,6 +99,17 @@ public boolean isShouldGiveSnowballs() {
return shouldGiveSnowballs;
}

public void doubleJump(Player player) {
player.setVelocity(player.getLocation().getDirection().multiply(TNTRun.DOUBLE_JUMP_POWER).setY(TNTRun.DOUBLE_JUMP_Y));
player.playSound(player.getLocation(), Sound.GHAST_FIREBALL, 1F, 1F);
Location pLocation = player.getLocation();
pLocation.add(0.0, 1.5, 0.0);
for (int i = 0; i <= 2; i++) {
player.getWorld().playEffect(pLocation.clone().add(0, -1, 0), Effect.SMOKE, i);
}
CooldownManager.get().set(player, TNTRun.DOUBLE_JUMP_COOLDOWN_ID, TNTRun.DOUBLE_JUMP_COOLDOWN);
}

@Override
public void onEnable() {
NovaTNTRun.instance = this;
Expand Down
131 changes: 70 additions & 61 deletions src/net/novauniverse/games/tntrun/game/TNTRun.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Map;
import java.util.UUID;

import net.zeeraa.novacore.spigot.module.modules.cooldown.Cooldown;
import net.zeeraa.novacore.spigot.module.modules.cooldown.CooldownManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
Expand Down Expand Up @@ -34,11 +36,13 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerToggleFlightEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.scheduler.BukkitRunnable;

Expand Down Expand Up @@ -71,12 +75,15 @@ public class TNTRun extends MapGame implements Listener {

public static final int DOUBLE_JUMP_CHARGES = 5;

public static final int DOUBLE_JUMP_COOLDOWN = 15;

public static final String DOUBLE_JUMP_COOLDOWN_ID = "DOUBLE_JUMP_COOLDOWN";

private TNTRunMapModule config;

private Task gameLoop;
private Task moveCheckTask;

private List<UUID> doubleJumpInProgress;
private ItemStack doubleJumpItem;

private Map<UUID, DoubleJumpCharges> doubleJumpCharges;

Expand All @@ -89,31 +96,21 @@ public TNTRun() {
this.started = false;
this.ended = false;
this.config = null;
this.doubleJumpInProgress = new ArrayList<UUID>();
this.doubleJumpCharges = new HashMap<UUID, DoubleJumpCharges>();
this.playerMovementCheck = new HashMap<UUID, PlayerStandingStillCheck>();
this.doubleJumpCharges = new HashMap<>();
this.playerMovementCheck = new HashMap<>();
this.doubleJumpItem = new ItemBuilder(Material.FEATHER).setName(ChatColor.GREEN + "Click to Double Jump").build();

this.moveCheckTask = new SimpleTask(NovaTNTRun.getInstance(), new Runnable() {
@Override
public void run() {
playerMovementCheck.values().forEach(o -> o.decrement());
}
}, 1L);
this.moveCheckTask = new SimpleTask(NovaTNTRun.getInstance(), () -> playerMovementCheck.values().forEach(PlayerStandingStillCheck::decrement), 1L);

this.actionbarTask = new SimpleTask(NovaTNTRun.getInstance(), new Runnable() {
@Override
public void run() {
Bukkit.getServer().getOnlinePlayers().forEach(player -> {
if (players.contains(player.getUniqueId())) {
if (doubleJumpCharges.containsKey(player.getUniqueId())) {
DoubleJumpCharges charges = doubleJumpCharges.get(player.getUniqueId());
String message = ChatColor.GOLD + "" + ChatColor.BOLD + "Double jump charges: " + (charges.hasCharges() ? ChatColor.AQUA : ChatColor.RED) + ChatColor.BOLD + charges.getCharges();
VersionIndependentUtils.get().sendActionBarMessage(player, message);
}
}
});
}
}, 10L);
this.actionbarTask = new SimpleTask(NovaTNTRun.getInstance(), () -> Bukkit.getServer().getOnlinePlayers().forEach(player -> {
if (players.contains(player.getUniqueId())) {
if (doubleJumpCharges.containsKey(player.getUniqueId())) {
DoubleJumpCharges charges = doubleJumpCharges.get(player.getUniqueId());
String message = ChatColor.GOLD + "" + ChatColor.BOLD + "Double jump charges: " + (charges.hasCharges() ? ChatColor.AQUA : ChatColor.RED) + ChatColor.BOLD + charges.getCharges();
VersionIndependentUtils.get().sendActionBarMessage(player, message);
}
}
}), 10L);
}

public boolean isStandingStill(Player player) {
Expand Down Expand Up @@ -181,6 +178,7 @@ public void tpToSpectator(Player player) {
NovaCore.getInstance().getVersionIndependentUtils().resetEntityMaxHealth(player);
player.setHealth(20);
player.setGameMode(GameMode.SPECTATOR);
player.getInventory().clear();
if (hasActiveMap()) {
player.teleport(getActiveMap().getSpectatorLocation());
}
Expand Down Expand Up @@ -241,7 +239,7 @@ public void tryStartDecay() {
if (!started) {
return;
}
TNTRunMapModule module = (TNTRunMapModule) this.getActiveMap().getMapData().getMapModule(TNTRunMapModule.class);
TNTRunMapModule module = this.getActiveMap().getMapData().getMapModule(TNTRunMapModule.class);
module.startDecay();
}

Expand All @@ -254,7 +252,7 @@ public void onStart() {

world.setDifficulty(Difficulty.PEACEFUL);

TNTRunMapModule cfg = (TNTRunMapModule) this.getActiveMap().getMapData().getMapModule(TNTRunMapModule.class);
TNTRunMapModule cfg = this.getActiveMap().getMapData().getMapModule(TNTRunMapModule.class);
if (cfg == null) {
Log.fatal("TNTRun", "The map " + this.getActiveMap().getMapData().getMapName() + " has no tntrun config map module");
Bukkit.getServer().broadcastMessage(ChatColor.RED + "TNTRun has run into an uncorrectable error and has to be ended");
Expand All @@ -263,7 +261,7 @@ public void onStart() {
}
this.config = cfg;

List<Player> toTeleport = new ArrayList<Player>();
List<Player> toTeleport = new ArrayList<>();

Bukkit.getServer().getOnlinePlayers().forEach(player -> {
if (players.contains(player.getUniqueId())) {
Expand All @@ -275,40 +273,31 @@ public void onStart() {

Collections.shuffle(toTeleport, getRandom());

List<Location> toUse = new ArrayList<Location>();
while (toTeleport.size() > 0) {
if (toUse.size() == 0) {
List<Location> toUse = new ArrayList<>();
while (!toTeleport.isEmpty()) {
if (toUse.isEmpty()) {
for (Location location : getActiveMap().getStarterLocations()) {
toUse.add(location);
}

Collections.shuffle(toUse, getRandom());
}

if (toUse.size() == 0) {
if (toUse.isEmpty()) {
// Could not load spawn locations. break out to prevent server from crashing
Log.fatal("TNTRun", "The map " + this.getActiveMap().getMapData().getMapName() + " has no spawn locations. Ending game to prevent crash");
Bukkit.getServer().broadcastMessage(ChatColor.RED + "TNTRun has run into an uncorrectable error and has to be ended");
this.endGame(GameEndReason.ERROR);
return;
}

tpToArena(toTeleport.remove(0), toUse.remove(0));
}

// Disable drops
this.getActiveMap().getWorld().setGameRuleValue("doTileDrops", "false");

gameLoop = new SimpleTask(new Runnable() {
@Override
public void run() {
Bukkit.getServer().getOnlinePlayers().forEach(player -> {
player.setFoodLevel(20);
player.setSaturation(20);
});
}
}, 20L);
gameLoop.start();
// Peaceful Mode prevents hunger
this.getActiveMap().getWorld().setDifficulty(Difficulty.PEACEFUL);


Task.tryStartTask(actionbarTask);
Task.tryStartTask(moveCheckTask);
Expand All @@ -322,8 +311,6 @@ public void onEnd(GameEndReason reason) {
return;
}

Task.tryStopTask(gameLoop);

getActiveMap().getStarterLocations().forEach(location -> {
Firework fw = (Firework) location.getWorld().spawnEntity(location, EntityType.FIREWORK);
FireworkMeta fwm = fw.getFireworkMeta();
Expand Down Expand Up @@ -355,7 +342,7 @@ public void onEnd(GameEndReason reason) {
public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
if (e.getEntity() instanceof Player) {

if (e.getDamager().getType() == EntityType.SNOWBALL || e.getEntityType() == EntityType.EGG) {
if (e.getDamager().getType() == EntityType.SNOWBALL || e.getDamager().getType() == EntityType.EGG) {
return;
}
}
Expand Down Expand Up @@ -394,7 +381,7 @@ public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
Player player = event.getPlayer();
if (player.getGameMode() == GameMode.ADVENTURE || player.getGameMode() == GameMode.SURVIVAL) {
player.setFlying(false);
if (!doubleJumpInProgress.contains(player.getUniqueId())) {
if (!CooldownManager.get().isActive(player.getUniqueId(), DOUBLE_JUMP_COOLDOWN_ID)) {
boolean allow = false;
if (started) {
if (doubleJumpCharges.containsKey(player.getUniqueId())) {
Expand All @@ -405,17 +392,11 @@ public void onPlayerToggleFlight(PlayerToggleFlightEvent event) {
}
}
} else {
// allow double jumping in lobby
allow = true;
}

if (allow) {
player.setVelocity(player.getLocation().getDirection().multiply(DOUBLE_JUMP_POWER).setY(DOUBLE_JUMP_Y));
player.playSound(player.getLocation(), Sound.GHAST_FIREBALL, 1F, 1F);
Location pLocation = player.getLocation();
pLocation.add(0.0, 1.5, 0.0);
for (int i = 0; i <= 2; i++) {
player.getWorld().playEffect(pLocation.clone().add(0, -1, 0), Effect.SMOKE, i);
}
NovaTNTRun.getInstance().doubleJump(player);
}
}
player.setAllowFlight(false);
Expand All @@ -431,7 +412,7 @@ public void onPlayerMove(PlayerMoveEvent event) {
Location loc = player.getLocation();
Block block = loc.getBlock().getRelative(BlockFace.DOWN);
if (block.getType() != Material.AIR && block.getType().isSolid()) {
if (!doubleJumpInProgress.contains(player.getUniqueId())) {
if (!CooldownManager.get().isActive(player.getUniqueId(), DOUBLE_JUMP_COOLDOWN_ID)) {
boolean allow = false;

if (started) {
Expand All @@ -456,10 +437,6 @@ public void onPlayerMove(PlayerMoveEvent event) {

@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
if (doubleJumpInProgress.contains(e.getPlayer().getUniqueId())) {
doubleJumpInProgress.remove(e.getPlayer().getUniqueId());
}

if (playerMovementCheck.containsKey(e.getPlayer().getUniqueId())) {
this.playerMovementCheck.remove(e.getPlayer().getUniqueId());
}
Expand All @@ -471,8 +448,13 @@ public void onPlayerJoin(PlayerJoinEvent e) {
if (hasStarted()) {
if (!players.contains(e.getPlayer().getUniqueId())) {
tpToSpectator(e.getPlayer());
} else {
CooldownManager.get().setupPlayer(e.getPlayer());
e.getPlayer().getInventory().setItem(0, doubleJumpItem);
}
} else {
CooldownManager.get().setupPlayer(e.getPlayer());
e.getPlayer().getInventory().setItem(0, doubleJumpItem);
e.getPlayer().sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "You can practise double jumping while you wait for the game to start");
}
}
Expand Down Expand Up @@ -536,4 +518,31 @@ public void onBlockBreak(BlockBreakEvent e) {
}
}
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onInteract(PlayerInteractEvent e) {
if (hasStarted()) {
if (e.getPlayer().getGameMode() != GameMode.CREATIVE) {
if (e.getItem().isSimilar(doubleJumpItem)) {
if (!CooldownManager.get().isActive(e.getPlayer().getUniqueId(), DOUBLE_JUMP_COOLDOWN_ID)) {
boolean allow = false;
if (started) {
if (doubleJumpCharges.containsKey(e.getPlayer().getUniqueId())) {
DoubleJumpCharges charges = doubleJumpCharges.get(e.getPlayer().getUniqueId());
if (charges.hasCharges()) {
charges.decrement();
allow = true;
}
}
} else {
// allow double jumping in lobby
allow = true;
}
if (allow) {
NovaTNTRun.getInstance().doubleJump(e.getPlayer());
}
}
}
}
}
}
}

0 comments on commit 96558b3

Please sign in to comment.