Skip to content

Commit

Permalink
fix: sword blocking shield ending up in inv on world change
Browse files Browse the repository at this point in the history
Fixes #753
  • Loading branch information
kernitus committed Oct 28, 2024
1 parent cbc0c4b commit 8aa3fa3
Showing 1 changed file with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ModuleSwordBlocking extends OCMModule {
public ModuleSwordBlocking(OCMMain plugin) {
super(plugin, "sword-blocking");

if(!Reflector.versionIsNewerOrEqualTo(1,13,0)){
if (!Reflector.versionIsNewerOrEqualTo(1, 13, 0)) {
lastInteractedBlocks = new WeakHashMap<>();
}
}
Expand All @@ -53,17 +53,16 @@ public void reload() {

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

Player player;

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

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

Expand All @@ -82,10 +81,10 @@ public void onRightClick(PlayerInteractEvent e) {
// 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()){
if(lastInteractedBlocks != null) {
if (e.isBlockInHand()) {
if (lastInteractedBlocks != null) {
final Block clickedBlock = e.getClickedBlock();
if(clickedBlock != null)
if (clickedBlock != null)
lastInteractedBlocks.put(clickedBlock.getLocation(), player.getUniqueId());
}
return; // Handle failed block place in separate listener
Expand All @@ -100,7 +99,7 @@ private void doShieldBlock(Player player) {
final ItemStack mainHandItem = inventory.getItemInMainHand();
final ItemStack offHandItem = inventory.getItemInOffHand();

if(!isHoldingSword(mainHandItem.getType())) return;
if (!isHoldingSword(mainHandItem.getType())) return;

if (module().getBoolean("use-permission") &&
!player.hasPermission("oldcombatmechanics.swordblock")) return;
Expand All @@ -119,17 +118,17 @@ private void doShieldBlock(Player player) {

@EventHandler
public void onHotBarChange(PlayerItemHeldEvent e) {
restore(e.getPlayer());
restore(e.getPlayer(), true);
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onWorldChange(PlayerChangedWorldEvent e) {
restore(e.getPlayer());
restore(e.getPlayer(), true);
}

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerLogout(PlayerQuitEvent e) {
restore(e.getPlayer());
restore(e.getPlayer(), true);
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand All @@ -138,13 +137,14 @@ public void onPlayerDeath(PlayerDeathEvent e) {
final UUID id = p.getUniqueId();
if (!areItemsStored(id)) return;

//TODO what if they legitimately had a shield?
e.getDrops().replaceAll(item ->
item.getType() == Material.SHIELD ?
storedItems.remove(id) : item
);

// Handle keepInventory = true
restore(p);
restore(p, true);
}

@EventHandler(priority = EventPriority.HIGHEST)
Expand All @@ -155,16 +155,14 @@ public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent e) {

@EventHandler(priority = EventPriority.HIGHEST)
public void onInventoryClick(InventoryClickEvent e) {
if (e.getWhoClicked() instanceof Player) {
final Player player = (Player) e.getWhoClicked();

if (e.getWhoClicked() instanceof Player player) {
if (areItemsStored(player.getUniqueId())) {
final ItemStack cursor = e.getCursor();
final ItemStack current = e.getCurrentItem();
if (cursor != null && cursor.getType() == Material.SHIELD ||
current != null && current.getType() == Material.SHIELD) {
e.setCancelled(true);
restore(player);
restore(player, true);
}
}
}
Expand All @@ -181,15 +179,19 @@ public void onItemDrop(PlayerDropItemEvent e) {
}
}

private void restore(Player p) {
private void restore(Player player) {
restore(player, false);
}

private void restore(Player p, boolean force) {
final UUID id = p.getUniqueId();

tryCancelTask(id);

// If they are still blocking with the shield, postpone restoring
if (!areItemsStored(id)) return;

if (isPlayerBlocking(p)) scheduleRestore(p);
// If they are still blocking with the shield, postpone restoring
if (!force && isPlayerBlocking(p)) scheduleRestore(p);
else p.getInventory().setItemInOffHand(storedItems.remove(id));
}

Expand All @@ -207,9 +209,9 @@ private void scheduleRestore(Player p) {

final BukkitTask checkBlocking = Bukkit.getScheduler()
.runTaskTimer(plugin, () -> {
if (!isPlayerBlocking(p))
restore(p);
}, 10L, 2L);
if (!isPlayerBlocking(p))
restore(p);
}, 10L, 2L);

final List<BukkitTask> tasks = new ArrayList<>(2);
tasks.add(removeItem);
Expand Down

0 comments on commit 8aa3fa3

Please sign in to comment.