Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.BukkitWorldConfiguration;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.bukkit.listener.debounce.BlockRedstoneKey;
import com.sk89q.worldguard.bukkit.listener.debounce.EventDebounce;
import com.sk89q.worldguard.bukkit.util.Materials;
import com.sk89q.worldguard.config.ConfigurationManager;
import com.sk89q.worldguard.config.WorldConfiguration;
Expand Down Expand Up @@ -66,6 +68,7 @@
*/
public class WorldGuardBlockListener extends AbstractListener {

private final EventDebounce<BlockRedstoneKey> redstoneDebounce = EventDebounce.create(50);

/**
* Construct the object.
Expand Down Expand Up @@ -409,6 +412,8 @@ public void onBlockPlace(BlockPlaceEvent event) {

/*
* Called when redstone changes.
* Debounced to avoid server lag from rapid redstone fluctuations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment change here probably shouldn't be done

* (e.g. 300 arrows on a pressure plate triggering repeated sponge checks).
*/
@EventHandler(priority = EventPriority.HIGH)
public void onBlockRedstoneChange(BlockRedstoneEvent event) {
Expand All @@ -418,6 +423,21 @@ public void onBlockRedstoneChange(BlockRedstoneEvent event) {
WorldConfiguration wcfg = getWorldConfig(world);

if (wcfg.simulateSponge && wcfg.redstoneSponges) {
boolean oldPowered = event.getOldCurrent() != 0;
boolean newPowered = event.getNewCurrent() != 0;
if (oldPowered == newPowered) {
return;
}

BlockRedstoneKey key = new BlockRedstoneKey(blockTo, newPowered);
// BlockRedstoneEvent is not Cancellable, so use the
// non-cancellable overload to skip the 27-block sponge
// search when the same transition has been checked within
// the debounce window.
if (redstoneDebounce.getIfNotPresent(key) == null) {
return;
}

int ox = blockTo.getX();
int oy = blockTo.getY();
int oz = blockTo.getZ();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.worldguard.bukkit.listener.debounce;

import org.bukkit.block.Block;

import java.util.Objects;

/**
* A key for debouncing BlockRedstoneEvents.
*
* Identifies a redstone change by the block location and new power state, so
* that repeated events for the same transition are coalesced into a single
* sponge check.
*/
public class BlockRedstoneKey {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you able to please swap this to a record?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel we should also be encoding the power levels in here, as otherwise we could be losing information about "turn on / turn off" – and purely seeing it as a "turn on"


private final String worldName;
private final int x;
private final int y;
private final int z;
private final boolean powered;

public BlockRedstoneKey(Block block, boolean powered) {
this.worldName = block.getWorld().getName();
this.x = block.getX();
this.y = block.getY();
this.z = block.getZ();
this.powered = powered;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BlockRedstoneKey)) return false;
BlockRedstoneKey that = (BlockRedstoneKey) o;
return x == that.x && y == that.y && z == that.z
&& powered == that.powered && worldName.equals(that.worldName);
}

@Override
public int hashCode() {
return Objects.hash(worldName, x, y, z, powered);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,20 @@ public <T extends Event & Cancellable> Entry getIfNotPresent(K key, Cancellable
}
}

/**
* Non-cancellable variant: returns an Entry if this key has not been
* seen within the debounce window, or null if the key is already cached.
*/
@Nullable
public Entry getIfNotPresent(K key) {
Entry entry = cache.getUnchecked(key);
if (entry.cancelled != null) {
return null;
}
entry.cancelled = false;
return entry;
}

public static <K> EventDebounce<K> create(int debounceTime) {
return new EventDebounce<>(debounceTime);
}
Expand Down