Skip to content

Fix ClassCastException in BlockCable when sapling grows nearby#1623

Merged
rubensworks merged 2 commits intomaster-1.21-ltsfrom
copilot/fix-sapling-growth-crash
Mar 4, 2026
Merged

Fix ClassCastException in BlockCable when sapling grows nearby#1623
rubensworks merged 2 commits intomaster-1.21-ltsfrom
copilot/fix-sapling-growth-crash

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

BlockCable.updateShape and getCloneItemStack were unconditionally casting LevelAccessor/LevelReader to Level. During world feature placement (e.g. sapling → tree growth), Minecraft invokes updateShape with a lightweight accessor that is not a Level, causing a ClassCastException crash.

Changes

  • updateShape: Wrap NetworkHelpers.onElementProviderBlockNeighborChange in an instanceof Level guard — silently skip the neighbor-change notification when the accessor isn't a full Level.
  • getCloneItemStack: Combine the existing rayTraceResult != null check with world instanceof Level to avoid the unsafe cast when called with a plain LevelReader.
// updateShape — before
NetworkHelpers.onElementProviderBlockNeighborChange((Level) worldIn, currentPos, facingState.getBlock(), facing, facingPos);

// after
if (worldIn instanceof Level level) {
    NetworkHelpers.onElementProviderBlockNeighborChange(level, currentPos, facingState.getBlock(), facing, facingPos);
}

// getCloneItemStack — before
if (rayTraceResult != null) {
    return rayTraceResult.getComponent().getCloneItemStack((Level) world, blockPos);
}

// after
if (rayTraceResult != null && world instanceof Level level) {
    return rayTraceResult.getComponent().getCloneItemStack(level, blockPos);
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Crash with sapling growing triggering BlockCable.updateShape</issue_title>
<issue_description>

Issue type:

  • 🐛 Bug

Short description:

Our ATM10 server keeps crashing, seemingly at random. After inspecting the crash report, we narrowed it down to when saplings (in our case oak) were growing near logic cables. Attempts to forcibly reproduce it were unsuccessful.

Steps to reproduce the problem:

  1. Plant oak sapling in close proximity to logic cable
  2. Hope it crashes after the sapling grows.

Expected behaviour:

N/A


Versions:

  • This mod: 1.30.5
  • Minecraft: 1.21.1
  • Mod loader version: NeoForge 21.1.219

Log file:

crash-2026-03-03_20.17.53-server.txt

</issue_description>

<agent_instructions>The problem here is that we're unsafely casting to (Level) in BlockCable. Could you wrap these in an instanceof check? There should be two places in BlockCable where this happens, and needs a safety check.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@rubensworks

Thanks for reporting!


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: rubensworks <440384+rubensworks@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix crash with sapling growing triggering BlockCable.updateShape Fix ClassCastException in BlockCable when sapling grows nearby Mar 4, 2026
@rubensworks rubensworks marked this pull request as ready for review March 4, 2026 18:14
@rubensworks rubensworks merged commit bd4a0d9 into master-1.21-lts Mar 4, 2026
5 checks passed
@coveralls
Copy link

Coverage Status

coverage: 45.725% (-0.01%) from 45.735%
when pulling 464b62a on copilot/fix-sapling-growth-crash
into 67ec6fb on master-1.21-lts.

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.

Crash with sapling growing triggering BlockCable.updateShape

3 participants