Skip to content

Commit

Permalink
Released version 1.2 (Added config and command)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceDTRH committed May 6, 2022
1 parent ffe412a commit ad9098f
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 26 deletions.
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<groupId>xyz.alicedtrh</groupId>
<artifactId>safetyblanket</artifactId>
<version>1.1-SNAPSHOT</version>
<version>1.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Safetyblanket</name>

<description>A plugin that prevent mobs targeting new players</description>
<properties>
<java.version>1.8</java.version>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>https://alicedtrh.xyz/plugins/safetyblanket</url>
Expand Down Expand Up @@ -70,6 +70,10 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>redempt.dev</id>
<url>https://redempt.dev</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -90,5 +94,11 @@
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.Redempt</groupId>
<artifactId>RedLib</artifactId>
<version>6.5.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package xyz.alicedtrh.safetyblanket;

import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

public class EarlyExpireSafetyBlanketTask extends BukkitRunnable {

private final Player player;

public EarlyExpireSafetyBlanketTask(Player player) {
this.player = player;
}

/**
* Re-enable enemy spawns.
*/
@Override
public void run() {
if (player.isValid() && player.isOnline() && player.getAffectsSpawning()) {
player.sendMessage("Enemy spawns now occur like normal.");
player.setAffectsSpawning(false);
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/xyz/alicedtrh/safetyblanket/SafetyBlanketConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package xyz.alicedtrh.safetyblanket;

import redempt.redlib.config.annotations.Comment;

public class SafetyBlanketConfig {
@Comment("(c) AliceDTRH 2022")

@Comment("This is used to keep track of the configuration file version, please don't touch it.")
public static String config_version = "1.2.0";

@Comment("This is used to debug the plugin, I suggest you don't touch this unless I specifically ask you to.")
public static boolean DEBUG = false;
@Comment("You can touch things below: ")
@Comment("The amount of time in milliseconds that a user will be considered new to the server. (Default: 900000 - Which means 15 minutes)")
public static int NEW_PLAYER_DURATION = 900000;

@Comment("Should mobs target new users? Enemies will still target new users when attacked in most cases. (Default: true)")
public static boolean PREVENT_TARGETING = true;

@Comment("Should mobs spawns be prevented near new users? (Default: true)")
public static boolean PREVENT_MOB_SPAWNS = true;

@Comment("Should we decrease fall damage for new users? (Default: true)")
public static boolean DECREASE_FALL_DAMAGE = true;

@Comment("How much should we decrease fall damage by? (Default: 0.65 - which means 65%)")
@Comment("Example: 10 half hearts of damage * 0.65 = 6.5 half hearts of damage")
public static double FALL_DAMAGE_REDUCTION_PERCENT = 0.65;

@Comment("Should we give new users an ambient regeneration effect? (Default: true)")
public static boolean REGEN_BOOST = true;

@Comment("Should we re-enable mob spawns early? (Default: true)")
public static boolean EARLY_MOB_SPAWN_DISABLE = true;

@Comment("How far along the new user path should we re-enable mob spawns? (Default: 0.5 - which means 50%)")
public static double EARLY_MOB_SPAWN_DISABLE_PERCENT = 0.5;

@Comment("Disable the nag about using /reload (Default: false)")
public static boolean I_WONT_COMPLAIN_WHEN_EVERYTHING_BREAKS = false;


}
14 changes: 14 additions & 0 deletions src/main/java/xyz/alicedtrh/safetyblanket/Safetyblanket.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
import org.bstats.charts.SingleLineChart;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import redempt.redlib.commandmanager.CommandHook;
import redempt.redlib.commandmanager.CommandParser;
import redempt.redlib.config.ConfigManager;

import java.util.logging.Logger;

Expand All @@ -24,14 +29,23 @@ public final class Safetyblanket extends JavaPlugin {
public void onEnable() {
//Register events
Bukkit.getPluginManager().registerEvents(new SafetyblanketEvents(), this);

int pluginId = 15141;
this.metrics = new Metrics(this, pluginId);

ConfigManager config = ConfigManager.create(this).target(SafetyBlanketConfig.class).saveDefaults().load();
new CommandParser(this.getResource("command.rdcml")).parse().register("safetyblanket", this);
}

@Override
public void onDisable() {
metrics.addCustomChart(new SingleLineChart("amount_of_people_blanketed", () -> blankets));
}

@CommandHook("disableuserblanket")
public void onCommandSafetyBlanket(CommandSender sender, Player arg1) {
new ExpireSafetyBlanketTask(arg1).run();
sender.sendMessage("Forcefully expired user blanket.");
}

}
81 changes: 57 additions & 24 deletions src/main/java/xyz/alicedtrh/safetyblanket/SafetyblanketEvents.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package xyz.alicedtrh.safetyblanket;

import org.bukkit.attribute.AttributeInstance;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -9,23 +11,20 @@
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.EntityTargetLivingEntityEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH;
import static org.bukkit.event.entity.EntityTargetEvent.TargetReason.*;
import static xyz.alicedtrh.safetyblanket.SafetyBlanketConfig.*;


public class SafetyblanketEvents implements Listener {
Safetyblanket plugin = Safetyblanket.getPlugin(Safetyblanket.class);
/**
* The amount of time in milliseconds that a player is considered new to the server.
*/
public static final int NEW_PLAYER_DURATION = 900000;

/**
* Reasons entities should NOT ignore
Expand All @@ -34,11 +33,31 @@ public class SafetyblanketEvents implements Listener {
TARGET_ATTACKED_ENTITY, TARGET_ATTACKED_OWNER, TARGET_ATTACKED_NEARBY_ENTITY, COLLISION, CUSTOM
};

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerCommandPreprocessEvent(PlayerCommandPreprocessEvent event) {
if (I_WONT_COMPLAIN_WHEN_EVERYTHING_BREAKS) {
return;
}
if (event.getMessage().equalsIgnoreCase("/reload") || event.getMessage().equalsIgnoreCase("/reload confirm")) {
Bukkit.getServer().getOperators().forEach(offlinePlayer -> {
Player player = offlinePlayer.getPlayer();
if (player != null && player.isOnline() && player.isOp()) {
player.sendMessage(Component.text("[SafetyBlanket] Reload is not supported.", TextColor.color(137, 0, 0)));
}
});
}


}

/**
* Prevent enemies from targeting players under blanket
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityTargetLivingEntityEvent(@NotNull EntityTargetLivingEntityEvent event) {
if (!PREVENT_TARGETING) {
return;
}
@Nullable LivingEntity target = event.getTarget();
if (target == null || !target.isValid()) return;

Expand All @@ -59,6 +78,10 @@ public void onEntityTargetLivingEntityEvent(@NotNull EntityTargetLivingEntityEve
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityTargetEvent(@NotNull EntityTargetEvent event) {
if (!PREVENT_TARGETING) {
return;
}

@Nullable Entity target = event.getTarget();
if (target == null || !target.isValid()) return;

Expand All @@ -79,6 +102,10 @@ public void onEntityTargetEvent(@NotNull EntityTargetEvent event) {
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityDamageEvent(@NotNull EntityDamageEvent event) {
if (!DECREASE_FALL_DAMAGE) {
return;
}

if (event.getEntityType() != EntityType.PLAYER) {
return;
}
Expand All @@ -91,31 +118,26 @@ public void onEntityDamageEvent(@NotNull EntityDamageEvent event) {
return;
}

double playerMaxHealth = 20.0;
AttributeInstance playerMaxHealthAttribute = player.getAttribute(GENERIC_MAX_HEALTH);
event.setDamage(Math.floor(event.getFinalDamage() * FALL_DAMAGE_REDUCTION_PERCENT));
player.sendMessage("You took less fall damage because you're new to the server. Please be careful.");

if (playerMaxHealthAttribute != null) {
playerMaxHealth = playerMaxHealthAttribute.getValue();
}

if (event.getFinalDamage() <= (playerMaxHealth / 2)) {
event.setDamage(Math.floor(event.getFinalDamage() * 0.65));
player.sendMessage("You took less fall damage because you're new to the server. Please be careful.");
}
}

/**
* Ensure monsters attack players under blanket, when attacked by said player.
*/
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDamageByEntityEvent(@NotNull EntityDamageByEntityEvent event) {
if(!(event.getEntity() instanceof Monster)) {
if (!PREVENT_TARGETING) {
return;
}
if (!(event.getEntity() instanceof Monster)) {
return;
}
Monster monster = (Monster) event.getEntity();

if(event.getDamager() instanceof Player && isPlayerNew((Player) event.getDamager())) {
monster.setTarget((Player)event.getDamager());
if (event.getDamager() instanceof Player && isPlayerNew((Player) event.getDamager())) {
monster.setTarget((Player) event.getDamager());
}

}
Expand Down Expand Up @@ -148,14 +170,25 @@ private void addSafetyBlanket(@NotNull Player player) {

new ExpireSafetyBlanketTask(player).runTaskLater(plugin, timeUntilRegular(player, TimeUnit.TICKS));
player.getPersistentDataContainer().set(Safetyblanket.HAS_NEW_PLAYER_EFFECTS, PersistentDataType.SHORT, (short) 1);
if(!player.hasPlayedBefore()) {
player.sendMessage("Enemies won't target you for "+timeUntilRegular(player, TimeUnit.MINUTES)+" minutes, unless you attack them first.");
player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, timeUntilRegular(player, TimeUnit.TICKS), 0, true, true, false));
if (!player.hasPlayedBefore()) {
if (PREVENT_TARGETING) {
player.sendMessage("Enemies won't target you for " + timeUntilRegular(player, TimeUnit.MINUTES) + " minutes, unless you attack them first.");
}
if (REGEN_BOOST) {
player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, timeUntilRegular(player, TimeUnit.TICKS), 0, true, true, false));
}
}
if (REGEN_BOOST) {
player.sendMessage("You have received a REGENERATION boost because you're new to this server.");
}
player.sendMessage("You have received a REGENERATION boost because you're new to this server.");
player.sendMessage("Use this time to get a base with a bed and a source of food going.");

player.setAffectsSpawning(false);
if (PREVENT_MOB_SPAWNS) {
player.setAffectsSpawning(false);
if (EARLY_MOB_SPAWN_DISABLE) {
new EarlyExpireSafetyBlanketTask(player).runTaskLater(plugin, (long) (timeUntilRegular(player, TimeUnit.TICKS) * EARLY_MOB_SPAWN_DISABLE_PERCENT));
}
}
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/command.rdcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
disableuserblanket player:arg {
permission safetyblanket.admin
help This lets you forcefully disable the blanket for a user.
hook disableuserblanket
}

0 comments on commit ad9098f

Please sign in to comment.