Skip to content

Commit

Permalink
📦 同步主仓库 (af7b6c7491784ed5dd6083934834208ff7fcb787)
Browse files Browse the repository at this point in the history
  • Loading branch information
INSide-734 committed Oct 22, 2024
1 parent 307b491 commit 53f9a13
Show file tree
Hide file tree
Showing 69 changed files with 1,358 additions and 349 deletions.
8 changes: 1 addition & 7 deletions MMOItems-API/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository>

<!-- Repository used to fetch all Spigot builds -->
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>

<!-- HolographicDisplays -->
<repository>
<id>codemc-repo</id>
Expand Down Expand Up @@ -316,7 +310,7 @@
<dependency>
<groupId>me.arasple.mc</groupId>
<artifactId>TrHologram</artifactId>
<version>1.11</version>
<version>2.2</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Expand Down
16 changes: 13 additions & 3 deletions MMOItems-API/src/main/java/net/Indyuce/mmoitems/ItemStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ItemStats {
LORE = new Lore(),
NBT_TAGS = new NBTTags(),
MAX_STACK_SIZE = new MaxStackSize(),
CAN_BREAK = new CanBreak(),
LORE_FORMAT = new LoreFormat(),
TOOLTIP = new TooltipStat(),

Expand Down Expand Up @@ -110,9 +111,9 @@ public class ItemStats {
UNBREAKABLE = new Unbreakable(),
TIER = new ItemTierStat(),
SET = new ItemSetStat(),
ARMOR = new DoubleStat("ARMOR", Material.GOLDEN_CHESTPLATE, "护甲", new String[]{"赋予持有者的护甲值"}),
ARMOR_TOUGHNESS = new DoubleStat("ARMOR_TOUGHNESS", Material.DIAMOND_CHESTPLATE, "护甲韧性", new String[]{"护甲韧性可减少所受的伤害"}),
MAX_HEALTH = new DoubleStat("MAX_HEALTH", Material.GOLDEN_APPLE, "最大生命值", new String[]{"物品赋予持有者的额外生命值"}),
ARMOR = new Armor(),
ARMOR_TOUGHNESS = new ArmorToughness(),
MAX_HEALTH = new MaxHealth(),
UNSTACKABLE = new Unstackable(),
MAX_MANA = new DoubleStat("MAX_MANA", Material.LAPIS_LAZULI, "最大法力值", new String[]{"增加最大法力值的魔力"}),
KNOCKBACK_RESISTANCE = new KnockbackResistance(),
Expand All @@ -133,6 +134,15 @@ public class ItemStats {
SAFE_FALL_DISTANCE = new SafeFallDistance(),
SCALE = new Scale(),
STEP_HEIGHT = new StepHeight(),
BURNING_TIME = new BurningTime(),
EXPLOSION_KNOCKBACK_RESISTANCE = new ExplosionKnockbackResistance(),
MINING_EFFICIENCY = new MiningEfficiency(),
MOVEMENT_EFFICIENCY = new MovementEfficiency(),
OXYGEN_BONUS = new OxygenBonus(),
SNEAKING_SPEED = new SneakingSpeed(),
SUBMERGED_MINING_SPEED = new SubmergedMiningSpeed(),
SWEEPING_DAMAGE_RATIO = new SweepingDamageRatio(),
WATER_MOVEMENT_EFFICIENCY = new WaterMovementEfficiency(),

// Permanent Effects
PERM_EFFECTS = new PermanentEffects(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void onLoad() {
saveDefaultConfig();
configManager = new ConfigManager();

statManager.loadInternalStats();
statManager.loadBuiltins();
typeManager.reload(false);
templateManager.preloadObjects();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;

// TODO getItemMeta, asNMSCopy = two clones. could be done with NO clone, simply initializing item with proper interfacing
public class ItemStackBuilder {
@NotNull
private final MMOItem mmoitem;
Expand All @@ -44,6 +46,12 @@ public class ItemStackBuilder {
private final LoreBuilder lore;
private final List<ItemTag> tags = new ArrayList<>();

/**
* @deprecated Temp fix before MI7
*/
@Deprecated
private List<Consumer<NBTItem>> futureActions;

private static final AttributeModifier FAKE_MODIFIER = VersionUtils.attrMod(new NamespacedKey(MMOItems.plugin, "decoy"), 0, Operation.ADD_NUMBER);

/**
Expand Down Expand Up @@ -97,6 +105,15 @@ public ItemMeta getMeta() {
return meta;
}

/**
* @deprecated Temp fix before MI7
*/
@Deprecated
public void addFutureAction(Consumer<NBTItem> action) {
if (futureActions == null) futureActions = new ArrayList<>();
futureActions.add(action);
}

public void addItemTag(List<ItemTag> newTags) {
tags.addAll(newTags);
}
Expand Down Expand Up @@ -241,7 +258,10 @@ public NBTItem buildNBT(boolean forDisplay) {

item.setItemMeta(meta);

return NBTItem.get(item).addTag(tags);
NBTItem nbt = NBTItem.get(item).addTag(tags);
if (futureActions != null) futureActions.forEach(a -> a.accept(nbt));

return nbt;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.lumine.mythic.lib.api.util.AltChar;
import io.lumine.mythic.lib.api.util.ui.SilentNumbers;
import io.lumine.mythic.lib.util.AdventureUtils;
import io.lumine.mythic.lib.version.VersionUtils;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.api.edition.NewItemEdition;
Expand All @@ -13,6 +14,7 @@
import net.Indyuce.mmoitems.stat.BrowserDisplayIDX;
import net.Indyuce.mmoitems.util.MMOUtils;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
Expand Down Expand Up @@ -86,6 +88,7 @@ public Inventory getInventory() {
final ItemStack item = currentType.getItem();
item.setAmount(Math.max(1, Math.min(64, items)));
ItemMeta meta = item.getItemMeta();
if (MythicLib.plugin.getVersion().isAbove(1, 20, 5)) VersionUtils.addEmptyAttributeModifier(meta);
AdventureUtils.setDisplayName(meta, String.format("&a%s&8 (点击浏览)", currentType.getName()));
meta.addItemFlags(ItemFlag.values());
List<String> lore = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.Indyuce.mmoitems.gui.edition;

import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.version.VersionUtils;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.item.template.MMOItemTemplate;
import net.Indyuce.mmoitems.stat.type.InternalStat;
Expand All @@ -23,7 +24,7 @@

public class ItemEdition extends EditionInventory {
private static final int[] slots = {19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 37, 38, 39, 40, 41, 42, 43};
private static final NamespacedKey STAT_ID_KEY = new NamespacedKey(MMOItems.plugin,"StatId");
private static final NamespacedKey STAT_ID_KEY = new NamespacedKey(MMOItems.plugin, "StatId");

public ItemEdition(Player player, MMOItemTemplate template) {
super(player, template);
Expand Down Expand Up @@ -52,9 +53,14 @@ public void arrangeInventory() {
ItemStack item = new ItemStack(stat.getDisplayMaterial());
ItemMeta meta = item.getItemMeta();
meta.addItemFlags(ItemFlag.values());
VersionUtils.addEmptyAttributeModifier(meta);
meta.setDisplayName(ChatColor.GREEN + stat.getName());
List<String> lore = MythicLib.plugin.parseColors(Arrays.stream(stat.getLore()).map(s -> ChatColor.GRAY + s).collect(Collectors.toList()));
lore.add("");
if (stat.getCategory() != null) {
lore.add(0, "");
lore.add(0, ChatColor.BLUE + stat.getCategory().getLoreTag());
}

stat.whenDisplayed(lore, getEventualStatData(stat));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package net.Indyuce.mmoitems.manager;

import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.element.Element;
import io.lumine.mythic.lib.util.annotation.BackwardsCompatibility;
import net.Indyuce.mmoitems.ItemStats;
import net.Indyuce.mmoitems.MMOItems;
import net.Indyuce.mmoitems.api.ConfigFile;
import net.Indyuce.mmoitems.api.Type;
import net.Indyuce.mmoitems.stat.annotation.HasCategory;
import net.Indyuce.mmoitems.stat.category.StatCategory;
import net.Indyuce.mmoitems.stat.type.*;
import net.Indyuce.mmoitems.util.ElementStatType;
import org.apache.commons.lang.Validate;
Expand All @@ -23,33 +26,49 @@

public class StatManager {
private final Map<String, ItemStat<?, ?>> stats = new LinkedHashMap<>();
private final Map<String, StatCategory> categories = new HashMap<>();

/**
* If, for whatever reason, a stat needs to change its internal
* string ID, this map keeps a reference for the deprecated old
* IDs while being separated from the main ItemStat map.
*/
@BackwardsCompatibility(version = "unknown")
@BackwardsCompatibility(version = "not_specified")
private final Map<String, ItemStat<?, ?>> legacyAliases = new HashMap<>();

/*
* These lists are sets of stats collected when the stats are registered for
* the first time to make their access easier. Check the classes
* individually to understand better
*/
private final List<DoubleStat> numeric = new ArrayList<>();
private final List<ItemRestriction> itemRestriction = new ArrayList<>();
private final List<DoubleStat> numericStats = new ArrayList<>();
private final List<ItemRestriction> itemRestrictions = new ArrayList<>();
private final List<ConsumableItemInteraction> consumableActions = new ArrayList<>();
private final List<PlayerConsumable> playerConsumables = new ArrayList<>();

/**
* Load default stats using java reflection, get all public static final
* fields in the ItemStat and register them as stat instances
*/
public void loadInternalStats() {
public void loadBuiltins() {

// Builtin categories
for (Field field : StatCategory.class.getFields())
try {
if (Modifier.isStatic(field.getModifiers())
&& Modifier.isFinal(field.getModifiers())
&& field.get(null) instanceof StatCategory)
registerCategory((StatCategory) field.get(null));
} catch (IllegalArgumentException | IllegalAccessException exception) {
MMOItems.plugin.getLogger().log(Level.SEVERE, String.format("Couldn't register category called '%s'", field.getName()), exception.getMessage());
}

// Load builtin stats
for (Field field : ItemStats.class.getFields())
try {
if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && field.get(null) instanceof ItemStat)
if (Modifier.isStatic(field.getModifiers())
&& Modifier.isFinal(field.getModifiers())
&& field.get(null) instanceof ItemStat)
register((ItemStat<?, ?>) field.get(null));
} catch (IllegalArgumentException | IllegalAccessException exception) {
MMOItems.plugin.getLogger().log(Level.SEVERE, String.format("Couldn't register stat called '%s'", field.getName()), exception.getMessage());
Expand All @@ -64,7 +83,7 @@ public void reload(boolean cleanFirst) {

// Clean fictive numeric stats before
if (cleanFirst)
numeric.removeIf(stat -> stat instanceof FictiveNumericStat); // temporary fix, this is for elements.
numericStats.removeIf(stat -> stat instanceof FakeElementalStat); // temporary fix, this is for elements TODO improve

// Register elemental stats
loadElements();
Expand Down Expand Up @@ -100,7 +119,16 @@ private void loadCustomStats() {
public void loadElements() {
for (ElementStatType type : ElementStatType.values())
for (Element element : MythicLib.plugin.getElements().getAll())
numeric.add(new FictiveNumericStat(element, type));
numericStats.add(new FakeElementalStat(element, type));
}

public void registerCategory(@NotNull StatCategory category) {
categories.put(category.getId(), category);
}

@NotNull
public StatCategory getCategory(@NotNull String id) {
return Objects.requireNonNull(categories.get(id), "No stat category found with ID '" + id + "'");
}

@NotNull
Expand All @@ -116,7 +144,7 @@ public void loadElements() {
*/
@NotNull
public List<DoubleStat> getNumericStats() {
return numeric;
return numericStats;
}

/**
Expand All @@ -125,7 +153,7 @@ public List<DoubleStat> getNumericStats() {
*/
@NotNull
public List<ItemRestriction> getItemRestrictionStats() {
return itemRestriction;
return itemRestrictions;
}

/**
Expand Down Expand Up @@ -158,7 +186,7 @@ public boolean has(String id) {
if (stat != null) return stat;

// Numeric registry (see to-do)
stat = numeric.stream().filter(doubleStat -> doubleStat.getId().equals(id)).findFirst().orElse(null);
stat = numericStats.stream().filter(doubleStat -> doubleStat.getId().equals(id)).findFirst().orElse(null);
if (stat != null) return stat;

// Legacy liases
Expand All @@ -169,16 +197,6 @@ public boolean has(String id) {
return null;
}

/**
* @deprecated Stat IDs are now stored in the stat instance directly.
* Please use StatManager#register(ItemStat) instead
*/
@Deprecated
@SuppressWarnings("unused")
public void register(String id, ItemStat<?, ?> stat) {
register(stat);
}

/**
* Registers a stat in MMOItems. It must be done right after MMOItems loads
* before any manager is initialized because stats are commonly used when
Expand All @@ -187,34 +205,30 @@ public void register(String id, ItemStat<?, ?> stat) {
* @param stat The stat to register
*/
public void register(@NotNull ItemStat<?, ?> stat) {
register(stat, false);
}

private void register(@NotNull ItemStat<?, ?> stat, boolean customStat) {

// Skip disabled stats.
if (!stat.isEnabled()) return;

// Safe check, this can happen with numerous extra RPG plugins
if (stats.containsKey(stat.getId())) {
MMOItems.plugin.getLogger().log(Level.SEVERE, "Could not register stat '" + stat.getId() + "' as a stat with the same ID already exists.");
return;
}
// Register stat
stats.compute(stat.getId(), (id, current) -> {
Validate.isTrue(current == null, "A stat with ID '" + id + "' already exists");
return stat;
});

// Main registry
stats.put(stat.getId(), stat);

// Register aliases
for (String alias : stat.getAliases())
legacyAliases.put(alias, stat);
// Register aliases (backwards compatibility)
for (String alias : stat.getAliases()) legacyAliases.put(alias, stat);

// Use-case specific registries
if (stat instanceof DoubleStat && !(stat instanceof GemStoneStat) && stat.isCompatible(Type.GEM_STONE))
numeric.add((DoubleStat) stat);
if (stat instanceof ItemRestriction) itemRestriction.add((ItemRestriction) stat);
numericStats.add((DoubleStat) stat);
if (stat instanceof ItemRestriction) itemRestrictions.add((ItemRestriction) stat);
if (stat instanceof ConsumableItemInteraction) consumableActions.add((ConsumableItemInteraction) stat);
if (stat instanceof PlayerConsumable) playerConsumables.add((PlayerConsumable) stat);

// Stat category
HasCategory statCatAnnot = stat.getClass().getAnnotation(HasCategory.class);
if (statCatAnnot != null) stat.setCategory(getCategory(UtilityMethods.enumName(statCatAnnot.cat())));

/*
* Cache stat for every type which may have this stat. Really important
* otherwise the stat will NOT be used anywhere in the plugin. This
Expand Down Expand Up @@ -268,4 +282,22 @@ private void registerCustomStat(@NotNull ConfigurationSection section) {
throw new RuntimeException("Unable to create a custom stat of type " + type, e);
}
}

/**
* @see #register(ItemStat)
* @deprecated Stat IDs are now stored in the stat instance directly.
*/
@Deprecated
@SuppressWarnings("unused")
public void register(@Nullable String id, @NotNull ItemStat<?, ?> stat) {
register(stat);
}

/**
* @see #loadBuiltins()
*/
@Deprecated
public void loadInternalStats() {
loadBuiltins();
}
}
Loading

0 comments on commit 53f9a13

Please sign in to comment.