Per-Island Chat Spy

This commit is contained in:
Fabrizio La Rosa 2020-07-04 15:28:30 +02:00
parent 1a50f15128
commit 11c72f9256
4 changed files with 129 additions and 13 deletions

View File

@ -1,28 +1,90 @@
package com.songoda.skyblock.command.commands.admin; package com.songoda.skyblock.command.commands.admin;
import com.songoda.core.compatibility.CompatibleSound;
import com.songoda.core.hooks.EconomyManager;
import com.songoda.skyblock.command.SubCommand; import com.songoda.skyblock.command.SubCommand;
import com.songoda.skyblock.config.FileManager; import com.songoda.skyblock.config.FileManager;
import com.songoda.skyblock.gui.bank.GuiBank;
import com.songoda.skyblock.island.Island;
import com.songoda.skyblock.island.IslandManager;
import com.songoda.skyblock.message.MessageManager; import com.songoda.skyblock.message.MessageManager;
import com.songoda.skyblock.playerdata.PlayerData; import com.songoda.skyblock.playerdata.PlayerData;
import com.songoda.skyblock.playerdata.PlayerDataManager; import com.songoda.skyblock.playerdata.PlayerDataManager;
import com.songoda.skyblock.sound.SoundManager;
import com.songoda.skyblock.utils.player.OfflinePlayer;
import org.bukkit.Bukkit;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.File; import java.io.File;
import java.util.UUID;
public class ChatSpyCommand extends SubCommand { public class ChatSpyCommand extends SubCommand {
@Override @Override
public void onCommandByPlayer(Player player, String[] args) { public void onCommandByPlayer(Player player, String[] args) {
MessageManager messageManager = skyblock.getMessageManager();
PlayerDataManager playerDataManager = skyblock.getPlayerDataManager(); PlayerDataManager playerDataManager = skyblock.getPlayerDataManager();
MessageManager messageManager = skyblock.getMessageManager();
IslandManager islandManager = skyblock.getIslandManager();
FileManager fileManager = skyblock.getFileManager(); FileManager fileManager = skyblock.getFileManager();
SoundManager soundManager = skyblock.getSoundManager();
FileManager.Config language = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")); FileManager.Config language = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml"));
FileConfiguration languageLoad = language.getFileConfiguration(); FileConfiguration languageLoad = language.getFileConfiguration();
PlayerData playerData = playerDataManager.getPlayerData(player); PlayerData playerData = playerDataManager.getPlayerData(player);
if (args.length < 1) {
toggleSpy(player, messageManager, languageLoad, playerData);
} else {
switch (args[0].toLowerCase()) {
case "toggle":
toggleSpy(player, messageManager, languageLoad, playerData);
break;
case "global":
if(!playerData.isGlobalChatSpy()){
playerData.enableGlobalChatSpy();
messageManager.sendMessage(player, languageLoad.getString("Command.Island.Admin.Bank.ByConsole.Message"));
} else {
messageManager.sendMessage(player, languageLoad.getString("Command.Island.Admin.Bank.ByConsole.Message"));
soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1f, 1f);
}
break;
case "add":
if(args.length > 2){
Island island = islandManager.getIslandByPlayer(new OfflinePlayer(UUID.fromString(args[1])).getBukkitOfflinePlayer());
if(island != null) {
playerData.addChatSpyIsland(island);
}
} else {
messageManager.sendMessage(player, languageLoad.getString("Command.Island.Admin.Bank.Unexpected.Message"));
soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1f, 1f);
}
break;
case "remove":
if(args.length > 2){
Island island = islandManager.getIslandByPlayer(new OfflinePlayer(UUID.fromString(args[1])).getBukkitOfflinePlayer());
if(island != null) {
playerData.removeChatSpyIsland(island);
}
} else {
messageManager.sendMessage(player, languageLoad.getString("Command.Island.Admin.Bank.Unexpected.Message"));
soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1f, 1f);
}
break;
case "list":
break;
default:
messageManager.sendMessage(player, languageLoad.getString("Command.Island.Admin.Bank.Unexpected.Message"));
soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1f, 1f);
break;
}
}
}
private void toggleSpy(Player player, MessageManager messageManager, FileConfiguration languageLoad, PlayerData playerData) {
if(playerData != null) { if(playerData != null) {
if(playerData.isChatSpy()){ if(playerData.isChatSpy()){
playerData.setChatSpy(false); playerData.setChatSpy(false);

View File

@ -95,7 +95,7 @@ public class Chat implements Listener {
for(Player targetPlayer : Bukkit.getServer().getOnlinePlayers()){ for(Player targetPlayer : Bukkit.getServer().getOnlinePlayers()){
if(targetPlayer.hasPermission("fabledskyblock.admin.chatspy")) { if(targetPlayer.hasPermission("fabledskyblock.admin.chatspy")) {
PlayerData pd = playerDataManager.getPlayerData(targetPlayer); PlayerData pd = playerDataManager.getPlayerData(targetPlayer);
if(pd != null && pd.isChatSpy()) { if(pd != null && pd.isChatSpy() && (pd.isGlobalChatSpy() || pd.isChatSpyIsland(island))) {
String message = ChatColor.translateAlternateColorCodes('&', messageManager.replaceMessage(targetPlayer, String message = ChatColor.translateAlternateColorCodes('&', messageManager.replaceMessage(targetPlayer,
islandChatEvent.getFormat().replace("%role", islandRole).replace("%player", player.getName()))) islandChatEvent.getFormat().replace("%role", islandRole).replace("%player", player.getName())))
.replace("%islandOwner", new OfflinePlayer(island.getOwnerUUID()).getName()) .replace("%islandOwner", new OfflinePlayer(island.getOwnerUUID()).getName())

View File

@ -5,6 +5,7 @@ import com.songoda.skyblock.bank.BankManager;
import com.songoda.skyblock.bank.Transaction; import com.songoda.skyblock.bank.Transaction;
import com.songoda.skyblock.config.FileManager.Config; import com.songoda.skyblock.config.FileManager.Config;
import com.songoda.skyblock.confirmation.Confirmation; import com.songoda.skyblock.confirmation.Confirmation;
import com.songoda.skyblock.island.Island;
import com.songoda.skyblock.menus.MenuType; import com.songoda.skyblock.menus.MenuType;
import com.songoda.skyblock.utils.structure.Area; import com.songoda.skyblock.utils.structure.Area;
@ -18,6 +19,7 @@ import java.util.*;
public class PlayerData { public class PlayerData {
private final SkyBlock skyblock;
private UUID uuid; private UUID uuid;
private UUID islandOwnerUUID; private UUID islandOwnerUUID;
private UUID ownershipUUID; private UUID ownershipUUID;
@ -35,6 +37,7 @@ public class PlayerData {
private Area area; private Area area;
private boolean chatSpy; private boolean chatSpy;
private Set<UUID> spiedIslands;
private boolean chat; private boolean chat;
private boolean preview; private boolean preview;
@ -44,6 +47,8 @@ public class PlayerData {
private List<Transaction> transactions; private List<Transaction> transactions;
public PlayerData(Player player) { public PlayerData(Player player) {
this.skyblock = SkyBlock.getInstance();
uuid = player.getUniqueId(); uuid = player.getUniqueId();
islandOwnerUUID = null; islandOwnerUUID = null;
@ -56,6 +61,12 @@ public class PlayerData {
chatSpy = getConfig().getFileConfiguration().getBoolean("ChatSpy", false); chatSpy = getConfig().getFileConfiguration().getBoolean("ChatSpy", false);
if (getConfig().getFileConfiguration().getString("ChatSpiedIslands") != null) {
for (String islandUUID : getConfig().getFileConfiguration().getStringList("ChatSpiedIslands")) {
spiedIslands.add(UUID.fromString(islandUUID));
}
}
chat = false; chat = false;
preview = false; preview = false;
transactions = new ArrayList<>(); transactions = new ArrayList<>();
@ -270,7 +281,7 @@ public class PlayerData {
} }
} }
public void save() { public synchronized void save() {
transactions = BankManager.getInstance().getTransactionList(getPlayerUUID()); transactions = BankManager.getInstance().getTransactionList(getPlayerUUID());
Config config = getConfig(); Config config = getConfig();
FileConfiguration configLoad = config.getFileConfiguration(); FileConfiguration configLoad = config.getFileConfiguration();
@ -288,6 +299,10 @@ public class PlayerData {
}else { }else {
configLoad.set("Bank.Transactions.Size", 0); configLoad.set("Bank.Transactions.Size", 0);
} }
configLoad.set("ChatSpy", chatSpy);
configLoad.set("ChatSpiedIslands", spiedIslands);
try { try {
configLoad.save(config.getFile()); configLoad.save(config.getFile());
} catch (IOException e) { } catch (IOException e) {
@ -318,6 +333,41 @@ public class PlayerData {
public void setChatSpy(boolean chatSpy) { public void setChatSpy(boolean chatSpy) {
this.chatSpy = chatSpy; this.chatSpy = chatSpy;
getConfig().getFileConfiguration().set("ChatSpy", chatSpy); Bukkit.getScheduler().runTaskAsynchronously(skyblock, this::save);
}
public void addChatSpyIsland(UUID uuid) {
spiedIslands.add(uuid);
Bukkit.getScheduler().runTaskAsynchronously(skyblock, this::save);
}
public boolean isChatSpyIsland(UUID uuid) {
return spiedIslands.contains(uuid);
}
public void removeChatSpyIsland(UUID uuid) {
spiedIslands.remove(uuid);
Bukkit.getScheduler().runTaskAsynchronously(skyblock, this::save);
}
public void addChatSpyIsland(Island island) {
this.addChatSpyIsland(island.getOwnerUUID());
}
public boolean isChatSpyIsland(Island island) {
return this.isChatSpyIsland(island.getOwnerUUID());
}
public void removeChatSpyIsland(Island island) {
this.removeChatSpyIsland(island.getOwnerUUID());
}
public boolean isGlobalChatSpy() {
return spiedIslands.isEmpty();
}
public void enableGlobalChatSpy() {
spiedIslands.clear();
Bukkit.getScheduler().runTaskAsynchronously(skyblock, this::save);
} }
} }

View File

@ -12,6 +12,7 @@ import java.util.UUID;
public class OfflinePlayer { public class OfflinePlayer {
private UUID uuid; private UUID uuid;
org.bukkit.OfflinePlayer bukkitOfflinePlayer;
private String name; private String name;
private String memberSince; private String memberSince;
@ -25,11 +26,10 @@ public class OfflinePlayer {
SkyBlock skyblock = SkyBlock.getInstance(); SkyBlock skyblock = SkyBlock.getInstance();
UserCacheManager userCacheManager = skyblock.getUserCacheManager(); UserCacheManager userCacheManager = skyblock.getUserCacheManager();
@SuppressWarnings("deprecation") bukkitOfflinePlayer = Bukkit.getServer().getOfflinePlayer(name);
org.bukkit.OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(name);
this.name = offlinePlayer.getName(); this.name = bukkitOfflinePlayer.getName();
this.uuid = offlinePlayer.getUniqueId(); this.uuid = bukkitOfflinePlayer.getUniqueId();
if (this.uuid == null && userCacheManager.hasUser(name)) { if (this.uuid == null && userCacheManager.hasUser(name)) {
this.uuid = userCacheManager.getUser(name); this.uuid = userCacheManager.getUser(name);
@ -51,9 +51,9 @@ public class OfflinePlayer {
SkyBlock skyblock = SkyBlock.getInstance(); SkyBlock skyblock = SkyBlock.getInstance();
UserCacheManager userCacheManager = skyblock.getUserCacheManager(); UserCacheManager userCacheManager = skyblock.getUserCacheManager();
org.bukkit.OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(uuid); bukkitOfflinePlayer = Bukkit.getServer().getOfflinePlayer(uuid);
this.name = offlinePlayer.getName(); this.name = bukkitOfflinePlayer.getName();
this.uuid = uuid; this.uuid = uuid;
if (this.name == null && userCacheManager.hasUser(uuid)) { if (this.name == null && userCacheManager.hasUser(uuid)) {
@ -103,4 +103,8 @@ public class OfflinePlayer {
public int getPlaytime() { public int getPlaytime() {
return playtime; return playtime;
} }
public org.bukkit.OfflinePlayer getBukkitOfflinePlayer() {
return bukkitOfflinePlayer;
}
} }