Skip to content

Commit

Permalink
Fix #11 (untested)
Browse files Browse the repository at this point in the history
Don't have time to test, but I wanna get what I have done out so that others can test if they want!
  • Loading branch information
srnyx committed Dec 16, 2023
1 parent d85b0a7 commit 7ed93ec
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 112 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
id("com.github.johnrengelman.shadow") version "8.1.1"
}

setupAnnoyingAPI("4.3.1", "xyz.srnyx", "3.0.1", "Each player has a limited number of lives. If you die, you are punished")
setupAnnoyingAPI("9f809c11af", "xyz.srnyx", "3.0.1", "Each player has a limited number of lives. If you die, you are punished")
spigotAPI("1.8.8")
repository(Repository.PLACEHOLDER_API)
repository("https://maven.enginehub.org/repo/")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/xyz/srnyx/limitedlives/LimitedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public LimitedConfig(@NotNull LimitedLives plugin) {
final ConfigurationSection crafting = hasObtaining ? obtaining.getConfigurationSection("crafting") : null;
final boolean hasCrafting = crafting != null;
recipeAmount = hasCrafting ? crafting.getInt("amount", 1) : 1;
recipe = hasCrafting && crafting.getBoolean("enabled", true) ? config.getRecipe("obtaining.crafting.recipe", item -> new ItemData(plugin, item).set(LimitedLives.ITEM_KEY, true).target, null, "life") : null;
recipe = hasCrafting && crafting.getBoolean("enabled", true) ? config.getRecipe("obtaining.crafting.recipe", item -> new ItemData(plugin, item).set(PlayerManager.ITEM_KEY, true).target, null, "life") : null;
}
}
76 changes: 0 additions & 76 deletions src/main/java/xyz/srnyx/limitedlives/LimitedLives.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
package xyz.srnyx.limitedlives;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;

import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import xyz.srnyx.annoyingapi.PluginPlatform;
import xyz.srnyx.annoyingapi.data.EntityData;
import xyz.srnyx.annoyingapi.file.AnnoyingData;
import xyz.srnyx.annoyingapi.file.AnnoyingFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;


public class LimitedLives extends AnnoyingPlugin {
@NotNull public static final String LIVES_KEY = "ll_lives";
@NotNull public static final String DEAD_KEY = "ll_dead";
@NotNull public static final String ITEM_KEY = "ll_item";

@NotNull public LimitedConfig config = new LimitedConfig(this);
@Nullable public WorldGuardManager worldGuard = null;

Expand Down Expand Up @@ -102,71 +93,4 @@ public void enable() {
public void reload() {
config = new LimitedConfig(this);
}

public int getLives(@NotNull Player player) {
final String livesString = new EntityData(this, player).get(LIVES_KEY);
if (livesString != null) try {
return Integer.parseInt(livesString);
} catch (final NumberFormatException e) {
log(Level.WARNING, "&cInvalid lives for &4" + player.getName());
}
return config.livesDefault;
}

@Nullable
public Integer setLives(@NotNull Player player, int amount) {
if (amount > config.livesMax || amount < config.livesMin) return null;
final int oldLives = getLives(player);
new EntityData(this, player).set(LIVES_KEY, amount);
if (oldLives <= config.livesMin && amount > config.livesMin) revive(player);
if (amount == config.livesMin) kill(player, null);
return amount;
}

@Nullable
public Integer addLives(@NotNull Player player, int amount) {
final int oldLives = getLives(player);
final int newLives = oldLives + amount;
if (newLives > config.livesMax) return null;
new EntityData(this, player).set(LIVES_KEY, newLives);
if (oldLives <= config.livesMin && newLives > config.livesMin) revive(player);
return newLives;
}

@Nullable
public Integer removeLives(@NotNull Player player, int amount, @Nullable Player killer) {
int newLives = getLives(player) - amount;
if (newLives < config.livesMin) return null;
new EntityData(this, player).set(LIVES_KEY, newLives);
if (newLives == config.livesMin) kill(player, killer);
return newLives;
}

@Nullable
public Integer withdrawLives(@NotNull Player player, @NotNull Player target, int amount) {
if (config.recipe == null || getLives(player) <= amount) return null;
final ItemStack item = config.recipe.getResult();
item.setAmount(amount);
player.getInventory().addItem(item);
return removeLives(target, amount, null);
}

private void revive(@NotNull Player player) {
new EntityData(this, player).remove(DEAD_KEY);
dispatchCommands(config.commandsRevive, player, null);
}

private void kill(@NotNull Player player, @Nullable Player killer) {
new EntityData(this, player).set(DEAD_KEY, killer != null ? killer.getUniqueId().toString() : "null");
dispatchCommands(config.commandsPunishmentDeath, player, killer);
}

public static void dispatchCommands(@NotNull List<String> commands, @NotNull OfflinePlayer player, @Nullable OfflinePlayer killer) {
for (String command : commands) {
command = command.replace("%player%", player.getName());
if (killer == null && command.contains("%killer%")) continue;
if (killer != null) command = command.replace("%killer%", killer.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/xyz/srnyx/limitedlives/LimitedPlaceholders.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ public String getIdentifier() {
@Override @Nullable
public String onPlaceholderRequest(@Nullable Player player, @NotNull String identifier) {
// lives
if (player != null && identifier.equals("lives")) return String.valueOf(plugin.getLives(player));
if (player != null && identifier.equals("lives")) return String.valueOf(new PlayerManager(plugin, player).getLives());

// lives_PLAYER
if (identifier.startsWith("lives_")) {
final Player target = Bukkit.getPlayerExact(identifier.substring(6));
return target == null ? "N/A" : String.valueOf(plugin.getLives(target));
return target == null ? "N/A" : String.valueOf(new PlayerManager(plugin, target).getLives());
}

// default
if (identifier.equals("default")) return String.valueOf(plugin.config.livesDefault);

// max
if (identifier.equals("max")) return String.valueOf(plugin.config.livesMax);
if (identifier.equals("max")) return String.valueOf(player == null ? plugin.config.livesMax : new PlayerManager(plugin, player).getMaxLives());

// min
if (identifier.equals("min")) return String.valueOf(plugin.config.livesMin);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/xyz/srnyx/limitedlives/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onPlayerDeath(@NotNull PlayerDeathEvent event) {
final boolean isPvp = killer != null && killer != player;

// Remove life
final Integer newLives = plugin.removeLives(player, 1, killer);
final Integer newLives = new PlayerManager(plugin, player).removeLives(1, killer);
if (newLives == null || newLives == plugin.config.livesMin) {
// No more lives
new AnnoyingMessage(plugin, "lives.zero").send(player);
Expand All @@ -61,7 +61,7 @@ public void onPlayerDeath(@NotNull PlayerDeathEvent event) {

// Give life to killer
if (!plugin.config.stealing || !isPvp) return;
final Integer newKillerLives = plugin.addLives(killer, 1);
final Integer newKillerLives = new PlayerManager(plugin, killer).addLives(1);
if (newKillerLives != null) new AnnoyingMessage(plugin, "lives.steal")
.replace("%target%", player.getName())
.replace("%lives%", newKillerLives)
Expand All @@ -72,9 +72,9 @@ public void onPlayerDeath(@NotNull PlayerDeathEvent event) {
public void onPlayerRespawn(@NotNull PlayerRespawnEvent event) {
final Player player = event.getPlayer();
final EntityData data = new EntityData(plugin, player);
final String killerString = data.get(LimitedLives.DEAD_KEY);
final String killerString = data.get(PlayerManager.DEAD_KEY);
if (killerString == null) return;
data.remove(LimitedLives.DEAD_KEY);
data.remove(PlayerManager.DEAD_KEY);
OfflinePlayer killer = null;
if (!killerString.equals("null")) try {
killer = Bukkit.getOfflinePlayer(UUID.fromString(killerString));
Expand All @@ -84,16 +84,16 @@ public void onPlayerRespawn(@NotNull PlayerRespawnEvent event) {
final OfflinePlayer finalKiller = killer;
new BukkitRunnable() {
public void run() {
LimitedLives.dispatchCommands(plugin.config.commandsPunishmentRespawn, player, finalKiller);
PlayerManager.dispatchCommands(plugin.config.commandsPunishmentRespawn, player, finalKiller);
}
}.runTaskLater(plugin, 1);
}

@EventHandler
public void onPlayerItemConsume(@NotNull PlayerItemConsumeEvent event) {
if (plugin.config.recipe == null || !new ItemData(plugin, event.getItem()).has(LimitedLives.ITEM_KEY)) return;
if (plugin.config.recipe == null || !new ItemData(plugin, event.getItem()).has(PlayerManager.ITEM_KEY)) return;
final Player player = event.getPlayer();
final Integer newLives = plugin.addLives(player, plugin.config.recipeAmount);
final Integer newLives = new PlayerManager(plugin, player).addLives(plugin.config.recipeAmount);
if (newLives == null) {
event.setCancelled(true);
new AnnoyingMessage(plugin, "eat.max")
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/xyz/srnyx/limitedlives/PlayerManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package xyz.srnyx.limitedlives;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import xyz.srnyx.annoyingapi.data.EntityData;
import xyz.srnyx.annoyingapi.utility.BukkitUtility;

import java.util.List;
import java.util.logging.Level;


public class PlayerManager {
@NotNull public static final String LIVES_KEY = "ll_lives";
@NotNull public static final String DEAD_KEY = "ll_dead";
@NotNull public static final String ITEM_KEY = "ll_item";

@NotNull private final LimitedLives plugin;
@NotNull private final Player player;
@NotNull private final EntityData data;

public PlayerManager(@NotNull LimitedLives plugin, @NotNull Player player) {
this.plugin = plugin;
this.player = player;
this.data = new EntityData(plugin, player);
}

public int getLives() {
final String livesString = new EntityData(plugin, player).get(LIVES_KEY);
if (livesString != null) try {
return Integer.parseInt(livesString);
} catch (final NumberFormatException e) {
AnnoyingPlugin.log(Level.WARNING, "&cInvalid lives for &4" + player.getName());
}
return plugin.config.livesDefault;
}

public int getMaxLives() {
return BukkitUtility.getPermissionValue(player, "limitedlives.max.", plugin.config.livesMax);
}

@Nullable
public Integer setLives(int amount) {
if (amount < plugin.config.livesMin || amount > getMaxLives()) return null;
final int oldLives = getLives();
data.set(LIVES_KEY, amount);
if (oldLives <= plugin.config.livesMin && amount > plugin.config.livesMin) revive();
if (amount == plugin.config.livesMin) kill(null);
return amount;
}

@Nullable
public Integer addLives(int amount) {
final int oldLives = getLives();
final int newLives = oldLives + amount;
if (newLives > getMaxLives()) return null;
data.set(LIVES_KEY, newLives);
if (oldLives <= plugin.config.livesMin && newLives > plugin.config.livesMin) revive();
return newLives;
}

@Nullable
public Integer removeLives(int amount, @Nullable Player killer) {
int newLives = getLives() - amount;
if (newLives < plugin.config.livesMin) return null;
data.set(LIVES_KEY, newLives);
if (newLives == plugin.config.livesMin) kill(killer);
return newLives;
}

@Nullable
public Integer withdrawLives(@NotNull Player sender, int amount) {
if (plugin.config.recipe == null || getLives() <= amount) return null;
final ItemStack item = plugin.config.recipe.getResult();
item.setAmount(amount);
sender.getInventory().addItem(item);
return new PlayerManager(plugin, player).removeLives(amount, null);
}

private void revive() {
new EntityData(plugin, player).remove(DEAD_KEY);
dispatchCommands(plugin.config.commandsRevive, player, null);
}

private void kill(@Nullable Player killer) {
new EntityData(plugin, player).set(DEAD_KEY, killer != null ? killer.getUniqueId().toString() : "null");
dispatchCommands(plugin.config.commandsPunishmentDeath, player, killer);
}

public static void dispatchCommands(@NotNull List<String> commands, @NotNull Player player, @Nullable OfflinePlayer killer) {
for (String command : commands) {
command = command.replace("%player%", player.getName());
if (killer == null && command.contains("%killer%")) continue;
if (killer != null) command = command.replace("%killer%", killer.getName());
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}
}
}

0 comments on commit 7ed93ec

Please sign in to comment.