Finally fix Towny errors for 1.12 - use the intended API everywhere

This commit is contained in:
Norbi Peti 2020-04-08 01:10:49 +02:00
parent e4b47efd3f
commit c688ec9243
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
8 changed files with 84 additions and 55 deletions

View file

@ -213,6 +213,7 @@
<dependency> <dependency>
<groupId>com.palmergames.bukkit.towny</groupId> <groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>Towny</artifactId> <artifactId>Towny</artifactId>
<!-- <version>8d3b6b6</version> ButtonCore repo -->
<version>0.96.1.0</version> <version>0.96.1.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View file

@ -28,8 +28,8 @@ public class NColorCommand extends UCommandBase {
Resident res; Resident res;
Town town; Town town;
try { try {
if ((res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase())) == null || !res.hasTown() if ((res = TownyComponent.dataSource.getResident(player.getName())) == null || !res.hasTown()
|| (town = res.getTown()) == null) { || (town = res.getTown()) == null) {
player.sendMessage("§cYou need to be in a town."); player.sendMessage("§cYou need to be in a town.");
return true; return true;
} }

View file

@ -3,7 +3,6 @@ package buttondevteam.chat.components.towncolors;
import buttondevteam.chat.commands.ucmds.UCommandBase; import buttondevteam.chat.commands.ucmds.UCommandBase;
import buttondevteam.chat.components.towncolors.admin.TownColorCommand; import buttondevteam.chat.components.towncolors.admin.TownColorCommand;
import buttondevteam.chat.components.towny.TownyComponent; import buttondevteam.chat.components.towny.TownyComponent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.CustomTabCompleteMethod; import buttondevteam.lib.chat.CustomTabCompleteMethod;
@ -21,21 +20,19 @@ import org.bukkit.entity.Player;
public class NationColorCommand extends UCommandBase { public class NationColorCommand extends UCommandBase {
@Command2.Subcommand @Command2.Subcommand
public boolean def(Player player, String color) { public boolean def(Player player, String color) {
Resident res; String msg = "§cYou need to be the king of a nation to set it's colors.";
if (!(TownyComponent.TU.getResidentMap().containsKey(player.getName().toLowerCase())
&& (res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase())).isKing())) {
player.sendMessage("§cYou need to be the king of a nation to set it's colors.");
return true;
}
final Nation n;
try { try {
n = res.getTown().getNation(); Resident res = TownyComponent.dataSource.getResident(player.getName());
if (!res.isKing()) {
player.sendMessage(msg);
return true;
}
final Nation n = res.getTown().getNation();
return buttondevteam.chat.components.towncolors.admin.NationColorCommand.SetNationColor(player, n, color);
} catch (NotRegisteredException e) { } catch (NotRegisteredException e) {
TBMCCoreAPI.SendException("Failed to set nation color for player " + player + "!", e); player.sendMessage(msg);
player.sendMessage("§cCouldn't find your town/nation... Error reported.");
return true; return true;
} }
return buttondevteam.chat.components.towncolors.admin.NationColorCommand.SetNationColor(player, n, color);
} }
@CustomTabCompleteMethod(param = "color") @CustomTabCompleteMethod(param = "color")

View file

@ -2,7 +2,6 @@ package buttondevteam.chat.components.towncolors;
import buttondevteam.chat.commands.ucmds.UCommandBase; import buttondevteam.chat.commands.ucmds.UCommandBase;
import buttondevteam.chat.components.towny.TownyComponent; import buttondevteam.chat.components.towny.TownyComponent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.CustomTabCompleteMethod; import buttondevteam.lib.chat.CustomTabCompleteMethod;
@ -25,26 +24,24 @@ public class TownColorCommand extends UCommandBase {
@Command2.Subcommand @Command2.Subcommand
public boolean def(Player player, String... colornames) { public boolean def(Player player, String... colornames) {
Resident res; String msg = "§cYou need to be the mayor of a town to set its colors.";
if (!(TownyComponent.TU.getResidentMap().containsKey(player.getName().toLowerCase())
&& (res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase())).isMayor())) {
player.sendMessage("§cYou need to be the mayor of a town to set its colors.");
return true;
}
val cc = component.colorCount().get();
if (colornames.length > cc) {
player.sendMessage("You can only use " + cc + " color" + (cc > 1 ? "s" : "") + ".");
return true;
}
final Town t;
try { try {
t = res.getTown(); Resident res = TownyComponent.dataSource.getResident(player.getName());
if (!res.isMayor()) {
player.sendMessage(msg);
return true;
}
val cc = component.colorCount().get();
if (colornames.length > cc) {
player.sendMessage("You can only use " + cc + " color" + (cc > 1 ? "s" : "") + ".");
return true;
}
final Town t = res.getTown();
return buttondevteam.chat.components.towncolors.admin.TownColorCommand.SetTownColor(player, t, colornames);
} catch (NotRegisteredException e) { } catch (NotRegisteredException e) {
TBMCCoreAPI.SendException("Failed to set town color for player " + player + "!", e); player.sendMessage(msg);
player.sendMessage("§cCouldn't find your town... Error reported.");
return true; return true;
} }
return buttondevteam.chat.components.towncolors.admin.TownColorCommand.SetTownColor(player, t, colornames);
} }
@CustomTabCompleteMethod(param = "colornames") @CustomTabCompleteMethod(param = "colornames")

View file

@ -14,6 +14,7 @@ import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import lombok.Getter; import lombok.Getter;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -85,9 +86,9 @@ public class TownColorComponent extends Component<PluginMain> implements Listene
loadNC.accept(ncs); loadNC.accept(ncs);
} }
TownColors.keySet().removeIf(t -> !TownyComponent.TU.getTownsMap().containsKey(t)); // Removes town colors for deleted/renamed towns TownColors.keySet().removeIf(t -> !TownyComponent.dataSource.hasTown(t)); // Removes town colors for deleted/renamed towns
if (usenc) if (usenc)
NationColor.keySet().removeIf(n -> !TownyComponent.TU.getNationsMap().containsKey(n)); // Removes nation colors for deleted/renamed nations NationColor.keySet().removeIf(n -> !TownyComponent.dataSource.hasNation(n)); // Removes nation colors for deleted/renamed nations
initDynmap(); initDynmap();
@ -119,7 +120,7 @@ public class TownColorComponent extends Component<PluginMain> implements Listene
return; return;
for (val entry : TownColors.entrySet()) { for (val entry : TownColors.entrySet()) {
try { try {
val town = TownyComponent.TU.getTownsMap().get(entry.getKey()); val town = TownyComponent.dataSource.getTown(entry.getKey());
Nation nation; Nation nation;
Color nc; Color nc;
if (!useNationColors().get()) if (!useNationColors().get())
@ -156,7 +157,12 @@ public class TownColorComponent extends Component<PluginMain> implements Listene
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 String name = ChatColor.stripColor(nickname); //Enforce "town colors" on non-members
val res = TownyComponent.TU.getResidentMap().get(player.getName().toLowerCase()); Resident res;
try {
res = TownyComponent.dataSource.getResident(player.getName());
} catch (NotRegisteredException e) {
return name;
}
if (res == null || !res.hasTown()) if (res == null || !res.hasTown())
return name; return name;
try { try {

View file

@ -9,6 +9,7 @@ import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.CustomTabCompleteMethod; import buttondevteam.lib.chat.CustomTabCompleteMethod;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyObject; import com.palmergames.bukkit.towny.object.TownyObject;
@ -23,12 +24,13 @@ import org.bukkit.command.CommandSender;
public class NationColorCommand extends AdminCommandBase { public class NationColorCommand extends AdminCommandBase {
@Command2.Subcommand @Command2.Subcommand
public boolean def(CommandSender sender, String nation, String color) { public boolean def(CommandSender sender, String nation, String color) {
final Nation n = TownyComponent.TU.getNationsMap().get(nation.toLowerCase()); try {
if (n == null) { final Nation n = TownyComponent.dataSource.getNation(nation);
return SetNationColor(sender, n, color);
} catch (NotRegisteredException e) {
sender.sendMessage("§cThe nation '" + nation + "' cannot be found."); sender.sendMessage("§cThe nation '" + nation + "' cannot be found.");
return true; return true;
} }
return SetNationColor(sender, n, color);
} }
@CustomTabCompleteMethod(param = "color") @CustomTabCompleteMethod(param = "color")
@ -38,7 +40,7 @@ public class NationColorCommand extends AdminCommandBase {
@CustomTabCompleteMethod(param = "nation") @CustomTabCompleteMethod(param = "nation")
public Iterable<String> def() { public Iterable<String> def() {
return TownyComponent.TU.getDataSource().getNations().stream().map(TownyObject::getName)::iterator; return TownyComponent.dataSource.getNations().stream().map(TownyObject::getName)::iterator;
} }
public static boolean SetNationColor(CommandSender sender, Nation nation, String color) { public static boolean SetNationColor(CommandSender sender, Nation nation, String color) {

View file

@ -8,6 +8,7 @@ import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.CustomTabCompleteMethod; import buttondevteam.lib.chat.CustomTabCompleteMethod;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Town; import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.TownyObject; import com.palmergames.bukkit.towny.object.TownyObject;
import lombok.val; import lombok.val;
@ -28,12 +29,17 @@ import java.util.stream.Collectors;
public class TownColorCommand extends AdminCommandBase { //TODO: Command path aliases public class TownColorCommand extends AdminCommandBase { //TODO: Command path aliases
@Command2.Subcommand @Command2.Subcommand
public boolean def(CommandSender sender, String town, String... colornames) { public boolean def(CommandSender sender, String town, String... colornames) {
if (!TownyComponent.TU.getTownsMap().containsKey(town.toLowerCase())) { if (!TownyComponent.dataSource.hasTown(town)) {
sender.sendMessage("§cThe town '" + town + "' cannot be found.");
return true;
}
try {
Town targetTown = TownyComponent.dataSource.getTown(town);
return SetTownColor(sender, targetTown, colornames);
} catch (NotRegisteredException e) {
sender.sendMessage("§cThe town '" + town + "' cannot be found."); sender.sendMessage("§cThe town '" + town + "' cannot be found.");
return true; return true;
} }
Town targetTown = TownyComponent.TU.getTownsMap().get(town.toLowerCase());
return SetTownColor(sender, targetTown, colornames);
} }
@CustomTabCompleteMethod(param = "colornames") @CustomTabCompleteMethod(param = "colornames")
@ -43,7 +49,7 @@ public class TownColorCommand extends AdminCommandBase { //TODO: Command path al
@CustomTabCompleteMethod(param = "town") @CustomTabCompleteMethod(param = "town")
public Iterable<String> def() { public Iterable<String> def() {
return TownyComponent.TU.getDataSource().getTowns().stream().map(TownyObject::getName)::iterator; return TownyComponent.dataSource.getTowns().stream().map(TownyObject::getName)::iterator;
} }
public static boolean SetTownColor(CommandSender sender, Town town, String[] colors) { public static boolean SetTownColor(CommandSender sender, Town town, String[] colors) {
@ -68,7 +74,7 @@ public class TownColorCommand extends AdminCommandBase { //TODO: Command path al
Color nc; Color nc;
if (usenc) { if (usenc) {
try { try {
nc = TownColorComponent.NationColor.get(TownyComponent.TU.getTownsMap().get(other.getKey()).getNation().getName().toLowerCase()); nc = TownColorComponent.NationColor.get(TownyComponent.dataSource.getTown(other.getKey()).getNation().getName().toLowerCase());
} catch (Exception e) { //Too lazy for lots of null-checks and it may throw exceptions anyways } catch (Exception e) { //Too lazy for lots of null-checks and it may throw exceptions anyways
nc = null; nc = null;
} }
@ -112,7 +118,11 @@ public class TownColorCommand extends AdminCommandBase { //TODO: Command path al
} }
public static String getTownNameCased(String name) { public static String getTownNameCased(String name) {
return TownyComponent.TU.getTownsMap().get(name.toLowerCase()).getName(); try {
return TownyComponent.dataSource.getTown(name).getName();
} catch (NotRegisteredException e) {
return null;
}
} }
public static Iterable<String> tabcompleteColor() { public static Iterable<String> tabcompleteColor() {

View file

@ -7,7 +7,7 @@ import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.chat.Color; import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import com.palmergames.bukkit.towny.TownyUniverse; import com.palmergames.bukkit.towny.db.TownyDataSource;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation; import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Resident;
@ -18,7 +18,6 @@ import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -27,7 +26,7 @@ import java.util.stream.Collectors;
* It provides the TC and NC channels, and posts Towny messages (global, town, nation) to the correct channels for other platforms like Discord. * It provides the TC and NC channels, and posts Towny messages (global, town, nation) to the correct channels for other platforms like Discord.
*/ */
public class TownyComponent extends Component<PluginMain> { public class TownyComponent extends Component<PluginMain> {
public static TownyUniverse TU; public static TownyDataSource dataSource;
private static ArrayList<String> Towns; private static ArrayList<String> Towns;
private static ArrayList<String> Nations; private static ArrayList<String> Nations;
@ -36,9 +35,19 @@ public class TownyComponent extends Component<PluginMain> {
@Override @Override
protected void enable() { protected void enable() {
TU = TownyUniverse.getInstance(); try {
Towns = TU.getTownsMap().values().stream().map(Town::getName).collect(Collectors.toCollection(ArrayList::new)); // Creates a snapshot of towns, new towns will be added when needed try {
Nations = TU.getNationsMap().values().stream().map(Nation::getName).collect(Collectors.toCollection(ArrayList::new)); // Same here but with nations dataSource = (TownyDataSource) Class.forName("com.palmergames.bukkit.towny.TownyUniverse").getMethod("getDataSource")
.invoke(null);
} catch (ClassNotFoundException e) {
dataSource = (TownyDataSource) Class.forName("com.palmergames.bukkit.towny.object.TownyUniverse").getMethod("getDataSource")
.invoke(null);
}
} catch (Exception e) {
throw new RuntimeException("Failed to find Towny's data source!", e);
}
Towns = dataSource.getTowns().stream().map(Town::getName).collect(Collectors.toCollection(ArrayList::new)); // Creates a snapshot of towns, new towns will be added when needed
Nations = dataSource.getNations().stream().map(Nation::getName).collect(Collectors.toCollection(ArrayList::new)); // Same here but with nations
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(
@ -62,9 +71,11 @@ public class TownyComponent extends Component<PluginMain> {
public void handleSpies(Channel channel, Player p) { public void handleSpies(Channel channel, Player p) {
if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) { if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) {
if (Optional.ofNullable(TU.getResidentMap().get(p.getName().toLowerCase())) try {
.filter(r -> r.hasMode("spy")).isPresent()) if (dataSource.getResident(p.getName()).hasMode("spy"))
VanillaUtils.tellRaw(p, jsonstr); VanillaUtils.tellRaw(p, jsonstr);
} catch (NotRegisteredException ignored) {
}
} }
} }
@ -74,7 +85,12 @@ public class TownyComponent extends Component<PluginMain> {
private static Channel.RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) { private static Channel.RecipientTestResult checkTownNationChat(CommandSender sender, boolean nationchat) {
if (!(sender instanceof Player)) if (!(sender instanceof Player))
return new Channel.RecipientTestResult("§cYou are not a player!"); return new Channel.RecipientTestResult("§cYou are not a player!");
Resident resident = TU.getResidentMap().get(sender.getName().toLowerCase()); Resident resident;
try {
resident = dataSource.getResident(sender.getName());
} catch (NotRegisteredException e) {
resident = null;
}
Channel.RecipientTestResult result = checkTownNationChatInternal(nationchat, resident); Channel.RecipientTestResult result = checkTownNationChatInternal(nationchat, resident);
if (result.errormessage != null && resident != null && resident.getModes().contains("spy")) // Only use spy if they wouldn't see it if (result.errormessage != null && resident != null && resident.getModes().contains("spy")) // Only use spy if they wouldn't see it
result = new Channel.RecipientTestResult(1000, "allspies"); // There won't be more than a thousand towns/nations probably result = new Channel.RecipientTestResult(1000, "allspies"); // There won't be more than a thousand towns/nations probably