Skip to content

Fix XP orb spawning to use queue system to prevent client lag on large amounts of xp spawns#96

Open
SkyzerFlyzer wants to merge 1 commit into
Shadows-of-Fire:1.21from
SkyzerFlyzer:claude/fix-xp-orb-spawning-QmCIT
Open

Fix XP orb spawning to use queue system to prevent client lag on large amounts of xp spawns#96
SkyzerFlyzer wants to merge 1 commit into
Shadows-of-Fire:1.21from
SkyzerFlyzer:claude/fix-xp-orb-spawning-QmCIT

Conversation

@SkyzerFlyzer

Copy link
Copy Markdown

This commit fixes all XP orb spawning issues by implementing a proper queue system and respecting Minecraft's 16-bit integer limit.

Problems Fixed

  1. All XP orbs spawned instantly

    • Previously: Loop spawned all orbs immediately in one tick
    • Now: XP orbs queue and spawn gradually over time
  2. Doesn't respect 32,767 max orb value

    • Previously: Always used orbSize, never considered limit
    • Now: Clamps to MAX_ORB_VALUE = 32767 (16-bit signed int)
  3. Last orb value bug

    • Previously: 2502 XP with size 5 → 501×5 = 2505 XP (3 extra!)
    • Now: 2502 XP with size 5 → 500×5 + 1×2 = 2502 XP ✓
  4. XP orbs spawn at least 3x faster than items

    • Previously: Items spawned at 3-10 per cycle
    • Now: XP spawns at 30 per cycle (minimum 3x faster)
    • Prevents long delays with large XP rewards

Implementation

GatewayEntity.java:

  • Added Queue<Integer> undroppedXP field
  • Added spawnXPOrb(int value) method
  • Added queueXP(int value) method
  • Added getXPDropCount() returning 30 (3x faster than items)
  • Updated tick() to spawn XP orbs in parallel with items
  • Added NBT serialization for XP queue (save/load)
  • Added ExperienceOrb import

NormalGatewayEntity.java:

  • Updated isCompleted() to check XP queue is empty

Reward.java (ExperienceReward):

  • Added MAX_ORB_VALUE = 32767 constant
  • Changed to queue XP instead of spawning directly
  • Fixed remainder calculation: Math.min(remaining, clampedOrbSize)
  • Respects 32,767 limit by clamping orbSize

Technical Details

XP orbs now follow same pattern as items:

  • Queue during reward generation
  • Spawn gradually every 4 ticks via tick()
  • Persist across world reloads (NBT)
  • Gateway waits for all XP to spawn before completing

Spawn rate: 30 XP orbs per 4 ticks (vs 3-10 items per 4 ticks)

Fixes: All reported XP orb spawning issues

This commit fixes all XP orb spawning issues by implementing a proper
queue system and respecting Minecraft's 16-bit integer limit.

## Problems Fixed

1. **All XP orbs spawned instantly**
   - Previously: Loop spawned all orbs immediately in one tick
   - Now: XP orbs queue and spawn gradually over time

2. **Doesn't respect 32,767 max orb value**
   - Previously: Always used orbSize, never considered limit
   - Now: Clamps to MAX_ORB_VALUE = 32767 (16-bit signed int)

3. **Last orb value bug**
   - Previously: 2502 XP with size 5 → 501×5 = 2505 XP (3 extra!)
   - Now: 2502 XP with size 5 → 500×5 + 1×2 = 2502 XP ✓

4. **XP orbs spawn at least 3x faster than items**
   - Previously: Items spawned at 3-10 per cycle
   - Now: XP spawns at 30 per cycle (minimum 3x faster)
   - Prevents long delays with large XP rewards

## Implementation

**GatewayEntity.java:**
- Added `Queue<Integer> undroppedXP` field
- Added `spawnXPOrb(int value)` method
- Added `queueXP(int value)` method
- Added `getXPDropCount()` returning 30 (3x faster than items)
- Updated tick() to spawn XP orbs in parallel with items
- Added NBT serialization for XP queue (save/load)
- Added ExperienceOrb import

**NormalGatewayEntity.java:**
- Updated `isCompleted()` to check XP queue is empty

**Reward.java (ExperienceReward):**
- Added `MAX_ORB_VALUE = 32767` constant
- Changed to queue XP instead of spawning directly
- Fixed remainder calculation: `Math.min(remaining, clampedOrbSize)`
- Respects 32,767 limit by clamping orbSize

## Technical Details

XP orbs now follow same pattern as items:
- Queue during reward generation
- Spawn gradually every 4 ticks via tick()
- Persist across world reloads (NBT)
- Gateway waits for all XP to spawn before completing

Spawn rate: 30 XP orbs per 4 ticks (vs 3-10 items per 4 ticks)

Fixes: All reported XP orb spawning issues
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