13
0

Player Seat Listener
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
zOnlyKroks 2022-03-24 14:57:03 +01:00
Ursprung 38bea9af63
Commit a93fe5797d

Datei anzeigen

@ -1,8 +1,26 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2021 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.lobby.listener; package de.steamwar.lobby.listener;
import org.bukkit.GameMode; import de.steamwar.lobby.LobbySystem;
import org.bukkit.Location; import org.bukkit.*;
import org.bukkit.Material;
import org.bukkit.block.data.Bisected; import org.bukkit.block.data.Bisected;
import org.bukkit.block.data.type.Stairs; import org.bukkit.block.data.type.Stairs;
import org.bukkit.entity.AbstractArrow; import org.bukkit.entity.AbstractArrow;
@ -12,45 +30,29 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.spigotmc.event.entity.EntityDismountEvent; import org.spigotmc.event.entity.EntityDismountEvent;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
public class PlayerSeatListener extends BasicListener{ public class PlayerSeatListener extends BasicListener{
private static class SeatLocation { public static final World world = Bukkit.getWorlds().get(0);
private int x; private Set<Location> seats = new HashSet<>();
private int y;
private int z;
public SeatLocation(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
static {
new BukkitRunnable() {
@Override @Override
public boolean equals(Object o) { public void run() {
if (this == o) return true; for(Arrow arrow : world.getEntitiesByClass(Arrow.class)) {
if (!(o instanceof SeatLocation)) return false; arrow.setTicksLived(1);
SeatLocation that = (SeatLocation) o;
return x == that.x &&
y == that.y &&
z == that.z;
} }
@Override
public int hashCode() {
return Objects.hash(x, y, z);
} }
}.runTaskTimer(LobbySystem.getPlugin(), 20*60,20*60);
} }
private Set<SeatLocation> seats = new HashSet<>();
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) if (event.getAction() != Action.RIGHT_CLICK_BLOCK)
@ -75,7 +77,7 @@ public class PlayerSeatListener extends BasicListener{
return; return;
Location location = event.getClickedBlock().getLocation(); Location location = event.getClickedBlock().getLocation();
SeatLocation seatLocation = getSeatLocation(location); Location seatLocation = getSeatLocation(location);
if (seats.contains(seatLocation)) if (seats.contains(seatLocation))
return; return;
seats.add(seatLocation); seats.add(seatLocation);
@ -91,7 +93,7 @@ public class PlayerSeatListener extends BasicListener{
public void onEntityDismount(EntityDismountEvent event) { public void onEntityDismount(EntityDismountEvent event) {
seats.remove(getSeatLocation(event.getDismounted().getLocation())); seats.remove(getSeatLocation(event.getDismounted().getLocation()));
if (event.getEntityType() != EntityType.PLAYER) if (event.getEntityType() != EntityType.PLAYER && event.getDismounted().getType() != EntityType.ARROW)
return; return;
event.getDismounted().remove(); event.getDismounted().remove();
@ -99,12 +101,12 @@ public class PlayerSeatListener extends BasicListener{
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
if (event.getPlayer().isInsideVehicle()) if (event.getPlayer().isInsideVehicle() && event.getPlayer().getVehicle().getType() == EntityType.ARROW)
event.getPlayer().getVehicle().remove(); event.getPlayer().getVehicle().remove();
} }
public SeatLocation getSeatLocation(Location location) { public Location getSeatLocation(Location location) {
return new SeatLocation(location.getBlockX(), location.getBlockY(), location.getBlockZ()); return new Location(world,location.getBlockX(), location.getBlockY(), location.getBlockZ());
} }
} }