Skip to content

Commit

Permalink
v5.1.0 (#32)
Browse files Browse the repository at this point in the history
- Adds getGame() to PlayerWinEvent
- Adds settings for blocking melee and/or ranged damage
- Displays the weapon name on death messages
- Merges messages "has_killed_times" and "killed_by"
- Also shows the kill counts for the victim
- Fixes pvp false
- Fixes ArrayIndexOutOfBoundsException on EliminationTournamentGame
- Probably fixes mcMMO skills during games
  • Loading branch information
RoinujNosde committed Apr 18, 2021
1 parent accc439 commit cf40362
Show file tree
Hide file tree
Showing 44 changed files with 172 additions and 54 deletions.
3 changes: 3 additions & 0 deletions crowdin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files:
- source: src/main/resources/language-en.yml
translation: /src/main/resources/language-%locale_with_underscore%.%file_extension%
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.roinujnosde</groupId>
<artifactId>TitansBattle</artifactId>
<version>5.0.0-beta</version>
<version>5.1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/me/roinujnosde/titansbattle/TitansBattle.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ public void debug(String message, boolean respectUserDecision) {
if (respectUserDecision && !configManager.isDebug()) {
return;
}
Bukkit.getConsoleSender().sendMessage(ChatColor.BLUE + "[TitansBattle] " + message);
getLogger().info(message);
}

public void debug(String message) {
debug(message, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ private void saveInventory(@NotNull CommandSender sender, @NotNull GameConfigura
@CommandPermission("titansbattle.join")
@Conditions("happening")
public void join(Player sender) {
plugin.debug(String.format("%s used /tb join", sender.getName()));
gameManager.getCurrentGame().ifPresent(g -> g.onJoin(databaseManager.getWarrior(sender.getUniqueId())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package me.roinujnosde.titansbattle.events;

import me.roinujnosde.titansbattle.games.Game;
import me.roinujnosde.titansbattle.types.Warrior;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand All @@ -39,17 +40,24 @@
public class PlayerWinEvent extends Event {

private static final HandlerList HANDLERS = new HandlerList();
private final Game game;
private final List<Warrior> players;

public PlayerWinEvent(@NotNull List<Warrior> players) {
public PlayerWinEvent(@NotNull Game game, @NotNull List<Warrior> players) {
this.game = game;
this.players = players;
}

public PlayerWinEvent(@NotNull Warrior warrior) {
public PlayerWinEvent(@NotNull Game game, @NotNull Warrior warrior) {
this.game = game;
players = new ArrayList<>();
players.add(warrior);
}

public @NotNull Game getGame() {
return game;
}

/**
* Returns the winner of the event (or the first of the list, if there are more than one winner)
* @return the winner of the event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public EliminationTournamentGame(TitansBattle plugin, GameConfiguration config)

@Override
public boolean isInBattle(@NotNull Warrior warrior) {
if (!battle || !getConfig().isPvP()) {
if (!battle) {
return false;
}
return isCurrentDuelist(warrior);
}

private boolean isCurrentDuelist(@NotNull Warrior warrior) {
if (!getConfig().isGroupMode()) {
return playerDuelists.get(0).isDuelist(warrior);
return playerDuelists.size() != 0 && playerDuelists.get(0).isDuelist(warrior);
}
return groupDuelists.get(0).isDuelist(warrior.getGroup());
return groupDuelists.size() != 0 && groupDuelists.get(0).isDuelist(warrior.getGroup());
}

private List<Warrior> getDuelLosers(@NotNull Warrior defeated) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public FreeForAllGame(TitansBattle plugin, GameConfiguration config) {

@Override
public boolean isInBattle(@NotNull Warrior warrior) {
return getConfig().isPvP() && battle && playerParticipants.contains(warrior);
return battle && playerParticipants.contains(warrior);
}

@Override
Expand Down Expand Up @@ -81,7 +81,7 @@ protected void processWinners() {
getCasualties().stream().filter(p -> winnerGroup.isMember(p.getUniqueId())).forEach(winners::add);
}
SoundUtils.playSound(VICTORY, plugin.getConfig(), winners);
PlayerWinEvent event = new PlayerWinEvent(winners);
PlayerWinEvent event = new PlayerWinEvent(this, winners);
Bukkit.getPluginManager().callEvent(event);
if (killer != null) {
plugin.getGameManager().setKiller(getConfig(), killer, null);
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/me/roinujnosde/titansbattle/games/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -58,6 +61,7 @@ public Game(TitansBattle plugin, GameConfiguration config) {
protected boolean canJoin(@NotNull Warrior warrior) {
Player player = warrior.toOnlinePlayer();
if (player == null) {
plugin.debug(String.format("canJoin() -> player %s %s == null", warrior.getName(), warrior.getUniqueId()));
return false;
}
if (!isLobby()) {
Expand Down Expand Up @@ -105,10 +109,14 @@ public boolean isParticipant(@NotNull Warrior warrior) {

public void onJoin(@NotNull Warrior warrior) {
if (!canJoin(warrior)) {
plugin.debug(String.format("Warrior %s can't join", warrior.getName()));
return;
}
Player player = warrior.toOnlinePlayer();
if (player == null) return;
if (player == null) {
plugin.debug(String.format("onJoin() -> player %s %s == null", warrior.getName(), warrior.getUniqueId()));
return;
}
teleport(warrior, getConfig().getLobby());
SoundUtils.playSound(JOIN_GAME, plugin.getConfig(), player);
playerParticipants.add(warrior);
Expand All @@ -126,7 +134,7 @@ protected void setKit(@NotNull Warrior warrior) {
}

public boolean shouldClearDropsOnDeath(@NotNull Warrior warrior) {
return false;
return isParticipant(warrior) && config.isClearItemsOnDeath();
}

public boolean shouldKeepInventoryOnDeath(@NotNull Warrior warrior) {
Expand Down Expand Up @@ -216,9 +224,6 @@ public void onDeath(@NotNull Warrior victim, @Nullable Warrior killer) {
if (!isParticipant(victim)) {
return;
}
if (killer == null) {
gameManager.broadcastKey("died_by_himself", this, victim.getName());
}
if (!isLobby()) {
ParticipantDeathEvent event = new ParticipantDeathEvent(victim, killer);
Bukkit.getPluginManager().callEvent(event);
Expand All @@ -229,15 +234,33 @@ public void onDeath(@NotNull Warrior victim, @Nullable Warrior killer) {
}
if (killer != null) {
killer.increaseKills(gameName);
gameManager.broadcastKey("killed_by", this, victim.getName(), killer.getName());
gameManager.broadcastKey("has_killed_times", this, killer.getName(), increaseKills(killer));
increaseKills(killer);
}
victim.increaseDeaths(gameName);
playDeathSound(victim);
}
broadcastDeathMessage(victim, killer);
processPlayerExit(victim);
}

@SuppressWarnings("deprecation")
protected void broadcastDeathMessage(@NotNull Warrior victim, @Nullable Warrior killer) {
if (killer == null) {
gameManager.broadcastKey("died_by_himself", this, victim.getName());
} else {
ItemStack itemInHand = Objects.requireNonNull(killer.toOnlinePlayer()).getItemInHand();
String weaponName = plugin.getLang("fist", this);
if (itemInHand != null && itemInHand.getType() != Material.AIR) {
ItemMeta itemMeta = itemInHand.getItemMeta();
if (itemMeta != null && itemMeta.hasDisplayName()) {
weaponName = itemMeta.getDisplayName();
}
}
gameManager.broadcastKey("killed_by", this, victim.getName(),
killsCount.getOrDefault(victim, 0), killer.getName(), killsCount.get(killer), weaponName);
}
}

protected abstract void onLobbyEnd();

protected boolean canStartBattle() {
Expand Down Expand Up @@ -375,8 +398,8 @@ public HashMap<Warrior, Integer> getKillsCount() {
return killsCount;
}

protected int increaseKills(Warrior warrior) {
return killsCount.compute(warrior, (p, i) -> i == null ? 1 : i + 1);
protected void increaseKills(Warrior warrior) {
killsCount.compute(warrior, (p, i) -> i == null ? 1 : i + 1);
}

public void sendMessageToParticipants(@NotNull String message) {
Expand Down Expand Up @@ -466,8 +489,12 @@ protected void teleport(@NotNull Collection<Warrior> collection, @NotNull Locati
}

protected void teleport(@Nullable Warrior warrior, Location destination) {
plugin.debug(String.format("teleport() -> destination %s", destination));
Player player = warrior != null ? warrior.toOnlinePlayer() : null;
if (player == null) return;
if (player == null) {
plugin.debug(String.format("teleport() -> warrior %s", warrior));
return;
}
player.teleport(destination);
SoundUtils.playSound(TELEPORT, plugin.getConfig(), player);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import me.roinujnosde.titansbattle.managers.DatabaseManager;
import me.roinujnosde.titansbattle.managers.GameManager;
import me.roinujnosde.titansbattle.managers.GroupManager;
import me.roinujnosde.titansbattle.types.GameConfiguration;
import me.roinujnosde.titansbattle.utils.Helper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand All @@ -34,6 +36,15 @@ public void onDamageLowest(EntityDamageEvent event) {
}
}

//un-cancelling so mcMMO skills can be used
//mcMMO's listener is on HIGHEST and ignoreCancelled = true, this will run before
@EventHandler(priority = EventPriority.HIGH)
public void onDamageHigh(EntityDamageEvent event) {
if (isParticipant(event.getEntity())) {
event.setCancelled(false);
}
}

private boolean isParticipant(Entity entity) {
Game game = gm.getCurrentGame().orElse(null);
if (game == null || !(entity instanceof Player)) {
Expand All @@ -58,19 +69,41 @@ public void onDamage(EntityDamageEvent event) {
}
event.setCancelled(false);
if (event instanceof EntityDamageByEntityEvent) {
EntityDamageByEntityEvent subEvent = (EntityDamageByEntityEvent) event;
Player attacker = Helper.getPlayerAttackerOrKiller(subEvent.getDamager());
if (attacker == null || !game.isParticipant(dm.getWarrior(attacker.getUniqueId()))) {
return;
}
if (!game.getConfig().isGroupMode()) {
return;
}
processEntityDamageByEntityEvent(event, defender, game);
}
}

private void processEntityDamageByEntityEvent(EntityDamageEvent event, Player defender, Game game) {
EntityDamageByEntityEvent subEvent = (EntityDamageByEntityEvent) event;
Player attacker = Helper.getPlayerAttackerOrKiller(subEvent.getDamager());
if (!isDamageTypeAllowed(subEvent, game)) {
event.setCancelled(true);
return;
}
if (attacker != null && !game.getConfig().isPvP()) {
event.setCancelled(true);
return;
}
if (attacker == null || !game.isParticipant(dm.getWarrior(attacker.getUniqueId()))) {
return;
}

if (!game.getConfig().isGroupMode()) {
return;
}

GroupManager groupManager = TitansBattle.getInstance().getGroupManager();
if (groupManager != null) {
event.setCancelled(groupManager.sameGroup(defender.getUniqueId(), attacker.getUniqueId()));
}
}

GroupManager groupManager = TitansBattle.getInstance().getGroupManager();
if (groupManager != null) {
event.setCancelled(groupManager.sameGroup(defender.getUniqueId(), attacker.getUniqueId()));
}
private boolean isDamageTypeAllowed(EntityDamageByEntityEvent event, Game game) {
GameConfiguration config = game.getConfig();
if (event.getDamager() instanceof Projectile) {
return config.isRangedDamage();
} else {
return config.isMeleeDamage();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class GameConfiguration implements ConfigurationSerializable {
private Boolean powerOfTwo = false;
private Boolean killer = true;
private Boolean pvp = true;
private Boolean clearItemsOnDeath = false;
@Path("damage-type.melee")
private Boolean meleeDamage = true;
@Path("damage-type.ranged")
private Boolean rangedDamage = true;
@Path("minimum.groups")
private Integer minimumGroups = 2;
@Path("maximum.groups")
Expand Down Expand Up @@ -215,6 +220,18 @@ public Boolean isPvP() {
return pvp;
}

public Boolean isClearItemsOnDeath() {
return clearItemsOnDeath;
}

public Boolean isMeleeDamage() {
return meleeDamage;
}

public Boolean isRangedDamage() {
return rangedDamage;
}

public Boolean isKiller() {
return killer;
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/me/roinujnosde/titansbattle/types/Warrior.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ public void sendMessage(@Nullable String message) {
}
}

@Override
public String toString() {
return "Warrior{" +
"name=" + player.getName() +
", uuid=" + player.getUniqueId() +
'}';
}

@Nullable
public Player toOnlinePlayer() {
return player.getPlayer();
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/me/roinujnosde/titansbattle/utils/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ public static boolean isWinnerQuitMessageEnabled(Player player) {
* @param entity the entity to investigate
* @return the attacker/killer or null
*/
public @Nullable
static Player getPlayerAttackerOrKiller(Entity entity) {
public static @Nullable Player getPlayerAttackerOrKiller(Entity entity) {
Player investigated = null;
if (entity instanceof Player) {
investigated = (Player) entity;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/language-af_ZA.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-ar_SA.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-ca_ES.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-cs_CZ.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-da_DK.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-de_DE.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-el_GR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
4 changes: 2 additions & 2 deletions src/main/resources/language-en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ game_is_happening: "&b» You can't join at this time!"
new_killer: "&b» {0} is the new Killer!"
preparation_over: "&b» The preparation time is over. FIGHT!"
not_participating: "&b» You are not participating!"
killed_by: "&b» {0} was killed by {1}"
fist: "the fist"
killed_by: "&b» {0}({1}) was killed by {2}({3}) using {4}"
died_by_himself: "&b» {0} died by himself!"
cancelled: "&b» {0} has cancelled the game."
game_expired: "&b» No winner. The game expired!"
watch_to_the_end: "&b» You died, it's sad. But don't disconnect, if your group wins, you have right to the prize!"
has_killed_times: "&b» {0} has killed {1} times in the game!"
who_won: "&b» Game over! Winner group is: {0}."
who_won_tournament: "&b» Game over! Winners are:\n &b1# &f{0}\n &b2# &f{1}\n &b3# &f{2}"
no_winner_tournament: "no winner"
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/language-en_US.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-es_ES.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-fi_FI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-fr_FR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
1 change: 1 addition & 0 deletions src/main/resources/language-he_IL.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Loading

0 comments on commit cf40362

Please sign in to comment.