Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate elimination and revive commands from main command #208

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import dev.piotrulla.craftinglib.CraftingException;
import dev.piotrulla.craftinglib.CraftingLib;
Expand All @@ -12,8 +13,12 @@
import eu.okaeri.configs.yaml.bukkit.YamlBukkitConfigurer;
import io.github.zrdzn.minecraft.greatlifesteal.command.LifeStealCommand;
import io.github.zrdzn.minecraft.greatlifesteal.command.LifeStealTabCompleter;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationCommand;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationCommandCompleter;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationFacade;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationFacadeFactory;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveCommand;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveCommandCompleter;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveRestoreHealthListener;
import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartFacade;
import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartItem;
Expand All @@ -27,11 +32,14 @@
import io.github.zrdzn.minecraft.greatlifesteal.update.UpdateListener;
import io.github.zrdzn.minecraft.greatlifesteal.update.UpdateNotifier;
import io.github.zrdzn.minecraft.greatlifesteal.user.UserListener;
import io.github.zrdzn.minecraft.greatlifesteal.user.UserNameCache;
import io.github.zrdzn.minecraft.greatlifesteal.user.UserNameListener;
import org.apache.log4j.BasicConfigurator;
import org.bstats.bukkit.Metrics;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.slf4j.Logger;
Expand All @@ -42,6 +50,7 @@ public class GreatLifeStealPlugin extends JavaPlugin {
private final Logger logger = LoggerFactory.getLogger(GreatLifeStealPlugin.class);

private HeartItem heartItem;
private UserNameCache userNameCache;

public static String formatColor(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
Expand Down Expand Up @@ -109,14 +118,35 @@ public void onEnable() {
ReviveRestoreHealthListener reviveRestoreHealthListener = new ReviveRestoreHealthListener(this, config,
eliminationFacade, spigotServer.getDamageableAdapter());

// Register user name cache.
Set<String> usersNames = server.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toSet());
this.userNameCache = new UserNameCache(usersNames);
UserNameListener userNameListener = new UserNameListener(this.userNameCache);
pluginManager.registerEvents(userNameListener, this);

pluginManager.registerEvents(updateListener, this);
pluginManager.registerEvents(userListener, this);
pluginManager.registerEvents(reviveRestoreHealthListener, this);
pluginManager.registerEvents(heartUseListener, this);

PluginCommand lifeStealCommand = this.getCommand("lifesteal");
lifeStealCommand.setExecutor(new LifeStealCommand(this, config, eliminationFacade, damageableAdapter, spigotServer, this.heartItem));
lifeStealCommand.setTabCompleter(new LifeStealTabCompleter(config));
lifeStealCommand.setExecutor(new LifeStealCommand(this, config, damageableAdapter, spigotServer, this.heartItem));
lifeStealCommand.setTabCompleter(new LifeStealTabCompleter(config, this.userNameCache));

PluginCommand eliminateCommand = this.getCommand("eliminate");
eliminateCommand.setExecutor(new EliminationCommand(this, config, eliminationFacade));
eliminateCommand.setTabCompleter(new EliminationCommandCompleter(config, this.userNameCache));

PluginCommand reviveCommand = this.getCommand("revive");
reviveCommand.setExecutor(new ReviveCommand(this, config, eliminationFacade));
reviveCommand.setTabCompleter(new ReviveCommandCompleter(config, this.userNameCache));
}

@Override
public void onDisable() {
if (this.userNameCache != null) {
this.userNameCache.clear();
}
}

public void loadConfigurations(PluginConfig config, SpigotServer spigotServer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import io.github.zrdzn.minecraft.greatlifesteal.GreatLifeStealPlugin;
import io.github.zrdzn.minecraft.greatlifesteal.PluginConfig;
import io.github.zrdzn.minecraft.greatlifesteal.action.ActionType;
import io.github.zrdzn.minecraft.greatlifesteal.action.ActionConfig;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.Elimination;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationConfig;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationException;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationFacade;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveConfig;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveStatus;
import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartDropLocation;
import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartItem;
import io.github.zrdzn.minecraft.greatlifesteal.message.MessageFacade;
import io.github.zrdzn.minecraft.greatlifesteal.spigot.DamageableAdapter;
import io.github.zrdzn.minecraft.greatlifesteal.spigot.SpigotServer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.Command;
Expand All @@ -29,7 +20,6 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,23 +29,19 @@ public class LifeStealCommand implements CommandExecutor {

private final GreatLifeStealPlugin plugin;
private final PluginConfig config;
private final EliminationFacade eliminationFacade;
private final DamageableAdapter adapter;
private final HeartItem heartItem;
private final SpigotServer spigotServer;
private final Server server;
private final BukkitScheduler scheduler;

public LifeStealCommand(GreatLifeStealPlugin plugin, PluginConfig config, EliminationFacade eliminationFacade,
DamageableAdapter adapter, SpigotServer spigotServer, HeartItem heartItem) {
public LifeStealCommand(GreatLifeStealPlugin plugin, PluginConfig config, DamageableAdapter adapter,
SpigotServer spigotServer, HeartItem heartItem) {
this.plugin = plugin;
this.config = config;
this.eliminationFacade = eliminationFacade;
this.adapter = adapter;
this.heartItem = heartItem;
this.spigotServer = spigotServer;
this.server = plugin.getServer();
this.scheduler = this.server.getScheduler();
}

@Override
Expand Down Expand Up @@ -207,13 +193,13 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
break;
case "lives": {
if (args.length == 1) {
MessageFacade.send(sender, this.config.getMessages().getNoActionSpecified());
MessageFacade.send(sender, this.config.getMessages().getNoEliminationSpecified());
return true;
}

ActionConfig action = this.config.getActions().get(args[1]);
if (action == null) {
MessageFacade.send(sender, this.config.getMessages().getNoActionEnabled());
EliminationConfig elimination = this.config.getEliminations().get(args[1]);
if (elimination == null) {
MessageFacade.send(sender, this.config.getMessages().getEliminationDoesNotExist());
return true;
}

Expand All @@ -238,7 +224,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}

double requiredHealth = action.getActivateAtHealth();
double requiredHealth = elimination.getActivateAtHealth();
double playerHealth = this.adapter.getMaxHealth(target);
int lives = 0;

Expand Down Expand Up @@ -339,132 +325,6 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

break;
}
case "eliminate": {
if (!sender.hasPermission("greatlifesteal.command.eliminate")) {
MessageFacade.send(sender, this.config.getMessages().getNoPermissions());
return true;
}

if (args.length == 1) {
MessageFacade.send(sender, this.config.getMessages().getNoActionSpecified());
return true;
}

String eliminationKey = args[1];

EliminationConfig eliminationConfig = this.config.getEliminations().get(eliminationKey);
if (eliminationConfig == null) {
MessageFacade.send(sender, this.config.getMessages().getNoActionEnabled());
return true;
}

if (args.length == 2) {
MessageFacade.send(sender, this.config.getMessages().getPlayerIsInvalid());
return true;
}

Player victim = this.server.getPlayer(args[2]);
if (victim == null) {
MessageFacade.send(sender, this.config.getMessages().getPlayerIsInvalid());
return true;
}

int senderHealth = 0;
if (sender instanceof Player) {
senderHealth = (int) ((Player) sender).getMaxHealth();
}

String victimName = victim.getName();

String[] placeholders = {
"{player}", victimName,
"{victim}", victimName,
"{killer}", sender.getName(),
"{victim_max_health}", String.valueOf((int) victim.getMaxHealth()),
"{killer_max_health}", String.valueOf(senderHealth),
};

this.scheduler.runTaskAsynchronously(this.plugin, () -> {
try {
Optional<Elimination> eliminationMaybe = this.eliminationFacade.findEliminationByPlayerUuid(victim.getUniqueId());
if (eliminationMaybe.isPresent()) {
MessageFacade.send(sender, this.config.getMessages().getPlayerIsAlreadyEliminated(), "{PLAYER}", victim.getName());
return;
}

this.eliminationFacade.createElimination(victim.getUniqueId(), victimName, eliminationKey, victim.getWorld().getName());
} catch (EliminationException exception) {
this.logger.error("Could not find or create an elimination.", exception);
MessageFacade.send(sender, this.config.getMessages().getCouldNotEliminate());
return;
}

this.scheduler.runTask(this.plugin, () ->
eliminationConfig.getCommands().forEach(eliminationCommand -> {
eliminationCommand = MessageFacade.formatPlaceholders(eliminationCommand, placeholders);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), eliminationCommand);
})
);
});

break;
}
case "revive": {
if (!sender.hasPermission("greatlifesteal.command.revive")) {
MessageFacade.send(sender, this.config.getMessages().getNoPermissions());
return true;
}

if (args.length == 1) {
MessageFacade.send(sender, this.config.getMessages().getNoActionSpecified());
return true;
}

String reviveKey = args[1];

ReviveConfig revive = this.config.getRevives().get(reviveKey);
if (revive == null) {
MessageFacade.send(sender, this.config.getMessages().getReviveDoesNotExist());
return true;
}

if (args.length == 2) {
MessageFacade.send(sender, this.config.getMessages().getPlayerIsInvalid());
return true;
}

String victimName = args[2];

this.scheduler.runTaskAsynchronously(this.plugin, () -> {
try {
Optional<Elimination> eliminationMaybe = this.eliminationFacade.findEliminationByPlayerName(victimName);
if (!eliminationMaybe.isPresent()) {
MessageFacade.send(sender, this.config.getMessages().getPlayerIsNotEliminated(), "{PLAYER}", victimName);
return;
}

// Execute all revive-related commands but do not remove the elimination from the database yet.
boolean statusChanged = this.eliminationFacade.updateReviveByPlayerName(victimName, ReviveStatus.COMPLETED);
if (statusChanged) {
this.scheduler.runTask(this.plugin, () ->
revive.getCommands().getInitial().forEach(reviveCommand -> {
reviveCommand = MessageFacade.formatPlaceholders(reviveCommand, "{victim}", victimName);
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), reviveCommand);
})
);

return;
}

MessageFacade.send(sender, this.config.getMessages().getPlayerIsAlreadyRevived(), "{PLAYER}", victimName);
} catch (EliminationException exception) {
this.logger.error("Could not find or update an elimination.", exception);
MessageFacade.send(sender, this.config.getMessages().getCouldNotRevive());
}
});

break;
}
default:
MessageFacade.send(sender, this.config.getMessages().getCommandUsage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import io.github.zrdzn.minecraft.greatlifesteal.PluginConfig;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationConfig;
import org.bukkit.Bukkit;
import io.github.zrdzn.minecraft.greatlifesteal.user.UserNameCache;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;

public class LifeStealTabCompleter implements TabCompleter {

private final PluginConfig config;
private final UserNameCache userNameCache;

public LifeStealTabCompleter(PluginConfig config) {
public LifeStealTabCompleter(PluginConfig config, UserNameCache userNameCache) {
this.config = config;
this.userNameCache = userNameCache;
}

@Override
Expand Down Expand Up @@ -45,14 +45,6 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
sender.hasPermission("greatlifesteal.command.withdraw")) {
this.add("withdraw");
}

if (sender.hasPermission("greatlifesteal.command.eliminate")) {
this.add("eliminate");
}

if (sender.hasPermission("greatlifesteal.command.revive")) {
this.add("revive");
}
}
};
}
Expand All @@ -78,24 +70,18 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}
};
} else if (args.length == 3) {
return Bukkit.getServer().getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.toList());
return new ArrayList<>(this.userNameCache.getUsersNames());
} else if (args.length == 4) {
return Collections.singletonList(String.valueOf((int) defaultHealth));
}
break;
case "lives":
case "eliminate":
case "revive":
return this.getEliminationCompletion(args);
case "withdraw":
if (args.length == 2) {
return Collections.singletonList("1");
} else if (args.length == 3) {
return Bukkit.getServer().getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.toList());
return new ArrayList<>(this.userNameCache.getUsersNames());
}
break;
default:
Expand All @@ -114,9 +100,7 @@ private List<String> getEliminationCompletion(String[] args) {
return Collections.emptyList();
}

return Bukkit.getServer().getOnlinePlayers().stream()
.map(Player::getName)
.collect(Collectors.toList());
return new ArrayList<>(this.userNameCache.getUsersNames());
}

return Collections.emptyList();
Expand Down
Loading
Loading