Skip to content
This repository has been archived by the owner on Dec 7, 2022. It is now read-only.

Commit

Permalink
Add PickUpItem and Teleport events to score-boxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew authored and Andrew committed Jun 15, 2016
1 parent 17d97ae commit e013f9c
Showing 1 changed file with 58 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
import in.twizmwaz.cardinal.util.Teams;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;

import java.util.Map;
Expand All @@ -45,43 +48,73 @@ public void unload() {
HandlerList.unregisterAll(this);
}

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
if (!ObserverModule.testObserverOrDead(event.getPlayer()) && region.contains(event.getPlayer().getLocation()) &&
(filter == null || filter.evaluate(event.getPlayer()).equals(FilterState.ALLOW))) {
addScore(event.getPlayer(), redeem(event.getPlayer()));
}
}

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerTeleport(PlayerTeleportEvent event) {
if (!ObserverModule.testObserverOrDead(event.getPlayer()) && region.contains(event.getTo().toVector()) && !region.contains(event.getFrom().toVector()) &&
(filter == null || filter.evaluate(event.getPlayer()).equals(FilterState.ALLOW))) {
addScore(event.getPlayer(), redeem(event.getPlayer()) + this.points);
}
}

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerMove(PlayerMoveEvent event) {
if (GameHandler.getGameHandler().getMatch().isRunning() && region.contains(event.getTo().toVector()) && !region.contains(event.getFrom().toVector()) &&
(filter == null || filter.evaluate(event.getPlayer()).equals(FilterState.ALLOW)) && !ObserverModule.testObserverOrDead(event.getPlayer())) {
int points = 0;
if (redeemables.size() > 0) {
for (ItemStack item : redeemables.keySet()) {
if (event.getPlayer().getInventory().contains(item)) {
for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
Optional<TeamModule> playerTeam = Teams.getTeamByPlayer(event.getPlayer());
if (playerTeam.isPresent() && score.getTeam() == playerTeam.get()) {
event.getPlayer().getInventory().remove(item);
points += redeemables.get(item);
if (!ObserverModule.testObserverOrDead(event.getPlayer()) && region.contains(event.getTo().toVector()) && !region.contains(event.getFrom().toVector()) &&
(filter == null || filter.evaluate(event.getPlayer()).equals(FilterState.ALLOW))) {
addScore(event.getPlayer(), redeem(event.getPlayer()) + this.points);
}
}

public int redeem(Player player) {
int points = 0;
if (redeemables.size() > 0) {
for (ItemStack item : redeemables.keySet()) {
if (player.getInventory().contains(item.getType())) {
for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
Optional<TeamModule> playerTeam = Teams.getTeamByPlayer(player);
if (playerTeam.isPresent() && score.getTeam() == playerTeam.get()) {
double itemCount = 0;
for (ItemStack itemstack : player.getInventory().getContents()) {
if (itemstack != null && itemstack.getType() == item.getType()) {
itemCount += itemstack.getAmount();
}
}
player.getInventory().remove(item.getType());
points += (redeemables.get(item) * itemCount);
}
}
}
}
points += this.points;
Optional<TeamModule> playerTeam = Teams.getTeamByPlayer(event.getPlayer());
if (points != 0 && playerTeam.isPresent()) {
for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
if (score.getTeam() == playerTeam.get()) {
if (!this.silent) {
ChatUtil.getGlobalChannel().sendLocalizedMessage(new UnlocalizedChatMessage(ChatColor.GRAY + "{0}",
new LocalizedChatMessage(ChatConstant.UI_SCORED_FOR,
new UnlocalizedChatMessage(playerTeam.get().getColor() + event.getPlayer().getName() + ChatColor.GRAY),
new UnlocalizedChatMessage(ChatColor.DARK_AQUA + "{0}" + ChatColor.GRAY, points == 1 ? new LocalizedChatMessage(ChatConstant.UI_ONE_POINT) : new LocalizedChatMessage(ChatConstant.UI_POINTS, points + "" + ChatColor.GRAY)),
new UnlocalizedChatMessage(playerTeam.get().getCompleteName()))));
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
}
score.addScore(points);
}
return points;
}

public void addScore(Player player, int points) {
Optional<TeamModule> playerTeam = Teams.getTeamByPlayer(player);
if (points != 0 && playerTeam.isPresent()) {
for (ScoreModule score : GameHandler.getGameHandler().getMatch().getModules().getModules(ScoreModule.class)) {
if (score.getTeam() == playerTeam.get()) {
if (!this.silent) {
ChatUtil.getGlobalChannel().sendLocalizedMessage(new UnlocalizedChatMessage(ChatColor.GRAY + "{0}",
new LocalizedChatMessage(ChatConstant.UI_SCORED_FOR,
new UnlocalizedChatMessage(playerTeam.get().getColor() + player.getName() + ChatColor.GRAY),
new UnlocalizedChatMessage(ChatColor.DARK_AQUA + "{0}" + ChatColor.GRAY, points == 1 ? new LocalizedChatMessage(ChatConstant.UI_ONE_POINT) : new LocalizedChatMessage(ChatConstant.UI_POINTS, points + "" + ChatColor.GRAY)),
new UnlocalizedChatMessage(playerTeam.get().getCompleteName()))));
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
}
score.addScore(points);
}
}
}
}

}


0 comments on commit e013f9c

Please sign in to comment.