Nation color, /tableflip fix, improvements and other fixes #92
3 changed files with 40 additions and 67 deletions
|
@ -3,7 +3,6 @@ package buttondevteam.chat.commands.ucmds.admin;
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.chat.listener.TownyListener;
|
import buttondevteam.chat.listener.TownyListener;
|
||||||
import buttondevteam.lib.chat.Color;
|
import buttondevteam.lib.chat.Color;
|
||||||
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
|
|
||||||
import com.palmergames.bukkit.towny.object.Town;
|
import com.palmergames.bukkit.towny.object.Town;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
@ -39,26 +38,13 @@ public class TownColorCommand extends AdminCommandBase {
|
||||||
sender.sendMessage("§cThe town '" + args[0] + "' cannot be found.");
|
sender.sendMessage("§cThe town '" + args[0] + "' cannot be found.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
Color[] clrs = null; //Add nation color as well
|
Color[] clrs = new Color[args.length - 1];
|
||||||
boolean hasNationColor = false;
|
|
||||||
Town targetTown = PluginMain.TU.getTownsMap().get(args[0].toLowerCase());
|
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++) {
|
for (int i = 1; i < args.length; i++) {
|
||||||
val c = getColorOrSendError(args[i], sender);
|
val c = getColorOrSendError(args[i], sender);
|
||||||
if (!c.isPresent())
|
if (!c.isPresent())
|
||||||
return true;
|
return true;
|
||||||
clrs[hasNationColor ? i : i - 1] = c.get();
|
clrs[i - 1] = c.get();
|
||||||
}
|
}
|
||||||
PluginMain.TownColors.put(args[0].toLowerCase(), clrs);
|
PluginMain.TownColors.put(args[0].toLowerCase(), clrs);
|
||||||
TownyListener.updateTownMembers(targetTown);
|
TownyListener.updateTownMembers(targetTown);
|
||||||
|
|
|
@ -5,6 +5,7 @@ import buttondevteam.chat.FlairStates;
|
||||||
import buttondevteam.chat.PlayerJoinTimerTask;
|
import buttondevteam.chat.PlayerJoinTimerTask;
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.chat.commands.UnlolCommand;
|
import buttondevteam.chat.commands.UnlolCommand;
|
||||||
|
import buttondevteam.lib.chat.Color;
|
||||||
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
|
import buttondevteam.lib.player.TBMCPlayerJoinEvent;
|
||||||
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
|
import buttondevteam.lib.player.TBMCPlayerLoadEvent;
|
||||||
import buttondevteam.lib.player.TBMCPlayerSaveEvent;
|
import buttondevteam.lib.player.TBMCPlayerSaveEvent;
|
||||||
|
@ -89,28 +90,41 @@ public class PlayerJoinLeaveListener implements Listener {
|
||||||
String nickname = user.getNick(true);
|
String nickname = user.getNick(true);
|
||||||
if (nickname.contains("~")) //StartsWith doesn't work because of color codes
|
if (nickname.contains("~")) //StartsWith doesn't work because of color codes
|
||||||
nickname = nickname.replace("~", ""); //It gets stacked otherwise
|
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());
|
val res = PluginMain.TU.getResidentMap().get(player.getName().toLowerCase());
|
||||||
if (res == null || !res.hasTown())
|
if (res == null || !res.hasTown())
|
||||||
return nickname;
|
return name;
|
||||||
try {
|
try {
|
||||||
val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase());
|
val clrs = PluginMain.TownColors.get(res.getTown().getName().toLowerCase());
|
||||||
if (clrs == null)
|
if (clrs == null)
|
||||||
return nickname;
|
return name;
|
||||||
StringBuilder ret = new StringBuilder();
|
StringBuilder ret = new StringBuilder();
|
||||||
String name = ChatColor.stripColor(nickname);
|
|
||||||
AtomicInteger prevlen = new AtomicInteger();
|
AtomicInteger prevlen = new AtomicInteger();
|
||||||
BiFunction<Integer, Integer, String> coloredNamePart = (len, i) -> "§"
|
BiFunction<Color, Integer, String> anyColoredNamePart = (c, len) -> "§" //Len==0 if last part
|
||||||
+ Integer.toHexString(clrs[i].ordinal()) // 'Odds' are the last character is chopped off so we make sure to include all chars at the end
|
+ Integer.toHexString(c.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())
|
+ (len == 0 ? name.substring(prevlen.get())
|
||||||
: name.substring(prevlen.get(), prevlen.addAndGet(len)));
|
: name.substring(prevlen.get(), prevlen.addAndGet(len)));
|
||||||
int len = name.length() / clrs.length;
|
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();
|
val nclar = cp.NameColorLocations().get();
|
||||||
int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray();
|
int[] ncl = nclar == null ? null : nclar.stream().mapToInt(Integer::intValue).toArray();
|
||||||
if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length))
|
if (ncl != null && (Arrays.stream(ncl).sum() != name.length() || ncl.length != clrs.length))
|
||||||
ncl = null; // Reset if name length changed
|
ncl = null; // Reset if name length changed
|
||||||
//System.out.println("ncl: "+Arrays.toString(ncl)+" - sum: "+Arrays.stream(ncl).sum()+" - name len: "+name.length());
|
//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++)
|
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();
|
return ret.toString();
|
||||||
} catch (NotRegisteredException e) {
|
} catch (NotRegisteredException e) {
|
||||||
return nickname;
|
return nickname;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package buttondevteam.chat.listener;
|
package buttondevteam.chat.listener;
|
||||||
|
|
||||||
import buttondevteam.chat.PluginMain;
|
import buttondevteam.chat.PluginMain;
|
||||||
import buttondevteam.chat.commands.ucmds.admin.TownColorCommand;
|
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.palmergames.bukkit.towny.event.*;
|
import com.palmergames.bukkit.towny.event.*;
|
||||||
import com.palmergames.bukkit.towny.object.Town;
|
import com.palmergames.bukkit.towny.object.Town;
|
||||||
|
@ -29,17 +28,7 @@ public class TownyListener implements Listener {
|
||||||
PlayerJoinLeaveListener.updatePlayerColors(p);
|
PlayerJoinLeaveListener.updatePlayerColors(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler //Gets called on town load as well
|
public static void updateTownMembers(Town town) {
|
||||||
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
|
|
||||||
town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName()))
|
town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName()))
|
||||||
.filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors);
|
.filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors);
|
||||||
}
|
}
|
||||||
|
@ -47,10 +36,13 @@ public class TownyListener implements Listener {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onTownLeave(TownRemoveResidentEvent event) {
|
public void onTownLeave(TownRemoveResidentEvent event) {
|
||||||
Player p = Bukkit.getPlayer(event.getResident().getName());
|
Player p = Bukkit.getPlayer(event.getResident().getName());
|
||||||
if (p != null) {
|
if (p != null)
|
||||||
User user = PluginMain.essentials.getUser(p);
|
resetNameColor(p);
|
||||||
user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", "")));
|
}
|
||||||
}
|
|
||||||
|
private void resetNameColor(Player p) {
|
||||||
|
User user = PluginMain.essentials.getUser(p);
|
||||||
|
user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", "")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
|
@ -74,44 +66,25 @@ public class TownyListener implements Listener {
|
||||||
PluginMain.NationColor.put(event.getNation().getName().toLowerCase(), clrs);
|
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
|
@EventHandler //Gets called on town load as well
|
||||||
public void onNationJoin(NationAddTownEvent event) {
|
public void onNationJoin(NationAddTownEvent event) {
|
||||||
updateTownMembers(event.getTown());
|
updateTownMembers(event.getTown());
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler //Gets called on town load as well
|
@EventHandler
|
||||||
public void onNationLeave(NationRemoveTownEvent event) {
|
public void onNationLeave(NationRemoveTownEvent event) {
|
||||||
updateTownMembers(event.getTown());
|
updateTownMembers(event.getTown()); //The town still has it's colours
|
||||||
}
|
|
||||||
|
|
||||||
private void updateTownMembers(Town town) {
|
|
||||||
town.getResidents().stream().map(r -> Bukkit.getPlayer(r.getName()))
|
|
||||||
.filter(Objects::nonNull).forEach(PlayerJoinLeaveListener::updatePlayerColors);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onTownLeave(TownRemoveResidentEvent event) {
|
public void onNationDelete(DeleteNationEvent event) {
|
||||||
Player p = Bukkit.getPlayer(event.getResident().getName());
|
PluginMain.NationColor.remove(event.getNationName().toLowerCase());
|
||||||
if (p != null) {
|
|
||||||
User user = PluginMain.essentials.getUser(p);
|
|
||||||
user.setNickname(ChatColor.stripColor(user.getNick(true).replace("~", "")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onTownDelete(DeleteTownEvent event) {
|
public void onNationCreate(NewNationEvent event) {
|
||||||
PluginMain.TownColors.remove(event.getTownName().toLowerCase());
|
Player p = Bukkit.getPlayer(event.getNation().getCapital().getMayor().getName());
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onTownCreate(NewTownEvent event) {
|
|
||||||
Player p = Bukkit.getPlayer(event.getTown().getMayor().getName());
|
|
||||||
if (p != null)
|
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.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue