fixed an npe with friend list

This commit is contained in:
Indyuce 2021-08-25 13:10:53 +02:00
parent 8b85248918
commit 8c95245ffb
2 changed files with 174 additions and 243 deletions

View File

@ -102,12 +102,6 @@
<url>https://papermc.io/repo/repository/maven-public/</url> <url>https://papermc.io/repo/repository/maven-public/</url>
</repository> </repository>
<!-- holographic displays -->
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.io/repository/maven-public/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>
@ -176,7 +170,6 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- Local repo -->
<dependency> <dependency>
<groupId>com.bekvon.bukkit.residence</groupId> <groupId>com.bekvon.bukkit.residence</groupId>
<artifactId>Residence</artifactId> <artifactId>Residence</artifactId>

View File

@ -27,284 +27,222 @@ import org.bukkit.inventory.meta.SkullMeta;
import java.util.UUID; import java.util.UUID;
public class EditableFriendList extends EditableInventory { public class EditableFriendList extends EditableInventory {
public EditableFriendList() { public EditableFriendList() {
super("friend-list"); super("friend-list");
} }
@Override @Override
public InventoryItem load(String function, ConfigurationSection config) { public InventoryItem load(String function, ConfigurationSection config) {
if (function.equals("friend")) if (function.equals("friend"))
return new FriendItem(config); return new FriendItem(config);
if (function.equals("previous")) if (function.equals("previous"))
return new SimplePlaceholderItem<FriendListInventory>(config) { return new SimplePlaceholderItem<FriendListInventory>(config) {
@Override @Override
public boolean canDisplay(FriendListInventory inv) { public boolean canDisplay(FriendListInventory inv) {
return inv.page > 0; return inv.page > 0;
} }
}; };
if (function.equals("next")) if (function.equals("next"))
return new SimplePlaceholderItem<FriendListInventory>(config) { return new SimplePlaceholderItem<FriendListInventory>(config) {
@Override @Override
public boolean canDisplay(FriendListInventory inv) { public boolean canDisplay(FriendListInventory inv) {
return inv.getEditable().getByFunction("friend").getSlots().size() * inv.page < inv.getPlayerData().getFriends().size(); return inv.getEditable().getByFunction("friend").getSlots().size() * inv.page < inv.getPlayerData().getFriends().size();
} }
}; };
return new SimplePlaceholderItem(config); return new SimplePlaceholderItem(config);
} }
public GeneratedInventory newInventory(PlayerData data) { public GeneratedInventory newInventory(PlayerData data) {
return new FriendListInventory(data, this); return new FriendListInventory(data, this);
} }
public static class OfflineFriendItem extends InventoryItem { public static class OfflineFriendItem extends InventoryItem {
public OfflineFriendItem(ConfigurationSection config) { public OfflineFriendItem(ConfigurationSection config) {
super(config); super(config);
} }
@Override @Override
public boolean hasDifferentDisplay() { public boolean hasDifferentDisplay() {
return true; return true;
} }
@Override @Override
public Placeholders getPlaceholders(GeneratedInventory inv, int n) { public Placeholders getPlaceholders(GeneratedInventory inv, int n) {
OfflinePlayer friend = Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n)); OfflinePlayer friend = Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n));
Placeholders holders = new Placeholders(); Placeholders holders = new Placeholders();
holders.register("name", friend.getName()); holders.register("name", friend.getName());
holders.register("last_seen", new DelayFormat(2).format(System.currentTimeMillis() - friend.getLastPlayed())); holders.register("last_seen", new DelayFormat(2).format(System.currentTimeMillis() - friend.getLastPlayed()));
return holders; return holders;
} }
@Override @Override
public ItemStack display(GeneratedInventory inv, int n) { public ItemStack display(GeneratedInventory inv, int n) {
OfflinePlayer friend = Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n)); OfflinePlayer friend = Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n));
ItemStack disp = super.display(inv, n); ItemStack disp = super.display(inv, n);
ItemMeta meta = disp.getItemMeta(); ItemMeta meta = disp.getItemMeta();
if (meta instanceof SkullMeta) if (meta instanceof SkullMeta)
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {
((SkullMeta) meta).setOwningPlayer(friend); ((SkullMeta) meta).setOwningPlayer(friend);
disp.setItemMeta(meta); disp.setItemMeta(meta);
}); });
return NBTItem.get(disp).addTag(new ItemTag("uuid", friend.getUniqueId().toString())).toItem(); return NBTItem.get(disp).addTag(new ItemTag("uuid", friend.getUniqueId().toString())).toItem();
} }
} }
public static class OnlineFriendItem extends SimplePlaceholderItem { public static class OnlineFriendItem extends SimplePlaceholderItem {
public OnlineFriendItem(ConfigurationSection config) { public OnlineFriendItem(ConfigurationSection config) {
super(config); super(config);
} }
@Override @Override
public boolean hasDifferentDisplay() { public boolean hasDifferentDisplay() {
return true; return true;
} }
@Override @Override
public Placeholders getPlaceholders(GeneratedInventory inv, int n) { public Placeholders getPlaceholders(GeneratedInventory inv, int n) {
Player friend = Bukkit.getPlayer(inv.getPlayerData().getFriends().get(n)); Player friend = Bukkit.getPlayer(inv.getPlayerData().getFriends().get(n));
PlayerData data = PlayerData.get(friend); PlayerData data = PlayerData.get(friend);
Placeholders holders = new Placeholders(); Placeholders holders = new Placeholders();
if (data.isOnline()) if (data.isOnline())
holders.register("name", data.getPlayer().getName()); holders.register("name", data.getPlayer().getName());
holders.register("class", data.getProfess().getName()); holders.register("class", data.getProfess().getName());
holders.register("level", data.getLevel()); holders.register("level", data.getLevel());
holders.register("online_since", new DelayFormat(2).format(System.currentTimeMillis() - data.getLastLogin())); holders.register("online_since", new DelayFormat(2).format(System.currentTimeMillis() - data.getLastLogin()));
return holders; return holders;
} }
@Override @Override
public ItemStack display(GeneratedInventory inv, int n) { public ItemStack display(GeneratedInventory inv, int n) {
Player friend = Bukkit.getPlayer(inv.getPlayerData().getFriends().get(n)); Player friend = Bukkit.getPlayer(inv.getPlayerData().getFriends().get(n));
ItemStack disp = super.display(inv, n); ItemStack disp = super.display(inv, n);
ItemMeta meta = disp.getItemMeta(); ItemMeta meta = disp.getItemMeta();
if (meta instanceof SkullMeta) if (meta instanceof SkullMeta)
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> { Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {
((SkullMeta) meta).setOwningPlayer(friend); ((SkullMeta) meta).setOwningPlayer(friend);
disp.setItemMeta(meta); disp.setItemMeta(meta);
}); });
return NBTItem.get(disp).addTag(new ItemTag("uuid", friend.getUniqueId().toString())).toItem(); return NBTItem.get(disp).addTag(new ItemTag("uuid", friend.getUniqueId().toString())).toItem();
} }
} }
public static class FriendItem extends SimplePlaceholderItem { public static class FriendItem extends SimplePlaceholderItem {
private final OnlineFriendItem online; private final OnlineFriendItem online;
private final OfflineFriendItem offline; private final OfflineFriendItem offline;
public FriendItem(ConfigurationSection config) { public FriendItem(ConfigurationSection config) {
super(config); super(config);
Validate.notNull(config.contains("online"), "Could not load online config"); Validate.notNull(config.contains("online"), "Could not load online config");
Validate.notNull(config.contains("offline"), "Could not load offline config"); Validate.notNull(config.contains("offline"), "Could not load offline config");
online = new OnlineFriendItem(config.getConfigurationSection("online")); online = new OnlineFriendItem(config.getConfigurationSection("online"));
offline = new OfflineFriendItem(config.getConfigurationSection("offline")); offline = new OfflineFriendItem(config.getConfigurationSection("offline"));
} }
@Override @Override
public ItemStack display(GeneratedInventory inv, int n) { public ItemStack display(GeneratedInventory inv, int n) {
return inv.getPlayerData().getFriends().size() <= n ? super.display(inv, n) return inv.getPlayerData().getFriends().size() <= n ? super.display(inv, n)
: Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n)).isOnline() ? online.display(inv, n) : offline.display(inv, n); : Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n)).isOnline() ? online.display(inv, n) : offline.display(inv, n);
} }
@Override @Override
public boolean hasDifferentDisplay() { public boolean hasDifferentDisplay() {
return true; return true;
} }
@Override @Override
public boolean canDisplay(GeneratedInventory inv) { public boolean canDisplay(GeneratedInventory inv) {
return true; return true;
} }
} }
public class FriendListInventory extends GeneratedInventory { public class FriendListInventory extends GeneratedInventory {
private int page; private int page;
public FriendListInventory(PlayerData playerData, EditableInventory editable) { public FriendListInventory(PlayerData playerData, EditableInventory editable) {
super(playerData, editable); super(playerData, editable);
} }
@Override @Override
public String calculateName() { public String calculateName() {
return getName(); return getName();
} }
@Override @Override
public void whenClicked(InventoryClickEvent event, InventoryItem item) { public void whenClicked(InventoryClickEvent event, InventoryItem item) {
if (item.getFunction().equals("previous")) { if (item.getFunction().equals("previous")) {
page--; page--;
open(); open();
return; return;
} }
if (item.getFunction().equals("next")) { if (item.getFunction().equals("next")) {
page++; page++;
open(); open();
return; return;
} }
if (item.getFunction().equals("request")) { if (item.getFunction().equals("request")) {
long remaining = playerData.getLastFriendRequest() + 60 * 2 * 1000 - System.currentTimeMillis(); long remaining = playerData.getLastFriendRequest() + 60 * 2 * 1000 - System.currentTimeMillis();
if (remaining > 0) { if (remaining > 0) {
MMOCore.plugin.configManager.getSimpleMessage("friend-request-cooldown", "cooldown", new DelayFormat().format(remaining)) MMOCore.plugin.configManager.getSimpleMessage("friend-request-cooldown", "cooldown", new DelayFormat().format(remaining))
.send(player); .send(player);
return; return;
} }
MMOCore.plugin.configManager.newPlayerInput(player, InputType.FRIEND_REQUEST, (input) -> { MMOCore.plugin.configManager.newPlayerInput(player, InputType.FRIEND_REQUEST, (input) -> {
Player target = Bukkit.getPlayer(input); Player target = Bukkit.getPlayer(input);
if (target == null) { if (target == null) {
MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player); MMOCore.plugin.configManager.getSimpleMessage("not-online-player", "player", input).send(player);
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1); player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
open(); open();
return; return;
} }
if (playerData.hasFriend(target.getUniqueId())) { if (playerData.hasFriend(target.getUniqueId())) {
MMOCore.plugin.configManager.getSimpleMessage("already-friends", "player", target.getName()).send(player); MMOCore.plugin.configManager.getSimpleMessage("already-friends", "player", target.getName()).send(player);
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1); player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
open(); open();
return; return;
} }
if (playerData.getUniqueId().equals(target.getUniqueId())) { if (playerData.getUniqueId().equals(target.getUniqueId())) {
MMOCore.plugin.configManager.getSimpleMessage("cant-request-to-yourself").send(player); MMOCore.plugin.configManager.getSimpleMessage("cant-request-to-yourself").send(player);
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1); player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
open(); open();
return; return;
} }
playerData.sendFriendRequest(PlayerData.get(target)); playerData.sendFriendRequest(PlayerData.get(target));
MMOCore.plugin.configManager.getSimpleMessage("sent-friend-request", "player", target.getName()).send(player); MMOCore.plugin.configManager.getSimpleMessage("sent-friend-request", "player", target.getName()).send(player);
player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1); player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
open(); open();
}); });
} }
if (item.getFunction().equals("friend") && event.getAction() == InventoryAction.PICKUP_HALF) if (item.getFunction().equals("friend") && event.getAction() == InventoryAction.PICKUP_HALF) {
InventoryManager.FRIEND_REMOVAL.newInventory(playerData, String tag = NBTItem.get(event.getCurrentItem()).getString("uuid");
Bukkit.getOfflinePlayer(UUID.fromString(NBTItem.get(event.getCurrentItem()).getString("uuid"))), this).open(); if (tag == null || tag.isEmpty())
} return;
}
// private int page; InventoryManager.FRIEND_REMOVAL.newInventory(playerData, Bukkit.getOfflinePlayer(UUID.fromString(tag)), this).open();
// private ItemStack next, prev, newreq; }
// }
// private int[] slots = { 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, }
// 24, 25, 28, 29, 30, 31, 32, 33, 34 };
//
// public EditableFriendList(Player player) {
// this(player, 1);
// }
//
// public EditableFriendList(Player player, int page) {
// super(player);
// this.page = page;
// }
//
// @Override
// public Inventory getInventory() {
// Inventory inv = Bukkit.createInventory(this, 54, "Friends");
//
// int n = 0;
// for (int j = 21 * (page - 1); j < Math.min(21 * page,
// playerData.getFriends().size()); j++) {
//
// /*
// * if the player is not found, delete it from the friend list.
// */
// UUID uuid = playerData.getFriends().get(j);
// OfflinePlayer offline = Bukkit.getOfflinePlayer(uuid);
// if (offline == null || offline.getName() == null) {
// playerData.removeFriend(uuid);
// continue;
// }
//
// PlayerDataManager data;
// ItemStack item = offline.isOnline() ? new
// ConfigItem("ONLINE_FRIEND").addPlaceholders("name", offline.getName(),
// "online_since", "" + new DelayFormat(2).format(System.currentTimeMillis()
// - (data = PlayerDataManager.get(offline)).getLastLogin()), "class",
// data.getProfess().getName(), "level", "" + data.getLevel()).build() : new
// ConfigItem("OFFLINE_FRIEND").addPlaceholders("name", offline.getName(),
// "last_seen", new DelayFormat(2).format(System.currentTimeMillis() -
// offline.getLastPlayed())).build();
//
// SkullMeta meta = (SkullMeta) item.getItemMeta();
// meta.setOwningPlayer(offline);
// item.setItemMeta(meta);
//
// inv.setItem(slots[n++], NBTItem.get(item).add(new ItemTag("uuid",
// offline.getUniqueId().toString())).toItem());
// }
//
// if (page > 1)
// inv.setItem(18, prev = new ConfigItem("PREVIOUS_PAGE").build());
//
// if (playerData.getFriends().size() > 21 * page)
// inv.setItem(26, next = new ConfigItem("NEXT_PAGE").build());
//
// ItemStack fill = new ConfigItem("NO_FRIEND").build();
// while (n < 21 * page)
// inv.setItem(slots[n++], fill);
//
// inv.setItem(49, newreq = new ConfigItem("NEW_FRIEND_REQUEST").build());
//
// return inv;
// }
} }