Refactor event dispatch system

This commit is contained in:
William committed 2023-03-17 10:55:28 +00:00
1 parent 9ca141a58f
commit 9903503f03
27 files changed
+329 -404

No files matched your search

@@ -1,6 +1,7 @@
package net.william278.huskhomes.teleport;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.event.ITeleportEvent;
import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.network.Message;
import net.william278.huskhomes.network.Payload;
@@ -11,10 +12,11 @@ import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
public class Teleport {
private final HuskHomes plugin;
protected final HuskHomes plugin;
protected final OnlineUser executor;
protected final Teleportable teleporter;
protected final Target target;
@@ -54,20 +56,23 @@ public class Teleport {
throw new TeleportationException(TeleportationException.Type.TELEPORTER_NOT_FOUND);
}
if (target instanceof Username username) {
Message.builder()
.type(Message.Type.TELEPORT_TO_NETWORKED_USER)
.target(username.name())
.payload(Payload.withString(teleporter.name()))
.build().send(plugin.getMessenger(), executor);
return;
}
fireEvent((event) -> {
executeEconomyActions();
if (target instanceof Username username) {
Message.builder()
.type(Message.Type.TELEPORT_TO_NETWORKED_USER)
.target(username.name())
.payload(Payload.withString(teleporter.name()))
.build().send(plugin.getMessenger(), executor);
return;
}
Message.builder()
.type(Message.Type.TELEPORT_TO_POSITION)
.target(teleporter.name())
.payload(Payload.withPosition((Position) target))
.build().send(plugin.getMessenger(), executor);
Message.builder()
.type(Message.Type.TELEPORT_TO_POSITION)
.target(teleporter.name())
.payload(Payload.withPosition((Position) target))
.build().send(plugin.getMessenger(), executor);
});
return;
}
@@ -76,24 +81,31 @@ public class Teleport {
if (target instanceof Username username) {
final Optional<OnlineUser> localTarget = username.findLocally(plugin);
if (localTarget.isPresent()) {
economyActions.forEach(action -> plugin.performEconomyTransaction(executor, action));
teleporter.teleportLocally(localTarget.get().getPosition(), async);
fireEvent((event) -> {
executeEconomyActions();
teleporter.teleportLocally(localTarget.get().getPosition(), async);
});
return;
}
if (plugin.getSettings().isCrossServer()) {
economyActions.forEach(action -> plugin.performEconomyTransaction(executor, action));
Message.builder()
.type(Message.Type.TELEPORT_TO_NETWORKED_POSITION)
.target(username.name())
.build().send(plugin.getMessenger(), executor);
fireEvent((event) -> {
executeEconomyActions();
Message.builder()
.type(Message.Type.TELEPORT_TO_NETWORKED_POSITION)
.target(username.name())
.build().send(plugin.getMessenger(), executor);
});
return;
}
throw new TeleportationException(TeleportationException.Type.TARGET_NOT_FOUND);
}
economyActions.forEach(action -> plugin.performEconomyTransaction(executor, action));
teleporter.teleportLocally((Position) target, async);
fireEvent((event) -> {
executeEconomyActions();
teleporter.teleportLocally((Position) target, async);
});
}
@NotNull
@@ -104,14 +116,23 @@ public class Teleport {
return Optional.of((OnlineUser) this.teleporter);
}
// Fire the teleport event
private void fireEvent(@NotNull Consumer<ITeleportEvent> afterFired) {
plugin.fireEvent(plugin.getTeleportEvent(this), afterFired);
}
// Check economy actions
protected void validateEconomyActions() throws TeleportationException {
economyActions.stream()
if (economyActions.stream()
.map(action -> plugin.validateEconomyCheck(executor, action))
.filter(result -> !result).findFirst()
.ifPresent(result -> {
throw new TeleportationException(TeleportationException.Type.ECONOMY_ACTION_FAILED);
});
.anyMatch(result -> !result)) {
throw new TeleportationException(TeleportationException.Type.ECONOMY_ACTION_FAILED);
}
}
// Perform transactions on economy actions
private void executeEconomyActions() {
economyActions.forEach(action -> plugin.performEconomyTransaction(executor, action));
}
@NotNull
@@ -10,12 +10,10 @@ import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
public class TimedTeleport extends Teleport {
private final HuskHomes plugin;
private final OnlineUser teleporter;
private final Position startLocation;
private final double startHealth;
@@ -25,14 +23,13 @@ public class TimedTeleport extends Teleport {
@NotNull Type type, int warmupTime, boolean updateLastPosition,
@NotNull List<EconomyHook.Action> actions, @NotNull HuskHomes plugin) {
super(teleporter, executor, target, type, updateLastPosition, actions, plugin);
this.plugin = plugin;
this.startLocation = teleporter.getPosition();
this.startHealth = teleporter.getHealth();
this.timeLeft = Math.max(warmupTime, 0);
this.teleporter = teleporter;
}
@Override
@Override
public void execute() throws TeleportationException {
// Check if the teleporter can bypass warmup
if (timeLeft == 0 || teleporter.hasPermission(Permission.BYPASS_TELEPORT_WARMUP.node)) {
@@ -57,14 +54,9 @@ public class TimedTeleport extends Teleport {
this.process();
}
// Execute the warmup, fire the event, then execute the teleport if warmup completes normally
private void process() {
// Execute the warmup start event
plugin.getEventDispatcher().dispatchTeleportWarmupEvent(this, timeLeft).thenAccept(event -> {
if (event.isCancelled()) {
return;
}
// Mark the player as warming up and display the message
plugin.fireEvent(plugin.getTeleportWarmupEvent(this, timeLeft), (event) -> {
plugin.getCache().getCurrentlyOnWarmup().add(teleporter.getUuid());
plugin.getLocales().getLocale("teleporting_warmup_start", Integer.toString(timeLeft))
.ifPresent(teleporter::sendMessage);
@@ -151,8 +143,8 @@ public class TimedTeleport extends Teleport {
private boolean hasTeleporterMoved() {
final double maxMovementDistance = 0.1d;
double movementDistance = Math.abs(startLocation.getX() - teleporter.getPosition().getX()) +
Math.abs(startLocation.getY() - teleporter.getPosition().getY()) +
Math.abs(startLocation.getZ() - teleporter.getPosition().getZ());
Math.abs(startLocation.getY() - teleporter.getPosition().getY()) +
Math.abs(startLocation.getZ() - teleporter.getPosition().getZ());
return movementDistance > maxMovementDistance;
}