Skip to content

Commit

Permalink
Improve everything
Browse files Browse the repository at this point in the history
Added a reload command with permission `payday.reload` (fixes Flibio#12)
Made the plugin listen to sponge reload events
Added a help command with permission `payday.base`
Set up tracking for who's been paid, and avoid paying people more than once in a time period (fixes Flibio#10)
Rewrote config code
Removed time units smaller than 1 second from the config
Improved performance
Improved interaction with Nucleus' API
Refactored almost everything
Incremented version to 1.5.0
  • Loading branch information
Brycey92 committed Feb 27, 2020
1 parent 50cedcf commit 7ca281b
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 194 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'eclipse'

sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.4.0'
version = '1.5.0'

jar {
manifest {
Expand Down
176 changes: 93 additions & 83 deletions src/main/java/io/github/hsyyid/payday/PayDay.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.hsyyid.payday;

import com.google.inject.Inject;
import io.github.hsyyid.payday.commands.PayDayCommand;
import io.github.hsyyid.payday.utils.Utils;
import io.github.nucleuspowered.nucleus.api.NucleusAPI;
import io.github.nucleuspowered.nucleus.api.service.NucleusAFKService;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
Expand All @@ -13,152 +15,160 @@
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.EventContext;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePostInitializationEvent;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.event.service.ChangeServiceProviderEvent;
import org.spongepowered.api.plugin.Dependency;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.api.service.economy.EconomyService;
import org.spongepowered.api.service.economy.account.UniqueAccount;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.UUID;

@Plugin(id = "payday", name = "PayDay", version = "1.4.0", description = "Pay your players as they play.", dependencies = {@Dependency(
@Plugin(id = "payday", name = "PayDay", version = "1.5.0", description = "Pay your players as they play.", dependencies = {@Dependency(
id = "nucleus", optional = true)})
public class PayDay {

public static ConfigurationNode config;
public static ConfigurationLoader<CommentedConfigurationNode> configurationManager;
public static EconomyService economyService;
private static EconomyService economyService;
private static PayDay instance;
private Task task;
private boolean functional = false;
private Optional<NucleusAFKService> afkService = Optional.empty();
private static boolean afkServicePresent = false;
private static NucleusAFKService afkService;

@Inject private Logger logger;

@Inject private PluginContainer container;

public Logger getLogger() {
return logger;
}

@Inject @DefaultConfig(sharedRoot = true) private File dConfig;

@Inject @DefaultConfig(sharedRoot = true) private ConfigurationLoader<CommentedConfigurationNode> confManager;

@Listener
public void onGameInit(GameInitializationEvent event) {
instance = this;
getLogger().info("PayDay loading...");
logger.info("PayDay loading...");

try {
if (!dConfig.exists()) {
dConfig.createNewFile();
config = confManager.load();
confManager.save(config);
}
Utils.loadConfig();

configurationManager = confManager;
config = confManager.load();
} catch (IOException exception) {
getLogger().error("The default configuration could not be loaded or created!");
}
Sponge.getCommandManager().register(this, PayDayCommand.getCommandSpec(), "payday");

// Setup other config options
Utils.getJoinPay();
Utils.enableAfkPay();
createTask();

logger.info("-----------------------------");
logger.info("PayDay was made by HassanS6000!");
logger.info("Patched to APIv5 by Kostronor from the Minecolonies team!");
logger.info("Further updated by Flibio!");
logger.info("Improved by Brycey92!");
logger.info("Please post all errors on the Sponge Thread or on GitHub!");
logger.info("Have fun, and enjoy! :D");
logger.info("-----------------------------");
logger.info("PayDay loaded!");
}

private void createTask() {
Task.Builder taskBuilder = Sponge.getScheduler().createTaskBuilder();

task = taskBuilder.execute(task ->
{
// Reset who's been paid, for all payment amounts
for(List<UUID> array: Utils.paidPlayersMap.values()){
array.clear();
}

for (Player player : Sponge.getServer().getOnlinePlayers()) {
// Check if the player is afk
if (!Utils.enableAfkPay() && afkService.isPresent() && afkService.get().isAFK(player)) {
continue;
}
for (Entry<String, BigDecimal> entry : Utils.getPaymentAmounts().entrySet()) {
if (entry.getKey().equals("*") || player.hasPermission(entry.getKey())) {
BigDecimal pay = entry.getValue();
player.sendMessage(Utils.getSalaryMessage(pay));
UniqueAccount uniqueAccount = economyService.getOrCreateAccount(player.getUniqueId()).get();
uniqueAccount.deposit(economyService.getDefaultCurrency(), pay, Cause.of(EventContext.empty(), container));
if (Utils.enableAfkPay() || ! (afkServicePresent && afkService.isAFK(player))) {
for (Entry<String, BigDecimal> entry : Utils.getPaymentAmounts().entrySet()) {
String key = entry.getKey();

if ((key.equals("*") || player.hasPermission(key)) && !Utils.paidPlayersMap.get(key).contains(player.getUniqueId())) {
BigDecimal pay = entry.getValue();
player.sendMessage(Utils.getSalaryMessage(pay));
UniqueAccount uniqueAccount = economyService.getOrCreateAccount(player.getUniqueId()).get();
uniqueAccount.deposit(economyService.getDefaultCurrency(), pay, Cause.of(EventContext.empty(), container));
Utils.paidPlayersMap.get(key).add(player.getUniqueId());
}
}
}
}
}).interval(Utils.getTimeAmount(), Utils.getTimeUnit()).name("PayDay - Pay").submit(this);

getLogger().info("-----------------------------");
getLogger().info("PayDay was made by HassanS6000!");
getLogger().info("Patched to APIv5 by Kostronor from the Minecolonies team!");
getLogger().info("Further updated by Flibio!");
getLogger().info("Please post all errors on the Sponge Thread or on GitHub!");
getLogger().info("Have fun, and enjoy! :D");
getLogger().info("-----------------------------");
getLogger().info("PayDay loaded!");
}

@Listener
public void onChangeServiceProvider(ChangeServiceProviderEvent event) {
if (Sponge.getPluginManager().getPlugin("nucleus").isPresent()) {
if (event.getService().equals(NucleusAFKService.class)) {
Object raw = event.getNewProviderRegistration().getProvider();
if (raw instanceof NucleusAFKService) {
afkService = Optional.of((NucleusAFKService) raw);
}
}
}
}

@Listener
public void onGamePostInit(GamePostInitializationEvent event) {
Optional<EconomyService> econService = Sponge.getServiceManager().provide(EconomyService.class);

if (econService.isPresent()) {
economyService = econService.get();
functional = true;

// Setup messages
getLogger().info("Initializing messages config!");
Utils.getFirstJoinMessage(BigDecimal.ONE);
Utils.getSalaryMessage(BigDecimal.ONE);
} else {
getLogger().error("Error! There is no Economy plugin found on this server, PayDay will not work correctly!");
}
else {
logger.error("Error! There is no Economy plugin found on this server, PayDay will not work!");
task.cancel();
functional = false;
return;
}

Optional<NucleusAFKService> afkServiceOptional = NucleusAPI.getAFKService();
if (afkServiceOptional.isPresent()) {
afkService = afkServiceOptional.get();
afkServicePresent = true;
}
else {
Utils.warnIfMissingAfkService();
afkServicePresent = false;
}
}

@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event) {
if (!Utils.getJoinPay() || !functional) {
return;
public void onReload(GameReloadEvent event) {
reload();
}

public static void reload() {
Utils.loadConfig();

instance.task.cancel();
instance.createTask();

if(!afkServicePresent) {
Utils.warnIfMissingAfkService();
}
Player player = event.getTargetEntity();

for (Entry<String, BigDecimal> entry : Utils.getPaymentAmounts().entrySet()) {
if (entry.getKey().equals("*") || player.hasPermission(entry.getKey())) {
BigDecimal pay = entry.getValue();
player.sendMessage(Utils.getFirstJoinMessage(pay));
UniqueAccount uniqueAccount = economyService.getOrCreateAccount(player.getUniqueId()).get();
uniqueAccount.deposit(economyService.getDefaultCurrency(), pay, Cause.of(EventContext.empty(), container));
instance.logger.info("Reloaded config!");
}

@Listener
public void onPlayerJoin(ClientConnectionEvent.Join event) {
if (functional && Utils.getJoinPay()) {
Player player = event.getTargetEntity();

for (Entry<String, BigDecimal> entry : Utils.getPaymentAmounts().entrySet()) {
String key = entry.getKey();

if ((key.equals("*") || player.hasPermission(key)) && !Utils.paidPlayersMap.get(key).contains(player.getUniqueId())) {
BigDecimal pay = entry.getValue();
player.sendMessage(Utils.getFirstJoinMessage(pay));
UniqueAccount uniqueAccount = economyService.getOrCreateAccount(player.getUniqueId()).get();
uniqueAccount.deposit(economyService.getDefaultCurrency(), pay, Cause.of(EventContext.empty(), container));
Utils.paidPlayersMap.get(key).add(player.getUniqueId());
}
}
}
}

public static PayDay getInstance() {
return instance;
}
public static PayDay getInstance() { return instance; }

public static ConfigurationLoader<CommentedConfigurationNode> getConfigManager() {
return configurationManager;
}
public static ConfigurationLoader<CommentedConfigurationNode> getConfigManager() { return instance.confManager; }

public static Logger getLogger() { return instance.logger; }

public static EconomyService getEconomyService() { return economyService; }

public static NucleusAFKService getAfkService() { return afkService; }
}
28 changes: 28 additions & 0 deletions src/main/java/io/github/hsyyid/payday/commands/PayDayCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.hsyyid.payday.commands;

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.text.Text;

public class PayDayCommand implements CommandExecutor {
public static CommandSpec getCommandSpec() {
return CommandSpec.builder()
.description(Text.of("PayDay commands"))
.permission("payday.base")
.executor(new PayDayCommand())
.child(ReloadCommand.getCommandSpec(), "reload")
.build();
}

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
src.sendMessage(Text.of("PayDay commands:"));
src.sendMessage(Text.of("/payday reload"));
src.sendMessage(Text.of(" - " + ReloadCommand.description));
return CommandResult.empty();
}
}
31 changes: 31 additions & 0 deletions src/main/java/io/github/hsyyid/payday/commands/ReloadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.hsyyid.payday.commands;

import io.github.hsyyid.payday.PayDay;
import io.github.hsyyid.payday.utils.Utils;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.text.Text;

public class ReloadCommand implements CommandExecutor {
public static final String description = "Reloads the PayDay config";

public static CommandSpec getCommandSpec() {
return CommandSpec.builder()
.description(Text.of(description))
.permission("payday.reload")
.executor(new ReloadCommand())
.build();
}

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
PayDay.reload();
Utils.warnIfMissingAfkService(src);
src.sendMessage(Text.of("Reloaded config!"));
return CommandResult.success();
}
}
Loading

0 comments on commit 7ca281b

Please sign in to comment.