Skip to content

Commit

Permalink
feat(resubmit): Support resubmit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Jan 27, 2024
1 parent 3ec99bf commit ebca144
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public CompletableFuture<Boolean> updateRequest(@NotNull RequestInformation requ
public @NotNull Map<Integer, RequestInformation> queryRequests(@Nullable Consumer<@NotNull TableQueryBuilder> conditions) throws SQLException {
TableQueryBuilder builder = DataTables.REQUESTS.createQuery();
if (conditions != null) conditions.accept(builder);
return builder.build().executeFunction(query -> readRequests(query.getResultSet()), new HashMap<>());
return builder.build().executeFunction(query -> readRequests(query.getResultSet()), new LinkedHashMap<>());
}

private @NotNull RequestInformation readRequest(ResultSet rs) throws SQLException {
Expand All @@ -122,7 +122,7 @@ public CompletableFuture<Boolean> updateRequest(@NotNull RequestInformation requ
}

private @NotNull Map<Integer, RequestInformation> readRequests(ResultSet resultSet) throws SQLException {
Map<Integer, RequestInformation> requests = new HashMap<>();
Map<Integer, RequestInformation> requests = new LinkedHashMap<>();
while (resultSet.next()) {
int requestID = resultSet.getInt("id");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.artformgames.plugin.votepass.api.user.UserKey;
import com.artformgames.plugin.votepass.lobby.api.data.user.PendingRequest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;

Expand All @@ -13,4 +14,6 @@ public interface UserRequestManager {

@NotNull CompletableFuture<Void> update(@NotNull RequestInformation content);

@NotNull CompletableFuture<@Nullable RequestInformation> lastFailed(@NotNull UserKey key, @NotNull String server);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import cc.carm.lib.easyplugin.command.SimpleCompleter;
import cc.carm.lib.easyplugin.command.SubCommand;
import com.artformgames.plugin.votepass.api.data.request.RequestInformation;
import com.artformgames.plugin.votepass.lobby.Main;
import com.artformgames.plugin.votepass.lobby.VotePassLobbyAPI;
import com.artformgames.plugin.votepass.lobby.api.data.server.ServerSettings;
import com.artformgames.plugin.votepass.lobby.api.data.user.PendingRequest;
import com.artformgames.plugin.votepass.lobby.api.user.LobbyUserData;
import com.artformgames.plugin.votepass.lobby.command.MainCommand;
import com.artformgames.plugin.votepass.lobby.conf.PluginMessages;
import com.artformgames.plugin.votepass.lobby.ui.RequestingGUI;
import com.artformgames.plugin.votepass.lobby.ui.ResubmitGUI;
import com.artformgames.plugin.votepass.lobby.ui.RulesBookUI;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -44,17 +46,25 @@ public Void execute(JavaPlugin plugin, CommandSender sender, String[] args) thro
if (settings.rules() != null) {
PluginMessages.RULES.send(player, settings.name());
RulesBookUI.open(player, settings.id(), settings.rules());
} else {
PendingRequest pendingRequest = requestData.getPendingRequest();
if (pendingRequest != null && pendingRequest.getSettings().equals(settings)) {
RequestingGUI.open(player, requestData, pendingRequest);
} else {
PluginMessages.ACCEPTED.send(player, settings.name());
RequestingGUI.open(player, requestData, requestData.createPendingRequest(settings));
}

return null;
}
PendingRequest pendingRequest = requestData.getPendingRequest();
if (pendingRequest != null && pendingRequest.getSettings().equals(settings)) {
RequestingGUI.open(player, requestData, pendingRequest);
return null;
}

PluginMessages.ACCEPTED.send(player, settings.name());
Main.getInstance().getRequestManager()
.lastFailed(requestData.getKey(), settings.id())
.thenAccept(lastFailed -> Main.getInstance().getScheduler().run(() -> {
if (lastFailed == null) {
RequestingGUI.open(player, requestData, requestData.createPendingRequest(settings));
} else {
ResubmitGUI.open(player, requestData, settings, lastFailed);
}
}));

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package com.artformgames.plugin.votepass.lobby.conf;

import cc.carm.lib.configuration.core.ConfigurationRoot;
import cc.carm.lib.configuration.core.Configuration;
import cc.carm.lib.configuration.core.annotation.HeaderComment;
import cc.carm.lib.configuration.core.value.type.ConfiguredList;
import cc.carm.lib.configuration.core.value.type.ConfiguredValue;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage;
import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import org.bukkit.Material;

public class PluginConfig extends ConfigurationRoot {
public interface PluginConfig extends Configuration {

public static final class SERVERS extends ConfigurationRoot {
interface SERVERS extends Configuration {

public static final ConfiguredValue<String> FOLDER_NAME = ConfiguredValue.of(String.class, "servers");
ConfiguredValue<String> FOLDER_NAME = ConfiguredValue.of(String.class, "servers");

public static final ConfiguredList<String> DISABLED = ConfiguredList.of(String.class);
ConfiguredList<String> DISABLED = ConfiguredList.of(String.class);

}

public static final class RULES extends ConfigurationRoot {
interface RULES extends Configuration {

public static final class BOOK extends ConfigurationRoot {
interface BOOK extends Configuration {

public static final ConfiguredMessageList<String> ACCEPT = ConfiguredMessageList.asStrings()
ConfiguredMessageList<String> ACCEPT = ConfiguredMessageList.asStrings()
.defaults("[&a&l[ACCEPT THE RULE]](hover=&fClick to &a&lconfirm and accept the server rules&f. run_command=/votepass accept %(server))")
.params("server")
.build();

public static final ConfiguredMessageList<String> DENY = ConfiguredMessageList.asStrings()
ConfiguredMessageList<String> DENY = ConfiguredMessageList.asStrings()
.defaults("[&c&l[DENY THE RULE]](hover=&fClick to &c&lrefuse the server rules&f. run_command=/votepass deny %(server))")
.params("server")
.build();
Expand All @@ -36,31 +37,32 @@ public static final class BOOK extends ConfigurationRoot {
}


public static final class ANSWERING extends ConfigurationRoot {
interface ANSWERING extends Configuration {

public static final ConfiguredMessage<String> TITLE = ConfiguredMessage.asString()
ConfiguredMessage<String> TITLE = ConfiguredMessage.asString()
.defaults("&a&lRequest &8| &f%(server)")
.params("server")
.build();


public static final class BOOK_ITEM extends ConfigurationRoot {
interface BOOK_ITEM extends Configuration {

public static final ConfiguredValue<Integer> SLOT = ConfiguredValue.of(Integer.class, 8);
@HeaderComment("Which slot should we put the answering book in players inventory? (0~8)")
ConfiguredValue<Integer> SLOT = ConfiguredValue.of(Integer.class, 8);

public static final ConfiguredMessage<String> NAME = ConfiguredMessage.asString()
ConfiguredMessage<String> NAME = ConfiguredMessage.asString()
.defaults("&c&lAnswering book &8(Right click)")
.build();

public static final ConfiguredMessageList<String> LORE = ConfiguredMessageList.asStrings()
ConfiguredMessageList<String> LORE = ConfiguredMessageList.asStrings()
.defaults("&fOpen and answer your questions!")
.build();

}

public static final class ITEMS extends ConfigurationRoot {
interface ITEMS extends Configuration {

public static final ConfiguredItem REQUIRED = ConfiguredItem.create()
ConfiguredItem REQUIRED = ConfiguredItem.create()
.defaultType(Material.WRITABLE_BOOK)
.defaultName("&8Q: &f%(title)")
.defaultLore(
Expand All @@ -70,7 +72,7 @@ public static final class ITEMS extends ConfigurationRoot {
"&e&l● &eClick and answer!"
).params("title", "description").build();

public static final ConfiguredItem FINISHED = ConfiguredItem.create()
ConfiguredItem FINISHED = ConfiguredItem.create()
.defaultType(Material.WRITTEN_BOOK)
.defaultName("&8Q: &f%(title)")
.defaultLore(
Expand All @@ -82,19 +84,19 @@ public static final class ITEMS extends ConfigurationRoot {
).params("title").build();


public static final ConfiguredItem CONFIRM = ConfiguredItem.create()
ConfiguredItem CONFIRM = ConfiguredItem.create()
.defaultType(Material.EMERALD)
.defaultName("&a&lConfirm and submit the request")
.defaultLore(" ", "&fClick to confirm and submit your request.")
.build();

public static final ConfiguredItem CANCEL = ConfiguredItem.create()
ConfiguredItem CANCEL = ConfiguredItem.create()
.defaultType(Material.REDSTONE)
.defaultName("&c&lCancel the request")
.defaultLore(" ", "&fClick to cancel your request.")
.build();

public static final ConfiguredItem PENDING = ConfiguredItem.create()
ConfiguredItem PENDING = ConfiguredItem.create()
.defaultType(Material.BARRIER)
.defaultName("&e&lPlease answer all the questions")
.defaultLore(
Expand All @@ -104,8 +106,56 @@ public static final class ITEMS extends ConfigurationRoot {
).build();

}
}

interface RESUBMIT extends Configuration {

ConfiguredMessage<String> TITLE = ConfiguredMessage.asString()
.defaults("&a&lRequest &8| &f%(server)")
.params("server")
.build();

interface ITEMS extends Configuration {

ConfiguredItem INFO = ConfiguredItem.create()
.defaultType(Material.NAME_TAG)
.defaultName("&ePrevious request &8#&f%(id)")
.defaultLore(
" ",
"&fWe found a previous request with",
"&a Approved %(approve)",
"&c Rejected %(reject)",
"&a Abstained %(abstain)",
"&7&oMaybe the request wasn't rejected, ",
"&7&obut simply closed automatically because it took too long.",
" ",
"&e Would you like to modify your previous request",
"&e and resubmit it for the next round of voting?",
" "
).params("id", "approve", "reject", "abstain").build();

ConfiguredItem ANSWERS = ConfiguredItem.create()
.defaultType(Material.WRITTEN_BOOK)
.defaultName("&8Q: &f%(question)")
.defaultLore(
" ",
"#answers#",
" "
).params("id", "question", "answers").build();

ConfiguredItem CONFIRM = ConfiguredItem.create()
.defaultType(Material.EMERALD)
.defaultName("&a&lYes, I'd like to modify previous request.")
.defaultLore(" ", "&fClick to confirm and modify your previous request.")
.build();

ConfiguredItem CANCEL = ConfiguredItem.create()
.defaultType(Material.REDSTONE)
.defaultName("&c&lNope, create a new request.")
.defaultLore(" ", "&fClick to create your new request.")
.build();

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class RequestManager implements UserRequestManager {
Expand Down Expand Up @@ -55,4 +56,27 @@ public class RequestManager implements UserRequestManager {
}
});
}

@Override
public @NotNull CompletableFuture<RequestInformation> lastFailed(@NotNull UserKey key, @NotNull String server) {
return CompletableFuture.supplyAsync(() -> {
try {
Map<Integer, RequestInformation> requests = Main.getInstance().getDataManager().queryRequests(b -> {
b.addCondition("server", server);
b.addCondition("user", key.id());
b.addCondition("result", RequestResult.REJECTED.getID());
b.addNotNullCondition("closed_time");
b.orderBy("create_time", false);
b.setLimit(1);
});
if (requests.isEmpty()) return null;
else return requests.values().iterator().next();
} catch (Exception ex) {
Main.severe("Error occurred when loading " + key.name() + "'s request, please check the configuration.");
ex.printStackTrace();
return null;
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cc.carm.lib.easyplugin.utils.ColorParser;
import cc.carm.lib.easyplugin.utils.ItemStackFactory;
import cc.carm.lib.mineconfiguration.bukkit.value.item.ConfiguredItem;
import cc.carm.lib.mineconfiguration.bukkit.value.item.PreparedItem;
import com.artformgames.plugin.votepass.api.data.request.RequestAnswer;
import com.artformgames.plugin.votepass.api.data.request.RequestInformation;
import com.artformgames.plugin.votepass.core.conf.CommonConfig;
Expand All @@ -16,18 +17,17 @@
import com.artformgames.plugin.votepass.lobby.api.user.LobbyUserData;
import com.artformgames.plugin.votepass.lobby.conf.PluginConfig;
import com.artformgames.plugin.votepass.lobby.conf.PluginMessages;
import com.google.common.collect.Lists;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.upperlevel.spigot.book.BookUtil;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class RequestingGUI extends AutoPagedGUI {
Expand All @@ -42,7 +42,11 @@ public static void open(Player player, LobbyUserData data, PendingRequest reques
@NotNull PendingRequest pendingRequest;

public RequestingGUI(@NotNull Player player, @NotNull LobbyUserData data, @NotNull PendingRequest pendingRequest) {
super(GUIType.SIX_BY_NINE, PluginConfig.ANSWERING.TITLE.parse(player, pendingRequest.getSettings().name()), 10, 34);
super(
GUIType.SIX_BY_NINE,
Objects.requireNonNull(PluginConfig.ANSWERING.TITLE.parse(player, pendingRequest.getSettings().name())),
10, 34
);
this.player = player;
this.data = data;
this.pendingRequest = pendingRequest;
Expand Down Expand Up @@ -87,11 +91,10 @@ public void initItems() {
protected GUIItem createQuestionItem(int id, ServerQuestion question) {
boolean isAnswered = getPendingRequest().isAnswered(id);
ConfiguredItem configuredItem = isAnswered ? PluginConfig.ANSWERING.ITEMS.FINISHED : PluginConfig.ANSWERING.ITEMS.REQUIRED;
PreparedItem item = configuredItem.prepare(question.title());
item.insertLore("description", ColorParser.parse(question.description()));

ItemStack icon = configuredItem.get(player, question.title());
insertLore(icon, "description", ColorParser.parse(question.description()));

return new GUIItem(icon) {
return new GUIItem(item.get(player)) {
@Override
public void onClick(Player clicker, ClickType type) {
player.closeInventory();
Expand Down Expand Up @@ -161,23 +164,6 @@ protected GUIItem createPendingItem() {
return new GUIItem(PluginConfig.ANSWERING.ITEMS.PENDING.get(player));
}


public void insertLore(@Nullable ItemStack item, String id, List<String> var2) {
if (item == null) return;

ItemMeta meta = item.getItemMeta();
if (meta == null || meta.getLore() == null || meta.getLore().isEmpty()) return;

List<String> lore = meta.getLore();
int index = lore.indexOf("#" + id + "#");
if (index >= 0) {
Lists.reverse(var2).forEach(var2x -> lore.add(index + 1, var2x));
lore.remove(index);
}
meta.setLore(lore);
item.setItemMeta(meta);
}

public ItemStack generateAnswerBook(@Nullable List<String> existingPages) {
ItemStackFactory factory = new ItemStackFactory(Material.WRITABLE_BOOK);
Optional.ofNullable(PluginConfig.ANSWERING.BOOK_ITEM.NAME.parse(player)).ifPresent(factory::setDisplayName);
Expand Down
Loading

0 comments on commit ebca144

Please sign in to comment.