Nation color, /tableflip fix, improvements and other fixes #92

Merged
NorbiPeti merged 8 commits from dev into master 2018-11-04 00:47:21 +00:00
3 changed files with 40 additions and 67 deletions
Showing only changes of commit 775789a451 - Show all commits

View file

@ -3,7 +3,6 @@ package buttondevteam.chat.commands.ucmds.admin;
import buttondevteam.chat.PluginMain;
import buttondevteam.chat.listener.TownyListener;
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;
@ -39,26 +38,13 @@ public class TownColorCommand extends AdminCommandBase {
sender.sendMessage("§cThe town '" + args[0] + "' cannot be found.");
return true;
}
Color[] clrs = null; //Add nation color as well
boolean hasNationColor = false;
Color[] clrs = new Color[args.length - 1];
Town targetTown = PluginMain.TU.getTownsMap().get(args[0].toLowerCase());
try {
Color c; //TODO: Add command for nation color
if (targetTown.getNation() != null
&& (c = PluginMain.NationColor.get(targetTown.getNation().getName().toLowerCase())) != null) {
clrs = new Color[args.length];
clrs[0] = c;
hasNationColor = true;
}
} catch (NotRegisteredException ignored) {
}
if (!hasNationColor)
clrs = new Color[args.length - 1];
for (int i = 1; i < args.length; i++) {
val c = getColorOrSendError(args[i], sender);
if (!c.isPresent())
return true;
clrs[hasNationColor ? i : i - 1] = c.get();
clrs[i - 1] = c.get();
}
PluginMain.TownColors.put(args[0].toLowerCase(), clrs);
TownyListener.updateTownMembers(targetTown);

View file

@ -5,6 +5,7 @@ import buttondevteam.chat.FlairStates;
import buttondevteam.chat.PlayerJoinTimerTask;
import buttondevteam.chat.PluginMain;
import buttondevteam.chat.commands.UnlolCommand;
import buttondevteam.lib.chat.Color;
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
import buttondevteam.lib.player.TBMCPlayerSaveEvent;
@ -89,28 +90,41 @@ public class PlayerJoinLeaveListener implements Listener {
String nickname = user.getNick(true);
if (nickname.contains("~")) //StartsWith doesn't work because of color codes
nickname = nickname.replace("~", ""); //It gets stacked otherwise
String name = ChatColor.stripColor(nickname); //Enforce "town colors" on non-members
val res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase());
if (res == null || !res.hasTown())
return nickname;
return name;
try {
val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase());
if (clrs == null)
return nickname;
return name;
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;
BiFunction<Color, Integer, String> anyColoredNamePart = (c, len) -> "§" //Len==0 if last part
+ Integer.toHexString(c.ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end
+ (len == 0 ? name.substring(prevlen.get())
: name.substring(prevlen.get(), prevlen.addAndGet(len)));
BiFunction<Integer, Integer, String> coloredNamePart = (len, i)
-> anyColoredNamePart.apply(clrs[i], i + 1 == clrs.length ? 0 : len);
final int len = name.length() / (clrs.length + 1); //The above param is needed because this isn't always passed
Color nc;
/*if(res.getTown().hasNation()
&&(nc=PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase()))!=null)
len = name.length() / (clrs.length+1);
else
len = name.length() / clrs.length;*/
val nclar = cp.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
//System.out.println("ncl: "+Arrays.toString(ncl)+" - sum: "+Arrays.stream(ncl).sum()+" - name len: "+name.length());
if (!res.getTown().hasNation()
|| (nc = PluginMain.NationColor.get(res.getTown().getNation().getName().toLowerCase())) == null)
nc = Color.White;
ret.append(anyColoredNamePart.apply(nc, ncl == null ? len : ncl[0])); //Make first color the nation color
for (int i = 0; i < clrs.length; i++)
ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i], i));
//ret.append(coloredNamePart.apply(ncl == null ? len : (nc==null?ncl[i]:ncl[i+1]), i));
ret.append(coloredNamePart.apply(ncl == null ? len : ncl[i + 1], i));
return ret.toString();
} catch (NotRegisteredException e) {
return nickname;

View file

@ -1,7 +1,6 @@
package buttondevteam.chat.listener;
import buttondevteam.chat.PluginMain;
import buttondevteam.chat.commands.ucmds.admin.TownColorCommand;
import com.earth2me.essentials.User;
import com.palmergames.bukkit.towny.event.*;
import com.palmergames.bukkit.towny.object.Town;
@ -29,17 +28,7 @@ public class TownyListener implements Listener {
PlayerJoinLeaveListener.updatePlayerColors(p);
}
@EventHandler //Gets called on town load as well
public void onNationJoin(NationAddTownEvent event) {
updateTownMembers(event.getTown());
}
@EventHandler //Gets called on town load as well
public void onNationLeave(NationRemoveTownEvent event) {
updateTownMembers(event.getTown());
}
public static void updateTownMembers(Town town) { //TODO: Update (or remove) nation color from town color
public static void updateTownMembers(Town town) {
town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName()))
.filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors);
}
@ -47,10 +36,13 @@ public class TownyListener implements Listener {
@EventHandler
public void onTownLeave(TownRemoveResidentEvent event) {
Player p = Bukkit.getPlayer(event.getResident().getName());
if (p != null) {
User user = PluginMain.essentials.getUser(p);
user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", "")));
}
if (p != null)
resetNameColor(p);
}
private void resetNameColor(Player p) {
User user = PluginMain.essentials.getUser(p);
user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", "")));
}
@EventHandler
@ -74,44 +66,25 @@ public class TownyListener implements Listener {
PluginMain.NationColor.put(event.getNation().getName().toLowerCase(), clrs);
}
@EventHandler //Gets called on town load as well
public void onNationJoin(NationAddTownEvent event) {
TownColorCommand.SetTownColor()
}
@EventHandler //Gets called on town load as well
public void onNationJoin(NationAddTownEvent event) {
updateTownMembers(event.getTown());
}
@EventHandler //Gets called on town load as well
@EventHandler
public void onNationLeave(NationRemoveTownEvent event) {
updateTownMembers(event.getTown());
}
private void updateTownMembers(Town town) {
town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName()))
.filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors);
updateTownMembers(event.getTown()); //The town still has it's colours
}
@EventHandler
public void onTownLeave(TownRemoveResidentEvent event) {
Player p = Bukkit.getPlayer(event.getResident().getName());
if (p != null) {
User user = PluginMain.essentials.getUser(p);
user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", "")));
}
public void onNationDelete(DeleteNationEvent event) {
PluginMain.NationColor.remove(event.getNationName().toLowerCase());
}
@EventHandler
public void onTownDelete(DeleteTownEvent event) {
PluginMain.TownColors.remove(event.getTownName().toLowerCase());
}
@EventHandler
public void onTownCreate(NewTownEvent event) {
Player p = Bukkit.getPlayer(event.getTown().getMayor().getName());
public void onNationCreate(NewNationEvent event) {
Player p = Bukkit.getPlayer(event.getNation().getCapital().getMayor().getName());
if (p != null)
p.sendMessage("§6Use /u towncolor to set a color for the town.");
p.sendMessage("§6Use /u nationcolor to set a color for the nation.");
}
}