Rewrite in Java master v2.0
authorJan Hasebos <jan@hasebos.net>
Tue, 28 Apr 2020 12:33:36 +0000 (14:33 +0200)
committerJan Hasebos <jan@hasebos.net>
Tue, 28 Apr 2020 12:33:36 +0000 (14:33 +0200)
LICENSE [deleted file]
README.md
config.yml [new file with mode: 0644]
levitateab.sk [deleted file]
levitatech.sk [deleted file]
levitateno.sk [deleted file]
levitatest.sk [deleted file]
levitateti.sk [deleted file]
nl/jtvjan/levitate/Main.java [new file with mode: 0644]
plugin.yml [new file with mode: 0644]

diff --git a/LICENSE b/LICENSE
deleted file mode 100644 (file)
index 503e899..0000000
--- a/LICENSE
+++ /dev/null
@@ -1,21 +0,0 @@
-The MIT License (MIT)
-
-Copyright (c) 2016 Jan Hasebos
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
index 9a7e6e5..f22ca0f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
 # levitate
-A Minecraft levitation plugin written in Skript.
+A Minecraft levitation plugin. Compiled versions can be found on [spigotmc.org](https://www.spigotmc.org/resources/levitate.20729/).
diff --git a/config.yml b/config.yml
new file mode 100644 (file)
index 0000000..8bbd42a
--- /dev/null
@@ -0,0 +1,2 @@
+delay: 30
+active: 1.5
diff --git a/levitateab.sk b/levitateab.sk
deleted file mode 100644 (file)
index 3b5e323..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-# Config
-
-options:
-#      Change the delay between uses
-       delay = 30
-#      How long do you want the player to levitate
-       active = 1.5
-
-
-# Skript
-
-on join:
-       execute console command "/speed fly 0.7 %player%"
-on sneak toggle:
-       set {levitate.%player%.waited} to difference between now and {levitate.%player%.lastused}
-       if player's gamemode is creative:
-               execute console command "/tm amsg -silent %player% &cYou cant levitate in creative!"
-               stop
-               
-       if {levitate.%player%.waited} is less than 1 seconds:
-               stop
-               
-       if {levitate.%player%.waited} is less than {@delay} seconds:
-               set {levitate.%player%.waited.inverted} to difference between {@delay} seconds and {levitate.%player%.waited}
-               execute console command "/tm amsg -silent %player% &cLevitate is on cooldown | %{levitate.%player%.waited.inverted}%"
-               stop
-               
-       execute console command "/fly %player% on"
-       execute console command "/tm amsg -silent %player% &bLevitate Activated!"
-       set {levitate.%player%.lastused} to now 
-       wait {@active} seconds
-       execute console command "/fly %player% off"
-       execute console command "/tm amsg -silent %player% &3Levitate Deactivated!"
-       set {levitate.%player%.lastused} to now
diff --git a/levitatech.sk b/levitatech.sk
deleted file mode 100644 (file)
index efb56ec..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-# Config
-
-options:
-#      Change the delay between uses
-       delay = 30
-#      How long do you want the player to levitate
-       active = 1.5
-
-
-# Skript
-
-on join:
-       execute console command "/speed fly 0.7 %player%"
-on sneak toggle:
-       set {levitate.%player%.waited} to difference between now and {levitate.%player%.lastused}
-       if player's gamemode is creative:
-               send "&cYou cant levitate in creative!"
-               stop
-               
-       if {levitate.%player%.waited} is less than 1 seconds:
-               stop
-               
-       if {levitate.%player%.waited} is less than {@delay} seconds:
-               set {levitate.%player%.waited.inverted} to difference between {@delay} seconds and {levitate.%player%.waited}
-               send "&cLevitate is on cooldown | %{levitate.%player%.waited.inverted}%"
-               stop
-               
-       execute console command "/fly %player% on"
-       send "&bLevitate Activated!"
-       set {levitate.%player%.lastused} to now 
-       wait {@active} seconds
-       execute console command "/fly %player% off"
-       send "&3Levitate Deactivated!"
-       set {levitate.%player%.lastused} to now
diff --git a/levitateno.sk b/levitateno.sk
deleted file mode 100644 (file)
index 6bff1db..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-# Config
-
-options:
-#      Change the delay between uses
-       delay = 30
-#      How long do you want the player to levitate
-       active = 1.5
-
-
-# Skript
-
-on join:
-       execute console command "/speed fly 0.7 %player%"
-on sneak toggle:
-       set {levitate.%player%.waited} to difference between now and {levitate.%player%.lastused}
-       if player's gamemode is creative:
-               stop
-               
-       if {levitate.%player%.waited} is less than 1 seconds:
-               stop
-               
-       if {levitate.%player%.waited} is less than {@delay} seconds:
-               set {levitate.%player%.waited.inverted} to difference between {@delay} seconds and {levitate.%player%.waited}
-               stop
-               
-       execute console command "/fly %player% on"
-       set {levitate.%player%.lastused} to now 
-       wait {@active} seconds
-       execute console command "/fly %player% off"
-       set {levitate.%player%.lastused} to now
diff --git a/levitatest.sk b/levitatest.sk
deleted file mode 100644 (file)
index fccc1f1..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-# Config
-
-options:
-#      Change the delay between uses
-       delay = 30
-#      How long do you want the player to levitate
-       active = 1.5
-
-
-# Skript
-
-on join:
-       execute console command "/speed fly 0.7 %player%"
-on sneak toggle:
-       set {levitate.%player%.waited} to difference between now and {levitate.%player%.lastused}
-       if player's gamemode is creative:
-               execute console command "/titlemanager msg -silent %player% <nl>&cYou cant levitate in creative!"
-               stop
-               
-       if {levitate.%player%.waited} is less than 1 seconds:
-               stop
-               
-       if {levitate.%player%.waited} is less than {@delay} seconds:
-               set {levitate.%player%.waited.inverted} to difference between {@delay} seconds and {levitate.%player%.waited}
-               execute console command "/titlemanager msg -silent %player% <nl>&cLevitate is on cooldown | %{levitate.%player%.waited.inverted}%"
-               stop
-               
-       execute console command "/fly %player% on"
-       execute console command "/titlemanager msg -silent %player% <nl>&bLevitate Activated!"
-       set {levitate.%player%.lastused} to now 
-       wait {@active} seconds
-       execute console command "/fly %player% off"
-       execute console command "/titlemanager msg -silent %player% <nl>&3Levitate Deactivated!"
-       set {levitate.%player%.lastused} to now
diff --git a/levitateti.sk b/levitateti.sk
deleted file mode 100644 (file)
index be54ef3..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-# Config
-
-options:
-#      Change the delay between uses
-       delay = 30
-#      How long do you want the player to levitate
-       active = 1.5
-
-
-# Skript
-
-on join:
-       execute console command "/speed fly 0.7 %player%"
-on sneak toggle:
-       set {levitate.%player%.waited} to difference between now and {levitate.%player%.lastused}
-       if player's gamemode is creative:
-               execute console command "/titlemanager msg -silent %player% &cYou cant levitate in creative!"
-               stop
-               
-       if {levitate.%player%.waited} is less than 1 seconds:
-               stop
-               
-       if {levitate.%player%.waited} is less than {@delay} seconds:
-               set {levitate.%player%.waited.inverted} to difference between {@delay} seconds and {levitate.%player%.waited}
-               execute console command "/titlemanager msg -silent %player% &cLevitate is on cooldown | %{levitate.%player%.waited.inverted}%"
-               stop
-               
-       execute console command "/fly %player% on"
-       execute console command "/titlemanager msg -silent %player% &bLevitate Activated!"
-       set {levitate.%player%.lastused} to now 
-       wait {@active} seconds
-       execute console command "/fly %player% off"
-       execute console command "/titlemanager msg -silent %player% &3Levitate Deactivated!"
-       set {levitate.%player%.lastused} to now
diff --git a/nl/jtvjan/levitate/Main.java b/nl/jtvjan/levitate/Main.java
new file mode 100644 (file)
index 0000000..ac1c77b
--- /dev/null
@@ -0,0 +1,118 @@
+package nl.jtvjan.levitate;
+
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.UUID;
+import org.bukkit.plugin.java.JavaPlugin;
+import org.bukkit.entity.Player;
+import org.bukkit.entity.Entity;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.command.CommandExecutor;
+import org.bukkit.event.player.PlayerToggleFlightEvent;
+import org.bukkit.event.player.PlayerGameModeChangeEvent;
+import org.bukkit.event.player.PlayerQuitEvent;
+import org.bukkit.event.player.PlayerJoinEvent;
+import org.bukkit.event.player.PlayerMoveEvent;
+import org.bukkit.event.Listener;
+import org.bukkit.event.EventHandler;
+import org.bukkit.GameMode;
+import org.bukkit.ChatColor;
+
+public final class Main extends JavaPlugin implements CommandExecutor, Listener {
+       private Set<UUID> hasDisabled = new HashSet<>();
+       private Map<UUID, Long> cooldown = new HashMap<>();
+       
+       @Override
+       public void onEnable() {
+               getServer().getPluginManager().registerEvents(this, this);
+               saveDefaultConfig();
+       }
+       
+       public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
+               if (sender instanceof Player) {
+                       Player player = (Player) sender;
+                       UUID uuid = player.getUniqueId();
+                       if (hasDisabled.contains(uuid)) {
+                               hasDisabled.remove(uuid);
+                               if (player.getGameMode() == GameMode.SURVIVAL)
+                                       player.setAllowFlight(true);
+                               sender.sendMessage("Levitation enabled");
+                       } else {
+                               hasDisabled.add(uuid);
+                               if (player.getGameMode() == GameMode.SURVIVAL)
+                                       player.setAllowFlight(false);
+                               sender.sendMessage("Levitation disabled");
+                       }
+               } else
+                       sender.sendMessage("This command can only be used by players");
+               
+               return true;
+       }
+       
+       private boolean canLevitate(Player player, GameMode gamemode) {
+               return gamemode == GameMode.SURVIVAL && player.hasPermission("levitate.levitate") && !hasDisabled.contains(player.getUniqueId());
+       }
+       
+       @EventHandler
+       public void onPlayerFly(PlayerToggleFlightEvent event) {
+               Player player = event.getPlayer();
+               UUID uuid = player.getUniqueId();
+               if (event.isFlying() && canLevitate(player, player.getGameMode()))
+                       if (!cooldown.containsKey(uuid) || System.currentTimeMillis() - cooldown.get(uuid) > getConfig().getDouble("delay", 30) * 1000) {
+                               cooldown.put(uuid, System.currentTimeMillis());
+                               player.sendMessage(ChatColor.AQUA + "Levitation activated");
+                               getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
+                                       public void run() {
+                                               player.setFlying(false);
+                                       }
+                               }, (long) (20 * getConfig().getDouble("active", 1.5)));
+                       } else {
+                               event.setCancelled(true);
+                               player.sendMessage("Levitiation is on cooldown, wait " + ((getConfig().getDouble("delay", 30) * 1000 + cooldown.get(uuid) - System.currentTimeMillis())/1000) + " seconds");
+                       }
+       }
+       
+       @EventHandler
+       public void onPlayerGamemodeChange(PlayerGameModeChangeEvent event) {
+               if (canLevitate(event.getPlayer(), event.getNewGameMode()))
+                       getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
+                               public void run() {
+                                       event.getPlayer().setAllowFlight(true);
+                               }
+                       }, 1); // we need this to happen after the gamemode change happens, not before it
+       }
+       
+       @EventHandler
+       public void onPlayerJoin(PlayerJoinEvent event) {
+               Player player = event.getPlayer();
+               if (canLevitate(player, player.getGameMode()))
+                       player.setAllowFlight(true);
+       }
+       
+       @EventHandler
+       public void onPlayerQuit(PlayerQuitEvent event) {
+               UUID uuid = event.getPlayer().getUniqueId();
+               hasDisabled.remove(uuid);
+               cooldown.remove(uuid);
+       }
+       
+       /* players cannot take damage while they are allowed to fly
+        * that's why we temporarily take away that power
+        * https://stackoverflow.com/a/35402318
+        */
+       @EventHandler
+       public void onPlayerMove(PlayerMoveEvent event) {
+               Player player = event.getPlayer();
+               if (player.getFallDistance() > 3 && canLevitate(player, player.getGameMode()) && !event.getTo().clone().subtract(0, 0.5, 0).getBlock().isEmpty()) {
+                       player.setAllowFlight(false);
+                       getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
+                               public void run() {
+                                       player.setAllowFlight(true);
+                               }
+                       }, 5);
+               }
+       }
+}
diff --git a/plugin.yml b/plugin.yml
new file mode 100644 (file)
index 0000000..35206b8
--- /dev/null
@@ -0,0 +1,13 @@
+name: levitate
+main: nl.jtvjan.levitate.Main
+version: 1.0.0
+commands:
+    levitate:
+        description: Toggle levitation
+        usage: /<command>
+        permission: levitate.levitate.toggle
+permissions:
+    levitate.levitate:
+        description: Allows the user to levitate
+    levitate.levitate.toggle:
+        description: Allows the user to turn off levitation using /levitate
This page took 0.17504 seconds and 4 git commands to generate.