Skip to content

Commit

Permalink
Get player on block build for sword blocking <1.13. Fixes #719
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Apr 27, 2024
1 parent dab1e02 commit 74af876
Showing 1 changed file with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import kernitus.plugin.OldCombatMechanics.OCMMain;
import kernitus.plugin.OldCombatMechanics.utilities.reflection.Reflector;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
Expand All @@ -33,8 +34,15 @@ public class ModuleSwordBlocking extends OCMModule {
private final Map<UUID, Collection<BukkitTask>> correspondingTasks = new HashMap<>();
private int restoreDelay;

// Only used <1.13, where BlockCanBuildEvent.getPlayer() is not available
private Map<Location, UUID> lastInteractedBlocks;

public ModuleSwordBlocking(OCMMain plugin) {
super(plugin, "sword-blocking");

if(!Reflector.versionIsNewerOrEqualAs(1,13,0)){
lastInteractedBlocks = new WeakHashMap<>();
}
}

@Override
Expand All @@ -44,10 +52,21 @@ public void reload() {

@EventHandler(priority = EventPriority.HIGHEST)
public void onBlockPlace(BlockCanBuildEvent e) {
final Player player = e.getPlayer();
if(e.isBuildable()) return;

Player player;

// If <1.13 get player who last interacted with block
if(lastInteractedBlocks != null) {
final Location blockLocation = e.getBlock().getLocation();
final UUID uuid = lastInteractedBlocks.remove(blockLocation);
player = Bukkit.getServer().getPlayer(uuid);
}
else player = e.getPlayer();

if (player == null || !isEnabled(player)) return;

if (!e.isBuildable()) doShieldBlock(player);
doShieldBlock(player);
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand All @@ -57,11 +76,18 @@ public void onRightClick(PlayerInteractEvent e) {

if (!isEnabled(player)) return;

debug("Action: " + action + " hand: " + e.getHand(), player);

if (action != Action.RIGHT_CLICK_BLOCK && action != Action.RIGHT_CLICK_AIR) return;
// If they clicked on an interactive block, the 2nd event with the offhand won't fire
// This is also the case if the main hand item was used, e.g. a bow
// TODO right-clicking on a mob also only fires one hand
if (action == Action.RIGHT_CLICK_BLOCK && e.getHand() == EquipmentSlot.HAND) return;
if (e.isBlockInHand()) return; // Handle failed block place in separate listener
if (e.isBlockInHand()){
if(lastInteractedBlocks != null)
lastInteractedBlocks.put(e.getClickedBlock().getLocation(), player.getUniqueId());
return; // Handle failed block place in separate listener
}

doShieldBlock(player);
}
Expand Down

0 comments on commit 74af876

Please sign in to comment.