175 lines
6.1 KiB
Java
175 lines
6.1 KiB
Java
/*
|
|
* Copyright (c) 2017 univento.eu - All rights reserved
|
|
* You are not allowed to use, distribute or modify this code
|
|
*/
|
|
|
|
package eu.univento.core.listeners;
|
|
|
|
import eu.univento.core.api.player.CustomPlayer;
|
|
import eu.univento.core.api.player.SpectateManager;
|
|
import eu.univento.core.api.server.ServerSettings;
|
|
import eu.univento.core.commands.ModCommands;
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.block.Block;
|
|
import org.bukkit.block.BlockFace;
|
|
import org.bukkit.entity.*;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.entity.EntityTargetEvent;
|
|
import org.bukkit.event.inventory.InventoryCloseEvent;
|
|
import org.bukkit.event.player.*;
|
|
import org.bukkit.event.server.ServerListPingEvent;
|
|
import org.bukkit.material.Stairs;
|
|
import org.bukkit.util.Vector;
|
|
|
|
import java.util.HashMap;
|
|
|
|
/**
|
|
* some basic events
|
|
*
|
|
* @author joethei
|
|
* @version 1.0
|
|
*/
|
|
|
|
public class Events implements Listener {
|
|
|
|
private HashMap<CustomPlayer, Location> seats = new HashMap<>();
|
|
private HashMap<CustomPlayer, ArmorStand> seatEntities = new HashMap<>();
|
|
|
|
@EventHandler
|
|
public void onServerListPing(ServerListPingEvent e) {
|
|
e.setMotd(ServerSettings.getServerType().name() + ";" + (ServerSettings.isGame() ? ServerSettings.getGameState() : ""));
|
|
}
|
|
|
|
@EventHandler
|
|
public void onInventoryClose(InventoryCloseEvent e) {
|
|
CustomPlayer p = CustomPlayer.getPlayer((Player) e.getPlayer());
|
|
p.setOpenInventory(false);
|
|
}
|
|
|
|
@EventHandler
|
|
public void onPlayerInteract(PlayerInteractEvent e) {
|
|
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
|
|
if(e.getClickedBlock() == null) return;
|
|
if(e.getClickedBlock().getRelative(BlockFace.DOWN).getType() != Material.ANVIL) return;
|
|
if(e.getClickedBlock().getState().getData() instanceof Stairs) {
|
|
loadSeat(p, e.getClickedBlock());
|
|
}
|
|
}
|
|
|
|
@EventHandler
|
|
public void onMove(PlayerMoveEvent e) {
|
|
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
|
|
if (p.getLocation().getY() <= 0) {
|
|
p.setVelocity(new Vector().setY(10.0D).multiply(1.0D));
|
|
p.setGliding(true);
|
|
}
|
|
if(e.getFrom().getY() != e.getTo().getY()) {
|
|
if(!p.isInsideVehicle() && seatEntities.containsKey(p)) {
|
|
if(seatEntities.get(p).isDead()) {
|
|
seatEntities.remove(p);
|
|
if(seats.get(p).getBlock().getState().getData() instanceof Stairs) loadSeat(p, seats.get(p).getBlock());
|
|
}else {
|
|
seatEntities.get(p).remove();
|
|
seatEntities.remove(p);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@EventHandler
|
|
public void onPlayerTeleport(PlayerTeleportEvent e) {
|
|
CustomPlayer p = CustomPlayer.getPlayer(e.getPlayer());
|
|
if(!p.isInsideVehicle() && seatEntities.containsKey(p)) {
|
|
seatEntities.get(p).remove();
|
|
seatEntities.remove(p);
|
|
seats.remove(p);
|
|
}
|
|
}
|
|
|
|
@EventHandler
|
|
public void onQuit(PlayerQuitEvent e) {
|
|
despawnSeat(CustomPlayer.getPlayer(e.getPlayer()));
|
|
}
|
|
|
|
@EventHandler
|
|
public void onKick(PlayerKickEvent e) {
|
|
despawnSeat(CustomPlayer.getPlayer(e.getPlayer()));
|
|
}
|
|
|
|
@EventHandler
|
|
public void onEntityTarget(EntityTargetEvent e) {
|
|
if (e.isCancelled()) return;
|
|
final Entity entity = e.getEntity();
|
|
final Entity target = e.getTarget();
|
|
if (!(target instanceof Player)) return;
|
|
CustomPlayer p = CustomPlayer.getPlayer((Player) target);
|
|
if (ModCommands.getVanishedPlayers().contains(p) || SpectateManager.contains(p)) {
|
|
if (entity instanceof Tameable) {
|
|
e.setTarget(null);
|
|
}
|
|
|
|
if (entity instanceof ExperienceOrb) {
|
|
repellExpOrb((Player) target, (ExperienceOrb) entity);
|
|
e.setCancelled(true);
|
|
e.setTarget(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void repellExpOrb(Player player, ExperienceOrb orb) {
|
|
final Location pLoc = player.getLocation();
|
|
final Location oLoc = orb.getLocation();
|
|
final Vector dir = oLoc.toVector().subtract(pLoc.toVector());
|
|
final double dx = Math.abs(dir.getX());
|
|
final double dz = Math.abs(dir.getZ());
|
|
if ((dx == 0.0) && (dz == 0.0)) {
|
|
dir.setX(0.001);
|
|
}
|
|
if ((dx < 3.0) && (dz < 3.0)) {
|
|
final Vector nDir = dir.normalize();
|
|
final Vector newV = nDir.clone().multiply(0.3);
|
|
newV.setY(0);
|
|
orb.setVelocity(newV);
|
|
if ((dx < 1.0) && (dz < 1.0)) {
|
|
orb.teleport(oLoc.clone().add(nDir.multiply(1.0)), PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
}
|
|
if ((dx < 0.5) && (dz < 0.5)) {
|
|
orb.remove();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void loadSeat(CustomPlayer p, Block b) {
|
|
if(seats.containsKey(p) || seats.containsValue(b.getLocation())) return;
|
|
Location location = b.getLocation();
|
|
Stairs stairs = (Stairs) b.getState().getData();
|
|
if(stairs.getFacing() == null) return;
|
|
location.setDirection(new Vector(stairs.getFacing().getModX(), stairs.getFacing().getModY(), stairs.getFacing().getModZ()));
|
|
location.setX(location.getBlockX() + 0.5);
|
|
location.setY(location.getY() + 0.3);
|
|
location.setZ(location.getBlockZ() + 0.5);
|
|
ArmorStand stand = p.getWorld().spawn(location, ArmorStand.class);
|
|
stand.setVisible(false);
|
|
stand.setMarker(true);
|
|
stand.setSmall(true);
|
|
stand.setBasePlate(false);
|
|
stand.setGravity(false);
|
|
stand.setPassenger(p);
|
|
seats.put(p, b.getLocation());
|
|
seatEntities.put(p, stand);
|
|
}
|
|
|
|
private void despawnSeat(CustomPlayer p) {
|
|
if (seats.containsKey(p) && seatEntities.containsKey(p)) {
|
|
seatEntities.get(p).eject();
|
|
if (seatEntities.get(p) != null) {
|
|
seatEntities.get(p).remove();
|
|
}
|
|
seatEntities.remove(p);
|
|
seats.remove(p);
|
|
}
|
|
}
|
|
|
|
} |