Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.block.BlockState;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.entity.Snowball;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.entity.EntityDamageByEntityEvent;
- import org.bukkit.event.entity.ProjectileHitEvent;
- import org.bukkit.event.player.PlayerInteractEvent;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.scheduler.BukkitRunnable;
- import org.bukkit.scheduler.BukkitTask;
- import org.bukkit.util.BlockIterator;
- import me.wazup.partygames.Cuboid;
- import me.wazup.partygames.Enums.PlayCounter;
- import me.wazup.partygames.PartyGames;
- import me.wazup.partygames.minigames.MiniGame;
- import me.wazup.partygames.minigames.MiniGameMap;
- public class Spleef extends MiniGame {
- String prefix = ChatColor.DARK_GREEN + "[Spleef] " + ChatColor.GREEN;
- String[] tutorialMessages;
- int roundDuration;
- ItemStack shovel, snowball;
- boolean giveSnowballs;
- Addon plugin;
- public Spleef(Addon plugin) {
- this.plugin = plugin;
- }
- @Override
- public String getName() {
- return "Spleef";
- }
- @Override
- public Material getRepresentingMaterial() {
- return Material.DIAMOND_SPADE;
- }
- @Override
- public String[] getTutorialMessages() {
- return tutorialMessages;
- }
- @Override
- public void handleSetupCommand(Player p, String[] args) {
- if(args.length == 0) {
- p.sendMessage(ChatColor.DARK_GREEN + " -------- " + ChatColor.GREEN + getName() + " Commands " + ChatColor.DARK_GREEN + " -------- ");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "CreateMap <Name>" + ChatColor.RED + " - Requires selection");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "DeleteMap <Name>");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "SetSpawn <Map>");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "SetFloor <Map>" + ChatColor.RED + " - Requires selection");
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + "List");
- return;
- }
- String subCommand = args[0].toLowerCase();
- if(subCommand.equals("createmap")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "There is already a map with that name!");
- return;
- }
- Cuboid cuboid = PartyGames.api.getPlayerSelection(p);
- if(cuboid == null) {
- p.sendMessage(prefix + "You must select the 2 corners of the map first! Use '/pg wand' to receive the selection wand!");
- return;
- }
- PartyGames.api.removePlayerSelection(p);
- maps.put(mapName.toLowerCase(), new SpleefMap(mapName, cuboid, null, null));
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName + ".Map-Cuboid", cuboid.toString());
- saveConfiguration(config);
- p.sendMessage(prefix + "Map has been created successfully!");
- return;
- }
- if(subCommand.equals("deletemap")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(!maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "Could not find a map with that name!");
- return;
- }
- maps.remove(mapName.toLowerCase());
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName, null);
- saveConfiguration(config);
- p.sendMessage(prefix + "Map has been removed successfully!");
- return;
- }
- if(subCommand.equals("setspawn")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(!maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "Could not find a map with that name!");
- return;
- }
- ((SpleefMap) maps.get(mapName.toLowerCase())).spawnpoint = p.getLocation();
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName + ".Spawnpoint", PartyGames.plugin.getStringFromLocation(p.getLocation(), true));
- saveConfiguration(config);
- p.sendMessage(prefix + "Map spawnpoint has been set successfully!");
- return;
- }
- if(subCommand.equals("setfloor")) {
- if(args.length == 1) {
- p.sendMessage(prefix + "You must specify a map name!");
- return;
- }
- String mapName = args[1];
- if(!maps.containsKey(mapName.toLowerCase())) {
- p.sendMessage(prefix + "Could not find a map with that name!");
- return;
- }
- Cuboid cuboid = PartyGames.api.getPlayerSelection(p);
- if(cuboid == null) {
- p.sendMessage(prefix + "You must select the 2 corners of the floor first! Use '/pg wand' to receive the selection wand!");
- return;
- }
- PartyGames.api.removePlayerSelection(p);
- ((SpleefMap) maps.get(mapName.toLowerCase())).floorCuboid = cuboid;
- FileConfiguration config = getConfiguration();
- config.set("Maps." + mapName + ".Floor-Cuboid", cuboid.toString());
- saveConfiguration(config);
- p.sendMessage(prefix + "Floor has been set successfully!");
- return;
- }
- if(subCommand.equals("list")) {
- if(maps.isEmpty()) {
- p.sendMessage(prefix + "No maps are loaded!");
- return;
- }
- p.sendMessage(ChatColor.DARK_GREEN + " -------- " + ChatColor.GREEN + getName() + " Maps " + ChatColor.DARK_GREEN + " -------- ");
- for(MiniGameMap map: maps.values()) {
- p.sendMessage(ChatColor.DARK_GREEN + " - " + ChatColor.GREEN + map.name + ChatColor.DARK_GREEN + " - Is Setup: " + ChatColor.GREEN + map.isSetup());
- }
- return;
- }
- p.sendMessage(prefix + "Unknown command!");
- }
- @Override
- public void load() {
- maps = new HashMap<String, MiniGameMap>();
- FileConfiguration config = getConfiguration();
- boolean save = false;
- if(!config.contains("Tutorial-Messages")) {
- config.set("Tutorial-Messages", Arrays.asList(new String[] {
- "&2 --------- &aSpleef &2--------- ",
- "- Use your shovel to break blocks below",
- "other players to eliminate them"
- }));
- save = true;
- }
- if(!config.contains("Shovel")) {
- config.set("Shovel", "DIAMOND_SPADE : 1 : enchant:DIG_SPEED:4");
- save = true;
- }
- if(!config.contains("Round-Duration")) {
- config.set("Round-Duration", 90);
- save = true;
- }
- if(!config.contains("Give-Snowballs-On-Break")) {
- config.set("Give-Snowballs-On-Break", true);
- save = true;
- }
- if(save) saveConfiguration(config);
- List<String> temp = config.getStringList("Tutorial-Messages");
- tutorialMessages = new String[temp.size()];
- for(int i = 0; i < tutorialMessages.length; i++) tutorialMessages[i] = ChatColor.translateAlternateColorCodes('&', temp.get(i));
- if(config.getConfigurationSection("Maps") != null && !config.getConfigurationSection("Maps").getKeys(false).isEmpty()) {
- for(String mapName: config.getConfigurationSection("Maps").getKeys(false)) {
- Cuboid cuboid = new Cuboid(config.getString("Maps." + mapName + ".Map-Cuboid"));
- Cuboid floorCuboid = config.contains("Maps." + mapName + ".Floor-Cuboid") ? new Cuboid(config.getString("Maps." + mapName + ".Floor-Cuboid")) : null;
- Location spawnpoint = config.contains("Maps." + mapName + ".Spawnpoint") ? PartyGames.plugin.getLocationFromString(config.getString("Maps." + mapName + ".Spawnpoint")) : null;
- maps.put(mapName.toLowerCase(), new SpleefMap(mapName, cuboid, floorCuboid, spawnpoint));
- }
- }
- shovel = PartyGames.api.getItemStackFromString(config.getString("Shovel"));
- roundDuration = config.getInt("Round-Duration");
- snowball = new ItemStack(Material.SNOW_BALL);
- giveSnowballs = config.getBoolean("Give-Snowballs-On-Break");
- }
- @Override
- public int getAliveCount(String mapName) {
- SpleefMap map = (SpleefMap) maps.get(mapName.toLowerCase());
- return map.alive.size();
- }
- private class SpleefMap extends MiniGameMap implements Listener {
- Cuboid mainCuboid;
- Cuboid floorCuboid;
- Location spawnpoint;
- ArrayList<String> alive;
- String[] topPlayers;
- HashMap<Location, BlockState> rollback;
- BukkitTask task;
- boolean allowBreaking;
- public SpleefMap(String name, Cuboid mainCuboid, Cuboid floorCuboid, Location spawnpoint) {
- this.name = name;
- this.mainCuboid = mainCuboid;
- this.floorCuboid = floorCuboid;
- this.spawnpoint = spawnpoint;
- alive = new ArrayList<String>();
- topPlayers = new String[3];
- rollback = new HashMap<Location, BlockState>();
- allowBreaking = false;
- Bukkit.getPluginManager().registerEvents(this, plugin);
- }
- public boolean isSetup() {
- return floorCuboid != null && spawnpoint != null;
- }
- public void start() {
- for(int i = 0; i < topPlayers.length; i++) topPlayers[i] = null;
- for(Player p: arena.getPlayers()) {
- p.getInventory().addItem(shovel);
- p.teleport(spawnpoint);
- alive.add(p.getName());
- }
- for(Player p: arena.getSpectators()) p.teleport(spawnpoint);
- startTask();
- }
- public void cancel(boolean immediate) {
- allowBreaking = false;
- alive.clear();
- arena = null;
- PartyGames.api.rollback(rollback, immediate);
- mainCuboid.clearEntities();
- if(task != null) {
- task.cancel();
- task = null;
- }
- }
- public void leave(Player p) {
- if(alive.contains(p.getName())) {
- alive.remove(p.getName());
- if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
- if(alive.size() == 1) {
- finish();
- }
- }
- }
- private void startTask() {
- task = new BukkitRunnable() {
- int breakingTimer = 5;
- int timer = roundDuration;
- public void run() {
- if(breakingTimer > 0) {
- breakingTimer--;
- if(breakingTimer == 4) arena.sendPlayCounterNotification(PlayCounter.READY);
- else if(breakingTimer == 2) arena.sendPlayCounterNotification(PlayCounter.SET);
- else if(breakingTimer == 0) {
- arena.sendPlayCounterNotification(PlayCounter.GO);
- allowBreaking = true;
- }
- } else {
- for(Player p: arena.getPlayers()) {
- p.setLevel(timer);
- if(alive.contains(p.getName())) {
- if(p.getLocation().getY() < floorCuboid.y1) {
- eliminatePlayer(p);
- }
- }
- }
- timer--;
- if(timer == 0) {
- finish();
- }
- }
- }
- }.runTaskTimer(plugin, 40L, 20L);
- }
- private void eliminatePlayer(Player p) {
- alive.remove(p.getName());
- p.sendMessage(PartyGames.api.getMessage("Player-Lose-Round"));
- p.getInventory().clear();
- String eliminationMessage = PartyGames.api.getMessage("Player-Lose-Round-Announce").replace("%player%", p.getName());
- for(Player x: arena.getAllPlayers()) x.sendMessage(eliminationMessage);
- if(arena.scoreboard != null) arena.scoreboard.updatePlaceholder(null, arena, PartyGames.plugin, "alive_players", alive.size());
- PartyGames.api.MakeSpectator(p, true);
- if(alive.size() < 3) topPlayers[alive.size()] = p.getName(); //If size is 2, then this player is 3rd which is number 2 in []
- if(alive.size() == 1) {
- finish();
- }
- }
- private void finish() {
- if(task != null) {
- task.cancel();
- task = null;
- }
- for(int i = 0; i < alive.size(); i++) if(i < topPlayers.length) topPlayers[i] = alive.get(i);
- PartyGames.api.sendRoundConclusion(arena, topPlayers);
- String winnerName = topPlayers[0];
- final Player winner = Bukkit.getPlayer(winnerName);
- task = new BukkitRunnable() {
- int timer = 5;
- boolean fireworks = false;
- public void run() {
- timer--;
- fireworks = !fireworks;
- if(fireworks) {
- if(winner != null) PartyGames.plugin.fireWorkEffect(winner, true);
- }
- if(timer == 0) {
- for(Player p: arena.getPlayers()) {
- PartyGames.api.MakeSpectator(p, false);
- }
- arena.finishCurrentMinigame(topPlayers);
- }
- }
- }.runTaskTimer(plugin, 0, 20L);
- }
- @EventHandler(priority = EventPriority.HIGH)
- public void onBlockBreakEvent(BlockBreakEvent e) {
- Player p = e.getPlayer();
- if(allowBreaking && alive.contains(p.getName()) && floorCuboid.contains(e.getBlock().getLocation())) {
- e.setCancelled(false);
- rollback.put(e.getBlock().getLocation(), e.getBlock().getState());
- if(giveSnowballs) p.getInventory().addItem(snowball);
- }
- }
- @EventHandler
- public void onProjectileHitEvent(ProjectileHitEvent e) {
- if(!allowBreaking || !(e.getEntity() instanceof Snowball)) return;
- BlockIterator bi = new BlockIterator(e.getEntity().getWorld(), e.getEntity().getLocation().toVector(), e.getEntity().getVelocity().normalize(), 0.0D, 4);
- Block hit = null;
- while (bi.hasNext()) {
- hit = bi.next();
- if (!hit.getType().equals(Material.AIR)) {
- if(mainCuboid.contains(hit.getLocation()) && !rollback.containsKey(hit.getLocation())) {
- rollback.put(hit.getLocation(), hit.getState());
- hit.setType(Material.AIR);
- }
- break;
- }
- }
- }
- public void handlePlayerInteractEvent(PlayerInteractEvent e) {
- Player p = e.getPlayer();
- if(allowBreaking && alive.contains(p.getName()) && p.getItemInHand() != null && p.getItemInHand().getType().equals(Material.SNOW_BALL)) {
- e.setCancelled(false);
- p.playSound(p.getLocation(), PartyGames.plugin.ITEM_PICKUP, 1, 1);
- }
- }
- public void handleEntityDamageByEntityEvent(EntityDamageByEntityEvent e) {}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement