Skip to content

Commit

Permalink
Revert "Remove chest protection inventory check (PGMDev#977)"
Browse files Browse the repository at this point in the history
This reverts commit 963f02a.
  • Loading branch information
applenick committed Jul 23, 2022
1 parent 8a75454 commit 5226615
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import tc.oc.pgm.events.PlayerPartyChangeEvent;
import tc.oc.pgm.kits.WalkSpeedKit;
import tc.oc.pgm.spawns.events.ParticipantSpawnEvent;
import tc.oc.pgm.util.TimeUtils;
import tc.oc.pgm.util.attribute.Attribute;
import tc.oc.pgm.util.bukkit.BukkitUtils;
import tc.oc.pgm.util.named.NameStyle;
Expand All @@ -60,6 +61,12 @@
@ListenerScope(MatchScope.LOADED)
public class ViewInventoryMatchModule implements MatchModule, Listener {

/**
* Amount of milliseconds after the match begins where players may not add / remove items from
* chests.
*/
public static final Duration CHEST_PROTECT_TIME = Duration.ofSeconds(2);

public static final Duration TICK = Duration.ofMillis(50);

protected final HashMap<String, InventoryTrackerEntry> monitoredInventories = new HashMap<>();
Expand Down Expand Up @@ -102,6 +109,26 @@ private void checkAllMonitoredInventories() {
}
}

@EventHandler(ignoreCancelled = true)
public void checkInventoryClick(final InventoryClickEvent event) {
if (event.getWhoClicked() instanceof Player) {
MatchPlayer player = this.match.getPlayer((Player) event.getWhoClicked());
if (player == null) {
return;
}
// we only cancel when the view is a chest because the other views tend to crash
if (!allowedInventoryType(event.getInventory().getType())) {
// cancel the click if the player cannot interact with the world or if the match has just
// started
if (!player.canInteract()
|| (player.getMatch().isRunning()
&& TimeUtils.isShorterThan(player.getMatch().getDuration(), CHEST_PROTECT_TIME))) {
event.setCancelled(true);
}
}
}
}

@EventHandler
public void closeMonitoredInventory(final InventoryCloseEvent event) {
this.monitoredInventories.remove(event.getPlayer().getName());
Expand Down Expand Up @@ -266,6 +293,16 @@ public boolean canPreviewInventory(MatchPlayer viewer, MatchPlayer holder) {
return viewer.isObserving() && holder.isAlive();
}

protected static boolean allowedInventoryType(InventoryType type) {
switch (type) {
case CREATIVE:
case PLAYER:
return true;
default:
return false;
}
}

protected void scheduleCheck(Player updater) {
if (this.updateQueue.containsKey(updater.getName())) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerArmorStandManipulateEvent;
import org.bukkit.event.player.PlayerBedEnterEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
Expand Down Expand Up @@ -334,13 +333,6 @@ public void onPotionSplash(final PotionSplashEvent event) {
// -- Player item/inventory actions --
// -----------------------------------

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClick(final InventoryClickEvent event) {
if (!event.getInventory().equals(event.getWhoClicked().getInventory())) {
cancelUnlessInteracting(event, event.getWhoClicked());
}
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerDropItem(final PlayerDropItemEvent event) {
if (match.getParticipant(event.getPlayer()) == null) {
Expand Down

0 comments on commit 5226615

Please sign in to comment.