Skip to content

Commit

Permalink
Major update
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Aug 1, 2023
1 parent 874d02c commit 70dfc47
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
workflow_dispatch:

jobs:
build:
publish:
uses: srnyx/tools/.github/workflows/publish.yml@main
with:
modrinth: LvTKDASD
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

Every player has a limited amount of lives. When a player loses all of their lives, they are punished (according to the config). Almost everything is configurable. *Originally made for [Mickaboo](https://youtube.com/@Mickabo)*

- **🐛 Bugs / 💡 Suggestions:** Please [open an issue](https://github.com/srnyx/limited-lives/issues/new/choose) to report a bug or suggest an idea
- **🆘 Support:** Please [join the Discord](https://srnyx.com/discord) to get support
**🐛 Bugs / 💡 Suggestions:** Please [open an issue](https://github.com/srnyx/limited-lives/issues/new/choose) to report a bug or suggest an idea

**🆘 Support:** Please [join the Discord](https://srnyx.com/discord) to get support

## Download

- **✅ Stable:** You can download the latest **stable** version at [Modrinth](https://modrinth.com/plugin/limitedlives), [Hangar](https://hangar.papermc.io/srnyx/LimitedLives), [Spigot](https://spigotmc.org/resources/109078), or [Bukkit](https://dev.bukkit.org/projects/limited-lives)
- **🚧 Snapshot:** You can download the latest **snapshot** version at [actions/workflows/build.yml](https://github.com/srnyx/limited-lives/actions/workflows/build.yml)
**✅ Stable:** You can download the latest **stable** version at [Modrinth](https://modrinth.com/plugin/limitedlives), [Hangar](https://hangar.papermc.io/srnyx/LimitedLives), [Spigot](https://spigotmc.org/resources/109078), or [Bukkit](https://dev.bukkit.org/projects/limited-lives)

**🚧 Snapshot:** You can download the latest **snapshot** version at [actions/workflows/build.yml](https://github.com/srnyx/limited-lives/actions/workflows/build.yml)

# Wiki

Expand Down
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.0.0", "xyz.srnyx", "2.0.1", "Each player has a limited number of lives. If you die, you are punished")
setupAnnoyingAPI("c74bafddda", "xyz.srnyx", "2.0.1", "Each player has a limited number of lives. If you die, you are punished")
spigotAPI("1.8.8")
repository(Repository.PLACEHOLDER_API)
dependencies.compileOnly("me.clip", "placeholderapi", "2.11.3")
4 changes: 2 additions & 2 deletions src/main/java/xyz/srnyx/limitedlives/LimitedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.jetbrains.annotations.Nullable;

import xyz.srnyx.annoyingapi.AnnoyingPlugin;
import xyz.srnyx.annoyingapi.data.ItemData;
import xyz.srnyx.annoyingapi.file.AnnoyingResource;
import xyz.srnyx.annoyingapi.utility.ItemDataUtility;

import java.util.Collections;
import java.util.List;
Expand Down 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 ItemDataUtility(plugin, item).set("ll_item", true).item, null, "life") : null;
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;
}
}
99 changes: 54 additions & 45 deletions src/main/java/xyz/srnyx/limitedlives/LimitedLives.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,57 @@

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.*;
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);
@NotNull public final AnnoyingData data = new AnnoyingData(this, "data.yml");
@Nullable public AnnoyingData oldData = new AnnoyingData(this, "data.yml", new AnnoyingFile.Options<>().canBeEmpty(false));
/**
* player, lives
*/
@NotNull public final Map<UUID, Integer> lives = new HashMap<>();
@Nullable public Map<UUID, Integer> oldLives = new HashMap<>();
/**
* player, killer
*/
@NotNull public final Map<UUID, UUID> deadPlayers = new HashMap<>();
@Nullable public Map<UUID, UUID> oldDeadPlayers = new HashMap<>();

public LimitedLives() {
options
.bStatsOptions(bStatsOptions -> bStatsOptions.id(18304))
.pluginOptions(pluginOptions -> pluginOptions.updatePlatforms(new PluginPlatform.Multi(
PluginPlatform.modrinth("limited-lives"),
PluginPlatform.hangar(this, "srnyx"),
PluginPlatform.spigot("109078"))))
.registrationOptions(registrationOptions -> registrationOptions
.automaticRegistration(automaticRegistration -> automaticRegistration.packages(
"xyz.srnyx.limitedlives.commands",
"xyz.srnyx.limitedlives.listeners"))
.papiExpansionToRegister(() -> new LimitedPlaceholders(this)));

// lives
final ConfigurationSection livesSection = data.getConfigurationSection("lives");
.bStatsOptions(bStatsOptions -> bStatsOptions.id(18304))
.registrationOptions
.automaticRegistration(automaticRegistration -> automaticRegistration.packages(
"xyz.srnyx.limitedlives.commands",
"xyz.srnyx.limitedlives.listeners"))
.papiExpansionToRegister(() -> new LimitedPlaceholders(this));

// oldLives
final ConfigurationSection livesSection = oldData.getConfigurationSection("lives");
if (livesSection != null) for (final String key : livesSection.getKeys(false)) try {
lives.put(UUID.fromString(key), livesSection.getInt(key));
oldLives.put(UUID.fromString(key), livesSection.getInt(key));
} catch (final IllegalArgumentException e) {
log(Level.WARNING, "&cInvalid UUID in &4data.yml&c: &4" + key);
}
if (oldLives.isEmpty()) oldLives = null;

// deadPlayers
final ConfigurationSection deadPlayersSection = data.getConfigurationSection("dead-players");
// oldDeadPlayers
final ConfigurationSection deadPlayersSection = oldData.getConfigurationSection("dead-players");
if (deadPlayersSection != null) for (final String key : deadPlayersSection.getKeys(false)) {
final UUID player;
try {
Expand All @@ -67,73 +77,72 @@ public LimitedLives() {
log(Level.WARNING, "&cInvalid UUID in &4data.yml&c: &4" + killerString);
continue;
}
deadPlayers.put(player, killer);
oldDeadPlayers.put(player, killer);
}
if (oldDeadPlayers.isEmpty()) oldDeadPlayers = null;

// No old data loaded
if (oldLives != null || oldDeadPlayers != null) return;
oldData.delete();
oldData = null;
}

@Override
public void enable() {
if (config.recipe != null) Bukkit.addRecipe(config.recipe);
}

@Override
public void disable() {
data.set("lives", lives.entrySet().stream().collect(HashMap::new, (m, e) -> m.put(e.getKey().toString(), e.getValue()), HashMap::putAll));
data.setSave("dead-players", deadPlayers.entrySet().stream().collect(HashMap::new, (m, e) -> {
final UUID killer = e.getValue();
m.put(e.getKey().toString(), killer == null ? null : killer.toString());
}, HashMap::putAll));
}

@Override
public void reload() {
config = new LimitedConfig(this);
disable();
}

public int getLives(@NotNull UUID uuid) {
return lives.getOrDefault(uuid, config.livesDefault);
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 OfflinePlayer player, int amount) {
public Integer setLives(@NotNull Player player, int amount) {
if (amount > config.livesMax || amount < config.livesMin) return null;
final UUID uuid = player.getUniqueId();
final int oldLives = getLives(uuid);
lives.put(uuid, amount);
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 OfflinePlayer player, int amount) {
final UUID uuid = player.getUniqueId();
final int oldLives = getLives(uuid);
public Integer addLives(@NotNull Player player, int amount) {
final int oldLives = getLives(player);
final int newLives = oldLives + amount;
if (newLives > config.livesMax) return null;
lives.put(uuid, newLives);
new EntityData(this, player).set(LIVES_KEY, newLives);
if (oldLives <= config.livesMin && newLives > config.livesMin) revive(player);
return newLives;
}

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

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

private void kill(@NotNull OfflinePlayer player, @Nullable Player killer) {
deadPlayers.put(player.getUniqueId(), killer != null && player != killer ? killer.getUniqueId() : 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);
}

Expand Down
12 changes: 5 additions & 7 deletions src/main/java/xyz/srnyx/limitedlives/LimitedPlaceholders.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package xyz.srnyx.limitedlives;

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

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

import xyz.srnyx.annoyingapi.AnnoyingPAPIExpansion;
import xyz.srnyx.annoyingapi.utility.BukkitUtility;


public class LimitedPlaceholders extends AnnoyingPAPIExpansion {
Expand All @@ -30,13 +29,12 @@ 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.getUniqueId()));
if (player != null && identifier.equals("lives")) return String.valueOf(plugin.getLives(player));

// lives_<player>
// lives_PLAYER
if (identifier.startsWith("lives_")) {
final OfflinePlayer target = BukkitUtility.getOfflinePlayer(identifier.substring(6));
if (target == null) return "N/A";
return String.valueOf(plugin.getLives(target.getUniqueId()));
final Player target = Bukkit.getPlayerExact(identifier.substring(6));
return target == null ? "N/A" : String.valueOf(plugin.getLives(target));
}

// default
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/xyz/srnyx/limitedlives/commands/LivesCmd.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package xyz.srnyx.limitedlives.commands;

import org.bukkit.OfflinePlayer;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -43,7 +43,7 @@ public void onCommand(@NotNull AnnoyingSender sender) {
// No arguments, get
if (args.length == 0 || (args.length == 1 && sender.argEquals(0, "get"))) {
if (sender.checkPlayer() && sender.checkPermission("limitedlives.get.self")) new AnnoyingMessage(plugin, "get.self")
.replace("%lives%", plugin.getLives(sender.getPlayer().getUniqueId()))
.replace("%lives%", plugin.getLives(sender.getPlayer()))
.send(sender);
return;
}
Expand All @@ -57,14 +57,14 @@ public void onCommand(@NotNull AnnoyingSender sender) {
// get <player>
if (sender.argEquals(0, "get")) {
if (!sender.checkPermission("limitedlives.get.other")) return;
final OfflinePlayer player = BukkitUtility.getOfflinePlayer(args[1]);
final Player player = Bukkit.getPlayer(args[1]);
if (player == null) {
sender.invalidArgument(args[1]);
return;
}
new AnnoyingMessage(plugin, "get.other")
.replace("%target%", player.getName())
.replace("%lives%", plugin.getLives(player.getUniqueId()))
.replace("%lives%", plugin.getLives(player))
.send(sender);
return;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public void onCommand(@NotNull AnnoyingSender sender) {

// <action> <lives> <player>
if (!sender.checkPermission("limitedlives." + action + ".other")) return;
final OfflinePlayer target = BukkitUtility.getOfflinePlayer(args[2]);
final Player target = Bukkit.getPlayer(args[2]);
if (target == null) {
sender.invalidArgument(args[2]);
return;
Expand Down Expand Up @@ -156,9 +156,9 @@ private enum ModificationAction {
ADD(LimitedLives::addLives),
REMOVE((pluginAction, player, lives) -> pluginAction.removeLives(player, lives, null));

@NotNull private final TriFunction<LimitedLives, OfflinePlayer, Integer, Integer> consumer;
@NotNull private final TriFunction<LimitedLives, Player, Integer, Integer> consumer;

ModificationAction(@NotNull TriFunction<LimitedLives, OfflinePlayer, Integer, Integer> consumer) {
ModificationAction(@NotNull TriFunction<LimitedLives, Player, Integer, Integer> consumer) {
this.consumer = consumer;
}

Expand All @@ -168,7 +168,7 @@ public String toString() {
}

@Nullable
public Integer process(@NotNull LimitedLives plugin, @NotNull OfflinePlayer player, int lives) {
public Integer process(@NotNull LimitedLives plugin, @NotNull Player player, int lives) {
return consumer.accept(plugin, player, lives);
}

Expand Down

0 comments on commit 70dfc47

Please sign in to comment.