Fix found issues, finish /member command

#69
This commit is contained in:
Norbi Peti 2020-02-15 00:22:34 +01:00
parent 23f3c0f133
commit c947c887a1
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 70 additions and 37 deletions

View file

@ -9,13 +9,9 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.time.Instant; import java.util.concurrent.TimeUnit;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import static buttondevteam.core.MainPlugin.permission; @CommandClass(path = "member", helpText = { //
@CommandClass(modOnly = true, path = "member", helpText = { //
"Member command", // "Member command", //
"Add or remove server members.", // "Add or remove server members.", //
}) })
@ -27,12 +23,12 @@ public class MemberCommand extends ICommand2MC {
this.component = component; this.component = component;
} }
@Command2.Subcommand @Command2.Subcommand(permGroup = Command2.Subcommand.MOD_GROUP)
public boolean add(CommandSender sender, OfflinePlayer player) { public boolean add(CommandSender sender, OfflinePlayer player) {
return addRemove(sender, player, true); return addRemove(sender, player, true);
} }
@Command2.Subcommand @Command2.Subcommand(permGroup = Command2.Subcommand.MOD_GROUP)
public boolean remove(CommandSender sender, OfflinePlayer player) { public boolean remove(CommandSender sender, OfflinePlayer player) {
return addRemove(sender, player, false); return addRemove(sender, player, false);
} }
@ -53,17 +49,25 @@ public class MemberCommand extends ICommand2MC {
} }
@Command2.Subcommand @Command2.Subcommand
public void def(CommandSender sender) { public void def(Player player) {
if(!(sender instanceof Player)) {
sender.sendMessage("§cYou need to be a player to use this command.");
return;
}
Player player= (Player) sender;
String msg; String msg;
if (component.checkMember(player)) if (!component.checkNotMember(player))
msg="You are a member."; msg = "You are a member.";
else { else {
component.getRegTime(player); double pt = component.getPlayTime(player);
long rt = component.getRegTime(player);
if (pt == -1 || rt == -1) {
Boolean result = component.addPlayerAsMember(player);
if (result == null)
msg = "Can't assign member group because groups are not supported by the permissions plugin.";
else if (result)
msg = "You meet all the requirements.";
else
msg = "You should be a member but failed to add you to the group.";
} else
msg = String.format("You need to play for %.2f hours total or play for %d more days to become a member.",
pt, TimeUnit.MILLISECONDS.toDays(rt));
} }
player.sendMessage(msg);
} }
} }

View file

@ -62,37 +62,60 @@ public class MemberComponent extends Component<MainPlugin> implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
if (checkMember(event.getPlayer()) && (checkRegTime(event.getPlayer()) || checkPlayTime(event.getPlayer()))) { if (checkNotMember(event.getPlayer()) && (checkRegTime(event.getPlayer()) || checkPlayTime(event.getPlayer()))) {
try { addPlayerAsMember(event.getPlayer());
if (permission.playerAddGroup(null, event.getPlayer(), memberGroup().get())) {
event.getPlayer().sendMessage("§bYou are a member now. YEEHAW");
MainPlugin.Instance.getLogger().info("Added " + event.getPlayer().getName() + " as a member.");
} else {
MainPlugin.Instance.getLogger().warning("Failed to assign the member role! Please make sure the member group exists or disable the component if it's unused.");
}
} catch (UnsupportedOperationException e) {
MainPlugin.Instance.getLogger().warning("Failed to assign the member role! Groups are not supported by the permissions implementation.");
}
} }
} }
public boolean checkMember(Player player) { public Boolean addPlayerAsMember(Player player) {
try {
if (permission.playerAddGroup(null, player, memberGroup().get())) {
player.sendMessage("§bYou are a member now!");
MainPlugin.Instance.getLogger().info("Added " + player.getName() + " as a member.");
return true;
} else {
MainPlugin.Instance.getLogger().warning("Failed to assign the member role! Please make sure the member group exists or disable the component if it's unused.");
return false;
}
} catch (UnsupportedOperationException e) {
MainPlugin.Instance.getLogger().warning("Failed to assign the member role! Groups are not supported by the permissions implementation.");
return null;
}
}
public boolean checkNotMember(Player player) {
return permission != null && !permission.playerInGroup(player, memberGroup().get()); return permission != null && !permission.playerInGroup(player, memberGroup().get());
} }
public boolean checkRegTime(Player player) { public boolean checkRegTime(Player player) {
return new Date(player.getFirstPlayed()).toInstant().plus(registeredForDays().get(), ChronoUnit.DAYS).isBefore(Instant.now()); return getRegTime(player) == -1;
} }
public boolean checkPlayTime(Player player) { public boolean checkPlayTime(Player player) {
return player.getStatistic(playtime.getKey()) > playtime.getValue() * playedHours().get(); return getPlayTime(player) > playtime.getValue() * playedHours().get();
} }
/**
* Returns milliseconds
*/
public long getRegTime(Player player) { public long getRegTime(Player player) {
Instant date = new Date(player.getFirstPlayed()).toInstant().plus(registeredForDays().get(), ChronoUnit.DAYS); Instant date = new Date(player.getFirstPlayed()).toInstant().plus(registeredForDays().get(), ChronoUnit.DAYS);
if (date.isBefore(Instant.now())) if (date.isAfter(Instant.now()))
return date.toEpochMilli() - Instant.now().toEpochMilli(); return date.toEpochMilli() - Instant.now().toEpochMilli();
return -1; return -1;
} }
public int getPlayTimeTotal(Player player) {
return player.getStatistic(playtime.getKey());
}
/**
* Returns hours
*/
public double getPlayTime(Player player) {
double pt = playedHours().get() - (double) getPlayTimeTotal(player) / playtime.getValue();
if (pt < 0) return -1;
return pt;
}
} }

View file

@ -18,6 +18,8 @@ import org.bukkit.boss.BossBar;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import javax.annotation.Nonnull;
@CommandClass(modOnly = true, path = "schrestart", helpText = { @CommandClass(modOnly = true, path = "schrestart", helpText = {
"Scheduled restart", // "Scheduled restart", //
"This command restarts the server 1 minute after it's executed, warning players every 10 seconds.", // "This command restarts the server 1 minute after it's executed, warning players every 10 seconds.", //
@ -31,6 +33,7 @@ public class ScheduledRestartCommand extends ICommand2MC {
private BukkitTask restarttask; private BukkitTask restarttask;
private volatile BossBar restartbar; private volatile BossBar restartbar;
@Getter @Getter
@Nonnull
private final RestartComponent component; private final RestartComponent component;
@Command2.Subcommand @Command2.Subcommand

View file

@ -60,6 +60,7 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
public boolean hasPermission(CommandSender sender, ICommand2MC command, Method method) { public boolean hasPermission(CommandSender sender, ICommand2MC command, Method method) {
if (sender instanceof ConsoleCommandSender) return true; //Always allow the console if (sender instanceof ConsoleCommandSender) return true; //Always allow the console
if (command == null) return true; //Allow viewing the command - it doesn't do anything anyway
String pg; String pg;
boolean p = true; boolean p = true;
String[] perms = { String[] perms = {
@ -89,9 +90,11 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
* @return The permission group for the subcommand or empty string * @return The permission group for the subcommand or empty string
*/ */
private String permGroup(ICommand2MC command, Method method) { private String permGroup(ICommand2MC command, Method method) {
val sc = method.getAnnotation(Subcommand.class); if (method != null) {
if (sc != null && sc.permGroup().length() > 0) { val sc = method.getAnnotation(Subcommand.class);
return sc.permGroup(); if (sc != null && sc.permGroup().length() > 0) {
return sc.permGroup();
}
} }
if (getAnnForValue(command.getClass(), CommandClass.class, CommandClass::modOnly, false)) if (getAnnForValue(command.getClass(), CommandClass.class, CommandClass::modOnly, false))
return Subcommand.MOD_GROUP; return Subcommand.MOD_GROUP;
@ -131,14 +134,14 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> implemen
/*var cmds = subcommands.values().stream().map(sd -> sd.command).filter(cmd -> plugin.equals(cmd.getPlugin())).toArray(ICommand2MC[]::new); /*var cmds = subcommands.values().stream().map(sd -> sd.command).filter(cmd -> plugin.equals(cmd.getPlugin())).toArray(ICommand2MC[]::new);
for (var cmd : cmds) for (var cmd : cmds)
unregisterCommand(cmd);*/ unregisterCommand(cmd);*/
subcommands.values().removeIf(sd -> plugin.equals(sd.command.getPlugin())); subcommands.values().removeIf(sd -> Optional.ofNullable(sd.command).map(ICommand2MC::getPlugin).map(plugin::equals).orElse(false));
} }
public void unregisterCommands(Component<?> component) { public void unregisterCommands(Component<?> component) {
/*var cmds = subcommands.values().stream().map(sd -> sd.command).filter(cmd -> component.equals(cmd.getComponent())).toArray(ICommand2MC[]::new); /*var cmds = subcommands.values().stream().map(sd -> sd.command).filter(cmd -> component.equals(cmd.getComponent())).toArray(ICommand2MC[]::new);
for (var cmd : cmds) for (var cmd : cmds)
unregisterCommand(cmd);*/ unregisterCommand(cmd);*/
subcommands.values().removeIf(sd -> Optional.ofNullable(sd.command.getComponent()) subcommands.values().removeIf(sd -> Optional.ofNullable(sd.command).map(ICommand2MC::getComponent)
.map(comp -> component.getClass().getSimpleName().equals(comp.getClass().getSimpleName())).orElse(false)); .map(comp -> component.getClass().getSimpleName().equals(comp.getClass().getSimpleName())).orElse(false));
} }