Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.asecta.minecraft.plugin;
- import java.lang.reflect.InvocationTargetException;
- import javax.script.Compilable;
- import javax.script.CompiledScript;
- import javax.script.Invocable;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- import javax.script.ScriptException;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.WorldBorder;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.comphenix.protocol.PacketType;
- import com.comphenix.protocol.ProtocolLibrary;
- import com.comphenix.protocol.ProtocolManager;
- import com.comphenix.protocol.events.PacketContainer;
- import com.comphenix.protocol.wrappers.EnumWrappers.ChatType;
- import com.comphenix.protocol.wrappers.EnumWrappers.WorldBorderAction;
- import com.comphenix.protocol.wrappers.WrappedChatComponent;
- public class WorldBorderMain extends JavaPlugin {
- public ProtocolManager protocolManager;
- public double speed = 250;
- private Thread thread;
- @Override
- public void onEnable() {
- protocolManager = ProtocolLibrary.getProtocolManager();
- startPulsing();
- }
- @Override
- public void onDisable() {
- thread.stop();
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (!(sender instanceof Player))
- return true;
- Player player = (Player) sender;
- speed = Double.parseDouble(args[0]);
- return true;
- }
- public void startPulsing() {
- Runnable pulser = () -> {
- while (true) {
- Bukkit.getOnlinePlayers().forEach(this::pulse);
- try {
- Thread.sleep(1000 / 40);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- };
- thread = new Thread(pulser);
- thread.start();
- }
- public void pulse(Player player) {
- double strength = (Math.sin(System.currentTimeMillis() / speed) + 1d) / 2d;
- setVignette(player, strength);
- }
- public void setVignette(Player player, double strength) {
- int warningDistance = calculateVignetteAt(player.getLocation(), strength);
- String message = String.format("§4§lSt: %.6f | WD: %d | BD: %.2f", strength, warningDistance,
- player.getWorld().getWorldBorder().getSize());
- PacketContainer borderPacket = new PacketContainer(PacketType.Play.Server.WORLD_BORDER);
- borderPacket.getModifier().writeDefaults();
- borderPacket.getWorldBorderActions().write(0, WorldBorderAction.SET_WARNING_BLOCKS);
- borderPacket.getIntegers().write(2, warningDistance);
- PacketContainer chatPacket = new PacketContainer(PacketType.Play.Server.CHAT);
- chatPacket.getModifier().writeDefaults();
- chatPacket.getChatTypes().write(0, ChatType.GAME_INFO);
- chatPacket.getChatComponents().write(0, WrappedChatComponent.fromText(message));
- try {
- protocolManager.sendServerPacket(player, borderPacket);
- protocolManager.sendServerPacket(player, chatPacket);
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- }
- private int calculateVignetteAt(Location location, double strength) {
- WorldBorder border = location.getWorld().getWorldBorder();
- double x = location.getX();
- double z = location.getZ();
- // The client uses this to determine the vingette. Might have to change it or
- // grab it from somewhere if we change the world size for optimization?
- int worldSize = 29999984;
- double radius = border.getSize() / 2;
- // We should probably cache some of this
- double centerX = border.getCenter().getX();
- double centerZ = border.getCenter().getZ();
- double minZ = z - Math.max(centerZ - radius, -worldSize);
- double maxZ = Math.min(centerZ + radius, worldSize) - z;
- double minX = x - Math.max(centerX - radius, -worldSize);
- double maxX = Math.min(centerX + radius, worldSize) - x;
- double dX = Math.min(minX, maxX);
- double dY = Math.min(minZ, maxZ);
- double distance = Math.min(dX, dY);
- return (int) (1 + distance / (1 - strength));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement