Skip to content

Commit

Permalink
Added heartRewardOnElimination option & hearts offline support
Browse files Browse the repository at this point in the history
  • Loading branch information
KartoffelChipss committed May 11, 2024
1 parent 38b6e9e commit e090f17
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>org.strassburger</groupId>
<artifactId>LifeStealZ</artifactId>
<name>LifeStealZ</name>
<version>1.1.11</version>
<version>1.1.12</version>
<description>A LifeSteal SMP plugin providing you all the features you need!</description>
<build>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.strassburger</groupId>
<artifactId>LifeStealZ</artifactId>
<version>1.1.11</version>
<version>1.1.12</version>
<packaging>jar</packaging>

<name>LifeStealZ</name>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/strassburger/lifestealz/LifeStealZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -108,7 +109,10 @@ private PlayerDataStorage createPlayerDataStorage() {
return new SQLitePlayerDataStorage();
}

public static void setMaxHealth(Player player, double maxHealth) {
public static void setMaxHealth(OfflinePlayer offlinePlayer, double maxHealth) {
if (!(offlinePlayer instanceof Player)) return;

Player player = (Player) offlinePlayer;
AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);
if (attribute != null) {
attribute.setBaseValue(maxHealth);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.strassburger.lifestealz.commands;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -37,14 +38,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return false;
}

Player target = Bukkit.getPlayer(targetName);
OfflinePlayer target = Bukkit.getOfflinePlayer(targetName);

if (target == null) {
if (target.getName() == null) {
sender.sendMessage(MessageUtils.getAndFormatMsg(false, "messages.playerNotFound", "&cPlayer not found!"));
return false;
}

PlayerData playerdata = LifeStealZ.getInstance().getPlayerDataStorage().load(target.getUniqueId());

if (playerdata == null) {
sender.sendMessage(MessageUtils.getAndFormatMsg(false, "messages.playerNotFound", "&cPlayer not found!"));
return false;
}

sender.sendMessage(MessageUtils.getAndFormatMsg(true, "messages.viewheartsOther", "&c%player% &7currently has &c%amount% &7hearts!", new Replaceable("%amount%", Integer.toString((int) Math.floor(playerdata.getMaxhp() / 2))), new Replaceable("%player%", target.getName())));
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.strassburger.lifestealz.commands;

import net.kyori.adventure.text.Component;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -132,15 +133,20 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return false;
}

Player targetPlayer = LifeStealZ.getInstance().getServer().getPlayer(targetPlayerName);
OfflinePlayer targetPlayer = LifeStealZ.getInstance().getServer().getOfflinePlayer(targetPlayerName);

if (targetPlayer == null) {
if (targetPlayer.getName() == null) {
throwUsageError(sender);
return false;
}

PlayerData targetPlayerData = playerDataStorage.load(targetPlayer.getUniqueId());

if (targetPlayerData == null) {
sender.sendMessage(MessageUtils.getAndFormatMsg(false, "messages.playerNotFound", "&cPlayer not found!"));
return false;
}

if (optionTwo.equals("get")) {
int hearts = (int) (targetPlayerData.getMaxhp() / 2);
sender.sendMessage(MessageUtils.getAndFormatMsg(true, "messages.getHearts", "&c%player% &7currently has &c%amount% &7hearts!", new Replaceable("%player%", targetPlayer.getName()), new Replaceable("%amount%", hearts + "")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void onPlayerDeath(PlayerDeathEvent event) {
PlayerData playerData = LifeStealZ.getInstance().getPlayerDataStorage().load(player.getUniqueId());

List<String> elimCommands = LifeStealZ.getInstance().getConfig().getStringList("eliminationCommands");
boolean heartRewardOnElimination = LifeStealZ.getInstance().getConfig().getBoolean("heartRewardOnElimination");
boolean disableBanOnElimination = LifeStealZ.getInstance().getConfig().getBoolean("disablePlayerBanOnElimination");
boolean announceElimination = LifeStealZ.getInstance().getConfig().getBoolean("announceElimination");
boolean dropHeartsOnDeath = LifeStealZ.getInstance().getConfig().getBoolean("dropHearts");
Expand Down Expand Up @@ -83,6 +84,7 @@ public void onPlayerDeath(PlayerDeathEvent event) {
String victimIP = getPlayerIP(player);
String killerIP = getPlayerIP(killer);

// Anti alt logic (If killer and victim are on same IP)
if (victimIP != null && victimIP.equals(killerIP) && LifeStealZ.getInstance().getConfig().getBoolean("antiAlt.enabled")) {
if (LifeStealZ.getInstance().getConfig().getBoolean("antiAlt.logAttempt")) LifeStealZ.getInstance().getLogger().info("[ALT WARNING] Player " + killer.getName() + " tried to kill " + player.getName() + " with the same IP address! (Proably an alt account)");
if (LifeStealZ.getInstance().getConfig().getBoolean("antiAlt.sendMessage")) {
Expand All @@ -96,14 +98,16 @@ public void onPlayerDeath(PlayerDeathEvent event) {
// Handle killer gaining hearts
if (dropHeartsOnDeath) world.dropItemNaturally(player.getLocation(), CustomItemManager.createHeart());
else {
if (killerPlayerData.getMaxhp() + 2.0 > maxHearts) {
if (dropHeartsIfMax) world.dropItemNaturally(killer.getLocation(), CustomItemManager.createHeart());
else killer.sendMessage(MessageUtils.getAndFormatMsg(false, "messages.maxHeartLimitReached", "&cYou already reached the limit of %limit% hearts!", new Replaceable("%limit%", (int) maxHearts / 2 + "")));
} else {
killerPlayerData.setMaxhp(killerPlayerData.getMaxhp() + 2.0);
LifeStealZ.getInstance().getPlayerDataStorage().save(killerPlayerData);
LifeStealZ.setMaxHealth(killer, killerPlayerData.getMaxhp());
killer.setHealth(Math.min(killer.getHealth() + 2.0, killerPlayerData.getMaxhp()));
if (playerData.getMaxhp() - 2.0 > minHearts || playerData.getMaxhp() - 2.0 <= minHearts && heartRewardOnElimination) {
if (killerPlayerData.getMaxhp() + 2.0 > maxHearts) {
if (dropHeartsIfMax) world.dropItemNaturally(killer.getLocation(), CustomItemManager.createHeart());
else killer.sendMessage(MessageUtils.getAndFormatMsg(false, "messages.maxHeartLimitReached", "&cYou already reached the limit of %limit% hearts!", new Replaceable("%limit%", (int) maxHearts / 2 + "")));
} else {
killerPlayerData.setMaxhp(killerPlayerData.getMaxhp() + 2.0);
LifeStealZ.getInstance().getPlayerDataStorage().save(killerPlayerData);
LifeStealZ.setMaxHealth(killer, killerPlayerData.getMaxhp());
killer.setHealth(Math.min(killer.getHealth() + 2.0, killerPlayerData.getMaxhp()));
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ worlds:
startHearts: 10
# The maximal amount of hearts, a player can have
maxHearts: 20
# The amount of hp a player should have after getting eliminated
respawnHP: 1
# The minimal amount of hearts. If a player gets to this amount of hearts, they will be eliminated.
# PLEASE ONLY CHANGE IF YOU KNOW WHAT YOU ARE DOING!
minHearts: 0
Expand Down Expand Up @@ -61,8 +63,8 @@ preventCrystalPVP: false

# Only disable this option if you want to add custom commands on elimination and don't want the player to get banned
disablePlayerBanOnElimination: false
# The amount of hp a player should have after getting eliminated
respawnHP: 10
# If the killer should gain a heart on elimination
heartRewardOnElimination: true

# Execute custom commands on events:
# You can use &player& to insert the player name
Expand Down

0 comments on commit e090f17

Please sign in to comment.