Browse Source

first commit

MasterCake 4 years ago
commit
fc8ee0ad13

+ 38 - 0
SpectatorAFK/.classpath

@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="test" value="true"/>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
+		<attributes>
+			<attribute name="test" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

+ 58 - 0
SpectatorAFK/.gitignore

@@ -0,0 +1,58 @@
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
+
+## Directory-based project format:
+.idea/
+# if you remove the above rule, at least ignore the following:
+
+# User-specific stuff:
+# .idea/**/workspace.xml
+# .idea/**/tasks.xml
+# .idea/dictionaries
+
+# Sensitive or high-churn files:
+# .idea/**/dataSources.ids
+# .idea/**/dataSources.xml
+# .idea/**/sqlDataSources.xml
+# .idea/**/dynamic.xml
+# .idea/**/uiDesigner.xml
+
+# Gradle:
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# CMake
+cmake-build-debug/
+
+# Mongo Explorer plugin:
+.idea/**/mongoSettings.xml
+
+## File-based project format:
+*.iws
+
+## Plugin-specific files:
+
+# IntelliJ
+out/
+*.iml
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Cursive Clojure plugin
+.idea/replstate.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties
+
+# Maven
+dependency-reduced-pom.xml
+*.jar
+target/
+bin/

+ 23 - 0
SpectatorAFK/.project

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>SpectatorAFK</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+	</natures>
+</projectDescription>

+ 71 - 0
SpectatorAFK/pom.xml

@@ -0,0 +1,71 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>xyz.spaceio</groupId>
+  <artifactId>SpectatorAFK</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <name>SpectatorAFK</name>
+  <description>Puts players into spectator mode after a specified amount of time afk</description>
+	<repositories>
+		<!-- Spigot repository -->
+		<repository>
+			<id>spigot-repo</id>
+			<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
+		</repository>
+	</repositories>
+	<dependencies>
+		<!--Spigot API -->
+		<dependency>
+			<groupId>org.spigotmc</groupId>
+			<artifactId>spigot-api</artifactId>
+			<version>1.15.1-R0.1-SNAPSHOT</version>
+			<scope>provided</scope>
+		</dependency>
+		<!-- Bukkit API -->
+		<dependency>
+			<groupId>org.bukkit</groupId>
+			<artifactId>craftbukkit</artifactId>
+			<version>1.15.1-R0.1-SNAPSHOT</version>
+			<scope>provided</scope>
+		</dependency>
+	</dependencies>
+	<build>
+		<!-- Uses the properties in this file for plugin.yml and config.yml -->
+		<resources>
+			<resource>
+				<directory>${basedir}/src/main/resources</directory>
+				<filtering>true</filtering>
+				<includes>
+					<include>plugin.yml</include>
+					<include>config.yml</include>
+					<include>translations/*</include>
+				</includes>
+			</resource>
+		</resources>
+		<plugins>
+			<!-- Sets the Java version to 8 -->
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-shade-plugin</artifactId>
+				<version>3.1.0</version>
+				<executions>
+					<execution>
+						<phase>package</phase>
+						<goals>
+							<goal>shade</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+			<!-- Sets the custom JARfile name (Project name without spaces is good) -->
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-jar-plugin</artifactId>
+				<version>2.4</version>
+				<configuration>
+					<outputDirectory>${dir}</outputDirectory>
+					<finalName>${project.name}</finalName>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

+ 30 - 0
SpectatorAFK/src/main/java/xyz/spaceio/spectatorafk/Listeners.java

@@ -0,0 +1,30 @@
+package xyz.spaceio.spectatorafk;
+
+import org.bukkit.GameMode;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.Listener;
+import org.bukkit.event.player.PlayerInteractEvent;
+import org.bukkit.event.player.PlayerQuitEvent;
+import org.bukkit.event.player.PlayerTeleportEvent;
+import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
+
+public class Listeners implements Listener {
+	@EventHandler
+	public void onInteract(PlayerInteractEvent e) {
+		if(Main.afk.contains(e.getPlayer())) {
+			Main.removeFromAfk(e.getPlayer());
+		}
+	}
+	@EventHandler
+	public void onTeleport(PlayerTeleportEvent e) {
+		if(e.getCause().equals(TeleportCause.SPECTATE) && Main.afk.contains(e.getPlayer())) {
+			e.setCancelled(true);
+		}
+	}
+	@EventHandler
+	public void onLeave(PlayerQuitEvent e) {
+		if(Main.afk.contains(e.getPlayer())) {
+			Main.removeFromAfk(e.getPlayer());
+		}
+	}
+}

+ 74 - 0
SpectatorAFK/src/main/java/xyz/spaceio/spectatorafk/Main.java

@@ -0,0 +1,74 @@
+package xyz.spaceio.spectatorafk;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.bukkit.Bukkit;
+import org.bukkit.GameMode;
+import org.bukkit.Location;
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.java.JavaPlugin;
+
+public class Main extends JavaPlugin{
+	public static HashMap<Player, Location> locations = new HashMap<>();
+	public static HashMap<Player, Integer> seconds = new HashMap<>();
+	public static List<Player> afk = new ArrayList<>();
+	
+	@Override
+	public void onDisable() {
+		for(Player p : afk) {
+			if(p.isOnline()) {
+				p.setGameMode(GameMode.SURVIVAL);
+				p.setWalkSpeed(0.2f);
+				p.setFlySpeed(0.2f);
+			}
+		}
+	}
+	
+	@Override
+	public void onEnable() {
+		this.getServer().getPluginManager().registerEvents(new Listeners(), this);
+		
+		Bukkit.getScheduler().runTaskTimer(this, () -> {
+			for(Player player : Bukkit.getOnlinePlayers()) {
+				Location ploc = player.getLocation();
+				if(locations.containsKey(player)) {
+					if(ploc.getX() == locations.get(player).getX() &&
+									ploc.getZ() == locations.get(player).getZ()) {
+						int before = seconds.getOrDefault(player, 0);
+						seconds.put(player, before + 1);
+					}else {
+						seconds.remove(player);
+						if(afk.contains(player)) {
+							removeFromAfk(player);
+						}
+					}
+				}
+				locations.put(player, ploc);
+				
+				int time = seconds.getOrDefault(player, 0);
+				if(time >= 120 && !afk.contains(player)) {
+					// put afk
+					afk.add(player);
+					player.teleport(player.getLocation().add(0, 0.2, 0));
+					player.setGameMode(GameMode.SPECTATOR);
+					player.setWalkSpeed(0);
+					player.setFlySpeed(0);
+					player.sendMessage("§aDu bist nun AFK!");
+				}
+			}
+		}, 20L, 20L);
+	}
+	
+	public static void removeFromAfk(Player player) {
+		player.setWalkSpeed(0.2f);
+		player.setFlySpeed(0.2f);
+		player.setGameMode(GameMode.SURVIVAL);
+		Main.afk.remove(player);
+		Main.seconds.remove(player);
+		
+		player.sendMessage("§aWelcome back!");
+		
+	}
+}

+ 6 - 0
SpectatorAFK/src/main/resources/plugin.yml

@@ -0,0 +1,6 @@
+name: SpectatorAFK
+version: ${project.version}
+description: ${project.description}
+author: Linus122
+main: xyz.spaceio.spectatorafk.Main
+api-version: 1.13