Skip to content

Commit

Permalink
feat: add passenger and vehicle retainment options
Browse files Browse the repository at this point in the history
  • Loading branch information
Thatsmusic99 committed Jul 2, 2023
1 parent 62cc70b commit edf32de
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 16 deletions.
Expand Up @@ -21,11 +21,14 @@
import io.github.niestrat99.advancedteleport.sql.PlayerSQLManager;
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import io.papermc.lib.PaperLib;
import io.papermc.paper.entity.TeleportFlag;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Animals;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.permissions.PermissionAttachmentInfo;
Expand Down Expand Up @@ -165,7 +168,9 @@ public void teleport(
);
} else {
ParticleManager.onTeleport(player, command);
PaperLib.teleportAsync(player, event.getToLocation(), PlayerTeleportEvent.TeleportCause.COMMAND).whenComplete((result, err) -> {

//
teleportWithOptions(player, event.getToLocation(), PlayerTeleportEvent.TeleportCause.COMMAND).whenComplete((result, err) -> {

// If we didn't succeed, let the player know.
if (!result) {
Expand All @@ -182,6 +187,48 @@ public void teleport(
});
}
}

@ApiStatus.Internal
public static CompletableFuture<Boolean> teleportWithOptions(
@NotNull Player player,
@NotNull Location location,
@NotNull PlayerTeleportEvent.TeleportCause cause
) {

// The ultimate gotcha card
try {

// Build base list
final @NotNull List<TeleportFlag> flags = new ArrayList<>();

// If we should retain passengers and are able to do so
if (MainConfig.get().RETAIN_PASSENGERS.get() && player.getPassengers().size() > 0) {
flags.add(TeleportFlag.EntityState.RETAIN_PASSENGERS);
}

// If we should retain vehicles and are able to do so
if (MainConfig.get().RETAIN_VEHICLES.get() && player.getVehicle() != null) {

// If it must be an animal, check first
if (MainConfig.get().RETAIN_LIVING_ONLY.get()) {
if (player.getVehicle() instanceof LivingEntity) {
flags.add(TeleportFlag.EntityState.RETAIN_VEHICLE);
}
} else {
flags.add(TeleportFlag.EntityState.RETAIN_VEHICLE);
}
}

//
if (flags.size() > 0) {
return CompletableFuture.completedFuture(player.teleport(location, cause, flags.toArray(new TeleportFlag[0])));
}

} catch (NoSuchMethodError | NoClassDefFoundError ignored) {
}

return PaperLib.teleportAsync(player, location, cause);
}

/**
* Returns whether teleportation is enabled for the player. This allows the player to receive teleportation requests
Expand Down
Expand Up @@ -39,7 +39,7 @@ public boolean onCommand(
// If the home has been set with the specific name, use that
Home home = homesOther.get(args[1]);
if (home != null) {
PaperLib.teleportAsync(player, home.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
ATPlayer.teleportWithOptions(player, home.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
CustomMessages.sendMessage(sender, "Teleport.teleportingToHomeOther",
Placeholder.unparsed("player", args[0]),
Placeholder.unparsed("home", args[1])
Expand All @@ -51,7 +51,7 @@ public boolean onCommand(
if (args[1].equalsIgnoreCase("bed") && MainConfig.get().ADD_BED_TO_HOMES.get()) {
home = target.getBedSpawn();
if (home != null) {
PaperLib.teleportAsync(player, home.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
ATPlayer.teleportWithOptions(player, home.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
CustomMessages.sendMessage(sender, "Teleport.teleportingToHomeOther",
Placeholder.unparsed("player", args[0]),
Placeholder.unparsed("home", args[1])
Expand Down
@@ -1,12 +1,12 @@
package io.github.niestrat99.advancedteleport.commands.teleport;

import io.github.niestrat99.advancedteleport.CoreClass;
import io.github.niestrat99.advancedteleport.api.ATPlayer;
import io.github.niestrat99.advancedteleport.api.events.ATTeleportEvent;
import io.github.niestrat99.advancedteleport.commands.PlayerCommand;
import io.github.niestrat99.advancedteleport.commands.TeleportATCommand;
import io.github.niestrat99.advancedteleport.config.CustomMessages;
import io.github.niestrat99.advancedteleport.utilities.ConditionChecker;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -126,7 +126,7 @@ public boolean onCommand(
if (allowFlight && target.getAllowFlight() && target.hasPermission("at.admin.tploc.safe-teleport") && blockBelow.getBlock().getType() == Material.AIR) {
target.setFlying(true);
}
PaperLib.teleportAsync(target, location, PlayerTeleportEvent.TeleportCause.COMMAND);
ATPlayer.teleportWithOptions(target, location, PlayerTeleportEvent.TeleportCause.COMMAND);
if (player != target) {
CustomMessages.sendMessage(player, "Info.teleportedToLocOther",
Placeholder.unparsed("x", String.valueOf(loc[0])),
Expand Down
Expand Up @@ -6,7 +6,6 @@
import io.github.niestrat99.advancedteleport.commands.TeleportATCommand;
import io.github.niestrat99.advancedteleport.config.CustomMessages;
import io.github.niestrat99.advancedteleport.config.MainConfig;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -45,7 +44,7 @@ public boolean onCommand(
CustomMessages.sendMessage(sender, "Error.noSuchPlayer");
} else {
CustomMessages.sendMessage(sender, "Teleport.teleporting", Placeholder.unparsed("player", target.getName()));
PaperLib.teleportAsync(player, target.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
ATPlayer.teleportWithOptions(player, target.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
}
return true;
}
Expand Down
Expand Up @@ -6,7 +6,6 @@
import io.github.niestrat99.advancedteleport.commands.TeleportATCommand;
import io.github.niestrat99.advancedteleport.config.CustomMessages;
import io.github.niestrat99.advancedteleport.config.MainConfig;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
Expand Down Expand Up @@ -48,7 +47,7 @@ public boolean onCommand(
Placeholder.unparsed("player", target.getName()));
CustomMessages.sendMessage(target, "Teleport.teleportingSelfToPlayer",
Placeholder.unparsed("player", sender.getName()));
PaperLib.teleportAsync(target, player.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
ATPlayer.teleportWithOptions(target, player.getLocation(), PlayerTeleportEvent.TeleportCause.COMMAND);
return true;
}

Expand Down
Expand Up @@ -107,6 +107,9 @@ public final class MainConfig extends ATConfig {
public ConfigOption<Boolean> DEBUG;
public ConfigOption<Boolean> USE_FLOODGATE_FORMS;
public ConfigOption<Boolean> SEND_ACTIONBAR_TO_CONSOLE;
public ConfigOption<Boolean> RETAIN_PASSENGERS;
public ConfigOption<Boolean> RETAIN_VEHICLES;
public ConfigOption<Boolean> RETAIN_LIVING_ONLY;

/**
*
Expand Down Expand Up @@ -573,13 +576,22 @@ It is recommend to avoid setting this option too high as this can have a worst c
addDefault("check-for-updates", true, "Whether or not the plugin should check for updates.");
addDefault("notify-admins-on-update", true, "Whether or not to notify admins when an update is available.\n" +
"Anyone with the permission at.admin.notify will receive this notification.");
addDefault("debug", false, "Used for debugging purposes.");
addDefault("debug", false, "Used for debugging purposes.", "Misceallaneous");
addDefault("use-floodgate-forms", true, """
Whether to use Cumulus forms for Bedrock players.
These work by having a Bedrock player type in the command itself (such as /warp, /tpa, /setwarp), then fill in the rest of the commands through a form.
This only works when Geyser and Floodgate are used on the server. This improves accessibility for mobile or console players.""");
addDefault("send-actionbar-to-console", true, "If you are just using action bars for messages and have empty base messages, the console will not receive them." +
"\nIf you have this option set to true, then the console will receive the message that the action bar uses.");
addDefault("retain-passengers", false, """
Keeps any entities riding teleporting players on their heads.
Only available to newer versions of Paper and uses experimental API - don't expect this to be set to true by default for a while!
Teleportation is also not async if this has to be used.""");
addDefault("retain-vehicle", false, """
Keeps any entities being riden by teleporting players.
Only available to newer versions of Paper and uses experimental API - don't expect this to be set to true by default for a while!
Teleportation is also not async if this has to be used.""");
addDefault("retain-living-vehicles-only", true, "If it's not a minecart or boat, take it with us. Requires the above option to be set to true.");

}

Expand Down Expand Up @@ -816,6 +828,9 @@ public void postSave() {
DEBUG = new ConfigOption<>("debug");
USE_FLOODGATE_FORMS = new ConfigOption<>("use-floodgate-forms");
SEND_ACTIONBAR_TO_CONSOLE = new ConfigOption<>("send-actionbar-to-console");
RETAIN_PASSENGERS = new ConfigOption<>("retain-passengers");
RETAIN_VEHICLES = new ConfigOption<>("retain-vehicle");
RETAIN_LIVING_ONLY = new ConfigOption<>("retain-living-vehicles-only");

new PaymentManager();
LimitationsManager.init();
Expand Down
@@ -1,10 +1,10 @@
package io.github.niestrat99.advancedteleport.managers;

import io.github.niestrat99.advancedteleport.CoreClass;
import io.github.niestrat99.advancedteleport.api.ATPlayer;
import io.github.niestrat99.advancedteleport.config.CustomMessages;
import io.github.niestrat99.advancedteleport.config.MainConfig;
import io.github.niestrat99.advancedteleport.payments.PaymentManager;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.bukkit.Location;
Expand Down Expand Up @@ -89,7 +89,7 @@ public void run() {
// If the player can't pay for the
if (!PaymentManager.getInstance().canPay(command, payingPlayer)) return;
ParticleManager.onTeleport(teleportingPlayer, command);
PaperLib.teleportAsync(teleportingPlayer, location, PlayerTeleportEvent.TeleportCause.COMMAND);
ATPlayer.teleportWithOptions(teleportingPlayer, location, PlayerTeleportEvent.TeleportCause.COMMAND);
movement.remove(uuid);
CustomMessages.sendMessage(teleportingPlayer, message, placeholders);
PaymentManager.getInstance().withdraw(command, payingPlayer);
Expand Down
Expand Up @@ -10,7 +10,6 @@
import io.github.niestrat99.advancedteleport.config.MainConfig;
import io.github.niestrat99.advancedteleport.utilities.ConditionChecker;
import io.github.thatsmusic99.configurationmaster.api.ConfigSection;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -61,7 +60,7 @@ public void onJoin(PlayerJoinEvent e) {

private void spawn(Player player, Spawn spawn) {
Bukkit.getScheduler().runTaskLater(CoreClass.getInstance(), () ->
PaperLib.teleportAsync(player, spawn.getLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN)
ATPlayer.teleportWithOptions(player, spawn.getLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN)
.whenComplete((result, err) -> {
if (!result)
CoreClass.getInstance().getLogger().warning("Failed to teleport " + player.getName() + " on joining.");
Expand Down
@@ -1,5 +1,6 @@
package io.github.niestrat99.advancedteleport.utilities;

import io.github.niestrat99.advancedteleport.api.ATPlayer;
import io.github.niestrat99.advancedteleport.api.TeleportRequest;
import io.github.niestrat99.advancedteleport.api.TeleportRequestType;
import io.github.niestrat99.advancedteleport.api.events.ATTeleportEvent;
Expand All @@ -8,7 +9,6 @@
import io.github.niestrat99.advancedteleport.managers.CooldownManager;
import io.github.niestrat99.advancedteleport.managers.MovementManager;
import io.github.niestrat99.advancedteleport.payments.PaymentManager;
import io.papermc.lib.PaperLib;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import org.bukkit.Bukkit;
import org.bukkit.Location;
Expand Down Expand Up @@ -48,7 +48,7 @@ private static void teleport(
MovementManager.createMovementTimer(fromPlayer, toLocation, type, "Teleport.eventTeleport", warmUp, payingPlayer);
return;
}
PaperLib.teleportAsync(fromPlayer, toLocation, PlayerTeleportEvent.TeleportCause.COMMAND);
ATPlayer.teleportWithOptions(fromPlayer, toLocation, PlayerTeleportEvent.TeleportCause.COMMAND);
CustomMessages.sendMessage(fromPlayer, "Teleport.eventTeleport");
PaymentManager.getInstance().withdraw(type, payingPlayer);

Expand Down

0 comments on commit edf32de

Please sign in to comment.