Skip to content

Replace Map.of with array in ItemCollectorBlockEntity#3805

Open
ZhuRuoLing wants to merge 1 commit into
Anvil-Dev:dev/26.1/1.6from
ZhuRuoLing:chore/do_not_use_integer_map
Open

Replace Map.of with array in ItemCollectorBlockEntity#3805
ZhuRuoLing wants to merge 1 commit into
Anvil-Dev:dev/26.1/1.6from
ZhuRuoLing:chore/do_not_use_integer_map

Conversation

@ZhuRuoLing

Copy link
Copy Markdown
Contributor

No description provided.

@anvil-craft

Copy link
Copy Markdown

⚠️ Dangerous command requires approval:

source /opt/data/.env 2>/dev/null && curl -sL \
  -H "Authorization: token $GITHUB_TOKEN" \
  "https://api.github.com/search/code?q=WatchableCyclingValue+repo:Anvil-Dev/AnvilCraft+path:src" \
  | pyth...

Reason: Security scan — [HIGH] Pipe to interpreter: curl | python3: Command pipes output from 'curl' directly to interpreter 'python3'. Downloaded content will be executed without inspection.
Safer: tirith run https://api.github.com/search/code?q=WatchableCyclingValue+repo:Anvil-Dev/AnvilCraft+path:src — or: vet https://api.github.com/search/code?q=WatchableCyclingValue+repo:Anvil-Dev/AnvilCraft+path:src (https://getvet.sh); script execution via -e/-c flag

Reply /approve to execute, /approve session to approve this pattern for the session, /approve always to approve permanently, or /deny to cancel.

@Override
public int getInputPower() {
int power = ItemCollectorBlockEntity.POWER_CONSUMPTION.get(this.cooldown.get()).get(this.rangeRadius.get());
int power = getPowerConsumption();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [Checkstyle] <com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck> reported by reviewdog 🐶
Method call to 'getPowerConsumption' needs "this.".

@anvil-craft

Copy link
Copy Markdown

现在我已获得全部信息。可以确认 WatchableCyclingValue.index() 返回 0-based position,映射完全正确。


代码审查摘要

结论:评论(无阻塞性问题,纯正向重构)

变更概览

一个文件变更:ItemCollectorBlockEntity.java

  • 删除private static final Map<Integer, Map<Integer, Integer>> POWER_CONSUMPTION(嵌套 Map.of
  • 新增public static final int[][] POWER_CONSUMPTION(二维数组)
  • 新增getPowerConsumption() 方法封装查找逻辑
  • 更新getInputPower() 从直接查表改为调用 getPowerConsumption()

✅ 语义正确性确认

通过交叉验证 WatchableCyclingValue 实现确认:

cooldown 值 cooldown.index() 旧 Map key 旧值映射 新数组行
0 0 0 {1→8,2→12,4→20,8→32} {8,12,20,32}
2 1 2 {1→5,2→8,4→12,8→20} {5,8,12,20}
10 2 10 {1→3,2→5,4→8,8→12} {3,5,8,12}
60 3 60 {1→2,2→3,4→5,8→8} {2,3,5,8}

WatchableCyclingValue<T>.get() 返回 values[index].index() 返回 this.index(0-based position)。由于构造参数的顺序完全匹配,映射关系严格守恒。

💡 建议

  1. POWER_CONSUMPTION 可保持 private——改为 public 暴露了内部表示,但 getPowerConsumption() 已提供访问入口。不过作为 static final 引用不可变,影响较小。

  2. 可清理 Map 导入——原文件顶部有 import java.util.Map;,如果其他地方不再使用可移除(IDE 自动优化即可)。

✅ 看起来不错

  • 性能提升int[][] 相比 Map<Integer, Map<Integer, Integer>> 去掉了自动装箱/散列查找,更缓存友好
  • 可读性提升getPowerConsumption() 将消耗计算逻辑集中,调用方不再关心内部数据结构
  • 零行为变化:新旧代码对所有 16 种 (cooldown, rangeRadius) 组合返回完全相同的功耗值
  • 改动范围极小:仅一个文件 42 行 diff,易于审核和回滚

结论:安全、正向的代码质量改进,建议合并。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants