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>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>Towny</artifactId>
<!-- <version>8d3b6b6</version> ButtonCore repo -->
<version>0.96.1.0</version>
<scope>provided</scope>
</dependency>

View file

@ -28,7 +28,7 @@ public class NColorCommand extends UCommandBase {
Resident res;
Town town;
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) {
player.sendMessage("§cYou need to be in a town.");
return true;

View file

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

View file

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

View file

@ -14,6 +14,7 @@ import buttondevteam.lib.player.TBMCPlayerJoinEvent;
import com.earth2me.essentials.User;
import com.palmergames.bukkit.towny.exceptions.NotRegisteredException;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import lombok.Getter;
import lombok.val;
import org.bukkit.Bukkit;
@ -85,9 +86,9 @@ public class TownColorComponent extends Component<PluginMain> implements Listene
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)
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();
@ -119,7 +120,7 @@ public class TownColorComponent extends Component<PluginMain> implements Listene
return;
for (val entry : TownColors.entrySet()) {
try {
val town = TownyComponent.TU.getTownsMap().get(entry.getKey());
val town = TownyComponent.dataSource.getTown(entry.getKey());
Nation nation;
Color nc;
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
nickname = nickname.replace("~", ""); //It gets stacked otherwise
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())
return name;
try {

View file

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

View file

@ -8,6 +8,7 @@ import buttondevteam.lib.chat.Color;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
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.TownyObject;
import lombok.val;
@ -28,12 +29,17 @@ import java.util.stream.Collectors;
public class TownColorCommand extends AdminCommandBase { //TODO: Command path aliases
@Command2.Subcommand
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;
}
Town targetTown = TownyComponent.TU.getTownsMap().get(town.toLowerCase());
try {
Town targetTown = TownyComponent.dataSource.getTown(town);
return SetTownColor(sender, targetTown, colornames);
} catch (NotRegisteredException e) {
sender.sendMessage("§cThe town '" + town + "' cannot be found.");
return true;
}
}
@CustomTabCompleteMethod(param = "colornames")
@ -43,7 +49,7 @@ public class TownColorCommand extends AdminCommandBase { //TODO: Command path al
@CustomTabCompleteMethod(param = "town")
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) {
@ -68,7 +74,7 @@ public class TownColorCommand extends AdminCommandBase { //TODO: Command path al
Color nc;
if (usenc) {
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
nc = null;
}
@ -112,7 +118,11 @@ public class TownColorCommand extends AdminCommandBase { //TODO: Command path al
}
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() {

View file

@ -7,7 +7,7 @@ import buttondevteam.core.component.channel.Channel;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.chat.Color;
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.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
@ -18,7 +18,6 @@ import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
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.
*/
public class TownyComponent extends Component<PluginMain> {
public static TownyUniverse TU;
public static TownyDataSource dataSource;
private static ArrayList<String> Towns;
private static ArrayList<String> Nations;
@ -36,9 +35,19 @@ public class TownyComponent extends Component<PluginMain> {
@Override
protected void enable() {
TU = TownyUniverse.getInstance();
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
Nations = TU.getNationsMap().values().stream().map(Nation::getName).collect(Collectors.toCollection(ArrayList::new)); // Same here but with nations
try {
try {
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(
TownChat = new Channel("§3TC§f", Color.DarkAqua, "tc", s -> checkTownNationChat(s, false)));
TBMCChatAPI.RegisterChatChannel(
@ -62,9 +71,11 @@ public class TownyComponent extends Component<PluginMain> {
public void handleSpies(Channel channel, Player p) {
if (channel.ID.equals(TownChat.ID) || channel.ID.equals(NationChat.ID)) {
if (Optional.ofNullable(TU.getResidentMap().get(p.getName().toLowerCase()))
.filter(r -> r.hasMode("spy")).isPresent())
try {
if (dataSource.getResident(p.getName()).hasMode("spy"))
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) {
if (!(sender instanceof 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);
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