Skip to content

Commit

Permalink
colored criteria names from config, %criteria% placeholder in advance…
Browse files Browse the repository at this point in the history
…ment displays, hide attributes
  • Loading branch information
qwertyuioplkjhgfd committed Oct 16, 2023
1 parent e874b9a commit 0268fea
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 24 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ The group is the id defined in `groups.yml`.

The parent is the id of a different Advancement for this to be under. (For Advancement Trees)

The display is an item, represented as described in Item Representation.
The display is an item, represented as described in Item Representation. This is the item that is displayed in both the
SFA GUI and the vanilla GUI. You can specify a placeholder line `%criteria%` in the lore, which will be replaced with the
criteria of the advancement.

The name is what will appear in chat when someone completes the advancement.

Expand All @@ -100,7 +102,7 @@ The key of the criterion should be unique per advancement (but may share keys in
The string itself doesn't matter, but if no name is specified, it will use the key.<br>
Each criterion has a name, type, and optional other parameters.

The name is what appears in the gui for progress.
The name is what appears in the gui for progress. (Supports color codes with `&`)

The type is the type of criterion. By default, these are the default criterion types:
- `consume`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.util.ConfigUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

Expand All @@ -21,6 +22,8 @@ public static BlockBreakCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

ItemStack item = ConfigUtils.getItem(config, "item");
if (item == null) {
SFAdvancements.warn("unknown item for break criterion " + id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.util.ConfigUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

Expand All @@ -25,6 +26,8 @@ public static ConsumeCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

ItemStack item = ConfigUtils.getItem(config, "item");
if (item == null) {
SFAdvancements.warn("unknown item for consume criterion " + id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.core.criteria.completer.CriterionCompleter;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -60,6 +61,8 @@ public static Criterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

return new Criterion(id, amount, name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.util.ConfigUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

Expand All @@ -24,6 +25,8 @@ public static InteractCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

ItemStack item = ConfigUtils.getItem(config, "item");
if (item == null) {
SFAdvancements.warn("unknown item for interact criterion " + id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.util.ConfigUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

Expand All @@ -22,6 +23,8 @@ public static InventoryCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

ItemStack item = ConfigUtils.getItem(config, "item");
if (item == null) {
SFAdvancements.warn("unknown item for inventory criterion " + id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.char321.sfadvancements.api.criteria;

import me.char321.sfadvancements.SFAdvancements;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.EntityType;

Expand All @@ -24,6 +25,8 @@ public static MobKillCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

String entity = config.getString("entity");
if (entity == null) {
SFAdvancements.warn("entity not provided for " + id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.char321.sfadvancements.api.criteria;

import me.char321.sfadvancements.SFAdvancements;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;

public class MultiBlockCriterion extends Criterion {
Expand All @@ -19,6 +20,8 @@ public static MultiBlockCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

String multiBlockId = config.getString("multiblock");
if(multiBlockId == null) {
SFAdvancements.warn("specify a multiblock for criterion " + id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.util.ConfigUtils;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
Expand All @@ -26,6 +27,8 @@ public static PlaceCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

ItemStack item = ConfigUtils.getItem(config, "item");
if (item == null) {
SFAdvancements.warn("unknown item for place criterion " + id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.char321.sfadvancements.api.criteria;

import me.char321.sfadvancements.SFAdvancements;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.ConfigurationSection;

Expand All @@ -18,6 +19,8 @@ public static ResearchCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

String research = config.getString("research");
if (research == null) {
SFAdvancements.warn("specify a research for criterion " + id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.util.ConfigUtils;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

Expand All @@ -17,6 +18,8 @@ public static SearchCriterion loadFromConfig(ConfigurationSection config) {
name = id;
}

name = ChatColor.translateAlternateColorCodes('&', name);

int amount = config.getInt("amount");
if (amount == 0) {
amount = 1;
Expand Down
69 changes: 48 additions & 21 deletions src/main/java/me/char321/sfadvancements/core/gui/OpenGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
Expand Down Expand Up @@ -186,31 +187,57 @@ private void refreshAdvancements() {
ItemStack display = null;
if (advindex < advancements.size()) {
Advancement adv = advancements.get(advindex);
display = adv.getDisplay().clone();
ItemMeta displayim = display.getItemMeta();
display = getDisplayFor(adv);
}

if (SFAdvancements.getAdvManager().isCompleted(playerUUID, adv)) {
Utils.makeShiny(displayim);
}
inventory.setItem(slot, display);
}
}

List<String> lore = displayim.getLore();
if (lore == null) {
lore = new ArrayList<>();
}
lore.add("");

for (Criterion criterion : adv.getCriteria()) {
String criterionName = criterion.getName();
int progress = SFAdvancements.getAdvManager().getCriterionProgress(playerUUID, criterion);
int max = criterion.getCount();
boolean cridone = progress >= max;
lore.add(ChatColor.GRAY + criterionName + ": " + (cridone? ChatColor.YELLOW : ChatColor.WHITE) + progress + "/" + max);
}
displayim.setLore(lore);
display.setItemMeta(displayim);
private ItemStack getDisplayFor(Advancement adv) {
ItemStack display = adv.getDisplay().clone();
ItemMeta displayim = display.getItemMeta();
if (displayim == null) {
throw new IllegalArgumentException("display item meta is null");
}

displayim.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
if (SFAdvancements.getAdvManager().isCompleted(playerUUID, adv)) {
Utils.makeShiny(displayim);
}

List<String> lore = displayim.getLore();
if (lore == null) {
lore = new ArrayList<>();
}

boolean loreAdded = false;
for (int i = lore.size() - 1; i >= 0; i--) {
if ("%criteria%".equals(lore.get(i))) {
lore.remove(i);
lore.addAll(i, this.getCriteriaLore(adv));
loreAdded = true;
}
}
if (!loreAdded) {
lore.addAll(this.getCriteriaLore(adv));
}

inventory.setItem(slot, display);
displayim.setLore(lore);
display.setItemMeta(displayim);
return display;
}

private List<String> getCriteriaLore(Advancement adv) {
List<String> res = new ArrayList<>();
for (Criterion criterion : adv.getCriteria()) {
String criterionName = criterion.getName();
System.out.println(adv.getName() + " " + criterionName);
int progress = SFAdvancements.getAdvManager().getCriterionProgress(playerUUID, criterion);
int max = criterion.getCount();
boolean cridone = progress >= max;
res.add(ChatColor.GRAY + criterionName + ": " + (cridone ? ChatColor.YELLOW : ChatColor.WHITE) + progress + "/" + max);
}
return res;
}
}
24 changes: 23 additions & 1 deletion src/main/java/me/char321/sfadvancements/vanilla/VanillaHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.api.Advancement;
import me.char321.sfadvancements.api.AdvancementGroup;
import me.char321.sfadvancements.api.criteria.Criterion;
import me.char321.sfadvancements.core.criteria.progress.PlayerProgress;
import me.char321.sfadvancements.util.Utils;
import net.roxeez.advancement.AdvancementCreator;
Expand All @@ -16,6 +17,7 @@
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -104,7 +106,7 @@ private static void registerAdvancement(AdvancementManager manager, Advancement
ItemStack item = advancement.getDisplay();
ItemMeta meta = item.getItemMeta();
display.setTitle(meta.getDisplayName());
display.setDescription(String.join("\n", meta.getLore()));
display.setDescription(getDescriptionFor(meta.getLore(), advancement));
display.setIcon(new Icon(item));
display.setHidden(advancement.isHidden());
display.setAnnounce(false);
Expand All @@ -117,6 +119,26 @@ private static void registerAdvancement(AdvancementManager manager, Advancement
});
}

private static String getDescriptionFor(List<String> lore, Advancement adv) {
lore = new ArrayList<>(lore);
for (int i = lore.size() - 1; i >= 0; i--) {
if ("%criteria%".equals(lore.get(i))) {
lore.remove(i);
lore.addAll(i, getCriteriaLore(adv));
return String.join("\n", lore);
}
}
return String.join("\n", lore);
}

private static List<String> getCriteriaLore(Advancement adv) {
List<String> res = new ArrayList<>();
for (Criterion criterion : adv.getCriteria()) {
res.add("§7" + criterion.getName());
}
return res;
}

public void syncProgress(Player p) {
for (AdvancementGroup group : SFAdvancements.getRegistry().getAdvancementGroups()) {
complete(p, Utils.keyOf(group.getId()));
Expand Down

0 comments on commit 0268fea

Please sign in to comment.