Add IslandInvite.java
This commit is contained in:
parent
04d5a76ac6
commit
ebf2894ccf
|
|
@ -3,6 +3,7 @@ package com.alttd.cometskyblock.commands.island;
|
|||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.commands.PlayerSubCommand;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import com.alttd.cometskyblock.request.Request;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class IslandAccept extends PlayerSubCommand {
|
||||
|
|
@ -13,6 +14,12 @@ public class IslandAccept extends PlayerSubCommand {
|
|||
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
Request request = islandPlayer.request();
|
||||
if (request == null) {
|
||||
player.sendRichMessage(plugin.messagesConfiguration().get().requests().noPendingRequests());
|
||||
return true;
|
||||
}
|
||||
request.accept();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package com.alttd.cometskyblock.commands.island;
|
|||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.commands.PlayerSubCommand;
|
||||
import com.alttd.cometskyblock.configuration.MessageConfiguration;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import com.alttd.cometskyblock.island.gui.IslandGUI;
|
||||
|
|
@ -19,7 +18,8 @@ public class IslandCommand extends PlayerSubCommand {
|
|||
registerSubCommand(new IslandGo(plugin)); // TODO -- Add some more output
|
||||
registerSubCommand(new IslandSethome(plugin)); // Todo -- Add some more output
|
||||
registerSubCommand(new IslandRestart(plugin)); // TODO -- Add IslandRestartCommand
|
||||
registerSubCommand(new IslandAccept(plugin)); // TODO -- Add IslandAcceptCommand
|
||||
registerSubCommand(new IslandAccept(plugin));
|
||||
registerSubCommand(new IslandDeny(plugin));
|
||||
registerSubCommand(new IslandLeave(plugin)); // TODO -- Add IslandLeaveCommand
|
||||
registerSubCommand(new IslandMembers(plugin)); // TODO -- Add IslandMembersCommand
|
||||
registerSubCommand(new IslandLevel(plugin)); // TODO -- Add IslandLevelCommand
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
package com.alttd.cometskyblock.commands.island;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.commands.PlayerSubCommand;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import com.alttd.cometskyblock.request.Request;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class IslandDeny extends PlayerSubCommand {
|
||||
|
||||
public IslandDeny(CometSkyBlockPlugin plugin) {
|
||||
super(plugin, "accept");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(Player player, IslandPlayer islandPlayer, String[] args) {
|
||||
Request request = islandPlayer.request();
|
||||
if (request == null) {
|
||||
player.sendRichMessage(plugin.messagesConfiguration().get().requests().noPendingRequests());
|
||||
return true;
|
||||
}
|
||||
request.deny();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -3,8 +3,8 @@ package com.alttd.cometskyblock.commands.island;
|
|||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.commands.PlayerSubCommand;
|
||||
import com.alttd.cometskyblock.configuration.MessageConfiguration;
|
||||
import com.alttd.cometskyblock.island.Island;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import com.alttd.cometskyblock.request.InviteRequest;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
|
@ -23,19 +23,19 @@ public class IslandInvite extends PlayerSubCommand {
|
|||
}
|
||||
Player target = Bukkit.getPlayer(args[0]);
|
||||
if (player == target) {
|
||||
// can't invite self
|
||||
player.sendRichMessage(invite.inviteSelf());
|
||||
return true;
|
||||
}
|
||||
if (target == null) {
|
||||
// target offline
|
||||
player.sendRichMessage(invite.targetOffline());
|
||||
return true;
|
||||
}
|
||||
IslandPlayer islandPlayer1 = IslandPlayer.getIslandPlayer(target.getUniqueId());
|
||||
if (islandPlayer1.islandId() == 0) {
|
||||
// target does not have an island
|
||||
if (islandPlayer1.islandId() != 0) {
|
||||
player.sendRichMessage(invite.targetHasIsland());
|
||||
return true;
|
||||
}
|
||||
// send island invite request
|
||||
islandPlayer1.request(new InviteRequest(plugin, player, target));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -97,7 +97,9 @@ public class MessageConfiguration implements Configuration {
|
|||
Invite invite = new Invite();
|
||||
@ConfigSerializable @Getter
|
||||
public static class Invite {
|
||||
|
||||
String inviteSelf = "<red>You can not invite yourself to this island.";
|
||||
String targetOffline = "<red><target> not found, is the player online?";
|
||||
String targetHasIsland = "<red><target> already has an island and can not be invited.";
|
||||
}
|
||||
|
||||
Kick kick = new Kick();
|
||||
|
|
@ -141,4 +143,22 @@ public class MessageConfiguration implements Configuration {
|
|||
|
||||
}
|
||||
|
||||
private Requests requests = new Requests();
|
||||
@ConfigSerializable @Getter
|
||||
public static class Requests {
|
||||
String timedOut = "Your request has timed out!";
|
||||
String noPendingRequests = "You have no pending requests, have they been timed out?";
|
||||
|
||||
private Invite invite = new Invite();
|
||||
|
||||
@ConfigSerializable @Getter
|
||||
public static class Invite {
|
||||
String islandInviteSend = "You have requested <target> to join your island.";
|
||||
String islandInviteReceived = "<requester> has requested you to join their island.<newline>Type <yellow>/island accept</yellow> to accept or <yellow>/island deny</yellow> to deny.";
|
||||
String accept = "<target> has accepted <requester>'s island invite.";
|
||||
String denied = "<target> has denied <requester>'s island invite.";
|
||||
String playerOffline = "This request has denied because both players are no longer online.";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,4 +27,6 @@ public class PluginConfiguration implements Configuration {
|
|||
|
||||
}
|
||||
|
||||
private int requestTimeOut = 30;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -137,6 +137,9 @@ public class Island extends YamlConfiguration {
|
|||
}
|
||||
|
||||
public void addMember(UUID uuid) {
|
||||
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(uuid);
|
||||
islandPlayer.islandId(islandId());
|
||||
islandPlayer.islandUUID(islandUUID());
|
||||
List<UUID> list = members();
|
||||
list.add(uuid);
|
||||
members(list);
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
package com.alttd.cometskyblock.island;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.request.Request;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -32,6 +35,7 @@ public class IslandPlayer extends YamlConfiguration {
|
|||
|
||||
private final File file;
|
||||
private final Object saveLock = new Object();
|
||||
@Getter @Setter private Request request;
|
||||
|
||||
private IslandPlayer(UUID uuid) {
|
||||
super();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
package com.alttd.cometskyblock.request;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class InviteRequest extends Request {
|
||||
|
||||
public InviteRequest(CometSkyBlockPlugin plugin, Player requester, Player target) {
|
||||
super(plugin, requester, target);
|
||||
|
||||
target.sendRichMessage(requests().invite().islandInviteReceived(), placeholders());
|
||||
requester.sendRichMessage(requests().invite().islandInviteSend(), placeholders());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept() {
|
||||
if (!target().isOnline() || !requester().isOnline()) {
|
||||
if (target().isOnline())
|
||||
target().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
if (requester().isOnline())
|
||||
requester().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
target().sendRichMessage(requests().invite().accept(), placeholders());
|
||||
requester().sendRichMessage(requests().invite().accept(), placeholders());
|
||||
IslandPlayer targetIslandPlayer = IslandPlayer.getIslandPlayer(target().getUniqueId());
|
||||
IslandPlayer islandPlayer = IslandPlayer.getIslandPlayer(requester().getUniqueId());
|
||||
targetIslandPlayer.islandId(islandPlayer.islandId());
|
||||
targetIslandPlayer.islandUUID(islandPlayer.islandUUID());
|
||||
super.accept();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deny() {
|
||||
if (!target().isOnline() || !requester().isOnline()) {
|
||||
if (target().isOnline())
|
||||
target().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
if (requester().isOnline())
|
||||
requester().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
target().sendRichMessage(requests().invite().denied(), placeholders());
|
||||
requester().sendRichMessage(requests().invite().denied(), placeholders());
|
||||
super.deny();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package com.alttd.cometskyblock.request;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import com.alttd.cometskyblock.configuration.MessageConfiguration;
|
||||
import com.alttd.cometskyblock.island.IslandPlayer;
|
||||
import lombok.Getter;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class Request {
|
||||
|
||||
private final CometSkyBlockPlugin plugin;
|
||||
@Getter private final Player requester;
|
||||
@Getter private final Player target;
|
||||
private final RequestTimeout timeoutTask;
|
||||
@Getter private MessageConfiguration.Requests requests;
|
||||
|
||||
@Getter private final TagResolver placeholders;
|
||||
|
||||
public Request(CometSkyBlockPlugin plugin, Player requester, Player target) {
|
||||
this.plugin = plugin;
|
||||
this.requester = requester;
|
||||
this.target = target;
|
||||
this.requests = plugin.messagesConfiguration().get().requests();
|
||||
this.timeoutTask = new RequestTimeout(this);
|
||||
|
||||
if (plugin.pluginConfiguration().get().requestTimeOut() > 0) {
|
||||
this.timeoutTask.runTaskLater(plugin,
|
||||
plugin.pluginConfiguration().get().requestTimeOut() * 20L);
|
||||
}
|
||||
|
||||
placeholders = TagResolver.resolver(
|
||||
Placeholder.component("requester", requester().displayName()),
|
||||
Placeholder.component("target", target().displayName())
|
||||
);
|
||||
}
|
||||
|
||||
public void accept() {
|
||||
cancel();
|
||||
}
|
||||
|
||||
public void deny() {
|
||||
cancel();
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
try {
|
||||
timeoutTask.cancel();
|
||||
IslandPlayer.getIslandPlayer(target.getUniqueId()).request(null);
|
||||
} catch (IllegalStateException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
package com.alttd.cometskyblock.request;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
public class RequestTimeout extends BukkitRunnable {
|
||||
private final Request request;
|
||||
|
||||
public RequestTimeout(Request request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (!request.target().isOnline() || !request.requester().isOnline()) {
|
||||
request.cancel();
|
||||
return;
|
||||
}
|
||||
TagResolver placeholders = TagResolver.resolver(
|
||||
Placeholder.component("requester", request.requester().displayName()),
|
||||
Placeholder.component("target", request.target().displayName())
|
||||
);
|
||||
var messagesConfig = CometSkyBlockPlugin.instance().messagesConfiguration().get().requests();
|
||||
request.requester().sendRichMessage(messagesConfig.timedOut(), placeholders);
|
||||
request.target().sendRichMessage(messagesConfig.timedOut(), placeholders);
|
||||
|
||||
request.cancel();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package com.alttd.cometskyblock.request;
|
||||
|
||||
import com.alttd.cometskyblock.CometSkyBlockPlugin;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class RestartRequest extends Request {
|
||||
|
||||
public RestartRequest(CometSkyBlockPlugin plugin, Player requester, Player target) {
|
||||
super(plugin, requester, target);
|
||||
|
||||
requester.sendRichMessage(requests().restart().islandInviteSend(), placeholders());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept() {
|
||||
if (!target().isOnline() || !requester().isOnline()) {
|
||||
if (target().isOnline())
|
||||
target().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
if (requester().isOnline())
|
||||
requester().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
target().sendRichMessage(requests().invite().accept(), placeholders());
|
||||
requester().sendRichMessage(requests().invite().accept(), placeholders());
|
||||
super.accept();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deny() {
|
||||
if (!target().isOnline() || !requester().isOnline()) {
|
||||
if (target().isOnline())
|
||||
target().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
if (requester().isOnline())
|
||||
requester().sendRichMessage(requests().invite().playerOffline());
|
||||
|
||||
cancel();
|
||||
return;
|
||||
}
|
||||
target().sendRichMessage(requests().invite().denied(), placeholders());
|
||||
requester().sendRichMessage(requests().invite().denied(), placeholders());
|
||||
super.deny();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user