Skip to content

Commit

Permalink
add machineId parameter to multiblockcraft criteria, fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
char3210 committed Dec 6, 2023
1 parent 9040178 commit 12ce816
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 32 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ The type is the type of criterion. By default, these are the default criterion t
- string parameter `search`, the exact string to search for in the guide
- `multiblockcraft`
- for crafting an item in a multiblock
- has an item parameter `item`, the item to craft
- has an integer parameter `amount`, the number of times to craft it (not how many items are to be crafted)
- has an integer parameter `amount`, the number of times to craft (not how many items are to be crafted)
- (optional) has an item parameter `item`, the item to craft
- if not present, any item crafted with the multiblock will complete the criterion
- (optional) has a string parameter `multiblock`, the slimefun id of the multiblock to craft
- if not present, any multiblock can be used to craft the item to complete the criterion
- you may specify neither an item nor multiblock, in which case any multiblock craft done by the player will increment the criterion

#### rewards

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nullable;

public class MultiBlockCraftCriterion extends Criterion {
private final ItemStack item;
@Nullable private final ItemStack item;
@Nullable private final String machineId;

public static MultiBlockCraftCriterion loadFromConfig(ConfigurationSection config) {
String id = config.getName();
Expand All @@ -29,28 +32,39 @@ public static MultiBlockCraftCriterion loadFromConfig(ConfigurationSection confi

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

ItemStack item = ConfigUtils.getItem(config, "item");
if (item == null) {
SFAdvancements.warn("unknown item for multiblock craft criterion " + id);
return null;

ItemStack item = null;
if (config.contains("item", true)) {
item = ConfigUtils.getItem(config, "item");
if (item == null) {
SFAdvancements.warn("unknown item for multiblock craft criterion " + id);
return null;
}
}

return new MultiBlockCraftCriterion(id, amount, name, item);
String machineid = config.getString("multiblock");

return new MultiBlockCraftCriterion(id, amount, name, item, machineid);
}

/**
*
* @param id
* @param count
* @param name
* @param item the output item of the multiblock craft
* @param item the output item of the multiblock craft.
* @param machineId multiblock machine id
*/
public MultiBlockCraftCriterion(String id, int count, String name, ItemStack item) {
public MultiBlockCraftCriterion(String id, int count, String name, @Nullable ItemStack item, @Nullable String machineId) {
super(id, count, name);
this.item = item;
this.machineId = machineId;
}

@Nullable
public ItemStack getItem() {
return item;
}

@Nullable
public String getMachineId() {
return machineId;
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
package me.char321.sfadvancements.core.criteria.completer;

import io.github.thebusybiscuit.slimefun4.api.events.MultiBlockCraftEvent;
import io.github.thebusybiscuit.slimefun4.api.events.ResearchUnlockEvent;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import me.char321.sfadvancements.SFAdvancements;
import me.char321.sfadvancements.api.criteria.Criterion;
import me.char321.sfadvancements.api.criteria.MultiBlockCraftCriterion;
import me.char321.sfadvancements.api.criteria.ResearchCriterion;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.ItemStack;

import java.util.*;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MultiBlockCraftCriterionCompleter implements CriterionCompleter, Listener {
private final Map<Material, Set<MultiBlockCraftCriterion>> criteria = new EnumMap<>(Material.class);
private final Set<MultiBlockCraftCriterion> nonMaterialCriteria = new HashSet<>();

public MultiBlockCraftCriterionCompleter() {
Bukkit.getPluginManager().registerEvents(this, SFAdvancements.instance());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onMultiBlockCraft(MultiBlockCraftEvent e) {
performCriteria(e.getPlayer(), e.getOutput());
performCriteria(e.getPlayer(), e.getMachine().getId(), e.getOutput());
}

private void performCriteria(Player player, ItemStack output) {
private void performCriteria(Player player, String machineId, ItemStack output) {
for (MultiBlockCraftCriterion criterion : nonMaterialCriteria) {
String machineId1 = criterion.getMachineId();
if (machineId1 == null || machineId1.equals(machineId)) {
criterion.perform(player);
}
}

Set<MultiBlockCraftCriterion> allcriteria = criteria.get(output.getType());
if (allcriteria == null) {
return;
}

for (MultiBlockCraftCriterion criterion : allcriteria) {
if (SlimefunUtils.isItemSimilar(output, criterion.getItem(), false, false)) {
String machineId1 = criterion.getMachineId();
if ((machineId1 == null || machineId1.equals(machineId)) &&
SlimefunUtils.isItemSimilar(output, criterion.getItem(), false, false)) {
criterion.perform(player);
}
}
Expand All @@ -53,8 +60,12 @@ public void register(Criterion criterion) {
}

MultiBlockCraftCriterion criterion1 = ((MultiBlockCraftCriterion) criterion);
Material material = criterion1.getItem().getType();
criteria.computeIfAbsent(material, k -> new HashSet<>()).add(criterion1);
ItemStack item = criterion1.getItem();
if (item == null) {
nonMaterialCriteria.add(criterion1);
} else {
criteria.computeIfAbsent(item.getType(), k -> new HashSet<>()).add(criterion1);
}
}

@Override
Expand All @@ -65,5 +76,6 @@ public Class<? extends Criterion> getCriterionClass() {
@Override
public void reload() {
criteria.clear();
nonMaterialCriteria.clear();
}
}
15 changes: 15 additions & 0 deletions src/main/java/me/char321/sfadvancements/util/Utils.java
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.api.Advancement;
import net.md_5.bungee.api.chat.TranslatableComponent;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand All @@ -16,12 +17,14 @@
import java.util.Locale;
import java.util.Map;

@SuppressWarnings("unused")
public class Utils {
private Utils() {}

public static ItemStack makeShiny(ItemStack item) {
item = item.clone();
ItemMeta im = item.getItemMeta();
//noinspection DataFlowIssue
im.addEnchant(Enchantment.DURABILITY, 1, false);
im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
item.setItemMeta(im);
Expand Down Expand Up @@ -70,6 +73,18 @@ public static Map<ItemStack, Integer> getContents(Inventory inv) {
return contents;
}

public static TranslatableComponent getItemName(ItemStack item) {
return getItemName(item.getType());
}

public static TranslatableComponent getItemName(Material type) {
if (type.isBlock()) {
return new TranslatableComponent("block.minecraft."+type.getKey().getKey());
} else {
return new TranslatableComponent("item.minecraft."+type.getKey().getKey());
}
}

public static void runSync(Runnable runnable) {
Bukkit.getScheduler().runTask(SFAdvancements.instance(), runnable);
}
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/me/char321/sfadvancements/vanilla/VanillaHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
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;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import net.roxeez.advancement.AdvancementManager;
import net.roxeez.advancement.display.Icon;
import net.roxeez.advancement.trigger.TriggerType;
Expand All @@ -17,8 +17,6 @@
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 @@ -61,6 +59,7 @@ private static void registerGroups(AdvancementManager manager) {
ItemStack item = group.getDisplayItem();
String background = group.getBackground();
ItemMeta meta = item.getItemMeta();
//noinspection DataFlowIssue
display.setTitle(meta.getDisplayName());
List<String> lore = meta.getLore();
if (lore == null) {
Expand Down Expand Up @@ -105,8 +104,17 @@ private static void registerAdvancement(AdvancementManager manager, Advancement
vadvancement.setDisplay(display -> {
ItemStack item = advancement.getDisplay();
ItemMeta meta = item.getItemMeta();
display.setTitle(meta.getDisplayName());
display.setDescription(getDescriptionFor(meta.getLore(), advancement));
BaseComponent title;
String description;
//noinspection DataFlowIssue
if (meta.hasDisplayName()) {
title = new TextComponent(meta.getDisplayName());
} else {
title = Utils.getItemName(item);
}
description = getDescriptionFor(meta.getLore(), advancement);
display.setTitle(title);
display.setDescription(description);
display.setIcon(new Icon(item));
display.setHidden(advancement.isHidden());
display.setAnnounce(false);
Expand All @@ -120,7 +128,7 @@ private static void registerAdvancement(AdvancementManager manager, Advancement
}

private static String getDescriptionFor(List<String> lore, Advancement adv) {
lore = new ArrayList<>(lore);
lore = lore == null ? new ArrayList<>() : new ArrayList<>(lore);
for (int i = lore.size() - 1; i >= 0; i--) {
if ("%criteria%".equals(lore.get(i))) {
lore.remove(i);
Expand Down

0 comments on commit 12ce816

Please sign in to comment.