2014-08-12 02:51:00 +02:00
|
|
|
/*
|
|
|
|
* WorldGuard, a suite of tools for Minecraft
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldGuard team and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldguard.bukkit.listener;
|
|
|
|
|
2018-07-21 18:03:32 +02:00
|
|
|
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
2019-04-18 03:24:10 +02:00
|
|
|
import com.sk89q.worldedit.util.Location;
|
2018-07-22 05:06:01 +02:00
|
|
|
import com.sk89q.worldguard.LocalPlayer;
|
2018-07-21 18:03:32 +02:00
|
|
|
import com.sk89q.worldguard.WorldGuard;
|
2014-08-12 02:51:00 +02:00
|
|
|
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
2018-12-26 06:19:23 +01:00
|
|
|
import com.sk89q.worldguard.config.WorldConfiguration;
|
2015-01-18 03:54:10 +01:00
|
|
|
import com.sk89q.worldguard.session.MoveType;
|
2018-12-26 06:19:23 +01:00
|
|
|
import com.sk89q.worldguard.util.Locations;
|
2014-08-12 02:51:00 +02:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.entity.Vehicle;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.vehicle.VehicleMoveEvent;
|
2015-01-18 03:54:10 +01:00
|
|
|
import org.bukkit.util.Vector;
|
2014-08-12 02:51:00 +02:00
|
|
|
|
2018-07-22 05:06:01 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2021-08-09 04:58:22 +02:00
|
|
|
public class WorldGuardVehicleListener extends AbstractListener {
|
2014-08-12 02:51:00 +02:00
|
|
|
|
|
|
|
public WorldGuardVehicleListener(WorldGuardPlugin plugin) {
|
2021-08-09 04:58:22 +02:00
|
|
|
super(plugin);
|
2014-08-12 02:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onVehicleMove(VehicleMoveEvent event) {
|
|
|
|
Vehicle vehicle = event.getVehicle();
|
2018-07-22 05:06:01 +02:00
|
|
|
if (vehicle.getPassengers().isEmpty()) return;
|
2019-04-18 03:24:10 +02:00
|
|
|
List<Player> playerPassengers = vehicle.getPassengers().stream()
|
|
|
|
.filter(ent -> ent instanceof Player).map(ent -> (Player) ent).collect(Collectors.toList());
|
2018-07-22 05:06:01 +02:00
|
|
|
if (playerPassengers.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-12 02:51:00 +02:00
|
|
|
World world = vehicle.getWorld();
|
2021-08-09 04:58:22 +02:00
|
|
|
WorldConfiguration wcfg = getWorldConfig(world);
|
2014-08-12 02:51:00 +02:00
|
|
|
|
|
|
|
if (wcfg.useRegions) {
|
|
|
|
// Did we move a block?
|
2018-07-22 05:06:01 +02:00
|
|
|
if (Locations.isDifferentBlock(BukkitAdapter.adapt(event.getFrom()), BukkitAdapter.adapt(event.getTo()))) {
|
2019-04-18 03:24:10 +02:00
|
|
|
for (Player player : playerPassengers) {
|
2021-08-09 04:58:22 +02:00
|
|
|
LocalPlayer localPlayer = getPlugin().wrapPlayer(player);
|
2019-04-18 03:24:10 +02:00
|
|
|
Location lastValid;
|
|
|
|
if ((lastValid = WorldGuard.getInstance().getPlatform().getSessionManager().get(localPlayer)
|
|
|
|
.testMoveTo(localPlayer, BukkitAdapter.adapt(event.getTo()), MoveType.RIDE)) != null) {
|
2018-07-22 05:06:01 +02:00
|
|
|
vehicle.setVelocity(new Vector(0, 0, 0));
|
|
|
|
vehicle.teleport(event.getFrom());
|
2019-04-18 03:24:10 +02:00
|
|
|
if (Locations.isDifferentBlock(lastValid, BukkitAdapter.adapt(event.getFrom()))) {
|
|
|
|
Vector dir = player.getLocation().getDirection();
|
|
|
|
player.teleport(BukkitAdapter.adapt(lastValid).setDirection(dir));
|
|
|
|
}
|
2018-07-22 05:06:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-08-12 02:51:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|