diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..eac169722a4f79888298cafb8047cf7633c0dea9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Tim Weber + +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. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..e5122b162adce67e83c59cd8e6dccedd054a62c0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,47 @@ +<?xml version="1.0" encoding="UTF-8"?> +<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>fsmpi.gaming.rtp</groupId> + <artifactId>rtp</artifactId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> + <maven.compiler.source>1.8</maven.compiler.source> + <maven.compiler.target>1.8</maven.compiler.target> + </properties> + + <build> + <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> + <resources> + <resource> + <directory>${project.basedir}/src/main/resources</directory> + <includes> + <include>plugin.yml</include> + <include>config.yml</include> + </includes> + </resource> + </resources> + </build> + + <repositories> + <repository> + <id>spigot-repo</id> + <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> + </repository> + </repositories> + + <dependencies> + <dependency> + <groupId>org.spigotmc</groupId> + <artifactId>spigot-api</artifactId> + <version>1.20.4-R0.1-SNAPSHOT</version> + <scope>provided</scope> + </dependency> + </dependencies> +</project> diff --git a/src/main/java/fsmpi/gaming/rtp/MyPlugin.java b/src/main/java/fsmpi/gaming/rtp/MyPlugin.java new file mode 100644 index 0000000000000000000000000000000000000000..ebd7d5348f8ffd9c023f138c60c0e4a6bf574240 --- /dev/null +++ b/src/main/java/fsmpi/gaming/rtp/MyPlugin.java @@ -0,0 +1,27 @@ +package fsmpi.gaming.rtp; + +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.plugin.java.JavaPlugin; + +public class MyPlugin extends JavaPlugin { + @Override + public void onEnable() { + RTPExecutor rtpExecutor = new RTPExecutor(); + getServer().getPluginCommand("rtp").setExecutor(rtpExecutor); + getServer().getScheduler().scheduleSyncRepeatingTask(this, rtpExecutor, RTPExecutor.TICK_INTERVAL, RTPExecutor.TICK_INTERVAL); + } + + @Override + public void onDisable() { + } + + @Override + public FileConfiguration getConfig() { + if (super.getConfig().getCurrentPath().equals("")) { + super.saveDefaultConfig(); + } + return super.getConfig(); + } + + +} diff --git a/src/main/java/fsmpi/gaming/rtp/RTPExecutor.java b/src/main/java/fsmpi/gaming/rtp/RTPExecutor.java new file mode 100644 index 0000000000000000000000000000000000000000..e171c6970d26c4bd6e71d7c425fca57b542bb5c5 --- /dev/null +++ b/src/main/java/fsmpi/gaming/rtp/RTPExecutor.java @@ -0,0 +1,111 @@ +package fsmpi.gaming.rtp; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Random; + +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.WorldBorder; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class RTPExecutor implements CommandExecutor, Runnable { + public static final int TIMEOUT_SECONDS = 300; + public static final int TICK_INTERVAL = 20; + ArrayList<Timeout> timeouts; + int timeout; + + public RTPExecutor() { + timeouts = new ArrayList<>(); + this.timeout = 300; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "You need to be a player!"); + return true; + } + + Player player = (Player) sender; + Timeout timeout = getTimeout(player); + + if (timeout != null) { + sender.sendMessage(ChatColor.RED + "You need to wait at least " + ((timeout.getTimeout() / 20) + 1) + " more seconds before you can teleport again!"); + return true; + } + + World world = player.getLocation().getWorld(); + + WorldBorder border = world.getWorldBorder(); + + Random r = new Random(); + + for (int i = 0; i < 10; i++) { + Location center = border.getCenter(); + double randX = (r.nextDouble()-0.5) * border.getSize() + center.getX(); + double randZ = (r.nextDouble()-0.5) * border.getSize() + center.getZ(); + double Y = getSafeY(world, randX, randZ); + if (Y != 0) { + player.teleport(new Location(world, randX, Y, randZ)); + setTimeout(player); + sender.sendMessage(ChatColor.GREEN + "Teleported Player to " + (int) randX + " " + (int) Y + " " + (int) randZ); + return true; + } + } + + sender.sendMessage(ChatColor.YELLOW + "Could not find a suitable Position, sorry! Please try again :)"); + return true; + } + + private Timeout getTimeout(Player player) { + for (Timeout timeout : timeouts) { + if (timeout.getPlayer().equals(player.getUniqueId())) { + return timeout; + } + } + return null; + } + + public void setTimeout(Player player) { + Timeout myTimeout = new Timeout(player.getUniqueId(), TIMEOUT_SECONDS * 20); + timeouts.add(myTimeout); + } + + public double getSafeY(World world, double X, double Z) { + for (int i = world.getMaxHeight(); i > world.getMinHeight(); i--) { + int x = (int) X; + int z = (int) Z; + if (isValid(world, x, i, z)) { + return i + 1; + } + } + return 0; + } + + private boolean isValid(World world, int x, int y, int z) { + return !world.getBlockAt(x, y, z).getType().equals(Material.BEDROCK) + && world.getBlockAt(x, y, z).getType().isSolid() + && world.getBlockAt(x, y + 1, z).getType().equals(Material.AIR) + && world.getBlockAt(x, y + 2, z).getType().equals(Material.AIR); + } + + @Override + public void run() { + Iterator<Timeout> i = timeouts.iterator(); + while (i.hasNext()) { + Timeout timeout = i.next(); + timeout.setTimeout(timeout.getTimeout() - TICK_INTERVAL); + + if (timeout.isFinished()) { + timeouts.remove(timeout); + } + } + } + +} diff --git a/src/main/java/fsmpi/gaming/rtp/Timeout.java b/src/main/java/fsmpi/gaming/rtp/Timeout.java new file mode 100644 index 0000000000000000000000000000000000000000..6dd1f4ed606050b98a8558857fd6e95d512d38bd --- /dev/null +++ b/src/main/java/fsmpi/gaming/rtp/Timeout.java @@ -0,0 +1,30 @@ +package fsmpi.gaming.rtp; + +import java.util.UUID; + + +public class Timeout { + + private UUID player; + private long timeout; + + public Timeout(UUID player, long timeout) { + this.player = player; + this.timeout = timeout; + } + public void setTimeout(long timeout) { + this.timeout = timeout; + } + + public long getTimeout() { + return this.timeout; + } + + public boolean isFinished() { + return timeout <= 0; + } + + public UUID getPlayer() { + return this.player; + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000000000000000000000000000000000000..25e8b21efb12ba9b2d09305a1058a2901d607add --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,10 @@ +name: RTP-Command +main: fsmpi.gaming.rtp.MyPlugin +version: 1 +api-version: 1.20 +commands: + rtp: + description: Random-TP. + usage: /rtp + permission: fsmpi.gaming.rtp +