forked from upstream/KitPvP
Migrate some Arena methods to new Utilities class
This commit is contained in:
1 parent
1dbee0d697
commit
defabd1d77
6 files changed
+100
-107
No files matched your search
@@ -511,7 +511,7 @@ public class MainCommand implements CommandExecutor {
|
||||
private void sendStatsMessage(CommandSender receiver, String username) {
|
||||
|
||||
for (String line : messages.getStringList("Messages.Stats.Message")) {
|
||||
receiver.sendMessage(arena.replaceBuiltInPlaceholdersIfPresent(line, username));
|
||||
receiver.sendMessage(arena.getUtilities().replaceBuiltInPlaceholdersIfPresent(line, username));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,8 +14,6 @@ import org.bukkit.scoreboard.Scoreboard;
|
||||
|
||||
import com.planetgallium.kitpvp.Game;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
|
||||
public class Arena {
|
||||
|
||||
private final Game plugin;
|
||||
@@ -35,8 +33,6 @@ public class Arena {
|
||||
private final Cooldowns cooldowns;
|
||||
private final Menus menus;
|
||||
|
||||
// TODO: maybe move some of these helper methods to a separate class (Utilities.java?)
|
||||
|
||||
public Arena(Game plugin, Resources resources) {
|
||||
this.plugin = plugin;
|
||||
this.random = new Random();
|
||||
@@ -46,18 +42,17 @@ public class Arena {
|
||||
|
||||
this.hitCache = new HashMap<>();
|
||||
|
||||
this.utilties = new Utilities(plugin);
|
||||
this.utilties = new Utilities(plugin, this);
|
||||
this.leaderboards = new Leaderboards(plugin);
|
||||
this.stats = new Stats(plugin, this);
|
||||
this.kits = new Kits(plugin, this);
|
||||
this.abilities = new Abilities(plugin);
|
||||
this.killstreaks = new KillStreaks(resources);
|
||||
this.cooldowns = new Cooldowns(plugin);
|
||||
this.cooldowns = new Cooldowns(plugin, this);
|
||||
this.menus = new Menus(resources);
|
||||
}
|
||||
|
||||
public void addPlayer(Player p, boolean toSpawn, boolean giveItems) {
|
||||
|
||||
CacheManager.getPlayerAbilityCooldowns(p.getName()).clear();
|
||||
|
||||
kits.resetKit(p.getName());
|
||||
@@ -97,7 +92,6 @@ public class Arena {
|
||||
if (resources.getScoreboard().getBoolean("Scoreboard.General.Enabled")) {
|
||||
updateScoreboards(p, false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void removePlayer(Player p) {
|
||||
@@ -140,11 +134,9 @@ public class Arena {
|
||||
}
|
||||
|
||||
public void giveItems(Player p) {
|
||||
|
||||
ConfigurationSection items = config.getConfigurationSection("Items");
|
||||
|
||||
for (String identifier : items.getKeys(false)) {
|
||||
|
||||
String itemPath = "Items." + identifier;
|
||||
|
||||
if (config.getBoolean(itemPath + ".Enabled")) {
|
||||
@@ -155,11 +147,8 @@ public class Arena {
|
||||
item.setItemMeta(meta);
|
||||
|
||||
p.getInventory().setItem(config.getInt(itemPath + ".Slot"), item);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void toSpawn(Player p, String arenaName) {
|
||||
@@ -173,92 +162,20 @@ public class Arena {
|
||||
}
|
||||
|
||||
public void updateScoreboards(Player p, boolean hide) {
|
||||
|
||||
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
|
||||
String scoreboardTitle = addPlaceholdersIfPossible(p,
|
||||
String scoreboardTitle = utilties.addPlaceholdersIfPossible(p,
|
||||
resources.getScoreboard().getString("Scoreboard.General.Title"));
|
||||
Infoboard scoreboard = new Infoboard(board, scoreboardTitle);
|
||||
|
||||
if (!hide) {
|
||||
|
||||
for (String line : resources.getScoreboard().getStringList("Scoreboard.Lines")) {
|
||||
scoreboard.add(addPlaceholdersIfPossible(p, line));
|
||||
scoreboard.add(utilties.addPlaceholdersIfPossible(p, line));
|
||||
}
|
||||
|
||||
} else {
|
||||
scoreboard.hide();
|
||||
}
|
||||
|
||||
scoreboard.update(p);
|
||||
|
||||
}
|
||||
|
||||
private String addPlaceholdersIfPossible(Player p, String text) {
|
||||
if (plugin.hasPlaceholderAPI()) {
|
||||
text = PlaceholderAPI.setPlaceholders(p, text);
|
||||
}
|
||||
|
||||
return replaceBuiltInPlaceholdersIfPresent(text, p.getName());
|
||||
}
|
||||
|
||||
public String replaceBuiltInPlaceholdersIfPresent(String s, String username) {
|
||||
|
||||
// The reason I'm doing all these if statements rather than a more concise code solution is to reduce
|
||||
// the amount of data that is unnecessarily fetched (ex by using .replace) to improve performance
|
||||
// no longer constantly fetching stats from database for EACH line of scoreboard on update and player join
|
||||
|
||||
if (s.contains("%streak%")) {
|
||||
s = s.replace("%streak%", String.valueOf(getKillStreaks().getStreak(username)));
|
||||
}
|
||||
|
||||
if (s.contains("%player%")) {
|
||||
s = s.replace("%player%", username);
|
||||
}
|
||||
|
||||
if (s.contains("%xp%")) {
|
||||
s = s.replace("%xp%", String.valueOf(stats.getStat("experience", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%level%")) {
|
||||
s = s.replace("%level%", String.valueOf(stats.getStat("level", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%level_prefix%")) {
|
||||
String levelPrefix = utilties.getPlayerLevelPrefix(username);
|
||||
s = s.replace("%level_prefix%", levelPrefix);
|
||||
}
|
||||
|
||||
if (s.contains("%max_xp%")) {
|
||||
s = s.replace("%max_xp%", String.valueOf(stats.getRegularOrRelativeNeededExperience(username)));
|
||||
}
|
||||
|
||||
if (s.contains("%max_level%")) {
|
||||
s = s.replace("%max_level%",
|
||||
String.valueOf(resources.getLevels().getInt("Levels.Options.Maximum-Level")));
|
||||
}
|
||||
|
||||
if (s.contains("%kd%")) {
|
||||
s = s.replace("%kd%", String.valueOf(getStats().getKDRatio(username)));
|
||||
}
|
||||
|
||||
if (s.contains("%kills%")) {
|
||||
s = s.replace("%kills%", String.valueOf(stats.getStat("kills", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%deaths%")) {
|
||||
s = s.replace("%deaths%", String.valueOf(stats.getStat("deaths", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%kit%")) {
|
||||
if (getKits().getKitOfPlayer(username) != null) {
|
||||
s = s.replace("%kit%", getKits().getKitOfPlayer(username).getName());
|
||||
} else {
|
||||
s = s.replace("%kit%", "None");
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
|
||||
}
|
||||
|
||||
public String generateRandomArenaSpawn(String arenaName) {
|
||||
@@ -268,18 +185,6 @@ public class Arena {
|
||||
return spawnKeys.get(random.nextInt(spawnKeys.size()));
|
||||
}
|
||||
|
||||
public boolean isCombatActionPermittedInRegion(Player p) {
|
||||
if (plugin.hasWorldGuard()) {
|
||||
if (WorldGuardAPI.getInstance().allows(p, WorldGuardFlag.PVP.getFlag())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
p.sendMessage(resources.getMessages().getString("Messages.Error.PVP"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Map<String, String> getHitCache() { return hitCache; }
|
||||
|
||||
public Stats getStats() { return stats; }
|
||||
|
||||
@@ -2,14 +2,20 @@ package com.planetgallium.kitpvp.game;
|
||||
|
||||
import com.planetgallium.kitpvp.Game;
|
||||
import com.planetgallium.kitpvp.util.Resources;
|
||||
import com.planetgallium.kitpvp.util.WorldGuardAPI;
|
||||
import com.planetgallium.kitpvp.util.WorldGuardFlag;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Utilities {
|
||||
|
||||
private final Game plugin;
|
||||
private final Arena arena;
|
||||
private final Resources resources;
|
||||
|
||||
public Utilities(Game plugin) {
|
||||
this.arena = plugin.getArena();
|
||||
public Utilities(Game plugin, Arena arena) {
|
||||
this.plugin = plugin;
|
||||
this.arena = arena;
|
||||
this.resources = plugin.getResources();
|
||||
}
|
||||
|
||||
@@ -19,4 +25,83 @@ public class Utilities {
|
||||
.replace("%level%", playerLevel);
|
||||
}
|
||||
|
||||
public String addPlaceholdersIfPossible(Player p, String text) {
|
||||
if (plugin.hasPlaceholderAPI()) {
|
||||
text = PlaceholderAPI.setPlaceholders(p, text);
|
||||
}
|
||||
|
||||
return replaceBuiltInPlaceholdersIfPresent(text, p.getName());
|
||||
}
|
||||
|
||||
public String replaceBuiltInPlaceholdersIfPresent(String s, String username) {
|
||||
|
||||
// The reason I'm doing all these if statements rather than a more concise code solution is to reduce
|
||||
// the amount of data that is unnecessarily fetched (ex by using .replace) to improve performance
|
||||
// no longer constantly fetching stats from database for EACH line of scoreboard on update and player join
|
||||
|
||||
if (s.contains("%streak%")) {
|
||||
s = s.replace("%streak%", String.valueOf(arena.getKillStreaks().getStreak(username)));
|
||||
}
|
||||
|
||||
if (s.contains("%player%")) {
|
||||
s = s.replace("%player%", username);
|
||||
}
|
||||
|
||||
if (s.contains("%xp%")) {
|
||||
s = s.replace("%xp%", String.valueOf(arena.getStats().getStat("experience", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%level%")) {
|
||||
s = s.replace("%level%", String.valueOf(arena.getStats().getStat("level", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%level_prefix%")) {
|
||||
String levelPrefix = getPlayerLevelPrefix(username);
|
||||
s = s.replace("%level_prefix%", levelPrefix);
|
||||
}
|
||||
|
||||
if (s.contains("%max_xp%")) {
|
||||
s = s.replace("%max_xp%", String.valueOf(arena.getStats().getRegularOrRelativeNeededExperience(username)));
|
||||
}
|
||||
|
||||
if (s.contains("%max_level%")) {
|
||||
s = s.replace("%max_level%",
|
||||
String.valueOf(resources.getLevels().getInt("Levels.Options.Maximum-Level")));
|
||||
}
|
||||
|
||||
if (s.contains("%kd%")) {
|
||||
s = s.replace("%kd%", String.valueOf(arena.getStats().getKDRatio(username)));
|
||||
}
|
||||
|
||||
if (s.contains("%kills%")) {
|
||||
s = s.replace("%kills%", String.valueOf(arena.getStats().getStat("kills", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%deaths%")) {
|
||||
s = s.replace("%deaths%", String.valueOf(arena.getStats().getStat("deaths", username)));
|
||||
}
|
||||
|
||||
if (s.contains("%kit%")) {
|
||||
if (arena.getKits().getKitOfPlayer(username) != null) {
|
||||
s = s.replace("%kit%", arena.getKits().getKitOfPlayer(username).getName());
|
||||
} else {
|
||||
s = s.replace("%kit%", "None");
|
||||
}
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public boolean isCombatActionPermittedInRegion(Player p) {
|
||||
if (plugin.hasWorldGuard()) {
|
||||
if (WorldGuardAPI.getInstance().allows(p, WorldGuardFlag.PVP.getFlag())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
p.sendMessage(resources.getMessages().getString("Messages.Error.PVP"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public class AbilityListener implements Listener {
|
||||
public void onAbility(PlayerAbilityEvent e) {
|
||||
Player p = e.getPlayer();
|
||||
|
||||
if (!arena.isCombatActionPermittedInRegion(p)) {
|
||||
if (!arena.getUtilities().isCombatActionPermittedInRegion(p)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.Random;
|
||||
import com.cryptomorin.xseries.XMaterial;
|
||||
import com.cryptomorin.xseries.XSound;
|
||||
import com.planetgallium.kitpvp.api.Kit;
|
||||
import com.planetgallium.kitpvp.game.Utilities;
|
||||
import com.planetgallium.kitpvp.util.Resource;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@@ -38,6 +39,7 @@ public class ItemListener implements Listener {
|
||||
|
||||
private final Game plugin;
|
||||
private final Arena arena;
|
||||
private final Utilities utilities;
|
||||
private final Resources resources;
|
||||
private final Resource config;
|
||||
private final Resource abilities;
|
||||
@@ -45,6 +47,7 @@ public class ItemListener implements Listener {
|
||||
public ItemListener(Game plugin) {
|
||||
this.plugin = plugin;
|
||||
this.arena = plugin.getArena();
|
||||
this.utilities = plugin.getArena().getUtilities();
|
||||
this.resources = plugin.getResources();
|
||||
this.config = resources.getConfig();
|
||||
this.abilities = resources.getAbilities();
|
||||
@@ -95,7 +98,7 @@ public class ItemListener implements Listener {
|
||||
|
||||
if (config.getBoolean("TNT.Enabled") && Toolkit.hasMatchingDisplayName(item, config.getString("TNT.Name"))) {
|
||||
|
||||
if (!arena.isCombatActionPermittedInRegion(p)) {
|
||||
if (!utilities.isCombatActionPermittedInRegion(p)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -382,7 +385,7 @@ public class ItemListener implements Listener {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (arena.isCombatActionPermittedInRegion(p)) {
|
||||
if (utilities.isCombatActionPermittedInRegion(p)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
@@ -579,7 +582,7 @@ public class ItemListener implements Listener {
|
||||
Player shooter = (Player) egg.getShooter();
|
||||
Location shooterLocation = shooter.getLocation();
|
||||
|
||||
if (!arena.isCombatActionPermittedInRegion(damagedPlayer)) {
|
||||
if (!utilities.isCombatActionPermittedInRegion(damagedPlayer)) {
|
||||
shooter.sendMessage(resources.getMessages().getString("Messages.Error.PVP"));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class Placeholders extends PlaceholderExpansion {
|
||||
public String translatePlaceholderAPIPlaceholders(String placeholderAPIIdentifier, String username) {
|
||||
if (placeholderAPItoBuiltIn.containsKey(placeholderAPIIdentifier)) {
|
||||
String toBuiltInPlaceholder = placeholderAPItoBuiltIn.get(placeholderAPIIdentifier);
|
||||
return arena.replaceBuiltInPlaceholdersIfPresent(toBuiltInPlaceholder, username);
|
||||
return arena.getUtilities().replaceBuiltInPlaceholdersIfPresent(toBuiltInPlaceholder, username);
|
||||
} else {
|
||||
Toolkit.printToConsole(String.format("&7[&b&lKIT-PVP&7] &cUnknown placeholder identifier [%s]. " +
|
||||
"Please see plugin page.", placeholderAPIIdentifier));
|
||||
|
||||
Reference in new issue
Block a user