-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework async-commands fix for all Paper version
Use some sneaky reflection to intercept `net.minecraft.server.CommandDispatcher`'s task scheduling and enforce our own read-write access Updates #501 Fixes #494 and #503
- Loading branch information
1 parent
1782863
commit c457c8c
Showing
20 changed files
with
845 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...kkit-core/src/main/java/dev/jorel/commandapi/paper/CommandDispatcherReadWriteManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dev.jorel.commandapi.paper; | ||
|
||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Since <a href="https://github.com/PaperMC/Paper/pull/3116">PaperMC/Paper#3116</a>, Paper sometimes reads | ||
* from the Brigadier CommandDispatcher asyncronously. This can cause ConcurrentModificationExceptions if the | ||
* CommandAPI tries to create a command and write to the dispatcher at the same time. | ||
* <p> | ||
* This class handles locking read and write access at appropriate times. | ||
* <p> | ||
* See <a href="https://github.com/JorelAli/CommandAPI/pull/501">CommandAPI#501</a> for more details. | ||
*/ | ||
public class CommandDispatcherReadWriteManager { | ||
private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); | ||
|
||
public void runWriteTask(Runnable writeTask) { | ||
readWriteLock.writeLock().lock(); | ||
try { | ||
writeTask.run(); | ||
} finally { | ||
readWriteLock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
public <T> T runWriteTask(Supplier<T> writeTask) { | ||
readWriteLock.writeLock().lock(); | ||
try { | ||
return writeTask.get(); | ||
} finally { | ||
readWriteLock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
public Runnable wrapReadTask(Runnable originalTask) { | ||
return () -> { | ||
readWriteLock.readLock().lock(); | ||
try { | ||
originalTask.run(); | ||
} finally { | ||
readWriteLock.readLock().unlock(); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.