Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

Expand All @@ -16,7 +17,8 @@

public class HitLoader implements LoaderInterface{

public static HashMap<String, CraftingHit> map = new HashMap<>();
// Keeps config order so the branding status lists hits as written.
public static HashMap<String, CraftingHit> map = new LinkedHashMap<>();
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public static HashMap<String, CraftingHit> get(){
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ public void openStation(PlayerInteractEvent e) {
}

if(i.getType().equals(Material.AIR)) return;
if (Cache.brandingTool != null && api.getChecker().checkItemWithPath(i, Cache.brandingTool)) {
for (String line : station.getStatusLines()) {
p.sendMessage(line);
}
return;
}
StationFeedback f = station.addMaterial(p, i);
switch (f) {
case NOT_INGREDIENT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,28 @@ public StationFeedback addMaterial(Player p, ItemStack i) {
return StationFeedback.SUCCESS;
}

/** Lines the branding tool shows on right-click: recipe, materials, and the current count of every configured hit. */
public List<String> getStatusLines() {
List<String> lines = new ArrayList<>();
lines.add("§7Recipe: " + recipe.getCleanedName());
for (Map.Entry<IngredientType, IntCounter> e : types.entrySet()) {
if (e.getKey() == null) continue;
lines.add(e.getKey().getName() + "§7: §e" + e.getValue().getCurrent() + "/" + e.getValue().getNeeded());
}
// Counted by id: a reload replaces the hit objects this station was keyed with.
Map<String, Integer> done = new HashMap<>();
for (Map.Entry<CraftingHit, IntCounter> e : hits.entrySet()) {
if (e.getKey() == null) continue;
done.merge(e.getKey().getId(), e.getValue().getCurrent(), Integer::sum);
}
for (CraftingHit hit : HitLoader.get().values()) {
lines.add(hit.getName() + "§7: §e" + done.getOrDefault(hit.getId(), 0));
}
lines.add("§7Left-click branding to finish");
lines.add("§cSHIFT + LEFT CLICK with the branding tool to cancel the project!");
return lines;
}

public boolean hasAllMaterials(Player p) {
return checkItems(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ void openingStationCreatesSelectionThenReportsMaterialFeedback() throws Exceptio
}
}

@Test
void rightClickWithBrandingShowsStatusInsteadOfAddingIt() throws Exception {
var p = player();
var b = block();
blocks(b);
var m = new CraftingManager();
var s = station(m, b);
when(s.hasRecipe()).thenReturn(true);
when(s.getStatusLines()).thenReturn(List.of("§7Recipe: Sword", "Strike§7: §e3"));
p.getInventory().setItemInMainHand(new ItemStack(Material.STICK));
Cache.brandingTool = "v.stick";
when(items.getChecker().checkItemWithPath(any(), eq(Cache.brandingTool))).thenReturn(true);
m.openStation(event(p, b, Action.RIGHT_CLICK_BLOCK));
verify(s, never()).addMaterial(any(), any());
verify(p).sendMessage("§7Recipe: Sword");
verify(p).sendMessage("Strike§7: §e3");
Cache.brandingTool = null;
}

@Test
void brandingCancelsFinishesOrReportsIncompleteCraftAndToolsRouteHits() throws Exception {
var p = player();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@ void materialAdmissionChecksItemTypePermissionsRecipeAndCapacity() throws Except
assertEquals(StationFeedback.SUCCESS, other.addMaterial(p, catalyst.build()));
}

@Test
void statusLinesListEveryConfiguredHitWithItsCount() throws Exception {
var p = server.addPlayer();
HitLoader.map.put("tap", new CraftingHit("tap", yaml("name: Tap\ntype: hammer\ntool: v.stick")));
var station = new CraftingStation(loc());
station.setRecipe(recipe("recipe: ['metal.2']"));
var item = ingredient("iron", "hits: ['strike.2']").build();
p.getInventory().setItemInMainHand(item);
assertEquals(StationFeedback.SUCCESS, station.addMaterial(p, item));
station.getHits().get(HitLoader.map.get("strike")).setCurrent(3);
// A reload swaps in new hit objects; counts must still match by id.
HitLoader.map.put(
"strike", new CraftingHit("strike", yaml("name: Strike\ntype: hammer\ntool: v.iron_axe")));
assertEquals(
List.of(
"§7Recipe: Sword",
"Metal§7: §e1/2",
"Strike§7: §e3",
"Tap§7: §e0",
"§7Left-click branding to finish",
"§cSHIFT + LEFT CLICK with the branding tool to cancel the project!"),
station.getStatusLines());
}

@Test
void restoredStationsAggregateMaterialAndHitCounts() throws Exception {
var loc = loc();
Expand Down
Loading