From 54f220c6ed4c17045e6fa4ad193e302a8676b611 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 26 Nov 2021 18:07:53 +0100 Subject: [PATCH] Add WarpListener Signed-off-by: yoyosource --- .../features/warp/WarpEntity_15.java | 59 ++++++++++ .../features/warp/WarpListener_15.java | 30 +++++ .../bausystem/shared/BaseArmorStand_15.java | 56 +++++++++ .../features/tracer/AbstractTraceEntity.java | 2 - .../features/warp/AbstractWarpEntity.java | 32 +++++ .../bausystem/features/warp/Warp.java | 1 - .../bausystem/features/warp/WarpListener.java | 111 ++++++++++++++++++ 7 files changed, 288 insertions(+), 3 deletions(-) create mode 100644 BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpEntity_15.java create mode 100644 BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpListener_15.java create mode 100644 BauSystem_15/src/de/steamwar/bausystem/shared/BaseArmorStand_15.java create mode 100644 BauSystem_API/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java diff --git a/BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpEntity_15.java b/BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpEntity_15.java new file mode 100644 index 00000000..6e455c79 --- /dev/null +++ b/BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpEntity_15.java @@ -0,0 +1,59 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.warp; + +import de.steamwar.bausystem.shared.BaseArmorStand_15; +import net.minecraft.server.v1_15_R1.ChatComponentText; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +public class WarpEntity_15 extends BaseArmorStand_15 implements AbstractWarpEntity { + + private String name; + + public WarpEntity_15(World world, Vector position, String name) { + super(world, position); + setInvisible(true); + setSmall(true); + this.name = name; + this.setNoGravity(true); + this.ticksLived = -12000; + } + + @Override + public void display(Player player) { + this.setCustomNameVisible(true); + this.setCustomName(new ChatComponentText(name)); + sendEntity(player); + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public boolean hide(Player player) { + sendEntityDestroy(player); + die(); + return true; + } +} diff --git a/BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpListener_15.java b/BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpListener_15.java new file mode 100644 index 00000000..6f98a453 --- /dev/null +++ b/BauSystem_15/src/de/steamwar/bausystem/features/warp/WarpListener_15.java @@ -0,0 +1,30 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.warp; + +import org.bukkit.World; +import org.bukkit.util.Vector; + +public class WarpListener_15 { + + public static AbstractWarpEntity create(World world, Vector position, String name) { + return new WarpEntity_15(world, position, name); + } +} diff --git a/BauSystem_15/src/de/steamwar/bausystem/shared/BaseArmorStand_15.java b/BauSystem_15/src/de/steamwar/bausystem/shared/BaseArmorStand_15.java new file mode 100644 index 00000000..751032ce --- /dev/null +++ b/BauSystem_15/src/de/steamwar/bausystem/shared/BaseArmorStand_15.java @@ -0,0 +1,56 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.shared; + +import net.minecraft.server.v1_15_R1.*; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +public class BaseArmorStand_15 extends EntityArmorStand implements AbstractEntity { + + private static final Vec3D ZERO = new Vec3D(0, 0, 0); + + protected Vector position; + + public BaseArmorStand_15(World world, Vector position) { + super(((CraftWorld) world).getHandle(), position.getX(), position.getY(), position.getZ()); + + this.position = position; + setNoGravity(true); + ticksLived = -12000; + } + + public void sendEntity(Player player) { + PacketPlayOutSpawnEntity packetPlayOutSpawnEntity = new PacketPlayOutSpawnEntity(getId(), getUniqueID(), position.getX(), position.getY(), position.getZ(), 0, 0, EntityTypes.ARMOR_STAND, 0, ZERO); + PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection; + playerConnection.sendPacket(packetPlayOutSpawnEntity); + + PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(getId(), datawatcher, true); + playerConnection.sendPacket(packetPlayOutEntityMetadata); + } + + public void sendEntityDestroy(Player player) { + PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(getId()); + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutEntityDestroy); + } +} diff --git a/BauSystem_API/src/de/steamwar/bausystem/features/tracer/AbstractTraceEntity.java b/BauSystem_API/src/de/steamwar/bausystem/features/tracer/AbstractTraceEntity.java index 63c6eb42..39ee7f5b 100644 --- a/BauSystem_API/src/de/steamwar/bausystem/features/tracer/AbstractTraceEntity.java +++ b/BauSystem_API/src/de/steamwar/bausystem/features/tracer/AbstractTraceEntity.java @@ -20,7 +20,6 @@ package de.steamwar.bausystem.features.tracer; import de.steamwar.bausystem.shared.AbstractEntity; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; public interface AbstractTraceEntity extends AbstractEntity { @@ -28,5 +27,4 @@ public interface AbstractTraceEntity extends AbstractEntity { void display(Player player, boolean exploded); boolean hide(Player player, boolean always); - } diff --git a/BauSystem_API/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java b/BauSystem_API/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java new file mode 100644 index 00000000..7cf33676 --- /dev/null +++ b/BauSystem_API/src/de/steamwar/bausystem/features/warp/AbstractWarpEntity.java @@ -0,0 +1,32 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.warp; + +import de.steamwar.bausystem.shared.AbstractEntity; +import org.bukkit.entity.Player; + +public interface AbstractWarpEntity extends AbstractEntity { + + void display(Player player); + + void setName(String name); + + boolean hide(Player player); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/Warp.java b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/Warp.java index c163c03a..2d8fface 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/Warp.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/Warp.java @@ -28,7 +28,6 @@ import yapion.hierarchy.types.YAPIONObject; import java.util.*; -// TODO: Rewrite! @Getter public class Warp { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java new file mode 100644 index 00000000..49d6af28 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpListener.java @@ -0,0 +1,111 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.warp; + +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.core.VersionedCallable; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.util.Vector; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Linked(LinkageType.LISTENER) +public class WarpListener implements Listener { + + private Map> warpsShown = new HashMap<>(); + private Map> selectedWarps = new HashMap<>(); + + @EventHandler + public void onPlayerMove(PlayerMoveEvent event) { + if (warpsShown.containsKey(event.getPlayer())) { + warpsShown.get(event.getPlayer()).forEach(warp -> { + warp.hide(event.getPlayer()); + }); + warpsShown.remove(event.getPlayer()); + } + if (event.getPlayer().getInventory().getItemInMainHand().getType() != Material.COMPASS) { + return; + } + + selectedWarps.remove(event.getPlayer()); + List warps = warpsShown.getOrDefault(event.getPlayer(), new ArrayList<>()); + Vector current = event.getPlayer().getLocation().clone().add(event.getPlayer().getLocation().getDirection().multiply(5)).toVector(); + current.setY(event.getPlayer().getLocation().getY() - 1); + Warp.getWarps().forEach(warp -> { + Vector vector = warp.getLocation().toVector().subtract(event.getPlayer().getLocation().toVector()); + if (vector.getX() * vector.getX() + vector.getZ() * vector.getZ() < 25) { + return; + } + + Vector position = event.getPlayer().getLocation().toVector().clone().add(vector.normalize().multiply(5)); + position.setY(event.getPlayer().getLocation().getY() - 1); + + String name = warp.getName(); + if (position.distanceSquared(current) < 0.2) { + name = "§a§l" + name; + selectedWarps.computeIfAbsent(event.getPlayer(), player -> new ArrayList<>()).add(warp); + } + warps.add(createEntity(event.getPlayer(), position, name)); + }); + + warpsShown.put(event.getPlayer(), warps); + warps.forEach(warp -> { + warp.display(event.getPlayer()); + }); + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getPlayer().getInventory().getItemInMainHand().getType() != Material.COMPASS) { + return; + } + if (event.getAction() != Action.RIGHT_CLICK_AIR) { + return; + } + List selected = selectedWarps.getOrDefault(event.getPlayer(), new ArrayList<>()); + if (selected.size() != 1) { + return; + } + selected.get(0).teleport(event.getPlayer()); + event.setCancelled(true); + } + + public static AbstractWarpEntity createEntity(Player player, Vector position, String name) { + return VersionedCallable.call(new VersionedCallable<>(() -> WarpListener_15.create(player.getWorld(), position, name), 15)); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + warpsShown.remove(event.getPlayer()); + selectedWarps.remove(event.getPlayer()); + } +}