Repository navigation
feat: record the materials each jewelry piece was made from #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/net/tfminecraft/geminfusion/goldsmith/GoldsmithProvenance.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package net.tfminecraft.geminfusion.goldsmith; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.inventory.meta.ItemMeta; | ||
| import org.bukkit.persistence.PersistentDataType; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.reflect.TypeToken; | ||
|
|
||
| import net.tfminecraft.geminfusion.PDCKeys; | ||
|
|
||
| /** | ||
| * Records the materials a finished jewelry piece was actually made from, as item path to amount. | ||
| * A slot accepts any material of its type, so this can differ from the project recipe. | ||
| */ | ||
| public final class GoldsmithProvenance { | ||
|
|
||
| private static final Gson GSON = new Gson(); | ||
| private static final Type INPUTS_TYPE = new TypeToken<LinkedHashMap<String, Integer>>() { | ||
| }.getType(); | ||
|
|
||
| private GoldsmithProvenance() { | ||
| } | ||
|
|
||
| public static void stamp(ItemStack item, Map<GoldsmithMaterial, Integer> deposited) { | ||
| LinkedHashMap<String, Integer> inputs = new LinkedHashMap<>(); | ||
| for (Map.Entry<GoldsmithMaterial, Integer> entry : deposited.entrySet()) { | ||
| inputs.merge(entry.getKey().getPath(), entry.getValue(), Integer::sum); | ||
| } | ||
| ItemMeta meta = item.getItemMeta(); | ||
| meta.getPersistentDataContainer().set(PDCKeys.goldsmithInputs(), PersistentDataType.STRING, | ||
| GSON.toJson(inputs)); | ||
| item.setItemMeta(meta); | ||
| } | ||
|
|
||
| /** | ||
| * @return deposited material paths and amounts, or null for jewelry made before inputs were recorded | ||
| */ | ||
| public static Map<String, Integer> read(ItemStack item) { | ||
| if (item == null || !item.hasItemMeta()) { | ||
| return null; | ||
| } | ||
| String json = item.getItemMeta().getPersistentDataContainer().get(PDCKeys.goldsmithInputs(), | ||
| PersistentDataType.STRING); | ||
| if (json == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| return GSON.fromJson(json, INPUTS_TYPE); | ||
| } catch (JsonParseException ex) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/net/tfminecraft/geminfusion/goldsmith/GoldsmithProvenanceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package net.tfminecraft.geminfusion.goldsmith; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import java.util.*; | ||
| import net.tfminecraft.geminfusion.InfusionMain; | ||
| import net.tfminecraft.geminfusion.PDCKeys; | ||
| import org.bukkit.Material; | ||
| import org.bukkit.inventory.ItemStack; | ||
| import org.bukkit.inventory.meta.ItemMeta; | ||
| import org.bukkit.persistence.PersistentDataType; | ||
| import org.junit.jupiter.api.*; | ||
| import org.mockbukkit.mockbukkit.MockBukkit; | ||
|
|
||
| class GoldsmithProvenanceTest { | ||
| InfusionMain previous; | ||
|
|
||
| @BeforeEach | ||
| void setup() { | ||
| MockBukkit.mock(); | ||
| previous = InfusionMain.plugin; | ||
| InfusionMain.plugin = mock(InfusionMain.class); | ||
| when(InfusionMain.plugin.namespace()).thenReturn("geminfusion"); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void cleanup() { | ||
| InfusionMain.plugin = previous; | ||
| MockBukkit.unmock(); | ||
| } | ||
|
|
||
| private static GoldsmithMaterial material(String path) { | ||
| GoldsmithMaterial material = mock(GoldsmithMaterial.class); | ||
| when(material.getPath()).thenReturn(path); | ||
| return material; | ||
| } | ||
|
|
||
| @Test | ||
| void stampsTheDepositedMaterialsNotTheRecipe() { | ||
| ItemStack ring = new ItemStack(Material.GOLD_INGOT); | ||
| LinkedHashMap<GoldsmithMaterial, Integer> deposited = new LinkedHashMap<>(); | ||
| deposited.put(material("m.materials.shiny_gold"), 3); | ||
| deposited.put(material("m.materials.rough_gold"), 1); | ||
| deposited.put(material("m.materials.shiny_gold"), 2); | ||
|
|
||
| GoldsmithProvenance.stamp(ring, deposited); | ||
|
|
||
| assertEquals( | ||
| Map.of("m.materials.shiny_gold", 5, "m.materials.rough_gold", 1), | ||
| GoldsmithProvenance.read(ring)); | ||
| } | ||
|
|
||
| @Test | ||
| void stampsAnEmptyListWhenNothingWasDeposited() { | ||
| ItemStack ring = new ItemStack(Material.GOLD_INGOT); | ||
| GoldsmithProvenance.stamp(ring, Map.of()); | ||
| assertEquals(Map.of(), GoldsmithProvenance.read(ring)); | ||
| } | ||
|
|
||
| @Test | ||
| void readsNothingFromUnstampedOrUnreadableItems() { | ||
| assertNull(GoldsmithProvenance.read(null)); | ||
| assertNull(GoldsmithProvenance.read(new ItemStack(Material.AIR))); | ||
| assertNull(GoldsmithProvenance.read(new ItemStack(Material.GOLD_INGOT))); | ||
|
|
||
| ItemStack broken = new ItemStack(Material.GOLD_INGOT); | ||
| ItemMeta meta = broken.getItemMeta(); | ||
| meta.getPersistentDataContainer() | ||
| .set(PDCKeys.goldsmithInputs(), PersistentDataType.STRING, "{not json"); | ||
| broken.setItemMeta(meta); | ||
| assertNull(GoldsmithProvenance.read(broken)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.