mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 05:58:00 +01:00
Added Acquired
This commit is contained in:
parent
0462c8f9a7
commit
26bd0dd43c
@ -28,6 +28,7 @@ import net.minestom.server.command.CommandManager;
|
||||
import net.minestom.server.command.CommandSender;
|
||||
import net.minestom.server.effects.Effects;
|
||||
import net.minestom.server.entity.acquirable.AcquirablePlayer;
|
||||
import net.minestom.server.entity.acquirable.Acquired;
|
||||
import net.minestom.server.entity.damage.DamageType;
|
||||
import net.minestom.server.entity.fakeplayer.FakePlayer;
|
||||
import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
|
||||
@ -329,12 +330,14 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
|
||||
}
|
||||
|
||||
//System.out.println(getAcquiredElement().getHandler().getBatchThread());
|
||||
//if (username.equals("TheMode911"))
|
||||
/*for (Player p1 : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
|
||||
if (username.equals("TheMode911"))
|
||||
for (Player p : MinecraftServer.getConnectionManager().getOnlinePlayers()) {
|
||||
//players.add(p1.getAcquiredElement());
|
||||
p1.getAcquirable().acquire(o -> {
|
||||
var acquired = p.getAcquirable().acquire();
|
||||
acquired.sync(player -> {
|
||||
//System.out.println("sync");
|
||||
});
|
||||
}*/
|
||||
}
|
||||
|
||||
super.update(time); // Super update (item pickup/fire management)
|
||||
|
||||
|
@ -51,49 +51,9 @@ public class AcquirableEntity {
|
||||
this.handler = new Handler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocks the current thread until 'this' can be acquired,
|
||||
* and execute {@code consumer} as a callback with the acquired object.
|
||||
*
|
||||
* @param consumer the acquisition consumer
|
||||
*/
|
||||
public void acquire(@NotNull EntityConsumer consumer) {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
public @NotNull Acquired<? extends Entity> acquire() {
|
||||
final TickThread elementThread = getHandler().getTickThread();
|
||||
Acquisition.acquire(currentThread, elementThread, () -> consumer.accept(unwrap()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes {@code consumer} only if this element can be safely
|
||||
* acquired without any synchronization.
|
||||
*
|
||||
* @param consumer the acquisition consumer
|
||||
* @return true if the acquisition happened without synchronization,
|
||||
* false otherwise
|
||||
*/
|
||||
public boolean tryAcquire(@NotNull EntityConsumer consumer) {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final TickThread elementThread = getHandler().getTickThread();
|
||||
if (Objects.equals(currentThread, elementThread)) {
|
||||
consumer.accept(unwrap());
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves {@link #unwrap()} only if this element can be safely
|
||||
* acquired without any synchronization.
|
||||
*
|
||||
* @return this element or null if unsafe
|
||||
*/
|
||||
public @Nullable Entity tryAcquire() {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
final TickThread elementThread = getHandler().getTickThread();
|
||||
if (Objects.equals(currentThread, elementThread)) {
|
||||
return unwrap();
|
||||
}
|
||||
return null;
|
||||
return new Acquired<>(unwrap(), elementThread);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,16 +2,17 @@ package net.minestom.server.entity.acquirable;
|
||||
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.utils.consumer.PlayerConsumer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AcquirablePlayer extends AcquirableEntity {
|
||||
|
||||
public AcquirablePlayer(@NotNull Entity entity) {
|
||||
super(entity);
|
||||
}
|
||||
|
||||
public void acquire(@NotNull PlayerConsumer consumer) {
|
||||
super.acquire(consumer);
|
||||
@Override
|
||||
public @NotNull Acquired<? extends Player> acquire() {
|
||||
return (Acquired<? extends Player>) super.acquire();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -0,0 +1,42 @@
|
||||
package net.minestom.server.entity.acquirable;
|
||||
|
||||
import net.minestom.server.thread.TickThread;
|
||||
import net.minestom.server.utils.async.AsyncUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Acquired<T> {
|
||||
|
||||
private final T value;
|
||||
private final TickThread tickThread;
|
||||
|
||||
protected Acquired(@NotNull T value, @NotNull TickThread tickThread) {
|
||||
this.value = value;
|
||||
this.tickThread = tickThread;
|
||||
}
|
||||
|
||||
public void sync(@NotNull Consumer<T> consumer) {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
Acquisition.acquire(currentThread, tickThread, () -> consumer.accept(unwrap()));
|
||||
}
|
||||
|
||||
public void async(@NotNull Consumer<T> consumer) {
|
||||
// TODO per-thread list
|
||||
AsyncUtils.runAsync(() -> sync(consumer));
|
||||
}
|
||||
|
||||
public @NotNull Optional<T> optional() {
|
||||
final Thread currentThread = Thread.currentThread();
|
||||
if (Objects.equals(currentThread, tickThread)) {
|
||||
return Optional.of(unwrap());
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public @NotNull T unwrap() {
|
||||
return value;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user