Added town colors to chat (still need for Dynmap)

This commit is contained in:
Norbi Peti 2017-09-24 02:37:27 +02:00
parent e0459fa36f
commit 9abcde610e
5 changed files with 320 additions and 228 deletions

447
.gitignore vendored
View file

@ -1,223 +1,224 @@
################# #################
## Eclipse ## Eclipse
################# #################
*.pydevproject *.pydevproject
.metadata/ .metadata/
bin/ bin/
tmp/ tmp/
*.tmp *.tmp
*.bak *.bak
*.swp *.swp
*~.nib *~.nib
local.properties local.properties
.classpath .classpath
.settings/ .settings/
.loadpath .loadpath
target/ target/
.project .project
# External tool builders # External tool builders
.externalToolBuilders/ .externalToolBuilders/
# Locally stored "Eclipse launch configurations" # Locally stored "Eclipse launch configurations"
*.launch *.launch
# CDT-specific # CDT-specific
.cproject .cproject
# PDT-specific # PDT-specific
.buildpath .buildpath
################# #################
## Visual Studio ## Visual Studio
################# #################
## Ignore Visual Studio temporary files, build results, and ## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons. ## files generated by popular Visual Studio add-ons.
# User-specific files # User-specific files
*.suo *.suo
*.user *.user
*.sln.docstates *.sln.docstates
# Build results # Build results
[Dd]ebug/ [Dd]ebug/
[Rr]elease/ [Rr]elease/
x64/ x64/
build/ build/
[Bb]in/ [Bb]in/
[Oo]bj/ [Oo]bj/
# MSTest test Results # MSTest test Results
[Tt]est[Rr]esult*/ [Tt]est[Rr]esult*/
[Bb]uild[Ll]og.* [Bb]uild[Ll]og.*
*_i.c *_i.c
*_p.c *_p.c
*.ilk *.ilk
*.meta *.meta
*.obj *.obj
*.pch *.pch
*.pdb *.pdb
*.pgc *.pgc
*.pgd *.pgd
*.rsp *.rsp
*.sbr *.sbr
*.tlb *.tlb
*.tli *.tli
*.tlh *.tlh
*.tmp *.tmp
*.tmp_proj *.tmp_proj
*.log *.log
*.vspscc *.vspscc
*.vssscc *.vssscc
.builds .builds
*.pidb *.pidb
*.log *.log
*.scc *.scc
# Visual C++ cache files # Visual C++ cache files
ipch/ ipch/
*.aps *.aps
*.ncb *.ncb
*.opensdf *.opensdf
*.sdf *.sdf
*.cachefile *.cachefile
# Visual Studio profiler # Visual Studio profiler
*.psess *.psess
*.vsp *.vsp
*.vspx *.vspx
# Guidance Automation Toolkit # Guidance Automation Toolkit
*.gpState *.gpState
# ReSharper is a .NET coding add-in # ReSharper is a .NET coding add-in
_ReSharper*/ _ReSharper*/
*.[Rr]e[Ss]harper *.[Rr]e[Ss]harper
# TeamCity is a build add-in # TeamCity is a build add-in
_TeamCity* _TeamCity*
# DotCover is a Code Coverage Tool # DotCover is a Code Coverage Tool
*.dotCover *.dotCover
# NCrunch # NCrunch
*.ncrunch* *.ncrunch*
.*crunch*.local.xml .*crunch*.local.xml
# Installshield output folder # Installshield output folder
[Ee]xpress/ [Ee]xpress/
# DocProject is a documentation generator add-in # DocProject is a documentation generator add-in
DocProject/buildhelp/ DocProject/buildhelp/
DocProject/Help/*.HxT DocProject/Help/*.HxT
DocProject/Help/*.HxC DocProject/Help/*.HxC
DocProject/Help/*.hhc DocProject/Help/*.hhc
DocProject/Help/*.hhk DocProject/Help/*.hhk
DocProject/Help/*.hhp DocProject/Help/*.hhp
DocProject/Help/Html2 DocProject/Help/Html2
DocProject/Help/html DocProject/Help/html
# Click-Once directory # Click-Once directory
publish/ publish/
# Publish Web Output # Publish Web Output
*.Publish.xml *.Publish.xml
*.pubxml *.pubxml
*.publishproj *.publishproj
# NuGet Packages Directory # NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line ## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/ #packages/
# Windows Azure Build Output # Windows Azure Build Output
csx csx
*.build.csdef *.build.csdef
# Windows Store app package directory # Windows Store app package directory
AppPackages/ AppPackages/
# Others # Others
sql/ sql/
*.Cache *.Cache
ClientBin/ ClientBin/
[Ss]tyle[Cc]op.* [Ss]tyle[Cc]op.*
~$* ~$*
*~ *~
*.dbmdl *.dbmdl
*.[Pp]ublish.xml *.[Pp]ublish.xml
*.pfx *.pfx
*.publishsettings *.publishsettings
# RIA/Silverlight projects # RIA/Silverlight projects
Generated_Code/ Generated_Code/
# Backup & report files from converting an old project file to a newer # Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-) # Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/ _UpgradeReport_Files/
Backup*/ Backup*/
UpgradeLog*.XML UpgradeLog*.XML
UpgradeLog*.htm UpgradeLog*.htm
# SQL Server files # SQL Server files
App_Data/*.mdf App_Data/*.mdf
App_Data/*.ldf App_Data/*.ldf
############# #############
## Windows detritus ## Windows detritus
############# #############
# Windows image file caches # Windows image file caches
Thumbs.db Thumbs.db
ehthumbs.db ehthumbs.db
# Folder config file # Folder config file
Desktop.ini Desktop.ini
# Recycle Bin used on file shares # Recycle Bin used on file shares
$RECYCLE.BIN/ $RECYCLE.BIN/
# Mac crap # Mac crap
.DS_Store .DS_Store
############# #############
## Python ## Python
############# #############
*.py[cod] *.py[cod]
# Packages # Packages
*.egg *.egg
*.egg-info *.egg-info
dist/ dist/
build/ build/
eggs/ eggs/
parts/ parts/
var/ var/
sdist/ sdist/
develop-eggs/ develop-eggs/
.installed.cfg .installed.cfg
# Installer logs # Installer logs
pip-log.txt pip-log.txt
# Unit test / coverage reports # Unit test / coverage reports
.coverage .coverage
.tox .tox
#Translations #Translations
*.mo *.mo
#Mr Developer #Mr Developer
.mr.developer.cfg .mr.developer.cfg
.metadata/* .metadata/*
TheButtonAutoFlair/out/artifacts/Autoflair/Autoflair.jar TheButtonAutoFlair/out/artifacts/Autoflair/Autoflair.jar
*.iml *.iml
*.name *.name
.idea/compiler.xml .idea/compiler.xml
*.xml *.xml
/.apt_generated/

View file

@ -12,7 +12,7 @@ import buttondevteam.lib.player.PlayerClass;
import buttondevteam.lib.player.PlayerData; import buttondevteam.lib.player.PlayerData;
import buttondevteam.lib.player.TBMCPlayerBase; import buttondevteam.lib.player.TBMCPlayerBase;
@PlayerClass(pluginname = "ButtonChat") @PlayerClass(pluginname = "Button1Chat")
public class ChatPlayer extends TBMCPlayerBase { public class ChatPlayer extends TBMCPlayerBase {
public PlayerData<String> UserName() { public PlayerData<String> UserName() {
return data(null); return data(null);

View file

@ -7,6 +7,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound; import org.bukkit.Sound;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -16,6 +17,8 @@ import com.earth2me.essentials.Essentials;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import buttondevteam.chat.commands.UnlolCommand; import buttondevteam.chat.commands.UnlolCommand;
import buttondevteam.chat.commands.ucmds.admin.DebugCommand; import buttondevteam.chat.commands.ucmds.admin.DebugCommand;
import buttondevteam.chat.formatting.*; import buttondevteam.chat.formatting.*;
@ -169,8 +172,8 @@ public class ChatProcessing {
sender.sendMessage("§cAn error occured while sending the message."); sender.sendMessage("§cAn error occured while sending the message.");
return true; return true;
} }
PluginMain.Instance.getServer().getConsoleSender().sendMessage(String.format("%s <%s> %s", channelidentifier, PluginMain.Instance.getServer().getConsoleSender()
(player != null ? player.getDisplayName() : sender.getName()), message)); .sendMessage(String.format("%s <%s§r> %s", channelidentifier, getSenderName(sender, player), message));
DebugCommand.SendDebugMessage( DebugCommand.SendDebugMessage(
"-- Full ChatProcessing time: " + (System.nanoTime() - processstart) / 1000000f + " ms"); "-- Full ChatProcessing time: " + (System.nanoTime() - processstart) / 1000000f + " ms");
DebugCommand.SendDebugMessage("-- ChatFormatter.Combine time: " + combinetime / 1000000f + " ms"); DebugCommand.SendDebugMessage("-- ChatFormatter.Combine time: " + combinetime / 1000000f + " ms");
@ -199,8 +202,8 @@ public class ChatProcessing {
json.addExtra(new TellrawPart(" <")); json.addExtra(new TellrawPart(" <"));
json.addExtra( json.addExtra(
new TellrawPart( new TellrawPart(
(player != null ? player.getDisplayName() getSenderName(
: sender.getName())) sender, player))
.setHoverEvent( .setHoverEvent(
TellrawEvent TellrawEvent
.create(TellrawEvent.HoverAction.SHOW_TEXT, .create(TellrawEvent.HoverAction.SHOW_TEXT,
@ -244,6 +247,30 @@ public class ChatProcessing {
return json; return json;
} }
private static String getSenderName(CommandSender sender, Player player) {
if (player == null)
return sender.getName();
val res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase());
if (res == null || !res.hasTown())
return player.getDisplayName();
try {
val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase());
if (clrs == null)
return player.getDisplayName();
String ret = "";
String name = ChatColor.stripColor(player.getDisplayName());
int len = name.length() / clrs.length;
// val bounds = new int[clrs.length - 1];
// for (int i = 0; i < clrs.length; i++)
for (int i = 0; i < clrs.length; i++)
ret += "§" + Integer.toHexString(clrs[i].ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end
+ (i + 1 == clrs.length ? name.substring(len * i) : name.substring(len * i, len * i + len));
return ret;
} catch (NotRegisteredException e) {
return player.getDisplayName();
}
}
static String getChannelID(Channel channel, CommandSender sender) { static String getChannelID(Channel channel, CommandSender sender) {
final String channelidentifier = ("[" + (sender instanceof IDiscordSender ? "d|" : "") + channel.DisplayName) final String channelidentifier = ("[" + (sender instanceof IDiscordSender ? "d|" : "") + channel.DisplayName)
+ "]"; + "]";

View file

@ -23,6 +23,8 @@ import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.Channel.RecipientTestResult; import buttondevteam.lib.chat.Channel.RecipientTestResult;
import buttondevteam.lib.player.TBMCPlayerBase; import buttondevteam.lib.player.TBMCPlayerBase;
import lombok.val;
import com.earth2me.essentials.Essentials; import com.earth2me.essentials.Essentials;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
@ -42,11 +44,16 @@ import java.net.URLConnection;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.stream.Collectors;
public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15. public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
// A user, which flair isn't obtainable: // A user, which flair isn't obtainable:
@ -89,6 +96,8 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
Towns = new ArrayList<Town>(TU.getTownsMap().values()); // Creates a snapshot of towns, new towns will be added when needed Towns = new ArrayList<Town>(TU.getTownsMap().values()); // Creates a snapshot of towns, new towns will be added when needed
Nations = new ArrayList<Nation>(TU.getNationsMap().values()); // Same here but with nations Nations = new ArrayList<Nation>(TU.getNationsMap().values()); // Same here but with nations
TownColors.keySet().removeIf(t -> !TU.getTownsMap().containsKey(t.toLowerCase())); // Removes town colors for deleted/renamed towns
TBMCChatAPI.RegisterChatChannel( TBMCChatAPI.RegisterChatChannel(
TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false))); TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false)));
TBMCChatAPI.RegisterChatChannel( TBMCChatAPI.RegisterChatChannel(
@ -253,7 +262,9 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
public static ArrayList<String> AnnounceMessages = new ArrayList<>(); public static ArrayList<String> AnnounceMessages = new ArrayList<>();
public static int AnnounceTime = 15 * 60 * 1000; public static int AnnounceTime = 15 * 60 * 1000;
public static Map<String, Color[]> TownColors = new HashMap<>();
@SuppressWarnings("unchecked")
public static void LoadFiles() { public static void LoadFiles() {
PluginMain.Instance.getLogger().info("Loading files..."); PluginMain.Instance.getLogger().info("Loading files...");
try { try {
@ -266,6 +277,11 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
AnnounceTime = yc.getInt("announcetime"); AnnounceTime = yc.getInt("announcetime");
AnnounceMessages.addAll(yc.getStringList("announcements")); AnnounceMessages.addAll(yc.getStringList("announcements"));
PlayerListener.AlphaDeaths = yc.getInt("alphadeaths"); PlayerListener.AlphaDeaths = yc.getInt("alphadeaths");
val cs = yc.getConfigurationSection("towncolors");
if (cs != null)
TownColors.putAll(cs.getValues(true).entrySet().stream()
.collect(Collectors.toMap(k -> k.getKey(), v -> ((List<String>) v.getValue()).stream()
.map(c -> Color.valueOf(c)).toArray(Color[]::new))));
} }
PluginMain.Instance.getLogger().info("Loaded files!"); PluginMain.Instance.getLogger().info("Loaded files!");
} catch (Exception e) { } catch (Exception e) {
@ -283,6 +299,8 @@ public class PluginMain extends JavaPlugin { // Translated to Java: 2015.07.15.
yc.set("announcetime", AnnounceTime); yc.set("announcetime", AnnounceTime);
yc.set("announcements", AnnounceMessages); yc.set("announcements", AnnounceMessages);
yc.set("alphadeaths", PlayerListener.AlphaDeaths); yc.set("alphadeaths", PlayerListener.AlphaDeaths);
yc.createSection("towncolors", TownColors.entrySet().stream().collect(Collectors.toMap(k -> k.getKey(),
v -> Arrays.stream(v.getValue()).map(c -> c.toString()).toArray(String[]::new))));
yc.save(file); yc.save(file);
PluginMain.Instance.getLogger().info("Saved files!"); PluginMain.Instance.getLogger().info("Saved files!");
} catch (Exception e) { } catch (Exception e) {

View file

@ -0,0 +1,46 @@
package buttondevteam.chat.commands.ucmds.admin;
import java.util.Arrays;
import org.bukkit.command.CommandSender;
import buttondevteam.chat.PluginMain;
import buttondevteam.lib.chat.Color;
import lombok.val;
public class TownColorCommand extends AdminCommandBase {
@Override
public String GetHelpText(String alias)[] { // TODO: Command path aliases
return new String[] { //
"§6---- Town Color ----", //
"This command allows setting a color for a town.", //
"The town will be shown with this color on Dynmap and all players in the town will appear in chat with these colors.", //
"The colors will split the name evenly.", //
"Usage: /" + GetCommandPath() + " <town> <colorname1> [colorname2...]", //
"Example: /" + GetCommandPath() + " Alderon blue gray" //
};
}
@Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (args.length < 2)
return false;
if (!PluginMain.TU.getTownsMap().containsKey(args[0])) {
sender.sendMessage("§cThe town '" + args[0] + "' cannot be found.");
return true;
}
val clrs = new Color[args.length - 1];
for (int i = 1; i < args.length; i++) {
val ii = i;
val c = Arrays.stream(Color.values()).filter(cc -> cc.getName().equalsIgnoreCase(args[ii])).findAny();
if (!c.isPresent()) {
sender.sendMessage("§cThe color '" + args[i] + "' cannot be found.");
return true;
}
clrs[i - 1] = c.get();
}
PluginMain.TownColors.put(args[0], clrs);
sender.sendMessage("§bColor(s) set.");
return true;
}
}