From 308006d624c38c127b6b9b92054925fc53e10c0a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sun, 30 Apr 2017 00:35:42 +0200 Subject: [PATCH] Chat channel improved, tried a bunch of ideas --- .../java/buttondevteam/core/MainPlugin.java | 55 +------- .../java/buttondevteam/lib/chat/Channel.java | 123 ++++++++++++++++-- .../lib/chat/ChatChannelRegisterEvent.java | 27 ++++ .../buttondevteam/lib/chat/TBMCChatAPI.java | 10 ++ 4 files changed, 159 insertions(+), 56 deletions(-) create mode 100644 src/main/java/buttondevteam/lib/chat/ChatChannelRegisterEvent.java diff --git a/src/main/java/buttondevteam/core/MainPlugin.java b/src/main/java/buttondevteam/core/MainPlugin.java index 9d48e81..df3f0ca 100644 --- a/src/main/java/buttondevteam/core/MainPlugin.java +++ b/src/main/java/buttondevteam/core/MainPlugin.java @@ -1,22 +1,21 @@ package buttondevteam.core; -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; +import java.util.ArrayList; import java.util.logging.Logger; import org.bukkit.Bukkit; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scoreboard.Scoreboard; + +import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Town; +import com.palmergames.bukkit.towny.object.TownyUniverse; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.TBMCChatAPI; -import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayerBase; import net.milkbowl.vault.permission.Permission; @@ -27,7 +26,6 @@ public class MainPlugin extends JavaPlugin { private PluginDescriptionFile pdfFile; private Logger logger; - private int C = 0, keep = 0; @Override public void onEnable() { @@ -43,45 +41,6 @@ public class MainPlugin extends JavaPlugin { TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterUserClass(TBMCPlayerBase.class); logger.info(pdfFile.getName() + " has been Enabled (V." + pdfFile.getVersion() + ")."); - Arrays.stream(new File(ChromaGamerBase.TBMC_PLAYERS_DIR).listFiles(f -> !f.isDirectory())).map(f -> { - YamlConfiguration yc = new YamlConfiguration(); - try { - yc.load(f); - } catch (IOException | InvalidConfigurationException e) { - TBMCCoreAPI.SendException("Error while converting player data!", e); - } - f.delete(); - return yc; - }).forEach(yc -> { - try { - int flairtime = yc.getInt("flairtime"), fcount = yc.getInt("fcount"), fdeaths = yc.getInt("fdeaths"); - String flairstate = yc.getString("flairstate"); - List usernames = yc.getStringList("usernames"); - boolean flaircheater = yc.getBoolean("flaircheater"); - final String uuid = yc.getString("uuid"); - C++; - if ((fcount == 0 || fdeaths == 0) - && (flairstate == null || "NoComment".equals(flairstate) || flairtime <= 0)) - return; // Those who received no Fs yet will also get their stats reset if no flair - final File file = new File(ChromaGamerBase.TBMC_PLAYERS_DIR + "minecraft", uuid + ".yml"); - YamlConfiguration targetyc = YamlConfiguration.loadConfiguration(file); - targetyc.set("PlayerName", yc.getString("playername")); - targetyc.set("minecraft_id", uuid); - ConfigurationSection bc = targetyc.createSection("ButtonChat"); - bc.set("FlairTime", "NoComment".equals(flairstate) ? -3 : flairtime); // FlairTimeNone: -3 - bc.set("FCount", fcount); - bc.set("FDeaths", fdeaths); - bc.set("FlairState", flairstate); - bc.set("UserNames", usernames); - bc.set("FlairCheater", flaircheater); - targetyc.save(file); - keep++; - } catch (Exception e) { - TBMCCoreAPI.SendException("Error while converting player data!", e); - } - }); - Bukkit.getScheduler().runTask(this, () -> logger.info("Converted " + keep + " player data from " + C)); - //TODO: Remove once ran it at least once } @Override diff --git a/src/main/java/buttondevteam/lib/chat/Channel.java b/src/main/java/buttondevteam/lib/chat/Channel.java index 5a257a4..178a4fe 100644 --- a/src/main/java/buttondevteam/lib/chat/Channel.java +++ b/src/main/java/buttondevteam/lib/chat/Channel.java @@ -2,26 +2,60 @@ package buttondevteam.lib.chat; import java.util.ArrayList; import java.util.List; +import java.util.function.Function; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; +import com.palmergames.bukkit.towny.object.Nation; +import com.palmergames.bukkit.towny.object.Resident; +import com.palmergames.bukkit.towny.object.Town; + +import buttondevteam.core.MainPlugin; public class Channel { public final String DisplayName; public final Color color; - public final String Command; + public final String ID; + /** + * Filters both the sender and the targets + */ + public final Function filteranderrormsg; private static List channels = new ArrayList<>(); - public Channel(String displayname, Color color, String command) { + /** + * Creates a channel. + * + * @param displayname + * The name that should appear at the start of the message + * @param color + * The default color of the messages sent in the channel + * @param command + * The command to be used for the channel without /. For example "mod". It's also used for scoreboard objective names. + * @param filteranderrormsg + * Checks all senders against the criteria provided here and sends the message returned if it has an index - otherwise displays the error.
+ * May be null to send to everyone. + */ + public Channel(String displayname, Color color, String command, + Function filteranderrormsg) { DisplayName = displayname; this.color = color; - Command = command; + ID = command; + this.filteranderrormsg = filteranderrormsg; } static { - channels.add(GlobalChat = new Channel("§fg§f", Color.White, "g")); - channels.add(TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc")); - channels.add(NationChat = new Channel("§6NC§f", Color.Gold, "nc")); - channels.add(AdminChat = new Channel("§cADMIN§f", Color.Red, "a")); - channels.add(ModChat = new Channel("§9MOD§f", Color.Blue, "mod")); + RegisterChannel(GlobalChat = new Channel("§fg§f", Color.White, "g", null)); + RegisterChannel(TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false))); + RegisterChannel(NationChat = new Channel("§6NC§f", Color.Gold, "nc", s -> checkTownNationChat(s, true))); + RegisterChannel(AdminChat = new Channel("§cADMIN§f", Color.Red, "a", s -> s.isOp() ? new RecipientTestResult(0) + : new RecipientTestResult("You need to be an admin to use this channel."))); + RegisterChannel(ModChat = new Channel("§9MOD§f", Color.Blue, "mod", + s -> s.isOp() || (s instanceof Player && MainPlugin.permission.playerInGroup((Player) s, "mod")) + ? new RecipientTestResult(0) // + : new RecipientTestResult("You need to be a mod to use this channel."))); } public static List getChannels() { @@ -33,4 +67,77 @@ public class Channel { public static Channel NationChat; public static Channel AdminChat; public static Channel ModChat; + + static void RegisterChannel(Channel channel) { + channels.add(channel); + Bukkit.getPluginManager().callEvent(new ChatChannelRegisterEvent(channel)); + } + + /** + * Return the error message for the message sender if they can't send it and the score + */ + private static RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) { + if (!(sender instanceof Player)) + return new RecipientTestResult("§cYou are not a player!"); + try { + Resident resident = MainPlugin.TU.getResidentMap().get(sender.getName().toLowerCase()); + if (resident != null && resident.getModes().contains("spy")) + return null; + /* + * p.sendMessage(String.format("[SPY-%s] - %s: %s", channel.DisplayName, ((Player) sender).getDisplayName(), message)); + */ + Town town = null; + if (resident != null && resident.hasTown()) + town = resident.getTown(); + if (town == null) + return new RecipientTestResult("You aren't in a town."); + Nation nation = null; + int index = -1; // TODO: Move all this to the event in ButtonChat along with the channel definitions and make scores only for the sender... + if (nationchat) { + if (town.hasNation()) + nation = town.getNation(); + if (nation == null) + return new RecipientTestResult("Your town isn't in a nation."); + index = MainPlugin.Nations.indexOf(nation); + if (index < 0) { + MainPlugin.Nations.add(nation); + index = MainPlugin.Nations.size() - 1; + } + } else { + index = MainPlugin.Towns.indexOf(town); + if (index < 0) { + MainPlugin.Towns.add(town); + index = MainPlugin.Towns.size() - 1; + } + } + return new RecipientTestResult(index); + } catch (NotRegisteredException e) { + return new RecipientTestResult("You (probably) aren't knwon by Towny! (Not in a town)"); + } + } + + public static class RecipientTestResult { + public String errormessage; + public int score; + + /** + * Creates a result that indicates an error + * + * @param errormessage + * The error message to show the sender if they don't meet the criteria. + */ + public RecipientTestResult(String errormessage) { + this.errormessage = errormessage; + } + + /** + * Creates a result that indicates a success + * + * @param score + * The score that identifies the target group. For example, the index of the town or nation to send to. + */ + public RecipientTestResult(int score) { + this.score = score; + } // TODO: Set score of players, if they get one, otherwise (on error) set score to -1 + } } diff --git a/src/main/java/buttondevteam/lib/chat/ChatChannelRegisterEvent.java b/src/main/java/buttondevteam/lib/chat/ChatChannelRegisterEvent.java new file mode 100644 index 0000000..d10c54b --- /dev/null +++ b/src/main/java/buttondevteam/lib/chat/ChatChannelRegisterEvent.java @@ -0,0 +1,27 @@ +package buttondevteam.lib.chat; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ChatChannelRegisterEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private Channel channel; + + public ChatChannelRegisterEvent(Channel channel) { + this.channel = channel; + } + + public Channel getChannel() { + return channel; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index a55dad0..cbad418 100644 --- a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -215,4 +215,14 @@ public class TBMCChatAPI { Bukkit.getPluginManager().callEvent(event); return event.isCancelled(); } + + /** + * Register a chat channel. See {@link Channel#Channel(String, Color, String, java.util.function.Function)} for details. + * + * @param channel + * A new {@link Channel} to register + */ + public static void RegisterChatChannel(Channel channel) { + Channel.RegisterChannel(channel); + } }