Allow spectators to edit inventories

Requires permission OpenInv.spectate
Closes #155
This commit is contained in:
Jikoo 2020-09-15 13:35:04 -04:00
parent 1a6d513603
commit da55790fd2
4 changed files with 29 additions and 7 deletions

View file

@ -136,6 +136,10 @@ OpenInv is a [Bukkit plugin](https://dev.bukkit.org/bukkit-plugins/openinv/) whi
<td>OpenInv.silent.default</td> <td>OpenInv.silent.default</td>
<td>Cause SilentContainer to be enabled by default.</td> <td>Cause SilentContainer to be enabled by default.</td>
</tr> </tr>
<tr>
<td>OpenInv.spectate</td>
<td>Allows users in spectate gamemode to edit inventories.</td>
</tr>
</table> </table>
## For Developers ## For Developers

View file

@ -19,6 +19,7 @@ package com.lishid.openinv.listeners;
import com.lishid.openinv.IOpenInv; import com.lishid.openinv.IOpenInv;
import com.lishid.openinv.util.InventoryAccess; import com.lishid.openinv.util.InventoryAccess;
import com.lishid.openinv.util.Permissions; import com.lishid.openinv.util.Permissions;
import org.bukkit.GameMode;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -56,22 +57,37 @@ public class InventoryListener implements Listener {
} }
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOWEST)
public void onInventoryClick(InventoryClickEvent event) { public void onInventoryClick(InventoryClickEvent event) {
onInventoryInteract(event); onInventoryInteract(event);
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOWEST)
public void onInventoryDrag(InventoryDragEvent event) { public void onInventoryDrag(InventoryDragEvent event) {
onInventoryInteract(event); onInventoryInteract(event);
} }
private void onInventoryInteract(InventoryInteractEvent event) { private void onInventoryInteract(InventoryInteractEvent event) {
HumanEntity entity = event.getWhoClicked(); HumanEntity entity = event.getWhoClicked();
if (Permissions.SPECTATE.hasPermission(entity) && entity.getGameMode() == GameMode.SPECTATOR) {
event.setCancelled(false);
}
if (event.isCancelled()) {
return;
}
Inventory inventory = event.getInventory(); Inventory inventory = event.getInventory();
if (InventoryAccess.isPlayerInventory(inventory) && !Permissions.EDITINV.hasPermission(entity)
|| InventoryAccess.isEnderChest(inventory) && !Permissions.EDITENDER.hasPermission(entity)) { if (InventoryAccess.isPlayerInventory(inventory)) {
event.setCancelled(true); if (!Permissions.EDITINV.hasPermission(entity)) {
event.setCancelled(true);
}
} else if (InventoryAccess.isEnderChest(inventory)) {
if (!Permissions.EDITENDER.hasPermission(entity)) {
event.setCancelled(true);
}
} }
} }

View file

@ -35,7 +35,8 @@ public enum Permissions {
EDITENDER("editender"), EDITENDER("editender"),
OPENSELF("openself"), OPENSELF("openself"),
OPENONLINE("openonline"), OPENONLINE("openonline"),
OPENOFFLINE("openoffline"); OPENOFFLINE("openoffline"),
SPECTATE("spectate");
private final String permission; private final String permission;

View file

@ -5,7 +5,7 @@ author: lishid
authors: [Jikoo, ShadowRanger] authors: [Jikoo, ShadowRanger]
description: > description: >
This plugin allows you to open a player's inventory as a chest and interact with it in real time. This plugin allows you to open a player's inventory as a chest and interact with it in real time.
api-version: "1.14" api-version: "1.15"
permissions: permissions:
OpenInv.any.default: OpenInv.any.default:
@ -27,6 +27,7 @@ permissions:
OpenInv.searchcontainer: true OpenInv.searchcontainer: true
OpenInv.openonline: true OpenInv.openonline: true
OpenInv.openoffline: true OpenInv.openoffline: true
OpenInv.spectate: true
OpenInv.openinv: OpenInv.openinv:
default: op default: op
children: children: