91 lines
3.4 KiB
Java
91 lines
3.4 KiB
Java
package eu.univento.lobby.listeners;
|
|
|
|
import eu.univento.core.Core;
|
|
import eu.univento.core.api.events.MoveEventFilter;
|
|
import eu.univento.core.api.languages.Messages;
|
|
import eu.univento.core.api.player.CustomPlayer;
|
|
import eu.univento.lobby.Lobby;
|
|
import eu.univento.lobby.utils.PetManager;
|
|
import org.bukkit.*;
|
|
import org.bukkit.block.Block;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
|
|
/**
|
|
* @author joethei
|
|
* @version 0.1
|
|
*/
|
|
public class Parkour implements Listener{
|
|
|
|
static ArrayList<CustomPlayer> players = new ArrayList<>();
|
|
public static ArrayList<CustomPlayer> getPlayers() {
|
|
return players;
|
|
}
|
|
|
|
@EventHandler
|
|
public void onBlockMove(MoveEventFilter.PlayerBlockMoveEvent e) {
|
|
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
|
|
Messages msgs = new Messages(p);
|
|
Block block = p.getLocation().subtract(0.0D, 1.0D, 0.0D).getBlock();
|
|
if (block.getType() == Material.STAINED_CLAY) {
|
|
if (players.contains(p)) {
|
|
spawnRandomBlock(block.getLocation());
|
|
}else if(p.getLocation().getBlock().getType() == Material.IRON_PLATE) {
|
|
players.add(p);
|
|
p.sendMessage(msgs.PREFIX() + msgs.Lobby_PARKOUR_ON());
|
|
spawnRandomBlock(block.getLocation());
|
|
p.setWalkSpeed(0.2F);
|
|
if(PetManager.hasPet(p)) PetManager.removePet(p);
|
|
}
|
|
} else {
|
|
if (block.getType() != Material.AIR && players.contains(p) && block.getType() != Material.IRON_PLATE) {
|
|
players.remove(p);
|
|
p.sendMessage(msgs.PREFIX() + msgs.Lobby_PARKOUR_OFF());
|
|
}
|
|
}
|
|
}
|
|
|
|
void spawnRandomBlock(Location loc) {
|
|
List<Location> locations = circle(loc, 3, 1, false, false, 1);
|
|
Random r = new Random();
|
|
Block block = locations.get(r.nextInt(locations.size())).getBlock();
|
|
if(block.getType() == Material.AIR) {
|
|
Random cr = new Random();
|
|
int color = cr.nextInt(15);
|
|
block.setType(Material.STAINED_CLAY);
|
|
block.setData((byte) color);
|
|
Bukkit.getScheduler().scheduleSyncDelayedTask(Lobby.getInstance(), () -> {
|
|
block.setType(Material.AIR);
|
|
for(CustomPlayer players : Core.getOnlinePlayers()) {
|
|
players.playSound(block.getLocation(), Sound.BLOCK_NOTE_BASS, 1.0F, 1.0F);
|
|
}
|
|
}, 20L * 10);
|
|
}
|
|
else{
|
|
spawnRandomBlock(loc);
|
|
}
|
|
}
|
|
|
|
List<Location> circle (Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {
|
|
List<Location> circleblocks = new ArrayList<>();
|
|
int cx = loc.getBlockX();
|
|
int cy = loc.getBlockY();
|
|
int cz = loc.getBlockZ();
|
|
for (int x = cx - r; x <= cx +r; x++)
|
|
for (int z = cz - r; z <= cz +r; z++)
|
|
for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
|
|
double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
|
|
if (dist < r*r && !(hollow && dist < (r-1)*(r-1))) {
|
|
Location l = new Location(loc.getWorld(), x, y + plus_y, z);
|
|
circleblocks.add(l);
|
|
}
|
|
}
|
|
|
|
return circleblocks;
|
|
}
|
|
|
|
} |