diff --git a/src/main/java/net/tfminecraft/advancedcrafting/objects/crafting/CraftingStation.java b/src/main/java/net/tfminecraft/advancedcrafting/objects/crafting/CraftingStation.java index 523d70c..6c6880c 100644 --- a/src/main/java/net/tfminecraft/advancedcrafting/objects/crafting/CraftingStation.java +++ b/src/main/java/net/tfminecraft/advancedcrafting/objects/crafting/CraftingStation.java @@ -204,6 +204,12 @@ public StationFeedback addMaterial(Player p, ItemStack i) { if(c.isAlloy()) { Alloy a = c.getAlloy(); if (a == null) return StationFeedback.NOT_INGREDIENT; + // An alloy needs the same Smith perk as its base; alloys without a known base stay usable. + if (!ProfessionPermissions.canUseAlloy(p, a)) { + p.sendMessage(ProfessionPermissions.missingIngredientPermissionMessage( + ProfessionPermissions.alloyPermission(a))); + return StationFeedback.NO_PERMS; + } key = "alloy."+a.getId(); type = a.getData().getType(); mergeHits = a.getData().getHits(); diff --git a/src/main/java/net/tfminecraft/advancedcrafting/utils/ProfessionPermissions.java b/src/main/java/net/tfminecraft/advancedcrafting/utils/ProfessionPermissions.java index c9e50d1..1e2d918 100644 --- a/src/main/java/net/tfminecraft/advancedcrafting/utils/ProfessionPermissions.java +++ b/src/main/java/net/tfminecraft/advancedcrafting/utils/ProfessionPermissions.java @@ -6,6 +6,7 @@ import net.tfminecraft.advancedcrafting.loaders.IngredientLoader; import net.tfminecraft.advancedcrafting.managers.AlloyManager; import net.tfminecraft.advancedcrafting.objects.alloys.Alloy; +import net.tfminecraft.advancedcrafting.objects.data.AlloyRecipe; import net.tfminecraft.advancedcrafting.objects.data.IngredientData; import net.tfminecraft.advancedcrafting.objects.data.PermissionNamespace; import net.tfminecraft.advancedcrafting.objects.ingredients.Ingredient; @@ -98,6 +99,31 @@ public static boolean canUseIngredient(Player player, Ingredient ingredient) { return hasIngredientPerm(player, data.getPermission()); } + /** + * The permission an alloy needs: the same as its base (the metal or wood it was forged on). + * Null when the base is not recorded, no longer configured or needs no permission. + */ + public static String alloyPermission(Alloy alloy) { + if (alloy == null || alloy.getData() == null) { + return null; + } + AlloyRecipe recipe = alloy.getData().getRecipe(); + if (recipe == null) { + return null; + } + Ingredient base = IngredientLoader.getByString(recipe.getBaseId()); + if (base == null || base.getIngredientData() == null) { + return null; + } + return base.getIngredientData().getPermission(); + } + + /** True when the player may work the alloy; alloys whose base needs no known permission stay usable. */ + public static boolean canUseAlloy(Player player, Alloy alloy) { + String permission = alloyPermission(alloy); + return permission == null || hasIngredientPerm(player, permission); + } + public static int resolveTier(IngredientData data) { if (data == null || !data.canBeBase() || !data.hasTier()) { return 0; diff --git a/src/test/java/net/tfminecraft/advancedcrafting/MathCoverageTest.java b/src/test/java/net/tfminecraft/advancedcrafting/MathCoverageTest.java index 5fcb92d..c17e22b 100644 --- a/src/test/java/net/tfminecraft/advancedcrafting/MathCoverageTest.java +++ b/src/test/java/net/tfminecraft/advancedcrafting/MathCoverageTest.java @@ -6,6 +6,7 @@ import java.util.*; import net.tfminecraft.advancedcrafting.cache.Cache; import net.tfminecraft.advancedcrafting.loaders.*; +import net.tfminecraft.advancedcrafting.objects.alloys.Alloy; import net.tfminecraft.advancedcrafting.objects.data.*; import net.tfminecraft.advancedcrafting.objects.ingredients.*; import net.tfminecraft.advancedcrafting.objects.stats.*; @@ -179,6 +180,28 @@ void professionPermissionsRequireExactTierAndSupportUnrestrictedMaterials() thro assertFalse(ProfessionPermissions.canUseIngredient(player, null)); assertFalse(ProfessionPermissions.canUseIngredient(player, mock(Ingredient.class))); assertEquals(0, ProfessionPermissions.resolveAlloyTier("unknown")); + StatData none = new StatData(); + Alloy forged = new Alloy("a", "A", new AlloyData(base, none, new HashMap<>(), null)); + assertNull(ProfessionPermissions.alloyPermission(null)); + assertNull(ProfessionPermissions.alloyPermission(mock(Alloy.class))); + assertNull(ProfessionPermissions.alloyPermission(forged)); + assertTrue(ProfessionPermissions.canUseAlloy(null, forged)); + forged.getData().setRecipe(new AlloyRecipe("gone", List.of())); + assertNull(ProfessionPermissions.alloyPermission(forged)); + Ingredient blank = mock(Ingredient.class); + when(blank.getId()).thenReturn("blank"); + IngredientLoader.oList.add(blank); + forged.getData().setRecipe(new AlloyRecipe("blank", List.of())); + assertNull(ProfessionPermissions.alloyPermission(forged)); + forged.getData().setRecipe(new AlloyRecipe("copper", List.of())); + assertNull(ProfessionPermissions.alloyPermission(forged)); + assertTrue(ProfessionPermissions.canUseAlloy(null, forged)); + forged.getData().setRecipe(new AlloyRecipe("IRON", List.of("copper"))); + assertEquals("smith", ProfessionPermissions.alloyPermission(forged)); + assertTrue(ProfessionPermissions.canUseAlloy(player, forged)); + assertFalse(ProfessionPermissions.canUseAlloy(null, forged)); + when(player.hasPermission("professions.smith")).thenReturn(false); + assertFalse(ProfessionPermissions.canUseAlloy(player, forged)); } @Test diff --git a/src/test/java/net/tfminecraft/advancedcrafting/StationCoverageTest.java b/src/test/java/net/tfminecraft/advancedcrafting/StationCoverageTest.java index 65bbe35..f745742 100644 --- a/src/test/java/net/tfminecraft/advancedcrafting/StationCoverageTest.java +++ b/src/test/java/net/tfminecraft/advancedcrafting/StationCoverageTest.java @@ -94,6 +94,57 @@ void materialAdmissionChecksItemTypePermissionsRecipeAndCapacity() throws Except assertEquals(StationFeedback.SUCCESS, other.addMaterial(p, catalyst.build())); } + @Test + void alloysNeedTheirBasePermissionUnlessTheBaseIsUnknown() throws Exception { + var p = server.addPlayer(); + var station = new CraftingStation(loc()); + station.setRecipe(recipe("recipe: ['metal.3']")); + Cache.permissionNamespaces.put("mythril_smith", new PermissionNamespace("mythril_smith", "Mythril Smith")); + var mythril = + ingredient("mythril", "base: true\npermission: mythril_smith\nhits: ['strike.2']"); + ingredient("tin", "hits: ['strike.1']"); + var forged = + new Alloy( + "starsteel", + "Starsteel", + new AlloyData( + mythril, + new net.tfminecraft.advancedcrafting.objects.data.StatData(), + new HashMap<>(Map.of(HitLoader.map.get("strike"), 1)), + null)); + forged.getData().setRecipe(new AlloyRecipe("mythril", List.of("tin"))); + AlloyManager.addAlloy(forged); + var item = alloyItem("starsteel"); + item.setAmount(5); + p.getInventory().setItemInMainHand(item); + // Without the base's perk the alloy is refused with the ingredient refusal message. + assertEquals(StationFeedback.NO_PERMS, station.addMaterial(p, item)); + assertEquals( + ProfessionPermissions.missingIngredientPermissionMessage("mythril_smith"), p.nextMessage()); + assertTrue(station.getCurrentMaterials().isEmpty()); + assertEquals(5, p.getInventory().getItemInMainHand().getAmount()); + // An old alloy with no recorded base keeps working as before. + var old = + new Alloy( + "oldsteel", + "Oldsteel", + new AlloyData( + mythril, + new net.tfminecraft.advancedcrafting.objects.data.StatData(), + new HashMap<>(), + null)); + AlloyManager.addAlloy(old); + var oldItem = alloyItem("oldsteel"); + p.getInventory().setItemInMainHand(oldItem); + assertEquals(StationFeedback.SUCCESS, station.addMaterial(p, oldItem)); + // With the perk the recorded-base alloy is accepted. + p.addAttachment( + org.mockbukkit.mockbukkit.MockBukkit.createMockPlugin(), "professions.mythril_smith", true); + p.getInventory().setItemInMainHand(item); + assertEquals(StationFeedback.SUCCESS, station.addMaterial(p, item)); + assertEquals(1, station.getCurrentMaterials().get("alloy.starsteel")); + } + @Test void statusLinesShowMaterialsAndHitsDoneButNoNeededHits() throws Exception { var p = server.addPlayer();