Skip to content

Commit e1e9f76

Browse files
committed
Fixed gun unloading
1 parent 63a2e4e commit e1e9f76

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

src/main/java/me/zombie_striker/qg/listener/QAListener.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -466,39 +466,35 @@ public void onPickup(PlayerPickupItemEvent e) {
466466

467467
@EventHandler(priority = EventPriority.LOW)
468468
public void onDropReload(PlayerDropItemEvent e) {
469-
Gun g = QualityArmory.getGun(e.getItemDrop().getItemStack());
470-
if (g == null) {
471-
return;
472-
}
469+
ItemStack droppedItem = e.getItemDrop().getItemStack();
470+
471+
Gun g = QualityArmory.getGun(droppedItem);
472+
if (g == null && QualityArmory.isIronSights(droppedItem))
473+
g = QualityArmory.getGun(e.getPlayer().getInventory().getItemInOffHand());
474+
475+
if (g == null) return;
473476

477+
Gun finalG = g;
474478
if (QAMain.unloadOnQ) {
475-
// Unload mode: Q removes bullets from magazine
476479
e.setCancelled(true);
477-
478-
// Prevent the gun from firing during unload
480+
479481
ignoreClick.add(e.getPlayer().getUniqueId());
482+
480483
Bukkit.getScheduler().runTaskLater(QAMain.getInstance(), () -> {
481484
ignoreClick.remove(e.getPlayer().getUniqueId());
485+
486+
if (e.getPlayer().isSneaking()) unloadAll(e.getPlayer(), finalG);
487+
else unloadOne(e.getPlayer(), finalG);
482488
}, 2L);
483-
484-
if (e.getPlayer().isSneaking()) {
485-
// Remove all bullets from magazine
486-
unloadAll(e.getPlayer(), g);
487-
} else {
488-
// Remove one bullet from magazine
489-
unloadOne(e.getPlayer(), g);
490-
}
491489
} else if (QAMain.reloadOnQ && !QAMain.reloadOnFOnly) {
492-
// Original reload mode
493490
e.setCancelled(true);
494491

495-
// Prevent the gun from firing during reload
496492
ignoreClick.add(e.getPlayer().getUniqueId());
493+
497494
Bukkit.getScheduler().runTaskLater(QAMain.getInstance(), () -> {
498495
ignoreClick.remove(e.getPlayer().getUniqueId());
496+
reload(e.getPlayer(), finalG);
499497
}, 2L);
500-
501-
reload(e.getPlayer(), g);
502498
}
503499
}
504500

@@ -511,14 +507,15 @@ public static void reload(Player player, Gun g) {
511507
public static void unloadOne(Player player, Gun g) {
512508
int currentAmmo = Gun.getAmount(player);
513509
if (currentAmmo <= 0) {
514-
return; // No ammo to unload
510+
QAMain.DEBUG("No ammo to unload");
511+
return;
515512
}
516513

517-
// Remove one bullet from the gun
514+
QAMain.DEBUG("Unloading one bullet from " + g.getDisplayName());
518515
Gun.updateAmmo(g, player, currentAmmo - 1);
519516

520-
// Return one bullet to player inventory (drops if full)
521517
if (g.getAmmoType() != null) {
518+
QAMain.DEBUG("Returning one bullet to player inventory");
522519
QualityArmory.addAmmoToInventory(player, g.getAmmoType(), 1);
523520
}
524521

@@ -528,22 +525,22 @@ public static void unloadOne(Player player, Gun g) {
528525
public static void unloadAll(Player player, Gun g) {
529526
int currentAmmo = Gun.getAmount(player);
530527
if (currentAmmo <= 0) {
531-
return; // No ammo to unload
528+
QAMain.DEBUG("No ammo to unload");
529+
return;
532530
}
533531

534-
// Remove all bullets from the gun
532+
QAMain.DEBUG("Unloading all ammo from " + g.getDisplayName());
535533
Gun.updateAmmo(g, player, 0);
536534

537-
// Return all bullets to player inventory (drops if full)
538535
if (g.getAmmoType() != null) {
536+
QAMain.DEBUG("Returning all ammo to player inventory");
539537
QualityArmory.addAmmoToInventory(player, g.getAmmoType(), currentAmmo);
540538
}
541539

542540
playUnloadSound(player);
543541
}
544542

545543
private static void playUnloadSound(Player player) {
546-
// Play reload sound with lower pitch (0.8) to distinguish from reloading
547544
try {
548545
player.getWorld().playSound(player.getLocation(), WeaponSounds.RELOAD_MAG_OUT.getSoundName(), 1, 0.8f);
549546
} catch (Error e2) {

src/main/resources/config.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ verboseItemLogging: false
7272
enableIgnoreArmorProtection: false
7373
enableReloadingWhenSwapToOffhand: true
7474
enableReloadOnlyWhenSwapToOffhand: false
75-
enableUnloadingOnDrop: false
7675
allowGunHitEntities: true
7776
showOutOfAmmoOnTitle: false
7877
showReloadingTitle: false

wiki/config/main.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,5 @@ Below you can find a list of all the options that you can configure and their ex
9898
* **weaponSwitchDelay**: `0` - The delay in seconds before a player can fire after switching weapon. A value of `0` means no delay.
9999
* **DefaultResourcepack**: Defines the resource pack URLs. Refer to [ResourcePack Configuration](resourcepack.md) for more information.
100100
* **restoreOffHand**: `false` - If `true`, restores the off-hand item after iron sights are unaimed.
101-
* **hitDistance**: `5` - The maximum distance (in blocks) at which a gun can register melee hits on entities when using the gun to hit directly.
101+
* **hitDistance**: `5` - The maximum distance (in blocks) at which a gun can register melee hits on entities when using the gun to hit directly.
102+
* **enableUnloadingOnDrop**: `false` - If `true`, allows players to unload their gun's magazine by pressing their drop key (default 'Q') while holding a gun.

0 commit comments

Comments
 (0)