Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check stack count before opening shulker box #1

Closed
ApliNi opened this issue Apr 17, 2023 · 5 comments · Fixed by #2
Closed

Check stack count before opening shulker box #1

ApliNi opened this issue Apr 17, 2023 · 5 comments · Fixed by #2
Labels
bug: compatibility Bad interaction between this and another plugin

Comments

@ApliNi
Copy link
Contributor

ApliNi commented Apr 17, 2023

Some servers make empty shulker boxes stackable via stacking plugins, need to check the stack count before opening them.

I do this in other plugins

public void onClick(InventoryClickEvent e) {
    ItemStack shulkerItem = e.getCurrentItem();
    // Do not open if stacked: compatible stacking plugin
    if(shulkerItem != null && shulkerItem.getAmount() != 1) return;

    // ...
}
@percyqaz
Copy link
Owner

percyqaz commented Apr 17, 2023

Thanks for the request
I think the fix will not be just as simple as checking the stack count

What happens if someone stacks empty shulker boxes while this plugin has an open shulker box?
Changes are only saved to the stack when the inventory gets closed so I think you would be able to get a duplication bug by:

  • Opening an empty shulker box
  • Putting items in it
  • Stacking the shulker box with other empty boxes
  • Close shulker box (this plugin will now save the contents to every shulker box in the stack)
  • You have duplicated items

I hope that makes sense, what do you think about this?
Also, if you have a plugin to hand that allows empty shulker stacking, can you send it to me as that means I can do testing with both plugins installed

@ApliNi
Copy link
Contributor Author

ApliNi commented Apr 17, 2023

I think, you fixed it, other shulker boxes cannot be left-clicked while opening a shulker box, but this leads me to another possibility: the player picks up an empty shulker box item after opening , they will be merged

plugin: https://www.spigotmc.org/resources/simple-stack-stack-any-items-to-64.83044/
Please add shulker boxes using the Unique Iten List feature of this plugin, in the config GUI

@ApliNi
Copy link
Contributor Author

ApliNi commented Apr 17, 2023

Maybe there needs to be a way to "lock" open shulker boxes, I think it would be possible to prevent stacking by adding NBTs with random data to them

Edit: This can be judged as a different item by the SimpleStack plugin (only the Unique Item List function)

// import
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;


// add
ItemMeta meta = shulkerItem.getItemMeta();
PersistentDataContainer data = meta.getPersistentDataContainer();
NamespacedKey nbtKey = new NamespacedKey(plugin, "__shulkerbox_plugin");
if(!data.has(nbtKey, PersistentDataType.STRING)){
    data.set(nbtKey, PersistentDataType.STRING, String.valueOf(System.currentTimeMillis()));
    shulkerItem.setItemMeta(meta);
}


// del
PersistentDataContainer data = meta.getPersistentDataContainer();
NamespacedKey nbtKey = new NamespacedKey(plugin, "__shulkerbox_plugin");
if(data.has(nbtKey, PersistentDataType.STRING)){
    data.remove(nbtKey);
}

@percyqaz
Copy link
Owner

percyqaz commented Apr 17, 2023

Yes, setting random NBT data on the shulker box should prevent it from stacking with other boxes and causing issues

So the goal is:

  • The instant a shulker box is right clicked, ensure it is a stack of 1 + mark it with random NBT
  • The instant a shulker box is closed (including if the player dies or disconnects), ensure the NBT is removed

Now during a shulker box being opened it should be impossible for identical shulker boxes to stack with it

Is there a way this could fail if the server crashes while someone has a shulker box open, and this leaves the NBT tag on the box forever? If so, should we just override that data when it's next opened and that will fix any glitched boxes?

Also, do you know a good way to set NBT data on item stacks without using net.minecraft.server?
I would prefer not to use it as then the plugin will only target a specific version of Spigot and won't be future proof.
I'm not a very experienced plugin developer so I wouldn't have the time to set up a system to update the plugin for each version of NMS/Spigot

@ApliNi
Copy link
Contributor Author

ApliNi commented Apr 17, 2023

I am studying how to operate nbt, there may be a better nbt library.

I may update the code on another branch: https://github.com/ApliNi/Shulkerbox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug: compatibility Bad interaction between this and another plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants