Chat fixes, town color fixes, some features #72

Merged
NorbiPeti merged 9 commits from dev into master 2018-04-24 10:14:51 +00:00
6 changed files with 111 additions and 76 deletions
Showing only changes of commit 4006cdad58 - Show all commits

View file

@ -1,16 +1,17 @@
package buttondevteam.chat;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import buttondevteam.lib.chat.*;
import buttondevteam.lib.chat.Channel;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.player.EnumPlayerData;
import buttondevteam.lib.player.PlayerClass;
import buttondevteam.lib.player.PlayerData;
import buttondevteam.lib.player.TBMCPlayerBase;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
@PlayerClass(pluginname = "Button1Chat")
public class ChatPlayer extends TBMCPlayerBase {
@ -104,7 +105,7 @@ public class ChatPlayer extends TBMCPlayerBase {
// PluginMain.Instance.getServer().getScoreboardManager().getMainScoreboard().getTeams().add()
Player p = Bukkit.getPlayer(uuid);
if (p != null)
p.setPlayerListName(String.format("%s%s", p.getName(), GetFormattedFlair()));
p.setPlayerListName(String.format("%s%s", p.getDisplayName(), GetFormattedFlair()));
}
public short GetFlairColor() {

View file

@ -18,21 +18,17 @@ import com.earth2me.essentials.Essentials;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.Objective;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.regex.Pattern;
public class ChatProcessing {
@ -214,35 +210,7 @@ public class ChatProcessing {
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();
StringBuilder ret = new StringBuilder();
String name = ChatColor.stripColor(player.getDisplayName());
AtomicInteger prevlen = new AtomicInteger();
BiFunction<Integer, Integer, String> coloredNamePart = (len, i) -> "§"
+ 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(prevlen.get())
: name.substring(prevlen.get(), prevlen.addAndGet(len)));
int len = name.length() / clrs.length;
val nclar = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get();
int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray();
if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length))
ncl = null; // Reset if name length changed
if (name.charAt(0) == '~') { // Ignore ~ in nicknames
prevlen.incrementAndGet();
ret.append("~");
}
for (int i = 0; i < clrs.length; i++)
ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i], i));
return ret.toString();
} catch (NotRegisteredException e) {
return player.getDisplayName();
}
return player.getDisplayName();
}
static String getChannelID(Channel channel, CommandSender sender) {

View file

@ -1,21 +1,20 @@
package buttondevteam.chat.commands.ucmds;
import buttondevteam.chat.ChatPlayer;
import buttondevteam.chat.PluginMain;
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import buttondevteam.chat.ChatPlayer;
import buttondevteam.chat.PluginMain;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.OptionallyPlayerCommandClass;
@OptionallyPlayerCommandClass(playerOnly = true)
@CommandClass
public class NColorCommand extends UCommandBase {
@ -46,6 +45,7 @@ public class NColorCommand extends UCommandBase {
}
if (args.length == 0)
return false;
String arg = player.getDisplayName().startsWith("~") ? "~" + args[0] : args[0]; //Add ~ for nicknames
if (!args[0].replace("|", "").equalsIgnoreCase(ChatColor.stripColor(player.getDisplayName()))) {
player.sendMessage("§cThe name you gave doesn't match your name. Make sure to use "
+ ChatColor.stripColor(player.getDisplayName()) + "§c with added vertical lines (|).");
@ -71,7 +71,8 @@ public class NColorCommand extends UCommandBase {
}
ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations()
.set(new ArrayList<>(Arrays.stream(nameparts).map(np -> np.length()).collect(Collectors.toList()))); // No byte[], no TIntArrayList
player.sendMessage("§bName colors set.");
PlayerJoinLeaveListener.updatePlayerColors(player);
player.sendMessage("§bName colors set: " + player.getDisplayName());
return true;
}
}

View file

@ -1,7 +1,10 @@
package buttondevteam.chat.commands.ucmds.admin;
import buttondevteam.chat.PluginMain;
import buttondevteam.chat.listener.PlayerJoinLeaveListener;
import buttondevteam.lib.chat.Color;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Town;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
@ -38,18 +41,29 @@ public class TownColorCommand extends AdminCommandBase {
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.");
sender.sendMessage("§cAvailable colors: " + Arrays.stream(Color.values()).map(Color::getName).collect(Collectors.joining(", ")));
val c = Arrays.stream(Color.values()).skip(1).filter(cc -> cc.getName().equalsIgnoreCase(args[ii])).findAny();
if (!c.isPresent()) { //^^ Skip black
sender.sendMessage("§cThe color '" + args[i] + "' cannot be found."); //ˇˇ Skip black
sender.sendMessage("§cAvailable colors: " + Arrays.stream(Color.values()).skip(1).map(col -> String.format("§%x%s§r", col.ordinal(), col.getName())).collect(Collectors.joining(", ")));
return true;
}
clrs[i - 1] = c.get();
}
PluginMain.TownColors.put(args[0].toLowerCase(), clrs);
//PluginMain.TU.getTownsMap().get(args[0].toLowerCase()).getResidents().forEach(r->{
Bukkit.getOnlinePlayers().forEach(p -> {
try {
Town t = PluginMain.TU.getResidentMap().get(p.getName().toLowerCase()).getTown();
if (t != null && t.getName().equalsIgnoreCase(args[0]))
PlayerJoinLeaveListener.updatePlayerColors(p);
} catch (NotRegisteredException e) {
}
});
val dtp = (DynmapTownyPlugin) Bukkit.getPluginManager().getPlugin("Dynmap-Towny");
if (dtp == null) {
sender.sendMessage("§cDynmap-Towny couldn'5 be found to set town color.");
sender.sendMessage("§cDynmap-Towny couldn't be found §6but otherwise §btown color set.");
PluginMain.Instance.getLogger().warning("Dynmap-Towny not found for setting town color!");
return true;
}
PluginMain.setTownColor(dtp, args[0].toLowerCase(), clrs);

View file

@ -9,14 +9,20 @@ import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
import buttondevteam.lib.player.TBMCPlayerSaveEvent;
import com.earth2me.essentials.Essentials;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.Arrays;
import java.util.Timer;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
public class PlayerJoinLeaveListener implements Listener {
@ -72,7 +78,7 @@ public class PlayerJoinLeaveListener implements Listener {
String nwithoutformatting = PluginMain.essentials.getUser(p).getNickname();
p.setDisplayName();
updatePlayerColors(p);
int index;
if (nwithoutformatting != null) {
@ -102,4 +108,40 @@ public class PlayerJoinLeaveListener implements Listener {
UnlolCommand.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer()));
}
private static String getPlayerDisplayName(Player player) {
String nickname = PluginMain.essentials.getUser(player).getNick(true);
val res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase());
if (res == null || !res.hasTown())
return nickname;
try {
val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase());
if (clrs == null)
return nickname;
StringBuilder ret = new StringBuilder();
String name = ChatColor.stripColor(nickname);
AtomicInteger prevlen = new AtomicInteger();
BiFunction<Integer, Integer, String> coloredNamePart = (len, i) -> "§"
+ 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(prevlen.get())
: name.substring(prevlen.get(), prevlen.addAndGet(len)));
int len = name.length() / clrs.length;
val nclar = ChatPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class).NameColorLocations().get();
int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray();
if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length))
ncl = null; // Reset if name length changed
if (name.charAt(0) == '~') { // Ignore ~ in nicknames
prevlen.incrementAndGet();
ret.append("~");
}
for (int i = 0; i < clrs.length; i++)
ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i], i));
return ret.toString();
} catch (NotRegisteredException e) {
return nickname;
}
}
public static void updatePlayerColors(Player player) {
player.setDisplayName(getPlayerDisplayName(player));
}
}

View file

@ -114,22 +114,31 @@ public class PlayerListener implements Listener {
if (player != null && sender instanceof Player)
player.sendMessage("§b" + ((Player) sender).getDisplayName() + " §bis in this world: "
+ ((Player) sender).getWorld().getName());
} else if (cmd.equalsIgnoreCase("minecraft:me")) {
if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) {
String msg = message.substring(index + 1);
Bukkit.broadcastMessage(String.format("* %s %s", ((Player) sender).getDisplayName(), msg));
return true;
} else {
sender.sendMessage("§cCan't use /minecraft:me while muted.");
return true;
}
} else
for (Channel channel : Channel.getChannels()) {
if (cmd.equalsIgnoreCase(channel.ID)) {
TBMCChatAPI.SendChatMessage(channel, sender, message.substring(index + 1));
return true;
}
}
} else if (cmd.equalsIgnoreCase("minecraft:me")) {
if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) {
String msg = message.substring(index + 1);
Bukkit.broadcastMessage(String.format("* %s %s", sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName(), msg));
return true;
} else {
sender.sendMessage("§cCan't use /minecraft:me while muted.");
return true;
}
} else if (cmd.equalsIgnoreCase("me")) { //Take over for Discord broadcast
if (!(sender instanceof Player) || !PluginMain.essentials.getUser((Player) sender).isMuted()) {
String msg = message.substring(index + 1);
Bukkit.broadcastMessage(String.format("§5* %s %s", sender instanceof Player ? ((Player) sender).getDisplayName() : sender.getName(), msg));
return true;
} else {
sender.sendMessage("§cCan't use /me while muted.");
return true;
}
} else
for (Channel channel : Channel.getChannels()) {
if (cmd.equalsIgnoreCase(channel.ID)) {
TBMCChatAPI.SendChatMessage(channel, sender, message.substring(index + 1));
return true;
}
}
// TODO: Target selectors
}
// We don't care if we have arguments