Skip to content

Commit

Permalink
Initial Release
Browse files Browse the repository at this point in the history
  • Loading branch information
Abasing authored Jul 27, 2024
1 parent 44efadd commit b0a731f
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 0 deletions.
60 changes: 60 additions & 0 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.abasing</groupId>
<artifactId>immortality</artifactId>
<name>immortality</name>
<version>1.1</version>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
77 changes: 77 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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>me.abasing</groupId>
<artifactId>immortality</artifactId>
<version>1.1</version>
<packaging>jar</packaging>

<name>immortality</name>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
197 changes: 197 additions & 0 deletions src/main/java/me/abasing/immortality/Immortality.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package me.abasing.immortality;

import java.util.*;
import java.util.stream.Collectors;

import org.bukkit.*;
import org.bukkit.command.*;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.lang.reflect.Method;
import java.util.logging.Level;

public class Immortality extends JavaPlugin implements Listener, TabCompleter {
private final Map<UUID, Boolean> ImmortalityPlayers = new HashMap<>();
private final Map<UUID, Long> cooldowns = new HashMap<>();
private final Map<UUID, Long> activationDelays = new HashMap<>();
private FileConfiguration messagesConfig;

@Override
public void onEnable() {
saveDefaultConfig();
loadMessagesConfig();
Objects.requireNonNull(getCommand("immortality")).setExecutor(new ImmortalityCommandExecutor());
Objects.requireNonNull(getCommand("immortality")).setTabCompleter(this);
getServer().getPluginManager().registerEvents(this, this);
}

@Override
public void onDisable() {
// Plugin shutdown logic
}

private void loadMessagesConfig() {
File messagesFile = new File(getDataFolder(), "messages.yml");
if (!messagesFile.exists()) {
saveResource("messages.yml", false);
}
messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
}

private String getMessage(String key) {
return ChatColor.translateAlternateColorCodes('&', messagesConfig.getString("messages." + key, key));
}

private void toggleImmortality(Player player) {
UUID playerUUID = player.getUniqueId();
boolean ImmortalityEnabled = !ImmortalityPlayers.getOrDefault(playerUUID, false);

// Cooldown logic
if (getConfig().getBoolean("enable-cooldown-time", true) && !player.hasPermission("Immortality.bypass.cooldown")) {
long cooldownTime = getConfig().getInt("cooldown-time", 600) * 1000L;
long lastUsed = cooldowns.getOrDefault(playerUUID, 0L);
if (System.currentTimeMillis() - lastUsed < cooldownTime) {
player.sendMessage(getMessage("cooldownActive"));
return;
}
cooldowns.put(playerUUID, System.currentTimeMillis());
}

// Health threshold logic
if (getConfig().getBoolean("enable-health-threshold", true) && !player.hasPermission("Immortality.bypass.health.requirment")) {
double healthThreshold = getConfig().getDouble("health-threshold", 5.0);
if (player.getHealth() < healthThreshold) {
player.sendMessage(getMessage("healthTooLow"));
return;
}
}

// Activation delay logic
if (getConfig().getBoolean("enable-activation-delay", true) && !player.hasPermission("Immortality.bypass.activation.delay")) {
int delay = getConfig().getInt("activation-delay", 5);
player.sendMessage(getMessage("activationDelayed").replace("%seconds%", String.valueOf(delay)));
activationDelays.put(playerUUID, System.currentTimeMillis() + delay * 1000L);
Bukkit.getScheduler().runTaskLater(this, () -> {
if (System.currentTimeMillis() >= activationDelays.getOrDefault(playerUUID, 0L)) {
setImmortality(player, ImmortalityEnabled);
}
}, delay * 20L);
return;
}

// Environment restrictions logic
if (getConfig().getBoolean("enable-environment-restrictions", true)) {
List<String> allowedWorlds = getConfig().getStringList("environment-restrictions.allowed-worlds");
List<String> allowedBiomes = getConfig().getStringList("environment-restrictions.allowed-biomes");
if (!allowedWorlds.contains(player.getWorld().getName()) || !allowedBiomes.contains(player.getLocation().getBlock().getBiome().name())) {
player.sendMessage(getMessage("environmentRestricted"));
return;
}
}

setImmortality(player, ImmortalityEnabled);
}

private void setImmortality(Player player, boolean enabled) {
UUID playerUUID = player.getUniqueId();
ImmortalityPlayers.put(playerUUID, enabled);
player.sendMessage(enabled ? getMessage("ImmortalityEnabled") : getMessage("ImmortalityDisabled"));
}

private void sendImmortalityList(Player player) {
player.sendMessage(getMessage("ImmortalityListHeader"));
ImmortalityPlayers.entrySet().stream()
.filter(Map.Entry::getValue)
.map(entry -> {
Player targetPlayer = getServer().getPlayer(entry.getKey());
return (targetPlayer != null) ? targetPlayer.getName() : null;
}).filter(Objects::nonNull)
.forEach(playerName -> player.sendMessage(getMessage("ImmortalityListEntry").replace("%player%", playerName)));
}

@EventHandler
public void onEntityDamage(EntityDamageEvent event) {
if (event.getEntity() instanceof Player) {
Player player = (Player) event.getEntity();
UUID playerUUID = player.getUniqueId();
if (ImmortalityPlayers.getOrDefault(playerUUID, false) && player.getHealth() - event.getFinalDamage() <= 0.5D) {
setPlayerHealth(player, Math.max(0.5D, player.getHealth() - event.getFinalDamage() + 0.5D));
event.setDamage(0.0D);
event.setCancelled(true);
}
}
}

private void setPlayerHealth(Player player, double health) {
try {
Method setHealthMethod = player.getClass().getMethod("setHealth", double.class);
setHealthMethod.invoke(player, health);
} catch (Exception e) {
getLogger().log(Level.SEVERE, "Error setting player health", e);
}
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (args.length == 1) {
return Arrays.asList("toggle", "list", "reload").stream()
.filter(option -> option.startsWith(args[0].toLowerCase()))
.collect(Collectors.toList());
}
if (args.length == 2 && args[0].equalsIgnoreCase("toggle")) {
return Bukkit.getOnlinePlayers().stream()
.map(Player::getName)
.filter(name -> name.toLowerCase().startsWith(args[1].toLowerCase()))
.collect(Collectors.toList());
}
return Collections.emptyList();
}

private class ImmortalityCommandExecutor implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
try {
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
if (sender.hasPermission("immortality.reload")) {
reloadConfig();
loadMessagesConfig();
sender.sendMessage(getMessage("reloadSuccess"));
} else {
sender.sendMessage(getMessage("noPermission"));
}
return true;
}

if (sender instanceof Player) {
Player player = (Player) sender;
if (args.length == 1 && args[0].equalsIgnoreCase("list")) {
sendImmortalityList(player);
return true;
}
if (args.length == 2 && args[0].equalsIgnoreCase("toggle")) {
Player targetPlayer = getServer().getPlayer(args[1]);
if (targetPlayer != null) {
toggleImmortality(targetPlayer);
return true;
}
player.sendMessage(getMessage("playerNotFound"));
return true;
}
toggleImmortality(player);
}
return true;
} catch (Exception e) {
sender.sendMessage(ChatColor.RED + "An error occurred while executing the command.");
getLogger().log(Level.SEVERE, "Error executing command", e);
return false;
}
}
}
}
10 changes: 10 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
enable-cooldown-time: true
cooldown-time: 600 # Cooldown in seconds
enable-health-threshold: true
health-threshold: 5.0 # Minimum health required to activate Immortality
enable-activation-delay: true
activation-delay: 5 # Delay in seconds
enable-environment-restrictions: true
environment-restrictions:
allowed-worlds: ["world", "world_nether"]
allowed-biomes: ["FOREST", "PLAINS"]
12 changes: 12 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
messages:
ImmortalityEnabled: "&aImmortality is now enabled for you."
ImmortalityDisabled: "&cImmortality is now disabled for you."
playerNotFound: "&cPlayer not found."
reloadSuccess: "&aImmortality configuration reloaded."
noPermission: "&cYou do not have permission to use this command."
cooldownActive: "&cYou must wait before reactivating Immortality."
healthTooLow: "&cYour health is too low to activate Immortality."
activationDelayed: "&eImmortality will activate in %seconds% seconds."
environmentRestricted: "&cYou cannot activate Immortality in this environment."
ImmortalityListHeader: "&6Players with Immortality enabled:"
ImmortalityListEntry: "&7- &e%player%"
32 changes: 32 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
main: me.abasing.immortality.Immortality
name: Immortality
version: '${project.version}'
authors:
- Abasing
- Zamin Ali
description: A plugin that makes players immortal, preventing them from dying when they would otherwise take fatal damage.
api-version: 1.12

commands:
immortality:
description: Toggles Immortality mode for the player.
usage: /<command> [toggle|list|reload] [player]
aliases: [immortal]
permission: immortality.use

permissions:
immortality.use:
description: Allows the player to use the immortality command.
default: true
immortality.reload:
description: Allows the player to reload the plugin configuration.
default: op
Immortality.bypass.cooldown:
description: Bypasses the cooldown for activating Immortality mode.
default: false
Immortality.bypass.health.requirment:
description: Bypasses the health threshold for activating Immortality mode.
default: false
Immortality.bypass.activation.delay:
description: Bypasses the activation delay for activating Immortality mode.
default: false

0 comments on commit b0a731f

Please sign in to comment.