Gliding activation and ToggleEvent cancelling control

This commit is contained in:
Maximilian P. Käfer 2020-11-15 21:46:41 +01:00
parent c488af138e
commit 631c817382
3 changed files with 56 additions and 6 deletions

View File

@ -18,12 +18,6 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>

View File

@ -1,5 +1,6 @@
package dev.kske.spawnfly;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
public final class SpawnFly extends JavaPlugin {
@ -7,6 +8,7 @@ public final class SpawnFly extends JavaPlugin {
@Override
public void onEnable() {
getLogger().info("onEnable has been invoked!");
Bukkit.getPluginManager().registerEvents(new SpawnListener(), this);
}
@Override

View File

@ -0,0 +1,54 @@
package dev.kske.spawnfly;
import java.util.*;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.*;
import org.bukkit.event.entity.EntityToggleGlideEvent;
import org.bukkit.event.player.*;
public class SpawnListener implements Listener {
Map<String, Boolean> activePlayers = new HashMap<>();
Map<String, Boolean> canceledEvents = new HashMap<>();
@EventHandler
public void onMove(PlayerMoveEvent e) {
Player player = e.getPlayer();
Location location = player.getLocation();
if(isInSpawn(location)) activePlayers.put(player.getName(), true);
else if(activePlayers.get(player.getName()) && player.getVelocity().getY() < 0) {
player.setGliding(true);
canceledEvents.put(player.getName(), true);
activePlayers.put(player.getName(), false);
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
activePlayers.put(e.getPlayer().getName(), isInSpawn(e.getPlayer().getLocation()));
canceledEvents.put(e.getPlayer().getName(), false);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
activePlayers.remove(e.getPlayer().getName());
canceledEvents.remove(e.getPlayer().getName());
}
private boolean isInSpawn(Location location) {
return (location.getX() <= -277 && location.getX() >= -302)
&& (location.getZ() <= -172 && location.getZ() >= -191);
}
@EventHandler
public void cancelEvent(EntityToggleGlideEvent e) {
if(canceledEvents.get(e.getEntity().getName()) && e.getEntity().getVelocity().getY() != 0 && !e.getEntity().isOnGround())
e.setCancelled(true);
else {
e.setCancelled(false);
canceledEvents.put(e.getEntity().getName(), false);
}
}
}