Skip to content

Commit

Permalink
Added cage detection. Improved team selector
Browse files Browse the repository at this point in the history
  • Loading branch information
PauMAVA committed Dec 27, 2019
1 parent c9d7d6a commit 5421007
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/me/PauMAVA/TTR/TTRCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void onEnable() {
} else {
this.match = new TTRMatch(MatchStatus.DISABLED);
}
this.configManager = new TTRConfigManager(this.getConfig());
this.teamHandler = new TTRTeamHandler();
this.teamHandler.setUpDefaultTeams();
this.configManager = new TTRConfigManager(this.getConfig());
this.matchWorld = this.getServer().getWorlds().get(0);
this.matchWorld.setSpawnLocation(this.configManager.getLobbyLocation());
this.getServer().getPluginManager().registerEvents(new EventListener(), this);
Expand Down
17 changes: 11 additions & 6 deletions src/me/PauMAVA/TTR/config/TTRConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import org.bukkit.configuration.file.FileConfiguration;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.*;

public class TTRConfigManager {

Expand Down Expand Up @@ -107,6 +104,14 @@ public Location getTeamCage(String teamName) {
return getTeam(teamName).getLocation("cage");
}

public List<Location> getTeamCages() {
List<Location> cages = new ArrayList<Location>();
for(String teamName: getTeamNames()) {
cages.add(getTeamCage(teamName));
}
return cages;
}

private void saveConfig() {
TTRCore.getInstance().saveConfig();
}
Expand All @@ -123,8 +128,8 @@ private void setUpFile() {
matchSection.addDefault("maxhealth", 20);
this.mapSection = this.configuration.createSection("map");
mapSection.addDefault("lobby", new Location(this.world, 1, 207, 1010));
mapSection.addDefault("ironspawns", new ArrayList<Location>(Arrays.asList(new Location(this.world, 0,207, 1138))));
mapSection.addDefault("xpspawns", new ArrayList<Location>(Arrays.asList(new Location(this.world, 0, 207, 1666))));
mapSection.addDefault("ironspawns", new ArrayList<Location>(Arrays.asList(new Location(this.world, 0,203, 1138))));
mapSection.addDefault("xpspawns", new ArrayList<Location>(Arrays.asList(new Location(this.world, 0, 203, 1166))));
this.teamsSection = this.configuration.createSection("teams");
ConfigurationSection team1section = teamsSection.createSection("team1");
ConfigurationSection team2section = teamsSection.createSection("team2");
Expand Down
44 changes: 44 additions & 0 deletions src/me/PauMAVA/TTR/match/Cage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* TheTowersRemastered (TTR)
* Copyright (c) 2019-2020 Pau Machetti Vallverdu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.PauMAVA.TTR.match;

import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.util.List;

public class Cage {

private Location location;
private int effectiveRadius;

public Cage(Location location, int effectiveRadius) {
this.location = location;
this.effectiveRadius = effectiveRadius;
}

public boolean isInCage(Player player) {
return location.distance(player.getLocation()) < effectiveRadius;
}

public Location getLocation() {
return this.location;
}

}
71 changes: 71 additions & 0 deletions src/me/PauMAVA/TTR/match/CageChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* TheTowersRemastered (TTR)
* Copyright (c) 2019-2020 Pau Machetti Vallverdu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package me.PauMAVA.TTR.match;

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.util.TTRPrefix;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.ArrayList;
import java.util.List;

public class CageChecker {

private List<Cage> cages = new ArrayList<Cage>();
private int checkerTaskPID;


public void startChecking() {
this.checkerTaskPID = new BukkitRunnable(){
@Override
public void run() {
for(Player p: Bukkit.getServer().getOnlinePlayers()) {
for(Cage cage: cages) {
Location particleLocation = new Location(cage.getLocation().getWorld(), cage.getLocation().getBlockX(), cage.getLocation().getBlockY() + 1, cage.getLocation().getBlockZ());
particleLocation.add(particleLocation.getX() > 0 ? 0.5 : -0.5, 0.0, particleLocation.getZ() > 0 ? 0.5 : -0.5);
cage.getLocation().getWorld().spawnParticle(Particle.SPELL, particleLocation, 100);
if(cage.isInCage(p)) {
playerOnCage(p);
cage.getLocation().getWorld().strikeLightningEffect(cage.getLocation());
}
}
}
}
}.runTaskTimer(TTRCore.getInstance(), 0L, 10L).getTaskId();
}

public void stopChecking() {
Bukkit.getScheduler().cancelTask(this.checkerTaskPID);
}

private void playerOnCage(Player player) {
Bukkit.broadcastMessage(TTRPrefix.TTR_GAME + "" + ChatColor.GRAY + player.getName() + " has scored a point!");
}

public void setCages(List<Location> cages, int effectiveRadius) {
for(Location cage: cages) {
this.cages.add(new Cage(cage, effectiveRadius));
}
}
}
55 changes: 55 additions & 0 deletions src/me/PauMAVA/TTR/match/LootSpawner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,60 @@

package me.PauMAVA.TTR.match;

import me.PauMAVA.TTR.TTRCore;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.Material;

import java.util.List;

public class LootSpawner {

private List<Location> ironLocations;
private List<Location> xpLocations;
private long ironFrequency = 150;
private long xpFrequency = 150;
private int ironTaskPID;
private int xpTaskPID;

public LootSpawner() {
this.ironLocations = TTRCore.getInstance().getConfigManager().getIronSpawns();
this.xpLocations = TTRCore.getInstance().getConfigManager().getXPSpawns();
}

public void startSpawning() {
setIronTask();
setXpTask();
}

public void stopSpawning() {
Bukkit.getScheduler().cancelTask(this.ironTaskPID);
Bukkit.getScheduler().cancelTask(this.xpTaskPID);
}

private void setIronTask() {
this.ironTaskPID = new BukkitRunnable(){
@Override
public void run() {
for(Location location: ironLocations) {
location.getWorld().dropItem(location, new ItemStack(Material.IRON_INGOT, 1));
}
}
}.runTaskTimer(TTRCore.getInstance(), 0L, this.ironFrequency).getTaskId();
}

private void setXpTask() {
this.xpTaskPID = new BukkitRunnable(){
@Override
public void run() {
for(Location location: xpLocations) {
location.getWorld().spawnEntity(location, EntityType.THROWN_EXP_BOTTLE);
}
}
}.runTaskTimer(TTRCore.getInstance(), 0L, this.xpFrequency).getTaskId();
}

}
20 changes: 20 additions & 0 deletions src/me/PauMAVA/TTR/match/TTRMatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@

package me.PauMAVA.TTR.match;

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.teams.TTRTeam;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class TTRMatch {

private MatchStatus status;
private LootSpawner lootSpawner;
private CageChecker checker;

public TTRMatch(MatchStatus initialStatus) {
status = initialStatus;
Expand All @@ -32,10 +39,23 @@ public boolean isOnCourse() {

public void startMatch() {
this.status = MatchStatus.INGAME;
this.lootSpawner = new LootSpawner();
this.checker = new CageChecker();
this.checker.setCages(TTRCore.getInstance().getConfigManager().getTeamCages(), 2);
this.checker.startChecking();
this.lootSpawner.startSpawning();
for(Player player: Bukkit.getServer().getOnlinePlayers()) {
TTRTeam playerTeam = TTRCore.getInstance().getTeamHandler().getPlayerTeam(player);
if(playerTeam == null) {
continue;
}
player.teleport(TTRCore.getInstance().getConfigManager().getTeamSpawn(playerTeam.getIdentifier()));
}
}

public void endMatch() {
this.status = MatchStatus.ENDED;
this.lootSpawner.stopSpawning();
}

public MatchStatus getStatus() {
Expand Down
22 changes: 15 additions & 7 deletions src/me/PauMAVA/TTR/teams/TTRTeamHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import me.PauMAVA.TTR.TTRCore;
import me.PauMAVA.TTR.match.TTRMatch;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

import java.util.ArrayList;
Expand All @@ -31,12 +33,9 @@ public class TTRTeamHandler {
private List<TTRTeam> teams = new ArrayList<TTRTeam>();

public void setUpDefaultTeams() {
teams.add(new TTRTeam("red"));
teams.add(new TTRTeam("blue"));
}

public void setUpCustomTeams(List<TTRTeam> customTeams) {
this.teams = customTeams;
for(String team: TTRCore.getInstance().getConfigManager().getTeamNames()) {
this.teams.add(new TTRTeam(team));
}
}

public boolean addPlayerToTeam(Player player, String teamIdentifier) {
Expand Down Expand Up @@ -68,10 +67,19 @@ public TTRTeam getPlayerTeam(Player player) {

private TTRTeam getTeam(String teamIdentifier) {
for(TTRTeam team: this.teams) {
if(team.getIdentifier().equalsIgnoreCase(teamIdentifier)) {
teamIdentifier = ChatColor.stripColor(teamIdentifier);
if(teamIdentifier.contentEquals(team.getIdentifier())) {
return team;
}
}
return null;
}

public void addPlayer(String teamIdentifier, Player player) {
getTeam(teamIdentifier).addPlayer(player);
}

public void removePlayer(String teamIdentifier, Player player) {
getTeam(teamIdentifier).removePlayer(player);
}
}
11 changes: 10 additions & 1 deletion src/me/PauMAVA/TTR/ui/CustomUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
Expand All @@ -28,7 +30,7 @@
import java.util.ArrayList;
import java.util.Arrays;

public class CustomUI {
public abstract class CustomUI {

private Inventory inventory;
private String title;
Expand Down Expand Up @@ -70,4 +72,11 @@ public void clearUI() {
this.inventory.clear();
}

public Inventory getInventory() {
return this.inventory;
}

@EventHandler
abstract void onInventoryClick(InventoryClickEvent event);

}
Loading

0 comments on commit 5421007

Please sign in to comment.