Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 33bece5

Browse files
committedFeb 27, 2023
attempt to fix joestrhq#18 dupe bug
1 parent 32621ca commit 33bece5

7 files changed

+76
-4
lines changed
 

‎pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>at.joestr</groupId>
88
<artifactId>postbox</artifactId>
9-
<version>1.2.3</version>
9+
<version>1.2.4-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>PostBox</name>

‎src/main/java/at/joestr/postbox/PostBoxPlugin.java

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.apache.commons.lang3.tuple.Triple;
5353
import org.bstats.bukkit.Metrics;
5454
import org.bukkit.Bukkit;
55+
import org.bukkit.NamespacedKey;
5556
import org.bukkit.command.PluginCommand;
5657
import org.bukkit.command.TabExecutor;
5758
import org.bukkit.event.Listener;
@@ -65,6 +66,7 @@ public class PostBoxPlugin extends JavaPlugin implements Listener {
6566
private static final Logger LOG = Logger.getLogger(PostBoxPlugin.class.getName());
6667
public static PostBoxPlugin instance = null;
6768

69+
private NamespacedKey namespacedKey;
6870
private HashMap<String, TabExecutor> commandMap;
6971
private Updater updater;
7072
private LuckPerms luckPermsApi;
@@ -110,6 +112,8 @@ public void onEnable() {
110112
.getString(CurrentEntries.CONF_UPDATER_CLASSIFIER.toString()),
111113
Bukkit.getUpdateFolderFile());
112114

115+
this.namespacedKey = new NamespacedKey(instance, "PostBox_c765a011bcd709a9c8d417be90a7f090");
116+
113117
this.commandMap.put("postbox", new CommandPostBox());
114118
this.commandMap.put("postbox-open", new CommandPostBoxOpen());
115119
this.commandMap.put("postbox-openother", new CommandPostBoxOpenOther());
@@ -219,4 +223,8 @@ public ProfileService getProfileService() {
219223
public ArrayList<Triple<UUID, Inventory, UUID>> getInventoryMappings() {
220224
return inventoryMappings;
221225
}
226+
227+
public NamespacedKey getNamespacedKey() {
228+
return namespacedKey;
229+
}
222230
}

‎src/main/java/at/joestr/postbox/commands/CommandPostBoxOpen.java

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import at.joestr.postbox.configuration.DatabaseModels.PostBoxModel;
3131
import at.joestr.postbox.configuration.LocaleHelper;
3232
import at.joestr.postbox.configuration.MessageHelper;
33+
import at.joestr.postbox.utils.PostBoxNamespacedKeyValues;
3334
import at.joestr.postbox.utils.PostBoxUtils;
3435
import java.sql.SQLException;
3536
import java.util.ArrayList;
@@ -50,6 +51,7 @@
5051
import org.bukkit.inventory.Inventory;
5152
import org.bukkit.inventory.ItemStack;
5253
import org.bukkit.inventory.meta.ItemMeta;
54+
import org.bukkit.persistence.PersistentDataType;
5355

5456
/**
5557
* @author joestr
@@ -178,6 +180,11 @@ public void run() {
178180
);
179181

180182
localItemMeta.setLore(loreLines);
183+
localItemMeta.getPersistentDataContainer().set(
184+
PostBoxPlugin.getInstance().getNamespacedKey(),
185+
PersistentDataType.INTEGER,
186+
PostBoxNamespacedKeyValues.IS_POST_BOX_ITEM.ordinal()
187+
);
181188
}
182189

183190
localItemStack.setItemMeta(localItemMeta);

‎src/main/java/at/joestr/postbox/commands/CommandPostBoxOpenOther.java

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import at.joestr.postbox.configuration.DatabaseModels.PostBoxModel;
3131
import at.joestr.postbox.configuration.LocaleHelper;
3232
import at.joestr.postbox.configuration.MessageHelper;
33+
import at.joestr.postbox.utils.PostBoxNamespacedKeyValues;
3334
import at.joestr.postbox.utils.PostBoxUtils;
3435
import java.sql.SQLException;
3536
import java.util.ArrayList;
@@ -52,6 +53,7 @@
5253
import org.bukkit.inventory.Inventory;
5354
import org.bukkit.inventory.ItemStack;
5455
import org.bukkit.inventory.meta.ItemMeta;
56+
import org.bukkit.persistence.PersistentDataType;
5557

5658
/**
5759
* @author joestr
@@ -207,6 +209,11 @@ public boolean onCommand(CommandSender sender, Command command, String string, S
207209
);
208210

209211
localItemMeta.setLore(loreLines);
212+
localItemMeta.getPersistentDataContainer().set(
213+
PostBoxPlugin.getInstance().getNamespacedKey(),
214+
PersistentDataType.INTEGER,
215+
PostBoxNamespacedKeyValues.IS_POST_BOX_ITEM.ordinal()
216+
);
210217
}
211218
localItemStack.setItemMeta(localItemMeta);
212219
inventory.setItem(inventoryItemCount++, localItemStack);

‎src/main/java/at/joestr/postbox/event/InventoryClickListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public InventoryClickListener() {
5555
}
5656

5757
// Highest Priority and ignore cancelled events
58-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
58+
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
5959
public void Inventory(InventoryClickEvent event) {
6060
Player player = (Player) event.getWhoClicked();
6161
Locale locale = LocaleHelper.resolve(player.getLocale());

‎src/main/java/at/joestr/postbox/event/InventoryCloseListener.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,39 @@
2424
package at.joestr.postbox.event;
2525

2626
import at.joestr.postbox.PostBoxPlugin;
27+
import at.joestr.postbox.utils.PostBoxNamespacedKeyValues;
28+
import org.bukkit.Bukkit;
2729
import org.bukkit.event.EventHandler;
2830
import org.bukkit.event.EventPriority;
2931
import org.bukkit.event.Listener;
3032
import org.bukkit.event.inventory.InventoryCloseEvent;
33+
import org.bukkit.inventory.ItemStack;
34+
import org.bukkit.persistence.PersistentDataContainer;
35+
import org.bukkit.persistence.PersistentDataType;
3136

3237
/**
3338
* @author joestr
3439
*/
3540
public class InventoryCloseListener implements Listener {
3641

3742
// Highest Priority and ignore cancelled events
38-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
43+
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = false)
3944
public void handle(InventoryCloseEvent event) {
40-
PostBoxPlugin.getInstance()
45+
boolean wasRemovedFromPostBox = PostBoxPlugin.getInstance()
4146
.getInventoryMappings()
4247
.removeIf(mapping -> mapping.getLeft().equals(event.getPlayer().getUniqueId()));
48+
49+
if (wasRemovedFromPostBox) {
50+
Bukkit.getScheduler().runTask(PostBoxPlugin.getInstance(), () -> {
51+
for (ItemStack itemStack : event.getPlayer().getInventory().getContents()) {
52+
PersistentDataContainer persistentDataContainer = itemStack.getItemMeta().getPersistentDataContainer();
53+
if (!persistentDataContainer.isEmpty() && persistentDataContainer.has(PostBoxPlugin.getInstance().getNamespacedKey(), PersistentDataType.INTEGER)) {
54+
if (persistentDataContainer.getOrDefault(PostBoxPlugin.getInstance().getNamespacedKey(), PersistentDataType.INTEGER, -1) == PostBoxNamespacedKeyValues.IS_POST_BOX_ITEM.ordinal()) {
55+
event.getPlayer().getInventory().remove(itemStack);
56+
}
57+
}
58+
}
59+
});
60+
}
4361
}
4462
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2022 Joel Strasser
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
package at.joestr.postbox.utils;
25+
26+
/**
27+
*
28+
* @author joestr
29+
*/
30+
public enum PostBoxNamespacedKeyValues {
31+
IS_POST_BOX_ITEM
32+
}

0 commit comments

Comments
 (0)
Please sign in to comment.