Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MykolaMurza committed Dec 17, 2023
0 parents commit 62c1182
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
/target
/out
randullo.iml
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Randullo - A Minecraft Random Teleport Plugin

## Overview

Randullo is a Minecraft plugin that teleports the player to a random place on the first join or respawn if there is no bed.

This plugin is built with the Paper library and should be compatible with any server running Paper.

## Usage

### Commands

There are no special commands or usage rules for this plugin.

## Installation

To install the plugin, download the latest release and place the `.jar` file into your server's `plugins` folder. Be careful to choose the correct version of Minecraft. Then restart your server or load the plugin with a plugin manager.

## Configuring

There is no configuration for this plugin enabled.

## Localization

Randullo doesn't contain any localization. There are no messages to char from this plugin.

## Contributing

Contributions are welcome! If you want to help improve Randullo, feel free to fork the repository and submit a pull request.
72 changes: 72 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?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>ua.mykolamurza</groupId>
<artifactId>randullo</artifactId>
<version>1.20.2-R0.1</version>
<packaging>jar</packaging>

<name>randullo</name>

<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<paper.version>1.20.2-R0.1-SNAPSHOT</paper.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>${paper.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
25 changes: 25 additions & 0 deletions src/main/java/ua/mykolamurza/randullo/Randullo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ua.mykolamurza.randullo;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import ua.mykolamurza.randullo.hadnler.PlayerRespawnHandler;

/**
* Random teleport for players on respawn and for new players on first spawn.
*
* @author Mykola Murza
* @version Minecraft 1.20.2
*/
public final class Randullo extends JavaPlugin {
@Override
public void onEnable() {
Bukkit.getLogger().info("Start Randullo.");

getServer().getPluginManager().registerEvents(new PlayerRespawnHandler(), this);
}

@Override
public void onDisable() {
Bukkit.getLogger().info("Stop Randullo.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ua.mykolamurza.randullo.hadnler;

import org.bukkit.HeightMap;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerRespawnEvent;

import java.security.SecureRandom;

/**
* @author Mykola Murza
*/
public class PlayerRespawnHandler implements Listener {
@EventHandler
public void onPlayerRespawn(PlayerRespawnEvent event) {
Player player = event.getPlayer();
World world = player.getWorld();

Location bedSpawnLocation = player.getBedSpawnLocation();
if (bedSpawnLocation != null) {
event.setRespawnLocation(bedSpawnLocation);
return;
}

Location placeToSpawnPlayer = findPlaceToSpawnPlayer(world);
event.setRespawnLocation(placeToSpawnPlayer);
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
World world = player.getWorld();

if (event.getPlayer().hasPlayedBefore()) {
return;
}

Location placeToSpawnPlayer = findPlaceToSpawnPlayer(world);
player.teleport(placeToSpawnPlayer);
}

private Location findPlaceToSpawnPlayer(World world) {
SecureRandom random = new SecureRandom();
double borderSize = world.getWorldBorder().getSize();
double halfBorderSize = borderSize / 2;

Location location = generateRandomLocation(world, random, borderSize, (int) halfBorderSize);

checkIsLocationSafeAndMoveIfNot(location, world, location.getBlockX() >= 0);

return location;
}

private Location generateRandomLocation(World world, SecureRandom random, double borderSize, int halfBorderSize) {
double x = (int) (random.nextDouble() * borderSize) - halfBorderSize + 0.5;
double z = (int) (random.nextDouble() * borderSize) - halfBorderSize + 0.5;
int y = getHighestBlockAt(world, x, z);

return new Location(world, x, y, z);
}

private int getHighestBlockAt(World world, double x, double z) {
return world.getHighestBlockYAt(new Location(world, x, 0d, z), HeightMap.WORLD_SURFACE);
}

// isXPositive - means coordinate X was bigger than 0, and we have to remove 10 from X value if zone in unsafe.
// We can't check this statement every time because it leads to infinite loop on values between -10 and 10.
private void checkIsLocationSafeAndMoveIfNot(Location location, World world, boolean isXPositive) {
Material type = location.getBlock().getType();
while (type == Material.LAVA || type == Material.LAVA_CAULDRON) {
location.setX(isXPositive ? location.getX() - 10 : location.getX() + 10);
location.setY(world.getHighestBlockAt(location.getBlockX(), location.getBlockZ()).getY());
type = location.getBlock().getType();
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: randullo
version: '${project.version}'
main: ua.mykolamurza.randullo.Randullo
api-version: '1.20'

0 comments on commit 62c1182

Please sign in to comment.