Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/dependencies.sha256
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
a37f7789fcdcd11c9e5890a2fac742aa4501749588af23182991953e06ee1d17 libs/MMOItems-6.10.1-SNAPSHOT.jar
225aa7f75d4e6e6ae6b3ad87a7ca678c025c027f00374ab1062275d24f3a73c9 libs/MythicLib-1.7.1-SNAPSHOT.jar
5a01b37bd7442fb0f2c809cadac82ba70f75173956e04efac3336595cc070368 libs/ItemsAdder-4.0.18.jar
c571502e768640c7cff10c49b3eef904ccf3fd95bd81803913abfe43cfc09746 libs/NBTAPI-2.16.1.jar
3 changes: 3 additions & 0 deletions .github/scripts/install-local-dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ mvn -B --no-transfer-progress org.apache.maven.plugins:maven-install-plugin:3.1.
mvn -B --no-transfer-progress org.apache.maven.plugins:maven-install-plugin:3.1.4:install-file \
-Dfile="libs/ItemsAdder-4.0.18.jar" -DgroupId="local" -DartifactId="LoneLibs" \
-Dversion="4.0.18-tfmc-5a01b37bd744" -Dpackaging=jar -DgeneratePom=true "$@"
mvn -B --no-transfer-progress org.apache.maven.plugins:maven-install-plugin:3.1.4:install-file \
-Dfile="libs/NBTAPI-2.16.1.jar" -DgroupId="local" -DartifactId="item-nbt-api-plugin" \
-Dversion="2.16.1-tfmc-c571502e7686" -Dpackaging=jar -DgeneratePom=true "$@"
1 change: 1 addition & 0 deletions .github/scripts/prepare-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ curl --fail --location --silent --show-error --retry 3 -H "Authorization: Bearer
curl --fail --location --silent --show-error --retry 3 -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github.raw+json" "https://api.github.com/repos/TF-Minecraft/ServerAssets/contents/jars/a37f7789fcdc/MMOItems-6.10.1-SNAPSHOT.jar?ref=$ref" > "libs/MMOItems-6.10.1-SNAPSHOT.jar"
curl --fail --location --silent --show-error --retry 3 -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github.raw+json" "https://api.github.com/repos/TF-Minecraft/ServerAssets/contents/jars/225aa7f75d4e/MythicLib-1.7.1-SNAPSHOT.jar?ref=$ref" > "libs/MythicLib-1.7.1-SNAPSHOT.jar"
curl --fail --location --silent --show-error --retry 3 -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github.raw+json" "https://api.github.com/repos/TF-Minecraft/ServerAssets/contents/jars/5a01b37bd744/ItemsAdder-4.0.18.jar?ref=$ref" > "libs/ItemsAdder-4.0.18.jar"
curl --fail --location --silent --show-error --retry 3 -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github.raw+json" "https://api.github.com/repos/TF-Minecraft/ServerAssets/contents/jars/c571502e7686/NBTAPI-2.16.1.jar?ref=$ref" > "libs/NBTAPI-2.16.1.jar"
bash .github/scripts/install-local-dependencies.sh "$@"
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
</properties>

<dependencies>
<!-- NBT-API's NBT class references authlib; mocking it in tests needs the type. -->
<dependency>
<groupId>com.mojang</groupId>
<artifactId>authlib</artifactId>
<version>7.0.61</version>
<scope>test</scope>
<exclusions>
<exclusion><groupId>*</groupId><artifactId>*</artifactId></exclusion>
</exclusions>
</dependency>
<!-- NBTAPI plugin: the ItemsAdder auto-update opt-out; TLibs' ItemSkinPreserver uses it too. -->
<dependency>
<groupId>local</groupId>
<artifactId>item-nbt-api-plugin</artifactId>
<version>2.16.1-tfmc-c571502e7686</version>
<scope>provided</scope>
</dependency>
<!-- ItemsAdder loads FastNBT dynamically on a server; mocks need its API types. -->
<dependency>
<groupId>beer.devs</groupId>
Expand Down Expand Up @@ -99,6 +116,10 @@
</dependencies>

<repositories>
<repository>
<id>minecraft-libraries</id>
<url>https://libraries.minecraft.net/</url>
</repository>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public class Cache {

public static boolean debugStatRefresh;

/** Crafted pieces still wearing an old default look get their recipe's current model. */
public static boolean armourLookMigration;
/** Old default looks per recipe id, for recipes whose old look did not come from their metal's type model. */
public static Map<String, List<String>> legacyModels = new HashMap<>();

public static boolean showIngredientStats = true;

/** Divisors applied to bucket-averaged stats before template factors (e.g. movement_speed: 100). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ public void load(File configFile) {
Cache.hitOvershootWarnMessage = overshootMessage;

Cache.debugStatRefresh = config.getBoolean("debug-stat-refresh", false);

Cache.armourLookMigration = config.getBoolean("armour-look-migration.enabled", false);
Cache.legacyModels.clear();
for (String entry : config.getStringList("armour-look-migration.legacy-models")) {
int open = entry.indexOf('(');
if (open <= 0 || !entry.endsWith(")")) {
AdvancedCrafting.plugin.getLogger().warning("Ignoring armour-look-migration legacy model '" + entry
+ "'; expected <recipe>(<model>)");
continue;
}
Cache.legacyModels.computeIfAbsent(entry.substring(0, open).trim().toLowerCase(), key -> new ArrayList<>())
.add(entry.substring(open + 1, entry.length() - 1).trim());
}
Cache.showIngredientStats = config.getBoolean("show-ingredient-stats", true);

Cache.globalStatOffsets.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ private void tryRefresh(ItemStack item, ItemConsumer writer) {
if (item == null || item.getType().isAir() || !AcItemRefresher.isManaged(item)) {
return;
}
ItemStack refreshed = AcItemRefresher.refreshIfOutdated(item);
ItemStack refreshed;
try {
refreshed = AcItemRefresher.refreshIfOutdated(item);
} catch (RuntimeException e) {
// One broken item (e.g. a recorded craft whose MMOItems data is gone) must not stop the rest of a sweep.
AdvancedCrafting.plugin.getLogger().warning("Could not refresh " + item.getType() + ": " + e);
return;
}
if (refreshed == item) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package net.tfminecraft.advancedcrafting.objects.crafting;

import net.tfminecraft.advancedcrafting.util.LegacyModelData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.WordUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import io.lumine.mythic.lib.api.item.NBTItem;
import net.tfminecraft.tlibs.TLibs;
Expand Down Expand Up @@ -53,6 +49,8 @@
import net.tfminecraft.advancedcrafting.utils.CraftTierLore;
import net.tfminecraft.advancedcrafting.utils.MMOStatApplicator;
import net.tfminecraft.advancedcrafting.utils.MajorityTierResolver;
import net.tfminecraft.advancedcrafting.utils.ModelApplier;
import net.tfminecraft.advancedcrafting.utils.ModelSchemeResolver;
import net.tfminecraft.advancedcrafting.utils.ProfessionPermissions;
import net.tfminecraft.advancedcrafting.lifecycle.CraftLifecycle;

Expand Down Expand Up @@ -506,20 +504,7 @@ private StationFeedback createItem(Player p, Double forcedQualityPercent, boolea
mmo.setData(ItemStats.LORE, lore);
ItemStack finalItem = mmo.newBuilder().build();
if(scheme != null) {
// A recipe-specific model on the main material wins over the secondary model ingredient.
if(scheme.getModel(recipe.getId()) == null && !recipe.getModelType().equalsIgnoreCase("none")) {
for(String s : currentMaterials.keySet()) {
String modeltype = s.split("\\.")[0];
String modelId = s.split("\\.")[1];
if(modeltype.equalsIgnoreCase("ingredient")) {
Ingredient ing = IngredientLoader.getByString(modelId);
if(ing.getIngredientData().getType().getId().equalsIgnoreCase(recipe.getModelType())) scheme = ing.getIngredientData().getModelScheme();
} else if(modeltype.equalsIgnoreCase("alloy")) {
Alloy a = AlloyManager.getAlloyById(modelId);
if(a.getData().getType().getId().equalsIgnoreCase(recipe.getModelType())) scheme = a.getData().getModelScheme();
}
}
}
scheme = ModelSchemeResolver.forModel(recipe, scheme, currentMaterials);
finalItem = applyModel(finalItem, scheme);
}
CraftProvenance provenance = CraftProvenance.from(recipe, currentMaterials, q);
Expand All @@ -539,29 +524,13 @@ private StationFeedback createItem(Player p, Double forcedQualityPercent, boolea
return StationFeedback.SUCCESS;
}

// This path mutates the existing ItemStack; replacing it would change aliases held by callers.
@SuppressWarnings("deprecation")
private ItemStack applyModel(ItemStack i, ModelScheme scheme) {
// A model named after the recipe (e.g. heavy_helmet) wins over the shared type (helmet).
String path = scheme.getModel(recipe.getId());
if(path == null) {
path = scheme.getModel(recipe.getType());
}
String path = ModelApplier.modelFor(scheme, recipe);
if(path == null) {
Bukkit.getLogger().warning("AC: No model in the scheme "+scheme.getId()+" for the recipe type "+recipe.getType());
return i;
}
String type = path.split("\\.")[0];
if(type.equalsIgnoreCase("v")) {
i.setType(Material.valueOf(path.split("\\.")[1].toUpperCase()));
ItemMeta m = i.getItemMeta();
LegacyModelData.set(m, Integer.parseInt(path.split("\\.")[2]));
i.setItemMeta(m);
} else if(type.equalsIgnoreCase("ia")) {
ItemAPI api = TLibs.getItemAPI();
i = api.getArmorMerger().merge(i, Optional.empty(), path);
}
return i;
return ModelApplier.apply(i, path);
}

public StationFeedback hit(Player p, ItemStack i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import net.tfminecraft.advancedcrafting.objects.crafting.CraftingRecipe;
import net.tfminecraft.advancedcrafting.objects.crafting.Quality;
import net.tfminecraft.advancedcrafting.objects.ingredients.Ingredient;
import net.tfminecraft.advancedcrafting.objects.schemes.ModelScheme;
import net.tfminecraft.advancedcrafting.objects.stats.StatTemplate;
import net.tfminecraft.advancedcrafting.utils.ModelSchemeResolver;
import net.tfminecraft.advancedcrafting.utils.PDCKeys;

public class CraftProvenance {
Expand Down Expand Up @@ -101,6 +103,14 @@ public void applyTo(ItemStack item) {
meta.getPersistentDataContainer().set(PDCKeys.craftInputs(), PersistentDataType.STRING, GSON.toJson(inputs));
meta.getPersistentDataContainer().set(PDCKeys.craftStatTemplateRevision(), PersistentDataType.INTEGER,
statTemplateRevision);
ModelScheme scheme = ModelSchemeResolver.resolve(RecipeLoader.getByString(recipeId), inputs);
if (scheme != null) {
meta.getPersistentDataContainer().set(PDCKeys.craftModelScheme(), PersistentDataType.STRING,
scheme.getId());
} else {
// A recipe or ingredient that no longer resolves must not leave an old metal for ArmourShop to trust.
meta.getPersistentDataContainer().remove(PDCKeys.craftModelScheme());
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
item.setItemMeta(meta);
}

Expand All @@ -122,7 +132,12 @@ public static CraftProvenance readFrom(ItemStack item) {
if (inputsJson != null) {
List<CraftInput> parsed = GSON.fromJson(inputsJson, new TypeToken<List<CraftInput>>() {}.getType());
if (parsed != null) {
inputs = parsed;
// Every refresh consumer reads these inputs; a hand-edited or truncated record must not break them.
for (CraftInput input : parsed) {
if (input != null && input.getKind() != null && input.getId() != null) {
inputs.add(input);
}
}
}
}
Integer templateRevision = meta.getPersistentDataContainer().get(PDCKeys.craftStatTemplateRevision(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public static ItemStack refreshIfOutdated(ItemStack item) {
}
CraftStatRefresher.RefreshResult crafted = CraftStatRefresher.refreshIfOutdated(item);
if (crafted.isChanged()) {
return crafted.getItem();
return ArmourLookMigrator.migrate(crafted.getItem());
}
AcItemLoreRefresher.RefreshResult lore = AcItemLoreRefresher.refreshIfOutdated(item);
if (lore.isChanged()) {
return lore.getItem();
return ArmourLookMigrator.migrate(lore.getItem());
}
return item;
return ArmourLookMigrator.migrate(item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package net.tfminecraft.advancedcrafting.utils;

import java.util.List;
import java.util.Locale;

import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import io.lumine.mythic.lib.api.item.NBTItem;
import net.tfminecraft.advancedcrafting.cache.Cache;
import net.tfminecraft.advancedcrafting.loaders.RecipeLoader;
import net.tfminecraft.advancedcrafting.objects.crafting.CraftingRecipe;
import net.tfminecraft.advancedcrafting.objects.data.CraftProvenance;
import net.tfminecraft.advancedcrafting.objects.schemes.ModelScheme;
import net.tfminecraft.advancedcrafting.util.LegacyModelData;

/**
* Brings crafted pieces made before the per-recipe models up to date. A piece that still wears the old default
* look of its recipe (its metal's type model, or a configured legacy model) gets the recipe's current model
* through the same merge skins use, so its MMOItems data stays. Any other look is a skin and is kept. Every
* recorded craft also gets its model scheme tag, which ArmourShop's metal skin lines read, and pieces with an
* ItemsAdder look are opted out of ItemsAdder's auto_update (see {@link IaAutoUpdate}).
*/
public final class ArmourLookMigrator {
private ArmourLookMigrator() {
}

/** The same item when nothing changes, otherwise an updated copy. */
public static ItemStack migrate(ItemStack item) {
if (!Cache.armourLookMigration || item == null || item.getType().isAir() || !item.hasItemMeta()) {
return item;
}
CraftProvenance provenance = CraftProvenance.readFrom(item);
if (provenance == null) {
return item;
}
CraftingRecipe recipe = RecipeLoader.getByString(provenance.getRecipeId());
ModelScheme scheme = ModelSchemeResolver.resolve(recipe, provenance.getInputs());
if (scheme == null) {
return item;
}
ItemStack result = item;
String target = scheme.getModel(recipe.getId());
if (target != null && !wears(item, target) && wearsLegacyLook(item, recipe, scheme)) {
String before = describe(item);
result = ModelApplier.apply(item.clone(), target);
ItemMeta meta = result.getItemMeta();
meta.getPersistentDataContainer().set(PDCKeys.previousModel(), PersistentDataType.STRING, before);
result.setItemMeta(meta);
Bukkit.getLogger().info("[AC][LookMigration] " + recipe.getId() + " (" + scheme.getId() + "): " + before
+ " -> " + target);
}
PersistentDataContainer tags = result.getItemMeta().getPersistentDataContainer();
if (!scheme.getId().equals(tags.get(PDCKeys.craftModelScheme(), PersistentDataType.STRING))) {
if (result == item) {
result = item.clone();
}
ItemMeta meta = result.getItemMeta();
meta.getPersistentDataContainer().set(PDCKeys.craftModelScheme(), PersistentDataType.STRING, scheme.getId());
result.setItemMeta(meta);
}
if (IaAutoUpdate.isExposed(result)) {
if (result == item) {
result = item.clone();
}
IaAutoUpdate.protect(result);
}
return result;
}

/** Configured legacy models replace the scheme's type model, e.g. infantry pieces wore the paper look. */
static boolean wearsLegacyLook(ItemStack item, CraftingRecipe recipe, ModelScheme scheme) {
List<String> legacy = Cache.legacyModels.get(recipe.getId().toLowerCase(Locale.ROOT));
if (legacy != null) {
for (String path : legacy) {
if (wears(item, path)) {
return true;
}
}
return false;
}
String typeModel = scheme.getModel(recipe.getType());
return typeModel != null && wears(item, typeModel);
}

/** Whether the item shows this scheme model: the same ItemsAdder item, or the same vanilla material and model. */
static boolean wears(ItemStack item, String path) {
String[] parts = path.split("\\.");
String ia = iaId(item);
if (parts[0].equalsIgnoreCase("ia")) {
return ia != null && ia.equalsIgnoreCase(path.substring(3));
}
if (!parts[0].equalsIgnoreCase("v") || parts.length != 3 || ia != null || hasItemsAdderCompound(item)
|| !item.getType().name().equalsIgnoreCase(parts[1])) {
return false;
}
int model;
try {
model = Integer.parseInt(parts[2]);
} catch (NumberFormatException e) {
return false;
}
ItemMeta meta = item.getItemMeta();
int current = LegacyModelData.has(meta) ? LegacyModelData.get(meta) : 0;
return current == model;
}

/** The look as a scheme path, for the log and the previous-model tag. */
static String describe(ItemStack item) {
String ia = iaId(item);
if (ia != null) {
return "ia." + ia;
}
ItemMeta meta = item.getItemMeta();
int model = LegacyModelData.has(meta) ? LegacyModelData.get(meta) : 0;
return "v." + item.getType().name().toLowerCase(Locale.ROOT) + "." + model;
}

/** TLibs stamps merged ItemsAdder looks as {@code ia: <namespace>.<id>}; returned as namespace:id. */
private static String iaId(ItemStack item) {
NBTItem nbt = NBTItem.get(item);
if (!nbt.hasTag("ia")) {
return null;
}
String ia = nbt.getString("ia");
int dot = ia.indexOf('.');
return dot > 0 && dot < ia.length() - 1 ? ia.substring(0, dot) + ":" + ia.substring(dot + 1) : null;
}

private static boolean hasItemsAdderCompound(ItemStack item) {
return NBTItem.get(item).hasTag("itemsadder");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public static RefreshResult refresh(ItemStack item, boolean force) {
StatData newStats = CraftStatCalculator.compute(recipe, provenance.getInputs());
StatRefreshDebug.logBefore(item, provenance, recipe, newStats);

LiveMMOItem mmo = new LiveMMOItem(NBTItem.get(item));
NBTItem nbt = NBTItem.get(item);
LiveMMOItem mmo = new LiveMMOItem(nbt);
MMOStatApplicator.applyExternalLayer(mmo, newStats, CraftStatCalculator.collectManagedStatIds(recipe), true);
RefreshKeeper.keepWear(nbt, mmo);

ItemStack rebuilt = mmo.newBuilder().build();
ItemStack rebuilt = RefreshKeeper.keepAppearance(item, mmo.newBuilder().build());
rebuilt.setAmount(item.getAmount());
provenance.syncRevisions();
provenance.applyTo(rebuilt);
Expand Down
Loading
Loading