Skip to content

Commit e90f395

Browse files
committed
feat: /post open <player> [target] first implementation
1 parent 8248a91 commit e90f395

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
plugins {
22
java
33
id("xyz.jpenilla.run-paper") version "2.3.0"
4-
id("io.github.goooler.shadow") version "8.1.8"
4+
id("com.gradleup.shadow") version "9.3.1"
55
}
66

77
group = "io.github.md5sha256"

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/io/github/md5sha256/democracypost/DemocracyPost.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public void onEnable() {
5656
if (!isEnabled()) {
5757
return;
5858
}
59+
if (this.databaseAdapter == null) {
60+
getLogger().severe("Database was not initialized (onLoad failed). Plugin will not enable.");
61+
getServer().getPluginManager().disablePlugin(this);
62+
return;
63+
}
5964
if (getServer().getPluginManager().getPlugin("Vault") == null) {
6065
getLogger().severe("Missing Vault!");
6166
getServer().getPluginManager().disablePlugin(this);
@@ -89,6 +94,9 @@ public void onEnable() {
8994
getServer().getScheduler().runTaskTimerAsynchronously(
9095
this,
9196
() -> {
97+
if (this.databaseAdapter == null) {
98+
return;
99+
}
92100
getLogger().fine("Transferring expired packages...");
93101
try {
94102
this.databaseAdapter.transferExpiredPackages(returnPackageExpiryDuration);

src/main/java/io/github/md5sha256/democracypost/command/PostCommand.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.bukkit.command.CommandSender;
1010
import org.bukkit.entity.Player;
1111
import org.bukkit.plugin.Plugin;
12-
import org.checkerframework.checker.units.qual.A;
1312
import org.incendo.cloud.SenderMapper;
1413
import org.incendo.cloud.annotations.AnnotationParser;
1514
import org.incendo.cloud.annotations.Argument;
@@ -54,6 +53,19 @@ public void commandOpen(
5453
});
5554
}
5655

56+
@Command("post open <player> <target>")
57+
@Permission("democracypost.open")
58+
public void commandOpenWithTarget(
59+
@Nonnull @Argument(value = "player") Player player,
60+
@Nonnull @Argument(value = "target") OfflinePlayer target
61+
) {
62+
if (!target.hasPlayedBefore()) {
63+
player.sendMessage(Component.text("Unknown or invalid target player.", NamedTextColor.RED));
64+
return;
65+
}
66+
this.postOfficeMenu.createParcelPostUi(target).show(player);
67+
}
68+
5769
@Command("post view <player>")
5870
@Permission("democracypost.view")
5971
public void commandView(

src/main/java/io/github/md5sha256/democracypost/ui/PostOfficeMenu.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ public InventoryGui createPostUi(String playerName, List<PostalPackage> packages
143143
}
144144

145145
public InventoryGui createParcelPostUi() {
146+
return createParcelPostUi(null);
147+
}
148+
149+
public InventoryGui createParcelPostUi(@javax.annotation.Nullable OfflinePlayer recipient) {
146150
String[] rows = new String[]{
147151
" ",
148152
" ddddddd ",
@@ -154,13 +158,14 @@ public InventoryGui createParcelPostUi() {
154158
// blank space = panes
155159
// b = back, p = post parcel, e = exit
156160
Inventory storageInv = this.plugin.getServer().createInventory(null, 9 * 3);
161+
UUID recipientUuid = recipient != null && recipient.hasPlayedBefore() ? recipient.getUniqueId() : null;
157162
InventoryGui gui = createGui(
158163
this.messageContainer.messageFor("menu.parcel.drop"),
159164
rows,
160165
elementPanes(' '),
161166
elementDrop('d', storageInv),
162167
elementBack('b', player -> returnAndClearItems(player, storageInv)),
163-
elementPost('p', storageInv)
168+
elementPost('p', storageInv, recipientUuid)
164169
);
165170
gui.setCloseAction(close -> handleInventoryClose(close, storageInv));
166171
return gui;
@@ -413,7 +418,7 @@ private GuiElement elementPrevious(char c) {
413418
LegacyComponentSerializer.legacySection().serialize(displayName));
414419
}
415420

416-
private GuiElement elementPost(char c, Inventory storageInv) {
421+
private GuiElement elementPost(char c, Inventory storageInv, @javax.annotation.Nullable UUID recipientUuid) {
417422
ItemStack itemStack = this.itemFactory.createSendPackageButton();
418423
ItemMeta meta = itemStack.getItemMeta();
419424
Component displayName = this.messageContainer.messageFor("menu.main.post-parcel")
@@ -422,7 +427,7 @@ private GuiElement elementPost(char c, Inventory storageInv) {
422427
itemStack.setItemMeta(meta);
423428
StaticGuiElement element = new StaticGuiElement(c, itemStack);
424429
element.setAction(action -> {
425-
processPost(action, element, itemStack, storageInv);
430+
processPost(action, element, itemStack, storageInv, recipientUuid);
426431
return true;
427432
});
428433
return element;
@@ -432,9 +437,9 @@ private void processPost(
432437
GuiElement.Click action,
433438
StaticGuiElement element,
434439
ItemStack display,
435-
Inventory storageInv) {
436-
// Don't clear the storage inv, items will be added back
437-
if (!(action.getWhoClicked() instanceof Conversable conversable)) {
440+
Inventory storageInv,
441+
@javax.annotation.Nullable UUID recipientUuid) {
442+
if (!(action.getWhoClicked() instanceof Player player)) {
438443
return;
439444
}
440445
List<ItemStack> items = new ArrayList<>();
@@ -455,6 +460,36 @@ private void processPost(
455460
}
456461
// Clear the storage inv here
457462
storageInv.clear();
463+
464+
if (recipientUuid != null) {
465+
// Send directly to pre-specified recipient (no prompt)
466+
OfflinePlayer recipient = this.plugin.getServer().getOfflinePlayer(recipientUuid);
467+
if (!recipient.hasPlayedBefore()) {
468+
String name = recipient.getName() != null ? recipient.getName() : recipientUuid.toString();
469+
player.sendMessage(this.messageContainer.messageFor("prompt.post-parcel.unknown-player")
470+
.replaceText(builder -> builder.matchLiteral("%player%").replacement(name)));
471+
InventoryUtil.addItems(player, items);
472+
return;
473+
}
474+
if (player.getUniqueId().equals(recipientUuid)) {
475+
player.sendMessage(this.messageContainer.messageFor("prompt.post-parcel.send-parcel-self"));
476+
InventoryUtil.addItems(player, items);
477+
return;
478+
}
479+
var response = this.economy.withdrawPlayer(player, this.postSettings.postPrice());
480+
if (!response.transactionSuccess()) {
481+
player.sendMessage(this.messageContainer.messageFor("prompt.post-parcel.insufficient-balance"));
482+
InventoryUtil.addItems(player, items);
483+
return;
484+
}
485+
this.postalPackageFactory.createAndPostPackage(player.getUniqueId(), recipientUuid, items, false);
486+
player.sendMessage(this.messageContainer.messageFor("prompt.post-parcel.send-parcel-success"));
487+
action.getGui().close(player, true);
488+
return;
489+
}
490+
491+
// No recipient: start conversation to ask for username
492+
Conversable conversable = (Conversable) player;
458493
// Copy the current history
459494
Deque<InventoryGui> history = new ArrayDeque<>(InventoryGui.getHistory(action.getWhoClicked()));
460495
// Remove the current UI so it isn't added back
@@ -477,7 +512,6 @@ private void processPost(
477512
.buildConversation(conversable);
478513
conversation.addConversationAbandonedListener(event -> {
479514
if (!event.gracefulExit()) {
480-
// If the conversation is abandoned, give the items back to the player
481515
InventoryUtil.addItems(action.getWhoClicked(), items);
482516
}
483517
Conversable who = event.getContext().getForWhom();
@@ -488,7 +522,6 @@ private void processPost(
488522
}
489523
}
490524
});
491-
// Start conversation to send
492525
conversation.begin();
493526
}
494527

0 commit comments

Comments
 (0)