Slimefun / Slimefun4

Slimefun 4 - A unique Spigot/Paper plugin that looks and feels like a modpack. We've been giving you backpacks, jetpacks, reactors and much more since 2013.
GNU General Public License v3.0
958 stars 547 forks source link

Fix: solve teleportation time overflow problem. #4163

Open Violet-Nonbloosom opened 5 months ago

Violet-Nonbloosom commented 5 months ago

Description

In TP time calculation, $\rm complexity \times complexity$ may result in int overflow when complexity is higher than $4\times 10^{4}$, not difficult to achieve with several popular addons.

Compared with original solution based on Math function, it is better to avoid overflow directly.

Proposed changes

Use long to storage temporary speed variable, which hardly overflow in TP time calculation.

If $\rm speed > distance$, time cost must be 1 tick, so we can skip calculation.

Otherwise, $\rm \dfrac {distance}{speed} \geq 1$, while speed must be in range of int.

Therefore we can simply convert speed to int variable to calculate ultimate time cost.

Related Issues (if applicable)

Checklist

github-actions[bot] commented 5 months ago
Pro Tip! You can help us label your Pull Requests by using the following branch naming convention next time you create a pull request. ❤️ Branch naming convention Label
feature/** 🎈 Feature
fix/** ✨ Fix
chore/** 🧹 Chores
api/** 🔧 API
performance/** 💡 Performance Optimization
compatibility/** 🤝 Compatibility

If your changes do not fall into any of these categories, don't worry. You can just ignore this message in that case! 👀

github-actions[bot] commented 5 months ago

Slimefun preview build

A Slimefun preview build is available for testing! Commit: afc73256

https://preview-builds.walshy.dev/download/Slimefun/4163/afc73256

Note: This is not a supported build and is only here for the purposes of testing. Do not run this on a live server and do not report bugs anywhere but this PR!

J3fftw1 commented 5 months ago

Why are we moving it to a long? Aren’t we delaying the issue then, this isn’t solving the issue.

should limit it to a value instead of moving the problem in my opinion

Violet-Nonbloosom commented 5 months ago

Why are we moving it to a long? Aren’t we delaying the issue then, this isn’t solving the issue.

should limit it to a value instead of moving the problem in my opinion

Though it may be more robust, I have no idea what will be happening in future overflow bug.

If long was not enough for complexity², int might not be suitable for complexity as well, which meant a thorough rework would be needed.

Besides, I have another subject to work at present, so my solution ends here.

Violet-Nonbloosom commented 5 months ago

Why are we moving it to a long? Aren’t we delaying the issue then, this isn’t solving the issue.

should limit it to a value instead of moving the problem in my opinion

@J3fftw1 As you wish.