SHARE
TWEET

Untitled

a guest Jun 25th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package io.asecta.minecraft.plugin;
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4.  
  5. import javax.script.Compilable;
  6. import javax.script.CompiledScript;
  7. import javax.script.Invocable;
  8. import javax.script.ScriptEngine;
  9. import javax.script.ScriptEngineManager;
  10. import javax.script.ScriptException;
  11.  
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.Location;
  14. import org.bukkit.WorldBorder;
  15. import org.bukkit.command.Command;
  16. import org.bukkit.command.CommandSender;
  17. import org.bukkit.entity.Player;
  18. import org.bukkit.plugin.java.JavaPlugin;
  19.  
  20. import com.comphenix.protocol.PacketType;
  21. import com.comphenix.protocol.ProtocolLibrary;
  22. import com.comphenix.protocol.ProtocolManager;
  23. import com.comphenix.protocol.events.PacketContainer;
  24. import com.comphenix.protocol.wrappers.EnumWrappers.ChatType;
  25. import com.comphenix.protocol.wrappers.EnumWrappers.WorldBorderAction;
  26. import com.comphenix.protocol.wrappers.WrappedChatComponent;
  27.  
  28. public class WorldBorderMain extends JavaPlugin {
  29.  
  30.     public ProtocolManager protocolManager;
  31.     public double speed = 250;
  32.     private Thread thread;
  33.  
  34.     @Override
  35.     public void onEnable() {
  36.         protocolManager = ProtocolLibrary.getProtocolManager();
  37.         startPulsing();
  38.     }
  39.  
  40.     @Override
  41.     public void onDisable() {
  42.         thread.stop();
  43.     }
  44.  
  45.     @Override
  46.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  47.         if (!(sender instanceof Player))
  48.             return true;
  49.         Player player = (Player) sender;
  50.         speed = Double.parseDouble(args[0]);
  51.         return true;
  52.     }
  53.  
  54.     public void startPulsing() {
  55.         Runnable pulser = () -> {
  56.             while (true) {
  57.                 Bukkit.getOnlinePlayers().forEach(this::pulse);
  58.                 try {
  59.                     Thread.sleep(1000 / 40);
  60.                 } catch (InterruptedException e) {
  61.                     e.printStackTrace();
  62.                 }
  63.             }
  64.         };
  65.         thread = new Thread(pulser);
  66.         thread.start();
  67.     }
  68.  
  69.     public void pulse(Player player) {
  70.         double strength = (Math.sin(System.currentTimeMillis() / speed) + 1d) / 2d;
  71.         setVignette(player, strength);
  72.     }
  73.  
  74.     public void setVignette(Player player, double strength) {
  75.         int warningDistance = calculateVignetteAt(player.getLocation(), strength);
  76.  
  77.         String message = String.format("§4§lSt: %.6f | WD: %d | BD: %.2f", strength, warningDistance,
  78.                 player.getWorld().getWorldBorder().getSize());
  79.  
  80.         PacketContainer borderPacket = new PacketContainer(PacketType.Play.Server.WORLD_BORDER);
  81.         borderPacket.getModifier().writeDefaults();
  82.         borderPacket.getWorldBorderActions().write(0, WorldBorderAction.SET_WARNING_BLOCKS);
  83.         borderPacket.getIntegers().write(2, warningDistance);
  84.  
  85.         PacketContainer chatPacket = new PacketContainer(PacketType.Play.Server.CHAT);
  86.         chatPacket.getModifier().writeDefaults();
  87.         chatPacket.getChatTypes().write(0, ChatType.GAME_INFO);
  88.         chatPacket.getChatComponents().write(0, WrappedChatComponent.fromText(message));
  89.  
  90.         try {
  91.             protocolManager.sendServerPacket(player, borderPacket);
  92.             protocolManager.sendServerPacket(player, chatPacket);
  93.         } catch (InvocationTargetException e) {
  94.             e.printStackTrace();
  95.         }
  96.     }
  97.  
  98.     private int calculateVignetteAt(Location location, double strength) {
  99.         WorldBorder border = location.getWorld().getWorldBorder();
  100.         double x = location.getX();
  101.         double z = location.getZ();
  102.  
  103.         // The client uses this to determine the vingette. Might have to change it or
  104.         // grab it from somewhere if we change the world size for optimization?
  105.         int worldSize = 29999984;
  106.         double radius = border.getSize() / 2;
  107.  
  108.         // We should probably cache some of this
  109.         double centerX = border.getCenter().getX();
  110.         double centerZ = border.getCenter().getZ();
  111.  
  112.         double minZ = z - Math.max(centerZ - radius, -worldSize);
  113.         double maxZ = Math.min(centerZ + radius, worldSize) - z;
  114.         double minX = x - Math.max(centerX - radius, -worldSize);
  115.         double maxX = Math.min(centerX + radius, worldSize) - x;
  116.  
  117.         double dX = Math.min(minX, maxX);
  118.         double dY = Math.min(minZ, maxZ);
  119.         double distance = Math.min(dX, dY);
  120.  
  121.         return (int) (1 + distance / (1 - strength));
  122.     }
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top