forked from SteamWar/SteamWar
Update WorldEditRenderer
This commit is contained in:
@@ -20,10 +20,15 @@
|
||||
package de.steamwar.core;
|
||||
|
||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.steamwar.Reflection;
|
||||
import de.steamwar.command.*;
|
||||
import de.steamwar.core.authlib.AuthlibInjector;
|
||||
import de.steamwar.core.events.*;
|
||||
import de.steamwar.core.events.AntiNocom;
|
||||
import de.steamwar.core.events.ChattingEvent;
|
||||
import de.steamwar.core.events.PlayerJoinedEvent;
|
||||
import de.steamwar.core.events.WorldLoadEvent;
|
||||
import de.steamwar.message.Message;
|
||||
import de.steamwar.network.NetworkReceiver;
|
||||
import de.steamwar.network.handlers.ServerDataHandler;
|
||||
@@ -32,23 +37,42 @@ import de.steamwar.sql.SteamwarUser;
|
||||
import de.steamwar.sql.internal.Statement;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class Core extends JavaPlugin{
|
||||
public class Core extends JavaPlugin implements PluginMessageListener, Listener {
|
||||
|
||||
public static final Message MESSAGE = new Message("SpigotCore", Core.class.getClassLoader());
|
||||
|
||||
private static final String CHANNEL = "vv:proxy_details";
|
||||
private static final Gson GSON = new Gson();
|
||||
private static final Map<Player, Integer> playerVersions = new HashMap<>();
|
||||
|
||||
public static int getVersion(){
|
||||
return Reflection.MAJOR_VERSION;
|
||||
}
|
||||
|
||||
public static int getPlayerVersion(Player player) {
|
||||
return playerVersions.getOrDefault(player, -1);
|
||||
}
|
||||
|
||||
public static boolean isBedrockPlayer(Player player) {
|
||||
return player.getName().startsWith(".");
|
||||
}
|
||||
|
||||
private static JavaPlugin instance;
|
||||
public static JavaPlugin getInstance() {
|
||||
return instance;
|
||||
@@ -67,6 +91,9 @@ public class Core extends JavaPlugin{
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
this.getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, this);
|
||||
Bukkit.getPluginManager().registerEvents(this, this);
|
||||
|
||||
errorHandler = new ErrorHandler();
|
||||
crashDetector = new CrashDetector();
|
||||
|
||||
@@ -103,7 +130,7 @@ public class Core extends JavaPlugin{
|
||||
new ServerDataHandler();
|
||||
|
||||
if(Bukkit.getPluginManager().getPlugin("WorldEdit") != null)
|
||||
WorldEditRenderer.impl.init();
|
||||
new WorldEditRenderer();
|
||||
|
||||
Bukkit.getScheduler().runTaskTimer(this, TabCompletionCache::invalidateOldEntries, 20, 20);
|
||||
Bukkit.getScheduler().runTaskTimer(Core.getInstance(), SteamwarUser::clear, 72000, 72000);
|
||||
@@ -122,5 +149,22 @@ public class Core extends JavaPlugin{
|
||||
errorHandler.unregister();
|
||||
if(crashDetector.onMainThread())
|
||||
Statement.closeAll();
|
||||
this.getServer().getMessenger().unregisterIncomingPluginChannel(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
|
||||
if (!channel.equals(CHANNEL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final JsonObject payload = GSON.fromJson(new String(bytes), JsonObject.class);
|
||||
final String version = payload.get("versionName").getAsString();
|
||||
playerVersions.put(player, Integer.parseInt(version.split("-")[0].split("\\.")[1]));
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
playerVersions.remove(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +31,16 @@ public class VersionDependent {
|
||||
}
|
||||
|
||||
public static <T> T getVersionImpl(Plugin plugin, String className){
|
||||
return getVersionImpl(plugin, Core.getVersion(), className);
|
||||
}
|
||||
|
||||
public static <T> T getVersionImpl(Plugin plugin, int fromVersion){
|
||||
return getVersionImpl(plugin, fromVersion, (new Exception()).getStackTrace()[1].getClassName());
|
||||
}
|
||||
|
||||
public static <T> T getVersionImpl(Plugin plugin, int fromVersion, String className){
|
||||
ClassLoader loader = plugin.getClass().getClassLoader();
|
||||
for(int version = Core.getVersion(); version >= 8; version--) {
|
||||
for(int version = fromVersion; version >= 8; version--) {
|
||||
try {
|
||||
return ((Class<? extends T>) Class.forName(className + version, true, loader)).getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
|
||||
@@ -1,27 +1,166 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
||||
* Copyright (C) 2024 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 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.
|
||||
* 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/>.
|
||||
* 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.core;
|
||||
|
||||
public interface WorldEditRenderer {
|
||||
WorldEditRenderer impl = VersionDependent.getVersionImpl(Core.getInstance());
|
||||
import com.sk89q.worldedit.EmptyClipboardException;
|
||||
import com.sk89q.worldedit.IncompleteRegionException;
|
||||
import com.sk89q.worldedit.LocalSession;
|
||||
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
|
||||
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
||||
import com.sk89q.worldedit.math.transform.Transform;
|
||||
import com.sk89q.worldedit.regions.Region;
|
||||
import com.sk89q.worldedit.regions.RegionSelector;
|
||||
import com.sk89q.worldedit.world.World;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.player.*;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
default void init() {
|
||||
public class WorldEditRenderer implements Listener {
|
||||
|
||||
private static final Material WAND = FlatteningWrapper.impl.getMaterial("WOOD_AXE");
|
||||
|
||||
private final WorldEditPlugin we;
|
||||
|
||||
public WorldEditRenderer() {
|
||||
we = WorldEditWrapper.getWorldEditPlugin();
|
||||
Bukkit.getPluginManager().registerEvents(this, Core.getInstance());
|
||||
|
||||
Bukkit.getScheduler().runTaskTimer(Core.getInstance(), this::render, 20, 20);
|
||||
}
|
||||
|
||||
private void render() {
|
||||
for(Player player : Bukkit.getOnlinePlayers()) {
|
||||
renderPlayer(player);
|
||||
}
|
||||
}
|
||||
|
||||
private void renderPlayer(Player player) {
|
||||
LocalSession session = we.getSession(player);
|
||||
renderClipboard(player, session);
|
||||
renderRegion(player, session);
|
||||
}
|
||||
|
||||
private void renderClipboard(Player player, LocalSession session) {
|
||||
try {
|
||||
Clipboard clipboard = session.getClipboard().getClipboard();
|
||||
Vector pos = player.getLocation().toVector();
|
||||
Region region = clipboard.getRegion();
|
||||
Transform transform = session.getClipboard().getTransform();
|
||||
Vector a = WorldEditWrapper.impl.applyTransform(WorldEditWrapper.impl.getMinimum(region).subtract(WorldEditWrapper.impl.getOrigin(clipboard)), transform).add(pos);
|
||||
Vector b = WorldEditWrapper.impl.applyTransform(WorldEditWrapper.impl.getMaximum(region).subtract(WorldEditWrapper.impl.getOrigin(clipboard)), transform).add(pos);
|
||||
a = new Vector(a.getBlockX(), a.getBlockY(), a.getBlockZ());
|
||||
b = new Vector(b.getBlockX(), b.getBlockY(), b.getBlockZ());
|
||||
WorldEditRendererWrapper.impl.hide(player, player, true, false);
|
||||
drawCuboid(Vector.getMinimum(a, b), Vector.getMaximum(a, b), true, player);
|
||||
} catch (EmptyClipboardException e) {
|
||||
WorldEditRendererWrapper.impl.hide(player, player, true, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void renderRegion(Player player, LocalSession session) {
|
||||
World world = session.getSelectionWorld();
|
||||
if(world != null) {
|
||||
RegionSelector regionSelector = session.getRegionSelector(world);
|
||||
try {
|
||||
Region region = regionSelector.getRegion();
|
||||
WorldEditRendererWrapper.impl.hide(player, player, false, false);
|
||||
drawCuboid(WorldEditWrapper.impl.getMinimum(region), WorldEditWrapper.impl.getMaximum(region), false, player);
|
||||
} catch (IncompleteRegionException e) {
|
||||
WorldEditRendererWrapper.impl.hide(player, player, false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawCuboid(Vector min, Vector max, boolean clipboard, Player owner) {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
//noinspection deprecation
|
||||
if(player.getItemInHand().getType() != WAND) {
|
||||
WorldEditRendererWrapper.impl.hide(player, owner, true, true);
|
||||
WorldEditRendererWrapper.impl.hide(player, owner, false, true);
|
||||
} else {
|
||||
WorldEditRendererWrapper.impl.hide(player, owner, true, false);
|
||||
WorldEditRendererWrapper.impl.hide(player, owner, false, false);
|
||||
WorldEditRendererWrapper.safeDraw(player, owner, clipboard, min, max);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
renderPlayer(event.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerMove(PlayerMoveEvent event) {
|
||||
WorldEditRendererWrapper.impl.tick(event.getPlayer());
|
||||
|
||||
renderClipboard(event.getPlayer(), we.getSession(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||
renderRegion(event.getPlayer(), we.getSession(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
renderRegion(event.getPlayer(), we.getSession(event.getPlayer()));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
|
||||
if (event.getMessage().startsWith("//")) {
|
||||
Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> {
|
||||
LocalSession session = we.getSession(event.getPlayer());
|
||||
renderRegion(event.getPlayer(), session);
|
||||
renderClipboard(event.getPlayer(), session);
|
||||
}, 5);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
|
||||
Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> {
|
||||
renderPlayer(event.getPlayer());
|
||||
}, 1);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDropItem(PlayerDropItemEvent event) {
|
||||
renderPlayer(event.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerItemHeld(PlayerItemHeldEvent event) {
|
||||
Bukkit.getScheduler().runTaskLater(Core.getInstance(), () -> {
|
||||
renderPlayer(event.getPlayer());
|
||||
}, 1);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
WorldEditRendererWrapper.impl.remove(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2020 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.core;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
public interface WorldEditRendererWrapper {
|
||||
WorldEditRendererWrapper fallback = VersionDependent.getVersionImpl(Core.getInstance(), 9);
|
||||
WorldEditRendererWrapper impl = VersionDependent.getVersionImpl(Core.getInstance());
|
||||
|
||||
static void safeDraw(Player player, Player owner, boolean clipboard, Vector pos1, Vector pos2) {
|
||||
if (Core.isBedrockPlayer(player) || Core.getPlayerVersion(player) < 20) {
|
||||
fallback.draw(player, owner, clipboard, pos1, pos2);
|
||||
} else {
|
||||
impl.draw(player, owner, clipboard, pos1, pos2);
|
||||
}
|
||||
}
|
||||
|
||||
void draw(Player player, Player owner, boolean clipboard, Vector pos1, Vector pos2);
|
||||
|
||||
default void tick(Player player) {
|
||||
}
|
||||
|
||||
default void hide(Player player, Player owner, boolean clipboard, boolean hide) {
|
||||
}
|
||||
|
||||
default void remove(Player player) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2020 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.entity;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Compound Axis Aligned Box (12 CAALine)
|
||||
*/
|
||||
public class CAABox extends CEntity {
|
||||
|
||||
public static final float DEFAULT_WIDTH = 1 / 16f;
|
||||
private float width = DEFAULT_WIDTH;
|
||||
|
||||
private Location pos1;
|
||||
private Location pos2;
|
||||
|
||||
public CAABox(REntityServer server) {
|
||||
super(server);
|
||||
}
|
||||
|
||||
public CAABox setPos1(Location pos1) {
|
||||
this.pos1 = pos1;
|
||||
updateAndSpawnLines();
|
||||
return this;
|
||||
}
|
||||
|
||||
public CAABox setPos2(Location pos2) {
|
||||
this.pos2 = pos2;
|
||||
updateAndSpawnLines();
|
||||
return this;
|
||||
}
|
||||
|
||||
public CAABox setWidth(float width) {
|
||||
this.width = width;
|
||||
updateAndSpawnLines();
|
||||
getEntitiesByType(CAALine.class).forEach(haaLine -> {
|
||||
haaLine.setWidth(width);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public CAABox setBlock(BlockData blockData) {
|
||||
getEntitiesByType(CAALine.class).forEach(haaLine -> {
|
||||
haaLine.setBlock(blockData);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
private void updateAndSpawnLines() {
|
||||
if (pos1 == null || pos2 == null) return;
|
||||
if (entities.isEmpty()) {
|
||||
for (int i = 0; i < 12; i++) {
|
||||
entities.add(new CAALine(server));
|
||||
}
|
||||
}
|
||||
|
||||
World world = pos1.getWorld();
|
||||
Vector min = Vector.getMinimum(pos1.toVector(), pos2.toVector());
|
||||
Vector max = Vector.getMaximum(pos1.toVector(), pos2.toVector())
|
||||
.add(new Vector(1 - width, 1 - width, 1 - width));
|
||||
|
||||
List<CAALine> lines = getEntitiesByType(CAALine.class);
|
||||
lines.forEach(line -> line.setFrom(null).setTo(null));
|
||||
|
||||
lines.get(0).setFrom(new Vector(min.getX(), min.getY(), min.getZ()).toLocation(world)).setTo(new Vector(max.getX() + width, min.getY(), min.getZ()).toLocation(world));
|
||||
lines.get(1).setFrom(new Vector(min.getX(), max.getY(), min.getZ()).toLocation(world)).setTo(new Vector(max.getX() + width, max.getY(), min.getZ()).toLocation(world));
|
||||
lines.get(2).setFrom(new Vector(min.getX(), min.getY(), max.getZ()).toLocation(world)).setTo(new Vector(max.getX() + width, min.getY(), max.getZ()).toLocation(world));
|
||||
lines.get(3).setFrom(new Vector(min.getX(), max.getY(), max.getZ()).toLocation(world)).setTo(new Vector(max.getX() + width, max.getY(), max.getZ()).toLocation(world));
|
||||
|
||||
lines.get(4).setFrom(new Vector(min.getX(), min.getY(), min.getZ()).toLocation(world)).setTo(new Vector(min.getX(), max.getY() + width, min.getZ()).toLocation(world));
|
||||
lines.get(5).setFrom(new Vector(max.getX(), min.getY(), min.getZ()).toLocation(world)).setTo(new Vector(max.getX(), max.getY() + width, min.getZ()).toLocation(world));
|
||||
lines.get(6).setFrom(new Vector(min.getX(), min.getY(), max.getZ()).toLocation(world)).setTo(new Vector(min.getX(), max.getY() + width, max.getZ()).toLocation(world));
|
||||
lines.get(7).setFrom(new Vector(max.getX(), min.getY(), max.getZ()).toLocation(world)).setTo(new Vector(max.getX(), max.getY() + width, max.getZ()).toLocation(world));
|
||||
|
||||
lines.get(8).setFrom(new Vector(min.getX(), min.getY(), min.getZ()).toLocation(world)).setTo(new Vector(min.getX(), min.getY(), max.getZ() + width).toLocation(world));
|
||||
lines.get(9).setFrom(new Vector(max.getX(), min.getY(), min.getZ()).toLocation(world)).setTo(new Vector(max.getX(), min.getY(), max.getZ() + width).toLocation(world));
|
||||
lines.get(10).setFrom(new Vector(min.getX(), max.getY(), min.getZ()).toLocation(world)).setTo(new Vector(min.getX(), max.getY(), max.getZ() + width).toLocation(world));
|
||||
lines.get(11).setFrom(new Vector(max.getX(), max.getY(), min.getZ()).toLocation(world)).setTo(new Vector(max.getX(), max.getY(), max.getZ() + width).toLocation(world));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2020 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.entity;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.Display;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.Transformation;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.joml.Quaternionf;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class CAALine extends CEntity {
|
||||
|
||||
public static final float DEFAULT_WIDTH = 1 / 16f;
|
||||
private static final float offset = 1 / 1024f;
|
||||
private static final Vector offsetVec = new Vector(offset, offset, offset);
|
||||
|
||||
private Location from;
|
||||
private Location to;
|
||||
private float width = DEFAULT_WIDTH;
|
||||
private BlockData blockData = RBlockDisplay.DEFAULT_BLOCK;
|
||||
|
||||
public CAALine(REntityServer server) {
|
||||
super(server);
|
||||
tick();
|
||||
}
|
||||
|
||||
public CAALine setFrom(Location from) {
|
||||
if (Objects.equals(from, this.from)) return this;
|
||||
this.from = from;
|
||||
tick();
|
||||
return this;
|
||||
}
|
||||
|
||||
public CAALine setTo(Location to) {
|
||||
if (Objects.equals(to, this.to)) return this;
|
||||
this.to = to;
|
||||
tick();
|
||||
return this;
|
||||
}
|
||||
|
||||
public CAALine setWidth(float width) {
|
||||
if (this.width == width) return this;
|
||||
this.width = width;
|
||||
tick();
|
||||
return this;
|
||||
}
|
||||
|
||||
public CAALine setBlock(BlockData blockData) {
|
||||
if (this.blockData.equals(blockData)) return this;
|
||||
if (blockData == null) {
|
||||
this.blockData = RBlockDisplay.DEFAULT_BLOCK;
|
||||
} else {
|
||||
this.blockData = blockData;
|
||||
}
|
||||
getEntitiesByType(RBlockDisplay.class).forEach(display -> {
|
||||
display.setBlock(blockData);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
private boolean hide = false;
|
||||
|
||||
@Override
|
||||
public void hide(boolean hide) {
|
||||
if (hide == this.hide) return;
|
||||
this.hide = hide;
|
||||
if (hide) {
|
||||
if (startLine != null) startLine.hide(true);
|
||||
if (middleLine != null) middleLine.hide(true);
|
||||
if (endLine != null) endLine.hide(true);
|
||||
} else {
|
||||
tick();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void tick() {
|
||||
if (from == null || to == null) return;
|
||||
if (hide) return;
|
||||
updateStart();
|
||||
updateMiddle();
|
||||
updateEnd();
|
||||
}
|
||||
|
||||
private RBlockDisplay startLine;
|
||||
private void updateStart() {
|
||||
Vector vec = to.clone().subtract(from).toVector();
|
||||
if (vec.length() > 35) vec.normalize().multiply(35);
|
||||
|
||||
if (startLine == null) {
|
||||
startLine = new RBlockDisplay(server, new Location(null, 0, 0, 0));
|
||||
startLine.setBrightness(new Display.Brightness(15, 15));
|
||||
startLine.setViewRange(560);
|
||||
startLine.setBlock(blockData);
|
||||
entities.add(startLine);
|
||||
} else {
|
||||
startLine.hide(false);
|
||||
}
|
||||
|
||||
startLine.move(from.clone().subtract(offsetVec));
|
||||
startLine.setTransform(new Transformation(new Vector3f(0, 0, 0), new Quaternionf(0, 0, 0, 1), addWidth(vec).toVector3f(), new Quaternionf(0, 0, 0, 1)));
|
||||
}
|
||||
|
||||
private RBlockDisplay middleLine;
|
||||
private void updateMiddle() {
|
||||
Vector vec = to.clone().subtract(from).toVector();
|
||||
if (vec.length() <= 70) {
|
||||
if (middleLine != null) middleLine.hide(true);
|
||||
return;
|
||||
}
|
||||
if (vec.length() > 280) vec.normalize().multiply(280);
|
||||
else vec = vec.clone().normalize().multiply(vec.length() - 60);
|
||||
|
||||
if (middleLine == null) {
|
||||
middleLine = new RBlockDisplay(server, new Location(null, 0, 0, 0));
|
||||
middleLine.setBrightness(new Display.Brightness(15, 15));
|
||||
middleLine.setViewRange(560);
|
||||
middleLine.setBlock(blockData);
|
||||
entities.add(middleLine);
|
||||
} else {
|
||||
middleLine.hide(false);
|
||||
}
|
||||
|
||||
Player player = server.getPlayers().stream().findFirst().orElse(null);
|
||||
if (player == null) return;
|
||||
|
||||
Vector tempVector = vec.clone().normalize().multiply(30);
|
||||
Location from = this.from.clone().add(tempVector);
|
||||
Location to = this.to.clone().subtract(tempVector);
|
||||
|
||||
Vector lineVec = to.clone().subtract(from).toVector();
|
||||
Vector playerVec = player.getLocation().toVector().subtract(from.toVector());
|
||||
double lineVecDotItself = lineVec.dot(lineVec);
|
||||
Vector projectionVec = lineVec.clone().multiply(lineVec.dot(playerVec)).divide(new Vector(lineVecDotItself, lineVecDotItself, lineVecDotItself));
|
||||
|
||||
Vector moveVec = from.toVector().add(projectionVec);
|
||||
if (moveVec.getX() < from.getX()) {
|
||||
moveVec.setX(from.getX());
|
||||
}
|
||||
if (moveVec.getX() > to.getX()) {
|
||||
moveVec.setX(to.getX());
|
||||
}
|
||||
if (moveVec.getY() < from.getY()) {
|
||||
moveVec.setY(from.getY());
|
||||
}
|
||||
if (moveVec.getY() > to.getY()) {
|
||||
moveVec.setY(to.getY());
|
||||
}
|
||||
if (moveVec.getZ() < from.getZ()) {
|
||||
moveVec.setZ(from.getZ());
|
||||
}
|
||||
if (moveVec.getZ() > to.getZ()) {
|
||||
moveVec.setZ(to.getZ());
|
||||
}
|
||||
|
||||
Vector translation = vec.clone().divide(new Vector(2, 2, 2));
|
||||
translation.setX(-translation.getX());
|
||||
translation.setY(-translation.getY());
|
||||
translation.setZ(-translation.getZ());
|
||||
|
||||
Vector first = moveVec.clone().add(translation).subtract(from.toVector());
|
||||
if (first.getX() < 0) {
|
||||
translation.setX(translation.getX() - first.getX());
|
||||
}
|
||||
if (first.getY() < 0) {
|
||||
translation.setY(translation.getY() - first.getY());
|
||||
}
|
||||
if (first.getZ() < 0) {
|
||||
translation.setZ(translation.getZ() - first.getZ());
|
||||
}
|
||||
|
||||
Vector second = to.toVector().subtract(moveVec.clone().subtract(translation));
|
||||
if (second.getX() < 0) {
|
||||
translation.setX(translation.getX() + second.getX());
|
||||
}
|
||||
if (second.getY() < 0) {
|
||||
translation.setY(translation.getY() + second.getY());
|
||||
}
|
||||
if (second.getZ() < 0) {
|
||||
translation.setZ(translation.getZ() + second.getZ());
|
||||
}
|
||||
|
||||
middleLine.move(moveVec.toLocation(player.getWorld()).subtract(offsetVec));
|
||||
middleLine.setTransform(new Transformation(translation.toVector3f(), new Quaternionf(0, 0, 0, 1), addWidth(vec).toVector3f(), new Quaternionf(0, 0, 0, 1)));
|
||||
}
|
||||
|
||||
private RBlockDisplay endLine;
|
||||
private void updateEnd() {
|
||||
Vector vec = to.clone().subtract(from).toVector();
|
||||
if (vec.length() <= 35) {
|
||||
if (endLine != null) endLine.hide(true);
|
||||
return;
|
||||
}
|
||||
if (vec.length() > 35) vec.normalize().multiply(35);
|
||||
|
||||
if (endLine == null) {
|
||||
endLine = new RBlockDisplay(server, new Location(null, 0, 0, 0));
|
||||
endLine.setBrightness(new Display.Brightness(15, 15));
|
||||
endLine.setViewRange(560);
|
||||
endLine.setBlock(blockData);
|
||||
entities.add(endLine);
|
||||
} else {
|
||||
endLine.hide(false);
|
||||
}
|
||||
|
||||
endLine.move(to.clone().subtract(offsetVec));
|
||||
endLine.setTransform(new Transformation(vec.toVector3f().negate(), new Quaternionf(0, 0, 0, 1), addWidth(vec).toVector3f(), new Quaternionf(0, 0, 0, 1)));
|
||||
}
|
||||
|
||||
private Vector addWidth(Vector vector) {
|
||||
vector = vector.clone();
|
||||
if (vector.getX() == 0) {
|
||||
vector.setX(vector.getX() + width);
|
||||
}
|
||||
if (vector.getY() == 0) {
|
||||
vector.setY(vector.getY() + width);
|
||||
}
|
||||
if (vector.getZ() == 0) {
|
||||
vector.setZ(vector.getZ() + width);
|
||||
}
|
||||
vector.add(offsetVec).add(offsetVec);
|
||||
return vector;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user