mirror of
https://github.com/CivMC/Civ.git
synced 2026-07-18 00:20:44 +00:00
Update BulkExchangeRule + fix parsing bug
This commit is contained in:
@@ -10,7 +10,6 @@ import com.untamedears.itemexchange.utility.Utilities;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -225,8 +224,8 @@ public final class ItemExchangeListener implements Listener {
|
||||
CraftingInventory inventory = event.getInventory();
|
||||
inventory.setResult(null);
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(ItemExchangePlugin.getInstance(), () -> {
|
||||
List<ExchangeRule> rules = new ArrayList<>();
|
||||
for (ItemStack item : inventory.getMatrix()) {
|
||||
final var rules = new ArrayList<ExchangeRule>();
|
||||
for (final ItemStack item : inventory.getMatrix()) {
|
||||
if (!ItemUtils.isValidItem(item)) {
|
||||
continue;
|
||||
}
|
||||
@@ -237,13 +236,12 @@ public final class ItemExchangeListener implements Listener {
|
||||
}
|
||||
BulkExchangeRule bulkRule = BulkExchangeRule.fromItem(item);
|
||||
if (Validation.checkValidity(bulkRule)) {
|
||||
rules.addAll(bulkRule.getRules());
|
||||
rules.addAll(bulkRule.rules());
|
||||
continue;
|
||||
}
|
||||
return;
|
||||
}
|
||||
BulkExchangeRule rule = new BulkExchangeRule();
|
||||
rule.setRules(rules);
|
||||
final var rule = new BulkExchangeRule(rules);
|
||||
inventory.setResult(rule.toItem());
|
||||
InventoryUtils.getViewingPlayers(inventory).forEach(Player::updateInventory);
|
||||
});
|
||||
@@ -262,7 +260,7 @@ public final class ItemExchangeListener implements Listener {
|
||||
return;
|
||||
}
|
||||
drop.remove();
|
||||
for (ExchangeRule rule : bulk.getRules()) {
|
||||
for (ExchangeRule rule : bulk.rules()) {
|
||||
drop.getWorld().dropItem(drop.getLocation(), rule.toItem()).setVelocity(drop.getVelocity());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.untamedears.itemexchange.rules;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.untamedears.itemexchange.ItemExchangeConfig;
|
||||
import com.untamedears.itemexchange.rules.interfaces.ExchangeData;
|
||||
import java.util.ArrayList;
|
||||
@@ -9,30 +8,34 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nonnull;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
import javax.annotation.Nullable;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import vg.civcraft.mc.civmodcore.inventory.items.ItemUtils;
|
||||
import vg.civcraft.mc.civmodcore.inventory.items.MetaUtils;
|
||||
import vg.civcraft.mc.civmodcore.nbt.NBTSerialization;
|
||||
import vg.civcraft.mc.civmodcore.nbt.NBTType;
|
||||
import vg.civcraft.mc.civmodcore.nbt.wrappers.NBTCompound;
|
||||
import vg.civcraft.mc.civmodcore.utilities.MoreClassUtils;
|
||||
|
||||
public final class BulkExchangeRule implements ExchangeData {
|
||||
public record BulkExchangeRule(List<ExchangeRule> rules) implements ExchangeData {
|
||||
|
||||
public static final String BULK_KEY = "BulkExchangeRule";
|
||||
public static final String RULES_KEY = "rules";
|
||||
|
||||
private List<ExchangeRule> rules;
|
||||
public BulkExchangeRule(@Nonnull final List<ExchangeRule> rules) {
|
||||
this.rules = Objects.requireNonNull(rules);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBroken() {
|
||||
return CollectionUtils.isEmpty(this.rules);
|
||||
return this.rules.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toNBT(@Nonnull final NBTCompound nbt) {
|
||||
nbt.setCompoundArray(RULES_KEY, getRules().stream()
|
||||
nbt.setCompoundArray(RULES_KEY, this.rules.stream()
|
||||
.map((rule) -> {
|
||||
final var ruleNBT = new NBTCompound();
|
||||
rule.toNBT(ruleNBT);
|
||||
@@ -43,58 +46,52 @@ public final class BulkExchangeRule implements ExchangeData {
|
||||
|
||||
@Nonnull
|
||||
public static BulkExchangeRule fromNBT(@Nonnull final NBTCompound nbt) {
|
||||
final var rule = new BulkExchangeRule();
|
||||
rule.setRules(Arrays.stream(nbt.getCompoundArray(RULES_KEY))
|
||||
.map(raw -> MoreClassUtils.castOrNull(ExchangeRule.class, ExchangeRule.fromNBT(raw)))
|
||||
.filter(Objects::nonNull)
|
||||
return new BulkExchangeRule(Arrays.stream(nbt.getCompoundArray(RULES_KEY))
|
||||
.map(ExchangeRule::fromNBT)
|
||||
.collect(Collectors.toCollection(ArrayList::new)));
|
||||
return rule;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getDisplayInfo() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Getters + Setters
|
||||
// ------------------------------------------------------------
|
||||
|
||||
public List<ExchangeRule> getRules() {
|
||||
if (this.rules == null) {
|
||||
return Lists.newArrayList();
|
||||
}
|
||||
return this.rules;
|
||||
}
|
||||
|
||||
public void setRules(List<ExchangeRule> rules) {
|
||||
this.rules = rules;
|
||||
}
|
||||
|
||||
public ItemStack toItem() {
|
||||
ItemStack item = NBTSerialization.processItem(ItemExchangeConfig.getRuleItem(), (nbt) -> {
|
||||
final ItemStack item = NBTSerialization.processItem(ItemExchangeConfig.getRuleItem(), (nbt) -> {
|
||||
final var ruleNBT = new NBTCompound();
|
||||
toNBT(ruleNBT);
|
||||
nbt.set(BULK_KEY, ruleNBT);
|
||||
});
|
||||
ItemUtils.setDisplayName(item, ChatColor.RED + "Bulk Rule Block");
|
||||
ItemUtils.setLore(item, String.format("This rule block holds %s exchange rule%s.",
|
||||
rules.size(), rules.size() == 1 ? "" : "s"));
|
||||
ItemUtils.handleItemMeta(item, (ItemMeta meta) -> {
|
||||
meta.displayName(Component.text()
|
||||
.color(NamedTextColor.RED)
|
||||
.content("Bulk Rule Block")
|
||||
.build());
|
||||
MetaUtils.setComponentLore(meta, Component.text(
|
||||
String.format(
|
||||
"This rule block holds %s exchange rule%s.",
|
||||
this.rules.size(), this.rules.size() == 1 ? "" : "s")));
|
||||
return true;
|
||||
});
|
||||
return item;
|
||||
}
|
||||
|
||||
public static BulkExchangeRule fromItem(ItemStack item) {
|
||||
if (!ItemUtils.isValidItem(item)) {
|
||||
@Nullable
|
||||
public static BulkExchangeRule fromItem(final ItemStack item) {
|
||||
if (!ItemUtils.isValidItem(item)
|
||||
|| item.getType() != ItemExchangeConfig.getRuleItemMaterial()) {
|
||||
return null;
|
||||
}
|
||||
if (item.getType() != ItemExchangeConfig.getRuleItemMaterial()) {
|
||||
final var meta = item.getItemMeta();
|
||||
if (meta == null) {
|
||||
return null;
|
||||
}
|
||||
// From NBT
|
||||
final var itemNBT = NBTSerialization.fromItem(item);
|
||||
if (itemNBT.hasKeyOfType(BULK_KEY, NBTType.COMPOUND)) {
|
||||
return null;
|
||||
final var rulesNBT = itemNBT.getCompound(BULK_KEY).getCompoundArray(RULES_KEY);
|
||||
final var rules = new ArrayList<ExchangeRule>(rulesNBT.length);
|
||||
for (final var ruleNBT : rulesNBT) {
|
||||
rules.add(ExchangeRule.fromNBT(ruleNBT));
|
||||
}
|
||||
return new BulkExchangeRule(rules);
|
||||
}
|
||||
return fromNBT(itemNBT.getCompound(BULK_KEY));
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ public final class ShopRule implements Validation {
|
||||
BulkExchangeRule bulk = BulkExchangeRule.fromItem(item);
|
||||
if (bulk != null) {
|
||||
PLUGIN.debug("[Resolve] \tBulk Exchange Rule found.");
|
||||
found.addAll(bulk.getRules());
|
||||
found.addAll(bulk.rules());
|
||||
//continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.untamedears.itemexchange.rules.interfaces;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import vg.civcraft.mc.civmodcore.nbt.NBTSerializable;
|
||||
import vg.civcraft.mc.civmodcore.utilities.Validation;
|
||||
@@ -15,7 +16,9 @@ public interface ExchangeData extends Validation, NBTSerializable {
|
||||
*
|
||||
* @return Returns any display information.
|
||||
*/
|
||||
List<String> getDisplayInfo();
|
||||
default List<String> getDisplayInfo() {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use {@link ExchangeData#isBroken()} instead.
|
||||
|
||||
Reference in New Issue
Block a user