Skip to content

Commit d79fcaa

Browse files
authored
refactor: Simplify teleport execution logic (#565)
* refactor: simplify teleport execution logic * docs: document better way of executing a teleport * fix: TimedTeleport should still handle internal errors * fix: Wrong builder method calls * fix: Typos * fix: More typos * fix: Cancel timed teleport on failed execute
1 parent 1723aa4 commit d79fcaa

17 files changed

+160
-210
lines changed

common/src/main/java/net/william278/huskhomes/api/BaseHuskHomesAPI.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import net.william278.huskhomes.random.RandomTeleportEngine;
3030
import net.william278.huskhomes.teleport.Teleport;
3131
import net.william278.huskhomes.teleport.TeleportBuilder;
32-
import net.william278.huskhomes.teleport.TeleportationException;
3332
import net.william278.huskhomes.user.OnlineUser;
3433
import net.william278.huskhomes.user.SavedUser;
3534
import net.william278.huskhomes.user.User;
@@ -773,18 +772,10 @@ public final void randomlyTeleportPlayer(@NotNull OnlineUser user, boolean timed
773772
throw new IllegalStateException("Random teleport engine returned an empty position");
774773
}
775774

776-
final TeleportBuilder builder = Teleport.builder(plugin)
775+
Teleport.builder(plugin)
777776
.teleporter(user)
778-
.target(position.get());
779-
try {
780-
if (timedTeleport) {
781-
builder.toTimedTeleport().execute();
782-
} else {
783-
builder.toTeleport().execute();
784-
}
785-
} catch (TeleportationException e) {
786-
e.displayMessage(user, rtpArgs);
787-
}
777+
.target(position.get())
778+
.buildAndComplete(timedTeleport);
788779
}).exceptionally(e -> {
789780
throw new IllegalStateException("Random teleport engine threw an exception", e);
790781
});

common/src/main/java/net/william278/huskhomes/command/BackCommand.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import net.william278.huskhomes.HuskHomes;
2323
import net.william278.huskhomes.position.Position;
2424
import net.william278.huskhomes.teleport.Teleport;
25-
import net.william278.huskhomes.teleport.TeleportationException;
2625
import net.william278.huskhomes.user.OnlineUser;
2726
import net.william278.huskhomes.util.TransactionResolver;
2827
import org.jetbrains.annotations.NotNull;
@@ -50,17 +49,12 @@ public void execute(@NotNull OnlineUser executor, @NotNull String[] args) {
5049
return;
5150
}
5251

53-
try {
54-
Teleport.builder(plugin)
55-
.teleporter(executor)
56-
.target(lastPosition.get())
57-
.actions(TransactionResolver.Action.BACK_COMMAND)
58-
.type(Teleport.Type.BACK)
59-
.toTimedTeleport()
60-
.execute();
61-
} catch (TeleportationException e) {
62-
e.displayMessage(executor, args);
63-
}
52+
Teleport.builder(plugin)
53+
.teleporter(executor)
54+
.target(lastPosition.get())
55+
.actions(TransactionResolver.Action.BACK_COMMAND)
56+
.type(Teleport.Type.BACK)
57+
.buildAndComplete(true);
6458
}
6559

6660
}

common/src/main/java/net/william278/huskhomes/command/RtpCommand.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import net.william278.huskhomes.position.World;
2424
import net.william278.huskhomes.teleport.Teleport;
2525
import net.william278.huskhomes.teleport.TeleportBuilder;
26-
import net.william278.huskhomes.teleport.TeleportationException;
2726
import net.william278.huskhomes.user.CommandUser;
2827
import net.william278.huskhomes.user.OnlineUser;
2928
import net.william278.huskhomes.util.TransactionResolver;
@@ -154,15 +153,7 @@ private void executeRtp(@NotNull OnlineUser teleporter, @NotNull CommandUser exe
154153
.teleporter(teleporter)
155154
.actions(TransactionResolver.Action.RANDOM_TELEPORT)
156155
.target(position.get());
157-
try {
158-
if (executor.equals(teleporter)) {
159-
builder.toTimedTeleport().execute();
160-
} else {
161-
builder.toTeleport().execute();
162-
}
163-
} catch (TeleportationException e) {
164-
e.displayMessage(executor, args);
165-
}
156+
builder.buildAndComplete(executor.equals(teleporter), args);
166157
});
167158
}
168159

common/src/main/java/net/william278/huskhomes/command/SavedPositionCommand.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import net.william278.huskhomes.position.SavedPosition;
2525
import net.william278.huskhomes.position.Warp;
2626
import net.william278.huskhomes.teleport.Teleport;
27-
import net.william278.huskhomes.teleport.TeleportBuilder;
2827
import net.william278.huskhomes.teleport.Teleportable;
29-
import net.william278.huskhomes.teleport.TeleportationException;
3028
import net.william278.huskhomes.user.CommandUser;
3129
import net.william278.huskhomes.user.OnlineUser;
3230
import net.william278.huskhomes.user.User;
@@ -166,19 +164,11 @@ protected void teleport(@NotNull CommandUser executor, @NotNull Teleportable tel
166164
return;
167165
}
168166

169-
final TeleportBuilder builder = Teleport.builder(plugin)
167+
Teleport.builder(plugin)
170168
.teleporter(teleporter)
171169
.actions(actions)
172-
.target(position);
173-
try {
174-
if (executor.equals(teleporter)) {
175-
builder.toTimedTeleport().execute();
176-
} else {
177-
builder.toTeleport().execute();
178-
}
179-
} catch (TeleportationException e) {
180-
e.displayMessage(executor, teleporter.getUsername());
181-
}
170+
.target(position)
171+
.buildAndComplete(executor.equals(teleporter), teleporter.getUsername());
182172
}
183173

184174
@NotNull

common/src/main/java/net/william278/huskhomes/command/SpawnCommand.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import net.william278.huskhomes.HuskHomes;
2323
import net.william278.huskhomes.position.Position;
2424
import net.william278.huskhomes.teleport.Teleport;
25-
import net.william278.huskhomes.teleport.TeleportBuilder;
2625
import net.william278.huskhomes.teleport.Teleportable;
27-
import net.william278.huskhomes.teleport.TeleportationException;
2826
import net.william278.huskhomes.user.CommandUser;
2927
import net.william278.huskhomes.util.TransactionResolver;
3028
import org.jetbrains.annotations.NotNull;
@@ -67,19 +65,11 @@ public void teleportToSpawn(@NotNull Teleportable teleporter, @NotNull CommandUs
6765
return;
6866
}
6967

70-
final TeleportBuilder builder = Teleport.builder(plugin)
68+
Teleport.builder(plugin)
7169
.teleporter(teleporter)
7270
.actions(TransactionResolver.Action.SPAWN_TELEPORT)
73-
.target(spawn);
74-
try {
75-
if (teleporter.equals(executor)) {
76-
builder.toTimedTeleport().execute();
77-
} else {
78-
builder.toTeleport().execute();
79-
}
80-
} catch (TeleportationException e) {
81-
e.displayMessage(executor, args);
82-
}
71+
.target(spawn)
72+
.buildAndComplete(teleporter.equals(executor), args);
8373
}
8474

8575
}

common/src/main/java/net/william278/huskhomes/command/TpCommand.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,10 @@ private void execute(@NotNull CommandUser executor, @NotNull Teleportable telepo
8888
final TeleportBuilder builder = Teleport.builder(plugin)
8989
.teleporter(teleportable)
9090
.target(target);
91-
try {
92-
if (executor instanceof OnlineUser user) {
93-
builder.executor(user);
94-
}
95-
builder.toTeleport().execute();
96-
} catch (TeleportationException e) {
97-
e.displayMessage(executor, args);
98-
return;
91+
if (executor instanceof OnlineUser user) {
92+
builder.executor(user);
9993
}
94+
builder.buildAndComplete(false, args);
10095

10196
// Display a teleport completion message
10297
final String teleporterName = teleportable instanceof OnlineUser user

common/src/main/java/net/william278/huskhomes/command/TpHereCommand.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import net.william278.huskhomes.HuskHomes;
2323
import net.william278.huskhomes.teleport.Teleport;
24-
import net.william278.huskhomes.teleport.TeleportationException;
2524
import net.william278.huskhomes.user.OnlineUser;
2625
import org.jetbrains.annotations.NotNull;
2726

@@ -44,18 +43,15 @@ public void execute(@NotNull OnlineUser executor, @NotNull String[] args) {
4443
return;
4544
}
4645

47-
try {
48-
Teleport.builder(plugin)
49-
.executor(executor)
50-
.teleporter(optionalTarget.get())
51-
.target(executor.getPosition())
52-
.toTeleport().execute();
53-
54-
plugin.getLocales().getLocale("teleporting_other_complete",
55-
optionalTarget.get(), executor.getUsername());
56-
} catch (TeleportationException e) {
57-
e.displayMessage(executor, args);
58-
}
46+
// Teleport the user
47+
final String target = optionalTarget.get();
48+
Teleport.builder(plugin)
49+
.executor(executor)
50+
.teleporter(target)
51+
.target(executor.getPosition())
52+
.buildAndComplete(false, target);
53+
54+
plugin.getLocales().getLocale("teleporting_other_complete", target, executor.getUsername());
5955
}
6056

6157
}

common/src/main/java/net/william278/huskhomes/command/TpOfflineCommand.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import net.william278.huskhomes.HuskHomes;
2323
import net.william278.huskhomes.position.Position;
2424
import net.william278.huskhomes.teleport.Teleport;
25-
import net.william278.huskhomes.teleport.TeleportationException;
2625
import net.william278.huskhomes.user.OnlineUser;
2726
import net.william278.huskhomes.user.SavedUser;
2827
import net.william278.huskhomes.user.User;
@@ -69,14 +68,10 @@ private void teleportToOfflinePosition(@NotNull OnlineUser user, @NotNull User t
6968

7069
plugin.getLocales().getLocale("teleporting_offline_player", target.getUsername())
7170
.ifPresent(user::sendMessage);
72-
try {
73-
Teleport.builder(plugin)
74-
.teleporter(user)
75-
.target(position.get())
76-
.toTeleport().execute();
77-
} catch (TeleportationException e) {
78-
e.displayMessage(user, args);
79-
}
71+
Teleport.builder(plugin)
72+
.teleporter(user)
73+
.target(position.get())
74+
.buildAndComplete(false, args);
8075
}
8176

8277
}

common/src/main/java/net/william278/huskhomes/listener/EventListener.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected final void handlePlayerJoin(@NotNull OnlineUser onlineUser) {
5959
plugin.runAsync(() -> {
6060
// Ensure the user is in the database
6161
plugin.getDatabase().ensureUser(onlineUser);
62+
plugin.getCurrentlyOnWarmup().remove(onlineUser.getUuid());
6263

6364
// Handle cross-server checks
6465
if (plugin.getSettings().getCrossServer().isEnabled()) {
@@ -161,17 +162,11 @@ private void handleInboundRespawn(@NotNull OnlineUser teleporter) {
161162
plugin.getSpawn().ifPresent(spawn -> {
162163
if (plugin.getSettings().getCrossServer().isEnabled()
163164
&& !spawn.getServer().equals(plugin.getServerName())) {
164-
plugin.runSyncDelayed(() -> {
165-
try {
166-
Teleport.builder(plugin)
167-
.teleporter(teleporter)
168-
.target(spawn)
169-
.updateLastPosition(false)
170-
.toTeleport().execute();
171-
} catch (TeleportationException e) {
172-
e.displayMessage(teleporter);
173-
}
174-
}, 40L);
165+
plugin.runSyncDelayed(() -> Teleport.builder(plugin)
166+
.teleporter(teleporter)
167+
.target(spawn)
168+
.updateLastPosition(false)
169+
.buildAndComplete(false), 40L);
175170
} else {
176171
try {
177172
teleporter.teleportLocally(spawn, plugin.getSettings().getGeneral().isTeleportAsync());

common/src/main/java/net/william278/huskhomes/manager/RequestsManager.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import net.william278.huskhomes.teleport.Teleport;
2727
import net.william278.huskhomes.teleport.TeleportBuilder;
2828
import net.william278.huskhomes.teleport.TeleportRequest;
29-
import net.william278.huskhomes.teleport.TeleportationException;
3029
import net.william278.huskhomes.user.OnlineUser;
3130
import net.william278.huskhomes.user.SavedUser;
3231
import net.william278.huskhomes.user.User;
@@ -323,12 +322,7 @@ private void handleRequestResponse(@NotNull TeleportRequest request, @NotNull On
323322
} else {
324323
builder.target(request.getRequesterName());
325324
}
326-
327-
try {
328-
builder.toTimedTeleport().execute();
329-
} catch (TeleportationException e) {
330-
e.displayMessage(recipient);
331-
}
325+
builder.buildAndComplete(true);
332326
}
333327
}));
334328
}
@@ -354,16 +348,11 @@ public void handleLocalRequestResponse(@NotNull OnlineUser requester, @NotNull T
354348

355349
// If the request is a tpa request, teleport the requester to the recipient
356350
if (accepted && (request.getType() == TeleportRequest.Type.TPA)) {
357-
try {
358-
Teleport.builder(plugin)
359-
.teleporter(requester)
360-
.target(request.getRecipientName())
361-
.actions(TransactionResolver.Action.ACCEPT_TELEPORT_REQUEST)
362-
.toTimedTeleport()
363-
.execute();
364-
} catch (TeleportationException e) {
365-
e.displayMessage(requester);
366-
}
351+
Teleport.builder(plugin)
352+
.teleporter(requester)
353+
.target(request.getRecipientName())
354+
.actions(TransactionResolver.Action.ACCEPT_TELEPORT_REQUEST)
355+
.buildAndComplete(true);
367356
}
368357
}
369358

common/src/main/java/net/william278/huskhomes/network/Broker.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import net.william278.huskhomes.position.Home;
2525
import net.william278.huskhomes.position.Warp;
2626
import net.william278.huskhomes.teleport.Teleport;
27-
import net.william278.huskhomes.teleport.TeleportationException;
2827
import net.william278.huskhomes.user.OnlineUser;
2928
import org.jetbrains.annotations.NotNull;
3029

@@ -58,17 +57,11 @@ protected void handle(@NotNull OnlineUser receiver, @NotNull Message message) {
5857
}
5958
switch (message.getType()) {
6059
case TELEPORT_TO_POSITION -> message.getPayload()
61-
.getPosition().ifPresent(position -> {
62-
try {
63-
Teleport.builder(plugin)
64-
.teleporter(receiver)
65-
.target(position)
66-
.toTeleport()
67-
.execute();
68-
} catch (TeleportationException e) {
69-
e.displayMessage(plugin.getConsole());
70-
}
71-
});
60+
.getPosition().ifPresent(position -> Teleport.builder(plugin)
61+
.teleporter(receiver)
62+
.target(position)
63+
.toTeleport()
64+
.complete());
7265
case TELEPORT_TO_NETWORKED_POSITION -> Message.builder()
7366
.type(Message.Type.TELEPORT_TO_POSITION)
7467
.target(message.getSender())
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of HuskHomes, licensed under the Apache License 2.0.
3+
*
4+
* Copyright (c) William278 <will27528@gmail.com>
5+
* Copyright (c) contributors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package net.william278.huskhomes.teleport;
21+
22+
import net.william278.huskhomes.user.OnlineUser;
23+
import org.jetbrains.annotations.NotNull;
24+
25+
/**
26+
* Represents a Teleport that is completable
27+
*/
28+
public interface Completable {
29+
30+
void execute() throws TeleportationException;
31+
32+
@NotNull
33+
OnlineUser getExecutor();
34+
35+
/**
36+
* Complete the teleport and handle exceptions
37+
*
38+
* @param args Optional args to pass to the teleport exception handler
39+
*/
40+
default void complete(@NotNull String... args) {
41+
try {
42+
execute();
43+
} catch (TeleportationException e) {
44+
e.displayMessage(getExecutor(), args);
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)