New placeholders

This commit is contained in:
Jules 2023-04-14 17:57:49 +02:00
parent 039baed3d3
commit afe3eec3fb
6 changed files with 104 additions and 45 deletions

View File

@ -16,8 +16,12 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
public class RPGPlaceholders extends PlaceholderExpansion {
@ -46,6 +50,8 @@ public class RPGPlaceholders extends PlaceholderExpansion {
return MMOCore.plugin.getDescription().getVersion();
}
private static final String ERROR_PLACEHOLDER = " ";
@SuppressWarnings("DuplicateExpressions")
@Override
public String onRequest(OfflinePlayer player, String identifier) {
@ -59,7 +65,7 @@ public class RPGPlaceholders extends PlaceholderExpansion {
return playerData.getProfess().getManaDisplay().getName();
if (identifier.equals("level"))
return "" + playerData.getLevel();
return String.valueOf(playerData.getLevel());
else if (identifier.startsWith("skill_level_")) {
String id = identifier.substring(12);
@ -118,14 +124,39 @@ public class RPGPlaceholders extends PlaceholderExpansion {
playerData.getCollectionSkills().getExperience(identifier.substring(22).replace(" ", "-").replace("_", "-").toLowerCase()));
else if (identifier.startsWith("profession_next_level_"))
return "" + PlayerData.get(player).getCollectionSkills()
.getLevelUpExperience(identifier.substring(22).replace(" ", "-").replace("_", "-").toLowerCase());
return String.valueOf(PlayerData.get(player).getCollectionSkills()
.getLevelUpExperience(identifier.substring(22).replace(" ", "-").replace("_", "-").toLowerCase()));
else if (identifier.startsWith("party_count")) {
AbstractParty party = playerData.getParty();
final @Nullable AbstractParty party = playerData.getParty();
return party == null ? "0" : String.valueOf(party.countMembers());
}
else if (identifier.startsWith("party_member_")) {
final int n = Integer.parseInt(identifier.substring(13)) - 1;
final @Nullable AbstractParty party = playerData.getParty();
if (party == null) return ERROR_PLACEHOLDER;
if (n >= party.countMembers()) return ERROR_PLACEHOLDER;
final @Nullable PlayerData member = party.getMember(n);
if (member == null) return ERROR_PLACEHOLDER;
return member.getPlayer().getName();
}
else if (identifier.equals("online_friends")) {
int count = 0;
for (UUID friendId : playerData.getFriends())
if (Bukkit.getPlayer(friendId) != null) count++;
return String.valueOf(count);
}
else if (identifier.startsWith("online_friend_")) {
final int n = Integer.parseInt(identifier.substring(14)) - 1;
if (n >= playerData.getFriends().size()) return ERROR_PLACEHOLDER;
final @Nullable Player friend = Bukkit.getPlayer(playerData.getFriends().get(n));
if (friend == null) return ERROR_PLACEHOLDER;
return friend.getName();
}
else if (identifier.startsWith("profession_"))
return String
.valueOf(playerData.getCollectionSkills().getLevel(identifier.substring(11).replace(" ", "-").replace("_", "-").toLowerCase()));
@ -199,13 +230,6 @@ public class RPGPlaceholders extends PlaceholderExpansion {
return format.toString();
}
/*
4) cooldown of region_change
5) cooldown of command
6) all cooldown you are showing in the in game message
*/
else if (identifier.equals("quest")) {
PlayerQuests data = playerData.getQuestData();
return data.hasCurrent() ? data.getCurrent().getQuest().getName() : "None";
@ -213,7 +237,6 @@ public class RPGPlaceholders extends PlaceholderExpansion {
else if (identifier.equals("quest_progress")) {
PlayerQuests data = playerData.getQuestData();
return data.hasCurrent() ? MythicLib.plugin.getMMOConfig().decimal
.format( (double) data.getCurrent().getObjectiveNumber() / data.getCurrent().getQuest().getObjectives().size() * 100L) : "0";
}

View File

@ -2,12 +2,13 @@ package net.Indyuce.mmocore.gui.api.item;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import io.lumine.mythic.lib.MythicLib;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.gui.api.GeneratedInventory;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
@ -115,8 +116,7 @@ public abstract class InventoryItem<T extends GeneratedInventory> {
ItemStack display = display(generated);
for (int slot : getSlots())
inv.setItem(slot, display);
} else
for (int j = 0; j < slots.size(); j++)
} else for (int j = 0; j < slots.size(); j++)
inv.setItem(slots.get(j), display(generated, j));
}
@ -129,45 +129,58 @@ public abstract class InventoryItem<T extends GeneratedInventory> {
return true;
}
@NotNull
public ItemStack display(T inv) {
return display(inv, modelData);
}
@NotNull
public ItemStack display(T inv, int n) {
return display(inv, n, null);
return display(inv, n, material);
}
@NotNull
public ItemStack display(T inv, int n, Material specificMaterial) {
return display(inv, n, specificMaterial, modelData);
}
public ItemStack display(T inv, int n, Material specificMaterial, int modelData) {
@NotNull
public ItemStack display(T inv, int n, Material material, int modelData) {
Placeholders placeholders = getPlaceholders(inv, n);
ItemStack item = new ItemStack(specificMaterial == null ? material : specificMaterial);
ItemMeta meta = item.getItemMeta();
if (texture != null && meta instanceof SkullMeta)
applyTexture(texture, (SkullMeta) meta);
final Placeholders placeholders = getPlaceholders(inv, n);
final OfflinePlayer effectivePlayer = getEffectivePlayer(inv, n);
if (hasName())
meta.setDisplayName(placeholders.apply(inv.getPlayer(), getName()));
final ItemStack item = new ItemStack(material);
final ItemMeta meta = item.getItemMeta();
meta.setCustomModelData(modelData);
if (texture != null && meta instanceof SkullMeta) applyTexture(texture, (SkullMeta) meta);
if (hideFlags())
meta.addItemFlags(ItemFlag.values());
if (hasName()) meta.setDisplayName(placeholders.apply(effectivePlayer, getName()));
if (hideFlags()) meta.addItemFlags(ItemFlag.values());
if (hasLore()) {
List<String> lore = new ArrayList<>();
getLore().forEach(line -> lore.add(ChatColor.GRAY + placeholders.apply(inv.getPlayer(), line)));
getLore().forEach(line -> lore.add(ChatColor.GRAY + placeholders.apply(effectivePlayer, line)));
meta.setLore(lore);
}
if (MythicLib.plugin.getVersion().isStrictlyHigher(1, 13))
meta.setCustomModelData(modelData);
item.setItemMeta(meta);
return item;
}
/**
* @param inv Inventory being generated
* @param n Index of item being generated
* @return Player relative to which placeholders are computed when the item is
* being displayed in the inventory. Most of the time, it's just
* the player opening the inventory, but for friends or party members,
* being able to parse placeholders based on other players is great too.
*/
@NotNull
public OfflinePlayer getEffectivePlayer(T inv, int n) {
return inv.getPlayer();
}
private void applyTexture(String value, SkullMeta meta) {
try {

View File

@ -1,7 +1,7 @@
package net.Indyuce.mmocore.gui.api.item;
import net.Indyuce.mmocore.MMOCore;
import org.bukkit.entity.Player;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
@ -14,7 +14,7 @@ public class Placeholders {
placeholders.put(path, obj.toString());
}
public String apply(Player player, String str) {
public String apply(OfflinePlayer player, String str) {
// Remove conditions first
str = removeCondition(str);

View File

@ -25,6 +25,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
@ -76,9 +77,15 @@ public class EditableFriendList extends EditableInventory {
return true;
}
@NotNull
@Override
public OfflinePlayer getEffectivePlayer(GeneratedInventory inv, int n) {
return Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n));
}
@Override
public Placeholders getPlaceholders(GeneratedInventory inv, int n) {
OfflinePlayer friend = Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n));
OfflinePlayer friend = getEffectivePlayer(inv, n);
Placeholders holders = new Placeholders();
holders.register("name", friend.getName());
@ -97,17 +104,23 @@ public class EditableFriendList extends EditableInventory {
return true;
}
@NotNull
@Override
public OfflinePlayer getEffectivePlayer(GeneratedInventory inv, int n) {
return Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n));
}
@Deprecated
@Override
public Placeholders getPlaceholders(GeneratedInventory inv, int n) {
Player friend = Bukkit.getPlayer(inv.getPlayerData().getFriends().get(n));
PlayerData data = PlayerData.get(friend);
final PlayerData friendData = PlayerData.get(getEffectivePlayer(inv, n));
Placeholders holders = new Placeholders();
if (data.isOnline())
holders.register("name", data.getPlayer().getName());
holders.register("class", data.getProfess().getName());
holders.register("level", data.getLevel());
holders.register("online_since", new DelayFormat(2).format(System.currentTimeMillis() - data.getLastLogin()));
if (friendData.isOnline())
holders.register("name", friendData.getPlayer().getName());
holders.register("class", friendData.getProfess().getName());
holders.register("level", friendData.getLevel());
holders.register("online_since", new DelayFormat(2).format(System.currentTimeMillis() - friendData.getLastLogin()));
return holders;
}
}
@ -131,7 +144,7 @@ public class EditableFriendList extends EditableInventory {
if (inv.getPlayerData().getFriends().size() <= n)
return super.display(inv, n);
final OfflinePlayer friend = Bukkit.getOfflinePlayer(inv.getPlayerData().getFriends().get(n));
final OfflinePlayer friend = getEffectivePlayer(inv, n);
ItemStack disp = (friend.isOnline() ? online : offline).display(inv, n);
ItemMeta meta = disp.getItemMeta();
meta.getPersistentDataContainer().set(UUID_NAMESPACEDKEY, PersistentDataType.STRING, friend.getUniqueId().toString());

View File

@ -21,6 +21,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
@ -60,10 +61,15 @@ public class EditablePartyView extends EditableInventory {
return holders;
}
@NotNull
@Override
public Player getEffectivePlayer(GeneratedInventory inv, int n) {
return ((Party) inv.getPlayerData().getParty()).getMembers().get(n).getPlayer();
}
@Override
public ItemStack display(GeneratedInventory inv, int n) {
Party party = (Party) inv.getPlayerData().getParty();
PlayerData member = party.getMembers().get(n);
final Player member = getEffectivePlayer(inv, n);
ItemStack disp = super.display(inv, n);
ItemMeta meta = disp.getItemMeta();
@ -71,7 +77,7 @@ public class EditablePartyView extends EditableInventory {
if (meta instanceof SkullMeta)
inv.dynamicallyUpdateItem(this, n, disp, current -> {
((SkullMeta) meta).setOwningPlayer(member.getPlayer());
((SkullMeta) meta).setOwningPlayer(member);
current.setItemMeta(meta);
});

View File

@ -17,6 +17,10 @@ public interface AbstractParty {
*/
List<PlayerData> getOnlineMembers();
default PlayerData getMember(int n) {
return getOnlineMembers().get(n);
}
/**
* @return Number of online/offline players in the party
*/