mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
Prevent Acquired from being used after unlock
This commit is contained in:
parent
9c99cb9b2e
commit
41a0f22468
@ -1,5 +1,6 @@
|
||||
package net.minestom.server.acquirable;
|
||||
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@ -12,6 +13,8 @@ public class Acquired<T> {
|
||||
private final boolean locked;
|
||||
private final ReentrantLock lock;
|
||||
|
||||
private boolean unlocked;
|
||||
|
||||
protected Acquired(@NotNull T value,
|
||||
boolean locked, @Nullable ReentrantLock lock) {
|
||||
this.value = value;
|
||||
@ -20,13 +23,20 @@ public class Acquired<T> {
|
||||
}
|
||||
|
||||
public @NotNull T get() {
|
||||
checkLock();
|
||||
return value;
|
||||
}
|
||||
|
||||
public void unlock() {
|
||||
checkLock();
|
||||
this.unlocked = true;
|
||||
if (!locked)
|
||||
return;
|
||||
Acquisition.acquireLeave(lock);
|
||||
}
|
||||
|
||||
private void checkLock() {
|
||||
Check.stateCondition(unlocked, "The acquired element has already been unlocked!");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user