Skip to content

Commit f161285

Browse files
feat: select shop type when creating shop
1 parent 703bc03 commit f161285

File tree

14 files changed

+264
-40
lines changed

14 files changed

+264
-40
lines changed

src/main/java/org/maxgamer/quickshop/QuickShop.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,10 @@ private void updateConfig(int selectedVersion) throws IOException {
21972197
getConfig().set("integration.superiorskyblock.trade-privilege-needs-list", new ArrayList<>());
21982198
getConfig().set("config-version", ++selectedVersion);
21992199
}
2200+
if (selectedVersion == 160) {
2201+
getConfig().set("shop.create-needs-select-type", false);
2202+
getConfig().set("config-version", ++selectedVersion);
2203+
}
22002204
if (getConfig().isSet("shop.shop")) {
22012205
getConfig().set("shop.shop", null);
22022206
}

src/main/java/org/maxgamer/quickshop/api/chat/QuickChat.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.jetbrains.annotations.Nullable;
2727
import org.maxgamer.quickshop.api.shop.Shop;
2828

29+
import java.util.Map;
30+
2931
/**
3032
* QuickChat is a system to allow us to process complex messages
3133
*/
@@ -79,12 +81,21 @@ void sendItemHologramChat(
7981
*/
8082
@NotNull QuickComponent getItemTextComponent(@NotNull Player player, @NotNull ItemStack itemStack, @NotNull String text);
8183

84+
/**
85+
* Send click-run-command chat to specified receiver
86+
*
87+
* @param receiver The PLAYER will receive this message
88+
* @param message The text will sent
89+
* @param textToCommandMapping The hover text associated with command, will be applied to placeholder (like {0} {1} {2}...)
90+
*/
91+
void sendExecutableChat(@NotNull CommandSender receiver, @NotNull String message, Map.Entry<String, String>... textToCommandMapping);
92+
8293
/**
8394
* Send click-run-command chat to specified receiver
8495
*
8596
* @param receiver The PLAYER will receive this message
8697
* @param message The text will sent
87-
* @param hoverText The will show when hover on chat
98+
* @param hoverText The hover text will show when hover on chat
8899
* @param command The command when click to be run
89100
*/
90101
void sendExecutableChat(@NotNull CommandSender receiver, @NotNull String message, @NotNull String hoverText, @NotNull String command);

src/main/java/org/maxgamer/quickshop/api/shop/Info.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ public interface Info {
5959
*/
6060
boolean hasChanged(@NotNull Shop shop);
6161

62+
/**
63+
* @return Shop type to use
64+
*/
65+
ShopType getShopType();
66+
67+
void setShopType(ShopType shopType);
68+
69+
/**
70+
* @return Pending create message, use as temporary store when creating shop needs select shop type.
71+
*/
72+
String getPendingCreateMessage();
73+
74+
void setPendingCreateMessage(String pendingCreateMessage);
75+
6276
/**
6377
* Check if this Info marked as skip protection checks
6478
*

src/main/java/org/maxgamer/quickshop/api/shop/ShopAction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
public enum ShopAction {
2323
// buy = trading create = creating shop cancelled = stopped
24-
BUY(),
25-
CREATE(),
26-
CANCELLED()
24+
BUY,
25+
CREATE,
26+
CREATE_TYPE_INPUT,
27+
CANCELLED
2728
}

src/main/java/org/maxgamer/quickshop/chat/platform/minedown/BungeeQuickChat.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
package org.maxgamer.quickshop.chat.platform.minedown;
2121

2222
import lombok.AllArgsConstructor;
23-
import net.md_5.bungee.api.chat.*;
23+
import net.md_5.bungee.api.chat.BaseComponent;
24+
import net.md_5.bungee.api.chat.ClickEvent;
25+
import net.md_5.bungee.api.chat.ComponentBuilder;
26+
import net.md_5.bungee.api.chat.HoverEvent;
27+
import net.md_5.bungee.api.chat.TextComponent;
2428
import net.md_5.bungee.chat.ComponentSerializer;
2529
import org.apache.commons.lang3.StringUtils;
2630
import org.bukkit.ChatColor;
@@ -41,6 +45,11 @@
4145

4246
import java.lang.reflect.InvocationTargetException;
4347
import java.nio.charset.StandardCharsets;
48+
import java.util.ArrayList;
49+
import java.util.Arrays;
50+
import java.util.Iterator;
51+
import java.util.List;
52+
import java.util.Map;
4453
import java.util.UUID;
4554
import java.util.logging.Level;
4655

@@ -337,6 +346,51 @@ public ComponentBuilder color(net.md_5.bungee.api.ChatColor color) {
337346

338347
}
339348

349+
private final String[] indexStrCache = {"{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", "{8}", "{9}"};
350+
351+
public String getIndexStr(int index) {
352+
return index <= 9 ? indexStrCache[index] : "{" + index + "}";
353+
}
354+
355+
@Override
356+
public void sendExecutableChat(@NotNull CommandSender receiver, @NotNull String message, Map.Entry<String, String>... textToCommandMapping) {
357+
List<BaseComponent> components = new ArrayList<>(Arrays.asList(fromLegacyText(message)));
358+
Iterator<Map.Entry<String, String>> iterator = Arrays.asList(textToCommandMapping).iterator();
359+
360+
int index = 0;
361+
replace:
362+
while (iterator.hasNext()) {
363+
Map.Entry<String, String> replacement = iterator.next();
364+
for (int i = 0; i < components.size(); i++) {
365+
BaseComponent component = components.get(i);
366+
if (component instanceof TextComponent) {
367+
String text = ((TextComponent) component).getText();
368+
if (text.contains(getIndexStr(index))) {
369+
String[] strings = text.split(getIndexStr(index).replace("{", "\\{").replace("}", "\\}"), 2);
370+
TextComponent component1 = new TextComponent(strings[0]);
371+
TextComponent component2 = new TextComponent(strings[1]);
372+
component1.copyFormatting(component);
373+
component2.copyFormatting(component);
374+
BaseComponent[] replacementComponents = fromLegacyText(replacement.getKey());
375+
ClickEvent clickEvent = new ClickEvent(ClickEvent.Action.RUN_COMMAND, replacement.getValue());
376+
HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, fromLegacyText(replacement.getKey()));
377+
for (BaseComponent baseComponent : replacementComponents) {
378+
baseComponent.setClickEvent(clickEvent);
379+
baseComponent.setHoverEvent(hoverEvent);
380+
component1.addExtra(baseComponent);
381+
}
382+
components.remove(i);
383+
components.add(i, component2);
384+
components.add(i, component1);
385+
index++;
386+
continue replace;
387+
}
388+
}
389+
}
390+
}
391+
receiver.spigot().sendMessage(components.toArray(new BaseComponent[0]));
392+
}
393+
340394
@Override
341395
public void sendExecutableChat(@NotNull CommandSender receiver, @NotNull String message, @NotNull String hoverText, @NotNull String command) {
342396
BaseComponent[] components =

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Convert.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
import org.jetbrains.annotations.Nullable;
2929
import org.maxgamer.quickshop.QuickShop;
3030
import org.maxgamer.quickshop.api.command.CommandHandler;
31-
import org.maxgamer.quickshop.database.*;
31+
import org.maxgamer.quickshop.database.AbstractDatabaseCore;
32+
import org.maxgamer.quickshop.database.DatabaseManager;
33+
import org.maxgamer.quickshop.database.MySQLCore;
34+
import org.maxgamer.quickshop.database.SQLiteCore;
35+
import org.maxgamer.quickshop.database.SimpleDatabaseHelper;
3236

3337
import java.io.File;
3438
import java.util.ArrayList;

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Create.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@
3030
import org.jetbrains.annotations.Nullable;
3131
import org.maxgamer.quickshop.QuickShop;
3232
import org.maxgamer.quickshop.api.command.CommandHandler;
33+
import org.maxgamer.quickshop.api.shop.Info;
3334
import org.maxgamer.quickshop.api.shop.ShopAction;
3435
import org.maxgamer.quickshop.shop.SimpleInfo;
3536
import org.maxgamer.quickshop.util.MsgUtil;
3637
import org.maxgamer.quickshop.util.Util;
3738
import org.maxgamer.quickshop.util.holder.Result;
3839

40+
import java.util.AbstractMap;
3941
import java.util.Collections;
4042
import java.util.List;
4143
import java.util.Objects;
4244

45+
import static org.maxgamer.quickshop.api.shop.ShopAction.CREATE_TYPE_INPUT;
46+
4347
public class SubCommand_Create implements CommandHandler<Player> {
4448

4549
private final QuickShop plugin;
@@ -142,11 +146,24 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
142146

143147

144148
// Send creation menu.
145-
plugin.getShopManager().getActions().put(sender.getUniqueId(),
146-
new SimpleInfo(b.getLocation(), ShopAction.CREATE, item, b.getRelative(sender.getFacing().getOppositeFace()), false));
147-
if (cmdArg.length >= 1) {
148-
String price = cmdArg[0];
149-
plugin.getShopManager().handleChat(sender, price);
149+
Info info = new SimpleInfo(b.getLocation(), ShopAction.CREATE, item, b.getRelative(sender.getFacing().getOppositeFace()), false);
150+
plugin.getShopManager().getActions().put(sender.getUniqueId(), info);
151+
if (plugin.getConfig().getBoolean("shop.create-needs-select-type")) {
152+
if (cmdArg.length >= 1) {
153+
info.setPendingCreateMessage(cmdArg[0]);
154+
}
155+
info.setAction(CREATE_TYPE_INPUT);
156+
plugin.getQuickChat().sendExecutableChat(sender, plugin.text().of(sender, "select-shop-type-or-cancel").forLocale(),
157+
new AbstractMap.SimpleEntry<>(plugin.text().of(sender, "select-shop-type-or-cancel-selling-button").forLocale(), "/qs amount SELL"),
158+
new AbstractMap.SimpleEntry<>(plugin.text().of(sender, "select-shop-type-or-cancel-buying-button").forLocale(), "/qs amount BUY"),
159+
new AbstractMap.SimpleEntry<>(plugin.text().of(sender, "select-shop-type-or-cancel-cancel-button").forLocale(), "/qs amount CANCEL"));
160+
} else {
161+
if (cmdArg.length >= 1) {
162+
String price = cmdArg[0];
163+
plugin.getShopManager().handleChat(sender, price);
164+
} else {
165+
plugin.text().of(sender, "how-much-to-trade-for", MsgUtil.getTranslateText(Objects.requireNonNull(item)), Integer.toString(plugin.isAllowStack() && QuickShop.getPermissionManager().hasPermission(sender, "quickshop.create.stacks") ? item.getAmount() : 1)).send();
166+
}
150167
}
151168
return;
152169
}

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_SuperCreate.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
import org.maxgamer.quickshop.util.MsgUtil;
3434
import org.maxgamer.quickshop.util.Util;
3535

36+
import java.util.AbstractMap;
3637
import java.util.Collections;
3738
import java.util.List;
3839

40+
import static org.maxgamer.quickshop.api.shop.ShopAction.CREATE_TYPE_INPUT;
41+
3942
@AllArgsConstructor
4043
public class SubCommand_SuperCreate implements CommandHandler<Player> {
4144

@@ -64,9 +67,16 @@ public void onCommand(@NotNull Player sender, @NotNull String commandLabel, @Not
6467
// }
6568
// Send creation menu.
6669
final SimpleInfo info = new SimpleInfo(b.getLocation(), ShopAction.CREATE, sender.getInventory().getItemInMainHand(), b.getRelative(sender.getFacing().getOppositeFace()), true);
67-
6870
plugin.getShopManager().getActions().put(sender.getUniqueId(), info);
69-
plugin.text().of(sender, "how-much-to-trade-for", MsgUtil.getTranslateText(info.getItem()), Integer.toString(plugin.isAllowStack() && QuickShop.getPermissionManager().hasPermission(sender, "quickshop.create.stacks") ? item.getAmount() : 1)).send();
71+
if (plugin.getConfig().getBoolean("shop.create-needs-select-type")) {
72+
info.setAction(CREATE_TYPE_INPUT);
73+
plugin.getQuickChat().sendExecutableChat(sender, plugin.text().of(sender, "select-shop-type-or-cancel").forLocale(),
74+
new AbstractMap.SimpleEntry<>(plugin.text().of(sender, "select-shop-type-or-cancel-selling-button").forLocale(), "/qs amount SELL"),
75+
new AbstractMap.SimpleEntry<>(plugin.text().of(sender, "select-shop-type-or-cancel-buying-button").forLocale(), "/qs amount BUY"),
76+
new AbstractMap.SimpleEntry<>(plugin.text().of(sender, "select-shop-type-or-cancel-cancel-button").forLocale(), "/qs amount CANCEL"));
77+
} else {
78+
plugin.text().of(sender, "how-much-to-trade-for", MsgUtil.getTranslateText(info.getItem()), Integer.toString(plugin.isAllowStack() && QuickShop.getPermissionManager().hasPermission(sender, "quickshop.create.stacks") ? item.getAmount() : 1)).send();
79+
}
7080
return;
7181
}
7282
plugin.text().of(sender, "not-looking-at-shop").send();

src/main/java/org/maxgamer/quickshop/command/subcommand/SubCommand_Update.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.maxgamer.quickshop.api.command.CommandHandler;
2929
import org.maxgamer.quickshop.util.MsgUtil;
3030
import org.maxgamer.quickshop.util.updater.QuickUpdater;
31-
import org.maxgamer.quickshop.util.updater.VersionType;
3231

3332
import java.util.logging.Level;
3433

@@ -47,8 +46,7 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabe
4746
return;
4847
}
4948
QuickUpdater updater = plugin.getUpdateWatcher().getUpdater();
50-
VersionType versionType = updater.getCurrentRunning();
51-
if (updater.isLatest(versionType)) {
49+
if (updater.isLatest()) {
5250
MsgUtil.sendDirectMessage(sender, ChatColor.GREEN + "You're running the latest version!");
5351
return;
5452
}
@@ -60,7 +58,7 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabe
6058
}
6159
MsgUtil.sendDirectMessage(sender, ChatColor.YELLOW + "Downloading update! This may take a while...");
6260
try {
63-
updater.install(updater.update(versionType));
61+
updater.installUpdate();
6462
} catch (Exception e) {
6563
MsgUtil.sendDirectMessage(sender, ChatColor.RED + "Update failed! Please check your console for more information.");
6664
plugin.getSentryErrorReporter().ignoreThrow();

src/main/java/org/maxgamer/quickshop/listener/PlayerListener.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,14 @@
5959
import org.maxgamer.quickshop.util.reload.ReloadResult;
6060
import org.maxgamer.quickshop.util.reload.ReloadStatus;
6161

62+
import java.util.AbstractMap;
6263
import java.util.Map;
6364
import java.util.Objects;
6465
import java.util.UUID;
6566
import java.util.concurrent.TimeUnit;
6667

68+
import static org.maxgamer.quickshop.api.shop.ShopAction.CREATE_TYPE_INPUT;
69+
6770
public class PlayerListener extends AbstractQSListener {
6871
private final CooldownMap<Player> cooldownMap = CooldownMap.create(Cooldown.of(1, TimeUnit.SECONDS));
6972
private boolean swapBehavior;
@@ -301,7 +304,15 @@ else if (e.useInteractedBlock() == Event.Result.ALLOW
301304
// Send creation menu.
302305
final SimpleInfo info = new SimpleInfo(b.getLocation(), ShopAction.CREATE, e.getItem(), last, false);
303306
plugin.getShopManager().getActions().put(p.getUniqueId(), info);
304-
plugin.text().of(p, "how-much-to-trade-for", MsgUtil.getTranslateText(Objects.requireNonNull(e.getItem())), Integer.toString(plugin.isAllowStack() && QuickShop.getPermissionManager().hasPermission(p, "quickshop.create.stacks") ? item.getAmount() : 1)).send();
307+
if (plugin.getConfig().getBoolean("shop.create-needs-select-type")) {
308+
info.setAction(CREATE_TYPE_INPUT);
309+
plugin.getQuickChat().sendExecutableChat(p, plugin.text().of(p, "select-shop-type-or-cancel").forLocale(),
310+
new AbstractMap.SimpleEntry<>(plugin.text().of(p, "select-shop-type-or-cancel-selling-button").forLocale(), "/qs amount SELL"),
311+
new AbstractMap.SimpleEntry<>(plugin.text().of(p, "select-shop-type-or-cancel-buying-button").forLocale(), "/qs amount BUY"),
312+
new AbstractMap.SimpleEntry<>(plugin.text().of(p, "select-shop-type-or-cancel-cancel-button").forLocale(), "/qs amount CANCEL"));
313+
} else {
314+
plugin.text().of(p, "how-much-to-trade-for", MsgUtil.getTranslateText(Objects.requireNonNull(e.getItem())), Integer.toString(plugin.isAllowStack() && QuickShop.getPermissionManager().hasPermission(p, "quickshop.create.stacks") ? item.getAmount() : 1)).send();
315+
}
305316
//Prevent use item by ancient
306317
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
307318
e.setUseItemInHand(Event.Result.DENY);
@@ -419,7 +430,7 @@ public void onPlayerBreakShopCreationChest(BlockBreakEvent event) {
419430
actionMap.remove(player.getUniqueId());
420431
if (info.getAction() == ShopAction.BUY) {
421432
plugin.text().of(player, "shop-purchase-cancelled").send();
422-
} else if (info.getAction() == ShopAction.CREATE) {
433+
} else if (info.getAction() == ShopAction.CREATE || info.getAction() == CREATE_TYPE_INPUT) {
423434
plugin.text().of(player, "shop-creation-cancelled").send();
424435
}
425436
}
@@ -440,7 +451,7 @@ public void onMove(PlayerMoveEvent e) {
440451
if (loc1.getWorld() != loc2.getWorld() || loc1.distanceSquared(loc2) > 25) {
441452
if (info.getAction() == ShopAction.BUY) {
442453
plugin.text().of(p, "shop-purchase-cancelled").send();
443-
} else if (info.getAction() == ShopAction.CREATE) {
454+
} else if (info.getAction() == ShopAction.CREATE || info.getAction() == CREATE_TYPE_INPUT) {
444455
plugin.text().of(p, "shop-creation-cancelled").send();
445456
}
446457
Util.debugLog(p.getName() + " too far with the shop location.");

0 commit comments

Comments
 (0)